signal-desktop/ts/sql/Client.ts

749 lines
20 KiB
TypeScript
Raw Normal View History

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';
2022-08-25 05:04:42 +00:00
import PQueue from 'p-queue';
2023-09-07 20:07:07 +00:00
import { batch } from 'react-redux';
2018-09-21 01:47:19 +00:00
2023-05-04 17:59:02 +00:00
import { has, get, groupBy, isTypedArray, last, map, omit } from 'lodash';
import { deleteExternalFiles } from '../types/Conversation';
import { expiringMessagesDeletionService } from '../services/expiringMessagesDeletion';
import { tapToViewMessagesDeletionService } from '../services/tapToViewMessagesDeletionService';
2021-09-24 00:49:05 +00:00
import * as Bytes from '../Bytes';
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';
import { cleanDataForIpc } from './cleanDataForIpc';
2023-08-16 20:54:39 +00:00
import type { AciString, ServiceIdString } from '../types/ServiceId';
import createTaskWithTimeout from '../textsecure/TaskWithTimeout';
import * as log from '../logging/log';
import { isValidUuid } from '../util/isValidUuid';
import * as Errors from '../types/errors';
import type { StoredJob } from '../jobs/types';
import { formatJobForInsert } from '../jobs/formatJobForInsert';
2023-09-07 20:07:07 +00:00
import {
cleanupMessage,
cleanupMessageFromMemory,
deleteMessageData,
} from '../util/cleanup';
import { drop } from '../util/drop';
2023-05-04 17:59:02 +00:00
import { ipcInvoke, doShutdown } from './channels';
import type {
2023-03-04 03:03:15 +00:00
AdjacentMessagesByConversationOptionsType,
2022-07-28 16:35:29 +00:00
AllItemsType,
AttachmentDownloadJobType,
ClientInterface,
ClientExclusiveInterface,
ClientSearchResultMessageType,
ConversationType,
2022-07-28 16:35:29 +00:00
GetConversationRangeCenteredOnMessageResultType,
GetRecentStoryRepliesOptionsType,
IdentityKeyIdType,
IdentityKeyType,
2022-07-28 16:35:29 +00:00
StoredIdentityKeyType,
ItemKeyType,
ItemType,
2022-07-28 16:35:29 +00:00
StoredItemType,
MessageType,
MessageTypeUnhydrated,
PreKeyIdType,
PreKeyType,
2022-07-28 16:35:29 +00:00
StoredPreKeyType,
ServerInterface,
ServerSearchResultMessageType,
SignedPreKeyIdType,
SignedPreKeyType,
2022-07-28 16:35:29 +00:00
StoredSignedPreKeyType,
KyberPreKeyType,
StoredKyberPreKeyType,
} from './Interface';
import { MINUTE } from '../util/durations';
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';
import { generateSnippetAroundMention } from '../util/search';
const ERASE_SQL_KEY = 'erase-sql-key';
const ERASE_ATTACHMENTS_KEY = 'erase-attachments';
const ERASE_STICKERS_KEY = 'erase-stickers';
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';
const ENSURE_FILE_PERMISSIONS = 'ensure-file-permissions';
const exclusiveInterface: ClientExclusiveInterface = {
createOrUpdateIdentityKey,
getIdentityKeyById,
bulkAddIdentityKeys,
getAllIdentityKeys,
createOrUpdateKyberPreKey,
getKyberPreKeyById,
bulkAddKyberPreKeys,
getAllKyberPreKeys,
createOrUpdatePreKey,
getPreKeyById,
bulkAddPreKeys,
getAllPreKeys,
createOrUpdateSignedPreKey,
getSignedPreKeyById,
bulkAddSignedPreKeys,
getAllSignedPreKeys,
createOrUpdateItem,
getItemById,
getAllItems,
updateConversation,
removeConversation,
searchMessages,
getRecentStoryReplies,
getOlderMessagesByConversation,
getConversationRangeCenteredOnMessage,
getNewerMessagesByConversation,
// Client-side only
flushUpdateConversationBatcher,
shutdown,
removeAllMessagesInConversation,
removeOtherData,
cleanupOrphanedAttachments,
ensureFilePermissions,
};
2023-05-04 17:59:02 +00:00
type ClientOverridesType = ClientExclusiveInterface &
Pick<
ServerInterface,
| 'removeMessage'
| 'removeMessages'
| 'saveAttachmentDownloadJob'
| 'saveMessage'
| 'saveMessages'
| 'updateConversations'
>;
const channels: ServerInterface = new Proxy({} as ServerInterface, {
get(_target, name) {
return async (...args: ReadonlyArray<unknown>) =>
ipcInvoke(String(name), args);
},
});
const clientExclusiveOverrides: ClientOverridesType = {
...exclusiveInterface,
removeMessage,
2023-03-20 22:23:53 +00:00
removeMessages,
saveAttachmentDownloadJob,
2023-05-04 17:59:02 +00:00
saveMessage,
saveMessages,
updateConversations,
};
2023-05-04 17:59:02 +00:00
const dataInterface: ClientInterface = new Proxy(
{
...clientExclusiveOverrides,
} as ClientInterface,
{
get(target, name) {
return async (...args: ReadonlyArray<unknown>) => {
if (has(target, name)) {
return get(target, name)(...args);
}
return get(channels, name)(...args);
};
},
}
);
export default dataInterface;
function _cleanData(
data: unknown
): ReturnType<typeof cleanDataForIpc>['cleaned'] {
const { cleaned, pathsChanged } = cleanDataForIpc(data);
if (pathsChanged.length) {
log.info(
`_cleanData cleaned the following paths: ${pathsChanged.join(', ')}`
);
}
return cleaned;
}
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) {
assertDev(false, 'received_at was not set on the message');
2023-04-11 03:54:43 +00:00
result.received_at = incrementMessageCounter();
}
if (data.attachments) {
const logId = getMessageIdForLogging(data);
2022-07-28 16:35:29 +00:00
result.attachments = data.attachments.map((attachment, index) => {
if (attachment.data && !isTypedArray(attachment.data)) {
log.warn(
`_cleanMessageData/${logId}: Attachment ${index} had non-array \`data\` field; deleting.`
);
return omit(attachment, ['data']);
}
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']);
}
return attachment;
});
}
2022-07-28 16:35:29 +00:00
return _cleanData(omit(result, ['dataMessage']));
}
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> {
log.info('Client.shutdown');
// Stop accepting new SQL jobs, flush outstanding queue
await doShutdown();
// Close database
await channels.close();
}
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);
await channels.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> {
const data = await channels.getIdentityKeyById(id);
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)
);
await channels.bulkAddIdentityKeys(updated);
2018-10-18 01:01:21 +00:00
}
2022-07-28 16:35:29 +00:00
async function getAllIdentityKeys(): Promise<Array<IdentityKeyType>> {
const keys = await channels.getAllIdentityKeys();
2022-07-28 16:35:29 +00:00
return keys.map(key => specToBytes(IDENTITY_KEY_SPEC, key));
}
2018-10-18 01:01:21 +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
);
await channels.createOrUpdateKyberPreKey(updated);
}
async function getKyberPreKeyById(
id: PreKeyIdType
): Promise<KyberPreKeyType | undefined> {
const data = await channels.getPreKeyById(id);
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)
);
await channels.bulkAddKyberPreKeys(updated);
}
async function getAllKyberPreKeys(): Promise<Array<KyberPreKeyType>> {
const keys = await channels.getAllKyberPreKeys();
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);
await channels.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> {
const data = await channels.getPreKeyById(id);
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)
);
await channels.bulkAddPreKeys(updated);
2018-10-18 01:01:21 +00:00
}
2022-07-28 16:35:29 +00:00
async function getAllPreKeys(): Promise<Array<PreKeyType>> {
const keys = await channels.getAllPreKeys();
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
// 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);
await channels.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> {
const data = await channels.getSignedPreKeyById(id);
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>> {
const keys = await channels.getAllSignedPreKeys();
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)
);
await channels.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>> = {
identityKeyMap: {
key: 'value',
valueSpec: {
isMap: true,
valueSpec: ['privKey', 'pubKey'],
},
},
profileKey: ['value'],
senderCertificate: ['value.serialized'],
senderCertificateNoE164: ['value.serialized'],
subscriberId: ['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
await channels.createOrUpdateItem(updated);
2018-10-18 01:01:21 +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];
const data = await channels.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> {
const items = await channels.getAllItems();
const result = Object.create(null);
for (const id of Object.keys(items)) {
const key = id as ItemKeyType;
const value = items[key];
2022-07-28 16:35:29 +00:00
const keys = ITEM_SPECS[key];
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);
}
}
return result;
2018-10-18 01:01:21 +00:00
}
// Conversation
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,
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]);
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);
},
});
2022-07-28 16:35:29 +00:00
function updateConversation(data: ConversationType): void {
2019-09-26 19:56:31 +00:00
updateConversationBatcher.add(data);
}
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> {
const { cleaned, pathsChanged } = cleanDataForIpc(array);
assertDev(
!pathsChanged.length,
`Paths were cleaned: ${JSON.stringify(pathsChanged)}`
);
await channels.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> {
const existing = await channels.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) {
await channels.removeConversation(id);
await deleteExternalFiles(existing, {
deleteAttachmentData: window.Signal.Migrations.deleteAttachmentData,
});
2018-09-21 01:47:19 +00:00
}
}
function handleSearchMessageJSON(
messages: Array<ServerSearchResultMessageType>
): Array<ClientSearchResultMessageType> {
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
}
async function searchMessages({
query,
options,
2023-08-16 20:54:39 +00:00
contactServiceIdsMatchingQuery,
conversationId,
}: {
query: string;
options?: { limit?: number };
2023-08-16 20:54:39 +00:00
contactServiceIdsMatchingQuery?: Array<ServiceIdString>;
conversationId?: string;
}): Promise<Array<ClientSearchResultMessageType>> {
const messages = await channels.searchMessages({
2019-01-14 21:49:58 +00:00
query,
conversationId,
options,
2023-08-16 20:54:39 +00:00
contactServiceIdsMatchingQuery,
});
return handleSearchMessageJSON(messages);
2018-09-21 01:47:19 +00:00
}
2018-10-18 01:01:21 +00:00
// Message
async function saveMessage(
data: MessageType,
2021-12-20 21:04:02 +00:00
options: {
jobToInsert?: Readonly<StoredJob>;
forceSave?: boolean;
ourAci: AciString;
2021-12-20 21:04:02 +00:00
}
2022-07-28 16:35:29 +00:00
): Promise<string> {
const id = await channels.saveMessage(_cleanMessageData(data), {
...options,
jobToInsert: options.jobToInsert && formatJobForInsert(options.jobToInsert),
});
softAssert(isValidUuid(id), 'saveMessage: messageId is not a UUID');
void expiringMessagesDeletionService.update();
void tapToViewMessagesDeletionService.update();
2018-10-18 01:01:21 +00:00
return id;
}
async function saveMessages(
arrayOfMessages: ReadonlyArray<MessageType>,
options: { forceSave?: boolean; ourAci: AciString }
2022-07-28 16:35:29 +00:00
): Promise<void> {
await channels.saveMessages(
arrayOfMessages.map(message => _cleanMessageData(message)),
options
);
void expiringMessagesDeletionService.update();
void tapToViewMessagesDeletionService.update();
}
2022-07-28 16:35:29 +00:00
async function removeMessage(id: string): Promise<void> {
const message = await channels.getMessageById(id);
// 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) {
await channels.removeMessage(id);
await cleanupMessage(message);
}
}
2023-03-20 22:23:53 +00:00
async function _cleanupMessages(
messages: ReadonlyArray<MessageAttributesType>
): Promise<void> {
2023-09-07 20:07:07 +00:00
// First, remove messages from memory, so we can batch the updates in redux
batch(() => {
messages.forEach(message => cleanupMessageFromMemory(message));
});
// Then, handle any asynchronous actions (e.g. deleting data from disk)
2023-03-20 22:23:53 +00:00
const queue = new PQueue({ concurrency: 3, timeout: MINUTE * 30 });
drop(
queue.addAll(
messages.map(
2023-09-07 20:07:07 +00:00
(message: MessageAttributesType) => async () =>
deleteMessageData(message)
2023-03-20 22:23:53 +00:00
)
)
);
await queue.onIdle();
}
async function removeMessages(
messageIds: ReadonlyArray<string>
): Promise<void> {
const messages = await channels.getMessagesById(messageIds);
await _cleanupMessages(messages);
await channels.removeMessages(messageIds);
}
function handleMessageJSON(
messages: Array<MessageTypeUnhydrated>
): Array<MessageType> {
return messages.map(message => JSON.parse(message.json));
2019-08-09 23:12:29 +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>> {
2023-03-04 03:03:15 +00:00
const messages = await channels.getNewerMessagesByConversation(options);
return handleMessageJSON(messages);
}
async function getRecentStoryReplies(
storyId: string,
options?: GetRecentStoryRepliesOptionsType
): Promise<Array<MessageType>> {
const messages = await channels.getRecentStoryReplies(storyId, options);
return handleMessageJSON(messages);
}
async function getOlderMessagesByConversation(
2023-03-04 03:03:15 +00:00
options: AdjacentMessagesByConversationOptionsType
2022-07-28 16:35:29 +00:00
): Promise<Array<MessageType>> {
2023-03-04 03:03:15 +00:00
const messages = await channels.getOlderMessagesByConversation(options);
return handleMessageJSON(messages);
}
2023-03-04 03:03:15 +00:00
async function getConversationRangeCenteredOnMessage(
options: AdjacentMessagesByConversationOptionsType
): Promise<GetConversationRangeCenteredOnMessageResultType<MessageType>> {
const result = await channels.getConversationRangeCenteredOnMessage(options);
return {
...result,
older: handleMessageJSON(result.older),
newer: handleMessageJSON(result.newer),
};
}
async function removeAllMessagesInConversation(
conversationId: string,
{
2021-01-13 00:42:15 +00:00
logId,
}: {
logId: string;
}
2022-07-28 16:35:29 +00:00
): Promise<void> {
let messages;
do {
2021-01-13 00:42:15 +00:00
const chunkSize = 20;
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
// time so we don't use too much memory.
// 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,
includeStoryReplies: true,
storyId: undefined,
});
if (!messages.length) {
return;
}
const ids = messages.map(message => message.id);
log.info(`removeAllMessagesInConversation/${logId}: Cleanup...`);
// eslint-disable-next-line no-await-in-loop
2023-03-20 22:23:53 +00:00
await _cleanupMessages(messages);
log.info(`removeAllMessagesInConversation/${logId}: Deleting...`);
// eslint-disable-next-line no-await-in-loop
await channels.removeMessages(ids);
} while (messages.length > 0);
}
// Attachment downloads
2022-07-28 16:35:29 +00:00
async function saveAttachmentDownloadJob(
job: AttachmentDownloadJobType
): Promise<void> {
await channels.saveAttachmentDownloadJob(_cleanData(job));
}
2018-10-18 01:01:21 +00:00
// Other
2022-07-28 16:35:29 +00:00
async function cleanupOrphanedAttachments(): Promise<void> {
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> {
await invokeWithTimeout(ENSURE_FILE_PERMISSIONS);
}
// 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> {
await Promise.all([
invokeWithTimeout(ERASE_SQL_KEY),
invokeWithTimeout(ERASE_ATTACHMENTS_KEY),
invokeWithTimeout(ERASE_STICKERS_KEY),
invokeWithTimeout(ERASE_TEMP_KEY),
invokeWithTimeout(ERASE_DRAFTS_KEY),
]);
}
async function invokeWithTimeout(name: string): Promise<void> {
return createTaskWithTimeout(
() => ipc.invoke(name),
`callChannel call to ${name}`
)();
}