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"); + }); +};