2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2020 Signal Messenger, LLC
|
2021-06-17 17:15:10 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
/* eslint-disable max-classes-per-file */
|
|
|
|
|
|
|
|
import { Collection, Model } from 'backbone';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { ConversationModel } from '../models/conversations';
|
2021-09-17 18:27:53 +00:00
|
|
|
import * as log from '../logging/log';
|
2022-11-22 18:43:43 +00:00
|
|
|
import * as Errors from '../types/errors';
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2022-01-04 15:27:16 +00:00
|
|
|
export type MessageRequestAttributesType = {
|
2021-06-17 17:15:10 +00:00
|
|
|
threadE164?: string;
|
|
|
|
threadUuid?: string;
|
|
|
|
groupV2Id?: string;
|
|
|
|
type: number;
|
|
|
|
};
|
|
|
|
|
|
|
|
class MessageRequestModel extends Model<MessageRequestAttributesType> {}
|
|
|
|
|
|
|
|
let singleton: MessageRequests | undefined;
|
|
|
|
|
|
|
|
export class MessageRequests extends Collection<MessageRequestModel> {
|
|
|
|
static getSingleton(): MessageRequests {
|
|
|
|
if (!singleton) {
|
|
|
|
singleton = new MessageRequests();
|
|
|
|
}
|
|
|
|
|
|
|
|
return singleton;
|
|
|
|
}
|
|
|
|
|
|
|
|
forConversation(conversation: ConversationModel): MessageRequestModel | null {
|
|
|
|
if (conversation.get('e164')) {
|
|
|
|
const syncByE164 = this.findWhere({
|
|
|
|
threadE164: conversation.get('e164'),
|
|
|
|
});
|
|
|
|
if (syncByE164) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2021-06-17 17:15:10 +00:00
|
|
|
`Found early message request response for E164 ${conversation.idForLogging()}`
|
|
|
|
);
|
|
|
|
this.remove(syncByE164);
|
|
|
|
return syncByE164;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (conversation.get('uuid')) {
|
|
|
|
const syncByUuid = this.findWhere({
|
|
|
|
threadUuid: conversation.get('uuid'),
|
|
|
|
});
|
|
|
|
if (syncByUuid) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2021-06-17 17:15:10 +00:00
|
|
|
`Found early message request response for UUID ${conversation.idForLogging()}`
|
|
|
|
);
|
|
|
|
this.remove(syncByUuid);
|
|
|
|
return syncByUuid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// V2 group
|
|
|
|
if (conversation.get('groupId')) {
|
|
|
|
const syncByGroupId = this.findWhere({
|
|
|
|
groupV2Id: conversation.get('groupId'),
|
|
|
|
});
|
|
|
|
if (syncByGroupId) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2021-06-17 17:15:10 +00:00
|
|
|
`Found early message request response for group v2 ID ${conversation.idForLogging()}`
|
|
|
|
);
|
|
|
|
this.remove(syncByGroupId);
|
|
|
|
return syncByGroupId;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
async onResponse(sync: MessageRequestModel): Promise<void> {
|
|
|
|
try {
|
|
|
|
const threadE164 = sync.get('threadE164');
|
|
|
|
const threadUuid = sync.get('threadUuid');
|
|
|
|
const groupV2Id = sync.get('groupV2Id');
|
|
|
|
|
|
|
|
let conversation;
|
|
|
|
|
|
|
|
// We multiplex between GV1/GV2 groups here, but we don't kick off migrations
|
|
|
|
if (groupV2Id) {
|
|
|
|
conversation = window.ConversationController.get(groupV2Id);
|
|
|
|
}
|
|
|
|
if (!conversation && (threadE164 || threadUuid)) {
|
2022-08-09 21:39:00 +00:00
|
|
|
conversation = window.ConversationController.lookupOrCreate({
|
|
|
|
e164: threadE164,
|
|
|
|
uuid: threadUuid,
|
2022-12-03 01:05:27 +00:00
|
|
|
reason: 'MessageRequests.onResponse',
|
2022-08-09 21:39:00 +00:00
|
|
|
});
|
2021-06-17 17:15:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!conversation) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn(
|
2023-04-15 00:52:50 +00:00
|
|
|
`Received message request response for unknown conversation: groupv2(${groupV2Id}) ${threadUuid} ${threadE164}`
|
2021-06-17 17:15:10 +00:00
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-21 18:41:48 +00:00
|
|
|
void conversation.applyMessageRequestResponse(sync.get('type'), {
|
2021-06-17 17:15:10 +00:00
|
|
|
fromSync: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
this.remove(sync);
|
|
|
|
} catch (error) {
|
2022-11-22 18:43:43 +00:00
|
|
|
log.error('MessageRequests.onResponse error:', Errors.toLogFormat(error));
|
2021-06-17 17:15:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|