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,
|
2025-09-16 17:39:03 -07:00
|
|
|
} 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');
|
|
|
|
|
2023-05-25 14:12:33 +02:00
|
|
|
export async function findStoryMessages(
|
2022-03-04 16:14:52 -05:00
|
|
|
conversationId: string,
|
2025-06-25 10:30:40 -07:00
|
|
|
storyContext?: ProcessedStoryContext
|
2024-09-04 15:59:39 -07:00
|
|
|
): Promise<Array<MessageAttributesType>> {
|
2022-03-04 16:14:52 -05:00
|
|
|
if (!storyContext) {
|
2023-05-25 14:12:33 +02:00
|
|
|
return [];
|
2022-03-04 16:14:52 -05:00
|
|
|
}
|
|
|
|
|
2025-06-25 10:30:40 -07:00
|
|
|
const { authorAci, sentTimestamp: sentAt } = storyContext;
|
2022-03-04 16:14:52 -05:00
|
|
|
|
2025-06-25 10:30:40 -07:00
|
|
|
if (!sentAt) {
|
2023-05-25 14:12:33 +02:00
|
|
|
return [];
|
2022-03-04 16:14:52 -05:00
|
|
|
}
|
|
|
|
|
2025-06-25 10:30:40 -07:00
|
|
|
if (authorAci == null) {
|
|
|
|
return [];
|
|
|
|
}
|
2023-08-10 18:43:33 +02:00
|
|
|
|
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);
|
2023-05-25 14:12:33 +02:00
|
|
|
const found = messages.filter(item =>
|
2023-08-10 18:43:33 +02:00
|
|
|
isStoryAMatch(item, conversationId, ourConversationId, authorAci, sentAt)
|
2022-03-04 16:14:52 -05:00
|
|
|
);
|
|
|
|
|
2023-05-31 16:07:43 -04:00
|
|
|
if (found.length === 0) {
|
2023-05-25 14:12:33 +02:00
|
|
|
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(
|
2024-07-24 13:14:11 -07:00
|
|
|
message: ReadonlyMessageAttributesType | null | undefined,
|
2022-03-04 16:14:52 -05:00
|
|
|
conversationId: string,
|
2022-07-08 16:54:27 -04:00
|
|
|
ourConversationId: string,
|
2023-08-10 18:43:33 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-08-09 14:39:00 -07:00
|
|
|
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,
|
2022-12-02 18:05:27 -07:00
|
|
|
reason: 'isStoryAMatch',
|
2022-03-04 16:14:52 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
|
|
|
message.sent_at === sentTimestamp &&
|
2024-04-29 17:20:20 -04:00
|
|
|
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
|
|
|
);
|
|
|
|
}
|