Add metadata field for threads
Useful for e.g. storing plugin-specific data about a thread.cshd
parent
c58ebf5698
commit
aadda31069
|
@ -28,6 +28,7 @@ const {THREAD_MESSAGE_TYPE, THREAD_STATUS, DISCORD_MESSAGE_ACTIVITY_TYPES} = req
|
|||
* @property {String} log_storage_type
|
||||
* @property {Object} log_storage_data
|
||||
* @property {String} created_at
|
||||
* @property {String} metadata
|
||||
*/
|
||||
class Thread {
|
||||
constructor(props) {
|
||||
|
@ -38,6 +39,12 @@ class Thread {
|
|||
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() {
|
||||
|
@ -767,6 +774,30 @@ class Thread {
|
|||
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;
|
||||
|
|
|
@ -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");
|
||||
});
|
||||
};
|
Loading…
Reference in New Issue