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 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;
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue