Add Signal.Startup module
		
	This commit is contained in:
		
					parent
					
						
							
								29832c445a
							
						
					
				
			
			
				commit
				
					
						ce8fe0d345
					
				
			
		
					 4 changed files with 181 additions and 0 deletions
				
			
		| 
						 | 
				
			
			@ -6,6 +6,8 @@ const LAST_PROCESSED_INDEX_KEY = 'attachmentMigration_lastProcessedIndex';
 | 
			
		|||
const IS_MIGRATION_COMPLETE_KEY = 'attachmentMigration_isComplete';
 | 
			
		||||
 | 
			
		||||
// Public API
 | 
			
		||||
exports.READ_RECEIPT_CONFIGURATION_SYNC = 'read-receipt-configuration-sync';
 | 
			
		||||
 | 
			
		||||
exports.getAttachmentMigrationLastProcessedIndex = connection =>
 | 
			
		||||
  exports._getItem(connection, LAST_PROCESSED_INDEX_KEY);
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										55
									
								
								js/modules/startup.js
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										55
									
								
								js/modules/startup.js
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,55 @@
 | 
			
		|||
const is = require('@sindresorhus/is');
 | 
			
		||||
 | 
			
		||||
const Errors = require('./types/errors');
 | 
			
		||||
const Settings = require('./settings');
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
exports.syncReadReceiptConfiguration = async ({
 | 
			
		||||
  deviceId,
 | 
			
		||||
  sendRequestConfigurationSyncMessage,
 | 
			
		||||
  storage,
 | 
			
		||||
}) => {
 | 
			
		||||
  if (!is.string(deviceId)) {
 | 
			
		||||
    throw new TypeError('"deviceId" is required');
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (!is.function(sendRequestConfigurationSyncMessage)) {
 | 
			
		||||
    throw new TypeError('"sendRequestConfigurationSyncMessage" is required');
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (!is.object(storage)) {
 | 
			
		||||
    throw new TypeError('"storage" is required');
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  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 {
 | 
			
		||||
    await sendRequestConfigurationSyncMessage();
 | 
			
		||||
    storage.put(settingName, true);
 | 
			
		||||
  } catch (error) {
 | 
			
		||||
    return {
 | 
			
		||||
      status: 'error',
 | 
			
		||||
      reason: 'failedToSendSyncMessage',
 | 
			
		||||
      error: Errors.toLogFormat(error),
 | 
			
		||||
    };
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  return {
 | 
			
		||||
    status: 'complete',
 | 
			
		||||
  };
 | 
			
		||||
};
 | 
			
		||||
| 
						 | 
				
			
			@ -146,6 +146,7 @@ window.Signal.Migrations.Migrations1DatabaseWithoutAttachmentData =
 | 
			
		|||
window.Signal.Migrations.upgradeMessageSchema = upgradeMessageSchema;
 | 
			
		||||
window.Signal.OS = require('./js/modules/os');
 | 
			
		||||
window.Signal.Settings = require('./js/modules/settings');
 | 
			
		||||
window.Signal.Startup = require('./js/modules/startup');
 | 
			
		||||
 | 
			
		||||
window.Signal.Types = {};
 | 
			
		||||
window.Signal.Types.Attachment = Attachment;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										123
									
								
								test/modules/startup_test.js
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										123
									
								
								test/modules/startup_test.js
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,123 @@
 | 
			
		|||
const sinon = require('sinon');
 | 
			
		||||
const { assert } = require('chai');
 | 
			
		||||
 | 
			
		||||
const Startup = require('../../js/modules/startup');
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
describe('Startup', () => {
 | 
			
		||||
  const sandbox = sinon.createSandbox();
 | 
			
		||||
 | 
			
		||||
  describe('syncReadReceiptConfiguration', () => {
 | 
			
		||||
    afterEach(() => {
 | 
			
		||||
      sandbox.restore();
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    it('should complete if user hasn’t previously synced', async () => {
 | 
			
		||||
      const deviceId = '2';
 | 
			
		||||
      const sendRequestConfigurationSyncMessage = sandbox.spy();
 | 
			
		||||
      const storagePutSpy = sandbox.spy();
 | 
			
		||||
      const storage = {
 | 
			
		||||
        get(name) {
 | 
			
		||||
          if (name !== 'read-receipt-configuration-sync') {
 | 
			
		||||
            return true;
 | 
			
		||||
          }
 | 
			
		||||
 | 
			
		||||
          return false;
 | 
			
		||||
        },
 | 
			
		||||
        put: storagePutSpy,
 | 
			
		||||
      };
 | 
			
		||||
 | 
			
		||||
      const expected = {
 | 
			
		||||
        status: 'complete',
 | 
			
		||||
      };
 | 
			
		||||
 | 
			
		||||
      const actual = await Startup.syncReadReceiptConfiguration({
 | 
			
		||||
        deviceId,
 | 
			
		||||
        sendRequestConfigurationSyncMessage,
 | 
			
		||||
        storage,
 | 
			
		||||
      });
 | 
			
		||||
 | 
			
		||||
      assert.deepEqual(actual, expected);
 | 
			
		||||
      assert.equal(sendRequestConfigurationSyncMessage.callCount, 1);
 | 
			
		||||
      assert.equal(storagePutSpy.callCount, 1);
 | 
			
		||||
      assert(storagePutSpy.calledWith('read-receipt-configuration-sync', true));
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    it('should be skipped if this is the primary device', async () => {
 | 
			
		||||
      const deviceId = '1';
 | 
			
		||||
      const sendRequestConfigurationSyncMessage = () => {};
 | 
			
		||||
      const storage = {};
 | 
			
		||||
 | 
			
		||||
      const expected = {
 | 
			
		||||
        status: 'skipped',
 | 
			
		||||
        reason: 'isPrimaryDevice',
 | 
			
		||||
      };
 | 
			
		||||
 | 
			
		||||
      const actual = await Startup.syncReadReceiptConfiguration({
 | 
			
		||||
        deviceId,
 | 
			
		||||
        sendRequestConfigurationSyncMessage,
 | 
			
		||||
        storage,
 | 
			
		||||
      });
 | 
			
		||||
 | 
			
		||||
      assert.deepEqual(actual, expected);
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    it('should be skipped if user has previously synced', async () => {
 | 
			
		||||
      const deviceId = '2';
 | 
			
		||||
      const sendRequestConfigurationSyncMessage = () => {};
 | 
			
		||||
      const storage = {
 | 
			
		||||
        get(name) {
 | 
			
		||||
          if (name !== 'read-receipt-configuration-sync') {
 | 
			
		||||
            return false;
 | 
			
		||||
          }
 | 
			
		||||
 | 
			
		||||
          return true;
 | 
			
		||||
        },
 | 
			
		||||
      };
 | 
			
		||||
 | 
			
		||||
      const expected = {
 | 
			
		||||
        status: 'skipped',
 | 
			
		||||
        reason: 'hasPreviouslySynced',
 | 
			
		||||
      };
 | 
			
		||||
 | 
			
		||||
      const actual = await Startup.syncReadReceiptConfiguration({
 | 
			
		||||
        deviceId,
 | 
			
		||||
        sendRequestConfigurationSyncMessage,
 | 
			
		||||
        storage,
 | 
			
		||||
      });
 | 
			
		||||
 | 
			
		||||
      assert.deepEqual(actual, expected);
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    it('should return error if sending of sync request fails', async () => {
 | 
			
		||||
      const deviceId = '2';
 | 
			
		||||
 | 
			
		||||
      const sendRequestConfigurationSyncMessage = sandbox.stub();
 | 
			
		||||
      sendRequestConfigurationSyncMessage.rejects(new Error('boom'));
 | 
			
		||||
 | 
			
		||||
      const storagePutSpy = sandbox.spy();
 | 
			
		||||
      const storage = {
 | 
			
		||||
        get(name) {
 | 
			
		||||
          if (name !== 'read-receipt-configuration-sync') {
 | 
			
		||||
            return true;
 | 
			
		||||
          }
 | 
			
		||||
 | 
			
		||||
          return false;
 | 
			
		||||
        },
 | 
			
		||||
        put: storagePutSpy,
 | 
			
		||||
      };
 | 
			
		||||
 | 
			
		||||
      const actual = await Startup.syncReadReceiptConfiguration({
 | 
			
		||||
        deviceId,
 | 
			
		||||
        sendRequestConfigurationSyncMessage,
 | 
			
		||||
        storage,
 | 
			
		||||
      });
 | 
			
		||||
 | 
			
		||||
      assert.equal(actual.status, 'error');
 | 
			
		||||
      assert.include(actual.error, 'boom');
 | 
			
		||||
 | 
			
		||||
      assert.equal(sendRequestConfigurationSyncMessage.callCount, 1);
 | 
			
		||||
      assert.equal(storagePutSpy.callCount, 0);
 | 
			
		||||
    });
 | 
			
		||||
  });
 | 
			
		||||
});
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue