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';
|
2023-08-10 16:43:33 +00:00
|
|
|
import type { StoryDistributionIdString } from '../types/StoryDistributionId';
|
|
|
|
import type { ServiceIdString } from '../types/ServiceId';
|
2022-11-29 02:07:26 +00:00
|
|
|
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';
|
2023-10-04 00:12:57 +00:00
|
|
|
import { __DEPRECATED$getMessageById } from '../messages/getMessageById';
|
2022-11-29 02:07:26 +00:00
|
|
|
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})`;
|
2023-10-04 00:12:57 +00:00
|
|
|
const message = await __DEPRECATED$getMessageById(story.messageId);
|
2022-11-29 02:07:26 +00:00
|
|
|
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<
|
2023-08-10 16:43:33 +00:00
|
|
|
ServiceIdString,
|
2022-10-13 16:14:50 +00:00
|
|
|
{
|
2023-08-10 16:43:33 +00:00
|
|
|
distributionListIds: Set<StoryDistributionIdString>;
|
2022-10-13 16:14:50 +00:00
|
|
|
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
|
2023-08-16 20:54:39 +00:00
|
|
|
// a sync message. Put all affected destinationServiceIds early on so that if
|
2022-11-29 02:07:26 +00:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
const destinationServiceId =
|
|
|
|
window.ConversationController.get(recipientId)?.getServiceId();
|
2022-11-29 02:07:26 +00:00
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
if (!destinationServiceId) {
|
2022-11-29 02:07:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
newStoryRecipients.set(destinationServiceId, {
|
2022-11-29 02:07:26 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
const destinationServiceId =
|
|
|
|
window.ConversationController.get(conversationId)?.getServiceId();
|
2022-10-13 16:14:50 +00:00
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
if (!destinationServiceId) {
|
2022-10-13 16:14:50 +00:00
|
|
|
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).
|
2023-08-10 16:43:33 +00:00
|
|
|
let recipient = newStoryRecipients.get(destinationServiceId);
|
2022-11-29 02:07:26 +00:00
|
|
|
if (!recipient) {
|
|
|
|
const isAllowedToReply =
|
|
|
|
sendStateByConversationId[conversationId].isAllowedToReplyToStory;
|
|
|
|
recipient = {
|
|
|
|
distributionListIds: new Set(),
|
|
|
|
isAllowedToReply: isAllowedToReply !== false,
|
|
|
|
};
|
2022-10-13 16:14:50 +00:00
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
newStoryRecipients.set(destinationServiceId, recipient);
|
2022-11-29 02:07:26 +00:00
|
|
|
}
|
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
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
newStoryRecipients.forEach((recipientData, destinationServiceId) => {
|
2022-11-29 02:07:26 +00:00
|
|
|
newStoryMessageRecipients.push({
|
2023-08-10 16:43:33 +00:00
|
|
|
destinationServiceId,
|
2022-11-29 02:07:26 +00:00
|
|
|
distributionListIds: Array.from(recipientData.distributionListIds),
|
|
|
|
isAllowedToReply: recipientData.isAllowedToReply,
|
|
|
|
});
|
|
|
|
});
|
2022-10-13 16:14:50 +00:00
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
const destinationServiceId = ourConversation.getCheckedServiceId(
|
|
|
|
'deleteStoryForEveryone'
|
|
|
|
);
|
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,
|
2023-08-10 16:43:33 +00:00
|
|
|
ourAci: window.textsecure.storage.user.getCheckedAci(),
|
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(
|
|
|
|
{
|
2023-08-10 16:43:33 +00:00
|
|
|
destinationServiceId,
|
2022-10-13 16:14:50 +00:00
|
|
|
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
|
|
|
}
|