Use sender+timestamp to cache receipts and read syncs

This commit is contained in:
Scott Nonnenberg 2023-11-02 06:28:49 -07:00 committed by GitHub
parent 8f6fe60342
commit 0b08fc9e1f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 5 deletions

View file

@ -24,6 +24,7 @@ import { getSourceServiceId } from '../messages/helpers';
import { queueUpdateMessage } from '../util/messageBatcher';
import { getMessageSentTimestamp } from '../util/getMessageSentTimestamp';
import { getMessageIdForLogging } from '../util/idForLogging';
import { generateCacheKey } from './generateCacheKey';
const { deleteSentProtoRecipient } = dataInterface;
@ -78,7 +79,12 @@ const deleteSentProtoBatcher = createWaitBatcher({
});
function remove(receipt: MessageReceiptAttributesType): void {
receipts.delete(receipt.envelopeId);
receipts.delete(
generateCacheKey({
sender: receipt.sourceServiceId,
timestamp: receipt.messageSentAt,
})
);
receipt.removeFromMessageReceiverCache();
}
@ -341,7 +347,13 @@ async function updateMessageSendState(
export async function onReceipt(
receipt: MessageReceiptAttributesType
): Promise<void> {
receipts.set(receipt.envelopeId, receipt);
receipts.set(
generateCacheKey({
sender: receipt.sourceServiceId,
timestamp: receipt.messageSentAt,
}),
receipt
);
const { messageSentAt, sourceConversationId, sourceServiceId, type } =
receipt;

View file

@ -14,6 +14,7 @@ import { isMessageUnread } from '../util/isMessageUnread';
import { notificationService } from '../services/notifications';
import { queueUpdateMessage } from '../util/messageBatcher';
import { strictAssert } from '../util/assert';
import { generateCacheKey } from './generateCacheKey';
export type ReadSyncAttributesType = {
envelopeId: string;
@ -25,10 +26,15 @@ export type ReadSyncAttributesType = {
timestamp: number;
};
const readSyncs = new Map<number, ReadSyncAttributesType>();
const readSyncs = new Map<string, ReadSyncAttributesType>();
function remove(sync: ReadSyncAttributesType): void {
readSyncs.delete(sync.timestamp);
readSyncs.delete(
generateCacheKey({
sender: sync.senderId,
timestamp: sync.timestamp,
})
);
sync.removeFromMessageReceiverCache();
}
@ -99,7 +105,13 @@ export function forMessage(
}
export async function onSync(sync: ReadSyncAttributesType): Promise<void> {
readSyncs.set(sync.timestamp, sync);
readSyncs.set(
generateCacheKey({
sender: sync.senderId,
timestamp: sync.timestamp,
}),
sync
);
const logId = `ReadSyncs.onSync(timestamp=${sync.timestamp})`;

View file

@ -0,0 +1,16 @@
// Copyright 2016 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
// This function is necessary because the only thing we can guarantee will be unique is
// three pieces of data: sender, deviceId, and timestamp.
// Because we don't care which device interacted with our message, we collapse this down
// to: sender + timestamp.
export function generateCacheKey({
sender,
timestamp,
}: {
sender: string;
timestamp: number;
}): string {
return `cacheKey-${sender}-${timestamp}`;
}