2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2020 Signal Messenger, LLC
|
2020-10-30 20:34:04 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2022-08-09 23:46:01 +00:00
|
|
|
import { debounce, pick, uniq, without } from 'lodash';
|
2021-01-29 22:16:48 +00:00
|
|
|
import PQueue from 'p-queue';
|
2023-08-10 16:43:33 +00:00
|
|
|
import { v4 as generateUuid } from 'uuid';
|
2024-03-15 14:20:33 +00:00
|
|
|
import { batch as batchDispatch } from 'react-redux';
|
2021-01-29 22:16:48 +00:00
|
|
|
|
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,
|
2022-12-05 22:46:54 +00:00
|
|
|
ConversationRenderInfoType,
|
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';
|
2022-08-09 23:46:01 +00:00
|
|
|
|
|
|
|
import dataInterface from './sql/Client';
|
|
|
|
import * as log from './logging/log';
|
|
|
|
import * as Errors from './types/errors';
|
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';
|
2022-09-15 19:17:15 +00:00
|
|
|
import { assertDev, strictAssert } from './util/assert';
|
2022-12-21 18:41:48 +00:00
|
|
|
import { drop } from './util/drop';
|
2021-06-07 16:39:13 +00:00
|
|
|
import { isGroupV1, isGroupV2 } from './util/whatTypeOfConversation';
|
2023-08-16 20:54:39 +00:00
|
|
|
import type { ServiceIdString, AciString, PniString } from './types/ServiceId';
|
2023-08-10 16:43:33 +00:00
|
|
|
import {
|
|
|
|
isServiceIdString,
|
|
|
|
normalizePni,
|
2023-08-16 20:54:39 +00:00
|
|
|
normalizeServiceId,
|
2023-08-10 16:43:33 +00:00
|
|
|
} from './types/ServiceId';
|
2023-09-14 17:04:48 +00:00
|
|
|
import { normalizeAci } from './util/normalizeAci';
|
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-27 16:46:43 +00:00
|
|
|
import { MINUTE, SECOND } from './util/durations';
|
2023-08-10 16:43:33 +00:00
|
|
|
import { getServiceIdsForE164s } from './util/getServiceIdsForE164s';
|
2022-11-09 21:11:45 +00:00
|
|
|
import { SIGNAL_ACI, SIGNAL_AVATAR_PATH } from './types/SignalConversation';
|
2022-12-05 22:46:54 +00:00
|
|
|
import { getTitleNoDefault } from './util/getTitle';
|
2023-05-24 22:05:04 +00:00
|
|
|
import * as StorageService from './services/storage';
|
2023-08-15 22:32:18 +00:00
|
|
|
import type { ConversationPropsForUnreadStats } from './util/countUnreadStats';
|
2023-08-14 23:28:47 +00:00
|
|
|
import { countAllConversationsUnreadStats } from './util/countUnreadStats';
|
2020-07-10 18:28:49 +00:00
|
|
|
|
2022-08-09 21:39:00 +00:00
|
|
|
type ConvoMatchType =
|
|
|
|
| {
|
2023-08-16 20:54:39 +00:00
|
|
|
key: 'serviceId' | 'pni';
|
2023-08-10 16:43:33 +00:00
|
|
|
value: ServiceIdString | undefined;
|
2022-08-09 21:39:00 +00:00
|
|
|
match: ConversationModel | undefined;
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
key: 'e164';
|
|
|
|
value: string | undefined;
|
|
|
|
match: ConversationModel | undefined;
|
|
|
|
};
|
|
|
|
|
|
|
|
const { hasOwnProperty } = Object.prototype;
|
|
|
|
|
|
|
|
function applyChangeToConversation(
|
|
|
|
conversation: ConversationModel,
|
2024-01-29 22:37:26 +00:00
|
|
|
pniSignatureVerified: boolean,
|
2022-08-09 21:39:00 +00:00
|
|
|
suggestedChange: Partial<
|
2023-08-16 20:54:39 +00:00
|
|
|
Pick<ConversationAttributesType, 'serviceId' | 'e164' | 'pni'>
|
2023-01-12 22:18:08 +00:00
|
|
|
>
|
2022-08-09 21:39:00 +00:00
|
|
|
) {
|
|
|
|
const change = { ...suggestedChange };
|
|
|
|
|
|
|
|
// Clear PNI if changing e164 without associated PNI
|
|
|
|
if (hasOwnProperty.call(change, 'e164') && !change.pni) {
|
|
|
|
change.pni = undefined;
|
|
|
|
}
|
|
|
|
|
2023-08-16 20:54:39 +00:00
|
|
|
// If we have a PNI but not an ACI, then the PNI will go in the serviceId field
|
|
|
|
// Tricky: We need a special check here, because the PNI can be in the serviceId slot
|
2022-08-09 21:39:00 +00:00
|
|
|
if (
|
|
|
|
change.pni &&
|
2023-08-16 20:54:39 +00:00
|
|
|
!change.serviceId &&
|
|
|
|
(!conversation.getServiceId() ||
|
|
|
|
conversation.getServiceId() === conversation.getPni())
|
2022-08-09 21:39:00 +00:00
|
|
|
) {
|
2023-08-16 20:54:39 +00:00
|
|
|
change.serviceId = change.pni;
|
2022-08-09 21:39:00 +00:00
|
|
|
}
|
|
|
|
|
2023-08-16 20:54:39 +00:00
|
|
|
// If we're clearing a PNI, but we didn't have an ACI - we need to clear serviceId field
|
2022-08-09 21:39:00 +00:00
|
|
|
if (
|
2023-08-16 20:54:39 +00:00
|
|
|
!change.serviceId &&
|
2022-08-09 21:39:00 +00:00
|
|
|
hasOwnProperty.call(change, 'pni') &&
|
|
|
|
!change.pni &&
|
2023-08-16 20:54:39 +00:00
|
|
|
conversation.getServiceId() === conversation.getPni()
|
2022-08-09 21:39:00 +00:00
|
|
|
) {
|
2023-08-16 20:54:39 +00:00
|
|
|
change.serviceId = undefined;
|
2022-08-09 21:39:00 +00:00
|
|
|
}
|
|
|
|
|
2023-08-16 20:54:39 +00:00
|
|
|
if (hasOwnProperty.call(change, 'serviceId')) {
|
|
|
|
conversation.updateServiceId(change.serviceId);
|
2022-08-09 21:39:00 +00:00
|
|
|
}
|
|
|
|
if (hasOwnProperty.call(change, 'e164')) {
|
2023-01-12 22:18:08 +00:00
|
|
|
conversation.updateE164(change.e164);
|
2022-08-09 21:39:00 +00:00
|
|
|
}
|
|
|
|
if (hasOwnProperty.call(change, 'pni')) {
|
2024-01-29 22:37:26 +00:00
|
|
|
conversation.updatePni(change.pni, pniSignatureVerified);
|
2022-08-09 21:39:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Note: we don't do a conversation.set here, because change is limited to these fields
|
|
|
|
}
|
|
|
|
|
2022-12-05 22:46:54 +00:00
|
|
|
export type CombineConversationsParams = Readonly<{
|
|
|
|
current: ConversationModel;
|
|
|
|
obsolete: ConversationModel;
|
|
|
|
obsoleteTitleInfo?: ConversationRenderInfoType;
|
|
|
|
}>;
|
|
|
|
export type SafeCombineConversationsParams = Readonly<{ logId: string }> &
|
|
|
|
CombineConversationsParams;
|
|
|
|
|
|
|
|
async function safeCombineConversations(
|
|
|
|
options: SafeCombineConversationsParams
|
|
|
|
) {
|
2022-08-09 21:39:00 +00:00
|
|
|
try {
|
2022-12-05 22:46:54 +00:00
|
|
|
await window.ConversationController.combineConversations(options);
|
2022-08-09 21:39:00 +00:00
|
|
|
} catch (error) {
|
|
|
|
log.warn(
|
2022-12-05 22:46:54 +00:00
|
|
|
`${options.logId}: error combining contacts: ${Errors.toLogFormat(error)}`
|
2022-08-09 21:39:00 +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,
|
2023-08-10 16:43:33 +00:00
|
|
|
getAllGroupsInvolvingServiceId,
|
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.ConversationController = new ConversationController(conversations);
|
2022-11-09 02:38:19 +00:00
|
|
|
window.getConversations = () => conversations;
|
2020-07-10 18:28:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2022-08-09 21:39:00 +00:00
|
|
|
private _combineConversationsQueue = new PQueue({ concurrency: 1 });
|
|
|
|
|
2022-11-09 02:38:19 +00:00
|
|
|
private _signalConversationId: undefined | string;
|
|
|
|
|
2022-06-01 01:26:57 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-08-14 23:28:47 +00:00
|
|
|
const includeMuted =
|
2022-06-01 01:26:57 +00:00
|
|
|
window.storage.get('badge-count-muted-conversations') || false;
|
|
|
|
|
2023-08-14 23:28:47 +00:00
|
|
|
const unreadStats = countAllConversationsUnreadStats(
|
2023-08-15 22:32:18 +00:00
|
|
|
this._conversations.map(
|
|
|
|
(conversation): ConversationPropsForUnreadStats => {
|
|
|
|
// Need to pull this out manually into the Redux shape
|
|
|
|
// because `conversation.format()` can return cached props by the
|
|
|
|
// time this runs
|
|
|
|
return {
|
|
|
|
activeAt: conversation.get('active_at') ?? undefined,
|
|
|
|
isArchived: conversation.get('isArchived'),
|
|
|
|
markedUnread: conversation.get('markedUnread'),
|
|
|
|
muteExpiresAt: conversation.get('muteExpiresAt'),
|
|
|
|
unreadCount: conversation.get('unreadCount'),
|
|
|
|
unreadMentionsCount: conversation.get('unreadMentionsCount'),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
),
|
2023-08-14 23:28:47 +00:00
|
|
|
{ includeMuted }
|
2022-06-01 01:26:57 +00:00
|
|
|
);
|
|
|
|
|
2023-08-14 23:28:47 +00:00
|
|
|
drop(window.storage.put('unreadCount', unreadStats.unreadCount));
|
|
|
|
|
|
|
|
if (unreadStats.unreadCount > 0) {
|
|
|
|
window.IPC.setBadge(unreadStats.unreadCount);
|
|
|
|
window.IPC.updateTrayIcon(unreadStats.unreadCount);
|
|
|
|
window.document.title = `${window.getTitle()} (${
|
|
|
|
unreadStats.unreadCount
|
|
|
|
})`;
|
|
|
|
} else if (unreadStats.markedUnread) {
|
|
|
|
window.IPC.setBadge('marked-unread');
|
|
|
|
window.IPC.updateTrayIcon(1);
|
|
|
|
window.document.title = `${window.getTitle()} (1)`;
|
2022-06-01 01:26:57 +00:00
|
|
|
} else {
|
2023-08-14 23:28:47 +00:00
|
|
|
window.IPC.setBadge(0);
|
|
|
|
window.IPC.updateTrayIcon(0);
|
2022-06-01 01:26:57 +00:00
|
|
|
window.document.title = window.getTitle();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
const id = generateUuid();
|
2020-07-10 18:28:49 +00:00
|
|
|
|
|
|
|
if (type === 'group') {
|
|
|
|
conversation = this._conversations.add({
|
|
|
|
id,
|
2023-08-16 20:54:39 +00:00
|
|
|
serviceId: undefined,
|
2022-08-09 21:39:00 +00:00
|
|
|
e164: undefined,
|
2020-07-10 18:28:49 +00:00
|
|
|
groupId: identifier,
|
|
|
|
type,
|
|
|
|
version: 2,
|
|
|
|
...additionalInitialProps,
|
|
|
|
});
|
2023-08-10 16:43:33 +00:00
|
|
|
} else if (isServiceIdString(identifier)) {
|
2020-07-10 18:28:49 +00:00
|
|
|
conversation = this._conversations.add({
|
|
|
|
id,
|
2023-08-16 20:54:39 +00:00
|
|
|
serviceId: identifier,
|
2022-08-09 21:39:00 +00:00
|
|
|
e164: undefined,
|
|
|
|
groupId: undefined,
|
2020-07-10 18:28:49 +00:00
|
|
|
type,
|
|
|
|
version: 2,
|
|
|
|
...additionalInitialProps,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
conversation = this._conversations.add({
|
|
|
|
id,
|
2023-08-16 20:54:39 +00:00
|
|
|
serviceId: undefined,
|
2020-07-10 18:28:49 +00:00
|
|
|
e164: identifier,
|
2022-08-09 21:39:00 +00:00
|
|
|
groupId: undefined,
|
2020-07-10 18:28:49 +00:00
|
|
|
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(),
|
2022-11-22 18:43:43 +00:00
|
|
|
Errors.toLogFormat(validationError)
|
2020-07-10 18:28:49 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
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:',
|
2022-11-22 18:43:43 +00:00
|
|
|
Errors.toLogFormat(error)
|
2020-07-10 18:28:49 +00:00
|
|
|
);
|
|
|
|
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();
|
2023-08-10 16:43:33 +00:00
|
|
|
const aci = window.textsecure.storage.user.getAci();
|
|
|
|
const pni = window.textsecure.storage.user.getPni();
|
2022-08-09 21:39:00 +00:00
|
|
|
|
|
|
|
if (!e164 && !aci && !pni) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2022-12-05 22:46:54 +00:00
|
|
|
const { conversation } = this.maybeMergeContacts({
|
2022-08-09 21:39:00 +00:00
|
|
|
aci,
|
2022-01-20 22:44:25 +00:00
|
|
|
e164,
|
2022-08-09 21:39:00 +00:00
|
|
|
pni,
|
2022-01-20 22:44:25 +00:00
|
|
|
reason: 'getOurConversationId',
|
|
|
|
});
|
2022-08-09 21:39:00 +00:00
|
|
|
|
2023-02-01 21:32:46 +00:00
|
|
|
return conversation.id;
|
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;
|
|
|
|
}
|
|
|
|
|
2022-11-09 21:11:45 +00:00
|
|
|
async getOrCreateSignalConversation(): Promise<ConversationModel> {
|
|
|
|
const conversation = await this.getOrCreateAndWait(SIGNAL_ACI, 'private', {
|
2022-11-09 02:38:19 +00:00
|
|
|
muteExpiresAt: Number.MAX_SAFE_INTEGER,
|
|
|
|
profileAvatar: { path: SIGNAL_AVATAR_PATH },
|
|
|
|
profileName: 'Signal',
|
|
|
|
profileSharing: true,
|
|
|
|
});
|
|
|
|
|
2022-11-17 17:35:27 +00:00
|
|
|
if (conversation.get('profileAvatar')?.path !== SIGNAL_AVATAR_PATH) {
|
|
|
|
conversation.set({
|
|
|
|
profileAvatar: { hash: SIGNAL_AVATAR_PATH, path: SIGNAL_AVATAR_PATH },
|
|
|
|
});
|
|
|
|
updateConversation(conversation.attributes);
|
|
|
|
}
|
|
|
|
|
2022-12-02 17:34:46 +00:00
|
|
|
if (!conversation.get('profileName')) {
|
|
|
|
conversation.set({ profileName: 'Signal' });
|
|
|
|
updateConversation(conversation.attributes);
|
|
|
|
}
|
|
|
|
|
2022-11-09 02:38:19 +00:00
|
|
|
this._signalConversationId = conversation.id;
|
|
|
|
|
|
|
|
return conversation;
|
|
|
|
}
|
|
|
|
|
2023-08-16 20:54:39 +00:00
|
|
|
isSignalConversationId(conversationId: string): boolean {
|
|
|
|
return this._signalConversationId === conversationId;
|
2022-11-09 02:38:19 +00:00
|
|
|
}
|
|
|
|
|
2021-07-15 23:48:09 +00:00
|
|
|
areWePrimaryDevice(): boolean {
|
|
|
|
const ourDeviceId = window.textsecure.storage.user.getDeviceId();
|
|
|
|
|
|
|
|
return ourDeviceId === 1;
|
|
|
|
}
|
|
|
|
|
2023-08-16 20:54:39 +00:00
|
|
|
// Note: If you don't know what kind of serviceId it is, put it in the 'aci' param.
|
2022-08-09 21:39:00 +00:00
|
|
|
maybeMergeContacts({
|
|
|
|
aci: providedAci,
|
2020-07-10 18:28:49 +00:00
|
|
|
e164,
|
2022-08-09 21:39:00 +00:00
|
|
|
pni: providedPni,
|
2022-01-20 22:44:25 +00:00
|
|
|
reason,
|
2023-10-23 17:40:42 +00:00
|
|
|
fromPniSignature = false,
|
2022-08-09 23:46:01 +00:00
|
|
|
mergeOldAndNew = safeCombineConversations,
|
2022-08-09 21:39:00 +00:00
|
|
|
}: {
|
2023-08-16 20:54:39 +00:00
|
|
|
aci?: AciString;
|
2022-08-09 21:39:00 +00:00
|
|
|
e164?: string;
|
2023-08-16 20:54:39 +00:00
|
|
|
pni?: PniString;
|
2022-08-09 21:39:00 +00:00
|
|
|
reason: string;
|
2022-12-05 22:46:54 +00:00
|
|
|
fromPniSignature?: boolean;
|
|
|
|
mergeOldAndNew?: (options: SafeCombineConversationsParams) => Promise<void>;
|
|
|
|
}): {
|
2023-02-01 21:32:46 +00:00
|
|
|
conversation: ConversationModel;
|
2022-12-05 22:46:54 +00:00
|
|
|
mergePromises: Array<Promise<void>>;
|
|
|
|
} {
|
2022-08-09 21:39:00 +00:00
|
|
|
const dataProvided = [];
|
|
|
|
if (providedAci) {
|
2023-03-24 00:52:46 +00:00
|
|
|
dataProvided.push(`aci=${providedAci}`);
|
2020-07-10 18:28:49 +00:00
|
|
|
}
|
2022-08-09 21:39:00 +00:00
|
|
|
if (e164) {
|
2023-03-24 00:52:46 +00:00
|
|
|
dataProvided.push(`e164=${e164}`);
|
2022-08-09 21:39:00 +00:00
|
|
|
}
|
|
|
|
if (providedPni) {
|
2023-03-24 00:52:46 +00:00
|
|
|
dataProvided.push(`pni=${providedPni}`);
|
2022-08-09 21:39:00 +00:00
|
|
|
}
|
2024-02-16 20:39:58 +00:00
|
|
|
if (fromPniSignature) {
|
|
|
|
dataProvided.push(`fromPniSignature=${fromPniSignature}`);
|
|
|
|
}
|
2023-03-24 00:52:46 +00:00
|
|
|
const logId = `maybeMergeContacts/${reason}/${dataProvided.join(',')}`;
|
2020-07-10 18:28:49 +00:00
|
|
|
|
2023-08-16 20:54:39 +00:00
|
|
|
const aci = providedAci
|
|
|
|
? normalizeAci(providedAci, 'maybeMergeContacts.aci')
|
|
|
|
: undefined;
|
2023-08-10 16:43:33 +00:00
|
|
|
const pni = providedPni
|
|
|
|
? normalizePni(providedPni, 'maybeMergeContacts.pni')
|
|
|
|
: undefined;
|
2022-12-05 22:46:54 +00:00
|
|
|
const mergePromises: Array<Promise<void>> = [];
|
2020-07-10 18:28:49 +00:00
|
|
|
|
2024-01-29 22:37:26 +00:00
|
|
|
const pniSignatureVerified = aci != null && pni != null && fromPniSignature;
|
|
|
|
|
2022-08-09 21:39:00 +00:00
|
|
|
if (!aci && !e164 && !pni) {
|
|
|
|
throw new Error(
|
|
|
|
`${logId}: Need to provide at least one of: aci, e164, pni`
|
2022-01-20 22:44:25 +00:00
|
|
|
);
|
2022-08-09 21:39:00 +00:00
|
|
|
}
|
2020-07-10 18:28:49 +00:00
|
|
|
|
2022-08-09 21:39:00 +00:00
|
|
|
const matches: Array<ConvoMatchType> = [
|
|
|
|
{
|
2023-08-16 20:54:39 +00:00
|
|
|
key: 'serviceId',
|
2022-08-09 21:39:00 +00:00
|
|
|
value: aci,
|
|
|
|
match: window.ConversationController.get(aci),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: 'e164',
|
|
|
|
value: e164,
|
|
|
|
match: window.ConversationController.get(e164),
|
|
|
|
},
|
|
|
|
{ key: 'pni', value: pni, match: window.ConversationController.get(pni) },
|
|
|
|
];
|
|
|
|
let unusedMatches: Array<ConvoMatchType> = [];
|
|
|
|
|
|
|
|
let targetConversation: ConversationModel | undefined;
|
2023-10-23 17:40:42 +00:00
|
|
|
let targetOldServiceIds:
|
|
|
|
| {
|
|
|
|
aci?: AciString;
|
|
|
|
pni?: PniString;
|
|
|
|
}
|
|
|
|
| undefined;
|
2022-08-09 21:39:00 +00:00
|
|
|
let matchCount = 0;
|
|
|
|
matches.forEach(item => {
|
|
|
|
const { key, value, match } = item;
|
|
|
|
|
|
|
|
if (!value) {
|
|
|
|
return;
|
2020-07-10 18:28:49 +00:00
|
|
|
}
|
|
|
|
|
2022-08-09 21:39:00 +00:00
|
|
|
if (!match) {
|
|
|
|
if (targetConversation) {
|
2022-01-20 22:44:25 +00:00
|
|
|
log.info(
|
2023-03-24 00:52:46 +00:00
|
|
|
`${logId}: No match for ${key}, applying to target ` +
|
|
|
|
`conversation - ${targetConversation.idForLogging()}`
|
2022-01-20 22:44:25 +00:00
|
|
|
);
|
2022-08-09 21:39:00 +00:00
|
|
|
// Note: This line might erase a known e164 or PNI
|
2024-01-29 22:37:26 +00:00
|
|
|
applyChangeToConversation(targetConversation, pniSignatureVerified, {
|
2023-01-12 22:18:08 +00:00
|
|
|
[key]: value,
|
|
|
|
});
|
2022-08-09 21:39:00 +00:00
|
|
|
} else {
|
|
|
|
unusedMatches.push(item);
|
2020-07-10 18:28:49 +00:00
|
|
|
}
|
2022-08-09 21:39:00 +00:00
|
|
|
return;
|
2020-07-10 18:28:49 +00:00
|
|
|
}
|
|
|
|
|
2022-08-09 21:39:00 +00:00
|
|
|
matchCount += 1;
|
|
|
|
unusedMatches.forEach(unused => {
|
|
|
|
strictAssert(unused.value, 'An unused value should always be truthy');
|
|
|
|
|
|
|
|
// Example: If we find that our PNI match has no ACI, then it will be our target.
|
2022-12-05 22:46:54 +00:00
|
|
|
|
|
|
|
if (!targetConversation && !match.get(unused.key)) {
|
|
|
|
log.info(
|
|
|
|
`${logId}: Match on ${key} does not have ${unused.key}, ` +
|
|
|
|
`so it will be our target conversation - ${match.idForLogging()}`
|
|
|
|
);
|
|
|
|
targetConversation = match;
|
|
|
|
}
|
2023-08-16 20:54:39 +00:00
|
|
|
// Tricky: PNI can end up in serviceId slot, so we need to special-case it
|
2022-08-09 21:39:00 +00:00
|
|
|
if (
|
|
|
|
!targetConversation &&
|
2023-08-16 20:54:39 +00:00
|
|
|
unused.key === 'serviceId' &&
|
2022-12-05 22:46:54 +00:00
|
|
|
match.get(unused.key) === pni
|
2022-08-09 21:39:00 +00:00
|
|
|
) {
|
|
|
|
log.info(
|
2023-08-16 20:54:39 +00:00
|
|
|
`${logId}: Match on ${key} has serviceId matching incoming pni, ` +
|
2022-12-05 22:46:54 +00:00
|
|
|
`so it will be our target conversation - ${match.idForLogging()}`
|
|
|
|
);
|
|
|
|
targetConversation = match;
|
|
|
|
}
|
2023-08-16 20:54:39 +00:00
|
|
|
// Tricky: PNI can end up in serviceId slot, so we need to special-case it
|
2022-12-05 22:46:54 +00:00
|
|
|
if (
|
|
|
|
!targetConversation &&
|
2023-08-16 20:54:39 +00:00
|
|
|
unused.key === 'serviceId' &&
|
|
|
|
match.get(unused.key) === match.getPni()
|
2022-12-05 22:46:54 +00:00
|
|
|
) {
|
|
|
|
log.info(
|
2023-08-16 20:54:39 +00:00
|
|
|
`${logId}: Match on ${key} has pni/serviceId which are the same value, ` +
|
2022-08-09 21:39:00 +00:00
|
|
|
`so it will be our target conversation - ${match.idForLogging()}`
|
|
|
|
);
|
|
|
|
targetConversation = match;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If PNI match already has an ACI, then we need to create a new one
|
|
|
|
if (!targetConversation) {
|
|
|
|
targetConversation = this.getOrCreate(unused.value, 'private');
|
|
|
|
log.info(
|
|
|
|
`${logId}: Match on ${key} already had ${unused.key}, ` +
|
|
|
|
`so created new target conversation - ${targetConversation.idForLogging()}`
|
|
|
|
);
|
|
|
|
}
|
2020-07-10 18:28:49 +00:00
|
|
|
|
2023-10-23 17:40:42 +00:00
|
|
|
targetOldServiceIds = {
|
|
|
|
aci: targetConversation.getAci(),
|
|
|
|
pni: targetConversation.getPni(),
|
|
|
|
};
|
|
|
|
|
2022-01-20 22:44:25 +00:00
|
|
|
log.info(
|
2022-08-09 21:39:00 +00:00
|
|
|
`${logId}: Applying new value for ${unused.key} to target conversation`
|
2022-01-20 22:44:25 +00:00
|
|
|
);
|
2024-01-29 22:37:26 +00:00
|
|
|
applyChangeToConversation(targetConversation, pniSignatureVerified, {
|
2023-01-12 22:18:08 +00:00
|
|
|
[unused.key]: unused.value,
|
|
|
|
});
|
2022-08-09 21:39:00 +00:00
|
|
|
});
|
2020-07-10 18:28:49 +00:00
|
|
|
|
2022-08-09 21:39:00 +00:00
|
|
|
unusedMatches = [];
|
2020-07-10 18:28:49 +00:00
|
|
|
|
2022-08-09 21:39:00 +00:00
|
|
|
if (targetConversation && targetConversation !== match) {
|
2022-12-05 22:46:54 +00:00
|
|
|
// We need to grab this before we start taking key data from it. If we're merging
|
|
|
|
// by e164, we want to be sure that is what is rendered in the notification.
|
|
|
|
const obsoleteTitleInfo =
|
|
|
|
key === 'e164'
|
|
|
|
? pick(match.attributes as ConversationAttributesType, [
|
|
|
|
'e164',
|
|
|
|
'type',
|
|
|
|
])
|
|
|
|
: pick(match.attributes as ConversationAttributesType, [
|
|
|
|
'e164',
|
|
|
|
'profileFamilyName',
|
|
|
|
'profileName',
|
|
|
|
'systemGivenName',
|
2023-02-14 19:04:04 +00:00
|
|
|
'systemFamilyName',
|
2023-02-14 22:35:34 +00:00
|
|
|
'systemNickname',
|
2022-12-05 22:46:54 +00:00
|
|
|
'type',
|
|
|
|
'username',
|
|
|
|
]);
|
|
|
|
|
2022-08-09 21:39:00 +00:00
|
|
|
// Clear the value on the current match, since it belongs on targetConversation!
|
|
|
|
// Note: we need to do the remove first, because it will clear the lookup!
|
2022-01-20 22:44:25 +00:00
|
|
|
log.info(
|
2023-03-24 00:52:46 +00:00
|
|
|
`${logId}: Clearing ${key} on match, and adding it to target ` +
|
|
|
|
`conversation - ${targetConversation.idForLogging()}`
|
2022-01-20 22:44:25 +00:00
|
|
|
);
|
2022-08-09 21:39:00 +00:00
|
|
|
const change: Pick<
|
|
|
|
Partial<ConversationAttributesType>,
|
2023-08-16 20:54:39 +00:00
|
|
|
'serviceId' | 'e164' | 'pni'
|
2022-08-09 21:39:00 +00:00
|
|
|
> = {
|
|
|
|
[key]: undefined,
|
|
|
|
};
|
2023-08-16 20:54:39 +00:00
|
|
|
// When the PNI is being used in the serviceId field alone, we need to clear it
|
|
|
|
if ((key === 'pni' || key === 'e164') && match.getServiceId() === pni) {
|
|
|
|
change.serviceId = undefined;
|
2022-08-09 21:39:00 +00:00
|
|
|
}
|
2024-01-29 22:37:26 +00:00
|
|
|
applyChangeToConversation(match, pniSignatureVerified, change);
|
2022-08-09 21:39:00 +00:00
|
|
|
|
2023-08-16 20:54:39 +00:00
|
|
|
// Note: The PNI check here is just to be bulletproof; if we know a
|
|
|
|
// serviceId is a PNI, then that should be put in the serviceId field
|
|
|
|
// as well!
|
2022-12-05 22:46:54 +00:00
|
|
|
const willMerge =
|
2023-08-16 20:54:39 +00:00
|
|
|
!match.getServiceId() && !match.get('e164') && !match.getPni();
|
2022-12-05 22:46:54 +00:00
|
|
|
|
2024-01-29 22:37:26 +00:00
|
|
|
applyChangeToConversation(targetConversation, pniSignatureVerified, {
|
2023-01-12 22:18:08 +00:00
|
|
|
[key]: value,
|
|
|
|
});
|
2022-12-05 22:46:54 +00:00
|
|
|
|
|
|
|
if (willMerge) {
|
2022-08-09 21:39:00 +00:00
|
|
|
log.warn(
|
|
|
|
`${logId}: Removing old conversation which matched on ${key}. ` +
|
2023-03-24 00:52:46 +00:00
|
|
|
`Merging with target conversation - ${targetConversation.idForLogging()}`
|
2022-08-09 21:39:00 +00:00
|
|
|
);
|
2022-12-05 22:46:54 +00:00
|
|
|
mergePromises.push(
|
|
|
|
mergeOldAndNew({
|
|
|
|
current: targetConversation,
|
|
|
|
logId,
|
|
|
|
obsolete: match,
|
|
|
|
obsoleteTitleInfo,
|
|
|
|
})
|
|
|
|
);
|
2022-08-09 21:39:00 +00:00
|
|
|
}
|
|
|
|
} else if (targetConversation && !targetConversation?.get(key)) {
|
|
|
|
// This is mostly for the situation where PNI was erased when updating e164
|
2023-03-24 00:52:46 +00:00
|
|
|
log.debug(
|
|
|
|
`${logId}: Re-adding ${key} on target conversation - ` +
|
|
|
|
`${targetConversation.idForLogging()}`
|
|
|
|
);
|
2024-01-29 22:37:26 +00:00
|
|
|
applyChangeToConversation(targetConversation, pniSignatureVerified, {
|
2023-01-12 22:18:08 +00:00
|
|
|
[key]: value,
|
|
|
|
});
|
2020-07-10 18:28:49 +00:00
|
|
|
}
|
|
|
|
|
2022-08-09 21:39:00 +00:00
|
|
|
if (!targetConversation) {
|
|
|
|
// log.debug(
|
|
|
|
// `${logId}: Match on ${key} is target conversation - ${match.idForLogging()}`
|
|
|
|
// );
|
|
|
|
targetConversation = match;
|
2023-10-23 17:40:42 +00:00
|
|
|
targetOldServiceIds = {
|
|
|
|
aci: targetConversation.getAci(),
|
|
|
|
pni: targetConversation.getPni(),
|
|
|
|
};
|
2022-08-09 21:39:00 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-10-23 17:40:42 +00:00
|
|
|
// If the change is not coming from PNI Signature, and target conversation
|
|
|
|
// had PNI and has acquired new ACI and/or PNI we should check if it had
|
|
|
|
// a PNI session on the original PNI. If yes - add a PhoneNumberDiscovery notification
|
|
|
|
if (
|
|
|
|
e164 &&
|
|
|
|
pni &&
|
|
|
|
targetConversation &&
|
|
|
|
targetOldServiceIds?.pni &&
|
|
|
|
!fromPniSignature &&
|
|
|
|
(targetOldServiceIds.pni !== pni ||
|
|
|
|
(aci && targetOldServiceIds.aci !== aci))
|
|
|
|
) {
|
2024-03-06 23:59:51 +00:00
|
|
|
targetConversation.unset('needsTitleTransition');
|
2023-10-23 17:40:42 +00:00
|
|
|
mergePromises.push(
|
|
|
|
targetConversation.addPhoneNumberDiscoveryIfNeeded(
|
|
|
|
targetOldServiceIds.pni
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-08-09 21:39:00 +00:00
|
|
|
if (targetConversation) {
|
2022-12-05 22:46:54 +00:00
|
|
|
return { conversation: targetConversation, mergePromises };
|
2020-07-10 18:28:49 +00:00
|
|
|
}
|
|
|
|
|
2022-08-09 21:39:00 +00:00
|
|
|
strictAssert(
|
|
|
|
matchCount === 0,
|
|
|
|
`${logId}: should be no matches if no targetConversation`
|
|
|
|
);
|
2020-07-10 18:28:49 +00:00
|
|
|
|
2022-08-09 21:39:00 +00:00
|
|
|
log.info(`${logId}: Creating a new conversation with all inputs`);
|
2022-08-17 23:12:38 +00:00
|
|
|
|
|
|
|
// This is not our precedence for lookup, but it ensures that the PNI gets into the
|
2023-08-16 20:54:39 +00:00
|
|
|
// serviceId slot if we have no ACI.
|
2022-08-17 23:12:38 +00:00
|
|
|
const identifier = aci || pni || e164;
|
|
|
|
strictAssert(identifier, `${logId}: identifier must be truthy!`);
|
|
|
|
|
2022-12-05 22:46:54 +00:00
|
|
|
return {
|
|
|
|
conversation: this.getOrCreate(identifier, 'private', { e164, pni }),
|
|
|
|
mergePromises,
|
|
|
|
};
|
2022-08-09 21:39:00 +00:00
|
|
|
}
|
2020-07-10 18:28:49 +00:00
|
|
|
|
2022-08-09 21:39:00 +00:00
|
|
|
/**
|
2023-08-16 20:54:39 +00:00
|
|
|
* Given a serviceId and/or an E164, returns a string representing the local
|
2022-08-09 21:39:00 +00:00
|
|
|
* database id of the given contact. Will create a new conversation if none exists;
|
|
|
|
* otherwise will return whatever is found.
|
|
|
|
*/
|
|
|
|
lookupOrCreate({
|
|
|
|
e164,
|
2023-08-16 20:54:39 +00:00
|
|
|
serviceId,
|
2022-12-03 01:05:27 +00:00
|
|
|
reason,
|
2022-08-09 21:39:00 +00:00
|
|
|
}: {
|
|
|
|
e164?: string | null;
|
2023-08-16 20:54:39 +00:00
|
|
|
serviceId?: ServiceIdString | null;
|
2022-12-03 01:05:27 +00:00
|
|
|
reason: string;
|
2022-08-09 21:39:00 +00:00
|
|
|
}): ConversationModel | undefined {
|
2023-08-16 20:54:39 +00:00
|
|
|
const normalizedServiceId = serviceId
|
|
|
|
? normalizeServiceId(serviceId, 'ConversationController.lookupOrCreate')
|
|
|
|
: undefined;
|
|
|
|
const identifier = normalizedServiceId || e164;
|
2020-07-10 18:28:49 +00:00
|
|
|
|
2023-08-16 20:54:39 +00:00
|
|
|
if ((!e164 && !serviceId) || !identifier) {
|
2022-12-03 01:05:27 +00:00
|
|
|
log.warn(
|
2023-08-16 20:54:39 +00:00
|
|
|
`lookupOrCreate: Called with neither e164 nor serviceId! reason: ${reason}`
|
2022-12-03 01:05:27 +00:00
|
|
|
);
|
2022-08-09 21:39:00 +00:00
|
|
|
return undefined;
|
|
|
|
}
|
2020-07-10 18:28:49 +00:00
|
|
|
|
2022-08-09 21:39:00 +00:00
|
|
|
const convoE164 = this.get(e164);
|
2023-08-16 20:54:39 +00:00
|
|
|
const convoServiceId = this.get(normalizedServiceId);
|
2022-08-09 21:39:00 +00:00
|
|
|
|
|
|
|
// 1. Handle no match at all
|
2023-08-16 20:54:39 +00:00
|
|
|
if (!convoE164 && !convoServiceId) {
|
2022-08-09 21:39:00 +00:00
|
|
|
log.info('lookupOrCreate: Creating new contact, no matches found');
|
|
|
|
const newConvo = this.getOrCreate(identifier, 'private');
|
2020-07-10 18:28:49 +00:00
|
|
|
|
2023-08-16 20:54:39 +00:00
|
|
|
// `identifier` would resolve to serviceId if we had both, so fix up e164
|
|
|
|
if (normalizedServiceId && e164) {
|
2023-01-12 22:18:08 +00:00
|
|
|
newConvo.updateE164(e164);
|
2020-07-10 18:28:49 +00:00
|
|
|
}
|
|
|
|
|
2022-08-09 21:39:00 +00:00
|
|
|
return newConvo;
|
|
|
|
}
|
|
|
|
|
2023-08-16 20:54:39 +00:00
|
|
|
// 2. Handle match on only service id
|
|
|
|
if (!convoE164 && convoServiceId) {
|
|
|
|
return convoServiceId;
|
2022-08-09 21:39:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 3. Handle match on only E164
|
2023-08-16 20:54:39 +00:00
|
|
|
if (convoE164 && !convoServiceId) {
|
2022-08-09 21:39:00 +00:00
|
|
|
return convoE164;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 that isn't the case.
|
2023-08-16 20:54:39 +00:00
|
|
|
if (!convoE164 || !convoServiceId) {
|
2022-08-09 21:39:00 +00:00
|
|
|
throw new Error(
|
2023-08-16 20:54:39 +00:00
|
|
|
`lookupOrCreate: convoE164 or convoServiceId are falsey but should both be true! reason: ${reason}`
|
2020-07-10 18:28:49 +00:00
|
|
|
);
|
2022-08-09 21:39:00 +00:00
|
|
|
}
|
2020-07-10 18:28:49 +00:00
|
|
|
|
2022-08-09 21:39:00 +00:00
|
|
|
// 4. If the two lookups agree, return that conversation
|
2023-08-16 20:54:39 +00:00
|
|
|
if (convoE164 === convoServiceId) {
|
|
|
|
return convoServiceId;
|
2020-07-10 18:28:49 +00:00
|
|
|
}
|
|
|
|
|
2023-08-16 20:54:39 +00:00
|
|
|
// 5. If the two lookups disagree, log and return the service id match
|
2022-08-09 21:39:00 +00:00
|
|
|
log.warn(
|
2023-08-16 20:54:39 +00:00
|
|
|
`lookupOrCreate: Found a split contact - service id ${normalizedServiceId} and E164 ${e164}. Returning service id match. reason: ${reason}`
|
2022-08-09 21:39:00 +00:00
|
|
|
);
|
2023-08-16 20:54:39 +00:00
|
|
|
return convoServiceId;
|
2020-07-10 18:28:49 +00:00
|
|
|
}
|
2020-09-11 19:37:01 +00:00
|
|
|
|
2022-09-13 16:16:01 +00:00
|
|
|
checkForConflicts(): Promise<void> {
|
|
|
|
return this._combineConversationsQueue.add(() =>
|
|
|
|
this.doCheckForConflicts()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-12-05 22:46:54 +00:00
|
|
|
// Note: `doCombineConversations` is directly used within this function since both
|
|
|
|
// run on `_combineConversationsQueue` queue and we don't want deadlocks.
|
2022-09-13 16:16:01 +00:00
|
|
|
private async doCheckForConflicts(): Promise<void> {
|
2024-03-15 14:20:33 +00:00
|
|
|
log.info('ConversationController.checkForConflicts: starting...');
|
2023-08-16 20:54:39 +00:00
|
|
|
const byServiceId = Object.create(null);
|
2020-07-10 18:28:49 +00:00
|
|
|
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];
|
2022-09-15 19:17:15 +00:00
|
|
|
assertDev(
|
2021-03-15 21:44:59 +00:00
|
|
|
conversation,
|
|
|
|
'Expected conversation to be found in array during iteration'
|
|
|
|
);
|
|
|
|
|
2023-08-16 20:54:39 +00:00
|
|
|
const serviceId = conversation.getServiceId();
|
|
|
|
const pni = conversation.getPni();
|
2020-07-10 18:28:49 +00:00
|
|
|
const e164 = conversation.get('e164');
|
|
|
|
|
2023-08-16 20:54:39 +00:00
|
|
|
if (serviceId) {
|
|
|
|
const existing = byServiceId[serviceId];
|
2020-07-10 18:28:49 +00:00
|
|
|
if (!existing) {
|
2023-08-16 20:54:39 +00:00
|
|
|
byServiceId[serviceId] = conversation;
|
2020-07-10 18:28:49 +00:00
|
|
|
} else {
|
2023-08-16 20:54:39 +00:00
|
|
|
log.warn(
|
|
|
|
`checkForConflicts: Found conflict with serviceId ${serviceId}`
|
|
|
|
);
|
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
|
2022-12-05 22:46:54 +00:00
|
|
|
await this.doCombineConversations({
|
|
|
|
current: conversation,
|
|
|
|
obsolete: existing,
|
|
|
|
});
|
2023-08-16 20:54:39 +00:00
|
|
|
byServiceId[serviceId] = conversation;
|
2020-07-10 18:28:49 +00:00
|
|
|
} else {
|
|
|
|
// Keep existing - note that this applies if neither had an e164
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
2022-12-05 22:46:54 +00:00
|
|
|
await this.doCombineConversations({
|
|
|
|
current: existing,
|
|
|
|
obsolete: conversation,
|
|
|
|
});
|
2020-07-10 18:28:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-09 21:39:00 +00:00
|
|
|
if (pni) {
|
2023-08-16 20:54:39 +00:00
|
|
|
const existing = byServiceId[pni];
|
2022-08-09 21:39:00 +00:00
|
|
|
if (!existing) {
|
2023-08-16 20:54:39 +00:00
|
|
|
byServiceId[pni] = conversation;
|
2022-10-18 23:42:45 +00:00
|
|
|
} else if (existing === conversation) {
|
2023-08-16 20:54:39 +00:00
|
|
|
// Conversation has both service id and pni set to the same value. This
|
2022-10-18 23:42:45 +00:00
|
|
|
// happens when starting a conversation by E164.
|
|
|
|
assertDev(
|
2023-08-16 20:54:39 +00:00
|
|
|
pni === serviceId,
|
|
|
|
'checkForConflicts: expected PNI to be equal to serviceId'
|
2022-10-18 23:42:45 +00:00
|
|
|
);
|
2022-08-09 21:39:00 +00:00
|
|
|
} else {
|
|
|
|
log.warn(`checkForConflicts: Found conflict with pni ${pni}`);
|
|
|
|
|
2022-08-10 18:39:04 +00:00
|
|
|
// Keep the newer one if it has additional data, otherwise keep existing
|
2023-08-16 20:54:39 +00:00
|
|
|
if (conversation.get('e164') || conversation.getPni()) {
|
2022-08-09 21:39:00 +00:00
|
|
|
// Keep new one
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
2022-12-05 22:46:54 +00:00
|
|
|
await this.doCombineConversations({
|
|
|
|
current: conversation,
|
|
|
|
obsolete: existing,
|
|
|
|
});
|
2023-08-16 20:54:39 +00:00
|
|
|
byServiceId[pni] = conversation;
|
2022-08-09 21:39:00 +00:00
|
|
|
} else {
|
|
|
|
// Keep existing - note that this applies if neither had an e164
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
2022-12-05 22:46:54 +00:00
|
|
|
await this.doCombineConversations({
|
|
|
|
current: existing,
|
|
|
|
obsolete: conversation,
|
|
|
|
});
|
2022-08-09 21:39:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-10 18:28:49 +00:00
|
|
|
if (e164) {
|
|
|
|
const existing = byE164[e164];
|
|
|
|
if (!existing) {
|
|
|
|
byE164[e164] = conversation;
|
|
|
|
} else {
|
2023-08-16 20:54:39 +00:00
|
|
|
// If we have two contacts with the same e164 but different truthy
|
|
|
|
// service ids, then we'll delete the e164 on the older one
|
2020-07-10 18:28:49 +00:00
|
|
|
if (
|
2023-08-16 20:54:39 +00:00
|
|
|
conversation.getServiceId() &&
|
|
|
|
existing.getServiceId() &&
|
|
|
|
conversation.getServiceId() !== existing.getServiceId()
|
2020-07-10 18:28:49 +00:00
|
|
|
) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn(
|
2023-08-16 20:54:39 +00:00
|
|
|
`checkForConflicts: Found two matches on e164 ${e164} with different truthy service ids. Dropping e164 on older.`
|
2020-07-10 18:28:49 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
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
|
|
|
|
2023-08-16 20:54:39 +00:00
|
|
|
// Keep the newer one if it has a service id, otherwise keep existing
|
|
|
|
if (conversation.getServiceId()) {
|
2020-07-10 18:28:49 +00:00
|
|
|
// Keep new one
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
2022-12-05 22:46:54 +00:00
|
|
|
await this.doCombineConversations({
|
|
|
|
current: conversation,
|
|
|
|
obsolete: existing,
|
|
|
|
});
|
2020-07-10 18:28:49 +00:00
|
|
|
byE164[e164] = conversation;
|
|
|
|
} else {
|
2023-08-16 20:54:39 +00:00
|
|
|
// Keep existing - note that this applies if neither had a service id
|
2020-07-10 18:28:49 +00:00
|
|
|
// eslint-disable-next-line no-await-in-loop
|
2022-12-05 22:46:54 +00:00
|
|
|
await this.doCombineConversations({
|
|
|
|
current: existing,
|
|
|
|
obsolete: conversation,
|
|
|
|
});
|
2021-03-15 21:44:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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');
|
2022-09-15 19:17:15 +00:00
|
|
|
assertDev(
|
2021-03-15 21:44:59 +00:00
|
|
|
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
|
2022-12-05 22:46:54 +00:00
|
|
|
await this.doCombineConversations({
|
|
|
|
current: conversation,
|
|
|
|
obsolete: existing,
|
|
|
|
});
|
2021-03-15 21:44:59 +00:00
|
|
|
byGroupV2Id[groupV2Id] = conversation;
|
|
|
|
} else {
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
2022-12-05 22:46:54 +00:00
|
|
|
await this.doCombineConversations({
|
|
|
|
current: existing,
|
|
|
|
obsolete: 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(
|
2022-12-05 22:46:54 +00:00
|
|
|
options: CombineConversationsParams
|
2020-09-11 19:37:01 +00:00
|
|
|
): Promise<void> {
|
2022-09-13 16:16:01 +00:00
|
|
|
return this._combineConversationsQueue.add(() =>
|
2022-12-05 22:46:54 +00:00
|
|
|
this.doCombineConversations(options)
|
2022-09-13 16:16:01 +00:00
|
|
|
);
|
|
|
|
}
|
2022-08-09 23:46:01 +00:00
|
|
|
|
2022-12-05 22:46:54 +00:00
|
|
|
private async doCombineConversations({
|
|
|
|
current,
|
|
|
|
obsolete,
|
|
|
|
obsoleteTitleInfo,
|
|
|
|
}: CombineConversationsParams): Promise<void> {
|
2022-09-13 16:16:01 +00:00
|
|
|
const logId = `combineConversations/${obsolete.id}->${current.id}`;
|
2020-07-10 18:28:49 +00:00
|
|
|
|
2022-09-13 16:16:01 +00:00
|
|
|
const conversationType = current.get('type');
|
2020-07-10 18:28:49 +00:00
|
|
|
|
2022-09-13 16:16:01 +00:00
|
|
|
if (!this.get(obsolete.id)) {
|
|
|
|
log.warn(`${logId}: Already combined obsolete conversation`);
|
2022-12-05 22:46:54 +00:00
|
|
|
return;
|
2022-09-13 16:16:01 +00:00
|
|
|
}
|
2020-07-10 18:28:49 +00:00
|
|
|
|
2022-09-13 16:16:01 +00:00
|
|
|
if (obsolete.get('type') !== conversationType) {
|
2022-09-15 19:17:15 +00:00
|
|
|
assertDev(
|
2022-09-13 16:16:01 +00:00
|
|
|
false,
|
|
|
|
`${logId}: cannot combine a private and group conversation. Doing nothing`
|
2022-08-09 23:46:01 +00:00
|
|
|
);
|
2022-09-13 16:16:01 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-08-09 23:46:01 +00:00
|
|
|
|
2022-12-05 22:46:54 +00:00
|
|
|
log.warn(
|
|
|
|
`${logId}: Combining two conversations -`,
|
|
|
|
`old: ${obsolete.idForLogging()} -> new: ${current.idForLogging()}`
|
|
|
|
);
|
|
|
|
|
|
|
|
const obsoleteActiveAt = obsolete.get('active_at');
|
|
|
|
const currentActiveAt = current.get('active_at');
|
2024-02-16 20:39:58 +00:00
|
|
|
let activeAt: number | null | undefined;
|
|
|
|
|
|
|
|
if (obsoleteActiveAt && currentActiveAt) {
|
|
|
|
activeAt = Math.max(obsoleteActiveAt, currentActiveAt);
|
|
|
|
} else {
|
|
|
|
activeAt = obsoleteActiveAt || currentActiveAt;
|
|
|
|
}
|
2022-12-05 22:46:54 +00:00
|
|
|
current.set('active_at', activeAt);
|
|
|
|
|
2024-03-06 23:59:51 +00:00
|
|
|
const currentHadMessages = (current.get('messageCount') ?? 0) > 0;
|
|
|
|
|
2022-09-13 16:16:01 +00:00
|
|
|
const dataToCopy: Partial<ConversationAttributesType> = pick(
|
|
|
|
obsolete.attributes,
|
|
|
|
[
|
|
|
|
'conversationColor',
|
|
|
|
'customColor',
|
|
|
|
'customColorId',
|
|
|
|
'draftAttachments',
|
|
|
|
'draftBodyRanges',
|
|
|
|
'draftTimestamp',
|
|
|
|
'messageCount',
|
|
|
|
'messageRequestResponseType',
|
2024-03-06 23:59:51 +00:00
|
|
|
'needsTitleTransition',
|
2023-03-22 00:46:01 +00:00
|
|
|
'profileSharing',
|
2022-09-13 16:16:01 +00:00
|
|
|
'quotedMessageId',
|
|
|
|
'sentMessageCount',
|
|
|
|
]
|
|
|
|
);
|
2022-08-09 23:46:01 +00:00
|
|
|
|
2022-09-13 16:16:01 +00:00
|
|
|
const keys = Object.keys(dataToCopy) as Array<
|
|
|
|
keyof ConversationAttributesType
|
|
|
|
>;
|
|
|
|
keys.forEach(key => {
|
|
|
|
if (current.get(key) === undefined) {
|
|
|
|
current.set(key, dataToCopy[key]);
|
|
|
|
|
|
|
|
// To ensure that any files on disk don't get deleted out from under us
|
|
|
|
if (key === 'draftAttachments') {
|
|
|
|
obsolete.set(key, undefined);
|
2022-08-09 23:46:01 +00:00
|
|
|
}
|
2022-09-13 16:16:01 +00:00
|
|
|
}
|
|
|
|
});
|
2022-08-09 23:46:01 +00:00
|
|
|
|
2022-09-13 16:16:01 +00:00
|
|
|
if (obsolete.get('isPinned')) {
|
|
|
|
obsolete.unpin();
|
2022-08-10 18:39:04 +00:00
|
|
|
|
2022-09-13 16:16:01 +00:00
|
|
|
if (!current.get('isPinned')) {
|
|
|
|
current.pin();
|
2022-08-10 18:39:04 +00:00
|
|
|
}
|
2022-09-13 16:16:01 +00:00
|
|
|
}
|
2022-08-10 18:39:04 +00:00
|
|
|
|
2022-09-13 16:16:01 +00:00
|
|
|
const obsoleteId = obsolete.get('id');
|
2023-08-10 16:43:33 +00:00
|
|
|
const obsoleteServiceId = obsolete.getServiceId();
|
2022-09-13 16:16:01 +00:00
|
|
|
const currentId = current.get('id');
|
2022-08-09 21:39:00 +00:00
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
if (conversationType === 'private' && obsoleteServiceId) {
|
2022-09-13 16:16:01 +00:00
|
|
|
if (!current.get('profileKey') && obsolete.get('profileKey')) {
|
|
|
|
log.warn(`${logId}: Copying profile key from old to new contact`);
|
2020-07-10 18:28:49 +00:00
|
|
|
|
2022-09-13 16:16:01 +00:00
|
|
|
const profileKey = obsolete.get('profileKey');
|
2022-08-09 21:39:00 +00:00
|
|
|
|
2022-09-13 16:16:01 +00:00
|
|
|
if (profileKey) {
|
|
|
|
await current.setProfileKey(profileKey);
|
2022-08-09 21:39:00 +00:00
|
|
|
}
|
2022-09-13 16:16:01 +00:00
|
|
|
}
|
2021-09-10 02:38:11 +00:00
|
|
|
|
2022-09-13 16:16:01 +00:00
|
|
|
log.warn(`${logId}: Delete all sessions tied to old conversationId`);
|
2023-08-16 20:54:39 +00:00
|
|
|
// Note: we use the conversationId here in case we've already lost our service id.
|
2022-12-12 22:06:16 +00:00
|
|
|
await window.textsecure.storage.protocol.removeSessionsByConversation(
|
|
|
|
obsoleteId
|
|
|
|
);
|
2022-08-09 21:39:00 +00:00
|
|
|
|
2022-09-13 16:16:01 +00:00
|
|
|
log.warn(
|
|
|
|
`${logId}: Delete all identity information tied to old conversationId`
|
|
|
|
);
|
2023-08-10 16:43:33 +00:00
|
|
|
if (obsoleteServiceId) {
|
2022-09-13 16:16:01 +00:00
|
|
|
await window.textsecure.storage.protocol.removeIdentityKey(
|
2023-08-10 16:43:33 +00:00
|
|
|
obsoleteServiceId
|
2022-08-09 21:39:00 +00:00
|
|
|
);
|
|
|
|
}
|
2020-07-10 18:28:49 +00:00
|
|
|
|
2022-08-09 21:39:00 +00:00
|
|
|
log.warn(
|
2022-09-13 16:16:01 +00:00
|
|
|
`${logId}: Ensure that all V1 groups have new conversationId instead of old`
|
2022-08-09 23:46:01 +00:00
|
|
|
);
|
2023-08-10 16:43:33 +00:00
|
|
|
const groups = await this.getAllGroupsInvolvingServiceId(
|
|
|
|
obsoleteServiceId
|
|
|
|
);
|
2022-09-13 16:16:01 +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);
|
2022-08-09 23:46:01 +00:00
|
|
|
});
|
2022-09-13 16:16:01 +00:00
|
|
|
}
|
2022-08-09 23:46:01 +00:00
|
|
|
|
2022-09-13 16:16:01 +00:00
|
|
|
// Note: we explicitly don't want to update V2 groups
|
|
|
|
|
2023-03-24 00:52:46 +00:00
|
|
|
const obsoleteHadMessages = (obsolete.get('messageCount') ?? 0) > 0;
|
|
|
|
|
2022-09-13 16:16:01 +00:00
|
|
|
log.warn(`${logId}: Delete the obsolete conversation from the database`);
|
|
|
|
await removeConversation(obsoleteId);
|
|
|
|
|
2023-05-24 22:05:04 +00:00
|
|
|
const obsoleteStorageID = obsolete.get('storageID');
|
|
|
|
|
|
|
|
if (obsoleteStorageID) {
|
|
|
|
log.warn(
|
|
|
|
`${logId}: Obsolete conversation was in storage service, scheduling removal`
|
|
|
|
);
|
|
|
|
|
|
|
|
const obsoleteStorageVersion = obsolete.get('storageVersion');
|
|
|
|
StorageService.addPendingDelete({
|
|
|
|
storageID: obsoleteStorageID,
|
|
|
|
storageVersion: obsoleteStorageVersion,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-10-04 00:12:57 +00:00
|
|
|
log.warn(`${logId}: Update cached messages in MessageCache`);
|
|
|
|
window.MessageCache.replaceAllObsoleteConversationIds({
|
|
|
|
conversationId: currentId,
|
|
|
|
obsoleteId,
|
2022-09-13 16:16:01 +00:00
|
|
|
});
|
|
|
|
log.warn(`${logId}: Update messages table`);
|
|
|
|
await migrateConversationMessages(obsoleteId, currentId);
|
2022-08-10 18:39:04 +00:00
|
|
|
|
2022-09-13 16:16:01 +00:00
|
|
|
log.warn(`${logId}: Emit refreshConversation event to close old/open new`);
|
|
|
|
window.Whisper.events.trigger('refreshConversation', {
|
|
|
|
newId: currentId,
|
|
|
|
oldId: obsoleteId,
|
2020-07-10 18:28:49 +00:00
|
|
|
});
|
2022-09-13 16:16:01 +00:00
|
|
|
|
|
|
|
log.warn(
|
|
|
|
`${logId}: Eliminate old conversation from ConversationController lookups`
|
|
|
|
);
|
|
|
|
this._conversations.remove(obsolete);
|
|
|
|
this._conversations.resetLookups();
|
|
|
|
|
|
|
|
current.captureChange('combineConversations');
|
2023-01-10 18:57:39 +00:00
|
|
|
drop(current.updateLastMessage());
|
|
|
|
|
|
|
|
const state = window.reduxStore.getState();
|
|
|
|
if (state.conversations.selectedConversationId === current.id) {
|
|
|
|
// TODO: DESKTOP-4807
|
|
|
|
drop(current.loadNewestMessages(undefined, undefined));
|
|
|
|
}
|
2022-12-05 22:46:54 +00:00
|
|
|
|
|
|
|
const titleIsUseful = Boolean(
|
|
|
|
obsoleteTitleInfo && getTitleNoDefault(obsoleteTitleInfo)
|
|
|
|
);
|
2024-03-06 23:59:51 +00:00
|
|
|
// If both conversations had messages - add merge
|
|
|
|
if (
|
|
|
|
titleIsUseful &&
|
|
|
|
conversationType === 'private' &&
|
|
|
|
currentHadMessages &&
|
|
|
|
obsoleteHadMessages
|
|
|
|
) {
|
|
|
|
assertDev(obsoleteTitleInfo, 'part of titleIsUseful boolean');
|
|
|
|
|
2023-01-10 18:57:39 +00:00
|
|
|
drop(current.addConversationMerge(obsoleteTitleInfo));
|
2022-12-05 22:46:54 +00:00
|
|
|
}
|
2022-09-13 16:16:01 +00:00
|
|
|
|
|
|
|
log.warn(`${logId}: Complete!`);
|
2020-07-10 18:28:49 +00:00
|
|
|
}
|
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
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
async getAllGroupsInvolvingServiceId(
|
|
|
|
serviceId: ServiceIdString
|
2020-09-24 20:57:54 +00:00
|
|
|
): Promise<Array<ConversationModel>> {
|
2023-08-10 16:43:33 +00:00
|
|
|
const groups = await getAllGroupsInvolvingServiceId(serviceId);
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-30 23:25:55 +00:00
|
|
|
async clearShareMyPhoneNumber(): Promise<void> {
|
|
|
|
const sharedWith = this.getAll().filter(c => c.get('shareMyPhoneNumber'));
|
|
|
|
|
|
|
|
if (sharedWith.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
log.info(
|
|
|
|
'ConversationController.clearShareMyPhoneNumber: ' +
|
|
|
|
`updating ${sharedWith.length} conversations`
|
|
|
|
);
|
|
|
|
|
|
|
|
await window.Signal.Data.updateConversations(
|
|
|
|
sharedWith.map(c => {
|
|
|
|
c.unset('shareMyPhoneNumber');
|
|
|
|
return c.attributes;
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-08-15 21:53:33 +00:00
|
|
|
// For testing
|
|
|
|
async _forgetE164(e164: string): Promise<void> {
|
|
|
|
const { server } = window.textsecure;
|
|
|
|
strictAssert(server, 'Server must be initialized');
|
2024-02-13 21:41:48 +00:00
|
|
|
const { entries: serviceIdMap } = await getServiceIdsForE164s(server, [
|
|
|
|
e164,
|
|
|
|
]);
|
2022-08-18 20:44:53 +00:00
|
|
|
|
2023-08-16 20:54:39 +00:00
|
|
|
const pni = serviceIdMap.get(e164)?.pni;
|
2022-08-15 21:53:33 +00:00
|
|
|
|
|
|
|
log.info(`ConversationController: forgetting e164=${e164} pni=${pni}`);
|
|
|
|
|
|
|
|
const convos = [this.get(e164), this.get(pni)];
|
|
|
|
|
|
|
|
for (const convo of convos) {
|
|
|
|
if (!convo) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
|
await removeConversation(convo.id);
|
|
|
|
this._conversations.remove(convo);
|
|
|
|
this._conversations.resetLookups();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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,
|
2022-06-27 16:46:43 +00:00
|
|
|
timeout: MINUTE * 30,
|
2021-11-23 22:01:03 +00:00
|
|
|
throwOnTimeout: true,
|
|
|
|
});
|
2022-12-21 18:41:48 +00:00
|
|
|
drop(
|
|
|
|
queue.addAll(
|
|
|
|
temporaryConversations.map(item => async () => {
|
|
|
|
await removeConversation(item.id);
|
|
|
|
})
|
|
|
|
)
|
2021-11-04 21:11:47 +00:00
|
|
|
);
|
|
|
|
await queue.onIdle();
|
2021-01-29 22:16:48 +00:00
|
|
|
|
2024-03-15 14:20:33 +00:00
|
|
|
// It is alright to call it first because the 'add'/'update' events are
|
|
|
|
// triggered after updating the collection.
|
2021-11-04 21:11:47 +00:00
|
|
|
this._initialFetchComplete = true;
|
2020-07-10 18:28:49 +00:00
|
|
|
|
2024-03-15 14:20:33 +00:00
|
|
|
// Hydrate the final set of conversations
|
|
|
|
batchDispatch(() => {
|
|
|
|
this._conversations.add(
|
|
|
|
collection.filter(conversation => !conversation.isTemporary)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
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
|
|
|
|
2023-08-16 20:54:39 +00:00
|
|
|
// Clean up the conversations that have service id as their e164.
|
2021-11-04 21:11:47 +00:00
|
|
|
const e164 = conversation.get('e164');
|
2023-08-16 20:54:39 +00:00
|
|
|
const serviceId = conversation.getServiceId();
|
|
|
|
if (e164 && isServiceIdString(e164) && serviceId) {
|
2021-11-04 21:11:47 +00:00
|
|
|
conversation.set({ e164: undefined });
|
|
|
|
updateConversation(conversation.attributes);
|
|
|
|
|
2023-08-16 20:54:39 +00:00
|
|
|
log.info(
|
|
|
|
`Cleaning up conversation(${serviceId}) with invalid e164`
|
|
|
|
);
|
2021-11-04 21:11:47 +00:00
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
log.error(
|
|
|
|
'ConversationController.load/map: Failed to prepare a conversation',
|
2022-11-22 18:43:43 +00:00
|
|
|
Errors.toLogFormat(error)
|
2021-11-04 21:11:47 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
2024-03-15 14:20:33 +00:00
|
|
|
log.info(
|
|
|
|
'ConversationController: done with initial fetch, ' +
|
|
|
|
`got ${this._conversations.length} conversations`
|
|
|
|
);
|
2021-11-04 21:11:47 +00:00
|
|
|
} catch (error) {
|
|
|
|
log.error(
|
|
|
|
'ConversationController: initial fetch failed',
|
2022-11-22 18:43:43 +00:00
|
|
|
Errors.toLogFormat(error)
|
2021-11-04 21:11:47 +00:00
|
|
|
);
|
|
|
|
throw error;
|
|
|
|
}
|
2020-07-10 18:28:49 +00:00
|
|
|
}
|
|
|
|
}
|