diff --git a/src/data/Thread.js b/src/data/Thread.js index 4ed6f8c..087c997 100644 --- a/src/data/Thread.js +++ b/src/data/Thread.js @@ -7,6 +7,7 @@ const utils = require("../utils"); const config = require("../cfg"); const attachments = require("./attachments"); const { formatters } = require("../formatters"); +const { callAfterThreadCloseHooks } = require("../hooks/afterThreadClose"); const ThreadMessage = require("./ThreadMessage"); @@ -517,6 +518,8 @@ class Thread { console.log(`Deleting channel ${this.channel_id}`); await channel.delete("Thread closed"); } + + await callAfterThreadCloseHooks({ threadId: this.id }); } /** diff --git a/src/hooks/afterThreadClose.js b/src/hooks/afterThreadClose.js new file mode 100644 index 0000000..9dd4e1d --- /dev/null +++ b/src/hooks/afterThreadClose.js @@ -0,0 +1,46 @@ +const Eris = require("eris"); + +/** + * @typedef AfterThreadCloseHookData + * @property {string} threadId + */ + +/** + * @callback AfterThreadCloseHookFn + * @param {AfterThreadCloseHookData} data + * @return {void|Promise} + */ + +/** + * @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} + */ +async function callAfterThreadCloseHooks(input) { + for (const hook of afterThreadCloseHooks) { + await hook(input); + } +} + +module.exports = { + afterThreadClose, + callAfterThreadCloseHooks, +}; diff --git a/src/pluginApi.js b/src/pluginApi.js index 65d8227..2e4b381 100644 --- a/src/pluginApi.js +++ b/src/pluginApi.js @@ -31,4 +31,5 @@ const Knex = require("knex"); /** * @typedef {object} PluginHooksAPI * @property {AddBeforeNewThreadHookFn} beforeNewThread + * @property {AddAfterThreadCloseHookFn} afterThreadClose */ diff --git a/src/plugins.js b/src/plugins.js index f27cf50..71bcf28 100644 --- a/src/plugins.js +++ b/src/plugins.js @@ -1,5 +1,6 @@ const attachments = require("./data/attachments"); const { beforeNewThread } = require("./hooks/beforeNewThread"); +const { afterThreadClose } = require("./hooks/afterThreadClose"); const formats = require("./formatters"); module.exports = { @@ -28,6 +29,7 @@ module.exports = { }, hooks: { beforeNewThread, + afterThreadClose, }, formats, };