From c27746b79e3c22cf75ce43bc22a6e335e47eb3b9 Mon Sep 17 00:00:00 2001 From: Daniel Gasienica Date: Tue, 13 Mar 2018 22:01:23 -0400 Subject: [PATCH] Add `Message.withInheritedSchemaVersion` --- js/modules/types/message.js | 39 ++++++++++++++++++ test/modules/types/message_test.js | 63 ++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 test/modules/types/message_test.js diff --git a/js/modules/types/message.js b/js/modules/types/message.js index 195359b64..2e7015ba0 100644 --- a/js/modules/types/message.js +++ b/js/modules/types/message.js @@ -1,9 +1,12 @@ const Attachment = require('./attachment'); +const SchemaVersion = require('./schema_version'); const GROUP = 'group'; const PRIVATE = 'private'; +const INITIAL_SCHEMA_VERSION = 0; + // Public API exports.GROUP = GROUP; exports.PRIVATE = PRIVATE; @@ -15,3 +18,39 @@ exports.upgradeSchema = async message => attachments: await Promise.all(message.attachments.map(Attachment.upgradeSchema)), }); + +// Inherits existing schema from attachments: +exports.withInheritedSchemaVersion = (message) => { + const isInitialized = SchemaVersion.isValid(message.schemaVersion) && + message.schemaVersion >= 1; + if (isInitialized) { + return message; + } + + const numAttachments = Array.isArray(message.attachments) + ? message.attachments.length : 0; + const hasAttachments = numAttachments > 0; + if (!hasAttachments) { + return Object.assign( + {}, + message, + { schemaVersion: INITIAL_SCHEMA_VERSION } + ); + } + + // All attachments should have the same schema version, so we just pick + // the first one: + const firstAttachment = message.attachments[0]; + const inheritedSchemaVersion = SchemaVersion.isValid(firstAttachment.schemaVersion) + ? firstAttachment.schemaVersion : INITIAL_SCHEMA_VERSION; + const messageWithInitialSchema = Object.assign( + {}, + message, + { + schemaVersion: inheritedSchemaVersion, + attachments: message.attachments.map(Attachment.removeSchemaVersion), + } + ); + + return messageWithInitialSchema; +}; diff --git a/test/modules/types/message_test.js b/test/modules/types/message_test.js new file mode 100644 index 000000000..d36eaf030 --- /dev/null +++ b/test/modules/types/message_test.js @@ -0,0 +1,63 @@ +const { assert } = require('chai'); + +const Message = require('../../../js/modules/types/message'); + + +describe('Message', () => { + describe('withInheritedSchemaVersion', () => { + it('should ignore messages with previously inherited schema', () => { + const input = { + body: 'Imagine there is no heaven…', + schemaVersion: 2, + }; + const expected = { + body: 'Imagine there is no heaven…', + schemaVersion: 2, + }; + + const actual = Message.withInheritedSchemaVersion(input); + assert.deepEqual(actual, expected); + }); + + context('for message without attachments', () => { + it('should initialize schema version to zero', () => { + const input = { + body: 'Imagine there is no heaven…', + attachments: [], + }; + const expected = { + body: 'Imagine there is no heaven…', + attachments: [], + schemaVersion: 0, + }; + + const actual = Message.withInheritedSchemaVersion(input); + assert.deepEqual(actual, expected); + }); + }); + + context('for message with attachments', () => { + it('should inherit existing attachment schema version', () => { + const input = { + body: 'Imagine there is no heaven…', + attachments: [{ + contentType: 'image/jpeg', + fileName: 'lennon.jpg', + schemaVersion: 7, + }], + }; + const expected = { + body: 'Imagine there is no heaven…', + attachments: [{ + contentType: 'image/jpeg', + fileName: 'lennon.jpg', + }], + schemaVersion: 7, + }; + + const actual = Message.withInheritedSchemaVersion(input); + assert.deepEqual(actual, expected); + }); + }); + }); +});