2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
2021-05-07 01:15:25 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2022-01-14 21:34:52 +00:00
|
|
|
import { z } from 'zod';
|
2022-07-01 16:55:13 +00:00
|
|
|
import { isBoolean, isNumber } from 'lodash';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { CallbackResultType } from '../textsecure/Types.d';
|
2021-07-15 23:48:09 +00:00
|
|
|
import dataInterface from '../sql/Client';
|
2021-09-17 18:27:53 +00:00
|
|
|
import * as log from '../logging/log';
|
2022-02-22 20:34:57 +00:00
|
|
|
import {
|
|
|
|
OutgoingMessageError,
|
|
|
|
SendMessageNetworkError,
|
|
|
|
SendMessageProtoError,
|
|
|
|
UnregisteredUserError,
|
|
|
|
} from '../textsecure/Errors';
|
|
|
|
import { SEALED_SENDER } from '../types/SealedSender';
|
2023-08-10 16:43:33 +00:00
|
|
|
import type { ServiceIdString } from '../types/ServiceId';
|
2021-07-15 23:48:09 +00:00
|
|
|
|
2022-02-22 20:34:57 +00:00
|
|
|
const { insertSentProto, updateConversation } = dataInterface;
|
2021-05-07 01:15:25 +00:00
|
|
|
|
2022-01-14 21:34:52 +00:00
|
|
|
export const sendTypesEnum = z.enum([
|
2022-07-01 16:55:13 +00:00
|
|
|
// Core user interactions, default urgent
|
|
|
|
'message',
|
2022-07-05 22:20:30 +00:00
|
|
|
'story', // non-urgent
|
2022-07-01 16:55:13 +00:00
|
|
|
'callingMessage', // excluded from send log; only call-initiation messages are urgent
|
2022-01-14 21:34:52 +00:00
|
|
|
'deleteForEveryone',
|
2022-07-01 16:55:13 +00:00
|
|
|
'expirationTimerUpdate', // non-urgent
|
|
|
|
'groupChange', // non-urgent
|
|
|
|
'reaction',
|
|
|
|
'typing', // excluded from send log; non-urgent
|
|
|
|
|
|
|
|
// Responding to incoming messages, all non-urgent
|
2022-01-14 21:34:52 +00:00
|
|
|
'deliveryReceipt',
|
2022-07-01 16:55:13 +00:00
|
|
|
'readReceipt',
|
|
|
|
'viewedReceipt',
|
|
|
|
|
|
|
|
// Encryption housekeeping, default non-urgent
|
2022-01-14 21:34:52 +00:00
|
|
|
'nullMessage',
|
|
|
|
'profileKeyUpdate',
|
2022-07-01 16:55:13 +00:00
|
|
|
'resendFromLog', // excluded from send log, only urgent if original message was urgent
|
2022-01-14 21:34:52 +00:00
|
|
|
'retryRequest', // excluded from send log
|
2022-07-01 16:55:13 +00:00
|
|
|
'senderKeyDistributionMessage', // only urgent if associated message is
|
|
|
|
|
|
|
|
// Sync messages sent during link, default non-urgent
|
|
|
|
'blockSyncRequest',
|
|
|
|
'configurationSyncRequest',
|
|
|
|
'contactSyncRequest', // urgent because it blocks the link process
|
|
|
|
'keySyncRequest', // urgent because it blocks the link process
|
|
|
|
'pniIdentitySyncRequest', // urgent because we need our PNI to be fully functional
|
|
|
|
|
2022-07-05 22:20:30 +00:00
|
|
|
// The actual sync messages, which we never send, just receive - non-urgent
|
|
|
|
'blockSync',
|
|
|
|
'configurationSync',
|
|
|
|
'contactSync',
|
|
|
|
'keySync',
|
|
|
|
'pniIdentitySync',
|
|
|
|
|
2022-07-01 16:55:13 +00:00
|
|
|
// Syncs, default non-urgent
|
|
|
|
'fetchLatestManifestSync',
|
|
|
|
'fetchLocalProfileSync',
|
|
|
|
'messageRequestSync',
|
|
|
|
'readSync', // urgent
|
2022-01-14 21:34:52 +00:00
|
|
|
'sentSync',
|
|
|
|
'stickerPackSync',
|
|
|
|
'verificationSync',
|
|
|
|
'viewOnceSync',
|
|
|
|
'viewSync',
|
2023-01-10 00:52:01 +00:00
|
|
|
'callEventSync',
|
2023-08-09 00:53:06 +00:00
|
|
|
'callLogEventSync',
|
2022-07-01 16:55:13 +00:00
|
|
|
|
|
|
|
// No longer used, all non-urgent
|
|
|
|
'legacyGroupChange',
|
|
|
|
'resetSession',
|
2022-01-14 21:34:52 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
export type SendTypesType = z.infer<typeof sendTypesEnum>;
|
2021-07-15 23:48:09 +00:00
|
|
|
|
|
|
|
export function shouldSaveProto(sendType: SendTypesType): boolean {
|
|
|
|
if (sendType === 'callingMessage') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sendType === 'resendFromLog') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sendType === 'retryRequest') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sendType === 'typing') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-02-22 20:34:57 +00:00
|
|
|
function processError(error: unknown): void {
|
|
|
|
if (
|
|
|
|
error instanceof OutgoingMessageError ||
|
|
|
|
error instanceof SendMessageNetworkError
|
|
|
|
) {
|
|
|
|
const conversation = window.ConversationController.getOrCreate(
|
|
|
|
error.identifier,
|
|
|
|
'private'
|
|
|
|
);
|
|
|
|
if (error.code === 401 || error.code === 403) {
|
|
|
|
if (
|
|
|
|
conversation.get('sealedSender') === SEALED_SENDER.ENABLED ||
|
|
|
|
conversation.get('sealedSender') === SEALED_SENDER.UNRESTRICTED
|
|
|
|
) {
|
|
|
|
log.warn(
|
|
|
|
`handleMessageSend: Got 401/403 for ${conversation.idForLogging()}, removing profile key`
|
|
|
|
);
|
|
|
|
|
2022-12-21 18:41:48 +00:00
|
|
|
void conversation.setProfileKey(undefined);
|
2022-02-22 20:34:57 +00:00
|
|
|
}
|
|
|
|
if (conversation.get('sealedSender') === SEALED_SENDER.UNKNOWN) {
|
|
|
|
log.warn(
|
|
|
|
`handleMessageSend: Got 401/403 for ${conversation.idForLogging()}, setting sealedSender = DISABLED`
|
|
|
|
);
|
|
|
|
conversation.set('sealedSender', SEALED_SENDER.DISABLED);
|
|
|
|
updateConversation(conversation.attributes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (error.code === 404) {
|
|
|
|
log.warn(
|
|
|
|
`handleMessageSend: Got 404 for ${conversation.idForLogging()}, marking unregistered.`
|
|
|
|
);
|
|
|
|
conversation.setUnregistered();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (error instanceof UnregisteredUserError) {
|
|
|
|
const conversation = window.ConversationController.getOrCreate(
|
2023-08-10 16:43:33 +00:00
|
|
|
error.serviceId,
|
2022-02-22 20:34:57 +00:00
|
|
|
'private'
|
|
|
|
);
|
|
|
|
log.warn(
|
|
|
|
`handleMessageSend: Got 404 for ${conversation.idForLogging()}, marking unregistered.`
|
|
|
|
);
|
|
|
|
conversation.setUnregistered();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-07 01:15:25 +00:00
|
|
|
export async function handleMessageSend(
|
2021-07-15 23:48:09 +00:00
|
|
|
promise: Promise<CallbackResultType>,
|
|
|
|
options: {
|
|
|
|
messageIds: Array<string>;
|
|
|
|
sendType: SendTypesType;
|
|
|
|
}
|
|
|
|
): Promise<CallbackResultType> {
|
2021-05-07 01:15:25 +00:00
|
|
|
try {
|
|
|
|
const result = await promise;
|
2021-07-15 23:48:09 +00:00
|
|
|
|
|
|
|
await maybeSaveToSendLog(result, options);
|
|
|
|
|
|
|
|
await handleMessageSendResult(
|
2023-08-10 16:43:33 +00:00
|
|
|
result.failoverServiceIds,
|
2021-07-15 23:48:09 +00:00
|
|
|
result.unidentifiedDeliveries
|
|
|
|
);
|
|
|
|
|
2021-05-07 01:15:25 +00:00
|
|
|
return result;
|
|
|
|
} catch (err) {
|
2022-02-22 20:34:57 +00:00
|
|
|
processError(err);
|
|
|
|
|
|
|
|
if (err instanceof SendMessageProtoError) {
|
2021-05-07 01:15:25 +00:00
|
|
|
await handleMessageSendResult(
|
2023-08-10 16:43:33 +00:00
|
|
|
err.failoverServiceIds,
|
2021-05-07 01:15:25 +00:00
|
|
|
err.unidentifiedDeliveries
|
|
|
|
);
|
2022-02-22 20:34:57 +00:00
|
|
|
|
|
|
|
err.errors?.forEach(processError);
|
2021-05-07 01:15:25 +00:00
|
|
|
}
|
2022-02-22 20:34:57 +00:00
|
|
|
|
2021-05-07 01:15:25 +00:00
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function handleMessageSendResult(
|
2023-08-10 16:43:33 +00:00
|
|
|
failoverServiceIds: Array<ServiceIdString> | undefined,
|
|
|
|
unidentifiedDeliveries: Array<ServiceIdString> | undefined
|
2021-05-07 01:15:25 +00:00
|
|
|
): Promise<void> {
|
|
|
|
await Promise.all(
|
2023-08-10 16:43:33 +00:00
|
|
|
(failoverServiceIds || []).map(async serviceId => {
|
|
|
|
const conversation = window.ConversationController.get(serviceId);
|
2021-05-07 01:15:25 +00:00
|
|
|
|
|
|
|
if (
|
|
|
|
conversation &&
|
|
|
|
conversation.get('sealedSender') !== SEALED_SENDER.DISABLED
|
|
|
|
) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2021-05-07 01:15:25 +00:00
|
|
|
`Setting sealedSender to DISABLED for conversation ${conversation.idForLogging()}`
|
|
|
|
);
|
|
|
|
conversation.set({
|
|
|
|
sealedSender: SEALED_SENDER.DISABLED,
|
|
|
|
});
|
|
|
|
window.Signal.Data.updateConversation(conversation.attributes);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
await Promise.all(
|
2023-08-10 16:43:33 +00:00
|
|
|
(unidentifiedDeliveries || []).map(async serviceId => {
|
|
|
|
const conversation = window.ConversationController.get(serviceId);
|
2021-05-07 01:15:25 +00:00
|
|
|
|
|
|
|
if (
|
|
|
|
conversation &&
|
|
|
|
conversation.get('sealedSender') === SEALED_SENDER.UNKNOWN
|
|
|
|
) {
|
|
|
|
if (conversation.get('accessKey')) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2021-05-07 01:15:25 +00:00
|
|
|
`Setting sealedSender to ENABLED for conversation ${conversation.idForLogging()}`
|
|
|
|
);
|
|
|
|
conversation.set({
|
|
|
|
sealedSender: SEALED_SENDER.ENABLED,
|
|
|
|
});
|
|
|
|
} else {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2021-05-07 01:15:25 +00:00
|
|
|
`Setting sealedSender to UNRESTRICTED for conversation ${conversation.idForLogging()}`
|
|
|
|
);
|
|
|
|
conversation.set({
|
|
|
|
sealedSender: SEALED_SENDER.UNRESTRICTED,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
window.Signal.Data.updateConversation(conversation.attributes);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
2021-07-15 23:48:09 +00:00
|
|
|
|
|
|
|
async function maybeSaveToSendLog(
|
|
|
|
result: CallbackResultType,
|
|
|
|
{
|
|
|
|
messageIds,
|
|
|
|
sendType,
|
|
|
|
}: {
|
|
|
|
messageIds: Array<string>;
|
|
|
|
sendType: SendTypesType;
|
|
|
|
}
|
|
|
|
): Promise<void> {
|
2022-08-15 21:53:33 +00:00
|
|
|
const {
|
|
|
|
contentHint,
|
|
|
|
contentProto,
|
|
|
|
recipients,
|
|
|
|
timestamp,
|
|
|
|
urgent,
|
|
|
|
hasPniSignatureMessage,
|
|
|
|
} = result;
|
2021-07-15 23:48:09 +00:00
|
|
|
|
|
|
|
if (!shouldSaveProto(sendType)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isNumber(contentHint) || !contentProto || !recipients || !timestamp) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn(
|
2021-07-15 23:48:09 +00:00
|
|
|
`handleMessageSend: Missing necessary information to save to log for ${sendType} message ${timestamp}`
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const identifiers = Object.keys(recipients);
|
|
|
|
if (identifiers.length === 0) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn(
|
2021-07-15 23:48:09 +00:00
|
|
|
`handleMessageSend: ${sendType} message ${timestamp} had no recipients`
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the identifier count is greater than one, we've done the save elsewhere
|
|
|
|
if (identifiers.length > 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await insertSentProto(
|
|
|
|
{
|
|
|
|
timestamp,
|
|
|
|
proto: Buffer.from(contentProto),
|
|
|
|
contentHint,
|
2022-07-01 16:55:13 +00:00
|
|
|
urgent: isBoolean(urgent) ? urgent : true,
|
2022-08-15 21:53:33 +00:00
|
|
|
hasPniSignatureMessage: Boolean(hasPniSignatureMessage),
|
2021-07-15 23:48:09 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
messageIds,
|
|
|
|
recipients,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|