Add afterThreadClose plugin hook

cshd
Dragory 2020-09-23 02:28:41 +03:00
parent 9be6b2aa1f
commit f5b6e46040
No known key found for this signature in database
GPG Key ID: 5F387BA66DF8AAC1
4 changed files with 52 additions and 0 deletions

View File

@ -7,6 +7,7 @@ const utils = require("../utils");
const config = require("../cfg"); const config = require("../cfg");
const attachments = require("./attachments"); const attachments = require("./attachments");
const { formatters } = require("../formatters"); const { formatters } = require("../formatters");
const { callAfterThreadCloseHooks } = require("../hooks/afterThreadClose");
const ThreadMessage = require("./ThreadMessage"); const ThreadMessage = require("./ThreadMessage");
@ -517,6 +518,8 @@ class Thread {
console.log(`Deleting channel ${this.channel_id}`); console.log(`Deleting channel ${this.channel_id}`);
await channel.delete("Thread closed"); await channel.delete("Thread closed");
} }
await callAfterThreadCloseHooks({ threadId: this.id });
} }
/** /**

View File

@ -0,0 +1,46 @@
const Eris = require("eris");
/**
* @typedef AfterThreadCloseHookData
* @property {string} threadId
*/
/**
* @callback AfterThreadCloseHookFn
* @param {AfterThreadCloseHookData} data
* @return {void|Promise<void>}
*/
/**
* @callback AddAfterThreadCloseHookFn
* @param {AfterThreadCloseHookFn} fn
* @return {void}
*/
/**
* @type AfterThreadCloseHookFn[]
*/
const afterThreadCloseHooks = [];
/**
* @type {AddAfterThreadCloseHookFn}
*/
let afterThreadClose; // Workaround to inconsistent IDE bug with @type and anonymous functions
afterThreadClose = (fn) => {
afterThreadCloseHooks.push(fn);
};
/**
* @param {AfterThreadCloseHookData} input
* @return {Promise<void>}
*/
async function callAfterThreadCloseHooks(input) {
for (const hook of afterThreadCloseHooks) {
await hook(input);
}
}
module.exports = {
afterThreadClose,
callAfterThreadCloseHooks,
};

View File

@ -31,4 +31,5 @@ const Knex = require("knex");
/** /**
* @typedef {object} PluginHooksAPI * @typedef {object} PluginHooksAPI
* @property {AddBeforeNewThreadHookFn} beforeNewThread * @property {AddBeforeNewThreadHookFn} beforeNewThread
* @property {AddAfterThreadCloseHookFn} afterThreadClose
*/ */

View File

@ -1,5 +1,6 @@
const attachments = require("./data/attachments"); const attachments = require("./data/attachments");
const { beforeNewThread } = require("./hooks/beforeNewThread"); const { beforeNewThread } = require("./hooks/beforeNewThread");
const { afterThreadClose } = require("./hooks/afterThreadClose");
const formats = require("./formatters"); const formats = require("./formatters");
module.exports = { module.exports = {
@ -28,6 +29,7 @@ module.exports = {
}, },
hooks: { hooks: {
beforeNewThread, beforeNewThread,
afterThreadClose,
}, },
formats, formats,
}; };