2022-02-16 18:36:21 +00:00
|
|
|
// Copyright 2022 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { isNumber } from 'lodash';
|
|
|
|
|
2022-03-04 19:22:31 +00:00
|
|
|
import * as Errors from '../../types/errors';
|
2022-02-16 18:36:21 +00:00
|
|
|
import { getSendOptions } from '../../util/getSendOptions';
|
|
|
|
import {
|
|
|
|
isDirectConversation,
|
|
|
|
isGroupV2,
|
2022-02-22 20:34:57 +00:00
|
|
|
isMe,
|
2022-02-16 18:36:21 +00:00
|
|
|
} from '../../util/whatTypeOfConversation';
|
|
|
|
import { SignalService as Proto } from '../../protobuf';
|
|
|
|
import {
|
|
|
|
handleMultipleSendErrors,
|
|
|
|
maybeExpandErrors,
|
|
|
|
} from './handleMultipleSendErrors';
|
|
|
|
import { ourProfileKeyService } from '../../services/ourProfileKey';
|
|
|
|
import { wrapWithSyncMessageSend } from '../../util/wrapWithSyncMessageSend';
|
|
|
|
|
|
|
|
import type { ConversationModel } from '../../models/conversations';
|
|
|
|
import type {
|
|
|
|
ConversationQueueJobBundle,
|
|
|
|
DeleteForEveryoneJobData,
|
|
|
|
} from '../conversationJobQueue';
|
2022-05-31 19:46:56 +00:00
|
|
|
import { getUntrustedConversationUuids } from './getUntrustedConversationUuids';
|
2022-02-22 20:34:57 +00:00
|
|
|
import { handleMessageSend } from '../../util/handleMessageSend';
|
|
|
|
import { isConversationAccepted } from '../../util/isConversationAccepted';
|
|
|
|
import { isConversationUnregistered } from '../../util/isConversationUnregistered';
|
2022-03-04 19:22:31 +00:00
|
|
|
import { getMessageById } from '../../messages/getMessageById';
|
|
|
|
import { isNotNil } from '../../util/isNotNil';
|
|
|
|
import type { CallbackResultType } from '../../textsecure/Types.d';
|
|
|
|
import type { MessageModel } from '../../models/messages';
|
|
|
|
import { SendMessageProtoError } from '../../textsecure/Errors';
|
|
|
|
import { strictAssert } from '../../util/assert';
|
|
|
|
import type { LoggerType } from '../../types/Logging';
|
2022-10-20 22:10:10 +00:00
|
|
|
import { isStory } from '../../messages/helpers';
|
2023-04-11 03:54:43 +00:00
|
|
|
import { sendToGroup } from '../../util/sendToGroup';
|
2022-02-16 18:36:21 +00:00
|
|
|
|
|
|
|
export async function sendDeleteForEveryone(
|
|
|
|
conversation: ConversationModel,
|
|
|
|
{
|
|
|
|
isFinalAttempt,
|
2022-06-13 21:39:35 +00:00
|
|
|
messaging,
|
2022-02-16 18:36:21 +00:00
|
|
|
shouldContinue,
|
|
|
|
timestamp,
|
|
|
|
timeRemaining,
|
|
|
|
log,
|
|
|
|
}: ConversationQueueJobBundle,
|
|
|
|
data: DeleteForEveryoneJobData
|
|
|
|
): Promise<void> {
|
2022-03-04 19:22:31 +00:00
|
|
|
const {
|
|
|
|
messageId,
|
|
|
|
recipients: recipientsFromJob,
|
|
|
|
revision,
|
|
|
|
targetTimestamp,
|
|
|
|
} = data;
|
|
|
|
|
2022-11-29 02:07:26 +00:00
|
|
|
const logId = `sendDeleteForEveryone(${conversation.idForLogging()}, ${messageId})`;
|
|
|
|
|
2022-03-04 19:22:31 +00:00
|
|
|
const message = await getMessageById(messageId);
|
|
|
|
if (!message) {
|
2022-11-29 02:07:26 +00:00
|
|
|
log.error(`${logId}: Failed to fetch message. Failing job.`);
|
2022-03-04 19:22:31 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-11-29 02:07:26 +00:00
|
|
|
|
2022-10-20 22:10:10 +00:00
|
|
|
const story = isStory(message.attributes);
|
2022-11-29 02:07:26 +00:00
|
|
|
if (story && !isGroupV2(conversation.attributes)) {
|
|
|
|
log.error(`${logId}: 1-on-1 Story DOE must use its own job. Failing job`);
|
|
|
|
return;
|
|
|
|
}
|
2022-03-04 19:22:31 +00:00
|
|
|
|
2022-02-16 18:36:21 +00:00
|
|
|
if (!shouldContinue) {
|
2022-11-29 02:07:26 +00:00
|
|
|
log.info(`${logId}: Ran out of time. Giving up on sending`);
|
2022-12-21 18:41:48 +00:00
|
|
|
void updateMessageWithFailure(
|
|
|
|
message,
|
|
|
|
[new Error('Ran out of time!')],
|
|
|
|
log
|
|
|
|
);
|
2022-02-16 18:36:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const sendType = 'deleteForEveryone';
|
|
|
|
const { ContentHint } = Proto.UnidentifiedSenderMessage.Message;
|
|
|
|
const contentHint = ContentHint.RESENDABLE;
|
|
|
|
const messageIds = [messageId];
|
|
|
|
|
2022-03-04 19:22:31 +00:00
|
|
|
const deletedForEveryoneSendStatus = message.get(
|
|
|
|
'deletedForEveryoneSendStatus'
|
|
|
|
);
|
|
|
|
const recipients = deletedForEveryoneSendStatus
|
|
|
|
? getRecipients(deletedForEveryoneSendStatus)
|
|
|
|
: recipientsFromJob;
|
|
|
|
|
2022-05-31 19:46:56 +00:00
|
|
|
const untrustedUuids = getUntrustedConversationUuids(recipients);
|
|
|
|
if (untrustedUuids.length) {
|
2022-02-16 18:36:21 +00:00
|
|
|
window.reduxActions.conversations.conversationStoppedByMissingVerification({
|
|
|
|
conversationId: conversation.id,
|
2022-05-31 19:46:56 +00:00
|
|
|
untrustedUuids,
|
2022-02-16 18:36:21 +00:00
|
|
|
});
|
|
|
|
throw new Error(
|
2022-05-31 19:46:56 +00:00
|
|
|
`Delete for everyone blocked because ${untrustedUuids.length} conversation(s) were untrusted. Failing this attempt.`
|
2022-02-16 18:36:21 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
await conversation.queueJob(
|
|
|
|
'conversationQueue/sendDeleteForEveryone',
|
2022-05-23 22:08:13 +00:00
|
|
|
async abortSignal => {
|
2022-02-16 18:36:21 +00:00
|
|
|
log.info(
|
2022-11-29 02:07:26 +00:00
|
|
|
`${logId}: Sending deleteForEveryone with timestamp ${timestamp}` +
|
|
|
|
`for message ${targetTimestamp}, isStory=${story}`
|
2022-02-16 18:36:21 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
let profileKey: Uint8Array | undefined;
|
|
|
|
if (conversation.get('profileSharing')) {
|
|
|
|
profileKey = await ourProfileKeyService.get();
|
|
|
|
}
|
|
|
|
|
2022-10-20 22:10:10 +00:00
|
|
|
const sendOptions = await getSendOptions(conversation.attributes, {
|
|
|
|
story,
|
|
|
|
});
|
2022-02-16 18:36:21 +00:00
|
|
|
|
|
|
|
try {
|
2022-02-22 20:34:57 +00:00
|
|
|
if (isMe(conversation.attributes)) {
|
2022-06-13 21:39:35 +00:00
|
|
|
const proto = await messaging.getContentMessage({
|
2022-02-22 20:34:57 +00:00
|
|
|
deletedForEveryoneTimestamp: targetTimestamp,
|
|
|
|
profileKey,
|
|
|
|
recipients: conversation.getRecipients(),
|
|
|
|
timestamp,
|
|
|
|
});
|
2022-03-04 19:22:31 +00:00
|
|
|
strictAssert(
|
|
|
|
proto.dataMessage,
|
|
|
|
'ContentMessage must have dataMessage'
|
|
|
|
);
|
2022-02-22 20:34:57 +00:00
|
|
|
|
|
|
|
await handleMessageSend(
|
2022-06-13 21:39:35 +00:00
|
|
|
messaging.sendSyncMessage({
|
2022-02-22 20:34:57 +00:00
|
|
|
encodedDataMessage: Proto.DataMessage.encode(
|
|
|
|
proto.dataMessage
|
|
|
|
).finish(),
|
|
|
|
destination: conversation.get('e164'),
|
|
|
|
destinationUuid: conversation.get('uuid'),
|
|
|
|
expirationStartTimestamp: null,
|
|
|
|
options: sendOptions,
|
|
|
|
timestamp,
|
2022-07-01 16:55:13 +00:00
|
|
|
urgent: false,
|
2022-02-22 20:34:57 +00:00
|
|
|
}),
|
|
|
|
{ messageIds, sendType }
|
|
|
|
);
|
2022-03-04 19:22:31 +00:00
|
|
|
await updateMessageWithSuccessfulSends(message);
|
2022-02-22 20:34:57 +00:00
|
|
|
} else if (isDirectConversation(conversation.attributes)) {
|
|
|
|
if (!isConversationAccepted(conversation.attributes)) {
|
|
|
|
log.info(
|
|
|
|
`conversation ${conversation.idForLogging()} is not accepted; refusing to send`
|
|
|
|
);
|
2022-12-21 18:41:48 +00:00
|
|
|
void updateMessageWithFailure(
|
2022-03-04 19:22:31 +00:00
|
|
|
message,
|
|
|
|
[new Error('Message request was not accepted')],
|
|
|
|
log
|
|
|
|
);
|
2022-02-22 20:34:57 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (isConversationUnregistered(conversation.attributes)) {
|
|
|
|
log.info(
|
|
|
|
`conversation ${conversation.idForLogging()} is unregistered; refusing to send`
|
|
|
|
);
|
2022-12-21 18:41:48 +00:00
|
|
|
void updateMessageWithFailure(
|
2022-03-04 19:22:31 +00:00
|
|
|
message,
|
|
|
|
[new Error('Contact no longer has a Signal account')],
|
|
|
|
log
|
|
|
|
);
|
2022-02-22 20:34:57 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-02-25 02:40:56 +00:00
|
|
|
if (conversation.isBlocked()) {
|
|
|
|
log.info(
|
|
|
|
`conversation ${conversation.idForLogging()} is blocked; refusing to send`
|
|
|
|
);
|
2022-12-21 18:41:48 +00:00
|
|
|
void updateMessageWithFailure(
|
2022-03-04 19:22:31 +00:00
|
|
|
message,
|
|
|
|
[new Error('Contact is blocked')],
|
|
|
|
log
|
|
|
|
);
|
2022-02-25 02:40:56 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-02-22 20:34:57 +00:00
|
|
|
|
2022-02-16 18:36:21 +00:00
|
|
|
await wrapWithSyncMessageSend({
|
|
|
|
conversation,
|
|
|
|
logId,
|
|
|
|
messageIds,
|
|
|
|
send: async sender =>
|
|
|
|
sender.sendMessageToIdentifier({
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
|
|
identifier: conversation.getSendTarget()!,
|
|
|
|
messageText: undefined,
|
|
|
|
attachments: [],
|
|
|
|
deletedForEveryoneTimestamp: targetTimestamp,
|
|
|
|
timestamp,
|
|
|
|
expireTimer: undefined,
|
|
|
|
contentHint,
|
|
|
|
groupId: undefined,
|
|
|
|
profileKey,
|
|
|
|
options: sendOptions,
|
2022-07-01 16:55:13 +00:00
|
|
|
urgent: true,
|
2022-10-20 22:10:10 +00:00
|
|
|
story,
|
2022-08-15 21:53:33 +00:00
|
|
|
includePniSignatureMessage: true,
|
2022-02-16 18:36:21 +00:00
|
|
|
}),
|
|
|
|
sendType,
|
|
|
|
timestamp,
|
|
|
|
});
|
2022-03-04 19:22:31 +00:00
|
|
|
|
|
|
|
await updateMessageWithSuccessfulSends(message);
|
2022-02-16 18:36:21 +00:00
|
|
|
} else {
|
|
|
|
if (isGroupV2(conversation.attributes) && !isNumber(revision)) {
|
|
|
|
log.error('No revision provided, but conversation is GroupV2');
|
|
|
|
}
|
|
|
|
|
|
|
|
const groupV2Info = conversation.getGroupV2Info({
|
|
|
|
members: recipients,
|
|
|
|
});
|
|
|
|
if (groupV2Info && isNumber(revision)) {
|
|
|
|
groupV2Info.revision = revision;
|
|
|
|
}
|
|
|
|
|
|
|
|
await wrapWithSyncMessageSend({
|
|
|
|
conversation,
|
|
|
|
logId,
|
|
|
|
messageIds,
|
|
|
|
send: async () =>
|
2023-04-11 03:54:43 +00:00
|
|
|
sendToGroup({
|
2022-05-23 22:08:13 +00:00
|
|
|
abortSignal,
|
2022-02-16 18:36:21 +00:00
|
|
|
contentHint,
|
|
|
|
groupSendOptions: {
|
|
|
|
groupV2: groupV2Info,
|
|
|
|
deletedForEveryoneTimestamp: targetTimestamp,
|
|
|
|
timestamp,
|
|
|
|
profileKey,
|
|
|
|
},
|
|
|
|
messageId,
|
|
|
|
sendOptions,
|
|
|
|
sendTarget: conversation.toSenderKeyTarget(),
|
|
|
|
sendType: 'deleteForEveryone',
|
2022-10-20 22:10:10 +00:00
|
|
|
story,
|
2022-07-01 16:55:13 +00:00
|
|
|
urgent: true,
|
2022-02-16 18:36:21 +00:00
|
|
|
}),
|
|
|
|
sendType,
|
|
|
|
timestamp,
|
|
|
|
});
|
2022-03-04 19:22:31 +00:00
|
|
|
|
|
|
|
await updateMessageWithSuccessfulSends(message);
|
2022-02-16 18:36:21 +00:00
|
|
|
}
|
|
|
|
} catch (error: unknown) {
|
2022-03-04 19:22:31 +00:00
|
|
|
if (error instanceof SendMessageProtoError) {
|
|
|
|
await updateMessageWithSuccessfulSends(message, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
const errors = maybeExpandErrors(error);
|
2022-02-16 18:36:21 +00:00
|
|
|
await handleMultipleSendErrors({
|
2022-03-04 19:22:31 +00:00
|
|
|
errors,
|
2022-02-16 18:36:21 +00:00
|
|
|
isFinalAttempt,
|
|
|
|
log,
|
2022-03-04 19:22:31 +00:00
|
|
|
markFailed: () => updateMessageWithFailure(message, errors, log),
|
2022-02-16 18:36:21 +00:00
|
|
|
timeRemaining,
|
|
|
|
toThrow: error,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2022-03-04 19:22:31 +00:00
|
|
|
|
|
|
|
function getRecipients(
|
|
|
|
sendStatusByConversationId: Record<string, boolean>
|
|
|
|
): Array<string> {
|
|
|
|
return Object.entries(sendStatusByConversationId)
|
|
|
|
.filter(([_, isSent]) => !isSent)
|
|
|
|
.map(([conversationId]) => {
|
|
|
|
const recipient = window.ConversationController.get(conversationId);
|
|
|
|
if (!recipient) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-01-20 19:42:55 +00:00
|
|
|
if (recipient.isUnregistered()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (recipient.isBlocked()) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-03-04 19:22:31 +00:00
|
|
|
return recipient.get('uuid');
|
|
|
|
})
|
|
|
|
.filter(isNotNil);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function updateMessageWithSuccessfulSends(
|
|
|
|
message: MessageModel,
|
|
|
|
result?: CallbackResultType | SendMessageProtoError
|
|
|
|
): Promise<void> {
|
|
|
|
if (!result) {
|
|
|
|
message.set({
|
|
|
|
deletedForEveryoneSendStatus: {},
|
|
|
|
deletedForEveryoneFailed: undefined,
|
|
|
|
});
|
|
|
|
await window.Signal.Data.saveMessage(message.attributes, {
|
|
|
|
ourUuid: window.textsecure.storage.user.getCheckedUuid().toString(),
|
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const deletedForEveryoneSendStatus = {
|
|
|
|
...message.get('deletedForEveryoneSendStatus'),
|
|
|
|
};
|
|
|
|
|
|
|
|
result.successfulIdentifiers?.forEach(identifier => {
|
|
|
|
const conversation = window.ConversationController.get(identifier);
|
|
|
|
if (!conversation) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
deletedForEveryoneSendStatus[conversation.id] = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
message.set({
|
|
|
|
deletedForEveryoneSendStatus,
|
|
|
|
deletedForEveryoneFailed: undefined,
|
|
|
|
});
|
|
|
|
await window.Signal.Data.saveMessage(message.attributes, {
|
|
|
|
ourUuid: window.textsecure.storage.user.getCheckedUuid().toString(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function updateMessageWithFailure(
|
|
|
|
message: MessageModel,
|
|
|
|
errors: ReadonlyArray<unknown>,
|
|
|
|
log: LoggerType
|
|
|
|
): Promise<void> {
|
|
|
|
log.error(
|
|
|
|
'updateMessageWithFailure: Setting this set of errors',
|
|
|
|
errors.map(Errors.toLogFormat)
|
|
|
|
);
|
|
|
|
|
|
|
|
message.set({ deletedForEveryoneFailed: true });
|
|
|
|
await window.Signal.Data.saveMessage(message.attributes, {
|
|
|
|
ourUuid: window.textsecure.storage.user.getCheckedUuid().toString(),
|
|
|
|
});
|
|
|
|
}
|