2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2020 Signal Messenger, LLC
|
2020-10-30 20:34:04 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-10-07 18:16:51 +00:00
|
|
|
import { ipcRenderer as ipc } from 'electron';
|
2018-09-21 01:47:19 +00:00
|
|
|
|
2024-07-22 18:16:33 +00:00
|
|
|
import { groupBy, isTypedArray, last, map, omit } from 'lodash';
|
2020-04-01 18:59:11 +00:00
|
|
|
|
2021-12-10 22:51:54 +00:00
|
|
|
import { deleteExternalFiles } from '../types/Conversation';
|
2024-06-17 19:24:39 +00:00
|
|
|
import { update as updateExpiringMessagesService } from '../services/expiringMessagesDeletion';
|
2022-05-31 23:53:14 +00:00
|
|
|
import { tapToViewMessagesDeletionService } from '../services/tapToViewMessagesDeletionService';
|
2021-09-24 00:49:05 +00:00
|
|
|
import * as Bytes from '../Bytes';
|
2020-04-01 18:59:11 +00:00
|
|
|
import { createBatcher } from '../util/batcher';
|
2023-05-04 17:59:02 +00:00
|
|
|
import { assertDev, softAssert } from '../util/assert';
|
2022-07-28 16:35:29 +00:00
|
|
|
import { mapObjectWithSpec } from '../util/mapObjectWithSpec';
|
|
|
|
import type { ObjectMappingSpecType } from '../util/mapObjectWithSpec';
|
2021-02-04 19:54:03 +00:00
|
|
|
import { cleanDataForIpc } from './cleanDataForIpc';
|
2023-08-16 20:54:39 +00:00
|
|
|
import type { AciString, ServiceIdString } from '../types/ServiceId';
|
2021-09-27 18:22:46 +00:00
|
|
|
import createTaskWithTimeout from '../textsecure/TaskWithTimeout';
|
2021-09-17 18:27:53 +00:00
|
|
|
import * as log from '../logging/log';
|
2023-08-10 16:43:33 +00:00
|
|
|
import { isValidUuid } from '../util/isValidUuid';
|
2022-11-17 00:29:15 +00:00
|
|
|
import * as Errors from '../types/errors';
|
2020-04-01 18:59:11 +00:00
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { StoredJob } from '../jobs/types';
|
2021-08-31 20:58:39 +00:00
|
|
|
import { formatJobForInsert } from '../jobs/formatJobForInsert';
|
2024-06-17 19:24:39 +00:00
|
|
|
import { cleanupMessages } from '../util/cleanup';
|
2024-07-22 18:16:33 +00:00
|
|
|
import { AccessType, ipcInvoke, doShutdown } from './channels';
|
2020-07-10 18:28:49 +00:00
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type {
|
2024-07-22 18:16:33 +00:00
|
|
|
ClientInterfaceWrap,
|
2023-03-04 03:03:15 +00:00
|
|
|
AdjacentMessagesByConversationOptionsType,
|
2022-07-28 16:35:29 +00:00
|
|
|
AllItemsType,
|
2024-07-22 18:16:33 +00:00
|
|
|
ServerReadableDirectInterface,
|
|
|
|
ServerWritableDirectInterface,
|
|
|
|
ClientReadableInterface,
|
|
|
|
ClientWritableInterface,
|
2021-07-15 23:48:09 +00:00
|
|
|
ClientSearchResultMessageType,
|
2020-04-01 18:59:11 +00:00
|
|
|
ConversationType,
|
2022-07-28 16:35:29 +00:00
|
|
|
GetConversationRangeCenteredOnMessageResultType,
|
2023-07-21 22:10:32 +00:00
|
|
|
GetRecentStoryRepliesOptionsType,
|
2021-09-10 02:38:11 +00:00
|
|
|
IdentityKeyIdType,
|
2021-12-10 22:51:54 +00:00
|
|
|
IdentityKeyType,
|
2022-07-28 16:35:29 +00:00
|
|
|
StoredIdentityKeyType,
|
2021-06-15 00:09:37 +00:00
|
|
|
ItemKeyType,
|
2020-04-01 18:59:11 +00:00
|
|
|
ItemType,
|
2022-07-28 16:35:29 +00:00
|
|
|
StoredItemType,
|
2020-04-01 18:59:11 +00:00
|
|
|
MessageType,
|
2021-07-12 23:51:45 +00:00
|
|
|
MessageTypeUnhydrated,
|
2021-09-10 02:38:11 +00:00
|
|
|
PreKeyIdType,
|
2021-12-10 22:51:54 +00:00
|
|
|
PreKeyType,
|
2022-07-28 16:35:29 +00:00
|
|
|
StoredPreKeyType,
|
2021-12-10 22:51:54 +00:00
|
|
|
ServerSearchResultMessageType,
|
2021-09-10 02:38:11 +00:00
|
|
|
SignedPreKeyIdType,
|
2021-12-10 22:51:54 +00:00
|
|
|
SignedPreKeyType,
|
2022-07-28 16:35:29 +00:00
|
|
|
StoredSignedPreKeyType,
|
2023-07-14 16:53:20 +00:00
|
|
|
KyberPreKeyType,
|
|
|
|
StoredKyberPreKeyType,
|
2024-07-22 18:16:33 +00:00
|
|
|
ClientOnlyReadableInterface,
|
|
|
|
ClientOnlyWritableInterface,
|
2020-04-01 18:59:11 +00:00
|
|
|
} from './Interface';
|
2022-07-18 20:01:43 +00:00
|
|
|
import { getMessageIdForLogging } from '../util/idForLogging';
|
2023-03-20 22:23:53 +00:00
|
|
|
import type { MessageAttributesType } from '../model-types';
|
2023-04-11 03:54:43 +00:00
|
|
|
import { incrementMessageCounter } from '../util/incrementMessageCounter';
|
2023-06-26 18:25:48 +00:00
|
|
|
import { generateSnippetAroundMention } from '../util/search';
|
2024-04-16 00:11:48 +00:00
|
|
|
import type { AttachmentDownloadJobType } from '../types/AttachmentDownload';
|
2024-06-17 19:24:39 +00:00
|
|
|
import type { SingleProtoJobQueue } from '../jobs/singleProtoJobQueue';
|
2018-07-27 01:13:56 +00:00
|
|
|
|
|
|
|
const ERASE_SQL_KEY = 'erase-sql-key';
|
|
|
|
const ERASE_ATTACHMENTS_KEY = 'erase-attachments';
|
2019-05-16 22:32:11 +00:00
|
|
|
const ERASE_STICKERS_KEY = 'erase-stickers';
|
2019-05-24 01:27:42 +00:00
|
|
|
const ERASE_TEMP_KEY = 'erase-temp';
|
2019-08-07 00:40:25 +00:00
|
|
|
const ERASE_DRAFTS_KEY = 'erase-drafts';
|
2018-08-08 17:00:33 +00:00
|
|
|
const CLEANUP_ORPHANED_ATTACHMENTS_KEY = 'cleanup-orphaned-attachments';
|
2020-02-21 23:40:04 +00:00
|
|
|
const ENSURE_FILE_PERMISSIONS = 'ensure-file-permissions';
|
2024-07-22 18:16:33 +00:00
|
|
|
const PAUSE_WRITE_ACCESS = 'pause-sql-writes';
|
|
|
|
const RESUME_WRITE_ACCESS = 'resume-sql-writes';
|
2018-07-27 01:13:56 +00:00
|
|
|
|
2024-07-22 18:16:33 +00:00
|
|
|
const clientOnlyReadable: ClientOnlyReadableInterface = {
|
2022-11-17 00:29:15 +00:00
|
|
|
getIdentityKeyById,
|
|
|
|
getAllIdentityKeys,
|
|
|
|
|
2023-07-14 16:53:20 +00:00
|
|
|
getKyberPreKeyById,
|
|
|
|
getAllKyberPreKeys,
|
|
|
|
|
2022-11-17 00:29:15 +00:00
|
|
|
getPreKeyById,
|
|
|
|
getAllPreKeys,
|
|
|
|
|
|
|
|
getSignedPreKeyById,
|
|
|
|
getAllSignedPreKeys,
|
|
|
|
|
|
|
|
getItemById,
|
|
|
|
getAllItems,
|
|
|
|
|
|
|
|
searchMessages,
|
|
|
|
|
2023-07-21 22:10:32 +00:00
|
|
|
getRecentStoryReplies,
|
2022-11-17 00:29:15 +00:00
|
|
|
getOlderMessagesByConversation,
|
|
|
|
getNewerMessagesByConversation,
|
2024-07-22 18:16:33 +00:00
|
|
|
getConversationRangeCenteredOnMessage,
|
|
|
|
};
|
|
|
|
|
|
|
|
const clientOnlyWritable: ClientOnlyWritableInterface = {
|
|
|
|
createOrUpdateIdentityKey,
|
|
|
|
bulkAddIdentityKeys,
|
|
|
|
|
|
|
|
createOrUpdateKyberPreKey,
|
|
|
|
bulkAddKyberPreKeys,
|
|
|
|
|
|
|
|
createOrUpdatePreKey,
|
|
|
|
bulkAddPreKeys,
|
|
|
|
|
|
|
|
createOrUpdateSignedPreKey,
|
|
|
|
bulkAddSignedPreKeys,
|
|
|
|
|
|
|
|
createOrUpdateItem,
|
|
|
|
|
|
|
|
updateConversation,
|
|
|
|
removeConversation,
|
|
|
|
|
|
|
|
removeMessage,
|
|
|
|
removeMessages,
|
2022-11-17 00:29:15 +00:00
|
|
|
|
|
|
|
// Client-side only
|
|
|
|
|
2023-08-17 23:35:41 +00:00
|
|
|
flushUpdateConversationBatcher,
|
|
|
|
|
2022-11-17 00:29:15 +00:00
|
|
|
shutdown,
|
2024-05-28 15:56:00 +00:00
|
|
|
removeMessagesInConversation,
|
2022-11-17 00:29:15 +00:00
|
|
|
|
|
|
|
removeOtherData,
|
|
|
|
cleanupOrphanedAttachments,
|
|
|
|
ensureFilePermissions,
|
|
|
|
};
|
|
|
|
|
2024-07-22 18:16:33 +00:00
|
|
|
type ClientOverridesType = ClientOnlyWritableInterface &
|
2023-05-04 17:59:02 +00:00
|
|
|
Pick<
|
2024-07-22 18:16:33 +00:00
|
|
|
ClientInterfaceWrap<ServerWritableDirectInterface>,
|
2023-05-04 17:59:02 +00:00
|
|
|
| 'saveAttachmentDownloadJob'
|
|
|
|
| 'saveMessage'
|
|
|
|
| 'saveMessages'
|
|
|
|
| 'updateConversations'
|
|
|
|
>;
|
|
|
|
|
2024-07-22 18:16:33 +00:00
|
|
|
const clientOnlyWritableOverrides: ClientOverridesType = {
|
|
|
|
...clientOnlyWritable,
|
2022-11-17 00:29:15 +00:00
|
|
|
saveAttachmentDownloadJob,
|
2023-05-04 17:59:02 +00:00
|
|
|
saveMessage,
|
|
|
|
saveMessages,
|
|
|
|
updateConversations,
|
2022-11-17 00:29:15 +00:00
|
|
|
};
|
|
|
|
|
2024-07-22 18:16:33 +00:00
|
|
|
type ReadableChannelInterface =
|
|
|
|
ClientInterfaceWrap<ServerReadableDirectInterface>;
|
|
|
|
|
|
|
|
const readableChannel: ReadableChannelInterface = new Proxy(
|
|
|
|
{} as ReadableChannelInterface,
|
2023-05-04 17:59:02 +00:00
|
|
|
{
|
2024-07-22 18:16:33 +00:00
|
|
|
get(_target, name) {
|
|
|
|
return async (...args: ReadonlyArray<unknown>) =>
|
|
|
|
ipcInvoke(AccessType.Read, String(name), args);
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
type WritableChannelInterface =
|
|
|
|
ClientInterfaceWrap<ServerWritableDirectInterface>;
|
|
|
|
|
|
|
|
const writableChannel: WritableChannelInterface = new Proxy(
|
|
|
|
{} as WritableChannelInterface,
|
|
|
|
{
|
|
|
|
get(_target, name) {
|
|
|
|
return async (...args: ReadonlyArray<unknown>) =>
|
|
|
|
ipcInvoke(AccessType.Write, String(name), args);
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
export const DataReader: ClientReadableInterface = new Proxy(
|
|
|
|
{
|
|
|
|
...clientOnlyReadable,
|
|
|
|
} as ClientReadableInterface,
|
2023-05-04 17:59:02 +00:00
|
|
|
{
|
|
|
|
get(target, name) {
|
|
|
|
return async (...args: ReadonlyArray<unknown>) => {
|
2024-07-22 18:16:33 +00:00
|
|
|
if (Reflect.has(target, name)) {
|
|
|
|
return Reflect.get(target, name)(...args);
|
2023-05-04 17:59:02 +00:00
|
|
|
}
|
|
|
|
|
2024-07-22 18:16:33 +00:00
|
|
|
return Reflect.get(readableChannel, name)(...args);
|
2023-05-04 17:59:02 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2024-07-22 18:16:33 +00:00
|
|
|
export const DataWriter: ClientWritableInterface = new Proxy(
|
|
|
|
{
|
|
|
|
...clientOnlyWritableOverrides,
|
|
|
|
} as ClientWritableInterface,
|
|
|
|
{
|
|
|
|
get(target, name) {
|
|
|
|
return async (...args: ReadonlyArray<unknown>) => {
|
|
|
|
if (Reflect.has(target, name)) {
|
|
|
|
return Reflect.get(target, name)(...args);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Reflect.get(writableChannel, name)(...args);
|
|
|
|
};
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
2022-11-17 00:29:15 +00:00
|
|
|
|
2021-02-04 19:54:03 +00:00
|
|
|
function _cleanData(
|
|
|
|
data: unknown
|
|
|
|
): ReturnType<typeof cleanDataForIpc>['cleaned'] {
|
|
|
|
const { cleaned, pathsChanged } = cleanDataForIpc(data);
|
2020-04-01 18:59:11 +00:00
|
|
|
|
2021-02-04 19:54:03 +00:00
|
|
|
if (pathsChanged.length) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2021-02-04 19:54:03 +00:00
|
|
|
`_cleanData cleaned the following paths: ${pathsChanged.join(', ')}`
|
|
|
|
);
|
2020-03-19 17:57:50 +00:00
|
|
|
}
|
|
|
|
|
2021-02-04 19:54:03 +00:00
|
|
|
return cleaned;
|
|
|
|
}
|
2020-03-19 17:57:50 +00:00
|
|
|
|
2022-07-18 20:01:43 +00:00
|
|
|
export function _cleanMessageData(data: MessageType): MessageType {
|
2022-07-28 16:35:29 +00:00
|
|
|
const result = { ...data };
|
2021-03-04 21:44:57 +00:00
|
|
|
// Ensure that all messages have the received_at set properly
|
|
|
|
if (!data.received_at) {
|
2022-09-15 19:17:15 +00:00
|
|
|
assertDev(false, 'received_at was not set on the message');
|
2023-04-11 03:54:43 +00:00
|
|
|
result.received_at = incrementMessageCounter();
|
2018-07-27 01:13:56 +00:00
|
|
|
}
|
2022-07-18 20:01:43 +00:00
|
|
|
if (data.attachments) {
|
|
|
|
const logId = getMessageIdForLogging(data);
|
2022-07-28 16:35:29 +00:00
|
|
|
result.attachments = data.attachments.map((attachment, index) => {
|
2022-07-18 20:01:43 +00:00
|
|
|
if (attachment.data && !isTypedArray(attachment.data)) {
|
|
|
|
log.warn(
|
|
|
|
`_cleanMessageData/${logId}: Attachment ${index} had non-array \`data\` field; deleting.`
|
|
|
|
);
|
|
|
|
return omit(attachment, ['data']);
|
|
|
|
}
|
|
|
|
|
2023-07-18 01:00:02 +00:00
|
|
|
if (attachment.screenshotData) {
|
|
|
|
assertDev(
|
|
|
|
false,
|
|
|
|
`_cleanMessageData/${logId}: Attachment ${index} had screenshotData field; deleting`
|
|
|
|
);
|
|
|
|
return omit(attachment, ['screenshotData']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (attachment.screenshot?.data) {
|
|
|
|
assertDev(
|
|
|
|
false,
|
|
|
|
`_cleanMessageData/${logId}: Attachment ${index} had screenshot.data field; deleting`
|
|
|
|
);
|
|
|
|
return omit(attachment, ['screenshot.data']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (attachment.thumbnail?.data) {
|
|
|
|
assertDev(
|
|
|
|
false,
|
|
|
|
`_cleanMessageData/${logId}: Attachment ${index} had thumbnail.data field; deleting`
|
|
|
|
);
|
|
|
|
return omit(attachment, ['thumbnail.data']);
|
|
|
|
}
|
|
|
|
|
2022-07-18 20:01:43 +00:00
|
|
|
return attachment;
|
|
|
|
});
|
|
|
|
}
|
2022-07-28 16:35:29 +00:00
|
|
|
return _cleanData(omit(result, ['dataMessage']));
|
2018-07-27 01:13:56 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 16:35:29 +00:00
|
|
|
function specToBytes<Input, Output>(
|
|
|
|
spec: ObjectMappingSpecType,
|
|
|
|
data: Input
|
|
|
|
): Output {
|
|
|
|
return mapObjectWithSpec<string, Uint8Array>(spec, data, x =>
|
|
|
|
Bytes.fromBase64(x)
|
|
|
|
);
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 16:35:29 +00:00
|
|
|
function specFromBytes<Input, Output>(
|
|
|
|
spec: ObjectMappingSpecType,
|
|
|
|
data: Input
|
|
|
|
): Output {
|
|
|
|
return mapObjectWithSpec<Uint8Array, string>(spec, data, x =>
|
|
|
|
Bytes.toBase64(x)
|
|
|
|
);
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Top-level calls
|
|
|
|
|
2022-07-28 16:35:29 +00:00
|
|
|
async function shutdown(): Promise<void> {
|
2022-04-11 17:53:57 +00:00
|
|
|
log.info('Client.shutdown');
|
|
|
|
|
2021-03-10 22:25:57 +00:00
|
|
|
// Stop accepting new SQL jobs, flush outstanding queue
|
2022-11-17 00:29:15 +00:00
|
|
|
await doShutdown();
|
2018-07-27 01:13:56 +00:00
|
|
|
}
|
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
// Identity Keys
|
|
|
|
|
2022-07-28 16:35:29 +00:00
|
|
|
const IDENTITY_KEY_SPEC = ['publicKey'];
|
|
|
|
async function createOrUpdateIdentityKey(data: IdentityKeyType): Promise<void> {
|
|
|
|
const updated: StoredIdentityKeyType = specFromBytes(IDENTITY_KEY_SPEC, data);
|
2024-07-22 18:16:33 +00:00
|
|
|
await writableChannel.createOrUpdateIdentityKey(updated);
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
2022-07-28 16:35:29 +00:00
|
|
|
async function getIdentityKeyById(
|
|
|
|
id: IdentityKeyIdType
|
|
|
|
): Promise<IdentityKeyType | undefined> {
|
2024-07-22 18:16:33 +00:00
|
|
|
const data = await readableChannel.getIdentityKeyById(id);
|
2020-04-01 18:59:11 +00:00
|
|
|
|
2022-07-28 16:35:29 +00:00
|
|
|
return specToBytes(IDENTITY_KEY_SPEC, data);
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
2022-07-28 16:35:29 +00:00
|
|
|
async function bulkAddIdentityKeys(
|
|
|
|
array: Array<IdentityKeyType>
|
|
|
|
): Promise<void> {
|
|
|
|
const updated: Array<StoredIdentityKeyType> = map(array, data =>
|
|
|
|
specFromBytes(IDENTITY_KEY_SPEC, data)
|
|
|
|
);
|
2024-07-22 18:16:33 +00:00
|
|
|
await writableChannel.bulkAddIdentityKeys(updated);
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
2022-07-28 16:35:29 +00:00
|
|
|
async function getAllIdentityKeys(): Promise<Array<IdentityKeyType>> {
|
2024-07-22 18:16:33 +00:00
|
|
|
const keys = await readableChannel.getAllIdentityKeys();
|
2020-04-01 18:59:11 +00:00
|
|
|
|
2022-07-28 16:35:29 +00:00
|
|
|
return keys.map(key => specToBytes(IDENTITY_KEY_SPEC, key));
|
2019-02-04 23:54:37 +00:00
|
|
|
}
|
2018-10-18 01:01:21 +00:00
|
|
|
|
2023-07-14 16:53:20 +00:00
|
|
|
// Kyber Pre Keys
|
|
|
|
|
|
|
|
const KYBER_PRE_KEY_SPEC = ['data'];
|
|
|
|
async function createOrUpdateKyberPreKey(data: KyberPreKeyType): Promise<void> {
|
|
|
|
const updated: StoredKyberPreKeyType = specFromBytes(
|
|
|
|
KYBER_PRE_KEY_SPEC,
|
|
|
|
data
|
|
|
|
);
|
2024-07-22 18:16:33 +00:00
|
|
|
await writableChannel.createOrUpdateKyberPreKey(updated);
|
2023-07-14 16:53:20 +00:00
|
|
|
}
|
|
|
|
async function getKyberPreKeyById(
|
|
|
|
id: PreKeyIdType
|
|
|
|
): Promise<KyberPreKeyType | undefined> {
|
2024-07-22 18:16:33 +00:00
|
|
|
const data = await readableChannel.getPreKeyById(id);
|
2023-07-14 16:53:20 +00:00
|
|
|
|
|
|
|
return specToBytes(KYBER_PRE_KEY_SPEC, data);
|
|
|
|
}
|
|
|
|
async function bulkAddKyberPreKeys(
|
|
|
|
array: Array<KyberPreKeyType>
|
|
|
|
): Promise<void> {
|
|
|
|
const updated: Array<StoredKyberPreKeyType> = map(array, data =>
|
|
|
|
specFromBytes(KYBER_PRE_KEY_SPEC, data)
|
|
|
|
);
|
2024-07-22 18:16:33 +00:00
|
|
|
await writableChannel.bulkAddKyberPreKeys(updated);
|
2023-07-14 16:53:20 +00:00
|
|
|
}
|
|
|
|
async function getAllKyberPreKeys(): Promise<Array<KyberPreKeyType>> {
|
2024-07-22 18:16:33 +00:00
|
|
|
const keys = await readableChannel.getAllKyberPreKeys();
|
2023-07-14 16:53:20 +00:00
|
|
|
|
|
|
|
return keys.map(key => specToBytes(KYBER_PRE_KEY_SPEC, key));
|
|
|
|
}
|
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
// Pre Keys
|
|
|
|
|
2022-07-28 16:35:29 +00:00
|
|
|
async function createOrUpdatePreKey(data: PreKeyType): Promise<void> {
|
|
|
|
const updated: StoredPreKeyType = specFromBytes(PRE_KEY_SPEC, data);
|
2024-07-22 18:16:33 +00:00
|
|
|
await writableChannel.createOrUpdatePreKey(updated);
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
2022-07-28 16:35:29 +00:00
|
|
|
async function getPreKeyById(
|
|
|
|
id: PreKeyIdType
|
|
|
|
): Promise<PreKeyType | undefined> {
|
2024-07-22 18:16:33 +00:00
|
|
|
const data = await readableChannel.getPreKeyById(id);
|
2020-04-01 18:59:11 +00:00
|
|
|
|
2022-07-28 16:35:29 +00:00
|
|
|
return specToBytes(PRE_KEY_SPEC, data);
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
2022-07-28 16:35:29 +00:00
|
|
|
async function bulkAddPreKeys(array: Array<PreKeyType>): Promise<void> {
|
|
|
|
const updated: Array<StoredPreKeyType> = map(array, data =>
|
|
|
|
specFromBytes(PRE_KEY_SPEC, data)
|
|
|
|
);
|
2024-07-22 18:16:33 +00:00
|
|
|
await writableChannel.bulkAddPreKeys(updated);
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
2022-07-28 16:35:29 +00:00
|
|
|
async function getAllPreKeys(): Promise<Array<PreKeyType>> {
|
2024-07-22 18:16:33 +00:00
|
|
|
const keys = await readableChannel.getAllPreKeys();
|
2020-04-01 18:59:11 +00:00
|
|
|
|
2022-07-28 16:35:29 +00:00
|
|
|
return keys.map(key => specToBytes(PRE_KEY_SPEC, key));
|
2019-02-04 23:54:37 +00:00
|
|
|
}
|
2018-10-18 01:01:21 +00:00
|
|
|
|
|
|
|
// Signed Pre Keys
|
|
|
|
|
2022-07-28 16:35:29 +00:00
|
|
|
const PRE_KEY_SPEC = ['privateKey', 'publicKey'];
|
|
|
|
async function createOrUpdateSignedPreKey(
|
|
|
|
data: SignedPreKeyType
|
|
|
|
): Promise<void> {
|
|
|
|
const updated: StoredSignedPreKeyType = specFromBytes(PRE_KEY_SPEC, data);
|
2024-07-22 18:16:33 +00:00
|
|
|
await writableChannel.createOrUpdateSignedPreKey(updated);
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
2022-07-28 16:35:29 +00:00
|
|
|
async function getSignedPreKeyById(
|
|
|
|
id: SignedPreKeyIdType
|
|
|
|
): Promise<SignedPreKeyType | undefined> {
|
2024-07-22 18:16:33 +00:00
|
|
|
const data = await readableChannel.getSignedPreKeyById(id);
|
2020-04-01 18:59:11 +00:00
|
|
|
|
2022-07-28 16:35:29 +00:00
|
|
|
return specToBytes(PRE_KEY_SPEC, data);
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
2022-07-28 16:35:29 +00:00
|
|
|
async function getAllSignedPreKeys(): Promise<Array<SignedPreKeyType>> {
|
2024-07-22 18:16:33 +00:00
|
|
|
const keys = await readableChannel.getAllSignedPreKeys();
|
2020-04-01 18:59:11 +00:00
|
|
|
|
2022-07-28 16:35:29 +00:00
|
|
|
return keys.map(key => specToBytes(PRE_KEY_SPEC, key));
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
2022-07-28 16:35:29 +00:00
|
|
|
async function bulkAddSignedPreKeys(
|
|
|
|
array: Array<SignedPreKeyType>
|
|
|
|
): Promise<void> {
|
|
|
|
const updated: Array<StoredSignedPreKeyType> = map(array, data =>
|
|
|
|
specFromBytes(PRE_KEY_SPEC, data)
|
|
|
|
);
|
2024-07-22 18:16:33 +00:00
|
|
|
await writableChannel.bulkAddSignedPreKeys(updated);
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Items
|
|
|
|
|
2022-07-28 16:35:29 +00:00
|
|
|
const ITEM_SPECS: Partial<Record<ItemKeyType, ObjectMappingSpecType>> = {
|
2024-07-15 20:58:55 +00:00
|
|
|
defaultWallpaperPhotoPointer: ['value'],
|
2022-07-28 16:35:29 +00:00
|
|
|
identityKeyMap: {
|
|
|
|
key: 'value',
|
|
|
|
valueSpec: {
|
|
|
|
isMap: true,
|
|
|
|
valueSpec: ['privKey', 'pubKey'],
|
|
|
|
},
|
|
|
|
},
|
2022-08-18 20:44:53 +00:00
|
|
|
profileKey: ['value'],
|
|
|
|
senderCertificate: ['value.serialized'],
|
|
|
|
senderCertificateNoE164: ['value.serialized'],
|
|
|
|
subscriberId: ['value'],
|
2024-07-15 20:58:55 +00:00
|
|
|
backupsSubscriberId: ['value'],
|
2023-08-18 16:33:41 +00:00
|
|
|
usernameLink: ['value.entropy', 'value.serverId'],
|
2018-10-18 01:01:21 +00:00
|
|
|
};
|
2022-07-28 16:35:29 +00:00
|
|
|
async function createOrUpdateItem<K extends ItemKeyType>(
|
|
|
|
data: ItemType<K>
|
|
|
|
): Promise<void> {
|
2018-10-18 01:01:21 +00:00
|
|
|
const { id } = data;
|
|
|
|
if (!id) {
|
|
|
|
throw new Error(
|
|
|
|
'createOrUpdateItem: Provided data did not have a truthy id'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-07-28 16:35:29 +00:00
|
|
|
const spec = ITEM_SPECS[id];
|
|
|
|
const updated: StoredItemType<K> = spec
|
|
|
|
? specFromBytes(spec, data)
|
|
|
|
: (data as unknown as StoredItemType<K>);
|
2018-10-18 01:01:21 +00:00
|
|
|
|
2024-07-22 18:16:33 +00:00
|
|
|
await writableChannel.createOrUpdateItem(updated);
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
2021-09-10 02:38:11 +00:00
|
|
|
async function getItemById<K extends ItemKeyType>(
|
|
|
|
id: K
|
|
|
|
): Promise<ItemType<K> | undefined> {
|
2022-07-28 16:35:29 +00:00
|
|
|
const spec = ITEM_SPECS[id];
|
2024-07-22 18:16:33 +00:00
|
|
|
const data = await readableChannel.getItemById(id);
|
2018-10-18 01:01:21 +00:00
|
|
|
|
2023-08-18 16:33:41 +00:00
|
|
|
try {
|
|
|
|
return spec ? specToBytes(spec, data) : (data as unknown as ItemType<K>);
|
|
|
|
} catch (error) {
|
|
|
|
log.warn(`getItemById(${id}): Failed to parse item from spec`, error);
|
|
|
|
return undefined;
|
|
|
|
}
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
2022-07-28 16:35:29 +00:00
|
|
|
async function getAllItems(): Promise<AllItemsType> {
|
2024-07-22 18:16:33 +00:00
|
|
|
const items = await readableChannel.getAllItems();
|
2020-04-01 18:59:11 +00:00
|
|
|
|
2021-06-15 00:09:37 +00:00
|
|
|
const result = Object.create(null);
|
2020-04-01 18:59:11 +00:00
|
|
|
|
2021-06-15 00:09:37 +00:00
|
|
|
for (const id of Object.keys(items)) {
|
|
|
|
const key = id as ItemKeyType;
|
|
|
|
const value = items[key];
|
2020-04-01 18:59:11 +00:00
|
|
|
|
2022-07-28 16:35:29 +00:00
|
|
|
const keys = ITEM_SPECS[key];
|
2021-06-15 00:09:37 +00:00
|
|
|
|
2023-08-18 18:02:00 +00:00
|
|
|
try {
|
|
|
|
const deserializedValue = keys
|
|
|
|
? (specToBytes(keys, { value }) as ItemType<typeof key>).value
|
|
|
|
: value;
|
|
|
|
|
|
|
|
result[key] = deserializedValue;
|
|
|
|
} catch (error) {
|
|
|
|
log.warn(`getAllItems(${id}): Failed to parse item from spec`, error);
|
|
|
|
}
|
2021-06-15 00:09:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Conversation
|
|
|
|
|
2020-04-01 18:59:11 +00:00
|
|
|
const updateConversationBatcher = createBatcher<ConversationType>({
|
2021-03-26 00:00:03 +00:00
|
|
|
name: 'sql.Client.updateConversationBatcher',
|
2019-09-26 19:56:31 +00:00
|
|
|
wait: 500,
|
|
|
|
maxSize: 20,
|
2020-04-01 18:59:11 +00:00
|
|
|
processBatch: async (items: Array<ConversationType>) => {
|
2019-09-26 19:56:31 +00:00
|
|
|
// We only care about the most recent update for each conversation
|
|
|
|
const byId = groupBy(items, item => item.id);
|
|
|
|
const ids = Object.keys(byId);
|
2021-11-11 22:43:05 +00:00
|
|
|
const mostRecent = ids.map((id: string): ConversationType => {
|
|
|
|
const maybeLast = last(byId[id]);
|
2022-09-15 19:17:15 +00:00
|
|
|
assertDev(maybeLast !== undefined, 'Empty array in `groupBy` result');
|
2021-11-11 22:43:05 +00:00
|
|
|
return maybeLast;
|
|
|
|
});
|
2018-09-21 01:47:19 +00:00
|
|
|
|
2019-09-26 19:56:31 +00:00
|
|
|
await updateConversations(mostRecent);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2024-07-22 18:16:33 +00:00
|
|
|
async function updateConversation(data: ConversationType): Promise<void> {
|
2019-09-26 19:56:31 +00:00
|
|
|
updateConversationBatcher.add(data);
|
|
|
|
}
|
2023-08-17 23:35:41 +00:00
|
|
|
async function flushUpdateConversationBatcher(): Promise<void> {
|
|
|
|
await updateConversationBatcher.flushAndWait();
|
|
|
|
}
|
2019-09-26 19:56:31 +00:00
|
|
|
|
2022-07-28 16:35:29 +00:00
|
|
|
async function updateConversations(
|
|
|
|
array: Array<ConversationType>
|
|
|
|
): Promise<void> {
|
2021-02-04 19:54:03 +00:00
|
|
|
const { cleaned, pathsChanged } = cleanDataForIpc(array);
|
2022-09-15 19:17:15 +00:00
|
|
|
assertDev(
|
2021-02-04 19:54:03 +00:00
|
|
|
!pathsChanged.length,
|
|
|
|
`Paths were cleaned: ${JSON.stringify(pathsChanged)}`
|
|
|
|
);
|
2024-07-22 18:16:33 +00:00
|
|
|
await writableChannel.updateConversations(cleaned);
|
2018-09-21 01:47:19 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 16:35:29 +00:00
|
|
|
async function removeConversation(id: string): Promise<void> {
|
2024-07-22 18:16:33 +00:00
|
|
|
const existing = await readableChannel.getConversationById(id);
|
2018-09-21 01:47:19 +00:00
|
|
|
|
|
|
|
// Note: It's important to have a fully database-hydrated model to delete here because
|
|
|
|
// it needs to delete all associated on-disk files along with the database delete.
|
|
|
|
if (existing) {
|
2024-07-22 18:16:33 +00:00
|
|
|
await writableChannel.removeConversation(id);
|
2021-12-10 22:51:54 +00:00
|
|
|
await deleteExternalFiles(existing, {
|
|
|
|
deleteAttachmentData: window.Signal.Migrations.deleteAttachmentData,
|
|
|
|
});
|
2018-09-21 01:47:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-12 23:51:45 +00:00
|
|
|
function handleSearchMessageJSON(
|
2021-12-10 22:51:54 +00:00
|
|
|
messages: Array<ServerSearchResultMessageType>
|
2021-04-05 22:18:19 +00:00
|
|
|
): Array<ClientSearchResultMessageType> {
|
2023-06-26 18:25:48 +00:00
|
|
|
return messages.map<ClientSearchResultMessageType>(message => {
|
|
|
|
const parsedMessage = JSON.parse(message.json);
|
|
|
|
assertDev(
|
|
|
|
message.ftsSnippet ?? typeof message.mentionStart === 'number',
|
|
|
|
'Neither ftsSnippet nor matching mention returned from message search'
|
|
|
|
);
|
|
|
|
const snippet =
|
|
|
|
message.ftsSnippet ??
|
|
|
|
generateSnippetAroundMention({
|
|
|
|
body: parsedMessage.body,
|
|
|
|
mentionStart: message.mentionStart ?? 0,
|
|
|
|
mentionLength: message.mentionLength ?? 1,
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
json: message.json,
|
|
|
|
|
|
|
|
// Empty array is a default value. `message.json` has the real field
|
|
|
|
bodyRanges: [],
|
|
|
|
...parsedMessage,
|
|
|
|
snippet,
|
|
|
|
};
|
|
|
|
});
|
2019-01-14 21:49:58 +00:00
|
|
|
}
|
|
|
|
|
2023-06-26 18:25:48 +00:00
|
|
|
async function searchMessages({
|
|
|
|
query,
|
|
|
|
options,
|
2023-08-16 20:54:39 +00:00
|
|
|
contactServiceIdsMatchingQuery,
|
2023-06-26 18:25:48 +00:00
|
|
|
conversationId,
|
|
|
|
}: {
|
|
|
|
query: string;
|
|
|
|
options?: { limit?: number };
|
2023-08-16 20:54:39 +00:00
|
|
|
contactServiceIdsMatchingQuery?: Array<ServiceIdString>;
|
2023-06-26 18:25:48 +00:00
|
|
|
conversationId?: string;
|
|
|
|
}): Promise<Array<ClientSearchResultMessageType>> {
|
2024-07-22 18:16:33 +00:00
|
|
|
const messages = await readableChannel.searchMessages({
|
2019-01-14 21:49:58 +00:00
|
|
|
query,
|
|
|
|
conversationId,
|
2023-06-26 18:25:48 +00:00
|
|
|
options,
|
2023-08-16 20:54:39 +00:00
|
|
|
contactServiceIdsMatchingQuery,
|
2023-06-26 18:25:48 +00:00
|
|
|
});
|
2020-04-01 18:59:11 +00:00
|
|
|
|
2021-07-12 23:51:45 +00:00
|
|
|
return handleSearchMessageJSON(messages);
|
2018-09-21 01:47:19 +00:00
|
|
|
}
|
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
// Message
|
|
|
|
|
2020-04-01 18:59:11 +00:00
|
|
|
async function saveMessage(
|
|
|
|
data: MessageType,
|
2021-12-20 21:04:02 +00:00
|
|
|
options: {
|
|
|
|
jobToInsert?: Readonly<StoredJob>;
|
|
|
|
forceSave?: boolean;
|
2023-08-10 16:43:33 +00:00
|
|
|
ourAci: AciString;
|
2021-12-20 21:04:02 +00:00
|
|
|
}
|
2022-07-28 16:35:29 +00:00
|
|
|
): Promise<string> {
|
2024-07-22 18:16:33 +00:00
|
|
|
const id = await writableChannel.saveMessage(_cleanMessageData(data), {
|
2021-08-31 20:58:39 +00:00
|
|
|
...options,
|
|
|
|
jobToInsert: options.jobToInsert && formatJobForInsert(options.jobToInsert),
|
|
|
|
});
|
2021-07-19 20:45:18 +00:00
|
|
|
|
2022-08-11 21:02:25 +00:00
|
|
|
softAssert(isValidUuid(id), 'saveMessage: messageId is not a UUID');
|
|
|
|
|
2024-06-17 19:24:39 +00:00
|
|
|
void updateExpiringMessagesService();
|
2022-12-21 18:41:48 +00:00
|
|
|
void tapToViewMessagesDeletionService.update();
|
2018-10-18 01:01:21 +00:00
|
|
|
|
2020-04-01 18:59:11 +00:00
|
|
|
return id;
|
2018-08-01 19:38:48 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 18:59:11 +00:00
|
|
|
async function saveMessages(
|
2022-03-16 00:11:28 +00:00
|
|
|
arrayOfMessages: ReadonlyArray<MessageType>,
|
2023-08-10 16:43:33 +00:00
|
|
|
options: { forceSave?: boolean; ourAci: AciString }
|
2024-06-03 17:02:25 +00:00
|
|
|
): Promise<Array<string>> {
|
2024-07-22 18:16:33 +00:00
|
|
|
const result = await writableChannel.saveMessages(
|
2021-02-04 19:54:03 +00:00
|
|
|
arrayOfMessages.map(message => _cleanMessageData(message)),
|
2021-07-19 20:45:18 +00:00
|
|
|
options
|
2021-02-04 19:54:03 +00:00
|
|
|
);
|
2021-07-19 20:45:18 +00:00
|
|
|
|
2024-06-17 19:24:39 +00:00
|
|
|
void updateExpiringMessagesService();
|
2022-12-21 18:41:48 +00:00
|
|
|
void tapToViewMessagesDeletionService.update();
|
2024-06-03 17:02:25 +00:00
|
|
|
|
|
|
|
return result;
|
2018-07-25 22:02:27 +00:00
|
|
|
}
|
|
|
|
|
2024-06-17 19:24:39 +00:00
|
|
|
async function removeMessage(
|
|
|
|
id: string,
|
|
|
|
options: {
|
|
|
|
singleProtoJobQueue: SingleProtoJobQueue;
|
|
|
|
fromSync?: boolean;
|
|
|
|
}
|
|
|
|
): Promise<void> {
|
2024-07-22 18:16:33 +00:00
|
|
|
const message = await readableChannel.getMessageById(id);
|
2018-07-27 01:13:56 +00:00
|
|
|
|
2018-07-25 22:02:27 +00:00
|
|
|
// Note: It's important to have a fully database-hydrated model to delete here because
|
|
|
|
// it needs to delete all associated on-disk files along with the database delete.
|
|
|
|
if (message) {
|
2024-07-22 18:16:33 +00:00
|
|
|
await writableChannel.removeMessage(id);
|
2024-06-17 19:24:39 +00:00
|
|
|
await cleanupMessages([message], {
|
|
|
|
...options,
|
2024-07-22 18:16:33 +00:00
|
|
|
markCallHistoryDeleted: DataWriter.markCallHistoryDeleted,
|
2024-06-17 19:24:39 +00:00
|
|
|
});
|
2018-07-25 22:02:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-28 15:56:00 +00:00
|
|
|
export async function deleteAndCleanup(
|
|
|
|
messages: Array<MessageAttributesType>,
|
2024-06-17 19:24:39 +00:00
|
|
|
logId: string,
|
|
|
|
options: {
|
|
|
|
fromSync?: boolean;
|
|
|
|
singleProtoJobQueue: SingleProtoJobQueue;
|
|
|
|
}
|
2024-05-28 15:56:00 +00:00
|
|
|
): Promise<void> {
|
|
|
|
const ids = messages.map(message => message.id);
|
|
|
|
|
|
|
|
log.info(`deleteAndCleanup/${logId}: Deleting ${ids.length} messages...`);
|
2024-07-22 18:16:33 +00:00
|
|
|
await writableChannel.removeMessages(ids);
|
2024-05-28 15:56:00 +00:00
|
|
|
|
|
|
|
log.info(`deleteAndCleanup/${logId}: Cleanup for ${ids.length} messages...`);
|
2024-06-17 19:24:39 +00:00
|
|
|
await cleanupMessages(messages, {
|
|
|
|
...options,
|
2024-07-22 18:16:33 +00:00
|
|
|
markCallHistoryDeleted: DataWriter.markCallHistoryDeleted,
|
2023-09-07 20:07:07 +00:00
|
|
|
});
|
|
|
|
|
2024-06-17 19:24:39 +00:00
|
|
|
log.info(`deleteAndCleanup/${logId}: Complete`);
|
2023-03-20 22:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function removeMessages(
|
2024-06-17 19:24:39 +00:00
|
|
|
messageIds: ReadonlyArray<string>,
|
|
|
|
options: {
|
|
|
|
fromSync?: boolean;
|
|
|
|
singleProtoJobQueue: SingleProtoJobQueue;
|
|
|
|
}
|
2023-03-20 22:23:53 +00:00
|
|
|
): Promise<void> {
|
2024-07-22 18:16:33 +00:00
|
|
|
const messages = await readableChannel.getMessagesById(messageIds);
|
2024-06-17 19:24:39 +00:00
|
|
|
await cleanupMessages(messages, {
|
|
|
|
...options,
|
2024-07-22 18:16:33 +00:00
|
|
|
markCallHistoryDeleted: DataWriter.markCallHistoryDeleted,
|
2024-06-17 19:24:39 +00:00
|
|
|
});
|
2024-07-22 18:16:33 +00:00
|
|
|
await writableChannel.removeMessages(messageIds);
|
2023-03-20 22:23:53 +00:00
|
|
|
}
|
|
|
|
|
2021-12-10 22:51:54 +00:00
|
|
|
function handleMessageJSON(
|
|
|
|
messages: Array<MessageTypeUnhydrated>
|
|
|
|
): Array<MessageType> {
|
2021-07-12 23:51:45 +00:00
|
|
|
return messages.map(message => JSON.parse(message.json));
|
2019-08-09 23:12:29 +00:00
|
|
|
}
|
|
|
|
|
2022-11-17 00:29:15 +00:00
|
|
|
async function getNewerMessagesByConversation(
|
2023-03-04 03:03:15 +00:00
|
|
|
options: AdjacentMessagesByConversationOptionsType
|
2022-07-28 16:35:29 +00:00
|
|
|
): Promise<Array<MessageType>> {
|
2024-07-24 00:31:40 +00:00
|
|
|
const messages =
|
|
|
|
await readableChannel.getNewerMessagesByConversation(options);
|
2019-05-31 22:42:01 +00:00
|
|
|
|
2021-12-10 22:51:54 +00:00
|
|
|
return handleMessageJSON(messages);
|
2019-05-31 22:42:01 +00:00
|
|
|
}
|
2022-10-22 06:26:16 +00:00
|
|
|
|
2023-07-21 22:10:32 +00:00
|
|
|
async function getRecentStoryReplies(
|
|
|
|
storyId: string,
|
|
|
|
options?: GetRecentStoryRepliesOptionsType
|
|
|
|
): Promise<Array<MessageType>> {
|
2024-07-22 18:16:33 +00:00
|
|
|
const messages = await readableChannel.getRecentStoryReplies(
|
|
|
|
storyId,
|
|
|
|
options
|
|
|
|
);
|
2023-07-21 22:10:32 +00:00
|
|
|
|
|
|
|
return handleMessageJSON(messages);
|
|
|
|
}
|
|
|
|
|
2022-11-17 00:29:15 +00:00
|
|
|
async function getOlderMessagesByConversation(
|
2023-03-04 03:03:15 +00:00
|
|
|
options: AdjacentMessagesByConversationOptionsType
|
2022-07-28 16:35:29 +00:00
|
|
|
): Promise<Array<MessageType>> {
|
2024-07-24 00:31:40 +00:00
|
|
|
const messages =
|
|
|
|
await readableChannel.getOlderMessagesByConversation(options);
|
2018-07-25 22:02:27 +00:00
|
|
|
|
2021-12-10 22:51:54 +00:00
|
|
|
return handleMessageJSON(messages);
|
2018-07-25 22:02:27 +00:00
|
|
|
}
|
2020-04-01 18:59:11 +00:00
|
|
|
|
2023-03-04 03:03:15 +00:00
|
|
|
async function getConversationRangeCenteredOnMessage(
|
|
|
|
options: AdjacentMessagesByConversationOptionsType
|
|
|
|
): Promise<GetConversationRangeCenteredOnMessageResultType<MessageType>> {
|
2024-07-24 00:31:40 +00:00
|
|
|
const result =
|
|
|
|
await readableChannel.getConversationRangeCenteredOnMessage(options);
|
2021-12-20 21:05:13 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
...result,
|
|
|
|
older: handleMessageJSON(result.older),
|
|
|
|
newer: handleMessageJSON(result.newer),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-05-28 15:56:00 +00:00
|
|
|
async function removeMessagesInConversation(
|
2020-04-01 18:59:11 +00:00
|
|
|
conversationId: string,
|
2020-07-10 18:28:49 +00:00
|
|
|
{
|
2021-01-13 00:42:15 +00:00
|
|
|
logId,
|
2024-05-28 15:56:00 +00:00
|
|
|
receivedAt,
|
2024-06-17 19:24:39 +00:00
|
|
|
singleProtoJobQueue,
|
|
|
|
fromSync,
|
2021-01-13 00:42:15 +00:00
|
|
|
}: {
|
2024-06-17 19:24:39 +00:00
|
|
|
fromSync?: boolean;
|
2021-01-13 00:42:15 +00:00
|
|
|
logId: string;
|
2024-05-28 15:56:00 +00:00
|
|
|
receivedAt?: number;
|
2024-06-17 19:24:39 +00:00
|
|
|
singleProtoJobQueue: SingleProtoJobQueue;
|
2021-01-13 00:42:15 +00:00
|
|
|
}
|
2022-07-28 16:35:29 +00:00
|
|
|
): Promise<void> {
|
2018-07-27 01:13:56 +00:00
|
|
|
let messages;
|
2018-07-25 22:02:27 +00:00
|
|
|
do {
|
2021-01-13 00:42:15 +00:00
|
|
|
const chunkSize = 20;
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2021-01-13 00:42:15 +00:00
|
|
|
`removeAllMessagesInConversation/${logId}: Fetching chunk of ${chunkSize} messages`
|
|
|
|
);
|
|
|
|
// Yes, we really want the await in the loop. We're deleting a chunk at a
|
2018-07-25 22:02:27 +00:00
|
|
|
// time so we don't use too much memory.
|
2022-11-17 00:29:15 +00:00
|
|
|
// eslint-disable-next-line no-await-in-loop
|
2023-03-04 03:03:15 +00:00
|
|
|
messages = await getOlderMessagesByConversation({
|
|
|
|
conversationId,
|
2021-01-13 00:42:15 +00:00
|
|
|
limit: chunkSize,
|
2022-09-30 00:57:11 +00:00
|
|
|
includeStoryReplies: true,
|
2024-05-28 15:56:00 +00:00
|
|
|
receivedAt,
|
2022-05-11 22:20:47 +00:00
|
|
|
storyId: undefined,
|
2018-07-27 01:13:56 +00:00
|
|
|
});
|
2018-07-25 22:02:27 +00:00
|
|
|
|
2018-07-27 01:13:56 +00:00
|
|
|
if (!messages.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-17 00:29:15 +00:00
|
|
|
// eslint-disable-next-line no-await-in-loop
|
2024-06-17 19:24:39 +00:00
|
|
|
await deleteAndCleanup(messages, logId, { fromSync, singleProtoJobQueue });
|
2018-07-27 01:13:56 +00:00
|
|
|
} while (messages.length > 0);
|
2018-07-25 22:02:27 +00:00
|
|
|
}
|
|
|
|
|
2019-01-30 20:15:07 +00:00
|
|
|
// Attachment downloads
|
|
|
|
|
2022-07-28 16:35:29 +00:00
|
|
|
async function saveAttachmentDownloadJob(
|
|
|
|
job: AttachmentDownloadJobType
|
|
|
|
): Promise<void> {
|
2024-07-22 18:16:33 +00:00
|
|
|
await writableChannel.saveAttachmentDownloadJob(_cleanData(job));
|
2019-01-30 20:15:07 +00:00
|
|
|
}
|
2021-12-08 19:52:46 +00:00
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
// Other
|
|
|
|
|
2022-07-28 16:35:29 +00:00
|
|
|
async function cleanupOrphanedAttachments(): Promise<void> {
|
2022-11-17 00:29:15 +00:00
|
|
|
try {
|
|
|
|
await invokeWithTimeout(CLEANUP_ORPHANED_ATTACHMENTS_KEY);
|
|
|
|
} catch (error) {
|
|
|
|
log.warn(
|
|
|
|
'sql/Client: cleanupOrphanedAttachments failure',
|
|
|
|
Errors.toLogFormat(error)
|
|
|
|
);
|
|
|
|
}
|
2018-08-08 17:00:33 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 16:35:29 +00:00
|
|
|
async function ensureFilePermissions(): Promise<void> {
|
2022-11-17 00:29:15 +00:00
|
|
|
await invokeWithTimeout(ENSURE_FILE_PERMISSIONS);
|
2020-02-21 23:40:04 +00:00
|
|
|
}
|
|
|
|
|
2018-07-27 01:13:56 +00:00
|
|
|
// Note: will need to restart the app after calling this, to set up afresh
|
2022-07-28 16:35:29 +00:00
|
|
|
async function removeOtherData(): Promise<void> {
|
2018-07-27 01:13:56 +00:00
|
|
|
await Promise.all([
|
2022-11-17 00:29:15 +00:00
|
|
|
invokeWithTimeout(ERASE_SQL_KEY),
|
|
|
|
invokeWithTimeout(ERASE_ATTACHMENTS_KEY),
|
|
|
|
invokeWithTimeout(ERASE_STICKERS_KEY),
|
|
|
|
invokeWithTimeout(ERASE_TEMP_KEY),
|
|
|
|
invokeWithTimeout(ERASE_DRAFTS_KEY),
|
2018-07-27 01:13:56 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2022-11-17 00:29:15 +00:00
|
|
|
async function invokeWithTimeout(name: string): Promise<void> {
|
2021-09-27 18:22:46 +00:00
|
|
|
return createTaskWithTimeout(
|
2022-11-17 00:29:15 +00:00
|
|
|
() => ipc.invoke(name),
|
2021-09-27 18:22:46 +00:00
|
|
|
`callChannel call to ${name}`
|
|
|
|
)();
|
2018-07-27 01:13:56 +00:00
|
|
|
}
|
2024-07-22 18:16:33 +00:00
|
|
|
|
|
|
|
export function pauseWriteAccess(): Promise<void> {
|
|
|
|
return invokeWithTimeout(PAUSE_WRITE_ACCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function resumeWriteAccess(): Promise<void> {
|
|
|
|
return invokeWithTimeout(RESUME_WRITE_ACCESS);
|
|
|
|
}
|