2021-09-02 12:44:54 -05:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import * as log from '../logging/log';
|
2025-01-10 08:18:32 +10:00
|
|
|
import * as Errors from '../types/errors';
|
2024-07-22 11:16:33 -07:00
|
|
|
import { DataReader } from '../sql/Client';
|
2025-01-10 08:18:32 +10:00
|
|
|
import { MessageModel } from '../models/messages';
|
2021-12-10 14:51:54 -08:00
|
|
|
import type { MessageAttributesType } from '../model-types.d';
|
2021-09-02 12:44:54 -05:00
|
|
|
|
2025-01-10 08:18:32 +10:00
|
|
|
export async function getMessageById(
|
|
|
|
messageId: string
|
2021-09-02 12:44:54 -05:00
|
|
|
): Promise<MessageModel | undefined> {
|
2025-01-10 08:18:32 +10:00
|
|
|
const message = window.MessageCache.getById(messageId);
|
2021-09-02 12:44:54 -05:00
|
|
|
if (message) {
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
2021-12-10 14:51:54 -08:00
|
|
|
let found: MessageAttributesType | undefined;
|
2021-09-02 12:44:54 -05:00
|
|
|
try {
|
2024-07-22 11:16:33 -07:00
|
|
|
found = await DataReader.getMessageById(messageId);
|
2021-09-02 12:44:54 -05:00
|
|
|
} catch (err: unknown) {
|
|
|
|
log.error(
|
|
|
|
`failed to load message with id ${messageId} ` +
|
|
|
|
`due to error ${Errors.toLogFormat(err)}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-12-10 14:51:54 -08:00
|
|
|
if (!found) {
|
2021-09-02 12:44:54 -05:00
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2025-01-10 08:18:32 +10:00
|
|
|
return window.MessageCache.register(new MessageModel(found));
|
2021-09-02 12:44:54 -05:00
|
|
|
}
|