// 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 { ConversationAttributesType, ConversationModelCollectionType, MessageAttributesType, MessageModelCollectionType, } from '../model-types.d'; import { MessageModel } from '../models/messages'; import { ConversationModel } from '../models/conversations'; import { StoredJob } from '../jobs/types'; import { ReactionType } from '../types/Reactions'; import { ConversationColorType, CustomColorType } from '../types/Colors'; import { StorageAccessType } from '../types/Storage.d'; import { AttachmentType } from '../types/Attachment'; 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: string; nonblockingApproval: boolean; publicKey: ArrayBuffer; timestamp: number; verified: number; }; 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: number; privateKey: ArrayBuffer; publicKey: ArrayBuffer; }; export type SearchResultMessageType = { json: string; snippet: string; }; export type ClientSearchResultMessageType = MessageType & { json: string; bodyRanges: []; snippet: string; }; export type SenderKeyType = { // Primary key id: 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: Buffer; lastUpdatedDate: number; }; export type SessionType = { id: string; conversationId: string; deviceId: number; record: string; version?: number; }; export type SignedPreKeyType = { confirmed: boolean; // eslint-disable-next-line camelcase created_at: number; id: number; privateKey: ArrayBuffer; publicKey: ArrayBuffer; }; export type StickerPackStatusType = | 'known' | 'ephemeral' | 'downloaded' | 'installed' | 'pending' | 'error'; export type StickerType = { id: number; packId: string; emoji: string | null; isCoverOnly: boolean; lastUsed?: number; path: string; width: number; height: number; }; export type StickerPackType = { id: string; key: string; attemptedStatus: 'downloaded' | 'installed' | 'ephemeral'; author: string; coverStickerId: number; createdAt: number; downloadAttempts: number; installedAt: number | null; lastUsed: number; status: StickerPackStatusType; stickerCount: number; stickers: ReadonlyArray; 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?: string; serverGuid?: string; serverTimestamp?: number; decrypted?: string; }; export type DataInterface = { close: () => Promise; removeDB: () => Promise; removeIndexedDBFiles: () => Promise; createOrUpdateIdentityKey: (data: IdentityKeyType) => Promise; getIdentityKeyById: (id: string) => Promise; bulkAddIdentityKeys: (array: Array) => Promise; removeIdentityKeyById: (id: string) => Promise; removeAllIdentityKeys: () => Promise; getAllIdentityKeys: () => Promise>; createOrUpdatePreKey: (data: PreKeyType) => Promise; getPreKeyById: (id: number) => Promise; bulkAddPreKeys: (array: Array) => Promise; removePreKeyById: (id: number) => Promise; removeAllPreKeys: () => Promise; getAllPreKeys: () => Promise>; createOrUpdateSignedPreKey: (data: SignedPreKeyType) => Promise; getSignedPreKeyById: (id: number) => Promise; bulkAddSignedPreKeys: (array: Array) => Promise; removeSignedPreKeyById: (id: number) => 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: string) => Promise; removeAllSenderKeys: () => Promise; getAllSenderKeys: () => Promise>; removeSenderKeyById: (id: string) => Promise; createOrUpdateSession: (data: SessionType) => Promise; createOrUpdateSessions: (array: Array) => Promise; commitSessionsAndUnprocessed(options: { sessions: Array; unprocessed: Array; }): Promise; bulkAddSessions: (array: Array) => Promise; removeSessionById: (id: string) => 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>; getMessageCount: (conversationId?: string) => Promise; hasUserInitiatedMessages: (conversationId: string) => Promise; getAllMessageIds: () => Promise>; getMessageMetricsForConversation: ( conversationId: string ) => Promise; hasGroupCallHistoryMessage: ( conversationId: string, eraId: string ) => Promise; migrateConversationMessages: ( obsoleteId: string, currentId: string ) => Promise; getNextTapToViewMessageTimestampToAgeOut: () => Promise; getUnprocessedCount: () => Promise; getAllUnprocessed: () => Promise>; updateUnprocessedAttempts: (id: string, attempts: number) => 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>; 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>; removeAll: () => Promise; 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; updateAllConversationColors: ( conversationColor?: ConversationColorType, customColorData?: { id: string; value: CustomColorType; } ) => 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>; getAllGroupsInvolvingId: (id: string) => Promise>; getAllPrivateConversations: () => Promise>; getConversationById: (id: string) => Promise; getExpiredMessages: () => Promise>; getMessageById: (id: string) => Promise; getMessageBySender: (options: { source: string; sourceUuid: string; sourceDevice: string; 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>; getLastConversationActivity: (options: { conversationId: string; ourConversationId: string; }) => Promise; getLastConversationPreview: (options: { conversationId: string; ourConversationId: string; }) => Promise; getTapToViewMessagesNeedingErase: () => Promise>; getUnreadCountForConversation: (conversationId: string) => Promise; getUnreadByConversationAndMarkRead: ( conversationId: string, newestUnreadId: number, readAt?: number ) => Promise< Array< Pick > >; getUnreadReactionsAndMarkRead: ( conversationId: string, newestUnreadId: number ) => Promise< Array> >; markReactionAsRead: ( targetAuthorUuid: string, targetTimestamp: number ) => Promise; removeReactionFromConversation: (reaction: { emoji: string; fromId: string; targetAuthorUuid: string; targetTimestamp: number; }) => Promise; addReaction: (reactionObj: ReactionType) => 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>; saveMessage: ( data: MessageType, options: { forceSave?: boolean } ) => Promise; saveMessages: ( arrayOfMessages: Array, options: { forceSave?: boolean } ) => Promise; updateConversation: (data: ConversationType) => Promise; // For testing only _getAllMessages: () => Promise>; // Server-only initialize: (options: { configDir: string; key: string }) => Promise; initializeRenderer: (options: { configDir: string; key: string; }) => Promise; removeKnownAttachments: ( allAttachments: Array ) => Promise>; removeKnownStickers: (allStickers: Array) => Promise>; removeKnownDraftAttachments: ( allStickers: Array ) => Promise>; }; export type ClientInterface = DataInterface & { getAllConversations: (options: { ConversationCollection: typeof ConversationModelCollectionType; }) => Promise; getAllGroupsInvolvingId: ( id: string, 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: string; 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; getLastConversationActivity: (options: { conversationId: string; ourConversationId: string; Message: typeof MessageModel; }) => Promise; getLastConversationPreview: (options: { conversationId: string; ourConversationId: string; Message: typeof MessageModel; }) => Promise; getTapToViewMessagesNeedingErase: (options: { MessageCollection: typeof MessageModelCollectionType; }) => Promise; getUnreadCountForConversation: (conversationId: string) => Promise; getUnreadByConversationAndMarkRead: ( conversationId: string, newestUnreadId: number, readAt?: number ) => Promise< Array< Pick > >; getUnreadReactionsAndMarkRead: ( conversationId: string, newestUnreadId: number ) => Promise< Array> >; markReactionAsRead: ( targetAuthorUuid: string, targetTimestamp: number ) => Promise; removeReactionFromConversation: (reaction: { emoji: string; fromId: string; targetAuthorUuid: string; targetTimestamp: number; }) => Promise; addReaction: (reactionObj: ReactionType) => 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; saveMessage: ( data: MessageType, options: { forceSave?: boolean; Message: typeof MessageModel } ) => Promise; saveMessages: ( arrayOfMessages: Array, options: { forceSave?: boolean; 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; }; export type ClientJobType = { fnName: string; start: number; resolve?: Function; reject?: Function; // Only in DEBUG mode complete?: boolean; args?: Array; };