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 * as log from '../logging/log';
|
|
|
|
import { find } from './iterables';
|
|
|
|
import { getContactId } from '../messages/helpers';
|
|
|
|
import { getTimestampFromLong } from './timestampLongUtils';
|
|
|
|
|
|
|
|
export async function findStoryMessage(
|
|
|
|
conversationId: string,
|
|
|
|
storyContext?: Proto.DataMessage.IStoryContext
|
|
|
|
): Promise<MessageModel | undefined> {
|
|
|
|
if (!storyContext) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { authorUuid, sentTimestamp } = storyContext;
|
|
|
|
|
|
|
|
if (!authorUuid || !sentTimestamp) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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.MessageController.filterBySentAt(sentAt);
|
|
|
|
const matchingMessage = find(inMemoryMessages, item =>
|
2022-07-08 20:54:27 +00:00
|
|
|
isStoryAMatch(
|
|
|
|
item.attributes,
|
|
|
|
conversationId,
|
|
|
|
ourConversationId,
|
|
|
|
authorUuid,
|
|
|
|
sentAt
|
|
|
|
)
|
2022-03-04 21:14:52 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if (matchingMessage) {
|
|
|
|
return matchingMessage;
|
|
|
|
}
|
|
|
|
|
|
|
|
log.info('findStoryMessage: db lookup needed', sentAt);
|
|
|
|
const messages = await window.Signal.Data.getMessagesBySentAt(sentAt);
|
|
|
|
const found = messages.find(item =>
|
2022-07-08 20:54:27 +00:00
|
|
|
isStoryAMatch(item, conversationId, ourConversationId, authorUuid, sentAt)
|
2022-03-04 21:14:52 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if (!found) {
|
|
|
|
log.info('findStoryMessage: message not found', sentAt);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const message = window.MessageController.register(found.id, found);
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
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,
|
2022-03-04 21:14:52 +00:00
|
|
|
authorUuid: string,
|
|
|
|
sentTimestamp: number
|
|
|
|
): message is MessageAttributesType {
|
|
|
|
if (!message) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-08-09 21:39:00 +00:00
|
|
|
const authorConversation = window.ConversationController.lookupOrCreate({
|
2022-03-04 21:14:52 +00:00
|
|
|
e164: undefined,
|
|
|
|
uuid: authorUuid,
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
|
|
|
message.sent_at === sentTimestamp &&
|
2022-08-09 21:39:00 +00:00
|
|
|
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
|
|
|
);
|
|
|
|
}
|