Add Message.withInheritedSchemaVersion
This commit is contained in:
parent
e9e46464c2
commit
c27746b79e
2 changed files with 102 additions and 0 deletions
|
@ -1,9 +1,12 @@
|
||||||
const Attachment = require('./attachment');
|
const Attachment = require('./attachment');
|
||||||
|
const SchemaVersion = require('./schema_version');
|
||||||
|
|
||||||
|
|
||||||
const GROUP = 'group';
|
const GROUP = 'group';
|
||||||
const PRIVATE = 'private';
|
const PRIVATE = 'private';
|
||||||
|
|
||||||
|
const INITIAL_SCHEMA_VERSION = 0;
|
||||||
|
|
||||||
// Public API
|
// Public API
|
||||||
exports.GROUP = GROUP;
|
exports.GROUP = GROUP;
|
||||||
exports.PRIVATE = PRIVATE;
|
exports.PRIVATE = PRIVATE;
|
||||||
|
@ -15,3 +18,39 @@ exports.upgradeSchema = async message =>
|
||||||
attachments:
|
attachments:
|
||||||
await Promise.all(message.attachments.map(Attachment.upgradeSchema)),
|
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;
|
||||||
|
};
|
||||||
|
|
63
test/modules/types/message_test.js
Normal file
63
test/modules/types/message_test.js
Normal file
|
@ -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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
Loading…
Add table
Add a link
Reference in a new issue