2018-04-02 19:08:53 +00:00
|
|
|
const { isObject, isString } = require('lodash');
|
2018-03-28 14:54:01 +00:00
|
|
|
|
|
|
|
const ITEMS_STORE_NAME = 'items';
|
|
|
|
const LAST_PROCESSED_INDEX_KEY = 'attachmentMigration_lastProcessedIndex';
|
|
|
|
const IS_MIGRATION_COMPLETE_KEY = 'attachmentMigration_isComplete';
|
2018-07-27 01:13:56 +00:00
|
|
|
const MESSAGE_LAST_INDEX_KEY = 'sqlMigration_messageLastIndex';
|
2018-08-02 01:34:50 +00:00
|
|
|
const MESSAGE_COUNT_KEY = 'sqlMigration_messageCount';
|
2018-07-27 01:13:56 +00:00
|
|
|
const UNPROCESSED_LAST_INDEX_KEY = 'sqlMigration_unprocessedLastIndex';
|
2018-03-28 14:54:01 +00:00
|
|
|
|
|
|
|
// Public API
|
2018-04-04 22:10:34 +00:00
|
|
|
exports.READ_RECEIPT_CONFIGURATION_SYNC = 'read-receipt-configuration-sync';
|
|
|
|
|
2018-03-28 14:54:01 +00:00
|
|
|
exports.getAttachmentMigrationLastProcessedIndex = connection =>
|
2018-03-28 21:12:51 +00:00
|
|
|
exports._getItem(connection, LAST_PROCESSED_INDEX_KEY);
|
2018-03-28 14:54:01 +00:00
|
|
|
exports.setAttachmentMigrationLastProcessedIndex = (connection, value) =>
|
2018-03-28 21:12:51 +00:00
|
|
|
exports._setItem(connection, LAST_PROCESSED_INDEX_KEY, value);
|
|
|
|
exports.deleteAttachmentMigrationLastProcessedIndex = connection =>
|
|
|
|
exports._deleteItem(connection, LAST_PROCESSED_INDEX_KEY);
|
2018-03-28 14:54:01 +00:00
|
|
|
|
|
|
|
exports.isAttachmentMigrationComplete = async connection =>
|
2018-03-28 21:12:51 +00:00
|
|
|
Boolean(await exports._getItem(connection, IS_MIGRATION_COMPLETE_KEY));
|
2018-03-28 14:54:01 +00:00
|
|
|
exports.markAttachmentMigrationComplete = connection =>
|
2018-03-28 21:12:51 +00:00
|
|
|
exports._setItem(connection, IS_MIGRATION_COMPLETE_KEY, true);
|
2018-03-28 14:54:01 +00:00
|
|
|
|
2018-07-27 01:13:56 +00:00
|
|
|
exports.getMessageExportLastIndex = connection =>
|
|
|
|
exports._getItem(connection, MESSAGE_LAST_INDEX_KEY);
|
|
|
|
exports.setMessageExportLastIndex = (connection, lastIndex) =>
|
|
|
|
exports._setItem(connection, MESSAGE_LAST_INDEX_KEY, lastIndex);
|
2018-08-02 01:34:50 +00:00
|
|
|
exports.getMessageExportCount = connection =>
|
|
|
|
exports._getItem(connection, MESSAGE_COUNT_KEY);
|
|
|
|
exports.setMessageExportCount = (connection, count) =>
|
|
|
|
exports._setItem(connection, MESSAGE_COUNT_KEY, count);
|
2018-07-27 01:13:56 +00:00
|
|
|
|
|
|
|
exports.getUnprocessedExportLastIndex = connection =>
|
|
|
|
exports._getItem(connection, UNPROCESSED_LAST_INDEX_KEY);
|
|
|
|
exports.setUnprocessedExportLastIndex = (connection, lastIndex) =>
|
|
|
|
exports._setItem(connection, UNPROCESSED_LAST_INDEX_KEY, lastIndex);
|
|
|
|
|
2018-03-28 14:54:01 +00:00
|
|
|
// Private API
|
2018-03-28 21:12:51 +00:00
|
|
|
exports._getItem = (connection, key) => {
|
2018-03-28 14:54:01 +00:00
|
|
|
if (!isObject(connection)) {
|
2018-04-11 19:44:52 +00:00
|
|
|
throw new TypeError("'connection' is required");
|
2018-03-28 14:54:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!isString(key)) {
|
2018-04-11 19:44:52 +00:00
|
|
|
throw new TypeError("'key' must be a string");
|
2018-03-28 14:54:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const transaction = connection.transaction(ITEMS_STORE_NAME, 'readonly');
|
|
|
|
const itemsStore = transaction.objectStore(ITEMS_STORE_NAME);
|
|
|
|
const request = itemsStore.get(key);
|
|
|
|
return new Promise((resolve, reject) => {
|
2018-04-27 21:25:04 +00:00
|
|
|
request.onerror = event => reject(event.target.error);
|
2018-03-28 14:54:01 +00:00
|
|
|
|
|
|
|
request.onsuccess = event =>
|
|
|
|
resolve(event.target.result ? event.target.result.value : null);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-03-28 21:12:51 +00:00
|
|
|
exports._setItem = (connection, key, value) => {
|
2018-03-28 14:54:01 +00:00
|
|
|
if (!isObject(connection)) {
|
2018-04-11 19:44:52 +00:00
|
|
|
throw new TypeError("'connection' is required");
|
2018-03-28 14:54:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!isString(key)) {
|
2018-04-11 19:44:52 +00:00
|
|
|
throw new TypeError("'key' must be a string");
|
2018-03-28 14:54:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const transaction = connection.transaction(ITEMS_STORE_NAME, 'readwrite');
|
|
|
|
const itemsStore = transaction.objectStore(ITEMS_STORE_NAME);
|
|
|
|
const request = itemsStore.put({ id: key, value }, key);
|
|
|
|
return new Promise((resolve, reject) => {
|
2018-04-27 21:25:04 +00:00
|
|
|
request.onerror = event => reject(event.target.error);
|
2018-03-28 14:54:01 +00:00
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
request.onsuccess = () => resolve();
|
2018-03-28 14:54:01 +00:00
|
|
|
});
|
|
|
|
};
|
2018-03-28 21:05:21 +00:00
|
|
|
|
2018-03-28 21:12:51 +00:00
|
|
|
exports._deleteItem = (connection, key) => {
|
2018-03-28 21:05:21 +00:00
|
|
|
if (!isObject(connection)) {
|
2018-04-11 19:44:52 +00:00
|
|
|
throw new TypeError("'connection' is required");
|
2018-03-28 21:05:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!isString(key)) {
|
2018-04-11 19:44:52 +00:00
|
|
|
throw new TypeError("'key' must be a string");
|
2018-03-28 21:05:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const transaction = connection.transaction(ITEMS_STORE_NAME, 'readwrite');
|
|
|
|
const itemsStore = transaction.objectStore(ITEMS_STORE_NAME);
|
|
|
|
const request = itemsStore.delete(key);
|
|
|
|
return new Promise((resolve, reject) => {
|
2018-04-27 21:25:04 +00:00
|
|
|
request.onerror = event => reject(event.target.error);
|
2018-03-28 21:05:21 +00:00
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
request.onsuccess = () => resolve();
|
2018-03-28 21:05:21 +00:00
|
|
|
});
|
|
|
|
};
|