// Copyright 2020-2021 Signal Messenger, LLC // SPDX-License-Identifier: AGPL-3.0-only /* eslint-disable @typescript-eslint/ban-types */ /* eslint-disable camelcase */ /* eslint-disable @typescript-eslint/no-explicit-any */ import type { ConversationAttributesType, ConversationModelCollectionType, MessageAttributesType, MessageModelCollectionType, } from '../model-types.d'; import type { MessageModel } from '../models/messages'; import type { ConversationModel } from '../models/conversations'; import type { StoredJob } from '../jobs/types'; import type { ReactionType } from '../types/Reactions'; import type { ConversationColorType, CustomColorType } from '../types/Colors'; import type { ProcessGroupCallRingRequestResult } from '../types/Calling'; import type { StorageAccessType } from '../types/Storage.d'; import type { AttachmentType } from '../types/Attachment'; import type { BodyRangesType } from '../types/Util'; import type { QualifiedAddressStringType } from '../types/QualifiedAddress'; import type { UUIDStringType } from '../types/UUID'; import type { BadgeType } from '../badges/types'; import type { RemoveAllConfiguration } from '../types/RemoveAllConfiguration'; import type { LoggerType } from '../types/Logging'; export type AttachmentDownloadJobTypeType = | 'long-message' | 'attachment' | 'preview' | 'contact' | 'quote' | 'sticker'; export type AttachmentDownloadJobType = { attachment: AttachmentType; attempts: number; id: string; index: number; messageId: string; pending: number; timestamp: number; type: AttachmentDownloadJobTypeType; }; export type MessageMetricsType = { id: string; // eslint-disable-next-line camelcase received_at: number; // eslint-disable-next-line camelcase sent_at: number; }; export type ConversationMetricsType = { oldest?: MessageMetricsType; newest?: MessageMetricsType; oldestUnread?: MessageMetricsType; totalUnread: number; }; export type ConversationType = ConversationAttributesType; export type EmojiType = { shortName: string; lastUsage: number; }; export type IdentityKeyType = { firstUse: boolean; id: UUIDStringType | `conversation:${string}`; nonblockingApproval: boolean; publicKey: Uint8Array; timestamp: number; verified: number; }; export type IdentityKeyIdType = IdentityKeyType['id']; export type ItemKeyType = keyof StorageAccessType; export type AllItemsType = Partial; export type ItemType = { id: K; value: StorageAccessType[K]; }; export type MessageType = MessageAttributesType; export type MessageTypeUnhydrated = { json: string; }; export type PreKeyType = { id: `${UUIDStringType}:${number}`; keyId: number; ourUuid: UUIDStringType; privateKey: Uint8Array; publicKey: Uint8Array; }; export type PreKeyIdType = PreKeyType['id']; export type SearchResultMessageType = { json: string; snippet: string; }; export type ClientSearchResultMessageType = MessageType & { json: string; bodyRanges: BodyRangesType; snippet: string; }; export type SentProtoType = { contentHint: number; proto: Uint8Array; timestamp: number; }; export type SentProtoWithMessageIdsType = SentProtoType & { messageIds: Array; }; export type SentRecipientsType = Record>; export type SentMessagesType = Array; // These two are for test only export type SentRecipientsDBType = { payloadId: number; recipientUuid: string; deviceId: number; }; export type SentMessageDBType = { payloadId: number; messageId: string; }; export type SenderKeyType = { // Primary key id: `${QualifiedAddressStringType}--${string}`; // These two are combined into one string to give us the final id senderId: string; distributionId: string; // Raw data to serialize/deserialize into signal-client SenderKeyRecord data: Uint8Array; lastUpdatedDate: number; }; export type SenderKeyIdType = SenderKeyType['id']; export type SessionType = { id: QualifiedAddressStringType; ourUuid: UUIDStringType; uuid: UUIDStringType; conversationId: string; deviceId: number; record: string; version?: number; }; export type SessionIdType = SessionType['id']; export type SignedPreKeyType = { confirmed: boolean; // eslint-disable-next-line camelcase created_at: number; ourUuid: UUIDStringType; id: `${UUIDStringType}:${number}`; keyId: number; privateKey: Uint8Array; publicKey: Uint8Array; }; export type SignedPreKeyIdType = SignedPreKeyType['id']; export type StickerType = Readonly<{ id: number; packId: string; emoji?: string; isCoverOnly: boolean; lastUsed?: number; path: string; width: number; height: number; }>; export const StickerPackStatuses = [ 'known', 'ephemeral', 'downloaded', 'installed', 'pending', 'error', ] as const; export type StickerPackStatusType = typeof StickerPackStatuses[number]; export type StickerPackType = Readonly<{ id: string; key: string; attemptedStatus?: 'downloaded' | 'installed' | 'ephemeral'; author: string; coverStickerId: number; createdAt: number; downloadAttempts: number; installedAt?: number; lastUsed?: number; status: StickerPackStatusType; stickerCount: number; stickers: Record; title: string; }>; export type UnprocessedType = { id: string; timestamp: number; version: number; attempts: number; envelope?: string; source?: string; sourceUuid?: string; sourceDevice?: number; serverGuid?: string; serverTimestamp?: number; decrypted?: string; }; export type UnprocessedUpdateType = { source?: string; sourceUuid?: string; sourceDevice?: number; serverGuid?: string; serverTimestamp?: number; decrypted?: string; }; export type LastConversationMessagesServerType = { activity?: MessageType; preview?: MessageType; hasUserInitiatedMessages: boolean; }; export type LastConversationMessagesType = { activity?: MessageModel; preview?: MessageModel; hasUserInitiatedMessages: boolean; }; export type DeleteSentProtoRecipientOptionsType = Readonly<{ timestamp: number; recipientUuid: string; deviceId: number; }>; export type DataInterface = { close: () => Promise; removeDB: () => Promise; removeIndexedDBFiles: () => Promise; createOrUpdateIdentityKey: (data: IdentityKeyType) => Promise; getIdentityKeyById: ( id: IdentityKeyIdType ) => Promise; bulkAddIdentityKeys: (array: Array) => Promise; removeIdentityKeyById: (id: IdentityKeyIdType) => Promise; removeAllIdentityKeys: () => Promise; getAllIdentityKeys: () => Promise>; createOrUpdatePreKey: (data: PreKeyType) => Promise; getPreKeyById: (id: PreKeyIdType) => Promise; bulkAddPreKeys: (array: Array) => Promise; removePreKeyById: (id: PreKeyIdType) => Promise; removeAllPreKeys: () => Promise; getAllPreKeys: () => Promise>; createOrUpdateSignedPreKey: (data: SignedPreKeyType) => Promise; getSignedPreKeyById: ( id: SignedPreKeyIdType ) => Promise; bulkAddSignedPreKeys: (array: Array) => Promise; removeSignedPreKeyById: (id: SignedPreKeyIdType) => Promise; removeAllSignedPreKeys: () => Promise; getAllSignedPreKeys: () => Promise>; createOrUpdateItem(data: ItemType): Promise; getItemById(id: K): Promise | undefined>; removeItemById: (id: ItemKeyType) => Promise; removeAllItems: () => Promise; getAllItems: () => Promise; createOrUpdateSenderKey: (key: SenderKeyType) => Promise; getSenderKeyById: (id: SenderKeyIdType) => Promise; removeAllSenderKeys: () => Promise; getAllSenderKeys: () => Promise>; removeSenderKeyById: (id: SenderKeyIdType) => Promise; insertSentProto: ( proto: SentProtoType, options: { recipients: SentRecipientsType; messageIds: SentMessagesType; } ) => Promise; deleteSentProtosOlderThan: (timestamp: number) => Promise; deleteSentProtoByMessageId: (messageId: string) => Promise; insertProtoRecipients: (options: { id: number; recipientUuid: string; deviceIds: Array; }) => Promise; deleteSentProtoRecipient: ( options: | DeleteSentProtoRecipientOptionsType | ReadonlyArray ) => Promise; getSentProtoByRecipient: (options: { now: number; recipientUuid: string; timestamp: number; }) => Promise; removeAllSentProtos: () => Promise; getAllSentProtos: () => Promise>; // Test-only _getAllSentProtoRecipients: () => Promise>; _getAllSentProtoMessageIds: () => Promise>; createOrUpdateSession: (data: SessionType) => Promise; createOrUpdateSessions: (array: Array) => Promise; commitSessionsAndUnprocessed(options: { sessions: Array; unprocessed: Array; }): Promise; bulkAddSessions: (array: Array) => Promise; removeSessionById: (id: SessionIdType) => Promise; removeSessionsByConversation: (conversationId: string) => Promise; removeAllSessions: () => Promise; getAllSessions: () => Promise>; eraseStorageServiceStateFromConversations: () => Promise; getConversationCount: () => Promise; saveConversation: (data: ConversationType) => Promise; saveConversations: (array: Array) => Promise; updateConversations: (array: Array) => Promise; getAllConversationIds: () => Promise>; searchConversations: ( query: string, options?: { limit?: number } ) => Promise>; getMessagesById: (messageIds: Array) => Promise>; saveMessage: ( data: MessageType, options?: { jobToInsert?: StoredJob; forceSave?: boolean; } ) => Promise; saveMessages: ( arrayOfMessages: Array, options?: { forceSave?: boolean } ) => Promise; getMessageCount: (conversationId?: string) => Promise; getAllMessageIds: () => Promise>; getMessageMetricsForConversation: ( conversationId: string ) => Promise; hasGroupCallHistoryMessage: ( conversationId: string, eraId: string ) => Promise; migrateConversationMessages: ( obsoleteId: string, currentId: string ) => Promise; getNextTapToViewMessageTimestampToAgeOut: () => Promise; getUnreadCountForConversation: (conversationId: string) => Promise; getUnreadByConversationAndMarkRead: ( conversationId: string, newestUnreadId: number, readAt?: number ) => Promise< Array< Pick > >; getUnreadReactionsAndMarkRead: ( conversationId: string, newestUnreadId: number ) => Promise< Array< Pick > >; markReactionAsRead: ( targetAuthorUuid: string, targetTimestamp: number ) => Promise; removeReactionFromConversation: (reaction: { emoji: string; fromId: string; targetAuthorUuid: string; targetTimestamp: number; }) => Promise; addReaction: (reactionObj: ReactionType) => Promise; _getAllReactions: () => Promise>; getUnprocessedCount: () => Promise; getAllUnprocessed: () => Promise>; updateUnprocessedWithData: ( id: string, data: UnprocessedUpdateType ) => Promise; updateUnprocessedsWithData: ( array: Array<{ id: string; data: UnprocessedUpdateType }> ) => Promise; getUnprocessedById: (id: string) => Promise; removeUnprocessed: (id: string | Array) => Promise; removeAllUnprocessed: () => Promise; getNextAttachmentDownloadJobs: ( limit?: number, options?: { timestamp?: number } ) => Promise>; saveAttachmentDownloadJob: (job: AttachmentDownloadJobType) => Promise; setAttachmentDownloadJobPending: ( id: string, pending: boolean ) => Promise; resetAttachmentDownloadPending: () => Promise; removeAttachmentDownloadJob: (id: string) => Promise; removeAllAttachmentDownloadJobs: () => Promise; createOrUpdateStickerPack: (pack: StickerPackType) => Promise; updateStickerPackStatus: ( id: string, status: StickerPackStatusType, options?: { timestamp: number } ) => Promise; createOrUpdateSticker: (sticker: StickerType) => Promise; updateStickerLastUsed: ( packId: string, stickerId: number, lastUsed: number ) => Promise; addStickerPackReference: (messageId: string, packId: string) => Promise; deleteStickerPackReference: ( messageId: string, packId: string ) => Promise | undefined>; getStickerCount: () => Promise; deleteStickerPack: (packId: string) => Promise>; getAllStickerPacks: () => Promise>; getAllStickers: () => Promise>; getRecentStickers: (options?: { limit?: number; }) => Promise>; clearAllErrorStickerPackAttempts: () => Promise; updateEmojiUsage: (shortName: string, timeUsed?: number) => Promise; getRecentEmojis: (limit?: number) => Promise>; getAllBadges(): Promise>; updateOrCreateBadges(badges: ReadonlyArray): Promise; badgeImageFileDownloaded(url: string, localPath: string): Promise; removeAll: () => Promise; removeAllConfiguration: (type?: RemoveAllConfiguration) => Promise; getMessagesNeedingUpgrade: ( limit: number, options: { maxVersion: number } ) => Promise>; getMessagesWithVisualMediaAttachments: ( conversationId: string, options: { limit: number } ) => Promise>; getMessagesWithFileAttachments: ( conversationId: string, options: { limit: number } ) => Promise>; getMessageServerGuidsForSpam: ( conversationId: string ) => Promise>; getMessagesUnexpectedlyMissingExpirationStartTimestamp: () => Promise< Array >; getSoonestMessageExpiry: () => Promise; getJobsInQueue(queueType: string): Promise>; insertJob(job: Readonly): Promise; deleteJob(id: string): Promise; processGroupCallRingRequest( ringId: bigint ): Promise; processGroupCallRingCancelation(ringId: bigint): Promise; cleanExpiredGroupCallRings(): Promise; updateAllConversationColors: ( conversationColor?: ConversationColorType, customColorData?: { id: string; value: CustomColorType; } ) => Promise; getMaxMessageCounter(): Promise; getStatisticsForLogging(): Promise>; }; // The reason for client/server divergence is the need to inject Backbone models and // collections into data calls so those are the objects returned. This was necessary in // July 2018 when creating the Data API as a drop-in replacement for previous database // requests via ORM. // Note: It is extremely important that items are duplicated between these two. Client.js // loops over all of its local functions to generate the server-side IPC-based API. export type ServerInterface = DataInterface & { getAllConversations: () => Promise>; getAllGroupsInvolvingUuid: ( id: UUIDStringType ) => Promise>; getAllPrivateConversations: () => Promise>; getConversationById: (id: string) => Promise; getExpiredMessages: () => Promise>; getMessageById: (id: string) => Promise; getMessageBySender: (options: { source: string; sourceUuid: string; sourceDevice: number; sent_at: number; }) => Promise>; getMessagesBySentAt: (sentAt: number) => Promise>; getOlderMessagesByConversation: ( conversationId: string, options?: { limit?: number; receivedAt?: number; sentAt?: number; messageId?: string; } ) => Promise>; getNewerMessagesByConversation: ( conversationId: string, options?: { limit?: number; receivedAt?: number; sentAt?: number } ) => Promise>; getLastConversationMessages: (options: { conversationId: string; ourUuid: UUIDStringType; }) => Promise; getTapToViewMessagesNeedingErase: () => Promise>; removeConversation: (id: Array | string) => Promise; removeMessage: (id: string) => Promise; removeMessages: (ids: Array) => Promise; searchMessages: ( query: string, options?: { limit?: number } ) => Promise>; searchMessagesInConversation: ( query: string, conversationId: string, options?: { limit?: number } ) => Promise>; updateConversation: (data: ConversationType) => Promise; // For testing only _getAllMessages: () => Promise>; // Server-only getCorruptionLog: () => string; initialize: (options: { configDir: string; key: string; logger: LoggerType; }) => Promise; initializeRenderer: (options: { configDir: string; key: string; }) => Promise; removeKnownAttachments: ( allAttachments: Array ) => Promise>; removeKnownStickers: (allStickers: Array) => Promise>; removeKnownDraftAttachments: ( allStickers: Array ) => Promise>; getAllBadgeImageFileLocalPaths: () => Promise>; }; export type ClientInterface = DataInterface & { getAllConversations: (options: { ConversationCollection: typeof ConversationModelCollectionType; }) => Promise; getAllGroupsInvolvingUuid: ( id: UUIDStringType, options: { ConversationCollection: typeof ConversationModelCollectionType; } ) => Promise; getAllPrivateConversations: (options: { ConversationCollection: typeof ConversationModelCollectionType; }) => Promise; getConversationById: ( id: string, options: { Conversation: typeof ConversationModel } ) => Promise; getExpiredMessages: (options: { MessageCollection: typeof MessageModelCollectionType; }) => Promise; getMessageById: ( id: string, options: { Message: typeof MessageModel } ) => Promise; getMessageBySender: ( data: { source: string; sourceUuid: string; sourceDevice: number; sent_at: number; }, options: { Message: typeof MessageModel } ) => Promise; getMessagesBySentAt: ( sentAt: number, options: { MessageCollection: typeof MessageModelCollectionType } ) => Promise; getOlderMessagesByConversation: ( conversationId: string, options: { limit?: number; messageId?: string; receivedAt?: number; sentAt?: number; MessageCollection: typeof MessageModelCollectionType; } ) => Promise; getNewerMessagesByConversation: ( conversationId: string, options: { limit?: number; receivedAt?: number; sentAt?: number; MessageCollection: typeof MessageModelCollectionType; } ) => Promise; getLastConversationMessages: (options: { conversationId: string; ourUuid: UUIDStringType; Message: typeof MessageModel; }) => Promise; getTapToViewMessagesNeedingErase: (options: { MessageCollection: typeof MessageModelCollectionType; }) => Promise; removeConversation: ( id: string, options: { Conversation: typeof ConversationModel } ) => Promise; removeMessage: ( id: string, options: { Message: typeof MessageModel } ) => Promise; removeMessages: ( ids: Array, options: { Message: typeof MessageModel } ) => Promise; searchMessages: ( query: string, options?: { limit?: number } ) => Promise>; searchMessagesInConversation: ( query: string, conversationId: string, options?: { limit?: number } ) => Promise>; updateConversation: (data: ConversationType, extra?: unknown) => void; // Test-only _getAllMessages: (options: { MessageCollection: typeof MessageModelCollectionType; }) => Promise; // Client-side only shutdown: () => Promise; removeAllMessagesInConversation: ( conversationId: string, options: { logId: string; MessageCollection: typeof MessageModelCollectionType; } ) => Promise; removeOtherData: () => Promise; cleanupOrphanedAttachments: () => Promise; ensureFilePermissions: () => Promise; // Client-side only, and test-only _removeConversations: (ids: Array) => Promise; _jobs: { [id: string]: ClientJobType }; // These are defined on the server-only and used in the client to determine // whether we should use IPC to use the database in the main process or // use the db already running in the renderer. goBackToMainProcess: () => Promise; startInRendererProcess: (isTesting?: boolean) => Promise; }; export type ClientJobType = { fnName: string; start: number; resolve?: Function; reject?: Function; // Only in DEBUG mode complete?: boolean; args?: Array; };