2022-10-13 16:14:50 +00:00
|
|
|
// Copyright 2022 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { noop } from 'lodash';
|
2022-11-29 02:07:26 +00:00
|
|
|
|
|
|
|
import type { ConversationQueueJobData } from '../jobs/conversationJobQueue';
|
2022-10-13 16:14:50 +00:00
|
|
|
import type { StoryDataType } from '../state/ducks/stories';
|
2022-11-29 02:07:26 +00:00
|
|
|
import * as Errors from '../types/errors';
|
|
|
|
import type { StoryMessageRecipientsType } from '../types/Stories';
|
|
|
|
import * as log from '../logging/log';
|
2022-10-13 16:14:50 +00:00
|
|
|
import { DAY } from './durations';
|
|
|
|
import { StoryRecipientUpdateEvent } from '../textsecure/messageReceiverEvents';
|
2022-11-29 02:07:26 +00:00
|
|
|
import {
|
|
|
|
conversationJobQueue,
|
|
|
|
conversationQueueJobEnum,
|
|
|
|
} from '../jobs/conversationJobQueue';
|
2022-10-13 16:14:50 +00:00
|
|
|
import { onStoryRecipientUpdate } from './onStoryRecipientUpdate';
|
|
|
|
import { sendDeleteForEveryoneMessage } from './sendDeleteForEveryoneMessage';
|
2022-11-29 02:07:26 +00:00
|
|
|
import { isGroupV2 } from './whatTypeOfConversation';
|
|
|
|
import { getMessageById } from '../messages/getMessageById';
|
|
|
|
import { strictAssert } from './assert';
|
|
|
|
import { repeat, zipObject } from './iterables';
|
|
|
|
import { isOlderThan } from './timestamp';
|
2022-10-13 16:14:50 +00:00
|
|
|
|
|
|
|
export async function deleteStoryForEveryone(
|
2022-11-10 04:24:42 +00:00
|
|
|
stories: ReadonlyArray<StoryDataType>,
|
2022-10-13 16:14:50 +00:00
|
|
|
story: StoryDataType
|
|
|
|
): Promise<void> {
|
|
|
|
if (!story.sendStateByConversationId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-29 02:07:26 +00:00
|
|
|
// Group stories are deleted as regular messages.
|
|
|
|
const sourceConversation = window.ConversationController.get(
|
|
|
|
story.conversationId
|
|
|
|
);
|
|
|
|
if (sourceConversation && isGroupV2(sourceConversation.attributes)) {
|
2022-12-21 18:41:48 +00:00
|
|
|
void sendDeleteForEveryoneMessage(sourceConversation.attributes, {
|
2022-11-29 02:07:26 +00:00
|
|
|
deleteForEveryoneDuration: DAY,
|
|
|
|
id: story.messageId,
|
|
|
|
timestamp: story.timestamp,
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const logId = `deleteStoryForEveryone(${story.messageId})`;
|
|
|
|
const message = await getMessageById(story.messageId);
|
|
|
|
if (!message) {
|
|
|
|
throw new Error('Story not found');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isOlderThan(story.timestamp, DAY)) {
|
|
|
|
throw new Error('Cannot send DOE for a story older than one day');
|
|
|
|
}
|
|
|
|
|
2022-10-13 16:14:50 +00:00
|
|
|
const conversationIds = new Set(Object.keys(story.sendStateByConversationId));
|
2022-11-29 02:07:26 +00:00
|
|
|
const newStoryRecipients = new Map<
|
2022-10-13 16:14:50 +00:00
|
|
|
string,
|
|
|
|
{
|
|
|
|
distributionListIds: Set<string>;
|
|
|
|
isAllowedToReply: boolean;
|
|
|
|
}
|
|
|
|
>();
|
|
|
|
|
|
|
|
const ourConversation =
|
|
|
|
window.ConversationController.getOurConversationOrThrow();
|
|
|
|
|
|
|
|
// Remove ourselves from the DOE.
|
|
|
|
conversationIds.delete(ourConversation.id);
|
|
|
|
|
2022-11-29 02:07:26 +00:00
|
|
|
// `updatedStoryRecipients` is used to build `storyMessageRecipients` for
|
|
|
|
// a sync message. Put all affected destinationUuids early on so that if
|
|
|
|
// there are no other distribution lists for them - we'd still include an
|
|
|
|
// empty list.
|
|
|
|
Object.entries(story.sendStateByConversationId).forEach(
|
|
|
|
([recipientId, sendState]) => {
|
|
|
|
if (recipientId === ourConversation.id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const destinationUuid =
|
|
|
|
window.ConversationController.get(recipientId)?.get('uuid');
|
|
|
|
|
|
|
|
if (!destinationUuid) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
newStoryRecipients.set(destinationUuid, {
|
|
|
|
distributionListIds: new Set(),
|
|
|
|
isAllowedToReply: sendState.isAllowedToReplyToStory !== false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2022-10-13 16:14:50 +00:00
|
|
|
// Find stories that were sent to other distribution lists so that we don't
|
|
|
|
// send a DOE request to the members of those lists.
|
|
|
|
stories.forEach(item => {
|
|
|
|
const { sendStateByConversationId } = item;
|
|
|
|
// We only want matching timestamp stories which are stories that were
|
|
|
|
// sent to multi distribution lists.
|
|
|
|
// We don't want the story we just passed in.
|
|
|
|
// Don't need to check for stories that have already been deleted.
|
|
|
|
// And only for sent stories, not incoming.
|
|
|
|
if (
|
|
|
|
item.timestamp !== story.timestamp ||
|
|
|
|
item.messageId === story.messageId ||
|
|
|
|
item.deletedForEveryone ||
|
|
|
|
!sendStateByConversationId
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Object.keys(sendStateByConversationId).forEach(conversationId => {
|
|
|
|
if (conversationId === ourConversation.id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const destinationUuid =
|
|
|
|
window.ConversationController.get(conversationId)?.get('uuid');
|
|
|
|
|
|
|
|
if (!destinationUuid) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove this conversationId so we don't send the DOE to those that
|
|
|
|
// still have access.
|
|
|
|
conversationIds.delete(conversationId);
|
|
|
|
|
2022-11-29 02:07:26 +00:00
|
|
|
// Build remaining distribution list ids that the user still has
|
|
|
|
// access to.
|
|
|
|
if (item.storyDistributionListId === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
2022-10-13 16:14:50 +00:00
|
|
|
|
2022-11-29 02:07:26 +00:00
|
|
|
// Build complete list of new story recipients (not counting ones that
|
|
|
|
// are in the deleted story).
|
|
|
|
let recipient = newStoryRecipients.get(destinationUuid);
|
|
|
|
if (!recipient) {
|
|
|
|
const isAllowedToReply =
|
|
|
|
sendStateByConversationId[conversationId].isAllowedToReplyToStory;
|
|
|
|
recipient = {
|
|
|
|
distributionListIds: new Set(),
|
|
|
|
isAllowedToReply: isAllowedToReply !== false,
|
|
|
|
};
|
2022-10-13 16:14:50 +00:00
|
|
|
|
2022-11-29 02:07:26 +00:00
|
|
|
newStoryRecipients.set(destinationUuid, recipient);
|
|
|
|
}
|
2022-10-13 16:14:50 +00:00
|
|
|
|
2022-11-29 02:07:26 +00:00
|
|
|
recipient.distributionListIds.add(item.storyDistributionListId);
|
2022-10-13 16:14:50 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-11-29 02:07:26 +00:00
|
|
|
// Include the sync message with the updated storyMessageRecipients list
|
|
|
|
const sender = window.textsecure.messaging;
|
|
|
|
strictAssert(sender, 'messaging has to be initialized');
|
2022-10-13 16:14:50 +00:00
|
|
|
|
2022-11-29 02:07:26 +00:00
|
|
|
const newStoryMessageRecipients: StoryMessageRecipientsType = [];
|
2022-10-13 16:14:50 +00:00
|
|
|
|
2022-11-29 02:07:26 +00:00
|
|
|
newStoryRecipients.forEach((recipientData, destinationUuid) => {
|
|
|
|
newStoryMessageRecipients.push({
|
|
|
|
destinationUuid,
|
|
|
|
distributionListIds: Array.from(recipientData.distributionListIds),
|
|
|
|
isAllowedToReply: recipientData.isAllowedToReply,
|
|
|
|
});
|
|
|
|
});
|
2022-10-13 16:14:50 +00:00
|
|
|
|
2022-11-29 02:07:26 +00:00
|
|
|
const destinationUuid = ourConversation
|
|
|
|
.getCheckedUuid('deleteStoryForEveryone')
|
|
|
|
.toString();
|
2022-10-13 16:14:50 +00:00
|
|
|
|
2022-11-29 02:07:26 +00:00
|
|
|
log.info(`${logId}: sending DOE to ${conversationIds.size} conversations`);
|
2022-10-13 16:14:50 +00:00
|
|
|
|
2022-11-29 02:07:26 +00:00
|
|
|
message.set({
|
|
|
|
deletedForEveryoneSendStatus: zipObject(conversationIds, repeat(false)),
|
|
|
|
});
|
2022-10-13 16:14:50 +00:00
|
|
|
|
2022-11-29 02:07:26 +00:00
|
|
|
// Send the DOE
|
2023-01-01 11:41:40 +00:00
|
|
|
log.info(`${logId}: enqueuing DeleteStoryForEveryone`);
|
2022-11-29 02:07:26 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
const jobData: ConversationQueueJobData = {
|
|
|
|
type: conversationQueueJobEnum.enum.DeleteStoryForEveryone,
|
|
|
|
conversationId: ourConversation.id,
|
|
|
|
storyId: story.messageId,
|
|
|
|
targetTimestamp: story.timestamp,
|
|
|
|
updatedStoryRecipients: newStoryMessageRecipients,
|
|
|
|
};
|
|
|
|
await conversationJobQueue.add(jobData, async jobToInsert => {
|
|
|
|
log.info(`${logId}: Deleting message with job ${jobToInsert.id}`);
|
|
|
|
|
|
|
|
await window.Signal.Data.saveMessage(message.attributes, {
|
|
|
|
jobToInsert,
|
|
|
|
ourUuid: window.textsecure.storage.user.getCheckedUuid().toString(),
|
2022-10-13 16:14:50 +00:00
|
|
|
});
|
|
|
|
});
|
2022-11-29 02:07:26 +00:00
|
|
|
} catch (error) {
|
|
|
|
log.error(
|
|
|
|
`${logId}: Failed to queue delete for everyone`,
|
|
|
|
Errors.toLogFormat(error)
|
|
|
|
);
|
|
|
|
throw error;
|
|
|
|
}
|
2022-10-13 16:14:50 +00:00
|
|
|
|
2022-11-29 02:07:26 +00:00
|
|
|
log.info(`${logId}: emulating sync message event`);
|
2022-10-13 16:14:50 +00:00
|
|
|
|
2022-11-29 02:07:26 +00:00
|
|
|
// Emulate message for Desktop (this will call deleteForEveryone())
|
|
|
|
const ev = new StoryRecipientUpdateEvent(
|
|
|
|
{
|
2022-10-13 16:14:50 +00:00
|
|
|
destinationUuid,
|
|
|
|
timestamp: story.timestamp,
|
2022-11-29 02:07:26 +00:00
|
|
|
storyMessageRecipients: newStoryMessageRecipients,
|
|
|
|
},
|
|
|
|
noop
|
|
|
|
);
|
2022-12-21 18:41:48 +00:00
|
|
|
void onStoryRecipientUpdate(ev);
|
2022-10-13 16:14:50 +00:00
|
|
|
}
|