diff --git a/lib/types/sqlite-types.js b/lib/types/sqlite-types.js index 2ef9ca8a3..ff4d08a40 100644 --- a/lib/types/sqlite-types.js +++ b/lib/types/sqlite-types.js @@ -1,67 +1,73 @@ // @flow import type { ClientDBMessageInfo } from './message-types.js'; import type { StoreOperations } from './store-ops-types.js'; export const outboundP2PMessageStatuses = Object.freeze({ // The message was prepared to be sent to other peers, but it's not encrypted. // It was inserted into DB in the same transaction as making changes to // the store. persisted: 'persisted', // Encryption is done in the same transaction as persisting the CryptoModule, // and message order is also tracked on the client side, // which means the message can be sent. encrypted: 'encrypted', // The message was sent to another peer (Tunnelbroker owns it), // waiting for the confirmation (handled in `peerToPeerMessageHandler`). sent: 'sent', }); export type OutboundP2PMessageStatuses = $Values< typeof outboundP2PMessageStatuses, >; export type InboundP2PMessage = { +messageID: string, +senderDeviceID: string, +plaintext: string, +status: string, }; export type OutboundP2PMessage = { +messageID: string, +deviceID: string, +userID: string, +timestamp: string, +plaintext: string, +ciphertext: string, +status: OutboundP2PMessageStatuses, +supportsAutoRetry: boolean, }; export type SQLiteAPI = { // read operations +getAllInboundP2PMessages: () => Promise>, +getAllOutboundP2PMessages: () => Promise>, +getRelatedMessages: ( messageID: string, ) => Promise>, +getOutboundP2PMessagesByID: ( ids: $ReadOnlyArray, ) => Promise>, + +searchMessages: ( + query: string, + threadID: string, + timestampCursor: ?string, + messageIDCursor: ?string, + ) => Promise>, // write operations +removeInboundP2PMessages: (ids: $ReadOnlyArray) => Promise, +markOutboundP2PMessageAsSent: ( messageID: string, deviceID: string, ) => Promise, +removeOutboundP2PMessagesOlderThan: ( messageID: string, deviceID: string, ) => Promise, +processDBStoreOperations: ( operations: StoreOperations, userID?: ?string, ) => Promise, }; diff --git a/lib/utils/__mocks__/config.js b/lib/utils/__mocks__/config.js index d716486c9..78d04437b 100644 --- a/lib/utils/__mocks__/config.js +++ b/lib/utils/__mocks__/config.js @@ -1,45 +1,46 @@ // @flow import { type Config } from '../config.js'; const getConfig = (): Config => ({ resolveKeyserverSessionInvalidationUsingNativeCredentials: null, setSessionIDOnRequest: true, calendarRangeInactivityLimit: null, platformDetails: { platform: 'web', codeVersion: 70, stateVersion: 50, }, authoritativeKeyserverID: '123', olmAPI: { initializeCryptoAccount: jest.fn(), getUserPublicKey: jest.fn(), encrypt: jest.fn(), encryptAndPersist: jest.fn(), decrypt: jest.fn(), decryptSequentialAndPersist: jest.fn(), contentInboundSessionCreator: jest.fn(), contentOutboundSessionCreator: jest.fn(), notificationsSessionCreator: jest.fn(), getOneTimeKeys: jest.fn(), validateAndUploadPrekeys: jest.fn(), signMessage: jest.fn(), verifyMessage: jest.fn(), markPrekeysAsPublished: jest.fn(), }, sqliteAPI: { getAllInboundP2PMessages: jest.fn(), removeInboundP2PMessages: jest.fn(), processDBStoreOperations: jest.fn(), getAllOutboundP2PMessages: jest.fn(), markOutboundP2PMessageAsSent: jest.fn(), removeOutboundP2PMessagesOlderThan: jest.fn(), getRelatedMessages: jest.fn(), getOutboundP2PMessagesByID: jest.fn(), + searchMessages: jest.fn(), }, }); const hasConfig = (): boolean => true; export { getConfig, hasConfig }; diff --git a/native/database/sqlite-api.js b/native/database/sqlite-api.js index 3c1e9bf5d..3e3400c40 100644 --- a/native/database/sqlite-api.js +++ b/native/database/sqlite-api.js @@ -1,24 +1,25 @@ // @flow import type { SQLiteAPI } from 'lib/types/sqlite-types.js'; import { commCoreModule } from '../native-modules.js'; import { processDBStoreOperations } from '../redux/redux-utils.js'; const sqliteAPI: SQLiteAPI = { // read operations getAllInboundP2PMessages: commCoreModule.getAllInboundP2PMessages, getAllOutboundP2PMessages: commCoreModule.getAllOutboundP2PMessages, getRelatedMessages: commCoreModule.getRelatedMessages, getOutboundP2PMessagesByID: commCoreModule.getOutboundP2PMessagesByID, + searchMessages: commCoreModule.searchMessages, // write operations removeInboundP2PMessages: commCoreModule.removeInboundP2PMessages, markOutboundP2PMessageAsSent: commCoreModule.markOutboundP2PMessageAsSent, removeOutboundP2PMessagesOlderThan: commCoreModule.removeOutboundP2PMessagesOlderThan, processDBStoreOperations, }; export { sqliteAPI }; diff --git a/web/database/sqlite-api.js b/web/database/sqlite-api.js index 8e771775c..d6dde8adc 100644 --- a/web/database/sqlite-api.js +++ b/web/database/sqlite-api.js @@ -1,102 +1,121 @@ // @flow import type { ClientDBMessageInfo } from 'lib/types/message-types.js'; import type { SQLiteAPI, InboundP2PMessage, OutboundP2PMessage, } from 'lib/types/sqlite-types.js'; import { getCommSharedWorker } from '../shared-worker/shared-worker-provider.js'; import { processDBStoreOperations } from '../shared-worker/utils/store.js'; import { workerRequestMessageTypes } from '../types/worker-types.js'; const sqliteAPI: SQLiteAPI = { // read operations async getAllInboundP2PMessages(): Promise { const sharedWorker = await getCommSharedWorker(); const data = await sharedWorker.schedule({ type: workerRequestMessageTypes.GET_INBOUND_P2P_MESSAGES, }); const messages: ?$ReadOnlyArray = data?.inboundP2PMessages; return messages ? [...messages] : []; }, async getAllOutboundP2PMessages(): Promise { const sharedWorker = await getCommSharedWorker(); const data = await sharedWorker.schedule({ type: workerRequestMessageTypes.GET_OUTBOUND_P2P_MESSAGES, }); const messages: ?$ReadOnlyArray = data?.outboundP2PMessages; return messages ? [...messages] : []; }, async getRelatedMessages(messageID: string): Promise { const sharedWorker = await getCommSharedWorker(); const data = await sharedWorker.schedule({ type: workerRequestMessageTypes.GET_RELATED_MESSAGES, messageID, }); const messages: ?$ReadOnlyArray = data?.messages; return messages ? [...messages] : []; }, async getOutboundP2PMessagesByID( ids: $ReadOnlyArray, ): Promise> { const sharedWorker = await getCommSharedWorker(); const data = await sharedWorker.schedule({ type: workerRequestMessageTypes.GET_OUTBOUND_P2P_MESSAGES_BY_ID, messageIDs: ids, }); const messages: ?$ReadOnlyArray = data?.outboundP2PMessages; return messages ? [...messages] : []; }, + async searchMessages( + query: string, + threadID: string, + timestampCursor: ?string, + messageIDCursor: ?string, + ): Promise { + const sharedWorker = await getCommSharedWorker(); + + const data = await sharedWorker.schedule({ + type: workerRequestMessageTypes.SEARCH_MESSAGES, + query, + threadID, + timestampCursor, + messageIDCursor, + }); + const messages: ?$ReadOnlyArray = data?.messages; + return messages ? [...messages] : []; + }, + // write operations async removeInboundP2PMessages(ids: $ReadOnlyArray): Promise { const sharedWorker = await getCommSharedWorker(); await sharedWorker.schedule({ type: workerRequestMessageTypes.REMOVE_INBOUND_P2P_MESSAGES, ids, }); }, async markOutboundP2PMessageAsSent( messageID: string, deviceID: string, ): Promise { const sharedWorker = await getCommSharedWorker(); await sharedWorker.schedule({ type: workerRequestMessageTypes.MARK_OUTBOUND_P2P_MESSAGE_AS_SENT, messageID, deviceID, }); }, async removeOutboundP2PMessagesOlderThan( messageID: string, deviceID: string, ): Promise { const sharedWorker = await getCommSharedWorker(); await sharedWorker.schedule({ type: workerRequestMessageTypes.REMOVE_OUTBOUND_P2P_MESSAGES, messageID, deviceID, }); }, processDBStoreOperations, }; export { sqliteAPI };