From 949a5efdb25026bd75bcdda6a6d23ec62ed92f69 Mon Sep 17 00:00:00 2001 From: Dark <7890309+DarkView@users.noreply.github.com> Date: Mon, 17 Feb 2020 20:04:43 +0100 Subject: [PATCH 1/2] Allow multiple users to do !alert without overriding each other --- src/data/Thread.js | 61 ++++++++++++++++++++++++++++++++++++++++---- src/modules/alert.js | 6 ++--- 2 files changed, 59 insertions(+), 8 deletions(-) diff --git a/src/data/Thread.js b/src/data/Thread.js index 23e9e4a..fad9c36 100644 --- a/src/data/Thread.js +++ b/src/data/Thread.js @@ -169,8 +169,8 @@ class Thread { } if (this.alert_id) { - await this.setAlert(null); - await this.postSystemMessage(`<@!${this.alert_id}> New message from ${this.user_name}`); + await this.deleteAlerts(); + await this.postSystemMessage(`${this.alert_id} New message from ${this.user_name}`); } } @@ -449,14 +449,65 @@ 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 { + if (! alerts.includes(`<@!${userId}>`)) { + alerts += ` <@!${userId}>`; + } + } + 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)) { + if (alerts.startsWith(`<@!${userId}>`)) { // we do this to properly handle the spacing between @s + alerts = alerts.replace(`<@!${userId}> `, ""); + } else { + alerts = alerts.replace(` <@!${userId}>`, ""); + } + } + + 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 + }) + } + /** * @returns {Promise} */ @@ -465,4 +516,4 @@ class Thread { } } -module.exports = Thread; +module.exports = Thread; \ No newline at end of file diff --git a/src/modules/alert.js b/src/modules/alert.js index a844230..65d6576 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.postSystemMessage(`Cancelled new message alert`); + await thread.removeAlert(msg.author.id) + await thread.postSystemMessage(`Cancelled your 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`); } }); From c41f7a09787374ec193a5fb24f0ca84d9066accf Mon Sep 17 00:00:00 2001 From: Dark <7890309+DarkView@users.noreply.github.com> Date: Tue, 18 Feb 2020 01:22:00 +0100 Subject: [PATCH 2/2] Change to comma separated list --- src/data/Thread.js | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/src/data/Thread.js b/src/data/Thread.js index fad9c36..6858d5c 100644 --- a/src/data/Thread.js +++ b/src/data/Thread.js @@ -169,8 +169,15 @@ class Thread { } if (this.alert_id) { + const ids = this.alert_id.split(","); + let mentions = ""; + + ids.forEach(id => { + mentions += `<@!${id}> `; + }); + await this.deleteAlerts(); - await this.postSystemMessage(`${this.alert_id} New message from ${this.user_name}`); + await this.postSystemMessage(`${mentions}New message from ${this.user_name}`); } } @@ -457,13 +464,15 @@ class Thread { alerts = alerts.alert_id; if (alerts == null) { - alerts = `<@!${userId}>`; + alerts = [userId] } else { - if (! alerts.includes(`<@!${userId}>`)) { - alerts += ` <@!${userId}>`; + alerts = alerts.split(","); + if (!alerts.includes(userId)) { + alerts.push(userId); } } + alerts = alerts.join(","); await knex('threads') .where('id', this.id) .update({ @@ -482,12 +491,20 @@ class Thread { .first(); alerts = alerts.alert_id; - if (! (alerts == null)) { - if (alerts.startsWith(`<@!${userId}>`)) { // we do this to properly handle the spacing between @s - alerts = alerts.replace(`<@!${userId}> `, ""); - } else { - alerts = alerts.replace(` <@!${userId}>`, ""); + 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')