2021-01-22 18:17:15 +00:00
|
|
|
// Copyright 2020-2021 Signal Messenger, LLC
|
2020-10-30 20:34:04 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2020-09-24 21:53:21 +00:00
|
|
|
/* eslint-disable no-await-in-loop */
|
|
|
|
/* eslint-disable camelcase */
|
|
|
|
/* eslint-disable no-param-reassign */
|
|
|
|
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
|
|
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
|
|
/* eslint-disable @typescript-eslint/ban-types */
|
2021-10-07 18:16:51 +00:00
|
|
|
import { ipcRenderer as ipc } from 'electron';
|
|
|
|
import fs from 'fs-extra';
|
|
|
|
import pify from 'pify';
|
2018-09-21 01:47:19 +00:00
|
|
|
|
2021-03-10 22:25:57 +00:00
|
|
|
import {
|
|
|
|
cloneDeep,
|
|
|
|
compact,
|
|
|
|
fromPairs,
|
|
|
|
get,
|
|
|
|
groupBy,
|
|
|
|
isFunction,
|
|
|
|
last,
|
|
|
|
map,
|
|
|
|
omit,
|
|
|
|
set,
|
2021-07-15 23:48:09 +00:00
|
|
|
toPairs,
|
|
|
|
uniq,
|
2021-03-10 22:25:57 +00:00
|
|
|
} from 'lodash';
|
2020-04-01 18:59:11 +00:00
|
|
|
|
2021-12-10 22:51:54 +00:00
|
|
|
import { deleteExternalFiles } from '../types/Conversation';
|
2021-09-24 00:49:05 +00:00
|
|
|
import * as Bytes from '../Bytes';
|
2020-04-01 18:59:11 +00:00
|
|
|
import { CURRENT_SCHEMA_VERSION } from '../../js/modules/types/message';
|
|
|
|
import { createBatcher } from '../util/batcher';
|
2021-10-07 18:16:51 +00:00
|
|
|
import { assert, strictAssert } from '../util/assert';
|
2021-02-04 19:54:03 +00:00
|
|
|
import { cleanDataForIpc } from './cleanDataForIpc';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { ReactionType } from '../types/Reactions';
|
|
|
|
import type { ConversationColorType, CustomColorType } from '../types/Colors';
|
2021-10-26 22:59:08 +00:00
|
|
|
import type { UUIDStringType } from '../types/UUID';
|
2021-11-02 23:01:13 +00:00
|
|
|
import type { BadgeType } from '../badges/types';
|
2021-08-20 16:06:15 +00:00
|
|
|
import type { ProcessGroupCallRingRequestResult } from '../types/Calling';
|
2021-08-30 21:39:57 +00:00
|
|
|
import type { RemoveAllConfiguration } from '../types/RemoveAllConfiguration';
|
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';
|
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';
|
2021-12-10 22:51:54 +00:00
|
|
|
import { cleanupMessage } from '../util/cleanup';
|
2020-07-10 18:28:49 +00:00
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type {
|
2020-04-01 18:59:11 +00:00
|
|
|
AttachmentDownloadJobType,
|
|
|
|
ClientInterface,
|
2021-03-10 22:25:57 +00:00
|
|
|
ClientJobType,
|
2021-07-15 23:48:09 +00:00
|
|
|
ClientSearchResultMessageType,
|
2020-04-01 18:59:11 +00:00
|
|
|
ConversationType,
|
2021-08-31 21:35:01 +00:00
|
|
|
DeleteSentProtoRecipientOptionsType,
|
2021-09-10 02:38:11 +00:00
|
|
|
IdentityKeyIdType,
|
2021-12-10 22:51:54 +00:00
|
|
|
IdentityKeyType,
|
2021-06-15 00:09:37 +00:00
|
|
|
ItemKeyType,
|
2020-04-01 18:59:11 +00:00
|
|
|
ItemType,
|
2021-08-16 16:56:27 +00:00
|
|
|
LastConversationMessagesType,
|
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,
|
2021-09-10 02:38:11 +00:00
|
|
|
SenderKeyIdType,
|
2021-12-10 22:51:54 +00:00
|
|
|
SenderKeyType,
|
2021-07-15 23:48:09 +00:00
|
|
|
SentMessageDBType,
|
|
|
|
SentMessagesType,
|
|
|
|
SentProtoType,
|
|
|
|
SentProtoWithMessageIdsType,
|
|
|
|
SentRecipientsDBType,
|
|
|
|
SentRecipientsType,
|
2021-03-10 22:25:57 +00:00
|
|
|
ServerInterface,
|
2021-12-10 22:51:54 +00:00
|
|
|
ServerSearchResultMessageType,
|
2021-09-10 02:38:11 +00:00
|
|
|
SessionIdType,
|
2021-12-10 22:51:54 +00:00
|
|
|
SessionType,
|
2021-09-10 02:38:11 +00:00
|
|
|
SignedPreKeyIdType,
|
2021-12-10 22:51:54 +00:00
|
|
|
SignedPreKeyType,
|
2020-04-01 18:59:11 +00:00
|
|
|
StickerPackStatusType,
|
|
|
|
StickerPackType,
|
|
|
|
StickerType,
|
2021-12-08 19:52:46 +00:00
|
|
|
StoryDistributionMemberType,
|
2021-12-10 22:51:54 +00:00
|
|
|
StoryDistributionType,
|
2021-12-08 19:52:46 +00:00
|
|
|
StoryDistributionWithMembersType,
|
|
|
|
StoryReadType,
|
2020-04-01 18:59:11 +00:00
|
|
|
UnprocessedType,
|
2021-04-16 23:13:13 +00:00
|
|
|
UnprocessedUpdateType,
|
2020-04-01 18:59:11 +00:00
|
|
|
} from './Interface';
|
2021-03-04 21:44:57 +00:00
|
|
|
import Server from './Server';
|
2021-09-08 20:39:14 +00:00
|
|
|
import { isCorruptionError } from './errors';
|
2018-07-27 01:13:56 +00:00
|
|
|
|
2021-10-07 18:16:51 +00:00
|
|
|
// We listen to a lot of events on ipc, often on the same channel. This prevents
|
2018-07-27 01:13:56 +00:00
|
|
|
// any warnings that might be sent to the console in that case.
|
2021-10-07 18:16:51 +00:00
|
|
|
if (ipc && ipc.setMaxListeners) {
|
|
|
|
ipc.setMaxListeners(0);
|
2021-01-06 15:41:43 +00:00
|
|
|
} else {
|
2021-10-07 18:16:51 +00:00
|
|
|
log.warn('sql/Client: ipc is not available!');
|
2021-01-06 15:41:43 +00:00
|
|
|
}
|
2018-07-27 01:13:56 +00:00
|
|
|
|
2021-10-07 18:16:51 +00:00
|
|
|
const getRealPath = pify(fs.realpath);
|
|
|
|
|
2021-04-26 22:01:22 +00:00
|
|
|
const MIN_TRACE_DURATION = 10;
|
|
|
|
|
2021-03-10 22:25:57 +00:00
|
|
|
const SQL_CHANNEL_KEY = 'sql-channel';
|
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';
|
2018-07-27 01:13:56 +00:00
|
|
|
|
2021-03-10 22:25:57 +00:00
|
|
|
type ClientJobUpdateType = {
|
|
|
|
resolve: Function;
|
|
|
|
reject: Function;
|
|
|
|
args?: Array<any>;
|
|
|
|
};
|
|
|
|
|
2021-10-07 18:16:51 +00:00
|
|
|
enum RendererState {
|
|
|
|
InMain = 'InMain',
|
|
|
|
Opening = 'Opening',
|
|
|
|
InRenderer = 'InRenderer',
|
|
|
|
Closing = 'Closing',
|
|
|
|
}
|
|
|
|
|
2021-03-10 22:25:57 +00:00
|
|
|
const _jobs: { [id: string]: ClientJobType } = Object.create(null);
|
|
|
|
const _DEBUG = false;
|
|
|
|
let _jobCounter = 0;
|
|
|
|
let _shuttingDown = false;
|
|
|
|
let _shutdownCallback: Function | null = null;
|
|
|
|
let _shutdownPromise: Promise<any> | null = null;
|
2021-10-07 18:16:51 +00:00
|
|
|
let state = RendererState.InMain;
|
2021-04-26 22:01:22 +00:00
|
|
|
const startupQueries = new Map<string, number>();
|
2021-03-10 22:25:57 +00:00
|
|
|
|
2020-04-01 18:59:11 +00:00
|
|
|
// Because we can't force this module to conform to an interface, we narrow our exports
|
|
|
|
// to this one default export, which does conform to the interface.
|
|
|
|
// Note: In Javascript, you need to access the .default property when requiring it
|
|
|
|
// https://github.com/microsoft/TypeScript/issues/420
|
|
|
|
const dataInterface: ClientInterface = {
|
2018-07-27 01:13:56 +00:00
|
|
|
close,
|
|
|
|
removeDB,
|
2018-11-01 23:40:19 +00:00
|
|
|
removeIndexedDBFiles,
|
2018-07-27 01:13:56 +00:00
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
createOrUpdateIdentityKey,
|
|
|
|
getIdentityKeyById,
|
|
|
|
bulkAddIdentityKeys,
|
|
|
|
removeIdentityKeyById,
|
|
|
|
removeAllIdentityKeys,
|
2019-02-04 23:54:37 +00:00
|
|
|
getAllIdentityKeys,
|
2018-10-18 01:01:21 +00:00
|
|
|
|
|
|
|
createOrUpdatePreKey,
|
|
|
|
getPreKeyById,
|
|
|
|
bulkAddPreKeys,
|
|
|
|
removePreKeyById,
|
|
|
|
removeAllPreKeys,
|
2019-02-04 23:54:37 +00:00
|
|
|
getAllPreKeys,
|
2018-10-18 01:01:21 +00:00
|
|
|
|
|
|
|
createOrUpdateSignedPreKey,
|
|
|
|
getSignedPreKeyById,
|
|
|
|
bulkAddSignedPreKeys,
|
|
|
|
removeSignedPreKeyById,
|
|
|
|
removeAllSignedPreKeys,
|
2021-12-10 22:51:54 +00:00
|
|
|
getAllSignedPreKeys,
|
2018-10-18 01:01:21 +00:00
|
|
|
|
|
|
|
createOrUpdateItem,
|
|
|
|
getItemById,
|
|
|
|
removeItemById,
|
|
|
|
removeAllItems,
|
2021-12-10 22:51:54 +00:00
|
|
|
getAllItems,
|
2018-10-18 01:01:21 +00:00
|
|
|
|
2021-05-14 01:18:43 +00:00
|
|
|
createOrUpdateSenderKey,
|
|
|
|
getSenderKeyById,
|
|
|
|
removeAllSenderKeys,
|
|
|
|
getAllSenderKeys,
|
2021-05-25 22:40:04 +00:00
|
|
|
removeSenderKeyById,
|
2021-05-14 01:18:43 +00:00
|
|
|
|
2021-07-15 23:48:09 +00:00
|
|
|
insertSentProto,
|
|
|
|
deleteSentProtosOlderThan,
|
|
|
|
deleteSentProtoByMessageId,
|
|
|
|
insertProtoRecipients,
|
|
|
|
deleteSentProtoRecipient,
|
|
|
|
getSentProtoByRecipient,
|
|
|
|
removeAllSentProtos,
|
|
|
|
getAllSentProtos,
|
|
|
|
_getAllSentProtoRecipients,
|
|
|
|
_getAllSentProtoMessageIds,
|
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
createOrUpdateSession,
|
2019-09-26 19:56:31 +00:00
|
|
|
createOrUpdateSessions,
|
2021-05-17 18:03:42 +00:00
|
|
|
commitSessionsAndUnprocessed,
|
2018-10-18 01:01:21 +00:00
|
|
|
bulkAddSessions,
|
|
|
|
removeSessionById,
|
2020-04-01 18:59:11 +00:00
|
|
|
removeSessionsByConversation,
|
2018-10-18 01:01:21 +00:00
|
|
|
removeAllSessions,
|
2019-02-04 23:54:37 +00:00
|
|
|
getAllSessions,
|
2018-10-18 01:01:21 +00:00
|
|
|
|
2021-12-10 22:51:54 +00:00
|
|
|
eraseStorageServiceStateFromConversations,
|
2018-09-21 01:47:19 +00:00
|
|
|
getConversationCount,
|
|
|
|
saveConversation,
|
|
|
|
saveConversations,
|
|
|
|
getConversationById,
|
|
|
|
updateConversation,
|
2019-09-26 19:56:31 +00:00
|
|
|
updateConversations,
|
2018-09-21 01:47:19 +00:00
|
|
|
removeConversation,
|
2021-05-28 16:15:17 +00:00
|
|
|
updateAllConversationColors,
|
2018-09-21 01:47:19 +00:00
|
|
|
|
|
|
|
getAllConversations,
|
|
|
|
getAllConversationIds,
|
2021-10-26 22:59:08 +00:00
|
|
|
getAllGroupsInvolvingUuid,
|
2019-01-14 21:49:58 +00:00
|
|
|
|
2018-09-21 01:47:19 +00:00
|
|
|
searchConversations,
|
2019-01-14 21:49:58 +00:00
|
|
|
searchMessages,
|
|
|
|
searchMessagesInConversation,
|
2018-09-21 01:47:19 +00:00
|
|
|
|
2018-08-06 23:18:58 +00:00
|
|
|
getMessageCount,
|
2018-07-27 01:13:56 +00:00
|
|
|
saveMessage,
|
|
|
|
saveMessages,
|
|
|
|
removeMessage,
|
2021-01-13 00:42:15 +00:00
|
|
|
removeMessages,
|
2021-12-08 19:52:46 +00:00
|
|
|
getTotalUnreadForConversation,
|
2021-05-07 01:15:25 +00:00
|
|
|
getUnreadByConversationAndMarkRead,
|
|
|
|
getUnreadReactionsAndMarkRead,
|
|
|
|
markReactionAsRead,
|
|
|
|
removeReactionFromConversation,
|
|
|
|
addReaction,
|
2021-10-15 22:54:31 +00:00
|
|
|
_getAllReactions,
|
2021-12-08 19:52:46 +00:00
|
|
|
_removeAllReactions,
|
2018-07-27 01:13:56 +00:00
|
|
|
getMessageBySender,
|
|
|
|
getMessageById,
|
2021-08-31 20:58:39 +00:00
|
|
|
getMessagesById,
|
2021-12-10 22:51:54 +00:00
|
|
|
_getAllMessages,
|
|
|
|
_removeAllMessages,
|
2018-07-27 01:13:56 +00:00
|
|
|
getAllMessageIds,
|
|
|
|
getMessagesBySentAt,
|
|
|
|
getExpiredMessages,
|
2021-06-18 19:12:04 +00:00
|
|
|
getMessagesUnexpectedlyMissingExpirationStartTimestamp,
|
2021-06-16 22:20:17 +00:00
|
|
|
getSoonestMessageExpiry,
|
2021-05-19 16:17:51 +00:00
|
|
|
getNextTapToViewMessageTimestampToAgeOut,
|
2019-06-26 19:33:13 +00:00
|
|
|
getTapToViewMessagesNeedingErase,
|
2019-05-31 22:42:01 +00:00
|
|
|
getOlderMessagesByConversation,
|
2021-12-08 19:52:46 +00:00
|
|
|
getOlderStories,
|
2019-05-31 22:42:01 +00:00
|
|
|
getNewerMessagesByConversation,
|
|
|
|
getMessageMetricsForConversation,
|
2021-12-10 22:51:54 +00:00
|
|
|
getLastConversationMessages,
|
2020-12-07 20:43:19 +00:00
|
|
|
hasGroupCallHistoryMessage,
|
2020-07-10 18:28:49 +00:00
|
|
|
migrateConversationMessages,
|
2018-07-27 01:13:56 +00:00
|
|
|
|
2018-09-28 22:51:26 +00:00
|
|
|
getUnprocessedCount,
|
2018-07-27 01:13:56 +00:00
|
|
|
getAllUnprocessed,
|
|
|
|
getUnprocessedById,
|
2019-02-05 01:23:50 +00:00
|
|
|
updateUnprocessedWithData,
|
2019-09-26 19:56:31 +00:00
|
|
|
updateUnprocessedsWithData,
|
2018-07-27 01:13:56 +00:00
|
|
|
removeUnprocessed,
|
|
|
|
removeAllUnprocessed,
|
|
|
|
|
2019-01-30 20:15:07 +00:00
|
|
|
getNextAttachmentDownloadJobs,
|
|
|
|
saveAttachmentDownloadJob,
|
|
|
|
resetAttachmentDownloadPending,
|
|
|
|
setAttachmentDownloadJobPending,
|
|
|
|
removeAttachmentDownloadJob,
|
|
|
|
removeAllAttachmentDownloadJobs,
|
|
|
|
|
2019-05-16 22:32:11 +00:00
|
|
|
createOrUpdateStickerPack,
|
|
|
|
updateStickerPackStatus,
|
|
|
|
createOrUpdateSticker,
|
|
|
|
updateStickerLastUsed,
|
|
|
|
addStickerPackReference,
|
|
|
|
deleteStickerPackReference,
|
2021-12-10 22:51:54 +00:00
|
|
|
getStickerCount,
|
2019-05-16 22:32:11 +00:00
|
|
|
deleteStickerPack,
|
|
|
|
getAllStickerPacks,
|
|
|
|
getAllStickers,
|
|
|
|
getRecentStickers,
|
2021-01-27 22:39:45 +00:00
|
|
|
clearAllErrorStickerPackAttempts,
|
2019-05-16 22:32:11 +00:00
|
|
|
|
2019-05-24 23:58:27 +00:00
|
|
|
updateEmojiUsage,
|
|
|
|
getRecentEmojis,
|
|
|
|
|
2021-11-02 23:01:13 +00:00
|
|
|
getAllBadges,
|
|
|
|
updateOrCreateBadges,
|
|
|
|
badgeImageFileDownloaded,
|
|
|
|
|
2021-12-08 19:52:46 +00:00
|
|
|
_getAllStoryDistributions,
|
|
|
|
_getAllStoryDistributionMembers,
|
|
|
|
_deleteAllStoryDistributions,
|
|
|
|
createNewStoryDistribution,
|
|
|
|
getAllStoryDistributionsWithMembers,
|
2021-12-10 02:15:59 +00:00
|
|
|
modifyStoryDistribution,
|
2021-12-08 19:52:46 +00:00
|
|
|
modifyStoryDistributionMembers,
|
|
|
|
deleteStoryDistribution,
|
|
|
|
|
|
|
|
_getAllStoryReads,
|
|
|
|
_deleteAllStoryReads,
|
|
|
|
addNewStoryRead,
|
|
|
|
getLastStoryReadsForAuthor,
|
|
|
|
|
2018-07-27 01:13:56 +00:00
|
|
|
removeAll,
|
2018-10-18 01:01:21 +00:00
|
|
|
removeAllConfiguration,
|
|
|
|
|
2020-04-01 18:59:11 +00:00
|
|
|
getMessagesNeedingUpgrade,
|
|
|
|
getMessagesWithVisualMediaAttachments,
|
|
|
|
getMessagesWithFileAttachments,
|
2021-05-27 20:17:05 +00:00
|
|
|
getMessageServerGuidsForSpam,
|
2020-04-01 18:59:11 +00:00
|
|
|
|
2021-04-29 23:02:27 +00:00
|
|
|
getJobsInQueue,
|
|
|
|
insertJob,
|
|
|
|
deleteJob,
|
|
|
|
|
2021-08-20 16:06:15 +00:00
|
|
|
processGroupCallRingRequest,
|
|
|
|
processGroupCallRingCancelation,
|
|
|
|
cleanExpiredGroupCallRings,
|
|
|
|
|
2021-09-15 18:45:22 +00:00
|
|
|
getMaxMessageCounter,
|
|
|
|
|
2021-07-30 16:43:16 +00:00
|
|
|
getStatisticsForLogging,
|
|
|
|
|
2020-04-01 18:59:11 +00:00
|
|
|
// Client-side only
|
|
|
|
|
|
|
|
shutdown,
|
|
|
|
removeAllMessagesInConversation,
|
|
|
|
|
2018-07-27 01:13:56 +00:00
|
|
|
removeOtherData,
|
2018-08-08 17:00:33 +00:00
|
|
|
cleanupOrphanedAttachments,
|
2020-02-21 23:40:04 +00:00
|
|
|
ensureFilePermissions,
|
2018-07-27 01:13:56 +00:00
|
|
|
|
2020-04-01 18:59:11 +00:00
|
|
|
// Client-side only, and test-only
|
|
|
|
|
2021-10-07 18:16:51 +00:00
|
|
|
startInRendererProcess,
|
2021-03-10 22:25:57 +00:00
|
|
|
goBackToMainProcess,
|
|
|
|
_jobs,
|
2018-07-27 01:13:56 +00:00
|
|
|
};
|
|
|
|
|
2020-04-01 18:59:11 +00:00
|
|
|
export default dataInterface;
|
|
|
|
|
2021-10-07 18:16:51 +00:00
|
|
|
async function startInRendererProcess(isTesting = false): Promise<void> {
|
|
|
|
strictAssert(
|
|
|
|
state === RendererState.InMain,
|
|
|
|
`startInRendererProcess: expected ${state} to be ${RendererState.InMain}`
|
|
|
|
);
|
|
|
|
|
|
|
|
log.info('data.startInRendererProcess: switching to renderer process');
|
|
|
|
state = RendererState.Opening;
|
|
|
|
|
|
|
|
if (!isTesting) {
|
|
|
|
ipc.send('database-ready');
|
|
|
|
|
|
|
|
await new Promise<void>(resolve => {
|
|
|
|
ipc.once('database-ready', () => {
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
2021-04-05 22:18:19 +00:00
|
|
|
}
|
2021-04-01 18:34:28 +00:00
|
|
|
|
2021-10-07 18:16:51 +00:00
|
|
|
const configDir = await getRealPath(ipc.sendSync('get-user-data-path'));
|
|
|
|
const key = ipc.sendSync('user-config-key');
|
|
|
|
|
|
|
|
await Server.initializeRenderer({ configDir, key });
|
|
|
|
|
|
|
|
log.info('data.startInRendererProcess: switched to renderer process');
|
|
|
|
|
|
|
|
state = RendererState.InRenderer;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function goBackToMainProcess(): Promise<void> {
|
2021-10-15 18:43:13 +00:00
|
|
|
if (state === RendererState.InMain) {
|
|
|
|
log.info('goBackToMainProcess: Already in the main process');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-07 18:16:51 +00:00
|
|
|
strictAssert(
|
|
|
|
state === RendererState.InRenderer,
|
|
|
|
`goBackToMainProcess: expected ${state} to be ${RendererState.InRenderer}`
|
|
|
|
);
|
|
|
|
|
2021-04-05 22:18:19 +00:00
|
|
|
// We don't need to wait for pending queries since they are synchronous.
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('data.goBackToMainProcess: switching to main process');
|
2021-09-07 21:21:55 +00:00
|
|
|
const closePromise = close();
|
2021-05-13 20:54:54 +00:00
|
|
|
|
2021-09-07 21:21:55 +00:00
|
|
|
// It should be the last query we run in renderer process
|
2021-10-07 18:16:51 +00:00
|
|
|
state = RendererState.Closing;
|
2021-09-07 21:21:55 +00:00
|
|
|
await closePromise;
|
2021-10-07 18:16:51 +00:00
|
|
|
state = RendererState.InMain;
|
2021-09-07 21:21:55 +00:00
|
|
|
|
2021-04-26 22:01:22 +00:00
|
|
|
// Print query statistics for whole startup
|
|
|
|
const entries = Array.from(startupQueries.entries());
|
|
|
|
startupQueries.clear();
|
|
|
|
|
|
|
|
// Sort by decreasing duration
|
|
|
|
entries
|
|
|
|
.sort((a, b) => b[1] - a[1])
|
|
|
|
.filter(([_, duration]) => duration > MIN_TRACE_DURATION)
|
|
|
|
.forEach(([query, duration]) => {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(`startup query: ${query} ${duration}ms`);
|
2021-04-26 22:01:22 +00:00
|
|
|
});
|
2021-10-07 18:16:51 +00:00
|
|
|
|
|
|
|
log.info('data.goBackToMainProcess: switched to main process');
|
2021-03-10 22:25:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const channelsAsUnknown = fromPairs(
|
|
|
|
compact(
|
2021-04-07 22:40:12 +00:00
|
|
|
map(toPairs(dataInterface), ([name, value]: [string, any]) => {
|
2021-03-10 22:25:57 +00:00
|
|
|
if (isFunction(value)) {
|
2021-04-07 22:40:12 +00:00
|
|
|
return [name, makeChannel(name)];
|
2021-03-10 22:25:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
})
|
|
|
|
)
|
|
|
|
) as any;
|
|
|
|
|
|
|
|
const channels: ServerInterface = channelsAsUnknown;
|
|
|
|
|
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
|
|
|
|
2021-02-04 19:54:03 +00:00
|
|
|
function _cleanMessageData(data: MessageType): MessageType {
|
2021-03-04 21:44:57 +00:00
|
|
|
// Ensure that all messages have the received_at set properly
|
|
|
|
if (!data.received_at) {
|
|
|
|
assert(false, 'received_at was not set on the message');
|
|
|
|
data.received_at = window.Signal.Util.incrementMessageCounter();
|
2018-07-27 01:13:56 +00:00
|
|
|
}
|
2021-03-04 21:44:57 +00:00
|
|
|
return _cleanData(omit(data, ['dataMessage']));
|
2018-07-27 01:13:56 +00:00
|
|
|
}
|
|
|
|
|
2021-03-10 22:25:57 +00:00
|
|
|
async function _shutdown() {
|
|
|
|
const jobKeys = Object.keys(_jobs);
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2021-03-10 22:25:57 +00:00
|
|
|
`data.shutdown: shutdown requested. ${jobKeys.length} jobs outstanding`
|
|
|
|
);
|
|
|
|
|
|
|
|
if (_shutdownPromise) {
|
|
|
|
await _shutdownPromise;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_shuttingDown = true;
|
|
|
|
|
|
|
|
// No outstanding jobs, return immediately
|
|
|
|
if (jobKeys.length === 0 || _DEBUG) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Outstanding jobs; we need to wait until the last one is done
|
|
|
|
_shutdownPromise = new Promise<void>((resolve, reject) => {
|
|
|
|
_shutdownCallback = (error: Error) => {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('data.shutdown: process complete');
|
2021-03-10 22:25:57 +00:00
|
|
|
if (error) {
|
|
|
|
reject(error);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve();
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
await _shutdownPromise;
|
|
|
|
}
|
|
|
|
|
|
|
|
function _makeJob(fnName: string) {
|
|
|
|
if (_shuttingDown && fnName !== 'close') {
|
|
|
|
throw new Error(
|
|
|
|
`Rejecting SQL channel job (${fnName}); application is shutting down`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
_jobCounter += 1;
|
|
|
|
const id = _jobCounter;
|
|
|
|
|
|
|
|
if (_DEBUG) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(`SQL channel job ${id} (${fnName}) started`);
|
2021-03-10 22:25:57 +00:00
|
|
|
}
|
|
|
|
_jobs[id] = {
|
|
|
|
fnName,
|
|
|
|
start: Date.now(),
|
|
|
|
};
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
function _updateJob(id: number, data: ClientJobUpdateType) {
|
|
|
|
const { resolve, reject } = data;
|
|
|
|
const { fnName, start } = _jobs[id];
|
|
|
|
|
|
|
|
_jobs[id] = {
|
|
|
|
..._jobs[id],
|
|
|
|
...data,
|
|
|
|
resolve: (value: any) => {
|
|
|
|
_removeJob(id);
|
|
|
|
const end = Date.now();
|
2021-04-26 22:01:22 +00:00
|
|
|
if (_DEBUG) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2021-03-10 22:25:57 +00:00
|
|
|
`SQL channel job ${id} (${fnName}) succeeded in ${end - start}ms`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return resolve(value);
|
|
|
|
},
|
|
|
|
reject: (error: Error) => {
|
|
|
|
_removeJob(id);
|
|
|
|
const end = Date.now();
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(`SQL channel job ${id} (${fnName}) failed in ${end - start}ms`);
|
2021-03-10 22:25:57 +00:00
|
|
|
|
|
|
|
return reject(error);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function _removeJob(id: number) {
|
|
|
|
if (_DEBUG) {
|
|
|
|
_jobs[id].complete = true;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete _jobs[id];
|
|
|
|
|
|
|
|
if (_shutdownCallback) {
|
|
|
|
const keys = Object.keys(_jobs);
|
|
|
|
if (keys.length === 0) {
|
|
|
|
_shutdownCallback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function _getJob(id: number) {
|
|
|
|
return _jobs[id];
|
|
|
|
}
|
|
|
|
|
2021-10-07 18:16:51 +00:00
|
|
|
if (ipc && ipc.on) {
|
|
|
|
ipc.on(`${SQL_CHANNEL_KEY}-done`, (_, jobId, errorForDisplay, result) => {
|
|
|
|
const job = _getJob(jobId);
|
|
|
|
if (!job) {
|
|
|
|
throw new Error(
|
|
|
|
`Received SQL channel reply to job ${jobId}, but did not have it in our registry!`
|
|
|
|
);
|
|
|
|
}
|
2021-03-10 22:25:57 +00:00
|
|
|
|
2021-10-07 18:16:51 +00:00
|
|
|
const { resolve, reject, fnName } = job;
|
2021-03-10 22:25:57 +00:00
|
|
|
|
2021-10-07 18:16:51 +00:00
|
|
|
if (!resolve || !reject) {
|
|
|
|
throw new Error(
|
|
|
|
`SQL channel job ${jobId} (${fnName}): didn't have a resolve or reject`
|
|
|
|
);
|
|
|
|
}
|
2021-03-10 22:25:57 +00:00
|
|
|
|
2021-10-07 18:16:51 +00:00
|
|
|
if (errorForDisplay) {
|
|
|
|
return reject(
|
|
|
|
new Error(
|
|
|
|
`Error received from SQL channel job ${jobId} (${fnName}): ${errorForDisplay}`
|
|
|
|
)
|
|
|
|
);
|
2021-03-10 22:25:57 +00:00
|
|
|
}
|
2021-10-07 18:16:51 +00:00
|
|
|
|
|
|
|
return resolve(result);
|
|
|
|
});
|
2021-03-10 22:25:57 +00:00
|
|
|
} else {
|
2021-10-07 18:16:51 +00:00
|
|
|
log.warn('sql/Client: ipc.on is not available!');
|
2021-03-10 22:25:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function makeChannel(fnName: string) {
|
|
|
|
return async (...args: Array<any>) => {
|
|
|
|
// During startup we want to avoid the high overhead of IPC so we utilize
|
|
|
|
// the db that exists in the renderer process to be able to boot up quickly
|
|
|
|
// once the app is running we switch back to the main process to avoid the
|
|
|
|
// UI from locking up whenever we do costly db operations.
|
2021-10-07 18:16:51 +00:00
|
|
|
if (state === RendererState.InRenderer) {
|
2021-03-10 22:25:57 +00:00
|
|
|
const serverFnName = fnName as keyof ServerInterface;
|
2021-04-26 22:01:22 +00:00
|
|
|
const start = Date.now();
|
|
|
|
|
2021-09-08 20:39:14 +00:00
|
|
|
try {
|
|
|
|
// Ignoring this error TS2556: Expected 3 arguments, but got 0 or more.
|
2021-12-17 01:14:21 +00:00
|
|
|
return await (Server[serverFnName] as Function)(...args);
|
2021-09-08 20:39:14 +00:00
|
|
|
} catch (error) {
|
|
|
|
if (isCorruptionError(error)) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error(
|
2021-09-08 20:39:14 +00:00
|
|
|
'Detected sql corruption in renderer process. ' +
|
|
|
|
`Restarting the application immediately. Error: ${error.message}`
|
|
|
|
);
|
2021-10-21 20:13:33 +00:00
|
|
|
ipc?.send(
|
|
|
|
'database-error',
|
|
|
|
`${error.stack}\n${Server.getCorruptionLog()}`
|
|
|
|
);
|
2021-09-08 20:39:14 +00:00
|
|
|
}
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error(
|
2021-09-08 20:39:14 +00:00
|
|
|
`Renderer SQL channel job (${fnName}) error ${error.message}`
|
|
|
|
);
|
|
|
|
throw error;
|
|
|
|
} finally {
|
|
|
|
const duration = Date.now() - start;
|
2021-04-26 22:01:22 +00:00
|
|
|
|
2021-09-08 20:39:14 +00:00
|
|
|
startupQueries.set(
|
|
|
|
serverFnName,
|
|
|
|
(startupQueries.get(serverFnName) || 0) + duration
|
2021-04-26 22:01:22 +00:00
|
|
|
);
|
|
|
|
|
2021-09-08 20:39:14 +00:00
|
|
|
if (duration > MIN_TRACE_DURATION || _DEBUG) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2021-09-08 20:39:14 +00:00
|
|
|
`Renderer SQL channel job (${fnName}) completed in ${duration}ms`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2021-03-10 22:25:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const jobId = _makeJob(fnName);
|
|
|
|
|
2021-09-27 18:22:46 +00:00
|
|
|
return createTaskWithTimeout(
|
|
|
|
() =>
|
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
try {
|
2021-10-07 18:16:51 +00:00
|
|
|
ipc.send(SQL_CHANNEL_KEY, jobId, fnName, ...args);
|
2021-09-27 18:22:46 +00:00
|
|
|
|
|
|
|
_updateJob(jobId, {
|
|
|
|
resolve,
|
|
|
|
reject,
|
|
|
|
args: _DEBUG ? args : undefined,
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
_removeJob(jobId);
|
|
|
|
|
|
|
|
reject(error);
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
`SQL channel job ${jobId} (${fnName})`
|
|
|
|
)();
|
2021-03-10 22:25:57 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
function keysToBytes(keys: Array<string>, data: any) {
|
2018-10-18 01:01:21 +00:00
|
|
|
const updated = cloneDeep(data);
|
2020-04-01 18:59:11 +00:00
|
|
|
|
|
|
|
const max = keys.length;
|
|
|
|
for (let i = 0; i < max; i += 1) {
|
2018-10-18 01:01:21 +00:00
|
|
|
const key = keys[i];
|
|
|
|
const value = get(data, key);
|
|
|
|
|
|
|
|
if (value) {
|
2021-09-24 00:49:05 +00:00
|
|
|
set(updated, key, Bytes.fromBase64(value));
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return updated;
|
|
|
|
}
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
function keysFromBytes(keys: Array<string>, data: any) {
|
2018-10-18 01:01:21 +00:00
|
|
|
const updated = cloneDeep(data);
|
2020-04-01 18:59:11 +00:00
|
|
|
|
|
|
|
const max = keys.length;
|
|
|
|
for (let i = 0; i < max; i += 1) {
|
2018-10-18 01:01:21 +00:00
|
|
|
const key = keys[i];
|
|
|
|
const value = get(data, key);
|
|
|
|
|
|
|
|
if (value) {
|
2021-09-24 00:49:05 +00:00
|
|
|
set(updated, key, Bytes.toBase64(value));
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return updated;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Top-level calls
|
|
|
|
|
2018-11-05 19:06:12 +00:00
|
|
|
async function shutdown() {
|
2021-03-10 22:25:57 +00:00
|
|
|
// Stop accepting new SQL jobs, flush outstanding queue
|
|
|
|
await _shutdown();
|
|
|
|
|
2018-11-05 19:06:12 +00:00
|
|
|
// Close database
|
|
|
|
await close();
|
|
|
|
}
|
|
|
|
|
2018-07-27 01:13:56 +00:00
|
|
|
// Note: will need to restart the app after calling this, to set up afresh
|
|
|
|
async function close() {
|
2021-03-10 22:25:57 +00:00
|
|
|
await channels.close();
|
2018-07-27 01:13:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Note: will need to restart the app after calling this, to set up afresh
|
|
|
|
async function removeDB() {
|
2021-03-10 22:25:57 +00:00
|
|
|
await channels.removeDB();
|
2018-07-27 01:13:56 +00:00
|
|
|
}
|
|
|
|
|
2018-11-01 23:40:19 +00:00
|
|
|
async function removeIndexedDBFiles() {
|
2021-03-10 22:25:57 +00:00
|
|
|
await channels.removeIndexedDBFiles();
|
2018-11-01 23:40:19 +00:00
|
|
|
}
|
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
// Identity Keys
|
|
|
|
|
|
|
|
const IDENTITY_KEY_KEYS = ['publicKey'];
|
2020-04-01 18:59:11 +00:00
|
|
|
async function createOrUpdateIdentityKey(data: IdentityKeyType) {
|
2021-09-24 00:49:05 +00:00
|
|
|
const updated = keysFromBytes(IDENTITY_KEY_KEYS, data);
|
2021-03-10 22:25:57 +00:00
|
|
|
await channels.createOrUpdateIdentityKey(updated);
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
2021-09-10 02:38:11 +00:00
|
|
|
async function getIdentityKeyById(id: IdentityKeyIdType) {
|
2021-03-10 22:25:57 +00:00
|
|
|
const data = await channels.getIdentityKeyById(id);
|
2020-04-01 18:59:11 +00:00
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
return keysToBytes(IDENTITY_KEY_KEYS, data);
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
2020-04-01 18:59:11 +00:00
|
|
|
async function bulkAddIdentityKeys(array: Array<IdentityKeyType>) {
|
2021-09-24 00:49:05 +00:00
|
|
|
const updated = map(array, data => keysFromBytes(IDENTITY_KEY_KEYS, data));
|
2021-03-10 22:25:57 +00:00
|
|
|
await channels.bulkAddIdentityKeys(updated);
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
2021-09-10 02:38:11 +00:00
|
|
|
async function removeIdentityKeyById(id: IdentityKeyIdType) {
|
2021-03-10 22:25:57 +00:00
|
|
|
await channels.removeIdentityKeyById(id);
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
|
|
|
async function removeAllIdentityKeys() {
|
2021-03-10 22:25:57 +00:00
|
|
|
await channels.removeAllIdentityKeys();
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
2019-02-04 23:54:37 +00:00
|
|
|
async function getAllIdentityKeys() {
|
2021-03-10 22:25:57 +00:00
|
|
|
const keys = await channels.getAllIdentityKeys();
|
2020-04-01 18:59:11 +00:00
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
return keys.map(key => keysToBytes(IDENTITY_KEY_KEYS, key));
|
2019-02-04 23:54:37 +00:00
|
|
|
}
|
2018-10-18 01:01:21 +00:00
|
|
|
|
|
|
|
// Pre Keys
|
|
|
|
|
2020-04-01 18:59:11 +00:00
|
|
|
async function createOrUpdatePreKey(data: PreKeyType) {
|
2021-09-24 00:49:05 +00:00
|
|
|
const updated = keysFromBytes(PRE_KEY_KEYS, data);
|
2021-03-10 22:25:57 +00:00
|
|
|
await channels.createOrUpdatePreKey(updated);
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
2021-09-10 02:38:11 +00:00
|
|
|
async function getPreKeyById(id: PreKeyIdType) {
|
2021-03-10 22:25:57 +00:00
|
|
|
const data = await channels.getPreKeyById(id);
|
2020-04-01 18:59:11 +00:00
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
return keysToBytes(PRE_KEY_KEYS, data);
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
2020-04-01 18:59:11 +00:00
|
|
|
async function bulkAddPreKeys(array: Array<PreKeyType>) {
|
2021-09-24 00:49:05 +00:00
|
|
|
const updated = map(array, data => keysFromBytes(PRE_KEY_KEYS, data));
|
2021-03-10 22:25:57 +00:00
|
|
|
await channels.bulkAddPreKeys(updated);
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
2021-09-10 02:38:11 +00:00
|
|
|
async function removePreKeyById(id: PreKeyIdType) {
|
2021-03-10 22:25:57 +00:00
|
|
|
await channels.removePreKeyById(id);
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
|
|
|
async function removeAllPreKeys() {
|
2021-03-10 22:25:57 +00:00
|
|
|
await channels.removeAllPreKeys();
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
2019-02-04 23:54:37 +00:00
|
|
|
async function getAllPreKeys() {
|
2021-03-10 22:25:57 +00:00
|
|
|
const keys = await channels.getAllPreKeys();
|
2020-04-01 18:59:11 +00:00
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
return keys.map(key => keysToBytes(PRE_KEY_KEYS, key));
|
2019-02-04 23:54:37 +00:00
|
|
|
}
|
2018-10-18 01:01:21 +00:00
|
|
|
|
|
|
|
// Signed Pre Keys
|
|
|
|
|
|
|
|
const PRE_KEY_KEYS = ['privateKey', 'publicKey'];
|
2020-04-01 18:59:11 +00:00
|
|
|
async function createOrUpdateSignedPreKey(data: SignedPreKeyType) {
|
2021-09-24 00:49:05 +00:00
|
|
|
const updated = keysFromBytes(PRE_KEY_KEYS, data);
|
2021-03-10 22:25:57 +00:00
|
|
|
await channels.createOrUpdateSignedPreKey(updated);
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
2021-09-10 02:38:11 +00:00
|
|
|
async function getSignedPreKeyById(id: SignedPreKeyIdType) {
|
2021-03-10 22:25:57 +00:00
|
|
|
const data = await channels.getSignedPreKeyById(id);
|
2020-04-01 18:59:11 +00:00
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
return keysToBytes(PRE_KEY_KEYS, data);
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
|
|
|
async function getAllSignedPreKeys() {
|
2021-03-10 22:25:57 +00:00
|
|
|
const keys = await channels.getAllSignedPreKeys();
|
2020-04-01 18:59:11 +00:00
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
return keys.map((key: SignedPreKeyType) => keysToBytes(PRE_KEY_KEYS, key));
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
2020-04-01 18:59:11 +00:00
|
|
|
async function bulkAddSignedPreKeys(array: Array<SignedPreKeyType>) {
|
2021-09-24 00:49:05 +00:00
|
|
|
const updated = map(array, data => keysFromBytes(PRE_KEY_KEYS, data));
|
2021-03-10 22:25:57 +00:00
|
|
|
await channels.bulkAddSignedPreKeys(updated);
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
2021-09-10 02:38:11 +00:00
|
|
|
async function removeSignedPreKeyById(id: SignedPreKeyIdType) {
|
2021-03-10 22:25:57 +00:00
|
|
|
await channels.removeSignedPreKeyById(id);
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
|
|
|
async function removeAllSignedPreKeys() {
|
2021-03-10 22:25:57 +00:00
|
|
|
await channels.removeAllSignedPreKeys();
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Items
|
|
|
|
|
2021-06-15 00:09:37 +00:00
|
|
|
const ITEM_KEYS: Partial<Record<ItemKeyType, Array<string>>> = {
|
2020-03-19 17:57:50 +00:00
|
|
|
senderCertificate: ['value.serialized'],
|
2020-12-09 22:26:37 +00:00
|
|
|
senderCertificateNoE164: ['value.serialized'],
|
2021-11-30 16:29:57 +00:00
|
|
|
subscriberId: ['value'],
|
2018-10-18 01:01:21 +00:00
|
|
|
profileKey: ['value'],
|
|
|
|
};
|
2021-06-15 00:09:37 +00:00
|
|
|
async function createOrUpdateItem<K extends ItemKeyType>(data: ItemType<K>) {
|
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'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const keys = ITEM_KEYS[id];
|
2021-09-24 00:49:05 +00:00
|
|
|
const updated = Array.isArray(keys) ? keysFromBytes(keys, data) : data;
|
2018-10-18 01:01:21 +00:00
|
|
|
|
2021-03-10 22:25:57 +00:00
|
|
|
await channels.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> {
|
2018-10-18 01:01:21 +00:00
|
|
|
const keys = ITEM_KEYS[id];
|
2021-03-10 22:25:57 +00:00
|
|
|
const data = await channels.getItemById(id);
|
2018-10-18 01:01:21 +00:00
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
return Array.isArray(keys) ? keysToBytes(keys, data) : data;
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
|
|
|
async function getAllItems() {
|
2021-03-10 22:25:57 +00:00
|
|
|
const items = await channels.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
|
|
|
|
2021-06-15 00:09:37 +00:00
|
|
|
const keys = ITEM_KEYS[key];
|
|
|
|
|
|
|
|
const deserializedValue = Array.isArray(keys)
|
2021-09-24 00:49:05 +00:00
|
|
|
? keysToBytes(keys, { value }).value
|
2021-06-15 00:09:37 +00:00
|
|
|
: value;
|
|
|
|
|
|
|
|
result[key] = deserializedValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
2021-06-15 00:09:37 +00:00
|
|
|
async function removeItemById(id: ItemKeyType) {
|
2021-03-10 22:25:57 +00:00
|
|
|
await channels.removeItemById(id);
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
|
|
|
async function removeAllItems() {
|
2021-03-10 22:25:57 +00:00
|
|
|
await channels.removeAllItems();
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
|
|
|
|
2021-05-14 01:18:43 +00:00
|
|
|
// Sender Keys
|
|
|
|
|
|
|
|
async function createOrUpdateSenderKey(key: SenderKeyType): Promise<void> {
|
|
|
|
await channels.createOrUpdateSenderKey(key);
|
|
|
|
}
|
|
|
|
async function getSenderKeyById(
|
2021-09-10 02:38:11 +00:00
|
|
|
id: SenderKeyIdType
|
2021-05-14 01:18:43 +00:00
|
|
|
): Promise<SenderKeyType | undefined> {
|
|
|
|
return channels.getSenderKeyById(id);
|
|
|
|
}
|
|
|
|
async function removeAllSenderKeys(): Promise<void> {
|
|
|
|
await channels.removeAllSenderKeys();
|
|
|
|
}
|
|
|
|
async function getAllSenderKeys(): Promise<Array<SenderKeyType>> {
|
|
|
|
return channels.getAllSenderKeys();
|
|
|
|
}
|
2021-09-10 02:38:11 +00:00
|
|
|
async function removeSenderKeyById(id: SenderKeyIdType): Promise<void> {
|
2021-05-25 22:40:04 +00:00
|
|
|
return channels.removeSenderKeyById(id);
|
|
|
|
}
|
2021-05-14 01:18:43 +00:00
|
|
|
|
2021-07-15 23:48:09 +00:00
|
|
|
// Sent Protos
|
|
|
|
|
|
|
|
async function insertSentProto(
|
|
|
|
proto: SentProtoType,
|
|
|
|
options: {
|
|
|
|
messageIds: SentMessagesType;
|
|
|
|
recipients: SentRecipientsType;
|
|
|
|
}
|
|
|
|
): Promise<number> {
|
|
|
|
return channels.insertSentProto(proto, {
|
|
|
|
...options,
|
|
|
|
messageIds: uniq(options.messageIds),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
async function deleteSentProtosOlderThan(timestamp: number): Promise<void> {
|
|
|
|
await channels.deleteSentProtosOlderThan(timestamp);
|
|
|
|
}
|
|
|
|
async function deleteSentProtoByMessageId(messageId: string): Promise<void> {
|
|
|
|
await channels.deleteSentProtoByMessageId(messageId);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function insertProtoRecipients(options: {
|
|
|
|
id: number;
|
|
|
|
recipientUuid: string;
|
|
|
|
deviceIds: Array<number>;
|
|
|
|
}): Promise<void> {
|
|
|
|
await channels.insertProtoRecipients(options);
|
|
|
|
}
|
2021-08-31 21:35:01 +00:00
|
|
|
async function deleteSentProtoRecipient(
|
|
|
|
options:
|
|
|
|
| DeleteSentProtoRecipientOptionsType
|
|
|
|
| ReadonlyArray<DeleteSentProtoRecipientOptionsType>
|
|
|
|
): Promise<void> {
|
2021-07-15 23:48:09 +00:00
|
|
|
await channels.deleteSentProtoRecipient(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getSentProtoByRecipient(options: {
|
|
|
|
now: number;
|
|
|
|
recipientUuid: string;
|
|
|
|
timestamp: number;
|
|
|
|
}): Promise<SentProtoWithMessageIdsType | undefined> {
|
|
|
|
return channels.getSentProtoByRecipient(options);
|
|
|
|
}
|
|
|
|
async function removeAllSentProtos(): Promise<void> {
|
|
|
|
await channels.removeAllSentProtos();
|
|
|
|
}
|
|
|
|
async function getAllSentProtos(): Promise<Array<SentProtoType>> {
|
|
|
|
return channels.getAllSentProtos();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test-only:
|
|
|
|
async function _getAllSentProtoRecipients(): Promise<
|
|
|
|
Array<SentRecipientsDBType>
|
|
|
|
> {
|
|
|
|
return channels._getAllSentProtoRecipients();
|
|
|
|
}
|
|
|
|
async function _getAllSentProtoMessageIds(): Promise<Array<SentMessageDBType>> {
|
|
|
|
return channels._getAllSentProtoMessageIds();
|
|
|
|
}
|
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
// Sessions
|
|
|
|
|
2020-04-01 18:59:11 +00:00
|
|
|
async function createOrUpdateSession(data: SessionType) {
|
2021-03-10 22:25:57 +00:00
|
|
|
await channels.createOrUpdateSession(data);
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
2020-04-01 18:59:11 +00:00
|
|
|
async function createOrUpdateSessions(array: Array<SessionType>) {
|
2021-03-10 22:25:57 +00:00
|
|
|
await channels.createOrUpdateSessions(array);
|
2019-09-26 19:56:31 +00:00
|
|
|
}
|
2021-05-17 18:03:42 +00:00
|
|
|
async function commitSessionsAndUnprocessed(options: {
|
|
|
|
sessions: Array<SessionType>;
|
|
|
|
unprocessed: Array<UnprocessedType>;
|
|
|
|
}) {
|
|
|
|
await channels.commitSessionsAndUnprocessed(options);
|
|
|
|
}
|
2020-04-01 18:59:11 +00:00
|
|
|
async function bulkAddSessions(array: Array<SessionType>) {
|
2021-03-10 22:25:57 +00:00
|
|
|
await channels.bulkAddSessions(array);
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
2021-09-10 02:38:11 +00:00
|
|
|
async function removeSessionById(id: SessionIdType) {
|
2021-03-10 22:25:57 +00:00
|
|
|
await channels.removeSessionById(id);
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
2020-04-01 18:59:11 +00:00
|
|
|
|
|
|
|
async function removeSessionsByConversation(conversationId: string) {
|
2021-03-10 22:25:57 +00:00
|
|
|
await channels.removeSessionsByConversation(conversationId);
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
2020-04-01 18:59:11 +00:00
|
|
|
async function removeAllSessions() {
|
2021-03-10 22:25:57 +00:00
|
|
|
await channels.removeAllSessions();
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
2020-04-01 18:59:11 +00:00
|
|
|
async function getAllSessions() {
|
2021-03-10 22:25:57 +00:00
|
|
|
const sessions = await channels.getAllSessions();
|
2020-04-01 18:59:11 +00:00
|
|
|
|
2019-02-04 23:54:37 +00:00
|
|
|
return sessions;
|
|
|
|
}
|
2018-10-18 01:01:21 +00:00
|
|
|
|
|
|
|
// Conversation
|
|
|
|
|
2018-09-21 01:47:19 +00:00
|
|
|
async function getConversationCount() {
|
2021-03-10 22:25:57 +00:00
|
|
|
return channels.getConversationCount();
|
2018-09-21 01:47:19 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 18:59:11 +00:00
|
|
|
async function saveConversation(data: ConversationType) {
|
2021-03-10 22:25:57 +00:00
|
|
|
await channels.saveConversation(data);
|
2018-09-21 01:47:19 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 18:59:11 +00:00
|
|
|
async function saveConversations(array: Array<ConversationType>) {
|
2021-03-10 22:25:57 +00:00
|
|
|
await channels.saveConversations(array);
|
2018-09-21 01:47:19 +00:00
|
|
|
}
|
|
|
|
|
2021-12-10 22:51:54 +00:00
|
|
|
async function getConversationById(id: string) {
|
|
|
|
return channels.getConversationById(id);
|
2018-09-21 01:47:19 +00:00
|
|
|
}
|
|
|
|
|
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]);
|
|
|
|
assert(maybeLast !== undefined, 'Empty array in `groupBy` result');
|
|
|
|
return maybeLast;
|
|
|
|
});
|
2018-09-21 01:47:19 +00:00
|
|
|
|
2019-09-26 19:56:31 +00:00
|
|
|
await updateConversations(mostRecent);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-04-01 18:59:11 +00:00
|
|
|
function updateConversation(data: ConversationType) {
|
2019-09-26 19:56:31 +00:00
|
|
|
updateConversationBatcher.add(data);
|
|
|
|
}
|
|
|
|
|
2020-04-01 18:59:11 +00:00
|
|
|
async function updateConversations(array: Array<ConversationType>) {
|
2021-02-04 19:54:03 +00:00
|
|
|
const { cleaned, pathsChanged } = cleanDataForIpc(array);
|
|
|
|
assert(
|
|
|
|
!pathsChanged.length,
|
|
|
|
`Paths were cleaned: ${JSON.stringify(pathsChanged)}`
|
|
|
|
);
|
2021-03-10 22:25:57 +00:00
|
|
|
await channels.updateConversations(cleaned);
|
2018-09-21 01:47:19 +00:00
|
|
|
}
|
|
|
|
|
2021-12-10 22:51:54 +00:00
|
|
|
async function removeConversation(id: string) {
|
|
|
|
const existing = await 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) {
|
2021-03-10 22:25:57 +00:00
|
|
|
await channels.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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-09 00:56:23 +00:00
|
|
|
async function eraseStorageServiceStateFromConversations() {
|
2021-03-10 22:25:57 +00:00
|
|
|
await channels.eraseStorageServiceStateFromConversations();
|
2020-07-24 23:32:08 +00:00
|
|
|
}
|
|
|
|
|
2021-12-10 22:51:54 +00:00
|
|
|
async function getAllConversations() {
|
|
|
|
return channels.getAllConversations();
|
2018-09-21 01:47:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function getAllConversationIds() {
|
2021-03-10 22:25:57 +00:00
|
|
|
const ids = await channels.getAllConversationIds();
|
2020-04-01 18:59:11 +00:00
|
|
|
|
2018-09-21 01:47:19 +00:00
|
|
|
return ids;
|
|
|
|
}
|
|
|
|
|
2021-12-10 22:51:54 +00:00
|
|
|
async function getAllGroupsInvolvingUuid(uuid: UUIDStringType) {
|
|
|
|
return channels.getAllGroupsInvolvingUuid(uuid);
|
2018-09-21 01:47:19 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 18:59:11 +00:00
|
|
|
async function searchConversations(query: string) {
|
2021-03-10 22:25:57 +00:00
|
|
|
const conversations = await channels.searchConversations(query);
|
2020-04-01 18:59:11 +00:00
|
|
|
|
2019-01-14 21:49:58 +00:00
|
|
|
return conversations;
|
|
|
|
}
|
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> {
|
2021-07-12 23:51:45 +00:00
|
|
|
return messages.map(message => ({
|
|
|
|
json: message.json,
|
2021-04-15 20:46:09 +00:00
|
|
|
|
|
|
|
// Empty array is a default value. `message.json` has the real field
|
2021-04-05 22:18:19 +00:00
|
|
|
bodyRanges: [],
|
2021-04-15 20:46:09 +00:00
|
|
|
|
2021-07-12 23:51:45 +00:00
|
|
|
...JSON.parse(message.json),
|
|
|
|
snippet: message.snippet,
|
2019-08-09 23:12:29 +00:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2020-04-01 18:59:11 +00:00
|
|
|
async function searchMessages(
|
|
|
|
query: string,
|
|
|
|
{ limit }: { limit?: number } = {}
|
|
|
|
) {
|
2021-03-10 22:25:57 +00:00
|
|
|
const messages = await channels.searchMessages(query, { limit });
|
2020-04-01 18:59:11 +00:00
|
|
|
|
2021-07-12 23:51:45 +00:00
|
|
|
return handleSearchMessageJSON(messages);
|
2019-01-14 21:49:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function searchMessagesInConversation(
|
2020-04-01 18:59:11 +00:00
|
|
|
query: string,
|
|
|
|
conversationId: string,
|
|
|
|
{ limit }: { limit?: number } = {}
|
2019-01-14 21:49:58 +00:00
|
|
|
) {
|
2021-03-10 22:25:57 +00:00
|
|
|
const messages = await channels.searchMessagesInConversation(
|
2019-01-14 21:49:58 +00:00
|
|
|
query,
|
|
|
|
conversationId,
|
|
|
|
{ limit }
|
|
|
|
);
|
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-05-27 21:37:06 +00:00
|
|
|
async function getMessageCount(conversationId?: string) {
|
2021-03-10 22:25:57 +00:00
|
|
|
return channels.getMessageCount(conversationId);
|
2018-08-06 23:18:58 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 18:59:11 +00:00
|
|
|
async function saveMessage(
|
|
|
|
data: MessageType,
|
2021-08-31 20:58:39 +00:00
|
|
|
options: { jobToInsert?: Readonly<StoredJob>; forceSave?: boolean } = {}
|
2020-04-01 18:59:11 +00:00
|
|
|
) {
|
2021-08-31 20:58:39 +00:00
|
|
|
const id = await channels.saveMessage(_cleanMessageData(data), {
|
|
|
|
...options,
|
|
|
|
jobToInsert: options.jobToInsert && formatJobForInsert(options.jobToInsert),
|
|
|
|
});
|
2021-07-19 20:45:18 +00:00
|
|
|
|
|
|
|
window.Whisper.ExpiringMessagesListener.update();
|
|
|
|
window.Whisper.TapToViewMessagesListener.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(
|
|
|
|
arrayOfMessages: Array<MessageType>,
|
2021-07-19 20:45:18 +00:00
|
|
|
options?: { forceSave?: boolean }
|
2020-04-01 18:59:11 +00:00
|
|
|
) {
|
2021-03-10 22:25:57 +00:00
|
|
|
await channels.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
|
|
|
|
|
|
|
window.Whisper.ExpiringMessagesListener.update();
|
|
|
|
window.Whisper.TapToViewMessagesListener.update();
|
2018-07-25 22:02:27 +00:00
|
|
|
}
|
|
|
|
|
2021-12-10 22:51:54 +00:00
|
|
|
async function removeMessage(id: string) {
|
|
|
|
const message = await 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) {
|
2021-03-10 22:25:57 +00:00
|
|
|
await channels.removeMessage(id);
|
2021-12-10 22:51:54 +00:00
|
|
|
await cleanupMessage(message);
|
2018-07-25 22:02:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-01 02:51:17 +00:00
|
|
|
// Note: this method will not clean up external files, just delete from SQL
|
2021-01-13 00:42:15 +00:00
|
|
|
async function removeMessages(ids: Array<string>) {
|
2021-03-10 22:25:57 +00:00
|
|
|
await channels.removeMessages(ids);
|
2018-08-01 02:51:17 +00:00
|
|
|
}
|
|
|
|
|
2021-12-10 22:51:54 +00:00
|
|
|
async function getMessageById(id: string) {
|
|
|
|
return channels.getMessageById(id);
|
2018-09-21 01:47:19 +00:00
|
|
|
}
|
|
|
|
|
2021-08-31 20:58:39 +00:00
|
|
|
async function getMessagesById(messageIds: Array<string>) {
|
|
|
|
if (!messageIds.length) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
return channels.getMessagesById(messageIds);
|
|
|
|
}
|
|
|
|
|
2018-09-21 01:47:19 +00:00
|
|
|
// For testing only
|
2021-12-10 22:51:54 +00:00
|
|
|
async function _getAllMessages() {
|
|
|
|
return channels._getAllMessages();
|
2018-07-25 22:02:27 +00:00
|
|
|
}
|
2021-12-08 19:52:46 +00:00
|
|
|
async function _removeAllMessages() {
|
|
|
|
await channels._removeAllMessages();
|
|
|
|
}
|
2018-07-25 22:02:27 +00:00
|
|
|
|
2018-07-27 01:13:56 +00:00
|
|
|
async function getAllMessageIds() {
|
2021-03-10 22:25:57 +00:00
|
|
|
const ids = await channels.getAllMessageIds();
|
2020-04-01 18:59:11 +00:00
|
|
|
|
2018-07-27 01:13:56 +00:00
|
|
|
return ids;
|
2018-07-25 22:02:27 +00:00
|
|
|
}
|
|
|
|
|
2021-12-10 22:51:54 +00:00
|
|
|
async function getMessageBySender({
|
|
|
|
source,
|
|
|
|
sourceUuid,
|
|
|
|
sourceDevice,
|
|
|
|
sent_at,
|
|
|
|
}: {
|
|
|
|
source: string;
|
|
|
|
sourceUuid: string;
|
|
|
|
sourceDevice: number;
|
|
|
|
sent_at: number;
|
|
|
|
}) {
|
|
|
|
return channels.getMessageBySender({
|
2018-07-27 01:13:56 +00:00
|
|
|
source,
|
2020-03-05 21:14:58 +00:00
|
|
|
sourceUuid,
|
2018-07-27 01:13:56 +00:00
|
|
|
sourceDevice,
|
|
|
|
sent_at,
|
|
|
|
});
|
2018-07-25 22:02:27 +00:00
|
|
|
}
|
|
|
|
|
2021-12-08 19:52:46 +00:00
|
|
|
async function getTotalUnreadForConversation(
|
2020-04-01 18:59:11 +00:00
|
|
|
conversationId: string,
|
2021-12-08 19:52:46 +00:00
|
|
|
storyId?: UUIDStringType
|
2020-04-01 18:59:11 +00:00
|
|
|
) {
|
2021-12-08 19:52:46 +00:00
|
|
|
return channels.getTotalUnreadForConversation(conversationId, storyId);
|
2021-05-07 01:15:25 +00:00
|
|
|
}
|
2020-04-01 18:59:11 +00:00
|
|
|
|
2021-12-08 19:52:46 +00:00
|
|
|
async function getUnreadByConversationAndMarkRead(options: {
|
|
|
|
conversationId: string;
|
|
|
|
newestUnreadAt: number;
|
|
|
|
readAt?: number;
|
|
|
|
storyId?: UUIDStringType;
|
|
|
|
}) {
|
|
|
|
return channels.getUnreadByConversationAndMarkRead(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getUnreadReactionsAndMarkRead(options: {
|
|
|
|
conversationId: string;
|
|
|
|
newestUnreadAt: number;
|
|
|
|
storyId?: UUIDStringType;
|
|
|
|
}) {
|
|
|
|
return channels.getUnreadReactionsAndMarkRead(options);
|
2021-05-07 01:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function markReactionAsRead(
|
|
|
|
targetAuthorUuid: string,
|
|
|
|
targetTimestamp: number
|
|
|
|
) {
|
|
|
|
return channels.markReactionAsRead(targetAuthorUuid, targetTimestamp);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function removeReactionFromConversation(reaction: {
|
|
|
|
emoji: string;
|
|
|
|
fromId: string;
|
|
|
|
targetAuthorUuid: string;
|
|
|
|
targetTimestamp: number;
|
|
|
|
}) {
|
|
|
|
return channels.removeReactionFromConversation(reaction);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function addReaction(reactionObj: ReactionType) {
|
|
|
|
return channels.addReaction(reactionObj);
|
2018-07-25 22:02:27 +00:00
|
|
|
}
|
|
|
|
|
2021-10-15 22:54:31 +00:00
|
|
|
async function _getAllReactions() {
|
|
|
|
return channels._getAllReactions();
|
|
|
|
}
|
2021-12-08 19:52:46 +00:00
|
|
|
async function _removeAllReactions() {
|
|
|
|
await channels._removeAllReactions();
|
|
|
|
}
|
2021-10-15 22:54:31 +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
|
|
|
}
|
|
|
|
|
2019-05-31 22:42:01 +00:00
|
|
|
async function getOlderMessagesByConversation(
|
2020-04-01 18:59:11 +00:00
|
|
|
conversationId: string,
|
|
|
|
{
|
|
|
|
limit = 100,
|
2021-12-08 19:52:46 +00:00
|
|
|
messageId,
|
2020-04-01 18:59:11 +00:00
|
|
|
receivedAt = Number.MAX_VALUE,
|
2021-01-13 16:32:18 +00:00
|
|
|
sentAt = Number.MAX_VALUE,
|
2021-12-08 19:52:46 +00:00
|
|
|
storyId,
|
2020-04-01 18:59:11 +00:00
|
|
|
}: {
|
|
|
|
limit?: number;
|
2021-12-08 19:52:46 +00:00
|
|
|
messageId?: string;
|
2020-04-01 18:59:11 +00:00
|
|
|
receivedAt?: number;
|
2021-01-13 16:32:18 +00:00
|
|
|
sentAt?: number;
|
2021-12-08 19:52:46 +00:00
|
|
|
storyId?: UUIDStringType;
|
2020-04-01 18:59:11 +00:00
|
|
|
}
|
2018-07-25 22:02:27 +00:00
|
|
|
) {
|
2021-03-10 22:25:57 +00:00
|
|
|
const messages = await channels.getOlderMessagesByConversation(
|
|
|
|
conversationId,
|
|
|
|
{
|
|
|
|
limit,
|
|
|
|
receivedAt,
|
|
|
|
sentAt,
|
|
|
|
messageId,
|
2021-12-08 19:52:46 +00:00
|
|
|
storyId,
|
2021-03-10 22:25:57 +00:00
|
|
|
}
|
|
|
|
);
|
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
|
|
|
}
|
2021-12-08 19:52:46 +00:00
|
|
|
async function getOlderStories(options: {
|
|
|
|
conversationId?: string;
|
|
|
|
limit?: number;
|
|
|
|
receivedAt?: number;
|
|
|
|
sentAt?: number;
|
|
|
|
sourceUuid?: string;
|
|
|
|
}): Promise<Array<MessageType>> {
|
|
|
|
return channels.getOlderStories(options);
|
|
|
|
}
|
|
|
|
|
2019-05-31 22:42:01 +00:00
|
|
|
async function getNewerMessagesByConversation(
|
2020-04-01 18:59:11 +00:00
|
|
|
conversationId: string,
|
|
|
|
{
|
|
|
|
limit = 100,
|
|
|
|
receivedAt = 0,
|
2021-01-13 16:32:18 +00:00
|
|
|
sentAt = 0,
|
2021-12-08 19:52:46 +00:00
|
|
|
storyId,
|
2020-04-01 18:59:11 +00:00
|
|
|
}: {
|
|
|
|
limit?: number;
|
|
|
|
receivedAt?: number;
|
2021-01-13 16:32:18 +00:00
|
|
|
sentAt?: number;
|
2021-12-08 19:52:46 +00:00
|
|
|
storyId?: UUIDStringType;
|
2020-04-01 18:59:11 +00:00
|
|
|
}
|
2019-05-31 22:42:01 +00:00
|
|
|
) {
|
2021-03-10 22:25:57 +00:00
|
|
|
const messages = await channels.getNewerMessagesByConversation(
|
|
|
|
conversationId,
|
|
|
|
{
|
|
|
|
limit,
|
|
|
|
receivedAt,
|
|
|
|
sentAt,
|
2021-12-08 19:52:46 +00:00
|
|
|
storyId,
|
2021-03-10 22:25:57 +00:00
|
|
|
}
|
|
|
|
);
|
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
|
|
|
}
|
2021-08-16 16:56:27 +00:00
|
|
|
async function getLastConversationMessages({
|
2021-01-20 17:31:44 +00:00
|
|
|
conversationId,
|
2021-10-26 22:59:08 +00:00
|
|
|
ourUuid,
|
2021-01-20 17:31:44 +00:00
|
|
|
}: {
|
|
|
|
conversationId: string;
|
2021-10-26 22:59:08 +00:00
|
|
|
ourUuid: UUIDStringType;
|
2021-08-16 16:56:27 +00:00
|
|
|
}): Promise<LastConversationMessagesType> {
|
2021-11-11 22:43:05 +00:00
|
|
|
const { preview, activity, hasUserInitiatedMessages } =
|
|
|
|
await channels.getLastConversationMessages({
|
|
|
|
conversationId,
|
|
|
|
ourUuid,
|
|
|
|
});
|
2021-08-16 16:56:27 +00:00
|
|
|
|
|
|
|
return {
|
2021-12-10 22:51:54 +00:00
|
|
|
preview,
|
|
|
|
activity,
|
2021-08-16 16:56:27 +00:00
|
|
|
hasUserInitiatedMessages,
|
|
|
|
};
|
2020-08-07 00:50:54 +00:00
|
|
|
}
|
2021-12-08 19:52:46 +00:00
|
|
|
async function getMessageMetricsForConversation(
|
|
|
|
conversationId: string,
|
|
|
|
storyId?: UUIDStringType
|
|
|
|
) {
|
2021-03-10 22:25:57 +00:00
|
|
|
const result = await channels.getMessageMetricsForConversation(
|
2021-12-08 19:52:46 +00:00
|
|
|
conversationId,
|
|
|
|
storyId
|
2021-03-10 22:25:57 +00:00
|
|
|
);
|
2020-04-01 18:59:11 +00:00
|
|
|
|
2019-05-31 22:42:01 +00:00
|
|
|
return result;
|
|
|
|
}
|
2020-12-07 20:43:19 +00:00
|
|
|
function hasGroupCallHistoryMessage(
|
|
|
|
conversationId: string,
|
|
|
|
eraId: string
|
|
|
|
): Promise<boolean> {
|
2021-03-10 22:25:57 +00:00
|
|
|
return channels.hasGroupCallHistoryMessage(conversationId, eraId);
|
2020-12-07 20:43:19 +00:00
|
|
|
}
|
2020-07-10 18:28:49 +00:00
|
|
|
async function migrateConversationMessages(
|
|
|
|
obsoleteId: string,
|
|
|
|
currentId: string
|
|
|
|
) {
|
2021-03-10 22:25:57 +00:00
|
|
|
await channels.migrateConversationMessages(obsoleteId, currentId);
|
2020-07-10 18:28:49 +00:00
|
|
|
}
|
2018-07-25 22:02:27 +00:00
|
|
|
|
|
|
|
async function removeAllMessagesInConversation(
|
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,
|
|
|
|
}: {
|
|
|
|
logId: string;
|
|
|
|
}
|
2018-07-25 22:02:27 +00:00
|
|
|
) {
|
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.
|
2019-05-31 22:42:01 +00:00
|
|
|
messages = await getOlderMessagesByConversation(conversationId, {
|
2021-01-13 00:42:15 +00:00
|
|
|
limit: chunkSize,
|
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;
|
|
|
|
}
|
|
|
|
|
2021-12-10 22:51:54 +00:00
|
|
|
const ids = messages.map(message => message.id);
|
2018-07-25 22:02:27 +00:00
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(`removeAllMessagesInConversation/${logId}: Cleanup...`);
|
2018-07-25 22:02:27 +00:00
|
|
|
// Note: It's very important that these models are fully hydrated because
|
|
|
|
// we need to delete all associated on-disk files along with the database delete.
|
2021-01-13 00:42:15 +00:00
|
|
|
const queue = new window.PQueue({ concurrency: 3, timeout: 1000 * 60 * 2 });
|
|
|
|
queue.addAll(
|
2021-12-10 22:51:54 +00:00
|
|
|
messages.map(
|
|
|
|
(message: MessageType) => async () => cleanupMessage(message)
|
|
|
|
)
|
2020-04-01 18:59:11 +00:00
|
|
|
);
|
2021-01-13 00:42:15 +00:00
|
|
|
await queue.onIdle();
|
2018-07-27 01:13:56 +00:00
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(`removeAllMessagesInConversation/${logId}: Deleting...`);
|
2021-03-10 22:25:57 +00:00
|
|
|
await channels.removeMessages(ids);
|
2018-07-27 01:13:56 +00:00
|
|
|
} while (messages.length > 0);
|
2018-07-25 22:02:27 +00:00
|
|
|
}
|
|
|
|
|
2021-12-10 22:51:54 +00:00
|
|
|
async function getMessagesBySentAt(sentAt: number) {
|
|
|
|
return channels.getMessagesBySentAt(sentAt);
|
2018-07-25 22:02:27 +00:00
|
|
|
}
|
|
|
|
|
2021-12-10 22:51:54 +00:00
|
|
|
async function getExpiredMessages() {
|
|
|
|
return channels.getExpiredMessages();
|
2018-07-25 22:02:27 +00:00
|
|
|
}
|
|
|
|
|
2021-06-18 19:12:04 +00:00
|
|
|
function getMessagesUnexpectedlyMissingExpirationStartTimestamp() {
|
|
|
|
return channels.getMessagesUnexpectedlyMissingExpirationStartTimestamp();
|
2018-08-07 19:33:56 +00:00
|
|
|
}
|
|
|
|
|
2021-06-16 22:20:17 +00:00
|
|
|
function getSoonestMessageExpiry() {
|
|
|
|
return channels.getSoonestMessageExpiry();
|
2018-07-27 01:13:56 +00:00
|
|
|
}
|
2018-07-25 22:02:27 +00:00
|
|
|
|
2021-05-19 16:17:51 +00:00
|
|
|
async function getNextTapToViewMessageTimestampToAgeOut() {
|
|
|
|
return channels.getNextTapToViewMessageTimestampToAgeOut();
|
2019-06-26 19:33:13 +00:00
|
|
|
}
|
2021-12-10 22:51:54 +00:00
|
|
|
async function getTapToViewMessagesNeedingErase() {
|
|
|
|
return channels.getTapToViewMessagesNeedingErase();
|
2019-06-26 19:33:13 +00:00
|
|
|
}
|
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
// Unprocessed
|
|
|
|
|
2018-09-28 22:51:26 +00:00
|
|
|
async function getUnprocessedCount() {
|
2021-03-10 22:25:57 +00:00
|
|
|
return channels.getUnprocessedCount();
|
2018-09-28 22:51:26 +00:00
|
|
|
}
|
|
|
|
|
2018-07-27 01:13:56 +00:00
|
|
|
async function getAllUnprocessed() {
|
2021-03-10 22:25:57 +00:00
|
|
|
return channels.getAllUnprocessed();
|
2018-07-25 22:02:27 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 18:59:11 +00:00
|
|
|
async function getUnprocessedById(id: string) {
|
2021-03-10 22:25:57 +00:00
|
|
|
return channels.getUnprocessedById(id);
|
2018-07-25 22:02:27 +00:00
|
|
|
}
|
|
|
|
|
2021-04-16 23:13:13 +00:00
|
|
|
async function updateUnprocessedWithData(
|
|
|
|
id: string,
|
|
|
|
data: UnprocessedUpdateType
|
|
|
|
) {
|
2021-03-10 22:25:57 +00:00
|
|
|
await channels.updateUnprocessedWithData(id, data);
|
2019-02-05 01:23:50 +00:00
|
|
|
}
|
2021-04-05 22:18:19 +00:00
|
|
|
async function updateUnprocessedsWithData(
|
2021-04-16 23:13:13 +00:00
|
|
|
array: Array<{ id: string; data: UnprocessedUpdateType }>
|
2021-04-05 22:18:19 +00:00
|
|
|
) {
|
2021-03-10 22:25:57 +00:00
|
|
|
await channels.updateUnprocessedsWithData(array);
|
2019-09-26 19:56:31 +00:00
|
|
|
}
|
2019-02-05 01:23:50 +00:00
|
|
|
|
2021-02-26 23:42:45 +00:00
|
|
|
async function removeUnprocessed(id: string | Array<string>) {
|
2021-03-10 22:25:57 +00:00
|
|
|
await channels.removeUnprocessed(id);
|
2018-07-25 22:02:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function removeAllUnprocessed() {
|
2021-03-10 22:25:57 +00:00
|
|
|
await channels.removeAllUnprocessed();
|
2018-07-25 22:02:27 +00:00
|
|
|
}
|
|
|
|
|
2019-01-30 20:15:07 +00:00
|
|
|
// Attachment downloads
|
|
|
|
|
2020-04-01 18:59:11 +00:00
|
|
|
async function getNextAttachmentDownloadJobs(
|
|
|
|
limit?: number,
|
|
|
|
options?: { timestamp?: number }
|
|
|
|
) {
|
2021-03-10 22:25:57 +00:00
|
|
|
return channels.getNextAttachmentDownloadJobs(limit, options);
|
2019-01-30 20:15:07 +00:00
|
|
|
}
|
2020-04-01 18:59:11 +00:00
|
|
|
async function saveAttachmentDownloadJob(job: AttachmentDownloadJobType) {
|
2021-03-10 22:25:57 +00:00
|
|
|
await channels.saveAttachmentDownloadJob(_cleanData(job));
|
2019-01-30 20:15:07 +00:00
|
|
|
}
|
2020-04-01 18:59:11 +00:00
|
|
|
async function setAttachmentDownloadJobPending(id: string, pending: boolean) {
|
2021-03-10 22:25:57 +00:00
|
|
|
await channels.setAttachmentDownloadJobPending(id, pending);
|
2019-01-30 20:15:07 +00:00
|
|
|
}
|
|
|
|
async function resetAttachmentDownloadPending() {
|
2021-03-10 22:25:57 +00:00
|
|
|
await channels.resetAttachmentDownloadPending();
|
2019-01-30 20:15:07 +00:00
|
|
|
}
|
2020-04-01 18:59:11 +00:00
|
|
|
async function removeAttachmentDownloadJob(id: string) {
|
2021-03-10 22:25:57 +00:00
|
|
|
await channels.removeAttachmentDownloadJob(id);
|
2019-01-30 20:15:07 +00:00
|
|
|
}
|
|
|
|
async function removeAllAttachmentDownloadJobs() {
|
2021-03-10 22:25:57 +00:00
|
|
|
await channels.removeAllAttachmentDownloadJobs();
|
2019-01-30 20:15:07 +00:00
|
|
|
}
|
|
|
|
|
2019-05-16 22:32:11 +00:00
|
|
|
// Stickers
|
|
|
|
|
2020-04-01 18:59:11 +00:00
|
|
|
async function getStickerCount() {
|
2021-03-10 22:25:57 +00:00
|
|
|
return channels.getStickerCount();
|
2020-04-01 18:59:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function createOrUpdateStickerPack(pack: StickerPackType) {
|
2021-03-10 22:25:57 +00:00
|
|
|
await channels.createOrUpdateStickerPack(pack);
|
2019-05-16 22:32:11 +00:00
|
|
|
}
|
2020-04-01 18:59:11 +00:00
|
|
|
async function updateStickerPackStatus(
|
|
|
|
packId: string,
|
|
|
|
status: StickerPackStatusType,
|
|
|
|
options?: { timestamp: number }
|
|
|
|
) {
|
2021-03-10 22:25:57 +00:00
|
|
|
await channels.updateStickerPackStatus(packId, status, options);
|
2019-05-16 22:32:11 +00:00
|
|
|
}
|
2020-04-01 18:59:11 +00:00
|
|
|
async function createOrUpdateSticker(sticker: StickerType) {
|
2021-03-10 22:25:57 +00:00
|
|
|
await channels.createOrUpdateSticker(sticker);
|
2019-05-16 22:32:11 +00:00
|
|
|
}
|
2020-04-01 18:59:11 +00:00
|
|
|
async function updateStickerLastUsed(
|
|
|
|
packId: string,
|
|
|
|
stickerId: number,
|
|
|
|
timestamp: number
|
|
|
|
) {
|
2021-03-10 22:25:57 +00:00
|
|
|
await channels.updateStickerLastUsed(packId, stickerId, timestamp);
|
2019-05-16 22:32:11 +00:00
|
|
|
}
|
2020-04-01 18:59:11 +00:00
|
|
|
async function addStickerPackReference(messageId: string, packId: string) {
|
2021-03-10 22:25:57 +00:00
|
|
|
await channels.addStickerPackReference(messageId, packId);
|
2019-05-16 22:32:11 +00:00
|
|
|
}
|
2020-04-01 18:59:11 +00:00
|
|
|
async function deleteStickerPackReference(messageId: string, packId: string) {
|
2021-07-29 18:59:26 +00:00
|
|
|
return channels.deleteStickerPackReference(messageId, packId);
|
2019-05-16 22:32:11 +00:00
|
|
|
}
|
2020-04-01 18:59:11 +00:00
|
|
|
async function deleteStickerPack(packId: string) {
|
2021-03-10 22:25:57 +00:00
|
|
|
const paths = await channels.deleteStickerPack(packId);
|
2020-04-01 18:59:11 +00:00
|
|
|
|
2019-05-16 22:32:11 +00:00
|
|
|
return paths;
|
|
|
|
}
|
|
|
|
async function getAllStickerPacks() {
|
2021-03-10 22:25:57 +00:00
|
|
|
const packs = await channels.getAllStickerPacks();
|
2020-04-01 18:59:11 +00:00
|
|
|
|
2019-05-16 22:32:11 +00:00
|
|
|
return packs;
|
|
|
|
}
|
|
|
|
async function getAllStickers() {
|
2021-03-10 22:25:57 +00:00
|
|
|
const stickers = await channels.getAllStickers();
|
2020-04-01 18:59:11 +00:00
|
|
|
|
2019-05-16 22:32:11 +00:00
|
|
|
return stickers;
|
|
|
|
}
|
|
|
|
async function getRecentStickers() {
|
2021-03-10 22:25:57 +00:00
|
|
|
const recentStickers = await channels.getRecentStickers();
|
2020-04-01 18:59:11 +00:00
|
|
|
|
2019-05-16 22:32:11 +00:00
|
|
|
return recentStickers;
|
|
|
|
}
|
2021-01-27 22:39:45 +00:00
|
|
|
async function clearAllErrorStickerPackAttempts() {
|
2021-03-10 22:25:57 +00:00
|
|
|
await channels.clearAllErrorStickerPackAttempts();
|
2021-01-27 22:39:45 +00:00
|
|
|
}
|
2019-05-16 22:32:11 +00:00
|
|
|
|
2019-05-24 23:58:27 +00:00
|
|
|
// Emojis
|
2020-04-01 18:59:11 +00:00
|
|
|
async function updateEmojiUsage(shortName: string) {
|
2021-03-10 22:25:57 +00:00
|
|
|
await channels.updateEmojiUsage(shortName);
|
2019-05-24 23:58:27 +00:00
|
|
|
}
|
2020-09-24 21:53:21 +00:00
|
|
|
async function getRecentEmojis(limit = 32) {
|
2021-03-10 22:25:57 +00:00
|
|
|
return channels.getRecentEmojis(limit);
|
2019-05-24 23:58:27 +00:00
|
|
|
}
|
|
|
|
|
2021-11-02 23:01:13 +00:00
|
|
|
// Badges
|
|
|
|
|
|
|
|
function getAllBadges(): Promise<Array<BadgeType>> {
|
|
|
|
return channels.getAllBadges();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function updateOrCreateBadges(
|
|
|
|
badges: ReadonlyArray<BadgeType>
|
|
|
|
): Promise<void> {
|
|
|
|
if (badges.length) {
|
|
|
|
await channels.updateOrCreateBadges(badges);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function badgeImageFileDownloaded(
|
|
|
|
url: string,
|
|
|
|
localPath: string
|
|
|
|
): Promise<void> {
|
|
|
|
return channels.badgeImageFileDownloaded(url, localPath);
|
|
|
|
}
|
|
|
|
|
2021-12-08 19:52:46 +00:00
|
|
|
// Story Distributions
|
|
|
|
|
|
|
|
async function _getAllStoryDistributions(): Promise<
|
|
|
|
Array<StoryDistributionType>
|
|
|
|
> {
|
|
|
|
return channels._getAllStoryDistributions();
|
|
|
|
}
|
|
|
|
async function _getAllStoryDistributionMembers(): Promise<
|
|
|
|
Array<StoryDistributionMemberType>
|
|
|
|
> {
|
|
|
|
return channels._getAllStoryDistributionMembers();
|
|
|
|
}
|
|
|
|
async function _deleteAllStoryDistributions(): Promise<void> {
|
|
|
|
await channels._deleteAllStoryDistributions();
|
|
|
|
}
|
|
|
|
async function createNewStoryDistribution(
|
2021-12-10 02:15:59 +00:00
|
|
|
distribution: StoryDistributionWithMembersType
|
2021-12-08 19:52:46 +00:00
|
|
|
): Promise<void> {
|
2021-12-10 02:15:59 +00:00
|
|
|
await channels.createNewStoryDistribution(distribution);
|
2021-12-08 19:52:46 +00:00
|
|
|
}
|
|
|
|
async function getAllStoryDistributionsWithMembers(): Promise<
|
|
|
|
Array<StoryDistributionWithMembersType>
|
|
|
|
> {
|
|
|
|
return channels.getAllStoryDistributionsWithMembers();
|
|
|
|
}
|
2021-12-10 02:15:59 +00:00
|
|
|
async function modifyStoryDistribution(
|
|
|
|
distribution: StoryDistributionType
|
|
|
|
): Promise<void> {
|
|
|
|
await channels.modifyStoryDistribution(distribution);
|
|
|
|
}
|
2021-12-08 19:52:46 +00:00
|
|
|
async function modifyStoryDistributionMembers(
|
|
|
|
id: string,
|
|
|
|
options: {
|
|
|
|
toAdd: Array<UUIDStringType>;
|
|
|
|
toRemove: Array<UUIDStringType>;
|
|
|
|
}
|
|
|
|
): Promise<void> {
|
|
|
|
await channels.modifyStoryDistributionMembers(id, options);
|
|
|
|
}
|
|
|
|
async function deleteStoryDistribution(id: UUIDStringType): Promise<void> {
|
|
|
|
await channels.deleteStoryDistribution(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Story Reads
|
|
|
|
|
|
|
|
async function _getAllStoryReads(): Promise<Array<StoryReadType>> {
|
|
|
|
return channels._getAllStoryReads();
|
|
|
|
}
|
|
|
|
async function _deleteAllStoryReads(): Promise<void> {
|
|
|
|
await channels._deleteAllStoryReads();
|
|
|
|
}
|
|
|
|
async function addNewStoryRead(read: StoryReadType): Promise<void> {
|
|
|
|
return channels.addNewStoryRead(read);
|
|
|
|
}
|
|
|
|
async function getLastStoryReadsForAuthor(options: {
|
|
|
|
authorId: UUIDStringType;
|
|
|
|
conversationId?: UUIDStringType;
|
|
|
|
limit?: number;
|
|
|
|
}): Promise<Array<StoryReadType>> {
|
|
|
|
return channels.getLastStoryReadsForAuthor(options);
|
|
|
|
}
|
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
// Other
|
|
|
|
|
2018-07-25 22:02:27 +00:00
|
|
|
async function removeAll() {
|
2021-03-10 22:25:57 +00:00
|
|
|
await channels.removeAll();
|
2018-07-25 22:02:27 +00:00
|
|
|
}
|
|
|
|
|
2021-08-30 21:39:57 +00:00
|
|
|
async function removeAllConfiguration(type?: RemoveAllConfiguration) {
|
|
|
|
await channels.removeAllConfiguration(type);
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
|
|
|
|
2018-08-08 17:00:33 +00:00
|
|
|
async function cleanupOrphanedAttachments() {
|
|
|
|
await callChannel(CLEANUP_ORPHANED_ATTACHMENTS_KEY);
|
|
|
|
}
|
|
|
|
|
2020-02-21 23:40:04 +00:00
|
|
|
async function ensureFilePermissions() {
|
|
|
|
await callChannel(ENSURE_FILE_PERMISSIONS);
|
|
|
|
}
|
|
|
|
|
2018-07-27 01:13:56 +00:00
|
|
|
// Note: will need to restart the app after calling this, to set up afresh
|
|
|
|
async function removeOtherData() {
|
|
|
|
await Promise.all([
|
|
|
|
callChannel(ERASE_SQL_KEY),
|
|
|
|
callChannel(ERASE_ATTACHMENTS_KEY),
|
2019-05-16 22:32:11 +00:00
|
|
|
callChannel(ERASE_STICKERS_KEY),
|
2019-05-24 01:27:42 +00:00
|
|
|
callChannel(ERASE_TEMP_KEY),
|
2019-08-07 00:40:25 +00:00
|
|
|
callChannel(ERASE_DRAFTS_KEY),
|
2018-07-27 01:13:56 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2020-04-01 18:59:11 +00:00
|
|
|
async function callChannel(name: string) {
|
2021-09-27 18:22:46 +00:00
|
|
|
return createTaskWithTimeout(
|
|
|
|
() =>
|
|
|
|
new Promise<void>((resolve, reject) => {
|
2021-10-07 18:16:51 +00:00
|
|
|
ipc.send(name);
|
|
|
|
ipc.once(`${name}-done`, (_, error) => {
|
2021-09-27 18:22:46 +00:00
|
|
|
if (error) {
|
|
|
|
reject(error);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
}),
|
|
|
|
`callChannel call to ${name}`
|
|
|
|
)();
|
2018-07-27 01:13:56 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 18:59:11 +00:00
|
|
|
async function getMessagesNeedingUpgrade(
|
|
|
|
limit: number,
|
|
|
|
{ maxVersion = CURRENT_SCHEMA_VERSION }: { maxVersion: number }
|
|
|
|
) {
|
2021-03-10 22:25:57 +00:00
|
|
|
const messages = await channels.getMessagesNeedingUpgrade(limit, {
|
2020-04-01 18:59:11 +00:00
|
|
|
maxVersion,
|
|
|
|
});
|
2018-07-27 01:13:56 +00:00
|
|
|
|
2020-04-01 18:59:11 +00:00
|
|
|
return messages;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getMessagesWithVisualMediaAttachments(
|
|
|
|
conversationId: string,
|
|
|
|
{ limit }: { limit: number }
|
|
|
|
) {
|
2021-03-10 22:25:57 +00:00
|
|
|
return channels.getMessagesWithVisualMediaAttachments(conversationId, {
|
2020-04-01 18:59:11 +00:00
|
|
|
limit,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getMessagesWithFileAttachments(
|
|
|
|
conversationId: string,
|
|
|
|
{ limit }: { limit: number }
|
2018-07-27 02:19:34 +00:00
|
|
|
) {
|
2021-03-10 22:25:57 +00:00
|
|
|
return channels.getMessagesWithFileAttachments(conversationId, {
|
2020-04-01 18:59:11 +00:00
|
|
|
limit,
|
|
|
|
});
|
|
|
|
}
|
2021-04-29 23:02:27 +00:00
|
|
|
|
2021-05-27 20:17:05 +00:00
|
|
|
function getMessageServerGuidsForSpam(
|
|
|
|
conversationId: string
|
|
|
|
): Promise<Array<string>> {
|
|
|
|
return channels.getMessageServerGuidsForSpam(conversationId);
|
|
|
|
}
|
|
|
|
|
2021-04-29 23:02:27 +00:00
|
|
|
function getJobsInQueue(queueType: string): Promise<Array<StoredJob>> {
|
|
|
|
return channels.getJobsInQueue(queueType);
|
|
|
|
}
|
|
|
|
|
|
|
|
function insertJob(job: Readonly<StoredJob>): Promise<void> {
|
|
|
|
return channels.insertJob(job);
|
|
|
|
}
|
|
|
|
|
|
|
|
function deleteJob(id: string): Promise<void> {
|
|
|
|
return channels.deleteJob(id);
|
|
|
|
}
|
2021-05-28 16:15:17 +00:00
|
|
|
|
2021-08-20 16:06:15 +00:00
|
|
|
function processGroupCallRingRequest(
|
|
|
|
ringId: bigint
|
|
|
|
): Promise<ProcessGroupCallRingRequestResult> {
|
|
|
|
return channels.processGroupCallRingRequest(ringId);
|
|
|
|
}
|
|
|
|
|
|
|
|
function processGroupCallRingCancelation(ringId: bigint): Promise<void> {
|
|
|
|
return channels.processGroupCallRingCancelation(ringId);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function cleanExpiredGroupCallRings(): Promise<void> {
|
|
|
|
await channels.cleanExpiredGroupCallRings();
|
|
|
|
}
|
|
|
|
|
2021-05-28 16:15:17 +00:00
|
|
|
async function updateAllConversationColors(
|
|
|
|
conversationColor?: ConversationColorType,
|
|
|
|
customColorData?: {
|
|
|
|
id: string;
|
|
|
|
value: CustomColorType;
|
|
|
|
}
|
|
|
|
): Promise<void> {
|
|
|
|
return channels.updateAllConversationColors(
|
|
|
|
conversationColor,
|
|
|
|
customColorData
|
|
|
|
);
|
|
|
|
}
|
2021-07-30 16:43:16 +00:00
|
|
|
|
2021-09-15 18:45:22 +00:00
|
|
|
function getMaxMessageCounter(): Promise<number | undefined> {
|
|
|
|
return channels.getMaxMessageCounter();
|
|
|
|
}
|
|
|
|
|
2021-07-30 16:43:16 +00:00
|
|
|
function getStatisticsForLogging(): Promise<Record<string, string>> {
|
|
|
|
return channels.getStatisticsForLogging();
|
|
|
|
}
|