2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
2021-08-31 20:58:39 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import * as log from '../logging/log';
|
2024-07-22 18:16:33 +00:00
|
|
|
import { DataReader } from '../sql/Client';
|
2021-08-31 20:58:39 +00:00
|
|
|
import type { MessageModel } from '../models/messages';
|
|
|
|
import type { MessageAttributesType } from '../model-types.d';
|
|
|
|
import * as Errors from '../types/errors';
|
|
|
|
|
|
|
|
export async function getMessagesById(
|
2022-01-25 01:39:18 +00:00
|
|
|
messageIds: Iterable<string>
|
2021-08-31 20:58:39 +00:00
|
|
|
): Promise<Array<MessageModel>> {
|
|
|
|
const messagesFromMemory: Array<MessageModel> = [];
|
|
|
|
const messageIdsToLookUpInDatabase: Array<string> = [];
|
|
|
|
|
2022-01-25 01:39:18 +00:00
|
|
|
for (const messageId of messageIds) {
|
2023-10-04 00:12:57 +00:00
|
|
|
const message = window.MessageCache.__DEPRECATED$getById(messageId);
|
2021-08-31 20:58:39 +00:00
|
|
|
if (message) {
|
|
|
|
messagesFromMemory.push(message);
|
|
|
|
} else {
|
|
|
|
messageIdsToLookUpInDatabase.push(messageId);
|
|
|
|
}
|
2022-01-25 01:39:18 +00:00
|
|
|
}
|
2021-08-31 20:58:39 +00:00
|
|
|
|
|
|
|
let rawMessagesFromDatabase: Array<MessageAttributesType>;
|
|
|
|
try {
|
2024-07-22 18:16:33 +00:00
|
|
|
rawMessagesFromDatabase = await DataReader.getMessagesById(
|
2021-08-31 20:58:39 +00:00
|
|
|
messageIdsToLookUpInDatabase
|
|
|
|
);
|
|
|
|
} catch (err: unknown) {
|
|
|
|
log.error(
|
|
|
|
`failed to load ${
|
|
|
|
messageIdsToLookUpInDatabase.length
|
|
|
|
} message(s) from database. ${Errors.toLogFormat(err)}`
|
|
|
|
);
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
const messagesFromDatabase = rawMessagesFromDatabase.map(rawMessage => {
|
|
|
|
// We use `window.Whisper.Message` instead of `MessageModel` here to avoid a circular
|
|
|
|
// import.
|
|
|
|
const message = new window.Whisper.Message(rawMessage);
|
2023-10-04 00:12:57 +00:00
|
|
|
return window.MessageCache.__DEPRECATED$register(
|
|
|
|
message.id,
|
|
|
|
message,
|
|
|
|
'getMessagesById'
|
|
|
|
);
|
2021-08-31 20:58:39 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return [...messagesFromMemory, ...messagesFromDatabase];
|
|
|
|
}
|