signal-desktop/ts/util/findStoryMessage.ts

97 lines
2.6 KiB
TypeScript
Raw Normal View History

2022-03-04 21:14:52 +00:00
// Copyright 2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { MessageAttributesType } from '../model-types.d';
import type { MessageModel } from '../models/messages';
import type { SignalService as Proto } from '../protobuf';
import type { AciString } from '../types/ServiceId';
2022-03-04 21:14:52 +00:00
import * as log from '../logging/log';
2023-09-14 17:04:48 +00:00
import { normalizeAci } from './normalizeAci';
import { filter } from './iterables';
2022-03-04 21:14:52 +00:00
import { getContactId } from '../messages/helpers';
import { getTimestampFromLong } from './timestampLongUtils';
export async function findStoryMessages(
2022-03-04 21:14:52 +00:00
conversationId: string,
storyContext?: Proto.DataMessage.IStoryContext
): Promise<Array<MessageModel>> {
2022-03-04 21:14:52 +00:00
if (!storyContext) {
return [];
2022-03-04 21:14:52 +00:00
}
const { authorAci: rawAuthorAci, sentTimestamp } = storyContext;
2022-03-04 21:14:52 +00:00
if (!rawAuthorAci || !sentTimestamp) {
return [];
2022-03-04 21:14:52 +00:00
}
const authorAci = normalizeAci(rawAuthorAci, 'findStoryMessage');
2022-03-04 21:14:52 +00:00
const sentAt = getTimestampFromLong(sentTimestamp);
2022-07-08 20:54:27 +00:00
const ourConversationId =
window.ConversationController.getOurConversationIdOrThrow();
2022-03-04 21:14:52 +00:00
const inMemoryMessages =
window.MessageCache.__DEPRECATED$filterBySentAt(sentAt);
const matchingMessages = [
...filter(inMemoryMessages, item =>
isStoryAMatch(
item.attributes,
conversationId,
ourConversationId,
authorAci,
sentAt
)
),
];
2022-03-04 21:14:52 +00:00
if (matchingMessages.length > 0) {
return matchingMessages;
2022-03-04 21:14:52 +00:00
}
log.info('findStoryMessages: db lookup needed', sentAt);
2022-03-04 21:14:52 +00:00
const messages = await window.Signal.Data.getMessagesBySentAt(sentAt);
const found = messages.filter(item =>
isStoryAMatch(item, conversationId, ourConversationId, authorAci, sentAt)
2022-03-04 21:14:52 +00:00
);
if (found.length === 0) {
log.info('findStoryMessages: message not found', sentAt);
return [];
2022-03-04 21:14:52 +00:00
}
const result = found.map(attributes =>
window.MessageCache.__DEPRECATED$register(
attributes.id,
attributes,
'findStoryMessages'
)
);
return result;
2022-03-04 21:14:52 +00:00
}
2022-07-08 20:54:27 +00:00
function isStoryAMatch(
2022-03-04 21:14:52 +00:00
message: MessageAttributesType | null | undefined,
conversationId: string,
2022-07-08 20:54:27 +00:00
ourConversationId: string,
authorAci: AciString,
2022-03-04 21:14:52 +00:00
sentTimestamp: number
): message is MessageAttributesType {
if (!message) {
return false;
}
const authorConversation = window.ConversationController.lookupOrCreate({
2022-03-04 21:14:52 +00:00
e164: undefined,
2023-08-16 20:54:39 +00:00
serviceId: authorAci,
reason: 'isStoryAMatch',
2022-03-04 21:14:52 +00:00
});
return (
message.sent_at === sentTimestamp &&
getContactId(message) === authorConversation?.id &&
2022-07-08 20:54:27 +00:00
(message.conversationId === conversationId ||
message.conversationId === ourConversationId)
2022-03-04 21:14:52 +00:00
);
}