From 5de750bc3e7e1f1e293073a2157ae4879026beed Mon Sep 17 00:00:00 2001 From: Dragory <2606411+Dragory@users.noreply.github.com> Date: Mon, 12 Oct 2020 20:53:53 +0300 Subject: [PATCH] Add metadata field for thread messages --- src/data/ThreadMessage.js | 32 +++++++++++++++++++ ...2_add_metadata_field_to_thread_messages.js | 11 +++++++ 2 files changed, 43 insertions(+) create mode 100644 src/data/migrations/20201012203622_add_metadata_field_to_thread_messages.js diff --git a/src/data/ThreadMessage.js b/src/data/ThreadMessage.js index b05f3d6..eb6b4f7 100644 --- a/src/data/ThreadMessage.js +++ b/src/data/ThreadMessage.js @@ -37,6 +37,12 @@ class ThreadMessage { } else { this.small_attachments = []; } + + if (props.metadata) { + if (typeof props.metadata === "string") { + this.metadata = JSON.parse(props.metadata); + } + } } getSQLProps() { @@ -50,6 +56,32 @@ class ThreadMessage { return obj; }, {}); } + + /** + * @param {string} key + * @param {*} value + * @return {Promise} + */ + async setMetadataValue(key, value) { + this.metadata = this.metadata || {}; + this.metadata[key] = value; + + if (this.id) { + await knex("thread_messages") + .where("id", this.id) + .update({ + metadata: this.getSQLProps().metadata, + }); + } + } + + /** + * @param {string} key + * @returns {*} + */ + getMetadataValue(key) { + return this.metadata ? this.metadata[key] : null; + } } module.exports = ThreadMessage; diff --git a/src/data/migrations/20201012203622_add_metadata_field_to_thread_messages.js b/src/data/migrations/20201012203622_add_metadata_field_to_thread_messages.js new file mode 100644 index 0000000..8822f1b --- /dev/null +++ b/src/data/migrations/20201012203622_add_metadata_field_to_thread_messages.js @@ -0,0 +1,11 @@ +exports.up = async function(knex) { + await knex.schema.table("thread_messages", table => { + table.text("metadata").nullable().defaultTo(null); + }); +}; + +exports.down = async function(knex) { + await knex.schema.table("thread_messages", table => { + table.dropColumn("metadata"); + }); +};