signal-desktop/js/modules/startup.js

66 lines
1.5 KiB
JavaScript
Raw Normal View History

2018-04-04 22:10:34 +00:00
const is = require('@sindresorhus/is');
const Errors = require('./types/errors');
const Settings = require('./settings');
exports.syncReadReceiptConfiguration = async ({
ourNumber,
2018-04-04 22:10:34 +00:00
deviceId,
sendRequestConfigurationSyncMessage,
storage,
prepareForSend,
2018-04-04 22:10:34 +00:00
}) => {
if (!is.string(deviceId)) {
throw new TypeError('deviceId is required');
2018-04-04 22:10:34 +00:00
}
if (!is.function(sendRequestConfigurationSyncMessage)) {
throw new TypeError('sendRequestConfigurationSyncMessage is required');
}
if (!is.function(prepareForSend)) {
throw new TypeError('prepareForSend is required');
}
if (!is.string(ourNumber)) {
throw new TypeError('ourNumber is required');
2018-04-04 22:10:34 +00:00
}
if (!is.object(storage)) {
throw new TypeError('storage is required');
2018-04-04 22:10:34 +00:00
}
const isPrimaryDevice = deviceId === '1';
if (isPrimaryDevice) {
return {
status: 'skipped',
reason: 'isPrimaryDevice',
};
}
const settingName = Settings.READ_RECEIPT_CONFIGURATION_SYNC;
const hasPreviouslySynced = Boolean(storage.get(settingName));
if (hasPreviouslySynced) {
return {
status: 'skipped',
reason: 'hasPreviouslySynced',
};
}
try {
2018-11-07 19:20:43 +00:00
const { wrap, sendOptions } = prepareForSend(ourNumber, {
syncMessage: true,
});
await wrap(sendRequestConfigurationSyncMessage(sendOptions));
2018-04-04 22:10:34 +00:00
storage.put(settingName, true);
} catch (error) {
return {
status: 'error',
reason: 'failedToSendSyncMessage',
error: Errors.toLogFormat(error),
};
}
return {
status: 'complete',
};
};