diff --git a/lib/types/sqlite-types.js b/lib/types/sqlite-types.js index ff4d08a40..47096839e 100644 --- a/lib/types/sqlite-types.js +++ b/lib/types/sqlite-types.js @@ -1,73 +1,76 @@ // @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, + +resetOutboundP2PMessagesForDevice: ( + 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 689027f78..b9f223ac7 100644 --- a/lib/utils/__mocks__/config.js +++ b/lib/utils/__mocks__/config.js @@ -1,51 +1,52 @@ // @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(), encryptNotification: jest.fn(), decrypt: jest.fn(), decryptAndPersist: jest.fn(), contentInboundSessionCreator: jest.fn(), contentOutboundSessionCreator: jest.fn(), keyserverNotificationsSessionCreator: jest.fn(), notificationsOutboundSessionCreator: jest.fn(), isContentSessionInitialized: jest.fn(), isDeviceNotificationsSessionInitialized: jest.fn(), isNotificationsSessionInitializedWithDevices: 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(), + resetOutboundP2PMessagesForDevice: 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 3e3400c40..bb9fb6adb 100644 --- a/native/database/sqlite-api.js +++ b/native/database/sqlite-api.js @@ -1,25 +1,27 @@ // @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, + resetOutboundP2PMessagesForDevice: + commCoreModule.resetOutboundP2PMessagesForDevice, removeOutboundP2PMessagesOlderThan: commCoreModule.removeOutboundP2PMessagesOlderThan, processDBStoreOperations, }; export { sqliteAPI }; diff --git a/web/database/sqlite-api.js b/web/database/sqlite-api.js index d6dde8adc..c4db88188 100644 --- a/web/database/sqlite-api.js +++ b/web/database/sqlite-api.js @@ -1,121 +1,134 @@ // @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 resetOutboundP2PMessagesForDevice( + deviceID: string, + ): Promise> { + const sharedWorker = await getCommSharedWorker(); + + const data = await sharedWorker.schedule({ + type: workerRequestMessageTypes.RESET_OUTBOUND_P2P_MESSAGES, + deviceID, + }); + const messageIDs: ?$ReadOnlyArray = data?.messageIDs; + return messageIDs ? [...messageIDs] : []; + }, + 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 };