Add metadata field for thread messages

cshd
Dragory 2020-10-12 20:53:53 +03:00
parent f9671c385d
commit 5de750bc3e
No known key found for this signature in database
GPG Key ID: 5F387BA66DF8AAC1
2 changed files with 43 additions and 0 deletions

View File

@ -37,6 +37,12 @@ class ThreadMessage {
} else { } else {
this.small_attachments = []; this.small_attachments = [];
} }
if (props.metadata) {
if (typeof props.metadata === "string") {
this.metadata = JSON.parse(props.metadata);
}
}
} }
getSQLProps() { getSQLProps() {
@ -50,6 +56,32 @@ class ThreadMessage {
return obj; return obj;
}, {}); }, {});
} }
/**
* @param {string} key
* @param {*} value
* @return {Promise<void>}
*/
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; module.exports = ThreadMessage;

View File

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