signal-desktop/ts/util/findStoryMessage.ts

73 lines
2 KiB
TypeScript
Raw Normal View History

2022-03-04 16:14:52 -05:00
// Copyright 2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
2024-09-04 15:59:39 -07:00
import type {
ReadonlyMessageAttributesType,
MessageAttributesType,
} from '../model-types.d';
2022-03-04 16:14:52 -05:00
import type { SignalService as Proto } from '../protobuf';
import type { AciString } from '../types/ServiceId';
2024-07-22 11:16:33 -07:00
import { DataReader } from '../sql/Client';
2022-03-04 16:14:52 -05:00
import * as log from '../logging/log';
2023-09-14 13:04:48 -04:00
import { normalizeAci } from './normalizeAci';
import { getAuthorId } from '../messages/helpers';
2022-03-04 16:14:52 -05:00
import { getTimestampFromLong } from './timestampLongUtils';
export async function findStoryMessages(
2022-03-04 16:14:52 -05:00
conversationId: string,
storyContext?: Proto.DataMessage.IStoryContext
2024-09-04 15:59:39 -07:00
): Promise<Array<MessageAttributesType>> {
2022-03-04 16:14:52 -05:00
if (!storyContext) {
return [];
2022-03-04 16:14:52 -05:00
}
const { authorAci: rawAuthorAci, sentTimestamp } = storyContext;
2022-03-04 16:14:52 -05:00
if (!rawAuthorAci || !sentTimestamp) {
return [];
2022-03-04 16:14:52 -05:00
}
const authorAci = normalizeAci(rawAuthorAci, 'findStoryMessage');
2022-03-04 16:14:52 -05:00
const sentAt = getTimestampFromLong(sentTimestamp);
2022-07-08 16:54:27 -04:00
const ourConversationId =
window.ConversationController.getOurConversationIdOrThrow();
2022-03-04 16:14:52 -05:00
2024-07-22 11:16:33 -07:00
const messages = await DataReader.getMessagesBySentAt(sentAt);
const found = messages.filter(item =>
isStoryAMatch(item, conversationId, ourConversationId, authorAci, sentAt)
2022-03-04 16:14:52 -05:00
);
if (found.length === 0) {
log.info('findStoryMessages: message not found', sentAt);
return [];
2022-03-04 16:14:52 -05:00
}
2024-09-04 15:59:39 -07:00
return found;
2022-03-04 16:14:52 -05:00
}
2022-07-08 16:54:27 -04:00
function isStoryAMatch(
message: ReadonlyMessageAttributesType | null | undefined,
2022-03-04 16:14:52 -05:00
conversationId: string,
2022-07-08 16:54:27 -04:00
ourConversationId: string,
authorAci: AciString,
2022-03-04 16:14:52 -05:00
sentTimestamp: number
2024-09-04 15:59:39 -07:00
): boolean {
2022-03-04 16:14:52 -05:00
if (!message) {
return false;
}
const authorConversation = window.ConversationController.lookupOrCreate({
2022-03-04 16:14:52 -05:00
e164: undefined,
2023-08-16 22:54:39 +02:00
serviceId: authorAci,
reason: 'isStoryAMatch',
2022-03-04 16:14:52 -05:00
});
return (
message.sent_at === sentTimestamp &&
getAuthorId(message) === authorConversation?.id &&
2022-07-08 16:54:27 -04:00
(message.conversationId === conversationId ||
message.conversationId === ourConversationId)
2022-03-04 16:14:52 -05:00
);
}