diff --git a/src/data/Thread.js b/src/data/Thread.js index 5f4263b..f27359f 100644 --- a/src/data/Thread.js +++ b/src/data/Thread.js @@ -335,8 +335,15 @@ class Thread { } if (this.alert_id) { - await this.setAlert(null); - await this.postSystemMessage(`<@!${this.alert_id}> New message from ${this.user_name}`); + const ids = this.alert_id.split(","); + let mentions = ""; + + ids.forEach(id => { + mentions += `<@!${id}> `; + }); + + await this.deleteAlerts(); + await this.postSystemMessage(`${mentions}New message from ${this.user_name}`); } } @@ -594,14 +601,77 @@ class Thread { * @param {String} userId * @returns {Promise} */ - async setAlert(userId) { + async addAlert(userId) { + let alerts = await knex("threads") + .where("id", this.id) + .select("alert_id") + .first(); + alerts = alerts.alert_id; + + if (alerts == null) { + alerts = [userId] + } else { + alerts = alerts.split(","); + if (! alerts.includes(userId)) { + alerts.push(userId); + } + } + + alerts = alerts.join(","); await knex("threads") .where("id", this.id) .update({ - alert_id: userId + alert_id: alerts }); } + /* + * @param {String} userId + * @returns {Promise} + */ + async removeAlert(userId) { + let alerts = await knex("threads") + .where("id", this.id) + .select("alert_id") + .first(); + alerts = alerts.alert_id; + + if (alerts != null) { + alerts = alerts.split(","); + + for (let i = 0; i < alerts.length; i++) { + if (alerts[i] === userId) { + alerts.splice(i, 1); + } + } + } else { + return; + } + + if (alerts.length === 0) { + alerts = null; + } else { + alerts = alerts.join(","); + } + + await knex("threads") + .where("id", this.id) + .update({ + alert_id: alerts + }); + } + + /** + * @returns {Promise} + */ + async deleteAlerts() { + await knex("threads") + .where("id", this.id) + .update({ + alert_id: null + }) + } + /** * @param {Eris.Member} moderator * @param {ThreadMessage} threadMessage diff --git a/src/modules/alert.js b/src/modules/alert.js index 6f8f1ba..7976d7b 100644 --- a/src/modules/alert.js +++ b/src/modules/alert.js @@ -1,10 +1,10 @@ module.exports = ({ bot, knex, config, commands }) => { commands.addInboxThreadCommand("alert", "[opt:string]", async (msg, args, thread) => { if (args.opt && args.opt.startsWith("c")) { - await thread.setAlert(null); + await thread.removeAlert(msg.author.id) await thread.postSystemMessage("Cancelled new message alert"); } else { - await thread.setAlert(msg.author.id); + await thread.addAlert(msg.author.id); await thread.postSystemMessage(`Pinging ${msg.author.username}#${msg.author.discriminator} when this thread gets a new reply`); } });