2022-05-23 22:21:14 +00:00
|
|
|
// Copyright 2020-2022 Signal Messenger, LLC
|
2020-10-30 20:34:04 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-06-28 21:46:33 +00:00
|
|
|
import { debounce, uniq, without } from 'lodash';
|
2021-01-29 22:16:48 +00:00
|
|
|
import PQueue from 'p-queue';
|
|
|
|
|
2020-07-10 18:28:49 +00:00
|
|
|
import dataInterface from './sql/Client';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type {
|
2020-07-10 18:28:49 +00:00
|
|
|
ConversationModelCollectionType,
|
2021-09-10 02:38:11 +00:00
|
|
|
ConversationAttributesType,
|
2020-09-24 20:57:54 +00:00
|
|
|
ConversationAttributesTypeType,
|
2020-07-10 18:28:49 +00:00
|
|
|
} from './model-types.d';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { ConversationModel } from './models/conversations';
|
2021-12-10 22:51:54 +00:00
|
|
|
import { getContactId } from './messages/helpers';
|
2020-11-20 17:30:45 +00:00
|
|
|
import { maybeDeriveGroupV2Id } from './groups';
|
2021-03-15 21:44:59 +00:00
|
|
|
import { assert } from './util/assert';
|
2021-06-07 16:39:13 +00:00
|
|
|
import { isGroupV1, isGroupV2 } from './util/whatTypeOfConversation';
|
2022-05-23 22:21:14 +00:00
|
|
|
import { getConversationUnreadCountForAppBadge } from './util/getConversationUnreadCountForAppBadge';
|
2021-10-26 22:59:08 +00:00
|
|
|
import { UUID, isValidUuid } from './types/UUID';
|
2021-09-10 02:38:11 +00:00
|
|
|
import { Address } from './types/Address';
|
|
|
|
import { QualifiedAddress } from './types/QualifiedAddress';
|
2021-09-17 18:27:53 +00:00
|
|
|
import * as log from './logging/log';
|
2022-01-25 17:44:45 +00:00
|
|
|
import { sleep } from './util/sleep';
|
2022-01-26 23:39:25 +00:00
|
|
|
import { isNotNil } from './util/isNotNil';
|
2022-06-01 01:26:57 +00:00
|
|
|
import { SECOND } from './util/durations';
|
2020-07-10 18:28:49 +00:00
|
|
|
|
2020-09-11 19:37:01 +00:00
|
|
|
const MAX_MESSAGE_BODY_LENGTH = 64 * 1024;
|
|
|
|
|
2020-07-10 18:28:49 +00:00
|
|
|
const {
|
|
|
|
getAllConversations,
|
2021-10-26 22:59:08 +00:00
|
|
|
getAllGroupsInvolvingUuid,
|
2020-07-10 18:28:49 +00:00
|
|
|
getMessagesBySentAt,
|
|
|
|
migrateConversationMessages,
|
|
|
|
removeConversation,
|
|
|
|
saveConversation,
|
|
|
|
updateConversation,
|
|
|
|
} = dataInterface;
|
|
|
|
|
|
|
|
// We have to run this in background.js, after all backbone models and collections on
|
|
|
|
// Whisper.* have been created. Once those are in typescript we can use more reasonable
|
|
|
|
// require statements for referencing these things, giving us more flexibility here.
|
2020-09-11 19:37:01 +00:00
|
|
|
export function start(): void {
|
2020-07-10 18:28:49 +00:00
|
|
|
const conversations = new window.Whisper.ConversationCollection();
|
|
|
|
|
|
|
|
window.getConversations = () => conversations;
|
|
|
|
window.ConversationController = new ConversationController(conversations);
|
|
|
|
}
|
|
|
|
|
|
|
|
export class ConversationController {
|
2021-11-04 21:11:47 +00:00
|
|
|
private _initialFetchComplete = false;
|
2020-09-11 19:37:01 +00:00
|
|
|
|
2021-11-04 21:11:47 +00:00
|
|
|
private _initialPromise: undefined | Promise<void>;
|
2020-09-11 19:37:01 +00:00
|
|
|
|
2022-01-20 00:40:29 +00:00
|
|
|
private _conversationOpenStart = new Map<string, number>();
|
|
|
|
|
2022-06-01 01:26:57 +00:00
|
|
|
private _hasQueueEmptied = false;
|
|
|
|
|
|
|
|
constructor(private _conversations: ConversationModelCollectionType) {
|
|
|
|
const debouncedUpdateUnreadCount = debounce(
|
|
|
|
this.updateUnreadCount.bind(this),
|
|
|
|
SECOND,
|
|
|
|
{
|
|
|
|
leading: true,
|
|
|
|
maxWait: SECOND,
|
|
|
|
trailing: true,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
// A few things can cause us to update the app-level unread count
|
|
|
|
window.Whisper.events.on('updateUnreadCount', debouncedUpdateUnreadCount);
|
|
|
|
this._conversations.on(
|
|
|
|
'add remove change:active_at change:unreadCount change:markedUnread change:isArchived change:muteExpiresAt',
|
|
|
|
debouncedUpdateUnreadCount
|
|
|
|
);
|
|
|
|
|
|
|
|
// If the conversation is muted we set a timeout so when the mute expires
|
|
|
|
// we can reset the mute state on the model. If the mute has already expired
|
|
|
|
// then we reset the state right away.
|
|
|
|
this._conversations.on('add', (model: ConversationModel): void => {
|
|
|
|
model.startMuteTimer();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
updateUnreadCount(): void {
|
|
|
|
if (!this._hasQueueEmptied) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const canCountMutedConversations =
|
|
|
|
window.storage.get('badge-count-muted-conversations') || false;
|
|
|
|
|
|
|
|
const newUnreadCount = this._conversations.reduce(
|
|
|
|
(result: number, conversation: ConversationModel) =>
|
|
|
|
result +
|
|
|
|
getConversationUnreadCountForAppBadge(
|
|
|
|
conversation.attributes,
|
|
|
|
canCountMutedConversations
|
|
|
|
),
|
|
|
|
0
|
|
|
|
);
|
|
|
|
window.storage.put('unreadCount', newUnreadCount);
|
|
|
|
|
|
|
|
if (newUnreadCount > 0) {
|
|
|
|
window.setBadgeCount(newUnreadCount);
|
|
|
|
window.document.title = `${window.getTitle()} (${newUnreadCount})`;
|
|
|
|
} else {
|
|
|
|
window.setBadgeCount(0);
|
|
|
|
window.document.title = window.getTitle();
|
|
|
|
}
|
|
|
|
window.updateTrayIcon(newUnreadCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
onEmpty(): void {
|
|
|
|
this._hasQueueEmptied = true;
|
|
|
|
this.updateUnreadCount();
|
|
|
|
}
|
2020-07-10 18:28:49 +00:00
|
|
|
|
2020-09-24 20:57:54 +00:00
|
|
|
get(id?: string | null): ConversationModel | undefined {
|
2020-07-10 18:28:49 +00:00
|
|
|
if (!this._initialFetchComplete) {
|
|
|
|
throw new Error(
|
|
|
|
'ConversationController.get() needs complete initial fetch'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function takes null just fine. Backbone typings are too restrictive.
|
|
|
|
return this._conversations.get(id as string);
|
|
|
|
}
|
2020-09-11 19:37:01 +00:00
|
|
|
|
2021-03-18 17:09:27 +00:00
|
|
|
getAll(): Array<ConversationModel> {
|
|
|
|
return this._conversations.models;
|
|
|
|
}
|
|
|
|
|
2020-09-11 19:37:01 +00:00
|
|
|
dangerouslyCreateAndAdd(
|
2021-09-10 02:38:11 +00:00
|
|
|
attributes: Partial<ConversationAttributesType>
|
2020-09-24 20:57:54 +00:00
|
|
|
): ConversationModel {
|
2020-07-10 18:28:49 +00:00
|
|
|
return this._conversations.add(attributes);
|
|
|
|
}
|
2020-09-11 19:37:01 +00:00
|
|
|
|
2021-01-29 22:16:48 +00:00
|
|
|
dangerouslyRemoveById(id: string): void {
|
|
|
|
this._conversations.remove(id);
|
|
|
|
this._conversations.resetLookups();
|
|
|
|
}
|
|
|
|
|
2020-07-10 18:28:49 +00:00
|
|
|
getOrCreate(
|
2020-09-24 20:57:54 +00:00
|
|
|
identifier: string | null,
|
|
|
|
type: ConversationAttributesTypeType,
|
2020-07-10 18:28:49 +00:00
|
|
|
additionalInitialProps = {}
|
2020-09-24 20:57:54 +00:00
|
|
|
): ConversationModel {
|
2020-07-10 18:28:49 +00:00
|
|
|
if (typeof identifier !== 'string') {
|
|
|
|
throw new TypeError("'id' must be a string");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type !== 'private' && type !== 'group') {
|
|
|
|
throw new TypeError(
|
|
|
|
`'type' must be 'private' or 'group'; got: '${type}'`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this._initialFetchComplete) {
|
|
|
|
throw new Error(
|
|
|
|
'ConversationController.get() needs complete initial fetch'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
let conversation = this._conversations.get(identifier);
|
|
|
|
if (conversation) {
|
|
|
|
return conversation;
|
|
|
|
}
|
|
|
|
|
2021-10-26 22:59:08 +00:00
|
|
|
const id = UUID.generate().toString();
|
2020-07-10 18:28:49 +00:00
|
|
|
|
|
|
|
if (type === 'group') {
|
|
|
|
conversation = this._conversations.add({
|
|
|
|
id,
|
|
|
|
uuid: null,
|
|
|
|
e164: null,
|
|
|
|
groupId: identifier,
|
|
|
|
type,
|
|
|
|
version: 2,
|
|
|
|
...additionalInitialProps,
|
|
|
|
});
|
2021-10-26 22:59:08 +00:00
|
|
|
} else if (isValidUuid(identifier)) {
|
2020-07-10 18:28:49 +00:00
|
|
|
conversation = this._conversations.add({
|
|
|
|
id,
|
|
|
|
uuid: identifier,
|
|
|
|
e164: null,
|
|
|
|
groupId: null,
|
|
|
|
type,
|
|
|
|
version: 2,
|
|
|
|
...additionalInitialProps,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
conversation = this._conversations.add({
|
|
|
|
id,
|
|
|
|
uuid: null,
|
|
|
|
e164: identifier,
|
|
|
|
groupId: null,
|
|
|
|
type,
|
|
|
|
version: 2,
|
|
|
|
...additionalInitialProps,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const create = async () => {
|
|
|
|
if (!conversation.isValid()) {
|
|
|
|
const validationError = conversation.validationError || {};
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error(
|
2020-07-10 18:28:49 +00:00
|
|
|
'Contact is not valid. Not saving, but adding to collection:',
|
|
|
|
conversation.idForLogging(),
|
|
|
|
validationError.stack
|
|
|
|
);
|
|
|
|
|
|
|
|
return conversation;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2021-06-07 16:39:13 +00:00
|
|
|
if (isGroupV1(conversation.attributes)) {
|
2021-09-24 00:49:05 +00:00
|
|
|
maybeDeriveGroupV2Id(conversation);
|
2020-11-20 17:30:45 +00:00
|
|
|
}
|
2020-07-10 18:28:49 +00:00
|
|
|
await saveConversation(conversation.attributes);
|
|
|
|
} catch (error) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error(
|
2020-07-10 18:28:49 +00:00
|
|
|
'Conversation save failed! ',
|
|
|
|
identifier,
|
|
|
|
type,
|
|
|
|
'Error:',
|
|
|
|
error && error.stack ? error.stack : error
|
|
|
|
);
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return conversation;
|
|
|
|
};
|
|
|
|
|
|
|
|
conversation.initialPromise = create();
|
|
|
|
|
|
|
|
return conversation;
|
|
|
|
}
|
2020-09-11 19:37:01 +00:00
|
|
|
|
2020-07-10 18:28:49 +00:00
|
|
|
async getOrCreateAndWait(
|
2020-09-24 20:57:54 +00:00
|
|
|
id: string | null,
|
|
|
|
type: ConversationAttributesTypeType,
|
2020-07-10 18:28:49 +00:00
|
|
|
additionalInitialProps = {}
|
2020-09-24 20:57:54 +00:00
|
|
|
): Promise<ConversationModel> {
|
2021-11-04 21:11:47 +00:00
|
|
|
await this.load();
|
2020-09-11 19:37:01 +00:00
|
|
|
const conversation = this.getOrCreate(id, type, additionalInitialProps);
|
2020-07-10 18:28:49 +00:00
|
|
|
|
2020-09-11 19:37:01 +00:00
|
|
|
if (conversation) {
|
|
|
|
await conversation.initialPromise;
|
|
|
|
return conversation;
|
|
|
|
}
|
2020-07-10 18:28:49 +00:00
|
|
|
|
2020-09-11 19:37:01 +00:00
|
|
|
throw new Error('getOrCreateAndWait: did not get conversation');
|
2020-07-10 18:28:49 +00:00
|
|
|
}
|
2020-09-11 19:37:01 +00:00
|
|
|
|
2020-09-24 20:57:54 +00:00
|
|
|
getConversationId(address: string | null): string | null {
|
2020-07-10 18:28:49 +00:00
|
|
|
if (!address) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const [id] = window.textsecure.utils.unencodeNumber(address);
|
|
|
|
const conv = this.get(id);
|
|
|
|
|
|
|
|
if (conv) {
|
|
|
|
return conv.get('id');
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2020-09-11 19:37:01 +00:00
|
|
|
|
2020-09-09 02:25:05 +00:00
|
|
|
getOurConversationId(): string | undefined {
|
2020-07-10 18:28:49 +00:00
|
|
|
const e164 = window.textsecure.storage.user.getNumber();
|
2021-09-10 02:38:11 +00:00
|
|
|
const uuid = window.textsecure.storage.user.getUuid()?.toString();
|
2022-01-20 22:44:25 +00:00
|
|
|
return this.ensureContactIds({
|
|
|
|
e164,
|
|
|
|
uuid,
|
|
|
|
highTrust: true,
|
|
|
|
reason: 'getOurConversationId',
|
|
|
|
});
|
2020-07-10 18:28:49 +00:00
|
|
|
}
|
2020-09-11 19:37:01 +00:00
|
|
|
|
2021-01-29 22:16:48 +00:00
|
|
|
getOurConversationIdOrThrow(): string {
|
|
|
|
const conversationId = this.getOurConversationId();
|
|
|
|
if (!conversationId) {
|
|
|
|
throw new Error(
|
|
|
|
'getOurConversationIdOrThrow: Failed to fetch ourConversationId'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return conversationId;
|
|
|
|
}
|
|
|
|
|
2021-07-30 16:43:16 +00:00
|
|
|
getOurConversation(): ConversationModel | undefined {
|
|
|
|
const conversationId = this.getOurConversationId();
|
|
|
|
return conversationId ? this.get(conversationId) : undefined;
|
|
|
|
}
|
|
|
|
|
2021-07-15 23:48:09 +00:00
|
|
|
getOurConversationOrThrow(): ConversationModel {
|
2021-07-30 16:43:16 +00:00
|
|
|
const conversation = this.getOurConversation();
|
2021-07-15 23:48:09 +00:00
|
|
|
if (!conversation) {
|
|
|
|
throw new Error(
|
|
|
|
'getOurConversationOrThrow: Failed to fetch our own conversation'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return conversation;
|
|
|
|
}
|
|
|
|
|
|
|
|
areWePrimaryDevice(): boolean {
|
|
|
|
const ourDeviceId = window.textsecure.storage.user.getDeviceId();
|
|
|
|
|
|
|
|
return ourDeviceId === 1;
|
|
|
|
}
|
|
|
|
|
2020-07-10 18:28:49 +00:00
|
|
|
/**
|
|
|
|
* Given a UUID and/or an E164, resolves to a string representing the local
|
2020-07-24 01:35:32 +00:00
|
|
|
* database id of the given contact. In high trust mode, it may create new contacts,
|
|
|
|
* and it may merge contacts.
|
2020-07-10 18:28:49 +00:00
|
|
|
*
|
2020-07-24 01:35:32 +00:00
|
|
|
* highTrust = uuid/e164 pairing came from CDS, the server, or your own device
|
2020-07-10 18:28:49 +00:00
|
|
|
*/
|
|
|
|
ensureContactIds({
|
|
|
|
e164,
|
|
|
|
uuid,
|
|
|
|
highTrust,
|
2022-01-20 22:44:25 +00:00
|
|
|
reason,
|
|
|
|
}:
|
|
|
|
| {
|
|
|
|
e164?: string | null;
|
|
|
|
uuid?: string | null;
|
|
|
|
highTrust?: false;
|
|
|
|
reason?: void;
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
e164?: string | null;
|
|
|
|
uuid?: string | null;
|
|
|
|
highTrust: true;
|
|
|
|
reason: string;
|
|
|
|
}): string | undefined {
|
2020-07-10 18:28:49 +00:00
|
|
|
// Check for at least one parameter being provided. This is necessary
|
|
|
|
// because this path can be called on startup to resolve our own ID before
|
|
|
|
// our phone number or UUID are known. The existing behavior in these
|
|
|
|
// cases can handle a returned `undefined` id, so we do that.
|
|
|
|
const normalizedUuid = uuid ? uuid.toLowerCase() : undefined;
|
|
|
|
const identifier = normalizedUuid || e164;
|
|
|
|
|
|
|
|
if ((!e164 && !uuid) || !identifier) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
const convoE164 = this.get(e164);
|
|
|
|
const convoUuid = this.get(normalizedUuid);
|
|
|
|
|
|
|
|
// 1. Handle no match at all
|
|
|
|
if (!convoE164 && !convoUuid) {
|
2022-01-20 22:44:25 +00:00
|
|
|
log.info(
|
|
|
|
'ensureContactIds: Creating new contact, no matches found',
|
|
|
|
highTrust ? reason : 'no reason'
|
|
|
|
);
|
2020-07-10 18:28:49 +00:00
|
|
|
const newConvo = this.getOrCreate(identifier, 'private');
|
|
|
|
if (highTrust && e164) {
|
|
|
|
newConvo.updateE164(e164);
|
|
|
|
}
|
|
|
|
if (normalizedUuid) {
|
|
|
|
newConvo.updateUuid(normalizedUuid);
|
|
|
|
}
|
2022-04-05 00:38:22 +00:00
|
|
|
if ((highTrust && e164) || normalizedUuid) {
|
2020-07-10 18:28:49 +00:00
|
|
|
updateConversation(newConvo.attributes);
|
|
|
|
}
|
|
|
|
|
|
|
|
return newConvo.get('id');
|
|
|
|
|
|
|
|
// 2. Handle match on only E164
|
2020-09-11 19:37:01 +00:00
|
|
|
}
|
|
|
|
if (convoE164 && !convoUuid) {
|
2020-07-10 18:28:49 +00:00
|
|
|
const haveUuid = Boolean(normalizedUuid);
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2020-07-10 18:28:49 +00:00
|
|
|
`ensureContactIds: e164-only match found (have UUID: ${haveUuid})`
|
|
|
|
);
|
|
|
|
// If we are only searching based on e164 anyway, then return the first result
|
|
|
|
if (!normalizedUuid) {
|
|
|
|
return convoE164.get('id');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fill in the UUID for an e164-only contact
|
|
|
|
if (normalizedUuid && !convoE164.get('uuid')) {
|
|
|
|
if (highTrust) {
|
2022-01-20 22:44:25 +00:00
|
|
|
log.info(
|
|
|
|
`ensureContactIds: Adding UUID (${uuid}) to e164-only match ` +
|
|
|
|
`(${e164}), reason: ${reason}`
|
|
|
|
);
|
2020-07-10 18:28:49 +00:00
|
|
|
convoE164.updateUuid(normalizedUuid);
|
|
|
|
updateConversation(convoE164.attributes);
|
|
|
|
}
|
|
|
|
return convoE164.get('id');
|
|
|
|
}
|
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2020-07-10 18:28:49 +00:00
|
|
|
'ensureContactIds: e164 already had UUID, creating a new contact'
|
|
|
|
);
|
|
|
|
// If existing e164 match already has UUID, create a new contact...
|
|
|
|
const newConvo = this.getOrCreate(normalizedUuid, 'private');
|
|
|
|
|
|
|
|
if (highTrust) {
|
2022-01-20 22:44:25 +00:00
|
|
|
log.info(
|
|
|
|
`ensureContactIds: Moving e164 (${e164}) from old contact ` +
|
|
|
|
`(${convoE164.get('uuid')}) to new (${uuid}), reason: ${reason}`
|
|
|
|
);
|
2020-07-10 18:28:49 +00:00
|
|
|
|
|
|
|
// Remove the e164 from the old contact...
|
|
|
|
convoE164.set({ e164: undefined });
|
|
|
|
updateConversation(convoE164.attributes);
|
|
|
|
|
|
|
|
// ...and add it to the new one.
|
|
|
|
newConvo.updateE164(e164);
|
|
|
|
updateConversation(newConvo.attributes);
|
|
|
|
}
|
|
|
|
|
|
|
|
return newConvo.get('id');
|
|
|
|
|
|
|
|
// 3. Handle match on only UUID
|
2020-09-11 19:37:01 +00:00
|
|
|
}
|
|
|
|
if (!convoE164 && convoUuid) {
|
2020-07-10 18:28:49 +00:00
|
|
|
if (e164 && highTrust) {
|
2022-01-20 22:44:25 +00:00
|
|
|
log.info(
|
|
|
|
`ensureContactIds: Adding e164 (${e164}) to UUID-only match ` +
|
|
|
|
`(${uuid}), reason: ${reason}`
|
|
|
|
);
|
2020-07-10 18:28:49 +00:00
|
|
|
convoUuid.updateE164(e164);
|
|
|
|
updateConversation(convoUuid.attributes);
|
|
|
|
}
|
|
|
|
return convoUuid.get('id');
|
|
|
|
}
|
|
|
|
|
|
|
|
// For some reason, TypeScript doesn't believe that we can trust that these two values
|
|
|
|
// are truthy by this point. So we'll throw if we get there.
|
|
|
|
if (!convoE164 || !convoUuid) {
|
|
|
|
throw new Error('ensureContactIds: convoE164 or convoUuid are falsey!');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now, we know that we have a match for both e164 and uuid checks
|
|
|
|
|
|
|
|
if (convoE164 === convoUuid) {
|
|
|
|
return convoUuid.get('id');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (highTrust) {
|
|
|
|
// Conflict: If e164 match already has a UUID, we remove its e164.
|
|
|
|
if (convoE164.get('uuid') && convoE164.get('uuid') !== normalizedUuid) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2022-01-20 22:44:25 +00:00
|
|
|
`ensureContactIds: e164 match (${e164}) had different ` +
|
|
|
|
`UUID(${convoE164.get('uuid')}) than incoming pair (${uuid}), ` +
|
|
|
|
`removing its e164, reason: ${reason}`
|
2020-07-10 18:28:49 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Remove the e164 from the old contact...
|
|
|
|
convoE164.set({ e164: undefined });
|
|
|
|
updateConversation(convoE164.attributes);
|
|
|
|
|
|
|
|
// ...and add it to the new one.
|
|
|
|
convoUuid.updateE164(e164);
|
|
|
|
updateConversation(convoUuid.attributes);
|
|
|
|
|
|
|
|
return convoUuid.get('id');
|
|
|
|
}
|
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn(
|
2020-07-10 18:28:49 +00:00
|
|
|
`ensureContactIds: Found a split contact - UUID ${normalizedUuid} and E164 ${e164}. Merging.`
|
|
|
|
);
|
|
|
|
|
|
|
|
// Conflict: If e164 match has no UUID, we merge. We prefer the UUID match.
|
|
|
|
// Note: no await here, we want to keep this function synchronous
|
2020-07-24 23:52:26 +00:00
|
|
|
convoUuid.updateE164(e164);
|
2020-09-11 19:37:01 +00:00
|
|
|
// `then` is used to trigger async updates, not affecting return value
|
|
|
|
// eslint-disable-next-line more/no-then
|
2021-03-15 21:44:59 +00:00
|
|
|
this.combineConversations(convoUuid, convoE164)
|
2020-07-10 18:28:49 +00:00
|
|
|
.then(() => {
|
|
|
|
// If the old conversation was currently displayed, we load the new one
|
|
|
|
window.Whisper.events.trigger('refreshConversation', {
|
|
|
|
newId: convoUuid.get('id'),
|
|
|
|
oldId: convoE164.get('id'),
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
const errorText = error && error.stack ? error.stack : error;
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn(`ensureContactIds error combining contacts: ${errorText}`);
|
2020-07-10 18:28:49 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return convoUuid.get('id');
|
|
|
|
}
|
2020-09-11 19:37:01 +00:00
|
|
|
|
|
|
|
async checkForConflicts(): Promise<void> {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('checkForConflicts: starting...');
|
2020-07-10 18:28:49 +00:00
|
|
|
const byUuid = Object.create(null);
|
|
|
|
const byE164 = Object.create(null);
|
2021-03-15 21:44:59 +00:00
|
|
|
const byGroupV2Id = Object.create(null);
|
|
|
|
// We also want to find duplicate GV1 IDs. You might expect to see a "byGroupV1Id" map
|
|
|
|
// here. Instead, we check for duplicates on the derived GV2 ID.
|
|
|
|
|
|
|
|
const { models } = this._conversations;
|
2020-07-10 18:28:49 +00:00
|
|
|
|
|
|
|
// We iterate from the oldest conversations to the newest. This allows us, in a
|
|
|
|
// conflict case, to keep the one with activity the most recently.
|
2021-03-15 21:44:59 +00:00
|
|
|
for (let i = models.length - 1; i >= 0; i -= 1) {
|
2020-07-10 18:28:49 +00:00
|
|
|
const conversation = models[i];
|
2021-03-15 21:44:59 +00:00
|
|
|
assert(
|
|
|
|
conversation,
|
|
|
|
'Expected conversation to be found in array during iteration'
|
|
|
|
);
|
|
|
|
|
2020-07-10 18:28:49 +00:00
|
|
|
const uuid = conversation.get('uuid');
|
|
|
|
const e164 = conversation.get('e164');
|
|
|
|
|
|
|
|
if (uuid) {
|
|
|
|
const existing = byUuid[uuid];
|
|
|
|
if (!existing) {
|
|
|
|
byUuid[uuid] = conversation;
|
|
|
|
} else {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn(`checkForConflicts: Found conflict with uuid ${uuid}`);
|
2020-07-10 18:28:49 +00:00
|
|
|
|
|
|
|
// Keep the newer one if it has an e164, otherwise keep existing
|
|
|
|
if (conversation.get('e164')) {
|
|
|
|
// Keep new one
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
2021-03-15 21:44:59 +00:00
|
|
|
await this.combineConversations(conversation, existing);
|
2020-07-10 18:28:49 +00:00
|
|
|
byUuid[uuid] = conversation;
|
|
|
|
} else {
|
|
|
|
// Keep existing - note that this applies if neither had an e164
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
2021-03-15 21:44:59 +00:00
|
|
|
await this.combineConversations(existing, conversation);
|
2020-07-10 18:28:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (e164) {
|
|
|
|
const existing = byE164[e164];
|
|
|
|
if (!existing) {
|
|
|
|
byE164[e164] = conversation;
|
|
|
|
} else {
|
|
|
|
// If we have two contacts with the same e164 but different truthy UUIDs, then
|
|
|
|
// we'll delete the e164 on the older one
|
|
|
|
if (
|
|
|
|
conversation.get('uuid') &&
|
|
|
|
existing.get('uuid') &&
|
|
|
|
conversation.get('uuid') !== existing.get('uuid')
|
|
|
|
) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn(
|
2020-07-10 18:28:49 +00:00
|
|
|
`checkForConflicts: Found two matches on e164 ${e164} with different truthy UUIDs. Dropping e164 on older.`
|
|
|
|
);
|
|
|
|
|
|
|
|
existing.set({ e164: undefined });
|
|
|
|
updateConversation(existing.attributes);
|
|
|
|
|
|
|
|
byE164[e164] = conversation;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn(`checkForConflicts: Found conflict with e164 ${e164}`);
|
2020-07-10 18:28:49 +00:00
|
|
|
|
|
|
|
// Keep the newer one if it has a UUID, otherwise keep existing
|
|
|
|
if (conversation.get('uuid')) {
|
|
|
|
// Keep new one
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
2021-03-15 21:44:59 +00:00
|
|
|
await this.combineConversations(conversation, existing);
|
2020-07-10 18:28:49 +00:00
|
|
|
byE164[e164] = conversation;
|
|
|
|
} else {
|
|
|
|
// Keep existing - note that this applies if neither had a UUID
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
2021-03-15 21:44:59 +00:00
|
|
|
await this.combineConversations(existing, conversation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let groupV2Id: undefined | string;
|
2021-06-07 16:39:13 +00:00
|
|
|
if (isGroupV1(conversation.attributes)) {
|
2021-09-24 00:49:05 +00:00
|
|
|
maybeDeriveGroupV2Id(conversation);
|
2021-03-15 21:44:59 +00:00
|
|
|
groupV2Id = conversation.get('derivedGroupV2Id');
|
|
|
|
assert(
|
|
|
|
groupV2Id,
|
|
|
|
'checkForConflicts: expected the group V2 ID to have been derived, but it was falsy'
|
|
|
|
);
|
2021-06-07 16:39:13 +00:00
|
|
|
} else if (isGroupV2(conversation.attributes)) {
|
2021-03-15 21:44:59 +00:00
|
|
|
groupV2Id = conversation.get('groupId');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (groupV2Id) {
|
|
|
|
const existing = byGroupV2Id[groupV2Id];
|
|
|
|
if (!existing) {
|
|
|
|
byGroupV2Id[groupV2Id] = conversation;
|
|
|
|
} else {
|
2021-06-07 16:39:13 +00:00
|
|
|
const logParenthetical = isGroupV1(conversation.attributes)
|
2021-03-15 21:44:59 +00:00
|
|
|
? ' (derived from a GV1 group ID)'
|
|
|
|
: '';
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn(
|
2021-03-15 21:44:59 +00:00
|
|
|
`checkForConflicts: Found conflict with group V2 ID ${groupV2Id}${logParenthetical}`
|
|
|
|
);
|
|
|
|
|
|
|
|
// Prefer the GV2 group.
|
2021-06-07 16:39:13 +00:00
|
|
|
if (
|
|
|
|
isGroupV2(conversation.attributes) &&
|
|
|
|
!isGroupV2(existing.attributes)
|
|
|
|
) {
|
2021-03-15 21:44:59 +00:00
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
|
await this.combineConversations(conversation, existing);
|
|
|
|
byGroupV2Id[groupV2Id] = conversation;
|
|
|
|
} else {
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
|
await this.combineConversations(existing, conversation);
|
2020-07-10 18:28:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('checkForConflicts: complete!');
|
2020-07-10 18:28:49 +00:00
|
|
|
}
|
2020-09-11 19:37:01 +00:00
|
|
|
|
2021-03-15 21:44:59 +00:00
|
|
|
async combineConversations(
|
2020-09-24 20:57:54 +00:00
|
|
|
current: ConversationModel,
|
|
|
|
obsolete: ConversationModel
|
2020-09-11 19:37:01 +00:00
|
|
|
): Promise<void> {
|
2021-03-15 21:44:59 +00:00
|
|
|
const conversationType = current.get('type');
|
|
|
|
|
|
|
|
if (obsolete.get('type') !== conversationType) {
|
|
|
|
assert(
|
|
|
|
false,
|
|
|
|
'combineConversations cannot combine a private and group conversation. Doing nothing'
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-10 18:28:49 +00:00
|
|
|
const obsoleteId = obsolete.get('id');
|
2021-10-26 22:59:08 +00:00
|
|
|
const obsoleteUuid = obsolete.getUuid();
|
2020-07-10 18:28:49 +00:00
|
|
|
const currentId = current.get('id');
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn('combineConversations: Combining two conversations', {
|
2020-07-10 18:28:49 +00:00
|
|
|
obsolete: obsoleteId,
|
|
|
|
current: currentId,
|
|
|
|
});
|
|
|
|
|
2021-09-10 02:38:11 +00:00
|
|
|
if (conversationType === 'private' && obsoleteUuid) {
|
2021-03-15 21:44:59 +00:00
|
|
|
if (!current.get('profileKey') && obsolete.get('profileKey')) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn(
|
2021-03-15 21:44:59 +00:00
|
|
|
'combineConversations: Copying profile key from old to new contact'
|
|
|
|
);
|
2020-07-10 18:28:49 +00:00
|
|
|
|
2021-03-15 21:44:59 +00:00
|
|
|
const profileKey = obsolete.get('profileKey');
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2021-03-15 21:44:59 +00:00
|
|
|
if (profileKey) {
|
|
|
|
await current.setProfileKey(profileKey);
|
|
|
|
}
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
2020-07-10 18:28:49 +00:00
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn(
|
2021-03-15 21:44:59 +00:00
|
|
|
'combineConversations: Delete all sessions tied to old conversationId'
|
|
|
|
);
|
2021-09-10 02:38:11 +00:00
|
|
|
const ourUuid = window.textsecure.storage.user.getCheckedUuid();
|
|
|
|
const deviceIds = await window.textsecure.storage.protocol.getDeviceIds({
|
|
|
|
ourUuid,
|
2021-10-26 22:59:08 +00:00
|
|
|
identifier: obsoleteUuid.toString(),
|
2021-09-10 02:38:11 +00:00
|
|
|
});
|
2021-03-15 21:44:59 +00:00
|
|
|
await Promise.all(
|
|
|
|
deviceIds.map(async deviceId => {
|
2021-09-10 02:38:11 +00:00
|
|
|
const addr = new QualifiedAddress(
|
|
|
|
ourUuid,
|
2021-10-26 22:59:08 +00:00
|
|
|
new Address(obsoleteUuid, deviceId)
|
2021-03-15 21:44:59 +00:00
|
|
|
);
|
2021-09-10 02:38:11 +00:00
|
|
|
await window.textsecure.storage.protocol.removeSession(addr);
|
2021-03-15 21:44:59 +00:00
|
|
|
})
|
|
|
|
);
|
2020-07-10 18:28:49 +00:00
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn(
|
2021-03-15 21:44:59 +00:00
|
|
|
'combineConversations: Delete all identity information tied to old conversationId'
|
|
|
|
);
|
2021-09-10 02:38:11 +00:00
|
|
|
|
|
|
|
if (obsoleteUuid) {
|
|
|
|
await window.textsecure.storage.protocol.removeIdentityKey(
|
2021-10-26 22:59:08 +00:00
|
|
|
obsoleteUuid
|
2021-09-10 02:38:11 +00:00
|
|
|
);
|
|
|
|
}
|
2020-07-10 18:28:49 +00:00
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn(
|
2021-03-15 21:44:59 +00:00
|
|
|
'combineConversations: Ensure that all V1 groups have new conversationId instead of old'
|
|
|
|
);
|
2021-10-26 22:59:08 +00:00
|
|
|
const groups = await this.getAllGroupsInvolvingUuid(obsoleteUuid);
|
2021-03-15 21:44:59 +00:00
|
|
|
groups.forEach(group => {
|
|
|
|
const members = group.get('members');
|
|
|
|
const withoutObsolete = without(members, obsoleteId);
|
|
|
|
const currentAdded = uniq([...withoutObsolete, currentId]);
|
|
|
|
|
|
|
|
group.set({
|
|
|
|
members: currentAdded,
|
|
|
|
});
|
|
|
|
updateConversation(group.attributes);
|
2020-07-10 18:28:49 +00:00
|
|
|
});
|
2021-03-15 21:44:59 +00:00
|
|
|
}
|
2020-07-10 18:28:49 +00:00
|
|
|
|
|
|
|
// Note: we explicitly don't want to update V2 groups
|
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn(
|
2021-03-15 21:44:59 +00:00
|
|
|
'combineConversations: Delete the obsolete conversation from the database'
|
2020-07-10 18:28:49 +00:00
|
|
|
);
|
2021-12-10 22:51:54 +00:00
|
|
|
await removeConversation(obsoleteId);
|
2020-07-10 18:28:49 +00:00
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn('combineConversations: Update messages table');
|
2020-07-10 18:28:49 +00:00
|
|
|
await migrateConversationMessages(obsoleteId, currentId);
|
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn(
|
2021-03-15 21:44:59 +00:00
|
|
|
'combineConversations: Eliminate old conversation from ConversationController lookups'
|
2020-07-10 18:28:49 +00:00
|
|
|
);
|
|
|
|
this._conversations.remove(obsolete);
|
2020-07-24 23:52:26 +00:00
|
|
|
this._conversations.resetLookups();
|
2020-07-10 18:28:49 +00:00
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn('combineConversations: Complete!', {
|
2020-07-10 18:28:49 +00:00
|
|
|
obsolete: obsoleteId,
|
|
|
|
current: currentId,
|
|
|
|
});
|
|
|
|
}
|
2020-09-11 19:37:01 +00:00
|
|
|
|
2020-07-10 18:28:49 +00:00
|
|
|
/**
|
|
|
|
* Given a groupId and optional additional initialization properties,
|
|
|
|
* ensures the existence of a group conversation and returns a string
|
|
|
|
* representing the local database ID of the group conversation.
|
|
|
|
*/
|
2020-09-09 02:25:05 +00:00
|
|
|
ensureGroup(groupId: string, additionalInitProps = {}): string {
|
2020-07-10 18:28:49 +00:00
|
|
|
return this.getOrCreate(groupId, 'group', additionalInitProps).get('id');
|
|
|
|
}
|
2020-09-11 19:37:01 +00:00
|
|
|
|
2020-07-10 18:28:49 +00:00
|
|
|
/**
|
|
|
|
* Given certain metadata about a message (an identifier of who wrote the
|
|
|
|
* message and the sent_at timestamp of the message) returns the
|
|
|
|
* conversation the message belongs to OR null if a conversation isn't
|
|
|
|
* found.
|
|
|
|
*/
|
2020-09-16 15:44:24 +00:00
|
|
|
async getConversationForTargetMessage(
|
2020-07-10 18:28:49 +00:00
|
|
|
targetFromId: string,
|
|
|
|
targetTimestamp: number
|
2021-06-17 17:15:10 +00:00
|
|
|
): Promise<ConversationModel | null | undefined> {
|
2021-12-10 22:51:54 +00:00
|
|
|
const messages = await getMessagesBySentAt(targetTimestamp);
|
|
|
|
const targetMessage = messages.find(m => getContactId(m) === targetFromId);
|
2020-07-10 18:28:49 +00:00
|
|
|
|
|
|
|
if (targetMessage) {
|
2021-12-10 22:51:54 +00:00
|
|
|
return this.get(targetMessage.conversationId);
|
2020-07-10 18:28:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2020-09-11 19:37:01 +00:00
|
|
|
|
2021-10-26 22:59:08 +00:00
|
|
|
async getAllGroupsInvolvingUuid(
|
|
|
|
uuid: UUID
|
2020-09-24 20:57:54 +00:00
|
|
|
): Promise<Array<ConversationModel>> {
|
2021-12-10 22:51:54 +00:00
|
|
|
const groups = await getAllGroupsInvolvingUuid(uuid.toString());
|
2020-10-28 21:54:33 +00:00
|
|
|
return groups.map(group => {
|
|
|
|
const existing = this.get(group.id);
|
|
|
|
if (existing) {
|
|
|
|
return existing;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._conversations.add(group);
|
|
|
|
});
|
2020-07-10 18:28:49 +00:00
|
|
|
}
|
2020-09-11 19:37:01 +00:00
|
|
|
|
2020-11-20 17:30:45 +00:00
|
|
|
getByDerivedGroupV2Id(groupId: string): ConversationModel | undefined {
|
|
|
|
return this._conversations.find(
|
|
|
|
item => item.get('derivedGroupV2Id') === groupId
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-11 19:37:01 +00:00
|
|
|
reset(): void {
|
2021-11-04 21:11:47 +00:00
|
|
|
delete this._initialPromise;
|
2020-07-10 18:28:49 +00:00
|
|
|
this._initialFetchComplete = false;
|
|
|
|
this._conversations.reset([]);
|
|
|
|
}
|
2020-09-11 19:37:01 +00:00
|
|
|
|
2021-11-04 21:11:47 +00:00
|
|
|
load(): Promise<void> {
|
|
|
|
this._initialPromise ||= this.doLoad();
|
|
|
|
return this._initialPromise;
|
2020-08-11 23:15:06 +00:00
|
|
|
}
|
2020-09-11 19:37:01 +00:00
|
|
|
|
2022-01-25 17:44:45 +00:00
|
|
|
// A number of things outside conversation.attributes affect conversation re-rendering.
|
|
|
|
// If it's scoped to a given conversation, it's easy to trigger('change'). There are
|
|
|
|
// important values in storage and the storage service which change rendering pretty
|
|
|
|
// radically, so this function is necessary to force regeneration of props.
|
2022-01-26 23:39:25 +00:00
|
|
|
async forceRerender(identifiers?: Array<string>): Promise<void> {
|
2022-01-25 17:44:45 +00:00
|
|
|
let count = 0;
|
2022-01-26 23:39:25 +00:00
|
|
|
const conversations = identifiers
|
|
|
|
? identifiers.map(identifier => this.get(identifier)).filter(isNotNil)
|
|
|
|
: this._conversations.models.slice();
|
2022-01-25 17:44:45 +00:00
|
|
|
log.info(
|
|
|
|
`forceRerender: Starting to loop through ${conversations.length} conversations`
|
|
|
|
);
|
|
|
|
|
|
|
|
for (let i = 0, max = conversations.length; i < max; i += 1) {
|
|
|
|
const conversation = conversations[i];
|
|
|
|
|
|
|
|
if (conversation.cachedProps) {
|
|
|
|
conversation.oldCachedProps = conversation.cachedProps;
|
|
|
|
conversation.cachedProps = null;
|
|
|
|
|
|
|
|
conversation.trigger('props-change', conversation, false);
|
|
|
|
count += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count % 10 === 0) {
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
|
await sleep(300);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.info(`forceRerender: Updated ${count} conversations`);
|
|
|
|
}
|
|
|
|
|
2022-01-20 00:40:29 +00:00
|
|
|
onConvoOpenStart(conversationId: string): void {
|
|
|
|
this._conversationOpenStart.set(conversationId, Date.now());
|
|
|
|
}
|
|
|
|
|
|
|
|
onConvoMessageMount(conversationId: string): void {
|
|
|
|
const loadStart = this._conversationOpenStart.get(conversationId);
|
|
|
|
if (loadStart === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._conversationOpenStart.delete(conversationId);
|
|
|
|
this.get(conversationId)?.onOpenComplete(loadStart);
|
|
|
|
}
|
|
|
|
|
2022-02-03 01:21:02 +00:00
|
|
|
repairPinnedConversations(): void {
|
|
|
|
const pinnedIds = window.storage.get('pinnedConversationIds', []);
|
|
|
|
|
|
|
|
for (const id of pinnedIds) {
|
|
|
|
const convo = this.get(id);
|
|
|
|
|
|
|
|
if (!convo || convo.get('isPinned')) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
log.warn(
|
|
|
|
`ConversationController: Repairing ${convo.idForLogging()}'s isPinned`
|
|
|
|
);
|
|
|
|
convo.set('isPinned', true);
|
|
|
|
|
|
|
|
window.Signal.Data.updateConversation(convo.attributes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-04 21:11:47 +00:00
|
|
|
private async doLoad(): Promise<void> {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('ConversationController: starting initial fetch');
|
2020-07-10 18:28:49 +00:00
|
|
|
|
|
|
|
if (this._conversations.length) {
|
|
|
|
throw new Error('ConversationController: Already loaded!');
|
|
|
|
}
|
|
|
|
|
2021-11-04 21:11:47 +00:00
|
|
|
try {
|
2021-12-10 22:51:54 +00:00
|
|
|
const collection = await getAllConversations();
|
2020-07-10 18:28:49 +00:00
|
|
|
|
2021-11-04 21:11:47 +00:00
|
|
|
// Get rid of temporary conversations
|
|
|
|
const temporaryConversations = collection.filter(conversation =>
|
2021-12-10 22:51:54 +00:00
|
|
|
Boolean(conversation.isTemporary)
|
2021-11-04 21:11:47 +00:00
|
|
|
);
|
2021-01-29 22:16:48 +00:00
|
|
|
|
2021-11-04 21:11:47 +00:00
|
|
|
if (temporaryConversations.length) {
|
|
|
|
log.warn(
|
|
|
|
`ConversationController: Removing ${temporaryConversations.length} temporary conversations`
|
2021-01-29 22:16:48 +00:00
|
|
|
);
|
2021-11-04 21:11:47 +00:00
|
|
|
}
|
2021-11-23 22:01:03 +00:00
|
|
|
const queue = new PQueue({
|
|
|
|
concurrency: 3,
|
|
|
|
timeout: 1000 * 60 * 2,
|
|
|
|
throwOnTimeout: true,
|
|
|
|
});
|
2021-11-04 21:11:47 +00:00
|
|
|
queue.addAll(
|
|
|
|
temporaryConversations.map(item => async () => {
|
2021-12-10 22:51:54 +00:00
|
|
|
await removeConversation(item.id);
|
2021-11-04 21:11:47 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
await queue.onIdle();
|
2021-01-29 22:16:48 +00:00
|
|
|
|
2021-11-04 21:11:47 +00:00
|
|
|
// Hydrate the final set of conversations
|
|
|
|
this._conversations.add(
|
2021-12-10 22:51:54 +00:00
|
|
|
collection.filter(conversation => !conversation.isTemporary)
|
2021-11-04 21:11:47 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
this._initialFetchComplete = true;
|
2020-07-10 18:28:49 +00:00
|
|
|
|
2021-11-04 21:11:47 +00:00
|
|
|
await Promise.all(
|
|
|
|
this._conversations.map(async conversation => {
|
|
|
|
try {
|
|
|
|
// Hydrate contactCollection, now that initial fetch is complete
|
|
|
|
conversation.fetchContacts();
|
|
|
|
|
|
|
|
const isChanged = maybeDeriveGroupV2Id(conversation);
|
|
|
|
if (isChanged) {
|
|
|
|
updateConversation(conversation.attributes);
|
2020-07-10 18:28:49 +00:00
|
|
|
}
|
|
|
|
|
2021-11-04 21:11:47 +00:00
|
|
|
// In case a too-large draft was saved to the database
|
|
|
|
const draft = conversation.get('draft');
|
|
|
|
if (draft && draft.length > MAX_MESSAGE_BODY_LENGTH) {
|
|
|
|
conversation.set({
|
|
|
|
draft: draft.slice(0, MAX_MESSAGE_BODY_LENGTH),
|
|
|
|
});
|
|
|
|
updateConversation(conversation.attributes);
|
|
|
|
}
|
2020-07-10 18:28:49 +00:00
|
|
|
|
2021-11-04 21:11:47 +00:00
|
|
|
// Clean up the conversations that have UUID as their e164.
|
|
|
|
const e164 = conversation.get('e164');
|
|
|
|
const uuid = conversation.get('uuid');
|
|
|
|
if (isValidUuid(e164) && uuid) {
|
|
|
|
conversation.set({ e164: undefined });
|
|
|
|
updateConversation(conversation.attributes);
|
|
|
|
|
|
|
|
log.info(`Cleaning up conversation(${uuid}) with invalid e164`);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
log.error(
|
|
|
|
'ConversationController.load/map: Failed to prepare a conversation',
|
|
|
|
error && error.stack ? error.stack : error
|
|
|
|
);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
|
|
|
log.info('ConversationController: done with initial fetch');
|
|
|
|
} catch (error) {
|
|
|
|
log.error(
|
|
|
|
'ConversationController: initial fetch failed',
|
|
|
|
error && error.stack ? error.stack : error
|
|
|
|
);
|
|
|
|
throw error;
|
|
|
|
}
|
2020-07-10 18:28:49 +00:00
|
|
|
}
|
|
|
|
}
|