pull/1154/head
Audric Ackermann 5 years ago
parent 1dec669d11
commit 935ac8d8f9
No known key found for this signature in database
GPG Key ID: 999F434D76324AD4

@ -5,7 +5,6 @@ interface StringToNumberMap {
[key: string]: number; [key: string]: number;
} }
/** /**
* This map olds the sent session timestamps, i.e. session requests message effectively sent to the recipient. * 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. * 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<string> = new Set(); const pendingSendSessionsTimestamp: Set<string> = new Set();
/** ======= exported functions ======= */ /** ======= exported functions ======= */
/** Returns true if we already have a session with that device */ /** Returns true if we already have a session with that device */
@ -68,7 +66,9 @@ export async function sendSessionRequestIfNeeded(
return Promise.resolve(); return Promise.resolve();
} }
const preKeyBundle = await window.libloki.storage.getPreKeyBundleForContact(device); const preKeyBundle = await window.libloki.storage.getPreKeyBundleForContact(
device
);
const sessionReset = new SessionResetMessage({ const sessionReset = new SessionResetMessage({
preKeyBundle, preKeyBundle,
timestamp: Date.now(), timestamp: Date.now(),
@ -112,10 +112,14 @@ export async function shouldProcessSessionRequest(
device: string, device: string,
messageTimestamp: number messageTimestamp: number
): Promise<boolean> { ): Promise<boolean> {
const existingSentTimestamp = await _getSentSessionRequest(device) || 0; const existingSentTimestamp = (await _getSentSessionRequest(device)) || 0;
const existingProcessedTimestamp = await _getProcessedSessionRequest(device) || 0; const existingProcessedTimestamp =
(await _getProcessedSessionRequest(device)) || 0;
return messageTimestamp > existingSentTimestamp && messageTimestamp > existingProcessedTimestamp; return (
messageTimestamp > existingSentTimestamp &&
messageTimestamp > existingProcessedTimestamp
);
} }
export async function onSessionRequestProcessed(device: string) { export async function onSessionRequestProcessed(device: string) {
@ -124,20 +128,23 @@ export async function onSessionRequestProcessed(device: string) {
/** ======= local / utility functions ======= */ /** ======= local / utility functions ======= */
/** /**
* We only need to fetch once from the database, because we are the only one writing to it * We only need to fetch once from the database, because we are the only one writing to it
*/ */
async function _fetchFromDBIfNeeded(): Promise<void> { async function _fetchFromDBIfNeeded(): Promise<void> {
if (!sentSessionsTimestamp) { if (!sentSessionsTimestamp) {
const sentItem = await window.Signal.Data.getItemById('sentSessionsTimestamp'); const sentItem = await window.Signal.Data.getItemById(
'sentSessionsTimestamp'
);
if (sentItem) { if (sentItem) {
sentSessionsTimestamp = sentItem.value; sentSessionsTimestamp = sentItem.value;
} else { } else {
sentSessionsTimestamp = {}; sentSessionsTimestamp = {};
} }
const processedItem = await window.Signal.Data.getItemById('processedSessionsTimestamp'); const processedItem = await window.Signal.Data.getItemById(
'processedSessionsTimestamp'
);
if (processedItem) { if (processedItem) {
processedSessionsTimestamp = processedItem.value; processedSessionsTimestamp = processedItem.value;
} else { } else {
@ -147,22 +154,31 @@ async function _fetchFromDBIfNeeded(): Promise<void> {
} }
async function _writeToDBSentSessions(): Promise<void> { async function _writeToDBSentSessions(): Promise<void> {
const data = { id: 'sentSessionsTimestamp', value: JSON.stringify(sentSessionsTimestamp) }; const data = {
id: 'sentSessionsTimestamp',
value: JSON.stringify(sentSessionsTimestamp),
};
await window.Signal.Data.createOrUpdateItem(data); await window.Signal.Data.createOrUpdateItem(data);
} }
async function _writeToDBProcessedSessions(): Promise<void> { async function _writeToDBProcessedSessions(): Promise<void> {
const data = { id: 'processedSessionsTimestamp', value: JSON.stringify(processedSessionsTimestamp) }; const data = {
id: 'processedSessionsTimestamp',
value: JSON.stringify(processedSessionsTimestamp),
};
await window.Signal.Data.createOrUpdateItem(data); await window.Signal.Data.createOrUpdateItem(data);
} }
/** /**
* This is a utility function to avoid duplicated code of _updateSentSessionTimestamp and _updateProcessedSessionTimestamp * This is a utility function to avoid duplicated code of _updateSentSessionTimestamp and _updateProcessedSessionTimestamp
*/ */
async function _updateSessionTimestamp(device: string, timestamp: number | undefined, map: StringToNumberMap): Promise<boolean> { async function _updateSessionTimestamp(
device: string,
timestamp: number | undefined,
map: StringToNumberMap
): Promise<boolean> {
await _fetchFromDBIfNeeded(); await _fetchFromDBIfNeeded();
if (!timestamp) { if (!timestamp) {
if (!!map[device]) { if (!!map[device]) {
@ -183,7 +199,10 @@ async function _updateSessionTimestamp(device: string, timestamp: number | undef
* @param device the device id * @param device the device id
* @param timestamp undefined to remove the key/value pair, otherwise updates the sent timestamp and write to DB * @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<void> { async function _updateSentSessionTimestamp(
device: string,
timestamp: number | undefined
): Promise<void> {
if (_updateSessionTimestamp(device, timestamp, sentSessionsTimestamp)) { if (_updateSessionTimestamp(device, timestamp, sentSessionsTimestamp)) {
await _writeToDBSentSessions(); 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 * 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<void> { async function _updateProcessedSessionTimestamp(
device: string,
timestamp: number | undefined
): Promise<void> {
if (_updateSessionTimestamp(device, timestamp, processedSessionsTimestamp)) { if (_updateSessionTimestamp(device, timestamp, processedSessionsTimestamp)) {
await _writeToDBProcessedSessions(); await _writeToDBProcessedSessions();
} }
} }
/** /**
* This is a utility function to avoid duplicate code between `_getProcessedSessionRequest()` and `_getSentSessionRequest()` * This is a utility function to avoid duplicate code between `_getProcessedSessionRequest()` and `_getSentSessionRequest()`
*/ */
async function _getSessionRequest(device: string, map: StringToNumberMap): Promise<number | undefined> { async function _getSessionRequest(
device: string,
map: StringToNumberMap
): Promise<number | undefined> {
await _fetchFromDBIfNeeded(); await _fetchFromDBIfNeeded();
return map[device]; return map[device];
} }
async function _getSentSessionRequest(device: string): Promise<number | undefined> { async function _getSentSessionRequest(
device: string
): Promise<number | undefined> {
return _getSessionRequest(device, sentSessionsTimestamp); return _getSessionRequest(device, sentSessionsTimestamp);
} }
async function _getProcessedSessionRequest(device: string): Promise<number | undefined> { async function _getProcessedSessionRequest(
device: string
): Promise<number | undefined> {
return _getSessionRequest(device, processedSessionsTimestamp); return _getSessionRequest(device, processedSessionsTimestamp);
} }

@ -3,7 +3,10 @@ import {
MessageQueueInterface, MessageQueueInterface,
MessageQueueInterfaceEvents, MessageQueueInterfaceEvents,
} from './MessageQueueInterface'; } from './MessageQueueInterface';
import { ContentMessage as OutgoingContentMessage, OpenGroupMessage } from '../messages/outgoing'; import {
ContentMessage as OutgoingContentMessage,
OpenGroupMessage,
} from '../messages/outgoing';
import { PendingMessageCache } from './PendingMessageCache'; import { PendingMessageCache } from './PendingMessageCache';
import { JobQueue, TypedEventEmitter } from '../utils'; import { JobQueue, TypedEventEmitter } from '../utils';

@ -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 { RawMessage } from '../types/RawMessage';
import { TypedEventEmitter } from '../utils'; import { TypedEventEmitter } from '../utils';

@ -1,8 +1,6 @@
// import { expect } from 'chai'; // import { expect } from 'chai';
// import { SessionProtocol } from '../../../session/protocols'; // import { SessionProtocol } from '../../../session/protocols';
// describe('SessionProtocol', () => { // describe('SessionProtocol', () => {
// it('has ', () => { // it('has ', () => {

Loading…
Cancel
Save