From bfade45b002254da4f581e6a0a6b164a7670df00 Mon Sep 17 00:00:00 2001 From: Audric Ackermann Date: Mon, 1 Jun 2020 16:28:52 +1000 Subject: [PATCH 1/5] add start of SessionProtocol --- ts/session/protocols/SessionProtocol.ts | 125 ++++++++++++++---- .../session/protocols/SessionProtocol_test.ts | 11 ++ 2 files changed, 111 insertions(+), 25 deletions(-) create mode 100644 ts/test/session/protocols/SessionProtocol_test.ts diff --git a/ts/session/protocols/SessionProtocol.ts b/ts/session/protocols/SessionProtocol.ts index ebeac9f37..c95629c2c 100644 --- a/ts/session/protocols/SessionProtocol.ts +++ b/ts/session/protocols/SessionProtocol.ts @@ -1,17 +1,89 @@ -// TODO: Need to flesh out these functions -// Structure of this can be changed for example sticking this all in a class -// The reason i haven't done it is to avoid having instances of the protocol, rather you should be able to call the functions directly +import { SessionResetMessage } from '../messages/outgoing'; +// import { MessageSender } from '../sending'; -import { OutgoingContentMessage } from '../messages/outgoing'; +// These two Maps should never be accessed directly but only +// through `_update*SessionTimestamp()`, `_get*SessionRequest()` or `_has'SessionRequest()` +let sentSessionsTimestamp: Map; +let processedSessionsTimestamp: Map; + +/** + * We only need to fetch once from the database, because we are the only one writing to it + */ +async function _fetchFromDBIfNeeded(): Promise { + if (!sentSessionsTimestamp) { + // TODO actually fetch from DB + sentSessionsTimestamp = new Map(); + processedSessionsTimestamp = new Map(); + } +} + +async function _writeToDBSentSessions(): Promise { + // TODO actually write to DB +} + +async function _writeToDBProcessedSessions(): Promise { + // TODO actually write to DB +} + + +/** + * This is a utility function to avoid duplicated code of _updateSentSessionTimestamp and _updateProcessedSessionTimestamp + */ +async function _updateSessionTimestamp(device: string, timestamp: number | undefined, map: Map): Promise { + await _fetchFromDBIfNeeded(); + if (!timestamp) { + return map.delete(device); + } + map.set(device, timestamp); + + return true; +} + +/** + * + * @param device the device id + * @param timestamp undefined to remove the key/value pair, otherwise updates the sent timestamp and write to DB + */ +async function _updateSentSessionTimestamp(device: string, timestamp: number|undefined): Promise { + if (_updateSessionTimestamp(device, timestamp, sentSessionsTimestamp)) { + await _writeToDBSentSessions(); + } +} + +/** + * timestamp undefined to remove the key/value pair, otherwise updates the processed timestamp and writes to DB + */ +async function _updateProcessedSessionTimestamp(device: string, timestamp: number|undefined): Promise { + if (_updateSessionTimestamp(device, timestamp, processedSessionsTimestamp)) { + await _writeToDBProcessedSessions(); + } +} export function hasSession(device: string): boolean { return false; // TODO: Implement } -export function hasSentSessionRequest(device: string): boolean { - // TODO: need a way to keep track of if we've sent a session request - // My idea was to use the timestamp of when it was sent but there might be another better approach - return false; +/** + * This is a utility function to avoid duplicate code between `_getProcessedSessionRequest()` and `_getSentSessionRequest()` + */ +async function _getSessionRequest(device: string, map: Map): Promise { + await _fetchFromDBIfNeeded(); + + return map.get(device); +} + +async function _getSentSessionRequest(device: string): Promise { + return _getSessionRequest(device, processedSessionsTimestamp); +} + +async function _getProcessedSessionRequest(device: string): Promise { + return _getSessionRequest(device, sentSessionsTimestamp); +} + +export async function hasSentSessionRequest(device: string): Promise { + const hasSent = await _getSessionRequest(device, sentSessionsTimestamp); + + return !!hasSent; } export async function sendSessionRequestIfNeeded( @@ -25,33 +97,36 @@ export async function sendSessionRequestIfNeeded( return Promise.reject(new Error('Need to implement this function')); } -// TODO: Replace OutgoingContentMessage with SessionReset -export async function sendSessionRequest( - message: OutgoingContentMessage +export async function sendSessionRequests( + message: SessionResetMessage, + device: string ): Promise { - // TODO: Optimistically store timestamp of when session request was sent + + // Optimistically store timestamp of when session request was sent + await _updateSentSessionTimestamp(device, Date.now()); + + // await MessageSender.send() + // TODO: Send out the request via MessageSender // TODO: On failure, unset the timestamp return Promise.resolve(); } -export function sessionEstablished(device: string) { - // TODO: this is called when we receive an encrypted message from the other user - // Maybe it should be renamed to something else - // TODO: This should make `hasSentSessionRequest` return `false` +export async function sessionEstablished(device: string) { + // remove our existing sent timestamp for that device + return _updateSentSessionTimestamp(device, undefined); } -export function shouldProcessSessionRequest( +export async function shouldProcessSessionRequest( device: string, messageTimestamp: number -): boolean { - // TODO: Need to do the following here - // messageTimestamp > session request sent timestamp && messageTimestamp > session request processed timestamp - return false; +): Promise { + const existingSentTimestamp = await _getSentSessionRequest(device) || 0; + const existingProcessedTimestamp = await _getProcessedSessionRequest(device) || 0; + + return messageTimestamp > existingSentTimestamp && messageTimestamp > existingProcessedTimestamp; } -export function sessionRequestProcessed(device: string) { - // TODO: this is called when we process the session request - // This should store the processed timestamp - // Again naming is crap so maybe some other name is better +export async function onSessionRequestProcessed(device: string) { + return _updateProcessedSessionTimestamp(device, Date.now()); } diff --git a/ts/test/session/protocols/SessionProtocol_test.ts b/ts/test/session/protocols/SessionProtocol_test.ts new file mode 100644 index 000000000..89bcec78c --- /dev/null +++ b/ts/test/session/protocols/SessionProtocol_test.ts @@ -0,0 +1,11 @@ +import { expect } from 'chai'; +import { SessionProtocol } from '../../../session/protocols'; + + + +describe('SessionProtocol', () => { + it('has ', () => { + + }); + +}); From 101fbedf0a899d49e664c3dad8f17349bead3839 Mon Sep 17 00:00:00 2001 From: Audric Ackermann Date: Tue, 2 Jun 2020 11:17:52 +1000 Subject: [PATCH 2/5] update SessionProtocol. need merge to go further --- ts/session/protocols/SessionProtocol.ts | 184 ++++++++++++++++-------- 1 file changed, 127 insertions(+), 57 deletions(-) diff --git a/ts/session/protocols/SessionProtocol.ts b/ts/session/protocols/SessionProtocol.ts index c95629c2c..56bd08dc8 100644 --- a/ts/session/protocols/SessionProtocol.ts +++ b/ts/session/protocols/SessionProtocol.ts @@ -1,11 +1,125 @@ import { SessionResetMessage } from '../messages/outgoing'; // import { MessageSender } from '../sending'; -// These two Maps should never be accessed directly but only -// through `_update*SessionTimestamp()`, `_get*SessionRequest()` or `_has'SessionRequest()` +/** + * This map olds the sent session timestamps, i.e. session requests message effectively sent to the recipient. + * It is backed by a database entry so it's loaded from db on startup. + * This map should not be used directly, but instead through + * `_updateSendSessionTimestamp()`, `_getSendSessionRequest()` or `_hasSendSessionRequest()` + */ let sentSessionsTimestamp: Map; + +/** + * This map olds the processed session timestamps, i.e. when we received a session request and handled it. + * It is backed by a database entry so it's loaded from db on startup. + * This map should not be used directly, but instead through + * `_updateProcessedSessionTimestamp()`, `_getProcessedSessionRequest()` or `_hasProcessedSessionRequest()` + */ let processedSessionsTimestamp: Map; +/** + * This map olds the timestamp on which a sent session reset is triggered for a specific device. + * Once the message is sent or failed to sent, this device is removed from here. + * This is a memory only map. Which means that on app restart it's starts empty. + */ +const pendingSendSessionsTimestamp: Set = new Set(); + + +/** ======= exported functions ======= */ + +/** Returns true if we already have a session with that device */ +export async function hasSession(device: string): Promise { + // Session does not use the concept of a deviceId, thus it's always 1 + const address = new window.libsignal.SignalProtocolAddress(device, 1); + const sessionCipher = new window.libsignal.SessionCipher( + window.textsecure.storage.protocol, + address + ); + + return sessionCipher.hasOpenSession(); +} + +/** + * Returns true if we sent a session request to that device already OR + * if a session request to that device is right now being sent. + */ +export async function hasSentSessionRequest(device: string): Promise { + const pendingSend = pendingSendSessionsTimestamp.has(device); + const hasSent = await _hasSentSessionRequest(device); + + return pendingSend || hasSent; +} + +/** + * Triggers a SessionResetMessage to be sent if: + * - we do not already have a session and + * - we did not sent a session request already to that device and + * - we do not have a session request currently being send to that device + */ +export async function sendSessionRequestIfNeeded( + device: string +): Promise { + if (hasSession(device) || hasSentSessionRequest(device)) { + return Promise.resolve(); + } + + const preKeyBundle = await window.libloki.storage.getPreKeyBundleForContact(device); + const sessionReset = new SessionResetMessage({ + preKeyBundle, + timestamp: Date.now(), + }); + + return sendSessionRequests(sessionReset, device); +} + +/** */ +export async function sendSessionRequests( + message: SessionResetMessage, + device: string +): Promise { + const timestamp = Date.now(); + + // mark the session as being pending send with current timestamp + // so we know we already triggered a new session with that device + pendingSendSessionsTimestamp.add(device); + // const rawMessage = toRawMessage(message); + // // TODO: Send out the request via MessageSender + + // return MessageSender.send(rawMessage) + // .then(async () => { + // await _updateSentSessionTimestamp(device, timestamp); + // pendingSendSessionsTimestamp.delete(device); + // }) + // .catch(() => { + // pendingSendSessionsTimestamp.delete(device); + // }); +} + +/** + * Called when a session is establish so we store on database this info. + */ +export async function onSessionEstablished(device: string) { + // remove our existing sent timestamp for that device + return _updateSentSessionTimestamp(device, undefined); +} + +export async function shouldProcessSessionRequest( + device: string, + messageTimestamp: number +): Promise { + const existingSentTimestamp = await _getSentSessionRequest(device) || 0; + const existingProcessedTimestamp = await _getProcessedSessionRequest(device) || 0; + + return messageTimestamp > existingSentTimestamp && messageTimestamp > existingProcessedTimestamp; +} + +export async function onSessionRequestProcessed(device: string) { + return _updateProcessedSessionTimestamp(device, Date.now()); +} + +/** ======= local / utility functions ======= */ + + /** * We only need to fetch once from the database, because we are the only one writing to it */ @@ -18,11 +132,15 @@ async function _fetchFromDBIfNeeded(): Promise { } async function _writeToDBSentSessions(): Promise { - // TODO actually write to DB + const data = { id: 'sentSessionsTimestamp', value: JSON.stringify(sentSessionsTimestamp) }; + + await window.Signal.Data.createOrUpdateItem(data); } async function _writeToDBProcessedSessions(): Promise { - // TODO actually write to DB + const data = { id: 'processedSessionsTimestamp', value: JSON.stringify(processedSessionsTimestamp) }; + + await window.Signal.Data.createOrUpdateItem(data); } @@ -59,9 +177,6 @@ async function _updateProcessedSessionTimestamp(device: string, timestamp: numbe } } -export function hasSession(device: string): boolean { - return false; // TODO: Implement -} /** * This is a utility function to avoid duplicate code between `_getProcessedSessionRequest()` and `_getSentSessionRequest()` @@ -73,60 +188,15 @@ async function _getSessionRequest(device: string, map: Map): Pro } async function _getSentSessionRequest(device: string): Promise { - return _getSessionRequest(device, processedSessionsTimestamp); -} - -async function _getProcessedSessionRequest(device: string): Promise { return _getSessionRequest(device, sentSessionsTimestamp); } -export async function hasSentSessionRequest(device: string): Promise { - const hasSent = await _getSessionRequest(device, sentSessionsTimestamp); - - return !!hasSent; -} - -export async function sendSessionRequestIfNeeded( - device: string -): Promise { - if (hasSession(device) || hasSentSessionRequest(device)) { - return Promise.resolve(); - } - - // TODO: Call sendSessionRequest with SessionReset - return Promise.reject(new Error('Need to implement this function')); -} - -export async function sendSessionRequests( - message: SessionResetMessage, - device: string -): Promise { - - // Optimistically store timestamp of when session request was sent - await _updateSentSessionTimestamp(device, Date.now()); - - // await MessageSender.send() - - // TODO: Send out the request via MessageSender - // TODO: On failure, unset the timestamp - return Promise.resolve(); -} - -export async function sessionEstablished(device: string) { - // remove our existing sent timestamp for that device - return _updateSentSessionTimestamp(device, undefined); +async function _getProcessedSessionRequest(device: string): Promise { + return _getSessionRequest(device, processedSessionsTimestamp); } -export async function shouldProcessSessionRequest( - device: string, - messageTimestamp: number -): Promise { - const existingSentTimestamp = await _getSentSessionRequest(device) || 0; - const existingProcessedTimestamp = await _getProcessedSessionRequest(device) || 0; - - return messageTimestamp > existingSentTimestamp && messageTimestamp > existingProcessedTimestamp; -} +async function _hasSentSessionRequest(device: string): Promise { + await _fetchFromDBIfNeeded(); -export async function onSessionRequestProcessed(device: string) { - return _updateProcessedSessionTimestamp(device, Date.now()); + return sentSessionsTimestamp.has(device); } From a7572470df4a20b217aa5ff03ca7201057e7ea88 Mon Sep 17 00:00:00 2001 From: Audric Ackermann Date: Tue, 2 Jun 2020 11:18:14 +1000 Subject: [PATCH 3/5] fix some ts errors on sending files --- ts/session/sending/MessageQueue.ts | 2 +- ts/session/sending/MessageQueueInterface.ts | 2 +- ts/session/sending/PendingMessageCache.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/ts/session/sending/MessageQueue.ts b/ts/session/sending/MessageQueue.ts index 274640ac4..ae922e306 100644 --- a/ts/session/sending/MessageQueue.ts +++ b/ts/session/sending/MessageQueue.ts @@ -3,7 +3,7 @@ import { MessageQueueInterface, MessageQueueInterfaceEvents, } from './MessageQueueInterface'; -import { OpenGroupMessage, OutgoingContentMessage } from '../messages/outgoing'; +import { ContentMessage as OutgoingContentMessage, OpenGroupMessage } from '../messages/outgoing'; import { PendingMessageCache } from './PendingMessageCache'; import { JobQueue, TypedEventEmitter } from '../utils'; diff --git a/ts/session/sending/MessageQueueInterface.ts b/ts/session/sending/MessageQueueInterface.ts index 553b25ed0..69a19ff89 100644 --- a/ts/session/sending/MessageQueueInterface.ts +++ b/ts/session/sending/MessageQueueInterface.ts @@ -1,4 +1,4 @@ -import { OpenGroupMessage, OutgoingContentMessage } from '../messages/outgoing'; +import { ContentMessage as OutgoingContentMessage, OpenGroupMessage } from '../messages/outgoing'; import { RawMessage } from '../types/RawMessage'; import { TypedEventEmitter } from '../utils'; diff --git a/ts/session/sending/PendingMessageCache.ts b/ts/session/sending/PendingMessageCache.ts index 2f10e58a6..1a88afbe9 100644 --- a/ts/session/sending/PendingMessageCache.ts +++ b/ts/session/sending/PendingMessageCache.ts @@ -1,5 +1,5 @@ import { RawMessage } from '../types/RawMessage'; -import { OutgoingContentMessage } from '../messages/outgoing'; +import { ContentMessage as OutgoingContentMessage } from '../messages/outgoing'; // TODO: We should be able to import functions straight from the db here without going through the window object From 1dec669d116e61bc939bdf9b683dca3371aa4938 Mon Sep 17 00:00:00 2001 From: Audric Ackermann Date: Tue, 2 Jun 2020 11:35:31 +1000 Subject: [PATCH 4/5] make sessionProtoco read and write to db --- ts/session/protocols/SessionProtocol.ts | 43 ++++++++++++++----- .../session/protocols/SessionProtocol_test.ts | 14 +++--- 2 files changed, 39 insertions(+), 18 deletions(-) diff --git a/ts/session/protocols/SessionProtocol.ts b/ts/session/protocols/SessionProtocol.ts index 56bd08dc8..d568b679f 100644 --- a/ts/session/protocols/SessionProtocol.ts +++ b/ts/session/protocols/SessionProtocol.ts @@ -1,13 +1,18 @@ import { SessionResetMessage } from '../messages/outgoing'; // import { MessageSender } from '../sending'; +interface StringToNumberMap { + [key: string]: number; +} + + /** * This map olds the sent session timestamps, i.e. session requests message effectively sent to the recipient. * It is backed by a database entry so it's loaded from db on startup. * This map should not be used directly, but instead through * `_updateSendSessionTimestamp()`, `_getSendSessionRequest()` or `_hasSendSessionRequest()` */ -let sentSessionsTimestamp: Map; +let sentSessionsTimestamp: StringToNumberMap; /** * This map olds the processed session timestamps, i.e. when we received a session request and handled it. @@ -15,7 +20,7 @@ let sentSessionsTimestamp: Map; * This map should not be used directly, but instead through * `_updateProcessedSessionTimestamp()`, `_getProcessedSessionRequest()` or `_hasProcessedSessionRequest()` */ -let processedSessionsTimestamp: Map; +let processedSessionsTimestamp: StringToNumberMap; /** * This map olds the timestamp on which a sent session reset is triggered for a specific device. @@ -125,9 +130,19 @@ export async function onSessionRequestProcessed(device: string) { */ async function _fetchFromDBIfNeeded(): Promise { if (!sentSessionsTimestamp) { - // TODO actually fetch from DB - sentSessionsTimestamp = new Map(); - processedSessionsTimestamp = new Map(); + const sentItem = await window.Signal.Data.getItemById('sentSessionsTimestamp'); + if (sentItem) { + sentSessionsTimestamp = sentItem.value; + } else { + sentSessionsTimestamp = {}; + } + + const processedItem = await window.Signal.Data.getItemById('processedSessionsTimestamp'); + if (processedItem) { + processedSessionsTimestamp = processedItem.value; + } else { + processedSessionsTimestamp = {}; + } } } @@ -147,12 +162,18 @@ async function _writeToDBProcessedSessions(): Promise { /** * This is a utility function to avoid duplicated code of _updateSentSessionTimestamp and _updateProcessedSessionTimestamp */ -async function _updateSessionTimestamp(device: string, timestamp: number | undefined, map: Map): Promise { +async function _updateSessionTimestamp(device: string, timestamp: number | undefined, map: StringToNumberMap): Promise { await _fetchFromDBIfNeeded(); if (!timestamp) { - return map.delete(device); + if (!!map[device]) { + delete map.device; + + return true; + } + + return false; } - map.set(device, timestamp); + map[device] = timestamp; return true; } @@ -181,10 +202,10 @@ async function _updateProcessedSessionTimestamp(device: string, timestamp: numbe /** * This is a utility function to avoid duplicate code between `_getProcessedSessionRequest()` and `_getSentSessionRequest()` */ -async function _getSessionRequest(device: string, map: Map): Promise { +async function _getSessionRequest(device: string, map: StringToNumberMap): Promise { await _fetchFromDBIfNeeded(); - return map.get(device); + return map[device]; } async function _getSentSessionRequest(device: string): Promise { @@ -198,5 +219,5 @@ async function _getProcessedSessionRequest(device: string): Promise { await _fetchFromDBIfNeeded(); - return sentSessionsTimestamp.has(device); + return !!sentSessionsTimestamp[device]; } diff --git a/ts/test/session/protocols/SessionProtocol_test.ts b/ts/test/session/protocols/SessionProtocol_test.ts index 89bcec78c..6d0f00d12 100644 --- a/ts/test/session/protocols/SessionProtocol_test.ts +++ b/ts/test/session/protocols/SessionProtocol_test.ts @@ -1,11 +1,11 @@ -import { expect } from 'chai'; -import { SessionProtocol } from '../../../session/protocols'; +// import { expect } from 'chai'; +// import { SessionProtocol } from '../../../session/protocols'; -describe('SessionProtocol', () => { - it('has ', () => { - - }); +// describe('SessionProtocol', () => { +// it('has ', () => { -}); +// }); + +// }); From 935ac8d8f911616731c20aa5b45b79bea6895731 Mon Sep 17 00:00:00 2001 From: Audric Ackermann Date: Tue, 2 Jun 2020 11:36:33 +1000 Subject: [PATCH 5/5] lint --- ts/session/protocols/SessionProtocol.ts | 66 +++++++++++++------ ts/session/sending/MessageQueue.ts | 5 +- ts/session/sending/MessageQueueInterface.ts | 5 +- .../session/protocols/SessionProtocol_test.ts | 2 - 4 files changed, 55 insertions(+), 23 deletions(-) diff --git a/ts/session/protocols/SessionProtocol.ts b/ts/session/protocols/SessionProtocol.ts index d568b679f..eae8fcbad 100644 --- a/ts/session/protocols/SessionProtocol.ts +++ b/ts/session/protocols/SessionProtocol.ts @@ -5,7 +5,6 @@ interface StringToNumberMap { [key: string]: number; } - /** * This map olds the sent session timestamps, i.e. session requests message effectively sent to the recipient. * It is backed by a database entry so it's loaded from db on startup. @@ -29,7 +28,6 @@ let processedSessionsTimestamp: StringToNumberMap; */ const pendingSendSessionsTimestamp: Set = new Set(); - /** ======= exported functions ======= */ /** Returns true if we already have a session with that device */ @@ -68,7 +66,9 @@ export async function sendSessionRequestIfNeeded( return Promise.resolve(); } - const preKeyBundle = await window.libloki.storage.getPreKeyBundleForContact(device); + const preKeyBundle = await window.libloki.storage.getPreKeyBundleForContact( + device + ); const sessionReset = new SessionResetMessage({ preKeyBundle, timestamp: Date.now(), @@ -112,10 +112,14 @@ export async function shouldProcessSessionRequest( device: string, messageTimestamp: number ): Promise { - const existingSentTimestamp = await _getSentSessionRequest(device) || 0; - const existingProcessedTimestamp = await _getProcessedSessionRequest(device) || 0; + const existingSentTimestamp = (await _getSentSessionRequest(device)) || 0; + const existingProcessedTimestamp = + (await _getProcessedSessionRequest(device)) || 0; - return messageTimestamp > existingSentTimestamp && messageTimestamp > existingProcessedTimestamp; + return ( + messageTimestamp > existingSentTimestamp && + messageTimestamp > existingProcessedTimestamp + ); } export async function onSessionRequestProcessed(device: string) { @@ -124,20 +128,23 @@ export async function onSessionRequestProcessed(device: string) { /** ======= local / utility functions ======= */ - /** * We only need to fetch once from the database, because we are the only one writing to it */ async function _fetchFromDBIfNeeded(): Promise { if (!sentSessionsTimestamp) { - const sentItem = await window.Signal.Data.getItemById('sentSessionsTimestamp'); + const sentItem = await window.Signal.Data.getItemById( + 'sentSessionsTimestamp' + ); if (sentItem) { sentSessionsTimestamp = sentItem.value; } else { sentSessionsTimestamp = {}; } - const processedItem = await window.Signal.Data.getItemById('processedSessionsTimestamp'); + const processedItem = await window.Signal.Data.getItemById( + 'processedSessionsTimestamp' + ); if (processedItem) { processedSessionsTimestamp = processedItem.value; } else { @@ -147,22 +154,31 @@ async function _fetchFromDBIfNeeded(): Promise { } async function _writeToDBSentSessions(): Promise { - const data = { id: 'sentSessionsTimestamp', value: JSON.stringify(sentSessionsTimestamp) }; + const data = { + id: 'sentSessionsTimestamp', + value: JSON.stringify(sentSessionsTimestamp), + }; await window.Signal.Data.createOrUpdateItem(data); } async function _writeToDBProcessedSessions(): Promise { - const data = { id: 'processedSessionsTimestamp', value: JSON.stringify(processedSessionsTimestamp) }; + const data = { + id: 'processedSessionsTimestamp', + value: JSON.stringify(processedSessionsTimestamp), + }; await window.Signal.Data.createOrUpdateItem(data); } - /** * This is a utility function to avoid duplicated code of _updateSentSessionTimestamp and _updateProcessedSessionTimestamp */ -async function _updateSessionTimestamp(device: string, timestamp: number | undefined, map: StringToNumberMap): Promise { +async function _updateSessionTimestamp( + device: string, + timestamp: number | undefined, + map: StringToNumberMap +): Promise { await _fetchFromDBIfNeeded(); if (!timestamp) { if (!!map[device]) { @@ -183,7 +199,10 @@ async function _updateSessionTimestamp(device: string, timestamp: number | undef * @param device the device id * @param timestamp undefined to remove the key/value pair, otherwise updates the sent timestamp and write to DB */ -async function _updateSentSessionTimestamp(device: string, timestamp: number|undefined): Promise { +async function _updateSentSessionTimestamp( + device: string, + timestamp: number | undefined +): Promise { if (_updateSessionTimestamp(device, timestamp, sentSessionsTimestamp)) { await _writeToDBSentSessions(); } @@ -192,27 +211,36 @@ async function _updateSentSessionTimestamp(device: string, timestamp: number|und /** * timestamp undefined to remove the key/value pair, otherwise updates the processed timestamp and writes to DB */ -async function _updateProcessedSessionTimestamp(device: string, timestamp: number|undefined): Promise { +async function _updateProcessedSessionTimestamp( + device: string, + timestamp: number | undefined +): Promise { if (_updateSessionTimestamp(device, timestamp, processedSessionsTimestamp)) { await _writeToDBProcessedSessions(); } } - /** * This is a utility function to avoid duplicate code between `_getProcessedSessionRequest()` and `_getSentSessionRequest()` */ -async function _getSessionRequest(device: string, map: StringToNumberMap): Promise { +async function _getSessionRequest( + device: string, + map: StringToNumberMap +): Promise { await _fetchFromDBIfNeeded(); return map[device]; } -async function _getSentSessionRequest(device: string): Promise { +async function _getSentSessionRequest( + device: string +): Promise { return _getSessionRequest(device, sentSessionsTimestamp); } -async function _getProcessedSessionRequest(device: string): Promise { +async function _getProcessedSessionRequest( + device: string +): Promise { return _getSessionRequest(device, processedSessionsTimestamp); } diff --git a/ts/session/sending/MessageQueue.ts b/ts/session/sending/MessageQueue.ts index ae922e306..90bb5d424 100644 --- a/ts/session/sending/MessageQueue.ts +++ b/ts/session/sending/MessageQueue.ts @@ -3,7 +3,10 @@ import { MessageQueueInterface, MessageQueueInterfaceEvents, } from './MessageQueueInterface'; -import { ContentMessage as OutgoingContentMessage, OpenGroupMessage } from '../messages/outgoing'; +import { + ContentMessage as OutgoingContentMessage, + OpenGroupMessage, +} from '../messages/outgoing'; import { PendingMessageCache } from './PendingMessageCache'; import { JobQueue, TypedEventEmitter } from '../utils'; diff --git a/ts/session/sending/MessageQueueInterface.ts b/ts/session/sending/MessageQueueInterface.ts index 69a19ff89..73c6f802c 100644 --- a/ts/session/sending/MessageQueueInterface.ts +++ b/ts/session/sending/MessageQueueInterface.ts @@ -1,4 +1,7 @@ -import { ContentMessage as OutgoingContentMessage, OpenGroupMessage } from '../messages/outgoing'; +import { + ContentMessage as OutgoingContentMessage, + OpenGroupMessage, +} from '../messages/outgoing'; import { RawMessage } from '../types/RawMessage'; import { TypedEventEmitter } from '../utils'; diff --git a/ts/test/session/protocols/SessionProtocol_test.ts b/ts/test/session/protocols/SessionProtocol_test.ts index 6d0f00d12..8cd26ddfc 100644 --- a/ts/test/session/protocols/SessionProtocol_test.ts +++ b/ts/test/session/protocols/SessionProtocol_test.ts @@ -1,8 +1,6 @@ // import { expect } from 'chai'; // import { SessionProtocol } from '../../../session/protocols'; - - // describe('SessionProtocol', () => { // it('has ', () => {