Add metadata field for threads

Useful for e.g. storing plugin-specific data about a thread.
cshd
Dragory 2020-10-12 19:57:35 +03:00
parent c58ebf5698
commit aadda31069
No known key found for this signature in database
GPG Key ID: 5F387BA66DF8AAC1
2 changed files with 42 additions and 0 deletions

View File

@ -28,6 +28,7 @@ const {THREAD_MESSAGE_TYPE, THREAD_STATUS, DISCORD_MESSAGE_ACTIVITY_TYPES} = req
* @property {String} log_storage_type * @property {String} log_storage_type
* @property {Object} log_storage_data * @property {Object} log_storage_data
* @property {String} created_at * @property {String} created_at
* @property {String} metadata
*/ */
class Thread { class Thread {
constructor(props) { constructor(props) {
@ -38,6 +39,12 @@ class Thread {
this.log_storage_data = JSON.parse(props.log_storage_data); this.log_storage_data = JSON.parse(props.log_storage_data);
} }
} }
if (props.metadata) {
if (typeof props.metadata === "string") {
this.metadata = JSON.parse(props.metadata);
}
}
} }
getSQLProps() { getSQLProps() {
@ -767,6 +774,30 @@ class Thread {
log_storage_data, log_storage_data,
}); });
} }
/**
* @param {string} key
* @param {*} value
* @return {Promise<void>}
*/
async setMetadataValue(key, value) {
this.metadata = this.metadata || {};
this.metadata[key] = value;
await knex("threads")
.where("id", this.id)
.update({
metadata: this.getSQLProps().metadata,
});
}
/**
* @param {string} key
* @returns {*}
*/
getMetadataValue(key) {
return this.metadata ? this.metadata[key] : null;
}
} }
module.exports = Thread; module.exports = Thread;

View File

@ -0,0 +1,11 @@
exports.up = async function(knex) {
await knex.schema.table("threads", table => {
table.text("metadata").nullable().defaultTo(null);
});
};
exports.down = async function(knex) {
await knex.schema.table("threads", table => {
table.dropColumn("metadata");
});
};