signal-desktop/ts/util/findStoryMessage.ts

74 lines
1.8 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.ts';
import { type AciString } from '../types/ServiceId.js';
import type { ProcessedStoryContext } from '../textsecure/Types.d.ts';
import { DataReader } from '../sql/Client.js';
import { createLogger } from '../logging/log.js';
import { getAuthorId } from '../messages/helpers.js';
2022-03-04 16:14:52 -05:00
2025-06-16 11:59:31 -07:00
const log = createLogger('findStoryMessage');
export async function findStoryMessages(
2022-03-04 16:14:52 -05:00
conversationId: string,
storyContext?: ProcessedStoryContext
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, sentTimestamp: sentAt } = storyContext;
2022-03-04 16:14:52 -05:00
if (!sentAt) {
return [];
2022-03-04 16:14:52 -05:00
}
if (authorAci == null) {
return [];
}
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
);
}