2021-03-03 20:09:58 +00:00
|
|
|
// Copyright 2020-2021 Signal Messenger, LLC
|
2020-10-30 20:34:04 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-04-08 19:27:20 +00:00
|
|
|
import { debounce, isNumber } from 'lodash';
|
2020-09-09 00:56:23 +00:00
|
|
|
import pMap from 'p-map';
|
|
|
|
|
|
|
|
import dataInterface from '../sql/Client';
|
2021-07-13 18:54:53 +00:00
|
|
|
import * as Bytes from '../Bytes';
|
2020-09-09 00:56:23 +00:00
|
|
|
import {
|
2021-09-24 00:49:05 +00:00
|
|
|
getRandomBytes,
|
2020-09-09 00:56:23 +00:00
|
|
|
deriveStorageItemKey,
|
|
|
|
deriveStorageManifestKey,
|
2021-09-24 00:49:05 +00:00
|
|
|
encryptProfile,
|
|
|
|
decryptProfile,
|
2020-09-09 00:56:23 +00:00
|
|
|
} from '../Crypto';
|
|
|
|
import {
|
|
|
|
mergeAccountRecord,
|
|
|
|
mergeContactRecord,
|
|
|
|
mergeGroupV1Record,
|
2020-09-09 02:25:05 +00:00
|
|
|
mergeGroupV2Record,
|
2020-09-09 00:56:23 +00:00
|
|
|
toAccountRecord,
|
|
|
|
toContactRecord,
|
|
|
|
toGroupV1Record,
|
2020-09-09 02:25:05 +00:00
|
|
|
toGroupV2Record,
|
2020-09-09 00:56:23 +00:00
|
|
|
} from './storageRecordOps';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { ConversationModel } from '../models/conversations';
|
2021-07-13 18:54:53 +00:00
|
|
|
import { strictAssert } from '../util/assert';
|
2021-08-26 14:10:58 +00:00
|
|
|
import * as durations from '../util/durations';
|
2021-06-09 22:28:54 +00:00
|
|
|
import { BackOff } from '../util/BackOff';
|
2021-07-15 23:48:09 +00:00
|
|
|
import { handleMessageSend } from '../util/handleMessageSend';
|
2020-09-29 23:29:11 +00:00
|
|
|
import { storageJobQueue } from '../util/JobQueue';
|
2020-12-18 17:09:31 +00:00
|
|
|
import { sleep } from '../util/sleep';
|
2021-04-09 20:12:05 +00:00
|
|
|
import { isMoreRecentThan } from '../util/timestamp';
|
2021-07-13 18:54:53 +00:00
|
|
|
import { normalizeNumber } from '../util/normalizeNumber';
|
2021-05-05 16:39:16 +00:00
|
|
|
import { ourProfileKeyService } from './ourProfileKey';
|
2021-06-07 16:39:13 +00:00
|
|
|
import {
|
|
|
|
ConversationTypes,
|
|
|
|
typeofConversation,
|
|
|
|
} from '../util/whatTypeOfConversation';
|
2021-07-13 18:54:53 +00:00
|
|
|
import { SignalService as Proto } from '../protobuf';
|
2021-09-17 18:27:53 +00:00
|
|
|
import * as log from '../logging/log';
|
2021-07-13 18:54:53 +00:00
|
|
|
|
|
|
|
type IManifestRecordIdentifier = Proto.ManifestRecord.IIdentifier;
|
|
|
|
|
2021-11-11 22:43:05 +00:00
|
|
|
const { eraseStorageServiceStateFromConversations, updateConversation } =
|
|
|
|
dataInterface;
|
2020-09-09 00:56:23 +00:00
|
|
|
|
2021-04-09 20:12:05 +00:00
|
|
|
const uploadBucket: Array<number> = [];
|
2020-09-29 23:29:11 +00:00
|
|
|
|
|
|
|
const validRecordTypes = new Set([
|
|
|
|
0, // UNKNOWN
|
|
|
|
1, // CONTACT
|
|
|
|
2, // GROUPV1
|
|
|
|
3, // GROUPV2
|
|
|
|
4, // ACCOUNT
|
|
|
|
]);
|
2020-09-09 00:56:23 +00:00
|
|
|
|
2021-06-09 22:28:54 +00:00
|
|
|
const backOff = new BackOff([
|
2021-08-26 14:10:58 +00:00
|
|
|
durations.SECOND,
|
|
|
|
5 * durations.SECOND,
|
|
|
|
30 * durations.SECOND,
|
|
|
|
2 * durations.MINUTE,
|
|
|
|
5 * durations.MINUTE,
|
2021-06-09 22:28:54 +00:00
|
|
|
]);
|
|
|
|
|
2021-08-26 14:10:58 +00:00
|
|
|
const conflictBackOff = new BackOff([
|
|
|
|
durations.SECOND,
|
|
|
|
5 * durations.SECOND,
|
|
|
|
30 * durations.SECOND,
|
|
|
|
]);
|
2020-09-09 23:07:58 +00:00
|
|
|
|
2021-04-06 22:54:47 +00:00
|
|
|
function redactStorageID(storageID: string): string {
|
|
|
|
return storageID.substring(0, 3);
|
|
|
|
}
|
|
|
|
|
2021-03-17 22:10:31 +00:00
|
|
|
type RemoteRecord = {
|
2020-09-09 00:56:23 +00:00
|
|
|
itemType: number;
|
|
|
|
storageID: string;
|
|
|
|
};
|
|
|
|
|
2021-03-17 22:10:31 +00:00
|
|
|
type UnknownRecord = RemoteRecord;
|
|
|
|
|
2020-09-09 00:56:23 +00:00
|
|
|
async function encryptRecord(
|
|
|
|
storageID: string | undefined,
|
2021-07-13 18:54:53 +00:00
|
|
|
storageRecord: Proto.IStorageRecord
|
|
|
|
): Promise<Proto.StorageItem> {
|
|
|
|
const storageItem = new Proto.StorageItem();
|
2020-09-09 00:56:23 +00:00
|
|
|
|
|
|
|
const storageKeyBuffer = storageID
|
2021-09-24 00:49:05 +00:00
|
|
|
? Bytes.fromBase64(String(storageID))
|
2020-09-09 00:56:23 +00:00
|
|
|
: generateStorageID();
|
|
|
|
|
|
|
|
const storageKeyBase64 = window.storage.get('storageKey');
|
2021-06-15 00:09:37 +00:00
|
|
|
if (!storageKeyBase64) {
|
|
|
|
throw new Error('No storage key');
|
|
|
|
}
|
2021-09-24 00:49:05 +00:00
|
|
|
const storageKey = Bytes.fromBase64(storageKeyBase64);
|
|
|
|
const storageItemKey = deriveStorageItemKey(
|
2020-09-09 00:56:23 +00:00
|
|
|
storageKey,
|
2021-09-24 00:49:05 +00:00
|
|
|
Bytes.toBase64(storageKeyBuffer)
|
2020-09-09 00:56:23 +00:00
|
|
|
);
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
const encryptedRecord = encryptProfile(
|
|
|
|
Proto.StorageRecord.encode(storageRecord).finish(),
|
2020-09-09 00:56:23 +00:00
|
|
|
storageItemKey
|
|
|
|
);
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
storageItem.key = storageKeyBuffer;
|
|
|
|
storageItem.value = encryptedRecord;
|
2020-09-09 00:56:23 +00:00
|
|
|
|
|
|
|
return storageItem;
|
|
|
|
}
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
function generateStorageID(): Uint8Array {
|
|
|
|
return getRandomBytes(16);
|
2020-09-09 00:56:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type GeneratedManifestType = {
|
|
|
|
conversationsToUpdate: Array<{
|
2020-09-24 20:57:54 +00:00
|
|
|
conversation: ConversationModel;
|
2020-09-09 00:56:23 +00:00
|
|
|
storageID: string | undefined;
|
|
|
|
}>;
|
2021-09-24 00:49:05 +00:00
|
|
|
deleteKeys: Array<Uint8Array>;
|
2021-07-13 18:54:53 +00:00
|
|
|
newItems: Set<Proto.IStorageItem>;
|
|
|
|
storageManifest: Proto.IStorageManifest;
|
2020-09-09 00:56:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
async function generateManifest(
|
|
|
|
version: number,
|
2021-07-13 18:54:53 +00:00
|
|
|
previousManifest?: Proto.IManifestRecord,
|
2020-09-09 00:56:23 +00:00
|
|
|
isNewManifest = false
|
|
|
|
): Promise<GeneratedManifestType> {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2020-10-06 22:25:00 +00:00
|
|
|
'storageService.generateManifest: generating manifest',
|
|
|
|
version,
|
|
|
|
isNewManifest
|
2020-09-09 00:56:23 +00:00
|
|
|
);
|
|
|
|
|
2021-03-12 01:56:01 +00:00
|
|
|
await window.ConversationController.checkForConflicts();
|
|
|
|
|
2021-07-13 18:54:53 +00:00
|
|
|
const ITEM_TYPE = Proto.ManifestRecord.Identifier.Type;
|
2020-09-09 00:56:23 +00:00
|
|
|
|
|
|
|
const conversationsToUpdate = [];
|
2021-04-09 20:12:05 +00:00
|
|
|
const insertKeys: Array<string> = [];
|
2021-09-24 00:49:05 +00:00
|
|
|
const deleteKeys: Array<Uint8Array> = [];
|
2021-07-13 18:54:53 +00:00
|
|
|
const manifestRecordKeys: Set<IManifestRecordIdentifier> = new Set();
|
|
|
|
const newItems: Set<Proto.IStorageItem> = new Set();
|
2020-09-09 00:56:23 +00:00
|
|
|
|
|
|
|
const conversations = window.getConversations();
|
|
|
|
for (let i = 0; i < conversations.length; i += 1) {
|
|
|
|
const conversation = conversations.models[i];
|
2021-07-13 18:54:53 +00:00
|
|
|
const identifier = new Proto.ManifestRecord.Identifier();
|
2020-09-09 00:56:23 +00:00
|
|
|
|
|
|
|
let storageRecord;
|
2021-06-07 16:39:13 +00:00
|
|
|
|
|
|
|
const conversationType = typeofConversation(conversation.attributes);
|
|
|
|
if (conversationType === ConversationTypes.Me) {
|
2021-07-13 18:54:53 +00:00
|
|
|
storageRecord = new Proto.StorageRecord();
|
2020-09-09 00:56:23 +00:00
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
|
storageRecord.account = await toAccountRecord(conversation);
|
|
|
|
identifier.type = ITEM_TYPE.ACCOUNT;
|
2021-06-07 16:39:13 +00:00
|
|
|
} else if (conversationType === ConversationTypes.Direct) {
|
2021-09-10 02:38:11 +00:00
|
|
|
// Contacts must have UUID
|
|
|
|
if (!conversation.get('uuid')) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-11-11 21:38:02 +00:00
|
|
|
const validationError = conversation.validate();
|
|
|
|
if (validationError) {
|
|
|
|
if (conversation.get('storageID')) {
|
|
|
|
log.warn(
|
|
|
|
'storageService.generateManifest: skipping contact',
|
|
|
|
conversation.idForLogging(),
|
|
|
|
'due to local validation error',
|
|
|
|
validationError
|
|
|
|
);
|
|
|
|
conversation.unset('storageID');
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-07-13 18:54:53 +00:00
|
|
|
storageRecord = new Proto.StorageRecord();
|
2020-09-09 00:56:23 +00:00
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
|
storageRecord.contact = await toContactRecord(conversation);
|
|
|
|
identifier.type = ITEM_TYPE.CONTACT;
|
2021-06-07 16:39:13 +00:00
|
|
|
} else if (conversationType === ConversationTypes.GroupV2) {
|
2021-07-13 18:54:53 +00:00
|
|
|
storageRecord = new Proto.StorageRecord();
|
2020-09-09 02:25:05 +00:00
|
|
|
// eslint-disable-next-line no-await-in-loop
|
2020-09-09 23:07:58 +00:00
|
|
|
storageRecord.groupV2 = await toGroupV2Record(conversation);
|
2020-09-09 02:25:05 +00:00
|
|
|
identifier.type = ITEM_TYPE.GROUPV2;
|
2021-06-07 16:39:13 +00:00
|
|
|
} else if (conversationType === ConversationTypes.GroupV1) {
|
2021-07-13 18:54:53 +00:00
|
|
|
storageRecord = new Proto.StorageRecord();
|
2020-09-09 00:56:23 +00:00
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
|
storageRecord.groupV1 = await toGroupV1Record(conversation);
|
|
|
|
identifier.type = ITEM_TYPE.GROUPV1;
|
2020-09-10 22:37:20 +00:00
|
|
|
} else {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2020-09-10 22:37:20 +00:00
|
|
|
'storageService.generateManifest: unknown conversation',
|
2021-04-06 22:54:47 +00:00
|
|
|
conversation.idForLogging()
|
2020-09-10 22:37:20 +00:00
|
|
|
);
|
2020-09-09 00:56:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (storageRecord) {
|
2020-09-29 23:29:11 +00:00
|
|
|
const currentStorageID = conversation.get('storageID');
|
|
|
|
|
2020-09-09 00:56:23 +00:00
|
|
|
const isNewItem =
|
2020-09-29 23:29:11 +00:00
|
|
|
isNewManifest ||
|
|
|
|
Boolean(conversation.get('needsStorageServiceSync')) ||
|
|
|
|
!currentStorageID;
|
2020-09-09 00:56:23 +00:00
|
|
|
|
|
|
|
const storageID = isNewItem
|
2021-09-24 00:49:05 +00:00
|
|
|
? Bytes.toBase64(generateStorageID())
|
2020-09-29 23:29:11 +00:00
|
|
|
: currentStorageID;
|
2020-09-09 00:56:23 +00:00
|
|
|
|
2020-09-10 22:37:20 +00:00
|
|
|
let storageItem;
|
|
|
|
try {
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
|
storageItem = await encryptRecord(storageID, storageRecord);
|
|
|
|
} catch (err) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error(
|
2020-10-07 23:44:55 +00:00
|
|
|
'storageService.generateManifest: encrypt record failed:',
|
|
|
|
err && err.stack ? err.stack : String(err)
|
2020-09-10 22:37:20 +00:00
|
|
|
);
|
|
|
|
throw err;
|
|
|
|
}
|
2020-09-09 00:56:23 +00:00
|
|
|
identifier.raw = storageItem.key;
|
|
|
|
|
|
|
|
// When a client needs to update a given record it should create it
|
|
|
|
// under a new key and delete the existing key.
|
|
|
|
if (isNewItem) {
|
|
|
|
newItems.add(storageItem);
|
|
|
|
|
2021-04-06 22:54:47 +00:00
|
|
|
if (storageID) {
|
2021-04-09 20:12:05 +00:00
|
|
|
insertKeys.push(storageID);
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2021-04-06 22:54:47 +00:00
|
|
|
'storageService.generateManifest: new key',
|
|
|
|
conversation.idForLogging(),
|
|
|
|
redactStorageID(storageID)
|
|
|
|
);
|
|
|
|
} else {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2021-04-06 22:54:47 +00:00
|
|
|
'storageService.generateManifest: no storage id',
|
|
|
|
conversation.idForLogging()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-09 00:56:23 +00:00
|
|
|
const oldStorageID = conversation.get('storageID');
|
|
|
|
if (oldStorageID) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2021-04-06 22:54:47 +00:00
|
|
|
'storageService.generateManifest: deleting key',
|
|
|
|
redactStorageID(oldStorageID)
|
|
|
|
);
|
2021-09-24 00:49:05 +00:00
|
|
|
deleteKeys.push(Bytes.fromBase64(oldStorageID));
|
2020-09-09 00:56:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
conversationsToUpdate.push({
|
|
|
|
conversation,
|
|
|
|
storageID,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
manifestRecordKeys.add(identifier);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-17 22:10:31 +00:00
|
|
|
const unknownRecordsArray: ReadonlyArray<UnknownRecord> = (
|
2020-10-06 22:25:00 +00:00
|
|
|
window.storage.get('storage-service-unknown-records') || []
|
|
|
|
).filter((record: UnknownRecord) => !validRecordTypes.has(record.itemType));
|
2020-09-09 00:56:23 +00:00
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2020-10-06 22:25:00 +00:00
|
|
|
'storageService.generateManifest: adding unknown records:',
|
|
|
|
unknownRecordsArray.length
|
2020-09-09 00:56:23 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// When updating the manifest, ensure all "unknown" keys are added to the
|
|
|
|
// new manifest, so we don't inadvertently delete something we don't understand
|
|
|
|
unknownRecordsArray.forEach((record: UnknownRecord) => {
|
2021-07-13 18:54:53 +00:00
|
|
|
const identifier = new Proto.ManifestRecord.Identifier();
|
2020-09-29 23:29:11 +00:00
|
|
|
identifier.type = record.itemType;
|
2021-07-13 18:54:53 +00:00
|
|
|
identifier.raw = Bytes.fromBase64(record.storageID);
|
2020-09-29 23:29:11 +00:00
|
|
|
|
|
|
|
manifestRecordKeys.add(identifier);
|
|
|
|
});
|
|
|
|
|
2021-06-15 00:09:37 +00:00
|
|
|
const recordsWithErrors: ReadonlyArray<UnknownRecord> = window.storage.get(
|
|
|
|
'storage-service-error-records',
|
|
|
|
new Array<UnknownRecord>()
|
|
|
|
);
|
2020-09-29 23:29:11 +00:00
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2020-10-06 22:25:00 +00:00
|
|
|
'storageService.generateManifest: adding records that had errors in the previous merge',
|
|
|
|
recordsWithErrors.length
|
2020-09-29 23:29:11 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// These records failed to merge in the previous fetchManifest, but we still
|
|
|
|
// need to include them so that the manifest is complete
|
|
|
|
recordsWithErrors.forEach((record: UnknownRecord) => {
|
2021-07-13 18:54:53 +00:00
|
|
|
const identifier = new Proto.ManifestRecord.Identifier();
|
2020-09-09 00:56:23 +00:00
|
|
|
identifier.type = record.itemType;
|
2021-07-13 18:54:53 +00:00
|
|
|
identifier.raw = Bytes.fromBase64(record.storageID);
|
2020-09-09 00:56:23 +00:00
|
|
|
|
|
|
|
manifestRecordKeys.add(identifier);
|
|
|
|
});
|
|
|
|
|
2020-09-10 22:37:20 +00:00
|
|
|
// Validate before writing
|
|
|
|
|
|
|
|
const rawDuplicates = new Set();
|
|
|
|
const typeRawDuplicates = new Set();
|
|
|
|
let hasAccountType = false;
|
|
|
|
manifestRecordKeys.forEach(identifier => {
|
|
|
|
// Ensure there are no duplicate StorageIdentifiers in your manifest
|
|
|
|
// This can be broken down into two parts:
|
|
|
|
// There are no duplicate type+raw pairs
|
|
|
|
// There are no duplicate raw bytes
|
2021-07-13 18:54:53 +00:00
|
|
|
strictAssert(identifier.raw, 'manifest record key without raw identifier');
|
|
|
|
const storageID = Bytes.toBase64(identifier.raw);
|
2020-09-10 22:37:20 +00:00
|
|
|
const typeAndRaw = `${identifier.type}+${storageID}`;
|
|
|
|
if (
|
|
|
|
rawDuplicates.has(identifier.raw) ||
|
|
|
|
typeRawDuplicates.has(typeAndRaw)
|
|
|
|
) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2020-09-10 22:37:20 +00:00
|
|
|
'storageService.generateManifest: removing duplicate identifier from manifest',
|
2020-10-07 23:44:55 +00:00
|
|
|
identifier.type
|
2020-09-10 22:37:20 +00:00
|
|
|
);
|
|
|
|
manifestRecordKeys.delete(identifier);
|
|
|
|
}
|
|
|
|
rawDuplicates.add(identifier.raw);
|
|
|
|
typeRawDuplicates.add(typeAndRaw);
|
|
|
|
|
|
|
|
// Ensure all deletes are not present in the manifest
|
|
|
|
const hasDeleteKey = deleteKeys.find(
|
2021-09-24 00:49:05 +00:00
|
|
|
key => Bytes.toBase64(key) === storageID
|
2020-09-10 22:37:20 +00:00
|
|
|
);
|
|
|
|
if (hasDeleteKey) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2020-09-10 22:37:20 +00:00
|
|
|
'storageService.generateManifest: removing key which has been deleted',
|
2020-10-07 23:44:55 +00:00
|
|
|
identifier.type
|
2020-09-10 22:37:20 +00:00
|
|
|
);
|
|
|
|
manifestRecordKeys.delete(identifier);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that there is *exactly* one Account type in the manifest
|
|
|
|
if (identifier.type === ITEM_TYPE.ACCOUNT) {
|
|
|
|
if (hasAccountType) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('storageService.generateManifest: removing duplicate account');
|
2020-09-10 22:37:20 +00:00
|
|
|
manifestRecordKeys.delete(identifier);
|
|
|
|
}
|
|
|
|
hasAccountType = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
rawDuplicates.clear();
|
|
|
|
typeRawDuplicates.clear();
|
|
|
|
|
2021-07-13 18:54:53 +00:00
|
|
|
const storageKeyDuplicates = new Set<string>();
|
2020-09-10 22:37:20 +00:00
|
|
|
|
|
|
|
newItems.forEach(storageItem => {
|
|
|
|
// Ensure there are no duplicate StorageIdentifiers in your list of inserts
|
2021-07-13 18:54:53 +00:00
|
|
|
strictAssert(storageItem.key, 'New storage item without key');
|
|
|
|
|
|
|
|
const storageID = Bytes.toBase64(storageItem.key);
|
2020-09-10 22:37:20 +00:00
|
|
|
if (storageKeyDuplicates.has(storageID)) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2020-09-10 22:37:20 +00:00
|
|
|
'storageService.generateManifest: removing duplicate identifier from inserts',
|
2021-04-06 22:54:47 +00:00
|
|
|
redactStorageID(storageID)
|
2020-09-10 22:37:20 +00:00
|
|
|
);
|
|
|
|
newItems.delete(storageItem);
|
|
|
|
}
|
|
|
|
storageKeyDuplicates.add(storageID);
|
|
|
|
});
|
|
|
|
|
|
|
|
storageKeyDuplicates.clear();
|
|
|
|
|
2021-04-09 20:12:05 +00:00
|
|
|
// If we have a copy of what the current remote manifest is then we run these
|
|
|
|
// additional validations comparing our pending manifest to the remote
|
|
|
|
// manifest:
|
|
|
|
if (previousManifest) {
|
|
|
|
const pendingInserts: Set<string> = new Set();
|
|
|
|
const pendingDeletes: Set<string> = new Set();
|
|
|
|
|
|
|
|
const remoteKeys: Set<string> = new Set();
|
2021-07-13 18:54:53 +00:00
|
|
|
(previousManifest.keys ?? []).forEach(
|
|
|
|
(identifier: IManifestRecordIdentifier) => {
|
|
|
|
strictAssert(identifier.raw, 'Identifier without raw field');
|
|
|
|
const storageID = Bytes.toBase64(identifier.raw);
|
2021-04-09 20:12:05 +00:00
|
|
|
remoteKeys.add(storageID);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
const localKeys: Set<string> = new Set();
|
2021-07-13 18:54:53 +00:00
|
|
|
manifestRecordKeys.forEach((identifier: IManifestRecordIdentifier) => {
|
|
|
|
strictAssert(identifier.raw, 'Identifier without raw field');
|
|
|
|
const storageID = Bytes.toBase64(identifier.raw);
|
2021-04-09 20:12:05 +00:00
|
|
|
localKeys.add(storageID);
|
|
|
|
|
|
|
|
if (!remoteKeys.has(storageID)) {
|
|
|
|
pendingInserts.add(storageID);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
remoteKeys.forEach(storageID => {
|
|
|
|
if (!localKeys.has(storageID)) {
|
|
|
|
pendingDeletes.add(storageID);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (deleteKeys.length !== pendingDeletes.size) {
|
2021-09-16 22:25:49 +00:00
|
|
|
const localDeletes = deleteKeys.map(key =>
|
2021-09-24 00:49:05 +00:00
|
|
|
redactStorageID(Bytes.toBase64(key))
|
2021-09-16 22:25:49 +00:00
|
|
|
);
|
|
|
|
const remoteDeletes: Array<string> = [];
|
|
|
|
pendingDeletes.forEach(id => remoteDeletes.push(redactStorageID(id)));
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error(
|
2021-09-16 22:25:49 +00:00
|
|
|
'Delete key sizes do not match',
|
|
|
|
'local',
|
|
|
|
localDeletes.join(','),
|
|
|
|
'remote',
|
|
|
|
remoteDeletes.join(',')
|
|
|
|
);
|
2021-04-09 20:12:05 +00:00
|
|
|
throw new Error('invalid write delete keys length do not match');
|
|
|
|
}
|
|
|
|
if (newItems.size !== pendingInserts.size) {
|
|
|
|
throw new Error('invalid write insert items length do not match');
|
|
|
|
}
|
|
|
|
deleteKeys.forEach(key => {
|
2021-09-24 00:49:05 +00:00
|
|
|
const storageID = Bytes.toBase64(key);
|
2021-04-09 20:12:05 +00:00
|
|
|
if (!pendingDeletes.has(storageID)) {
|
|
|
|
throw new Error(
|
|
|
|
'invalid write delete key missing from pending deletes'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
insertKeys.forEach(storageID => {
|
|
|
|
if (!pendingInserts.has(storageID)) {
|
|
|
|
throw new Error(
|
|
|
|
'invalid write insert key missing from pending inserts'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-07-13 18:54:53 +00:00
|
|
|
const manifestRecord = new Proto.ManifestRecord();
|
2020-09-09 00:56:23 +00:00
|
|
|
manifestRecord.version = version;
|
|
|
|
manifestRecord.keys = Array.from(manifestRecordKeys);
|
|
|
|
|
|
|
|
const storageKeyBase64 = window.storage.get('storageKey');
|
2021-06-15 00:09:37 +00:00
|
|
|
if (!storageKeyBase64) {
|
|
|
|
throw new Error('No storage key');
|
|
|
|
}
|
2021-09-24 00:49:05 +00:00
|
|
|
const storageKey = Bytes.fromBase64(storageKeyBase64);
|
|
|
|
const storageManifestKey = deriveStorageManifestKey(storageKey, version);
|
|
|
|
const encryptedManifest = encryptProfile(
|
|
|
|
Proto.ManifestRecord.encode(manifestRecord).finish(),
|
2020-09-09 00:56:23 +00:00
|
|
|
storageManifestKey
|
|
|
|
);
|
|
|
|
|
2021-07-13 18:54:53 +00:00
|
|
|
const storageManifest = new Proto.StorageManifest();
|
2020-09-09 00:56:23 +00:00
|
|
|
storageManifest.version = version;
|
2021-09-24 00:49:05 +00:00
|
|
|
storageManifest.value = encryptedManifest;
|
2020-09-09 00:56:23 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
conversationsToUpdate,
|
|
|
|
deleteKeys,
|
|
|
|
newItems,
|
|
|
|
storageManifest,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async function uploadManifest(
|
|
|
|
version: number,
|
|
|
|
{
|
|
|
|
conversationsToUpdate,
|
|
|
|
deleteKeys,
|
|
|
|
newItems,
|
|
|
|
storageManifest,
|
|
|
|
}: GeneratedManifestType
|
|
|
|
): Promise<void> {
|
|
|
|
if (!window.textsecure.messaging) {
|
|
|
|
throw new Error('storageService.uploadManifest: We are offline!');
|
|
|
|
}
|
|
|
|
|
2020-09-29 23:29:11 +00:00
|
|
|
if (newItems.size === 0 && deleteKeys.length === 0) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('storageService.uploadManifest: nothing to upload');
|
2020-09-29 23:29:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-09 00:56:23 +00:00
|
|
|
const credentials = window.storage.get('storageCredentials');
|
|
|
|
try {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2020-10-06 22:25:00 +00:00
|
|
|
'storageService.uploadManifest: keys inserting, deleting:',
|
|
|
|
newItems.size,
|
|
|
|
deleteKeys.length
|
2020-09-09 00:56:23 +00:00
|
|
|
);
|
|
|
|
|
2021-07-13 18:54:53 +00:00
|
|
|
const writeOperation = new Proto.WriteOperation();
|
2020-09-10 22:37:20 +00:00
|
|
|
writeOperation.manifest = storageManifest;
|
|
|
|
writeOperation.insertItem = Array.from(newItems);
|
2021-09-24 00:49:05 +00:00
|
|
|
writeOperation.deleteKey = deleteKeys;
|
2020-09-09 00:56:23 +00:00
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('storageService.uploadManifest: uploading...', version);
|
2020-09-09 00:56:23 +00:00
|
|
|
await window.textsecure.messaging.modifyStorageRecords(
|
2021-09-24 00:49:05 +00:00
|
|
|
Proto.WriteOperation.encode(writeOperation).finish(),
|
2020-09-09 00:56:23 +00:00
|
|
|
{
|
|
|
|
credentials,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2020-10-06 22:25:00 +00:00
|
|
|
'storageService.uploadManifest: upload done, updating conversation(s) with new storageIDs:',
|
|
|
|
conversationsToUpdate.length
|
2020-09-09 00:56:23 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// update conversations with the new storageID
|
|
|
|
conversationsToUpdate.forEach(({ conversation, storageID }) => {
|
|
|
|
conversation.set({
|
|
|
|
needsStorageServiceSync: false,
|
|
|
|
storageID,
|
|
|
|
});
|
|
|
|
updateConversation(conversation.attributes);
|
|
|
|
});
|
|
|
|
} catch (err) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error(
|
2020-10-07 23:44:55 +00:00
|
|
|
'storageService.uploadManifest: failed!',
|
|
|
|
err && err.stack ? err.stack : String(err)
|
2020-09-09 00:56:23 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if (err.code === 409) {
|
2021-06-09 22:28:54 +00:00
|
|
|
if (conflictBackOff.isFull()) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error(
|
2020-09-09 00:56:23 +00:00
|
|
|
'storageService.uploadManifest: Exceeded maximum consecutive conflicts'
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2021-06-09 22:28:54 +00:00
|
|
|
`storageService.uploadManifest: Conflict found with v${version}, running sync job times(${conflictBackOff.getIndex()})`
|
2020-09-09 23:07:58 +00:00
|
|
|
);
|
|
|
|
|
2020-09-09 00:56:23 +00:00
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2020-09-09 00:56:23 +00:00
|
|
|
'storageService.uploadManifest: setting new manifestVersion',
|
|
|
|
version
|
|
|
|
);
|
|
|
|
window.storage.put('manifestVersion', version);
|
2021-06-09 22:28:54 +00:00
|
|
|
conflictBackOff.reset();
|
|
|
|
backOff.reset();
|
2021-07-15 23:48:09 +00:00
|
|
|
|
|
|
|
if (window.ConversationController.areWePrimaryDevice()) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn(
|
2021-07-15 23:48:09 +00:00
|
|
|
'uploadManifest: We are primary device; not sending sync manifest'
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await handleMessageSend(
|
|
|
|
window.textsecure.messaging.sendFetchManifestSyncMessage(),
|
|
|
|
{ messageIds: [], sendType: 'otherSync' }
|
|
|
|
);
|
2020-09-09 00:56:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function stopStorageServiceSync() {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('storageService.stopStorageServiceSync');
|
2020-09-09 00:56:23 +00:00
|
|
|
|
|
|
|
await window.storage.remove('storageKey');
|
|
|
|
|
2021-06-09 22:28:54 +00:00
|
|
|
if (backOff.isFull()) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2021-06-09 22:28:54 +00:00
|
|
|
'storageService.stopStorageServiceSync: too many consecutive stops'
|
2020-09-09 23:07:58 +00:00
|
|
|
);
|
2021-06-09 22:28:54 +00:00
|
|
|
return;
|
2020-09-09 00:56:23 +00:00
|
|
|
}
|
2021-06-09 22:28:54 +00:00
|
|
|
|
|
|
|
await sleep(backOff.getAndIncrement());
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('storageService.stopStorageServiceSync: requesting new keys');
|
2021-06-09 22:28:54 +00:00
|
|
|
setTimeout(() => {
|
|
|
|
if (!window.textsecure.messaging) {
|
|
|
|
throw new Error('storageService.stopStorageServiceSync: We are offline!');
|
|
|
|
}
|
2021-07-15 23:48:09 +00:00
|
|
|
|
|
|
|
if (window.ConversationController.areWePrimaryDevice()) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn(
|
2021-07-15 23:48:09 +00:00
|
|
|
'stopStorageServiceSync: We are primary device; not sending key sync request'
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
handleMessageSend(window.textsecure.messaging.sendRequestKeySyncMessage(), {
|
|
|
|
messageIds: [],
|
|
|
|
sendType: 'otherSync',
|
|
|
|
});
|
2021-06-09 22:28:54 +00:00
|
|
|
});
|
2020-09-09 00:56:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function createNewManifest() {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('storageService.createNewManifest: creating new manifest');
|
2020-09-09 00:56:23 +00:00
|
|
|
|
2021-06-15 00:09:37 +00:00
|
|
|
const version = window.storage.get('manifestVersion', 0);
|
2020-09-09 00:56:23 +00:00
|
|
|
|
2021-11-11 22:43:05 +00:00
|
|
|
const { conversationsToUpdate, newItems, storageManifest } =
|
|
|
|
await generateManifest(version, undefined, true);
|
2020-09-09 00:56:23 +00:00
|
|
|
|
|
|
|
await uploadManifest(version, {
|
|
|
|
conversationsToUpdate,
|
|
|
|
// we have created a new manifest, there should be no keys to delete
|
|
|
|
deleteKeys: [],
|
|
|
|
newItems,
|
|
|
|
storageManifest,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function decryptManifest(
|
2021-07-13 18:54:53 +00:00
|
|
|
encryptedManifest: Proto.IStorageManifest
|
|
|
|
): Promise<Proto.ManifestRecord> {
|
2020-09-09 00:56:23 +00:00
|
|
|
const { version, value } = encryptedManifest;
|
|
|
|
|
|
|
|
const storageKeyBase64 = window.storage.get('storageKey');
|
2021-06-15 00:09:37 +00:00
|
|
|
if (!storageKeyBase64) {
|
|
|
|
throw new Error('No storage key');
|
|
|
|
}
|
2021-09-24 00:49:05 +00:00
|
|
|
const storageKey = Bytes.fromBase64(storageKeyBase64);
|
|
|
|
const storageManifestKey = deriveStorageManifestKey(
|
2020-09-09 00:56:23 +00:00
|
|
|
storageKey,
|
2021-07-13 18:54:53 +00:00
|
|
|
normalizeNumber(version ?? 0)
|
2020-09-09 00:56:23 +00:00
|
|
|
);
|
|
|
|
|
2021-07-13 18:54:53 +00:00
|
|
|
strictAssert(value, 'StorageManifest has no value field');
|
2021-09-24 00:49:05 +00:00
|
|
|
const decryptedManifest = decryptProfile(value, storageManifestKey);
|
2020-09-09 00:56:23 +00:00
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
return Proto.ManifestRecord.decode(decryptedManifest);
|
2020-09-09 00:56:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function fetchManifest(
|
2021-06-15 00:09:37 +00:00
|
|
|
manifestVersion: number
|
2021-07-13 18:54:53 +00:00
|
|
|
): Promise<Proto.ManifestRecord | undefined> {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('storageService.fetchManifest');
|
2020-09-09 00:56:23 +00:00
|
|
|
|
|
|
|
if (!window.textsecure.messaging) {
|
|
|
|
throw new Error('storageService.fetchManifest: We are offline!');
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2021-11-11 22:43:05 +00:00
|
|
|
const credentials =
|
|
|
|
await window.textsecure.messaging.getStorageCredentials();
|
2020-09-09 00:56:23 +00:00
|
|
|
window.storage.put('storageCredentials', credentials);
|
|
|
|
|
|
|
|
const manifestBinary = await window.textsecure.messaging.getStorageManifest(
|
|
|
|
{
|
|
|
|
credentials,
|
|
|
|
greaterThanVersion: manifestVersion,
|
|
|
|
}
|
|
|
|
);
|
2021-09-24 00:49:05 +00:00
|
|
|
const encryptedManifest = Proto.StorageManifest.decode(manifestBinary);
|
2020-09-09 00:56:23 +00:00
|
|
|
|
|
|
|
// if we don't get a value we're assuming that there's no newer manifest
|
|
|
|
if (!encryptedManifest.value || !encryptedManifest.version) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('storageService.fetchManifest: nothing changed');
|
2020-09-09 00:56:23 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
return decryptManifest(encryptedManifest);
|
|
|
|
} catch (err) {
|
|
|
|
await stopStorageServiceSync();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} catch (err) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error(
|
2020-10-07 23:44:55 +00:00
|
|
|
'storageService.fetchManifest: failed!',
|
|
|
|
err && err.stack ? err.stack : String(err)
|
2020-09-09 00:56:23 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if (err.code === 404) {
|
|
|
|
await createNewManifest();
|
|
|
|
return;
|
2020-09-09 02:25:05 +00:00
|
|
|
}
|
|
|
|
if (err.code === 204) {
|
2020-09-09 00:56:23 +00:00
|
|
|
// noNewerManifest we're ok
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type MergeableItemType = {
|
|
|
|
itemType: number;
|
|
|
|
storageID: string;
|
2021-07-13 18:54:53 +00:00
|
|
|
storageRecord: Proto.IStorageRecord;
|
2020-09-09 00:56:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
type MergedRecordType = UnknownRecord & {
|
|
|
|
hasConflict: boolean;
|
2020-09-29 23:29:11 +00:00
|
|
|
hasError: boolean;
|
2020-09-09 00:56:23 +00:00
|
|
|
isUnsupported: boolean;
|
|
|
|
};
|
|
|
|
|
|
|
|
async function mergeRecord(
|
|
|
|
itemToMerge: MergeableItemType
|
|
|
|
): Promise<MergedRecordType> {
|
|
|
|
const { itemType, storageID, storageRecord } = itemToMerge;
|
|
|
|
|
2021-07-13 18:54:53 +00:00
|
|
|
const ITEM_TYPE = Proto.ManifestRecord.Identifier.Type;
|
2020-09-09 00:56:23 +00:00
|
|
|
|
|
|
|
let hasConflict = false;
|
|
|
|
let isUnsupported = false;
|
2020-09-29 23:29:11 +00:00
|
|
|
let hasError = false;
|
2020-09-09 00:56:23 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
if (itemType === ITEM_TYPE.UNKNOWN) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('storageService.mergeRecord: Unknown item type', storageID);
|
2020-09-09 00:56:23 +00:00
|
|
|
} else if (itemType === ITEM_TYPE.CONTACT && storageRecord.contact) {
|
|
|
|
hasConflict = await mergeContactRecord(storageID, storageRecord.contact);
|
|
|
|
} else if (itemType === ITEM_TYPE.GROUPV1 && storageRecord.groupV1) {
|
|
|
|
hasConflict = await mergeGroupV1Record(storageID, storageRecord.groupV1);
|
2020-10-07 18:27:08 +00:00
|
|
|
} else if (itemType === ITEM_TYPE.GROUPV2 && storageRecord.groupV2) {
|
2020-09-09 02:25:05 +00:00
|
|
|
hasConflict = await mergeGroupV2Record(storageID, storageRecord.groupV2);
|
2020-09-09 00:56:23 +00:00
|
|
|
} else if (itemType === ITEM_TYPE.ACCOUNT && storageRecord.account) {
|
|
|
|
hasConflict = await mergeAccountRecord(storageID, storageRecord.account);
|
|
|
|
} else {
|
|
|
|
isUnsupported = true;
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('storageService.mergeRecord: Unknown record:', itemType);
|
2020-09-09 00:56:23 +00:00
|
|
|
}
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2021-04-06 22:54:47 +00:00
|
|
|
'storageService.mergeRecord: merged',
|
|
|
|
redactStorageID(storageID),
|
|
|
|
itemType,
|
|
|
|
hasConflict
|
|
|
|
);
|
2020-09-09 00:56:23 +00:00
|
|
|
} catch (err) {
|
2020-09-29 23:29:11 +00:00
|
|
|
hasError = true;
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error(
|
2021-03-31 00:16:28 +00:00
|
|
|
'storageService.mergeRecord: Error with',
|
2021-04-06 22:54:47 +00:00
|
|
|
redactStorageID(storageID),
|
2021-04-09 16:19:38 +00:00
|
|
|
itemType,
|
|
|
|
String(err)
|
2020-09-09 00:56:23 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
hasConflict,
|
2020-09-29 23:29:11 +00:00
|
|
|
hasError,
|
2020-09-09 00:56:23 +00:00
|
|
|
isUnsupported,
|
|
|
|
itemType,
|
|
|
|
storageID,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async function processManifest(
|
2021-07-13 18:54:53 +00:00
|
|
|
manifest: Proto.IManifestRecord
|
2020-09-09 00:56:23 +00:00
|
|
|
): Promise<boolean> {
|
|
|
|
if (!window.textsecure.messaging) {
|
|
|
|
throw new Error('storageService.processManifest: We are offline!');
|
|
|
|
}
|
|
|
|
|
|
|
|
const remoteKeysTypeMap = new Map();
|
2021-07-13 18:54:53 +00:00
|
|
|
(manifest.keys || []).forEach(({ raw, type }: IManifestRecordIdentifier) => {
|
|
|
|
strictAssert(raw, 'Identifier without raw field');
|
|
|
|
remoteKeysTypeMap.set(Bytes.toBase64(raw), type);
|
2020-09-09 00:56:23 +00:00
|
|
|
});
|
|
|
|
|
2021-04-08 19:27:20 +00:00
|
|
|
const remoteKeys = new Set(remoteKeysTypeMap.keys());
|
|
|
|
const localKeys: Set<string> = new Set();
|
|
|
|
|
|
|
|
const conversations = window.getConversations();
|
|
|
|
conversations.forEach((conversation: ConversationModel) => {
|
|
|
|
const storageID = conversation.get('storageID');
|
|
|
|
if (storageID) {
|
|
|
|
localKeys.add(storageID);
|
|
|
|
}
|
|
|
|
});
|
2020-09-09 00:56:23 +00:00
|
|
|
|
2021-03-17 22:10:31 +00:00
|
|
|
const unknownRecordsArray: ReadonlyArray<UnknownRecord> =
|
2020-09-09 00:56:23 +00:00
|
|
|
window.storage.get('storage-service-unknown-records') || [];
|
|
|
|
|
2021-04-08 19:27:20 +00:00
|
|
|
const stillUnknown = unknownRecordsArray.filter((record: UnknownRecord) => {
|
2020-09-29 23:29:11 +00:00
|
|
|
// Do not include any unknown records that we already support
|
|
|
|
if (!validRecordTypes.has(record.itemType)) {
|
2021-04-08 19:27:20 +00:00
|
|
|
localKeys.add(record.storageID);
|
|
|
|
return false;
|
2020-09-29 23:29:11 +00:00
|
|
|
}
|
2021-04-08 19:27:20 +00:00
|
|
|
return true;
|
2020-09-09 00:56:23 +00:00
|
|
|
});
|
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2021-04-08 19:27:20 +00:00
|
|
|
'storageService.processManifest: local records:',
|
|
|
|
conversations.length
|
|
|
|
);
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('storageService.processManifest: local keys:', localKeys.size);
|
|
|
|
log.info(
|
2021-04-08 19:27:20 +00:00
|
|
|
'storageService.processManifest: unknown records:',
|
|
|
|
stillUnknown.length
|
|
|
|
);
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('storageService.processManifest: remote keys:', remoteKeys.size);
|
2020-09-09 00:56:23 +00:00
|
|
|
|
2020-09-29 23:29:11 +00:00
|
|
|
const remoteOnlySet: Set<string> = new Set();
|
|
|
|
remoteKeys.forEach((key: string) => {
|
2021-04-08 19:27:20 +00:00
|
|
|
if (!localKeys.has(key)) {
|
2020-09-29 23:29:11 +00:00
|
|
|
remoteOnlySet.add(key);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2021-04-08 19:27:20 +00:00
|
|
|
'storageService.processManifest: remote ids:',
|
|
|
|
Array.from(remoteOnlySet).map(redactStorageID).join(',')
|
|
|
|
);
|
|
|
|
|
2021-03-17 22:10:31 +00:00
|
|
|
const remoteOnlyRecords = new Map<string, RemoteRecord>();
|
|
|
|
remoteOnlySet.forEach(storageID => {
|
|
|
|
remoteOnlyRecords.set(storageID, {
|
|
|
|
storageID,
|
|
|
|
itemType: remoteKeysTypeMap.get(storageID),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-10-02 00:01:30 +00:00
|
|
|
if (!remoteOnlyRecords.size) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-04-09 20:12:05 +00:00
|
|
|
const conflictCount = await processRemoteRecords(remoteOnlyRecords);
|
2021-04-08 19:27:20 +00:00
|
|
|
|
|
|
|
// Post-merge, if our local records contain any storage IDs that were not
|
|
|
|
// present in the remote manifest then we'll need to clear it, generate a
|
|
|
|
// new storageID for that record, and upload.
|
|
|
|
// This might happen if a device pushes a manifest which doesn't contain
|
|
|
|
// the keys that we have in our local database.
|
|
|
|
window.getConversations().forEach((conversation: ConversationModel) => {
|
|
|
|
const storageID = conversation.get('storageID');
|
|
|
|
if (storageID && !remoteKeys.has(storageID)) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2021-04-08 19:27:20 +00:00
|
|
|
'storageService.processManifest: local key was not in remote manifest',
|
|
|
|
redactStorageID(storageID),
|
|
|
|
conversation.idForLogging()
|
|
|
|
);
|
2021-04-09 20:12:05 +00:00
|
|
|
conversation.unset('storageID');
|
2021-04-08 19:27:20 +00:00
|
|
|
updateConversation(conversation.attributes);
|
|
|
|
}
|
|
|
|
});
|
2021-03-17 22:10:31 +00:00
|
|
|
|
2021-04-09 20:12:05 +00:00
|
|
|
return conflictCount !== 0;
|
2021-03-17 22:10:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function processRemoteRecords(
|
2021-04-09 20:12:05 +00:00
|
|
|
remoteOnlyRecords: Map<string, RemoteRecord>
|
2021-03-17 22:10:31 +00:00
|
|
|
): Promise<number> {
|
|
|
|
const storageKeyBase64 = window.storage.get('storageKey');
|
2021-06-15 00:09:37 +00:00
|
|
|
if (!storageKeyBase64) {
|
|
|
|
throw new Error('No storage key');
|
|
|
|
}
|
2021-09-24 00:49:05 +00:00
|
|
|
const storageKey = Bytes.fromBase64(storageKeyBase64);
|
2020-09-09 00:56:23 +00:00
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2021-04-08 19:27:20 +00:00
|
|
|
'storageService.processRemoteRecords: remote only keys',
|
2021-03-17 22:10:31 +00:00
|
|
|
remoteOnlyRecords.size
|
2020-09-09 00:56:23 +00:00
|
|
|
);
|
|
|
|
|
2021-07-13 18:54:53 +00:00
|
|
|
const readOperation = new Proto.ReadOperation();
|
2021-03-17 22:10:31 +00:00
|
|
|
readOperation.readKey = Array.from(remoteOnlyRecords.keys()).map(
|
2021-07-13 18:54:53 +00:00
|
|
|
Bytes.fromBase64
|
2021-03-17 22:10:31 +00:00
|
|
|
);
|
2020-09-09 00:56:23 +00:00
|
|
|
|
|
|
|
const credentials = window.storage.get('storageCredentials');
|
2021-11-11 22:43:05 +00:00
|
|
|
const storageItemsBuffer =
|
|
|
|
await window.textsecure.messaging.getStorageRecords(
|
|
|
|
Proto.ReadOperation.encode(readOperation).finish(),
|
|
|
|
{
|
|
|
|
credentials,
|
|
|
|
}
|
|
|
|
);
|
2020-09-09 00:56:23 +00:00
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
const storageItems = Proto.StorageItems.decode(storageItemsBuffer);
|
2020-09-09 00:56:23 +00:00
|
|
|
|
|
|
|
if (!storageItems.items) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('storageService.processRemoteRecords: No storage items retrieved');
|
2021-03-17 22:10:31 +00:00
|
|
|
return 0;
|
2020-09-09 00:56:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const decryptedStorageItems = await pMap(
|
|
|
|
storageItems.items,
|
2020-09-29 22:07:03 +00:00
|
|
|
async (
|
2021-07-13 18:54:53 +00:00
|
|
|
storageRecordWrapper: Proto.IStorageItem
|
2020-09-29 22:07:03 +00:00
|
|
|
): Promise<MergeableItemType> => {
|
2020-09-09 00:56:23 +00:00
|
|
|
const { key, value: storageItemCiphertext } = storageRecordWrapper;
|
|
|
|
|
|
|
|
if (!key || !storageItemCiphertext) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error(
|
2021-03-17 22:10:31 +00:00
|
|
|
'storageService.processRemoteRecords: No key or Ciphertext available'
|
2020-09-09 23:07:58 +00:00
|
|
|
);
|
2020-09-09 00:56:23 +00:00
|
|
|
await stopStorageServiceSync();
|
|
|
|
throw new Error(
|
2021-03-17 22:10:31 +00:00
|
|
|
'storageService.processRemoteRecords: Missing key and/or Ciphertext'
|
2020-09-09 00:56:23 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-13 18:54:53 +00:00
|
|
|
const base64ItemID = Bytes.toBase64(key);
|
2020-09-09 00:56:23 +00:00
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
const storageItemKey = deriveStorageItemKey(storageKey, base64ItemID);
|
2020-09-09 00:56:23 +00:00
|
|
|
|
|
|
|
let storageItemPlaintext;
|
|
|
|
try {
|
2021-09-24 00:49:05 +00:00
|
|
|
storageItemPlaintext = decryptProfile(
|
|
|
|
storageItemCiphertext,
|
2020-09-09 00:56:23 +00:00
|
|
|
storageItemKey
|
|
|
|
);
|
|
|
|
} catch (err) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error(
|
2021-03-17 22:10:31 +00:00
|
|
|
'storageService.processRemoteRecords: Error decrypting storage item'
|
2020-09-09 23:07:58 +00:00
|
|
|
);
|
2020-09-09 00:56:23 +00:00
|
|
|
await stopStorageServiceSync();
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
const storageRecord = Proto.StorageRecord.decode(storageItemPlaintext);
|
2020-09-09 00:56:23 +00:00
|
|
|
|
2021-03-17 22:10:31 +00:00
|
|
|
const remoteRecord = remoteOnlyRecords.get(base64ItemID);
|
|
|
|
if (!remoteRecord) {
|
|
|
|
throw new Error(
|
|
|
|
"Got a remote record that wasn't requested with " +
|
|
|
|
`storageID: ${base64ItemID}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-09 00:56:23 +00:00
|
|
|
return {
|
2021-03-17 22:10:31 +00:00
|
|
|
itemType: remoteRecord.itemType,
|
2020-09-09 00:56:23 +00:00
|
|
|
storageID: base64ItemID,
|
|
|
|
storageRecord,
|
|
|
|
};
|
|
|
|
},
|
2020-09-29 23:29:11 +00:00
|
|
|
{ concurrency: 5 }
|
2020-09-09 00:56:23 +00:00
|
|
|
);
|
|
|
|
|
2021-04-08 19:27:20 +00:00
|
|
|
// Merge Account records last since it contains the pinned conversations
|
|
|
|
// and we need all other records merged first before we can find the pinned
|
|
|
|
// records in our db
|
2021-07-13 18:54:53 +00:00
|
|
|
const ITEM_TYPE = Proto.ManifestRecord.Identifier.Type;
|
2021-04-08 19:27:20 +00:00
|
|
|
const sortedStorageItems = decryptedStorageItems.sort((_, b) =>
|
|
|
|
b.itemType === ITEM_TYPE.ACCOUNT ? -1 : 1
|
2020-09-29 22:07:03 +00:00
|
|
|
);
|
|
|
|
|
2020-09-09 00:56:23 +00:00
|
|
|
try {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2021-03-17 22:10:31 +00:00
|
|
|
`storageService.processRemoteRecords: Attempting to merge ${sortedStorageItems.length} records`
|
2020-09-16 18:04:28 +00:00
|
|
|
);
|
2020-09-29 22:07:03 +00:00
|
|
|
const mergedRecords = await pMap(sortedStorageItems, mergeRecord, {
|
2020-09-09 00:56:23 +00:00
|
|
|
concurrency: 5,
|
|
|
|
});
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2021-03-17 22:10:31 +00:00
|
|
|
`storageService.processRemoteRecords: Processed ${mergedRecords.length} records`
|
2020-09-16 18:04:28 +00:00
|
|
|
);
|
2020-09-09 00:56:23 +00:00
|
|
|
|
2021-03-17 22:10:31 +00:00
|
|
|
// Collect full map of previously and currently unknown records
|
2020-09-09 00:56:23 +00:00
|
|
|
const unknownRecords: Map<string, UnknownRecord> = new Map();
|
2021-03-17 22:10:31 +00:00
|
|
|
|
2021-11-11 22:43:05 +00:00
|
|
|
const unknownRecordsArray: ReadonlyArray<UnknownRecord> =
|
|
|
|
window.storage.get(
|
|
|
|
'storage-service-unknown-records',
|
|
|
|
new Array<UnknownRecord>()
|
|
|
|
);
|
2020-09-09 00:56:23 +00:00
|
|
|
unknownRecordsArray.forEach((record: UnknownRecord) => {
|
|
|
|
unknownRecords.set(record.storageID, record);
|
|
|
|
});
|
|
|
|
|
2020-10-06 22:25:00 +00:00
|
|
|
const newRecordsWithErrors: Array<UnknownRecord> = [];
|
2020-09-29 23:29:11 +00:00
|
|
|
|
2021-03-17 22:10:31 +00:00
|
|
|
let conflictCount = 0;
|
2020-09-29 23:29:11 +00:00
|
|
|
|
|
|
|
mergedRecords.forEach((mergedRecord: MergedRecordType) => {
|
2020-09-09 00:56:23 +00:00
|
|
|
if (mergedRecord.isUnsupported) {
|
|
|
|
unknownRecords.set(mergedRecord.storageID, {
|
|
|
|
itemType: mergedRecord.itemType,
|
|
|
|
storageID: mergedRecord.storageID,
|
|
|
|
});
|
2020-09-29 23:29:11 +00:00
|
|
|
} else if (mergedRecord.hasError) {
|
2020-10-06 22:25:00 +00:00
|
|
|
newRecordsWithErrors.push({
|
2020-09-29 23:29:11 +00:00
|
|
|
itemType: mergedRecord.itemType,
|
|
|
|
storageID: mergedRecord.storageID,
|
|
|
|
});
|
2020-09-09 00:56:23 +00:00
|
|
|
}
|
2020-09-29 23:29:11 +00:00
|
|
|
|
2021-03-17 22:10:31 +00:00
|
|
|
if (mergedRecord.hasConflict) {
|
|
|
|
conflictCount += 1;
|
|
|
|
}
|
2020-09-09 00:56:23 +00:00
|
|
|
});
|
|
|
|
|
2020-09-29 23:29:11 +00:00
|
|
|
// Filter out all the unknown records we're already supporting
|
|
|
|
const newUnknownRecords = Array.from(unknownRecords.values()).filter(
|
|
|
|
(record: UnknownRecord) => !validRecordTypes.has(record.itemType)
|
|
|
|
);
|
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2021-03-17 22:10:31 +00:00
|
|
|
'storageService.processRemoteRecords: Unknown records found:',
|
2020-10-06 22:25:00 +00:00
|
|
|
newUnknownRecords.length
|
2020-09-09 00:56:23 +00:00
|
|
|
);
|
2020-09-29 23:29:11 +00:00
|
|
|
window.storage.put('storage-service-unknown-records', newUnknownRecords);
|
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2021-03-17 22:10:31 +00:00
|
|
|
'storageService.processRemoteRecords: Records with errors:',
|
2020-10-06 22:25:00 +00:00
|
|
|
newRecordsWithErrors.length
|
2020-09-29 23:29:11 +00:00
|
|
|
);
|
2020-10-06 22:25:00 +00:00
|
|
|
// Refresh the list of records that had errors with every push, that way
|
|
|
|
// this list doesn't grow unbounded and we keep the list of storage keys
|
|
|
|
// fresh.
|
|
|
|
window.storage.put('storage-service-error-records', newRecordsWithErrors);
|
2020-09-29 23:29:11 +00:00
|
|
|
|
2021-03-17 22:10:31 +00:00
|
|
|
if (conflictCount !== 0) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2021-03-17 22:10:31 +00:00
|
|
|
'storageService.processRemoteRecords: ' +
|
|
|
|
`${conflictCount} conflicts found, uploading changes`
|
2020-09-09 00:56:23 +00:00
|
|
|
);
|
|
|
|
|
2021-03-17 22:10:31 +00:00
|
|
|
return conflictCount;
|
2020-09-09 00:56:23 +00:00
|
|
|
}
|
2020-09-09 02:25:05 +00:00
|
|
|
|
2021-06-09 22:28:54 +00:00
|
|
|
conflictBackOff.reset();
|
2020-09-09 00:56:23 +00:00
|
|
|
} catch (err) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error(
|
2021-03-17 22:10:31 +00:00
|
|
|
'storageService.processRemoteRecords: failed!',
|
2020-10-07 23:44:55 +00:00
|
|
|
err && err.stack ? err.stack : String(err)
|
2020-09-09 00:56:23 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-03-17 22:10:31 +00:00
|
|
|
return 0;
|
2020-09-09 00:56:23 +00:00
|
|
|
}
|
|
|
|
|
2021-09-16 22:25:49 +00:00
|
|
|
async function sync(
|
|
|
|
ignoreConflicts = false
|
|
|
|
): Promise<Proto.ManifestRecord | undefined> {
|
2020-09-09 00:56:23 +00:00
|
|
|
if (!window.storage.get('storageKey')) {
|
2020-09-29 23:29:11 +00:00
|
|
|
throw new Error('storageService.sync: Cannot start; no storage key!');
|
2020-09-09 00:56:23 +00:00
|
|
|
}
|
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('storageService.sync: starting...');
|
2020-09-09 00:56:23 +00:00
|
|
|
|
2021-07-13 18:54:53 +00:00
|
|
|
let manifest: Proto.ManifestRecord | undefined;
|
2020-09-09 00:56:23 +00:00
|
|
|
try {
|
2020-10-29 00:40:50 +00:00
|
|
|
// If we've previously interacted with strage service, update 'fetchComplete' record
|
|
|
|
const previousFetchComplete = window.storage.get('storageFetchComplete');
|
|
|
|
const manifestFromStorage = window.storage.get('manifestVersion');
|
|
|
|
if (!previousFetchComplete && isNumber(manifestFromStorage)) {
|
|
|
|
window.storage.put('storageFetchComplete', true);
|
|
|
|
}
|
|
|
|
|
|
|
|
const localManifestVersion = manifestFromStorage || 0;
|
2021-04-09 20:12:05 +00:00
|
|
|
manifest = await fetchManifest(localManifestVersion);
|
2020-09-09 00:56:23 +00:00
|
|
|
|
|
|
|
// Guarding against no manifests being returned, everything should be ok
|
|
|
|
if (!manifest) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('storageService.sync: no new manifest');
|
2021-04-09 20:12:05 +00:00
|
|
|
return undefined;
|
2020-09-09 00:56:23 +00:00
|
|
|
}
|
|
|
|
|
2021-07-13 18:54:53 +00:00
|
|
|
strictAssert(
|
|
|
|
manifest.version !== undefined && manifest.version !== null,
|
|
|
|
'Manifest without version'
|
|
|
|
);
|
|
|
|
const version = normalizeNumber(manifest.version);
|
2020-09-09 00:56:23 +00:00
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2020-09-29 23:29:11 +00:00
|
|
|
`storageService.sync: manifest versions - previous: ${localManifestVersion}, current: ${version}`
|
2020-09-09 00:56:23 +00:00
|
|
|
);
|
|
|
|
|
2020-09-29 23:29:11 +00:00
|
|
|
window.storage.put('manifestVersion', version);
|
|
|
|
|
2020-09-09 00:56:23 +00:00
|
|
|
const hasConflicts = await processManifest(manifest);
|
2021-09-16 22:25:49 +00:00
|
|
|
if (hasConflicts && !ignoreConflicts) {
|
2021-03-12 17:38:43 +00:00
|
|
|
await upload(true);
|
2020-09-09 00:56:23 +00:00
|
|
|
}
|
2020-10-29 00:40:50 +00:00
|
|
|
|
|
|
|
// We now know that we've successfully completed a storage service fetch
|
|
|
|
window.storage.put('storageFetchComplete', true);
|
2020-09-09 00:56:23 +00:00
|
|
|
} catch (err) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error(
|
2020-10-07 23:44:55 +00:00
|
|
|
'storageService.sync: error processing manifest',
|
|
|
|
err && err.stack ? err.stack : String(err)
|
2020-09-09 00:56:23 +00:00
|
|
|
);
|
2020-09-29 23:29:11 +00:00
|
|
|
}
|
2020-09-09 00:56:23 +00:00
|
|
|
|
2021-04-20 23:16:49 +00:00
|
|
|
window.Signal.Util.postLinkExperience.stop();
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('storageService.sync: complete');
|
2021-04-09 20:12:05 +00:00
|
|
|
return manifest;
|
2020-09-09 00:56:23 +00:00
|
|
|
}
|
|
|
|
|
2021-03-12 17:38:43 +00:00
|
|
|
async function upload(fromSync = false): Promise<void> {
|
2020-09-09 23:07:58 +00:00
|
|
|
if (!window.textsecure.messaging) {
|
2020-09-29 23:29:11 +00:00
|
|
|
throw new Error('storageService.upload: We are offline!');
|
2020-09-09 23:07:58 +00:00
|
|
|
}
|
|
|
|
|
2021-04-09 20:12:05 +00:00
|
|
|
// Rate limit uploads coming from syncing
|
|
|
|
if (fromSync) {
|
|
|
|
uploadBucket.push(Date.now());
|
|
|
|
if (uploadBucket.length >= 3) {
|
|
|
|
const [firstMostRecentWrite] = uploadBucket;
|
|
|
|
|
2021-08-26 14:10:58 +00:00
|
|
|
if (isMoreRecentThan(5 * durations.MINUTE, firstMostRecentWrite)) {
|
2021-04-09 20:12:05 +00:00
|
|
|
throw new Error(
|
|
|
|
'storageService.uploadManifest: too many writes too soon.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
uploadBucket.shift();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-09 00:56:23 +00:00
|
|
|
if (!window.storage.get('storageKey')) {
|
2020-09-09 23:07:58 +00:00
|
|
|
// requesting new keys runs the sync job which will detect the conflict
|
|
|
|
// and re-run the upload job once we're merged and up-to-date.
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('storageService.upload: no storageKey, requesting new keys');
|
2021-06-09 22:28:54 +00:00
|
|
|
backOff.reset();
|
2021-07-15 23:48:09 +00:00
|
|
|
|
|
|
|
if (window.ConversationController.areWePrimaryDevice()) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn('upload: We are primary device; not sending key sync request');
|
2021-07-15 23:48:09 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await handleMessageSend(
|
|
|
|
window.textsecure.messaging.sendRequestKeySyncMessage(),
|
|
|
|
{ messageIds: [], sendType: 'otherSync' }
|
|
|
|
);
|
2020-09-09 23:07:58 +00:00
|
|
|
return;
|
2020-09-09 00:56:23 +00:00
|
|
|
}
|
|
|
|
|
2021-07-13 18:54:53 +00:00
|
|
|
let previousManifest: Proto.ManifestRecord | undefined;
|
2021-03-12 17:38:43 +00:00
|
|
|
if (!fromSync) {
|
2021-03-31 00:16:28 +00:00
|
|
|
// Syncing before we upload so that we repair any unknown records and
|
|
|
|
// records with errors as well as ensure that we have the latest up to date
|
|
|
|
// manifest.
|
2021-09-16 22:25:49 +00:00
|
|
|
// We are going to upload after this sync so we can ignore any conflicts
|
|
|
|
// that arise during the sync.
|
|
|
|
const ignoreConflicts = true;
|
|
|
|
previousManifest = await sync(ignoreConflicts);
|
2021-03-12 17:38:43 +00:00
|
|
|
}
|
|
|
|
|
2021-06-15 00:09:37 +00:00
|
|
|
const localManifestVersion = window.storage.get('manifestVersion', 0);
|
2020-09-09 00:56:23 +00:00
|
|
|
const version = Number(localManifestVersion) + 1;
|
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('storageService.upload: will update to manifest version', version);
|
2020-09-09 00:56:23 +00:00
|
|
|
|
|
|
|
try {
|
2021-04-09 20:12:05 +00:00
|
|
|
const generatedManifest = await generateManifest(version, previousManifest);
|
2020-09-29 23:29:11 +00:00
|
|
|
await uploadManifest(version, generatedManifest);
|
2020-09-09 00:56:23 +00:00
|
|
|
} catch (err) {
|
|
|
|
if (err.code === 409) {
|
2021-06-09 22:28:54 +00:00
|
|
|
await sleep(conflictBackOff.getAndIncrement());
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('storageService.upload: pushing sync on the queue');
|
2020-09-29 23:29:11 +00:00
|
|
|
// The sync job will check for conflicts and as part of that conflict
|
|
|
|
// check if an item needs sync and doesn't match with the remote record
|
|
|
|
// it'll kick off another upload.
|
|
|
|
setTimeout(runStorageServiceSyncJob);
|
|
|
|
return;
|
2020-09-09 00:56:23 +00:00
|
|
|
}
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error(
|
2020-09-29 23:29:11 +00:00
|
|
|
'storageService.upload',
|
|
|
|
err && err.stack ? err.stack : String(err)
|
|
|
|
);
|
2020-09-09 00:56:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-29 23:29:11 +00:00
|
|
|
let storageServiceEnabled = false;
|
|
|
|
|
|
|
|
export function enableStorageService(): void {
|
|
|
|
storageServiceEnabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note: this function is meant to be called before ConversationController is hydrated.
|
|
|
|
// It goes directly to the database, so in-memory conversations will be out of date.
|
|
|
|
export async function eraseAllStorageServiceState(): Promise<void> {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('storageService.eraseAllStorageServiceState: starting...');
|
2020-09-29 23:29:11 +00:00
|
|
|
await Promise.all([
|
|
|
|
window.storage.remove('manifestVersion'),
|
|
|
|
window.storage.remove('storage-service-unknown-records'),
|
|
|
|
window.storage.remove('storageCredentials'),
|
|
|
|
]);
|
|
|
|
await eraseStorageServiceStateFromConversations();
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('storageService.eraseAllStorageServiceState: complete');
|
2020-09-29 23:29:11 +00:00
|
|
|
}
|
|
|
|
|
2020-10-29 00:40:50 +00:00
|
|
|
export const storageServiceUploadJob = debounce(() => {
|
2020-09-29 23:29:11 +00:00
|
|
|
if (!storageServiceEnabled) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('storageService.storageServiceUploadJob: called before enabled');
|
2020-09-29 23:29:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-04-09 20:12:05 +00:00
|
|
|
storageJobQueue(async () => {
|
|
|
|
await upload();
|
|
|
|
}, `upload v${window.storage.get('manifestVersion')}`);
|
2020-09-29 23:29:11 +00:00
|
|
|
}, 500);
|
|
|
|
|
2020-10-29 00:40:50 +00:00
|
|
|
export const runStorageServiceSyncJob = debounce(() => {
|
2020-09-29 23:29:11 +00:00
|
|
|
if (!storageServiceEnabled) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('storageService.runStorageServiceSyncJob: called before enabled');
|
2020-09-29 23:29:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-05 16:39:16 +00:00
|
|
|
ourProfileKeyService.blockGetWithPromise(
|
|
|
|
storageJobQueue(async () => {
|
|
|
|
await sync();
|
|
|
|
}, `sync v${window.storage.get('manifestVersion')}`)
|
|
|
|
);
|
2020-09-29 23:29:11 +00:00
|
|
|
}, 500);
|