signal-desktop/ts/util/onStoryRecipientUpdate.ts
2022-11-28 18:07:26 -08:00

184 lines
6 KiB
TypeScript

// Copyright 2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { isEqual } from 'lodash';
import type { DeleteAttributesType } from '../messageModifiers/Deletes';
import type { StoryRecipientUpdateEvent } from '../textsecure/messageReceiverEvents';
import * as log from '../logging/log';
import { Deletes } from '../messageModifiers/Deletes';
import { SendStatus } from '../messages/MessageSendState';
import { deleteForEveryone } from './deleteForEveryone';
import { getConversationIdForLogging } from './idForLogging';
import { isStory } from '../state/selectors/message';
import { normalizeUuid } from './normalizeUuid';
import { queueUpdateMessage } from './messageBatcher';
export async function onStoryRecipientUpdate(
event: StoryRecipientUpdateEvent
): Promise<void> {
const { data, confirm } = event;
const { destinationUuid, timestamp } = data;
const conversation = window.ConversationController.get(destinationUuid);
const logId = `onStoryRecipientUpdate(${destinationUuid}, ${timestamp})`;
if (!conversation) {
log.info(`${logId}: no conversation`);
return;
}
const targetConversation =
await window.ConversationController.getConversationForTargetMessage(
conversation.id,
timestamp
);
if (!targetConversation) {
log.info(`${logId}: no targetConversation`);
return;
}
targetConversation.queueJob(logId, async () => {
log.info(`${logId}: updating`);
// Build up some maps for fast/easy lookups
const isAllowedToReply = new Map<string, boolean>();
const distributionListIdToConversationIds = new Map<string, Set<string>>();
data.storyMessageRecipients.forEach(item => {
const convo = window.ConversationController.get(item.destinationUuid);
if (!convo || !item.distributionListIds) {
return;
}
for (const rawUuid of item.distributionListIds) {
const uuid = normalizeUuid(rawUuid, `${logId}.distributionListId`);
const existing = distributionListIdToConversationIds.get(uuid);
if (existing === undefined) {
distributionListIdToConversationIds.set(uuid, new Set([convo.id]));
} else {
existing.add(convo.id);
}
}
isAllowedToReply.set(convo.id, item.isAllowedToReply !== false);
});
const ourConversationId =
window.ConversationController.getOurConversationIdOrThrow();
const now = Date.now();
const messages = await window.Signal.Data.getMessagesBySentAt(timestamp);
// Now we figure out who needs to be added and who needs to removed
const handledMessages = messages.filter(item => {
if (!isStory(item)) {
return false;
}
const { sendStateByConversationId, storyDistributionListId } = item;
if (!sendStateByConversationId || !storyDistributionListId) {
return false;
}
const newConversationIds =
distributionListIdToConversationIds.get(storyDistributionListId) ??
new Set();
const nextSendStateByConversationId = {
...sendStateByConversationId,
};
// Find conversation ids present in the local send state, but missing
// in the remote state, and remove them from the local state.
for (const oldId of Object.keys(sendStateByConversationId)) {
if (!newConversationIds.has(oldId)) {
const recipient = window.ConversationController.get(oldId);
const recipientLogId = recipient
? getConversationIdForLogging(recipient.attributes)
: oldId;
log.info(`${logId}: removing`, {
recipient: recipientLogId,
messageId: item.id,
storyDistributionListId,
});
delete nextSendStateByConversationId[oldId];
}
}
// Find conversation ids present in the remote send state, but missing in
// the local send state, and add them to the local state.
for (const newId of newConversationIds) {
if (sendStateByConversationId[newId] === undefined) {
const recipient = window.ConversationController.get(newId);
const recipientLogId = recipient
? getConversationIdForLogging(recipient.attributes)
: newId;
log.info(`${logId}: adding`, {
recipient: recipientLogId,
messageId: item.id,
storyDistributionListId,
});
nextSendStateByConversationId[newId] = {
isAllowedToReplyToStory: Boolean(isAllowedToReply.get(newId)),
status: SendStatus.Sent,
updatedAt: now,
};
}
}
if (isEqual(sendStateByConversationId, nextSendStateByConversationId)) {
log.info(`${logId}: sendStateByConversationId does not need update`, {
messageId: item.id,
});
return true;
}
const message = window.MessageController.register(item.id, item);
const sendStateConversationIds = new Set(
Object.keys(nextSendStateByConversationId)
);
if (
sendStateConversationIds.size === 0 ||
(sendStateConversationIds.size === 1 &&
sendStateConversationIds.has(ourConversationId))
) {
log.info(`${logId} DOE`, {
messageId: item.id,
storyDistributionListId,
});
const delAttributes: DeleteAttributesType = {
fromId: ourConversationId,
serverTimestamp: Number(item.serverTimestamp),
targetSentTimestamp: item.timestamp,
};
const doe = Deletes.getSingleton().add(delAttributes);
// There are no longer any remaining members for this message so lets
// run it through deleteForEveryone which marks the message as
// deletedForEveryone locally.
deleteForEveryone(message, doe);
} else {
message.set({
sendStateByConversationId: nextSendStateByConversationId,
});
queueUpdateMessage(message.attributes);
}
return true;
});
if (handledMessages.length) {
window.Whisper.events.trigger('incrementProgress');
confirm();
}
});
}