2018-10-18 01:01:21 +00:00
|
|
|
/* global window, setTimeout, IDBKeyRange */
|
2018-07-27 01:13:56 +00:00
|
|
|
|
|
|
|
const electron = require('electron');
|
2018-09-21 01:47:19 +00:00
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
const {
|
|
|
|
cloneDeep,
|
|
|
|
forEach,
|
|
|
|
get,
|
|
|
|
isFunction,
|
|
|
|
isObject,
|
|
|
|
map,
|
|
|
|
set,
|
|
|
|
} = require('lodash');
|
|
|
|
|
|
|
|
const { base64ToArrayBuffer, arrayBufferToBase64 } = require('./crypto');
|
2018-07-25 22:02:27 +00:00
|
|
|
const MessageType = require('./types/message');
|
|
|
|
|
2018-07-27 01:13:56 +00:00
|
|
|
const { ipcRenderer } = electron;
|
|
|
|
|
|
|
|
// We listen to a lot of events on ipcRenderer, often on the same channel. This prevents
|
|
|
|
// any warnings that might be sent to the console in that case.
|
|
|
|
ipcRenderer.setMaxListeners(0);
|
|
|
|
|
2018-08-08 19:15:45 +00:00
|
|
|
const DATABASE_UPDATE_TIMEOUT = 2 * 60 * 1000; // two minutes
|
|
|
|
|
2018-07-27 01:13:56 +00:00
|
|
|
const SQL_CHANNEL_KEY = 'sql-channel';
|
|
|
|
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';
|
2018-07-27 01:13:56 +00:00
|
|
|
|
|
|
|
const _jobs = Object.create(null);
|
|
|
|
const _DEBUG = false;
|
|
|
|
let _jobCounter = 0;
|
2018-11-05 19:06:12 +00:00
|
|
|
let _shuttingDown = false;
|
|
|
|
let _shutdownCallback = null;
|
|
|
|
let _shutdownPromise = null;
|
2018-07-27 01:13:56 +00:00
|
|
|
|
|
|
|
const channels = {};
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
_jobs,
|
|
|
|
_cleanData,
|
|
|
|
|
2018-11-05 19:06:12 +00:00
|
|
|
shutdown,
|
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,
|
|
|
|
getAllSignedPreKeys,
|
|
|
|
bulkAddSignedPreKeys,
|
|
|
|
removeSignedPreKeyById,
|
|
|
|
removeAllSignedPreKeys,
|
|
|
|
|
|
|
|
createOrUpdateItem,
|
|
|
|
getItemById,
|
|
|
|
getAllItems,
|
|
|
|
bulkAddItems,
|
|
|
|
removeItemById,
|
|
|
|
removeAllItems,
|
|
|
|
|
|
|
|
createOrUpdateSession,
|
|
|
|
getSessionById,
|
|
|
|
getSessionsByNumber,
|
|
|
|
bulkAddSessions,
|
|
|
|
removeSessionById,
|
|
|
|
removeSessionsByNumber,
|
|
|
|
removeAllSessions,
|
2019-02-04 23:54:37 +00:00
|
|
|
getAllSessions,
|
2018-10-18 01:01:21 +00:00
|
|
|
|
2018-09-21 01:47:19 +00:00
|
|
|
getConversationCount,
|
|
|
|
saveConversation,
|
|
|
|
saveConversations,
|
|
|
|
getConversationById,
|
|
|
|
updateConversation,
|
|
|
|
removeConversation,
|
|
|
|
_removeConversations,
|
|
|
|
|
|
|
|
getAllConversations,
|
|
|
|
getAllConversationIds,
|
|
|
|
getAllPrivateConversations,
|
|
|
|
getAllGroupsInvolvingId,
|
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,
|
2018-08-01 19:38:48 +00:00
|
|
|
saveLegacyMessage,
|
2018-07-27 01:13:56 +00:00
|
|
|
saveMessages,
|
|
|
|
removeMessage,
|
2018-08-01 02:51:17 +00:00
|
|
|
_removeMessages,
|
2018-07-27 01:13:56 +00:00
|
|
|
getUnreadByConversation,
|
|
|
|
|
|
|
|
removeAllMessagesInConversation,
|
|
|
|
|
|
|
|
getMessageBySender,
|
|
|
|
getMessageById,
|
2018-09-21 01:47:19 +00:00
|
|
|
getAllMessages,
|
2018-07-27 01:13:56 +00:00
|
|
|
getAllMessageIds,
|
|
|
|
getMessagesBySentAt,
|
|
|
|
getExpiredMessages,
|
2018-08-07 19:33:56 +00:00
|
|
|
getOutgoingWithoutExpiresAt,
|
2018-07-27 01:13:56 +00:00
|
|
|
getNextExpiringMessage,
|
2019-06-26 19:33:13 +00:00
|
|
|
getNextTapToViewMessageToAgeOut,
|
|
|
|
getTapToViewMessagesNeedingErase,
|
2019-05-31 22:42:01 +00:00
|
|
|
getOlderMessagesByConversation,
|
|
|
|
getNewerMessagesByConversation,
|
|
|
|
getMessageMetricsForConversation,
|
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,
|
|
|
|
saveUnprocessed,
|
|
|
|
saveUnprocesseds,
|
2019-02-05 01:23:50 +00:00
|
|
|
updateUnprocessedAttempts,
|
|
|
|
updateUnprocessedWithData,
|
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,
|
|
|
|
deleteStickerPack,
|
|
|
|
getAllStickerPacks,
|
|
|
|
getAllStickers,
|
|
|
|
getRecentStickers,
|
|
|
|
|
2019-05-24 23:58:27 +00:00
|
|
|
updateEmojiUsage,
|
|
|
|
getRecentEmojis,
|
|
|
|
|
2018-07-27 01:13:56 +00:00
|
|
|
removeAll,
|
2018-10-18 01:01:21 +00:00
|
|
|
removeAllConfiguration,
|
|
|
|
|
2018-07-27 01:13:56 +00:00
|
|
|
removeOtherData,
|
2018-08-08 17:00:33 +00:00
|
|
|
cleanupOrphanedAttachments,
|
2018-07-27 01:13:56 +00:00
|
|
|
|
|
|
|
// Returning plain JSON
|
|
|
|
getMessagesNeedingUpgrade,
|
|
|
|
getLegacyMessagesNeedingUpgrade,
|
|
|
|
getMessagesWithVisualMediaAttachments,
|
|
|
|
getMessagesWithFileAttachments,
|
|
|
|
};
|
|
|
|
|
|
|
|
// When IPC arguments are prepared for the cross-process send, they are JSON.stringified.
|
|
|
|
// We can't send ArrayBuffers or BigNumbers (what we get from proto library for dates).
|
|
|
|
function _cleanData(data) {
|
|
|
|
const keys = Object.keys(data);
|
|
|
|
for (let index = 0, max = keys.length; index < max; index += 1) {
|
|
|
|
const key = keys[index];
|
|
|
|
const value = data[key];
|
|
|
|
|
|
|
|
if (value === null || value === undefined) {
|
|
|
|
// eslint-disable-next-line no-continue
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isFunction(value.toNumber)) {
|
|
|
|
// eslint-disable-next-line no-param-reassign
|
|
|
|
data[key] = value.toNumber();
|
|
|
|
} else if (Array.isArray(value)) {
|
|
|
|
// eslint-disable-next-line no-param-reassign
|
|
|
|
data[key] = value.map(item => _cleanData(item));
|
|
|
|
} else if (isObject(value)) {
|
|
|
|
// eslint-disable-next-line no-param-reassign
|
|
|
|
data[key] = _cleanData(value);
|
|
|
|
} else if (
|
|
|
|
typeof value !== 'string' &&
|
|
|
|
typeof value !== 'number' &&
|
|
|
|
typeof value !== 'boolean'
|
|
|
|
) {
|
|
|
|
window.log.info(`_cleanData: key ${key} had type ${typeof value}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2018-11-05 19:06:12 +00:00
|
|
|
async function _shutdown() {
|
|
|
|
if (_shutdownPromise) {
|
|
|
|
return _shutdownPromise;
|
|
|
|
}
|
|
|
|
|
|
|
|
_shuttingDown = true;
|
|
|
|
|
|
|
|
const jobKeys = Object.keys(_jobs);
|
|
|
|
window.log.info(
|
|
|
|
`data.shutdown: starting process. ${jobKeys.length} jobs outstanding`
|
|
|
|
);
|
|
|
|
|
|
|
|
// No outstanding jobs, return immediately
|
|
|
|
if (jobKeys.length === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Outstanding jobs; we need to wait until the last one is done
|
|
|
|
_shutdownPromise = new Promise((resolve, reject) => {
|
|
|
|
_shutdownCallback = error => {
|
|
|
|
window.log.info('data.shutdown: process complete');
|
|
|
|
if (error) {
|
|
|
|
return reject(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
return resolve();
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
return _shutdownPromise;
|
|
|
|
}
|
|
|
|
|
2018-07-27 01:13:56 +00:00
|
|
|
function _makeJob(fnName) {
|
2018-11-05 19:06:12 +00:00
|
|
|
if (_shuttingDown && fnName !== 'close') {
|
|
|
|
throw new Error(
|
|
|
|
`Rejecting SQL channel job (${fnName}); application is shutting down`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-07-27 01:13:56 +00:00
|
|
|
_jobCounter += 1;
|
|
|
|
const id = _jobCounter;
|
|
|
|
|
2018-08-10 19:19:40 +00:00
|
|
|
if (_DEBUG) {
|
|
|
|
window.log.info(`SQL channel job ${id} (${fnName}) started`);
|
|
|
|
}
|
2018-07-27 01:13:56 +00:00
|
|
|
_jobs[id] = {
|
|
|
|
fnName,
|
2018-08-09 01:32:10 +00:00
|
|
|
start: Date.now(),
|
2018-07-27 01:13:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
function _updateJob(id, data) {
|
|
|
|
const { resolve, reject } = data;
|
2018-08-09 01:32:10 +00:00
|
|
|
const { fnName, start } = _jobs[id];
|
2018-07-27 01:13:56 +00:00
|
|
|
|
|
|
|
_jobs[id] = {
|
|
|
|
..._jobs[id],
|
|
|
|
...data,
|
|
|
|
resolve: value => {
|
|
|
|
_removeJob(id);
|
2018-08-09 01:32:10 +00:00
|
|
|
const end = Date.now();
|
2018-10-31 22:43:36 +00:00
|
|
|
const delta = end - start;
|
|
|
|
if (delta > 10) {
|
|
|
|
window.log.info(
|
|
|
|
`SQL channel job ${id} (${fnName}) succeeded in ${end - start}ms`
|
|
|
|
);
|
|
|
|
}
|
2018-07-27 01:13:56 +00:00
|
|
|
return resolve(value);
|
|
|
|
},
|
|
|
|
reject: error => {
|
|
|
|
_removeJob(id);
|
2018-08-09 01:32:10 +00:00
|
|
|
const end = Date.now();
|
|
|
|
window.log.info(
|
|
|
|
`SQL channel job ${id} (${fnName}) failed in ${end - start}ms`
|
|
|
|
);
|
2018-07-27 01:13:56 +00:00
|
|
|
return reject(error);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function _removeJob(id) {
|
|
|
|
if (_DEBUG) {
|
|
|
|
_jobs[id].complete = true;
|
2018-11-05 19:06:12 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete _jobs[id];
|
|
|
|
|
|
|
|
if (_shutdownCallback) {
|
|
|
|
const keys = Object.keys(_jobs);
|
|
|
|
if (keys.length === 0) {
|
|
|
|
_shutdownCallback();
|
|
|
|
}
|
2018-07-27 01:13:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function _getJob(id) {
|
|
|
|
return _jobs[id];
|
|
|
|
}
|
|
|
|
|
|
|
|
ipcRenderer.on(
|
|
|
|
`${SQL_CHANNEL_KEY}-done`,
|
|
|
|
(event, jobId, errorForDisplay, result) => {
|
|
|
|
const job = _getJob(jobId);
|
|
|
|
if (!job) {
|
|
|
|
throw new Error(
|
2018-08-16 17:45:09 +00:00
|
|
|
`Received SQL channel reply to job ${jobId}, but did not have it in our registry!`
|
2018-07-27 01:13:56 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const { resolve, reject, fnName } = job;
|
|
|
|
|
|
|
|
if (errorForDisplay) {
|
|
|
|
return reject(
|
2018-08-16 17:45:09 +00:00
|
|
|
new Error(
|
|
|
|
`Error received from SQL channel job ${jobId} (${fnName}): ${errorForDisplay}`
|
|
|
|
)
|
2018-07-27 01:13:56 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return resolve(result);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
function makeChannel(fnName) {
|
|
|
|
channels[fnName] = (...args) => {
|
|
|
|
const jobId = _makeJob(fnName);
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
ipcRenderer.send(SQL_CHANNEL_KEY, jobId, fnName, ...args);
|
|
|
|
|
|
|
|
_updateJob(jobId, {
|
|
|
|
resolve,
|
|
|
|
reject,
|
|
|
|
args: _DEBUG ? args : null,
|
|
|
|
});
|
|
|
|
|
|
|
|
setTimeout(
|
2018-08-16 17:45:09 +00:00
|
|
|
() =>
|
|
|
|
reject(new Error(`SQL channel job ${jobId} (${fnName}) timed out`)),
|
2018-08-08 19:15:45 +00:00
|
|
|
DATABASE_UPDATE_TIMEOUT
|
2018-07-27 01:13:56 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
forEach(module.exports, fn => {
|
|
|
|
if (isFunction(fn)) {
|
|
|
|
makeChannel(fn.name);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
function keysToArrayBuffer(keys, data) {
|
|
|
|
const updated = cloneDeep(data);
|
|
|
|
for (let i = 0, max = keys.length; i < max; i += 1) {
|
|
|
|
const key = keys[i];
|
|
|
|
const value = get(data, key);
|
|
|
|
|
|
|
|
if (value) {
|
|
|
|
set(updated, key, base64ToArrayBuffer(value));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return updated;
|
|
|
|
}
|
|
|
|
|
|
|
|
function keysFromArrayBuffer(keys, data) {
|
|
|
|
const updated = cloneDeep(data);
|
|
|
|
for (let i = 0, max = keys.length; i < max; i += 1) {
|
|
|
|
const key = keys[i];
|
|
|
|
const value = get(data, key);
|
|
|
|
|
|
|
|
if (value) {
|
|
|
|
set(updated, key, arrayBufferToBase64(value));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return updated;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Top-level calls
|
|
|
|
|
2018-11-05 19:06:12 +00:00
|
|
|
async function shutdown() {
|
|
|
|
// Stop accepting new SQL jobs, flush outstanding queue
|
|
|
|
await _shutdown();
|
|
|
|
|
|
|
|
// 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() {
|
|
|
|
await channels.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note: will need to restart the app after calling this, to set up afresh
|
|
|
|
async function removeDB() {
|
|
|
|
await channels.removeDB();
|
|
|
|
}
|
|
|
|
|
2018-11-01 23:40:19 +00:00
|
|
|
async function removeIndexedDBFiles() {
|
|
|
|
await channels.removeIndexedDBFiles();
|
|
|
|
}
|
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
// Identity Keys
|
|
|
|
|
|
|
|
const IDENTITY_KEY_KEYS = ['publicKey'];
|
|
|
|
async function createOrUpdateIdentityKey(data) {
|
|
|
|
const updated = keysFromArrayBuffer(IDENTITY_KEY_KEYS, data);
|
|
|
|
await channels.createOrUpdateIdentityKey(updated);
|
|
|
|
}
|
|
|
|
async function getIdentityKeyById(id) {
|
|
|
|
const data = await channels.getIdentityKeyById(id);
|
|
|
|
return keysToArrayBuffer(IDENTITY_KEY_KEYS, data);
|
|
|
|
}
|
|
|
|
async function bulkAddIdentityKeys(array) {
|
|
|
|
const updated = map(array, data =>
|
|
|
|
keysFromArrayBuffer(IDENTITY_KEY_KEYS, data)
|
|
|
|
);
|
|
|
|
await channels.bulkAddIdentityKeys(updated);
|
|
|
|
}
|
|
|
|
async function removeIdentityKeyById(id) {
|
|
|
|
await channels.removeIdentityKeyById(id);
|
|
|
|
}
|
|
|
|
async function removeAllIdentityKeys() {
|
|
|
|
await channels.removeAllIdentityKeys();
|
|
|
|
}
|
2019-02-04 23:54:37 +00:00
|
|
|
async function getAllIdentityKeys() {
|
|
|
|
const keys = await channels.getAllIdentityKeys();
|
|
|
|
return keys.map(key => keysToArrayBuffer(IDENTITY_KEY_KEYS, key));
|
|
|
|
}
|
2018-10-18 01:01:21 +00:00
|
|
|
|
|
|
|
// Pre Keys
|
|
|
|
|
|
|
|
async function createOrUpdatePreKey(data) {
|
|
|
|
const updated = keysFromArrayBuffer(PRE_KEY_KEYS, data);
|
|
|
|
await channels.createOrUpdatePreKey(updated);
|
|
|
|
}
|
|
|
|
async function getPreKeyById(id) {
|
|
|
|
const data = await channels.getPreKeyById(id);
|
|
|
|
return keysToArrayBuffer(PRE_KEY_KEYS, data);
|
|
|
|
}
|
|
|
|
async function bulkAddPreKeys(array) {
|
|
|
|
const updated = map(array, data => keysFromArrayBuffer(PRE_KEY_KEYS, data));
|
|
|
|
await channels.bulkAddPreKeys(updated);
|
|
|
|
}
|
|
|
|
async function removePreKeyById(id) {
|
|
|
|
await channels.removePreKeyById(id);
|
|
|
|
}
|
|
|
|
async function removeAllPreKeys() {
|
|
|
|
await channels.removeAllPreKeys();
|
|
|
|
}
|
2019-02-04 23:54:37 +00:00
|
|
|
async function getAllPreKeys() {
|
|
|
|
const keys = await channels.getAllPreKeys();
|
|
|
|
return keys.map(key => keysToArrayBuffer(PRE_KEY_KEYS, key));
|
|
|
|
}
|
2018-10-18 01:01:21 +00:00
|
|
|
|
|
|
|
// Signed Pre Keys
|
|
|
|
|
|
|
|
const PRE_KEY_KEYS = ['privateKey', 'publicKey'];
|
|
|
|
async function createOrUpdateSignedPreKey(data) {
|
|
|
|
const updated = keysFromArrayBuffer(PRE_KEY_KEYS, data);
|
|
|
|
await channels.createOrUpdateSignedPreKey(updated);
|
|
|
|
}
|
|
|
|
async function getSignedPreKeyById(id) {
|
|
|
|
const data = await channels.getSignedPreKeyById(id);
|
|
|
|
return keysToArrayBuffer(PRE_KEY_KEYS, data);
|
|
|
|
}
|
|
|
|
async function getAllSignedPreKeys() {
|
|
|
|
const keys = await channels.getAllSignedPreKeys();
|
2019-02-04 23:54:37 +00:00
|
|
|
return keys.map(key => keysToArrayBuffer(PRE_KEY_KEYS, key));
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
|
|
|
async function bulkAddSignedPreKeys(array) {
|
|
|
|
const updated = map(array, data => keysFromArrayBuffer(PRE_KEY_KEYS, data));
|
|
|
|
await channels.bulkAddSignedPreKeys(updated);
|
|
|
|
}
|
|
|
|
async function removeSignedPreKeyById(id) {
|
|
|
|
await channels.removeSignedPreKeyById(id);
|
|
|
|
}
|
|
|
|
async function removeAllSignedPreKeys() {
|
|
|
|
await channels.removeAllSignedPreKeys();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Items
|
|
|
|
|
|
|
|
const ITEM_KEYS = {
|
|
|
|
identityKey: ['value.pubKey', 'value.privKey'],
|
|
|
|
senderCertificate: [
|
|
|
|
'value.certificate',
|
|
|
|
'value.signature',
|
|
|
|
'value.serialized',
|
|
|
|
],
|
|
|
|
signaling_key: ['value'],
|
|
|
|
profileKey: ['value'],
|
|
|
|
};
|
|
|
|
async function createOrUpdateItem(data) {
|
|
|
|
const { id } = data;
|
|
|
|
if (!id) {
|
|
|
|
throw new Error(
|
|
|
|
'createOrUpdateItem: Provided data did not have a truthy id'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const keys = ITEM_KEYS[id];
|
|
|
|
const updated = Array.isArray(keys) ? keysFromArrayBuffer(keys, data) : data;
|
|
|
|
|
|
|
|
await channels.createOrUpdateItem(updated);
|
|
|
|
}
|
|
|
|
async function getItemById(id) {
|
|
|
|
const keys = ITEM_KEYS[id];
|
|
|
|
const data = await channels.getItemById(id);
|
|
|
|
|
|
|
|
return Array.isArray(keys) ? keysToArrayBuffer(keys, data) : data;
|
|
|
|
}
|
|
|
|
async function getAllItems() {
|
|
|
|
const items = await channels.getAllItems();
|
|
|
|
return map(items, item => {
|
|
|
|
const { id } = item;
|
|
|
|
const keys = ITEM_KEYS[id];
|
|
|
|
return Array.isArray(keys) ? keysToArrayBuffer(keys, item) : item;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
async function bulkAddItems(array) {
|
|
|
|
const updated = map(array, data => {
|
|
|
|
const { id } = data;
|
|
|
|
const keys = ITEM_KEYS[id];
|
|
|
|
return Array.isArray(keys) ? keysFromArrayBuffer(keys, data) : data;
|
|
|
|
});
|
|
|
|
await channels.bulkAddItems(updated);
|
|
|
|
}
|
|
|
|
async function removeItemById(id) {
|
|
|
|
await channels.removeItemById(id);
|
|
|
|
}
|
|
|
|
async function removeAllItems() {
|
|
|
|
await channels.removeAllItems();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sessions
|
|
|
|
|
|
|
|
async function createOrUpdateSession(data) {
|
|
|
|
await channels.createOrUpdateSession(data);
|
|
|
|
}
|
|
|
|
async function getSessionById(id) {
|
|
|
|
const session = await channels.getSessionById(id);
|
|
|
|
return session;
|
|
|
|
}
|
|
|
|
async function getSessionsByNumber(number) {
|
|
|
|
const sessions = await channels.getSessionsByNumber(number);
|
|
|
|
return sessions;
|
|
|
|
}
|
|
|
|
async function bulkAddSessions(array) {
|
|
|
|
await channels.bulkAddSessions(array);
|
|
|
|
}
|
|
|
|
async function removeSessionById(id) {
|
|
|
|
await channels.removeSessionById(id);
|
|
|
|
}
|
|
|
|
async function removeSessionsByNumber(number) {
|
|
|
|
await channels.removeSessionsByNumber(number);
|
|
|
|
}
|
|
|
|
async function removeAllSessions(id) {
|
|
|
|
await channels.removeAllSessions(id);
|
|
|
|
}
|
2019-02-04 23:54:37 +00:00
|
|
|
async function getAllSessions(id) {
|
|
|
|
const sessions = await channels.getAllSessions(id);
|
|
|
|
return sessions;
|
|
|
|
}
|
2018-10-18 01:01:21 +00:00
|
|
|
|
|
|
|
// Conversation
|
|
|
|
|
2018-09-21 01:47:19 +00:00
|
|
|
async function getConversationCount() {
|
|
|
|
return channels.getConversationCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function saveConversation(data) {
|
|
|
|
await channels.saveConversation(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function saveConversations(data) {
|
|
|
|
await channels.saveConversations(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getConversationById(id, { Conversation }) {
|
|
|
|
const data = await channels.getConversationById(id);
|
|
|
|
return new Conversation(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function updateConversation(id, data, { Conversation }) {
|
|
|
|
const existing = await getConversationById(id, { Conversation });
|
|
|
|
if (!existing) {
|
|
|
|
throw new Error(`Conversation ${id} does not exist!`);
|
|
|
|
}
|
|
|
|
|
2019-08-07 00:40:25 +00:00
|
|
|
const merged = {
|
|
|
|
...existing.attributes,
|
|
|
|
...data,
|
|
|
|
};
|
2018-09-21 01:47:19 +00:00
|
|
|
await channels.updateConversation(merged);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function removeConversation(id, { Conversation }) {
|
|
|
|
const existing = await getConversationById(id, { Conversation });
|
|
|
|
|
|
|
|
// 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 existing.cleanup();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note: this method will not clean up external files, just delete from SQL
|
|
|
|
async function _removeConversations(ids) {
|
|
|
|
await channels.removeConversation(ids);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getAllConversations({ ConversationCollection }) {
|
|
|
|
const conversations = await channels.getAllConversations();
|
|
|
|
|
|
|
|
const collection = new ConversationCollection();
|
|
|
|
collection.add(conversations);
|
|
|
|
return collection;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getAllConversationIds() {
|
|
|
|
const ids = await channels.getAllConversationIds();
|
|
|
|
return ids;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getAllPrivateConversations({ ConversationCollection }) {
|
|
|
|
const conversations = await channels.getAllPrivateConversations();
|
|
|
|
|
|
|
|
const collection = new ConversationCollection();
|
|
|
|
collection.add(conversations);
|
|
|
|
return collection;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getAllGroupsInvolvingId(id, { ConversationCollection }) {
|
|
|
|
const conversations = await channels.getAllGroupsInvolvingId(id);
|
|
|
|
|
|
|
|
const collection = new ConversationCollection();
|
|
|
|
collection.add(conversations);
|
|
|
|
return collection;
|
|
|
|
}
|
|
|
|
|
2019-01-14 21:49:58 +00:00
|
|
|
async function searchConversations(query) {
|
2018-09-21 01:47:19 +00:00
|
|
|
const conversations = await channels.searchConversations(query);
|
2019-01-14 21:49:58 +00:00
|
|
|
return conversations;
|
|
|
|
}
|
2018-09-21 01:47:19 +00:00
|
|
|
|
2019-08-09 23:12:29 +00:00
|
|
|
function handleSearchMessageJSON(messages) {
|
|
|
|
return messages.map(message => ({
|
|
|
|
...JSON.parse(message.json),
|
|
|
|
snippet: message.snippet,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2019-01-14 21:49:58 +00:00
|
|
|
async function searchMessages(query, { limit } = {}) {
|
|
|
|
const messages = await channels.searchMessages(query, { limit });
|
2019-08-09 23:12:29 +00:00
|
|
|
return handleSearchMessageJSON(messages);
|
2019-01-14 21:49:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function searchMessagesInConversation(
|
|
|
|
query,
|
|
|
|
conversationId,
|
|
|
|
{ limit } = {}
|
|
|
|
) {
|
|
|
|
const messages = await channels.searchMessagesInConversation(
|
|
|
|
query,
|
|
|
|
conversationId,
|
|
|
|
{ limit }
|
|
|
|
);
|
2019-08-09 23:12:29 +00:00
|
|
|
return handleSearchMessageJSON(messages);
|
2018-09-21 01:47:19 +00:00
|
|
|
}
|
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
// Message
|
|
|
|
|
2018-08-06 23:18:58 +00:00
|
|
|
async function getMessageCount() {
|
|
|
|
return channels.getMessageCount();
|
|
|
|
}
|
|
|
|
|
2018-08-03 04:12:27 +00:00
|
|
|
async function saveMessage(data, { forceSave, Message } = {}) {
|
2018-07-27 01:13:56 +00:00
|
|
|
const id = await channels.saveMessage(_cleanData(data), { forceSave });
|
2019-06-26 19:33:13 +00:00
|
|
|
Message.updateTimers();
|
2018-07-27 01:13:56 +00:00
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
async function saveLegacyMessage(data) {
|
|
|
|
const db = await window.Whisper.Database.open();
|
|
|
|
try {
|
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
const transaction = db.transaction('messages', 'readwrite');
|
|
|
|
|
|
|
|
transaction.onerror = () => {
|
|
|
|
window.Whisper.Database.handleDOMException(
|
|
|
|
'saveLegacyMessage transaction error',
|
|
|
|
transaction.error,
|
|
|
|
reject
|
|
|
|
);
|
|
|
|
};
|
|
|
|
transaction.oncomplete = resolve;
|
|
|
|
|
|
|
|
const store = transaction.objectStore('messages');
|
|
|
|
|
|
|
|
if (!data.id) {
|
|
|
|
// eslint-disable-next-line no-param-reassign
|
|
|
|
data.id = window.getGuid();
|
|
|
|
}
|
|
|
|
|
|
|
|
const request = store.put(data, data.id);
|
|
|
|
request.onsuccess = resolve;
|
|
|
|
request.onerror = () => {
|
|
|
|
window.Whisper.Database.handleDOMException(
|
|
|
|
'saveLegacyMessage request error',
|
|
|
|
request.error,
|
|
|
|
reject
|
|
|
|
);
|
|
|
|
};
|
|
|
|
});
|
|
|
|
} finally {
|
|
|
|
db.close();
|
|
|
|
}
|
2018-08-01 19:38:48 +00:00
|
|
|
}
|
|
|
|
|
2018-07-27 01:13:56 +00:00
|
|
|
async function saveMessages(arrayOfMessages, { forceSave } = {}) {
|
|
|
|
await channels.saveMessages(_cleanData(arrayOfMessages), { forceSave });
|
2018-07-25 22:02:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function removeMessage(id, { Message }) {
|
|
|
|
const message = await getMessageById(id, { Message });
|
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) {
|
2018-07-27 01:13:56 +00:00
|
|
|
await channels.removeMessage(id);
|
2018-08-17 00:28:12 +00:00
|
|
|
await message.cleanup();
|
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
|
|
|
|
async function _removeMessages(ids) {
|
|
|
|
await channels.removeMessage(ids);
|
|
|
|
}
|
|
|
|
|
2018-07-25 22:02:27 +00:00
|
|
|
async function getMessageById(id, { Message }) {
|
2018-07-27 01:13:56 +00:00
|
|
|
const message = await channels.getMessageById(id);
|
2018-08-09 23:09:41 +00:00
|
|
|
if (!message) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-07-27 01:13:56 +00:00
|
|
|
return new Message(message);
|
2018-09-21 01:47:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// For testing only
|
|
|
|
async function getAllMessages({ MessageCollection }) {
|
|
|
|
const messages = await channels.getAllMessages();
|
|
|
|
return new MessageCollection(messages);
|
2018-07-25 22:02:27 +00:00
|
|
|
}
|
|
|
|
|
2018-07-27 01:13:56 +00:00
|
|
|
async function getAllMessageIds() {
|
|
|
|
const ids = await channels.getAllMessageIds();
|
|
|
|
return ids;
|
2018-07-25 22:02:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function getMessageBySender(
|
|
|
|
// eslint-disable-next-line camelcase
|
|
|
|
{ source, sourceDevice, sent_at },
|
|
|
|
{ Message }
|
|
|
|
) {
|
2018-07-27 01:13:56 +00:00
|
|
|
const messages = await channels.getMessageBySender({
|
|
|
|
source,
|
|
|
|
sourceDevice,
|
|
|
|
sent_at,
|
|
|
|
});
|
|
|
|
if (!messages || !messages.length) {
|
2018-07-25 22:02:27 +00:00
|
|
|
return null;
|
|
|
|
}
|
2018-07-27 01:13:56 +00:00
|
|
|
|
|
|
|
return new Message(messages[0]);
|
2018-07-25 22:02:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function getUnreadByConversation(conversationId, { MessageCollection }) {
|
2018-07-27 01:13:56 +00:00
|
|
|
const messages = await channels.getUnreadByConversation(conversationId);
|
|
|
|
return new MessageCollection(messages);
|
2018-07-25 22:02:27 +00:00
|
|
|
}
|
|
|
|
|
2019-08-09 23:12:29 +00:00
|
|
|
function handleMessageJSON(messages) {
|
|
|
|
return messages.map(message => JSON.parse(message.json));
|
|
|
|
}
|
|
|
|
|
2019-05-31 22:42:01 +00:00
|
|
|
async function getOlderMessagesByConversation(
|
2018-07-25 22:02:27 +00:00
|
|
|
conversationId,
|
|
|
|
{ limit = 100, receivedAt = Number.MAX_VALUE, MessageCollection }
|
|
|
|
) {
|
2019-05-31 22:42:01 +00:00
|
|
|
const messages = await channels.getOlderMessagesByConversation(
|
|
|
|
conversationId,
|
|
|
|
{
|
|
|
|
limit,
|
|
|
|
receivedAt,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2019-08-09 23:12:29 +00:00
|
|
|
return new MessageCollection(handleMessageJSON(messages));
|
2019-05-31 22:42:01 +00:00
|
|
|
}
|
|
|
|
async function getNewerMessagesByConversation(
|
|
|
|
conversationId,
|
|
|
|
{ limit = 100, receivedAt = 0, MessageCollection }
|
|
|
|
) {
|
|
|
|
const messages = await channels.getNewerMessagesByConversation(
|
|
|
|
conversationId,
|
|
|
|
{
|
|
|
|
limit,
|
|
|
|
receivedAt,
|
|
|
|
}
|
|
|
|
);
|
2018-07-25 22:02:27 +00:00
|
|
|
|
2019-08-09 23:12:29 +00:00
|
|
|
return new MessageCollection(handleMessageJSON(messages));
|
2018-07-25 22:02:27 +00:00
|
|
|
}
|
2019-05-31 22:42:01 +00:00
|
|
|
async function getMessageMetricsForConversation(conversationId) {
|
|
|
|
const result = await channels.getMessageMetricsForConversation(
|
|
|
|
conversationId
|
|
|
|
);
|
|
|
|
return result;
|
|
|
|
}
|
2018-07-25 22:02:27 +00:00
|
|
|
|
|
|
|
async function removeAllMessagesInConversation(
|
|
|
|
conversationId,
|
|
|
|
{ MessageCollection }
|
|
|
|
) {
|
2018-07-27 01:13:56 +00:00
|
|
|
let messages;
|
2018-07-25 22:02:27 +00:00
|
|
|
do {
|
|
|
|
// Yes, we really want the await in the loop. We're deleting 100 at a
|
|
|
|
// time so we don't use too much memory.
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
2019-05-31 22:42:01 +00:00
|
|
|
messages = await getOlderMessagesByConversation(conversationId, {
|
2018-07-27 01:13:56 +00:00
|
|
|
limit: 100,
|
|
|
|
MessageCollection,
|
|
|
|
});
|
2018-07-25 22:02:27 +00:00
|
|
|
|
2018-07-27 01:13:56 +00:00
|
|
|
if (!messages.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ids = messages.map(message => message.id);
|
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.
|
2018-07-27 01:13:56 +00:00
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
|
await Promise.all(messages.map(message => message.cleanup()));
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
|
await channels.removeMessage(ids);
|
|
|
|
} while (messages.length > 0);
|
2018-07-25 22:02:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function getMessagesBySentAt(sentAt, { MessageCollection }) {
|
2018-07-27 01:13:56 +00:00
|
|
|
const messages = await channels.getMessagesBySentAt(sentAt);
|
|
|
|
return new MessageCollection(messages);
|
2018-07-25 22:02:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function getExpiredMessages({ MessageCollection }) {
|
2018-07-27 01:13:56 +00:00
|
|
|
const messages = await channels.getExpiredMessages();
|
|
|
|
return new MessageCollection(messages);
|
2018-07-25 22:02:27 +00:00
|
|
|
}
|
|
|
|
|
2018-08-07 19:33:56 +00:00
|
|
|
async function getOutgoingWithoutExpiresAt({ MessageCollection }) {
|
|
|
|
const messages = await channels.getOutgoingWithoutExpiresAt();
|
|
|
|
return new MessageCollection(messages);
|
|
|
|
}
|
|
|
|
|
2018-07-25 22:02:27 +00:00
|
|
|
async function getNextExpiringMessage({ MessageCollection }) {
|
2018-07-27 01:13:56 +00:00
|
|
|
const messages = await channels.getNextExpiringMessage();
|
|
|
|
return new MessageCollection(messages);
|
|
|
|
}
|
2018-07-25 22:02:27 +00:00
|
|
|
|
2019-06-26 19:33:13 +00:00
|
|
|
async function getNextTapToViewMessageToAgeOut({ Message }) {
|
|
|
|
const message = await channels.getNextTapToViewMessageToAgeOut();
|
|
|
|
if (!message) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Message(message);
|
|
|
|
}
|
|
|
|
async function getTapToViewMessagesNeedingErase({ MessageCollection }) {
|
|
|
|
const messages = await channels.getTapToViewMessagesNeedingErase();
|
|
|
|
return new MessageCollection(messages);
|
|
|
|
}
|
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
// Unprocessed
|
|
|
|
|
2018-09-28 22:51:26 +00:00
|
|
|
async function getUnprocessedCount() {
|
|
|
|
return channels.getUnprocessedCount();
|
|
|
|
}
|
|
|
|
|
2018-07-27 01:13:56 +00:00
|
|
|
async function getAllUnprocessed() {
|
|
|
|
return channels.getAllUnprocessed();
|
2018-07-25 22:02:27 +00:00
|
|
|
}
|
|
|
|
|
2019-02-05 01:23:50 +00:00
|
|
|
async function getUnprocessedById(id) {
|
|
|
|
return channels.getUnprocessedById(id);
|
2018-07-25 22:02:27 +00:00
|
|
|
}
|
|
|
|
|
2018-07-27 01:13:56 +00:00
|
|
|
async function saveUnprocessed(data, { forceSave } = {}) {
|
|
|
|
const id = await channels.saveUnprocessed(_cleanData(data), { forceSave });
|
|
|
|
return id;
|
2018-07-25 22:02:27 +00:00
|
|
|
}
|
|
|
|
|
2018-07-27 01:13:56 +00:00
|
|
|
async function saveUnprocesseds(arrayOfUnprocessed, { forceSave } = {}) {
|
|
|
|
await channels.saveUnprocesseds(_cleanData(arrayOfUnprocessed), {
|
|
|
|
forceSave,
|
2018-07-25 22:02:27 +00:00
|
|
|
});
|
2018-07-27 01:13:56 +00:00
|
|
|
}
|
2018-07-25 22:02:27 +00:00
|
|
|
|
2019-02-05 01:23:50 +00:00
|
|
|
async function updateUnprocessedAttempts(id, attempts) {
|
|
|
|
await channels.updateUnprocessedAttempts(id, attempts);
|
|
|
|
}
|
|
|
|
async function updateUnprocessedWithData(id, data) {
|
|
|
|
await channels.updateUnprocessedWithData(id, data);
|
|
|
|
}
|
|
|
|
|
2018-07-27 01:13:56 +00:00
|
|
|
async function removeUnprocessed(id) {
|
|
|
|
await channels.removeUnprocessed(id);
|
2018-07-25 22:02:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function removeAllUnprocessed() {
|
2018-07-27 01:13:56 +00:00
|
|
|
await channels.removeAllUnprocessed();
|
2018-07-25 22:02:27 +00:00
|
|
|
}
|
|
|
|
|
2019-01-30 20:15:07 +00:00
|
|
|
// Attachment downloads
|
|
|
|
|
|
|
|
async function getNextAttachmentDownloadJobs(limit) {
|
|
|
|
return channels.getNextAttachmentDownloadJobs(limit);
|
|
|
|
}
|
|
|
|
async function saveAttachmentDownloadJob(job) {
|
|
|
|
await channels.saveAttachmentDownloadJob(job);
|
|
|
|
}
|
|
|
|
async function setAttachmentDownloadJobPending(id, pending) {
|
|
|
|
await channels.setAttachmentDownloadJobPending(id, pending);
|
|
|
|
}
|
|
|
|
async function resetAttachmentDownloadPending() {
|
|
|
|
await channels.resetAttachmentDownloadPending();
|
|
|
|
}
|
|
|
|
async function removeAttachmentDownloadJob(id) {
|
|
|
|
await channels.removeAttachmentDownloadJob(id);
|
|
|
|
}
|
|
|
|
async function removeAllAttachmentDownloadJobs() {
|
|
|
|
await channels.removeAllAttachmentDownloadJobs();
|
|
|
|
}
|
|
|
|
|
2019-05-16 22:32:11 +00:00
|
|
|
// Stickers
|
|
|
|
|
|
|
|
async function createOrUpdateStickerPack(pack) {
|
|
|
|
await channels.createOrUpdateStickerPack(pack);
|
|
|
|
}
|
|
|
|
async function updateStickerPackStatus(packId, status, options) {
|
|
|
|
await channels.updateStickerPackStatus(packId, status, options);
|
|
|
|
}
|
|
|
|
async function createOrUpdateSticker(sticker) {
|
|
|
|
await channels.createOrUpdateSticker(sticker);
|
|
|
|
}
|
|
|
|
async function updateStickerLastUsed(packId, stickerId, timestamp) {
|
|
|
|
await channels.updateStickerLastUsed(packId, stickerId, timestamp);
|
|
|
|
}
|
|
|
|
async function addStickerPackReference(messageId, packId) {
|
|
|
|
await channels.addStickerPackReference(messageId, packId);
|
|
|
|
}
|
|
|
|
async function deleteStickerPackReference(messageId, packId) {
|
|
|
|
const paths = await channels.deleteStickerPackReference(messageId, packId);
|
|
|
|
return paths;
|
|
|
|
}
|
|
|
|
async function deleteStickerPack(packId) {
|
|
|
|
const paths = await channels.deleteStickerPack(packId);
|
|
|
|
return paths;
|
|
|
|
}
|
|
|
|
async function getAllStickerPacks() {
|
|
|
|
const packs = await channels.getAllStickerPacks();
|
|
|
|
return packs;
|
|
|
|
}
|
|
|
|
async function getAllStickers() {
|
|
|
|
const stickers = await channels.getAllStickers();
|
|
|
|
return stickers;
|
|
|
|
}
|
|
|
|
async function getRecentStickers() {
|
|
|
|
const recentStickers = await channels.getRecentStickers();
|
|
|
|
return recentStickers;
|
|
|
|
}
|
|
|
|
|
2019-05-24 23:58:27 +00:00
|
|
|
// Emojis
|
|
|
|
async function updateEmojiUsage(shortName) {
|
|
|
|
await channels.updateEmojiUsage(shortName);
|
|
|
|
}
|
|
|
|
async function getRecentEmojis(limit = 32) {
|
|
|
|
return channels.getRecentEmojis(limit);
|
|
|
|
}
|
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
// Other
|
|
|
|
|
2018-07-25 22:02:27 +00:00
|
|
|
async function removeAll() {
|
2018-07-27 01:13:56 +00:00
|
|
|
await channels.removeAll();
|
2018-07-25 22:02:27 +00:00
|
|
|
}
|
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
async function removeAllConfiguration() {
|
|
|
|
await channels.removeAllConfiguration();
|
|
|
|
}
|
|
|
|
|
2018-08-08 17:00:33 +00:00
|
|
|
async function cleanupOrphanedAttachments() {
|
|
|
|
await callChannel(CLEANUP_ORPHANED_ATTACHMENTS_KEY);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function callChannel(name) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
ipcRenderer.send(name);
|
|
|
|
ipcRenderer.once(`${name}-done`, (event, error) => {
|
|
|
|
if (error) {
|
|
|
|
return reject(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
return resolve();
|
|
|
|
});
|
|
|
|
|
|
|
|
setTimeout(
|
|
|
|
() => reject(new Error(`callChannel call to ${name} timed out`)),
|
2018-10-15 18:53:58 +00:00
|
|
|
DATABASE_UPDATE_TIMEOUT
|
2018-07-27 01:13:56 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
// Functions below here return plain JSON instead of Backbone Models
|
2018-07-27 01:13:56 +00:00
|
|
|
|
|
|
|
async function getLegacyMessagesNeedingUpgrade(
|
2018-07-27 02:19:34 +00:00
|
|
|
limit,
|
2018-10-18 01:01:21 +00:00
|
|
|
{ maxVersion = MessageType.CURRENT_SCHEMA_VERSION }
|
2018-07-27 02:19:34 +00:00
|
|
|
) {
|
2018-10-18 01:01:21 +00:00
|
|
|
const db = await window.Whisper.Database.open();
|
|
|
|
try {
|
2018-11-01 18:34:55 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2018-10-18 01:01:21 +00:00
|
|
|
const transaction = db.transaction('messages', 'readonly');
|
|
|
|
const messages = [];
|
|
|
|
|
|
|
|
transaction.onerror = () => {
|
|
|
|
window.Whisper.Database.handleDOMException(
|
|
|
|
'getLegacyMessagesNeedingUpgrade transaction error',
|
|
|
|
transaction.error,
|
|
|
|
reject
|
|
|
|
);
|
|
|
|
};
|
|
|
|
transaction.oncomplete = () => {
|
|
|
|
resolve(messages);
|
|
|
|
};
|
|
|
|
|
|
|
|
const store = transaction.objectStore('messages');
|
|
|
|
const index = store.index('schemaVersion');
|
|
|
|
const range = IDBKeyRange.upperBound(maxVersion, true);
|
|
|
|
|
|
|
|
const request = index.openCursor(range);
|
|
|
|
let count = 0;
|
|
|
|
|
|
|
|
request.onsuccess = event => {
|
|
|
|
const cursor = event.target.result;
|
|
|
|
|
|
|
|
if (cursor) {
|
|
|
|
count += 1;
|
|
|
|
messages.push(cursor.value);
|
|
|
|
|
|
|
|
if (count >= limit) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cursor.continue();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
request.onerror = () => {
|
|
|
|
window.Whisper.Database.handleDOMException(
|
|
|
|
'getLegacyMessagesNeedingUpgrade request error',
|
|
|
|
request.error,
|
|
|
|
reject
|
|
|
|
);
|
|
|
|
};
|
|
|
|
});
|
|
|
|
} finally {
|
|
|
|
db.close();
|
|
|
|
}
|
2018-07-25 22:02:27 +00:00
|
|
|
}
|
|
|
|
|
2018-07-27 01:13:56 +00:00
|
|
|
async function getMessagesNeedingUpgrade(
|
|
|
|
limit,
|
|
|
|
{ maxVersion = MessageType.CURRENT_SCHEMA_VERSION }
|
2018-07-25 22:02:27 +00:00
|
|
|
) {
|
2018-07-27 01:13:56 +00:00
|
|
|
const messages = await channels.getMessagesNeedingUpgrade(limit, {
|
|
|
|
maxVersion,
|
|
|
|
});
|
2018-07-25 22:02:27 +00:00
|
|
|
|
2018-07-27 01:13:56 +00:00
|
|
|
return messages;
|
2018-07-25 22:02:27 +00:00
|
|
|
}
|
|
|
|
|
2018-07-27 01:13:56 +00:00
|
|
|
async function getMessagesWithVisualMediaAttachments(
|
2018-07-25 22:02:27 +00:00
|
|
|
conversationId,
|
2018-07-27 01:13:56 +00:00
|
|
|
{ limit }
|
2018-07-25 22:02:27 +00:00
|
|
|
) {
|
2018-07-27 01:13:56 +00:00
|
|
|
return channels.getMessagesWithVisualMediaAttachments(conversationId, {
|
|
|
|
limit,
|
|
|
|
});
|
2018-07-25 22:02:27 +00:00
|
|
|
}
|
|
|
|
|
2018-07-27 01:13:56 +00:00
|
|
|
async function getMessagesWithFileAttachments(conversationId, { limit }) {
|
|
|
|
return channels.getMessagesWithFileAttachments(conversationId, {
|
|
|
|
limit,
|
|
|
|
});
|
|
|
|
}
|