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
|
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
import type { AciString } from '../types/ServiceId';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { ConversationModel } from '../models/conversations';
|
2022-11-22 18:43:43 +00:00
|
|
|
import * as Errors from '../types/errors';
|
2023-08-21 20:08:27 +00:00
|
|
|
import * as log from '../logging/log';
|
|
|
|
import { drop } from '../util/drop';
|
|
|
|
import { getConversationIdForLogging } from '../util/idForLogging';
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2022-01-04 15:27:16 +00:00
|
|
|
export type MessageRequestAttributesType = {
|
2023-08-21 20:08:27 +00:00
|
|
|
envelopeId: string;
|
2021-06-17 17:15:10 +00:00
|
|
|
groupV2Id?: string;
|
2023-08-21 20:08:27 +00:00
|
|
|
removeFromMessageReceiverCache: () => unknown;
|
|
|
|
threadAci?: AciString;
|
|
|
|
threadE164?: string;
|
2021-06-17 17:15:10 +00:00
|
|
|
type: number;
|
|
|
|
};
|
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
const messageRequests = new Map<string, MessageRequestAttributesType>();
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
function remove(sync: MessageRequestAttributesType): void {
|
|
|
|
messageRequests.delete(sync.envelopeId);
|
|
|
|
sync.removeFromMessageReceiverCache();
|
|
|
|
}
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
export function forConversation(
|
|
|
|
conversation: ConversationModel
|
|
|
|
): MessageRequestAttributesType | null {
|
|
|
|
const logId = `MessageRequests.forConversation(${getConversationIdForLogging(
|
|
|
|
conversation.attributes
|
|
|
|
)})`;
|
|
|
|
|
|
|
|
const messageRequestValues = Array.from(messageRequests.values());
|
|
|
|
|
|
|
|
if (conversation.get('e164')) {
|
|
|
|
const syncByE164 = messageRequestValues.find(
|
|
|
|
item => item.threadE164 === conversation.get('e164')
|
|
|
|
);
|
|
|
|
if (syncByE164) {
|
|
|
|
log.info(`${logId}: Found early message request response for E164`);
|
|
|
|
remove(syncByE164);
|
|
|
|
return syncByE164;
|
2021-06-17 17:15:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
if (conversation.getServiceId()) {
|
|
|
|
const syncByServiceId = messageRequestValues.find(
|
|
|
|
item => item.threadAci === conversation.getServiceId()
|
|
|
|
);
|
|
|
|
if (syncByServiceId) {
|
|
|
|
log.info(`${logId}: Found early message request response for serviceId`);
|
|
|
|
remove(syncByServiceId);
|
|
|
|
return syncByServiceId;
|
2021-06-17 17:15:10 +00:00
|
|
|
}
|
2023-08-21 20:08:27 +00:00
|
|
|
}
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
// V2 group
|
|
|
|
if (conversation.get('groupId')) {
|
|
|
|
const syncByGroupId = messageRequestValues.find(
|
|
|
|
item => item.groupV2Id === conversation.get('groupId')
|
|
|
|
);
|
|
|
|
if (syncByGroupId) {
|
|
|
|
log.info(`${logId}: Found early message request response for gv2`);
|
|
|
|
remove(syncByGroupId);
|
|
|
|
return syncByGroupId;
|
2021-06-17 17:15:10 +00:00
|
|
|
}
|
2023-08-21 20:08:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function onResponse(
|
|
|
|
sync: MessageRequestAttributesType
|
|
|
|
): Promise<void> {
|
|
|
|
messageRequests.set(sync.envelopeId, sync);
|
|
|
|
const { threadE164, threadAci, groupV2Id } = sync;
|
|
|
|
|
|
|
|
const logId = `MessageRequests.onResponse(groupv2(${groupV2Id}) ${threadAci} ${threadE164})`;
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
try {
|
|
|
|
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 || threadAci)) {
|
|
|
|
conversation = window.ConversationController.lookupOrCreate({
|
|
|
|
e164: threadE164,
|
|
|
|
serviceId: threadAci,
|
|
|
|
reason: logId,
|
2021-06-17 17:15:10 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
if (!conversation) {
|
|
|
|
log.warn(
|
|
|
|
`${logId}: received message request response for unknown conversation`
|
|
|
|
);
|
|
|
|
remove(sync);
|
|
|
|
return;
|
|
|
|
}
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
drop(
|
|
|
|
conversation.applyMessageRequestResponse(sync.type, {
|
2021-06-17 17:15:10 +00:00
|
|
|
fromSync: true,
|
2023-08-21 20:08:27 +00:00
|
|
|
})
|
|
|
|
);
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
remove(sync);
|
|
|
|
} catch (error) {
|
|
|
|
remove(sync);
|
|
|
|
log.error(`${logId} error:`, Errors.toLogFormat(error));
|
2021-06-17 17:15:10 +00:00
|
|
|
}
|
|
|
|
}
|