From 9f97c0c16b8a60acfa7d1ddb5b71457240a3769d Mon Sep 17 00:00:00 2001 From: "Navy.gif" Date: Thu, 2 Sep 2021 14:38:55 +0300 Subject: [PATCH] function to properly change read state of a thread, added markunread command --- structure/ChannelHandler.js | 12 +++++++----- structure/Modmail.js | 10 +++++----- structure/commands/Logs.js | 2 +- structure/commands/Markread.js | 2 +- structure/commands/Markunread.js | 19 +++++++++++++++++++ 5 files changed, 33 insertions(+), 12 deletions(-) create mode 100644 structure/commands/Markunread.js diff --git a/structure/ChannelHandler.js b/structure/ChannelHandler.js index 4f6ada0..546e7db 100644 --- a/structure/ChannelHandler.js +++ b/structure/ChannelHandler.js @@ -71,7 +71,7 @@ class ChannelHandler { } - async markread(target, channel, staff) { + async setReadState(target, channel, staff, state) { const history = await this.cache.loadModmailHistory(target) .catch((err) => { @@ -86,11 +86,13 @@ class ChannelHandler { error: true, msg: `User has no modmail history.` }; - if(!history[history.length-1].markread) history.push({ author: staff.id, timestamp: Date.now(), markread: true }); // To keep track of read state + if (history[history.length - 1].readState !== state) history.push({ author: staff.id, timestamp: Date.now(), readState: state }); // To keep track of read state + if (state === 'unread') await this.load(await this.client.resolveUser(target), history); - if (channel) await channel.edit({ parentID: this.readMail.id, lockPermissions: true }); + if (channel) await channel.edit({ parentID: state === 'read' ? this.readMail.id : this.newMail.id, lockPermissions: true }); if (!this.cache.updatedThreads.includes(target)) this.cache.updatedThreads.push(target); - if (this.cache.queue.includes(target)) this.cache.queue.splice(this.cache.queue.indexOf(target), 1); + if (this.cache.queue.includes(target) && state === 'read') this.cache.queue.splice(this.cache.queue.indexOf(target), 1); + else if (!this.cache.queue.includes(target)) this.cache.queue.push(target); return {}; } @@ -162,7 +164,7 @@ class ChannelHandler { for (let i = context < len ? context : len; i > 0; i--) { const entry = history[len - i]; if (!entry) continue; - if (entry.markread) continue; + if (entry.readState === 'read') continue; const user = await this.client.resolveUser(entry.author).catch(this.client.logger.error.bind(this.client.logger)); const mem = await this.modmail.getMember(user.id).catch(this.client.logger.error.bind(this.client.logger)); diff --git a/structure/Modmail.js b/structure/Modmail.js index cb7c8a2..c6c233b 100644 --- a/structure/Modmail.js +++ b/structure/Modmail.js @@ -286,7 +286,7 @@ class Modmail { } - async markread(message, args) { + async changeReadState(message, args, state = 'read') { const { author } = message; @@ -318,14 +318,14 @@ class Modmail { }; user = await this.client.resolveUser(result[0]); - response = await this.channels.markread(user.id, channel, author); + response = await this.channels.setReadState(user.id, channel, author, state); } else if (user) { const _ch = this.cache.channels[user.id]; if (_ch) channel = await this.client.resolveChannel(_ch); - response = await this.channels.markread(user.id, channel, author); + response = await this.channels.setReadState(user.id, channel, author, state); } else return `Could not resolve ${id} to a target.`; @@ -345,12 +345,12 @@ class Modmail { const [userId] = result; user = await this.getUser(userId); - response = await this.channels.markread(userId, channel, author); + response = await this.channels.setReadState(userId, channel, author, state); } if (response.error) return response; - this.log({ author, action: `${author.tag} marked ${user.tag}'s thread as read`, target: user }); + this.log({ author, action: `${author.tag} marked ${user.tag}'s thread as ${state}`, target: user }); return 'Done'; } diff --git a/structure/commands/Logs.js b/structure/commands/Logs.js index a022343..0d5260b 100644 --- a/structure/commands/Logs.js +++ b/structure/commands/Logs.js @@ -27,7 +27,7 @@ class Logs extends Command { const { member, channel } = message; const history = await this.client.cache.loadModmailHistory(user.id); if (!history.length) return 'Not found in modmail DB'; - const page = this.paginate([...history].filter((e) => !e.markread).reverse(), pageNr, 10); + const page = this.paginate([...history].filter((e) => !('readState' in e)).reverse(), pageNr, 10); const embed = { author: { diff --git a/structure/commands/Markread.js b/structure/commands/Markread.js index 1e85615..59b4cc0 100644 --- a/structure/commands/Markread.js +++ b/structure/commands/Markread.js @@ -10,7 +10,7 @@ class Markread extends Command { async execute(message, { args }) { - return this.client.modmail.markread(message, args); + return this.client.modmail.changeReadState(message, args); } diff --git a/structure/commands/Markunread.js b/structure/commands/Markunread.js new file mode 100644 index 0000000..7ec8b88 --- /dev/null +++ b/structure/commands/Markunread.js @@ -0,0 +1,19 @@ +const Command = require('../Command'); + +class Markunread extends Command { + + constructor(client) { + super(client, { + name: 'markunread' + }); + } + + async execute(message, { args }) { + + return this.client.modmail.changeReadState(message, args, 'unread'); + + } + +} + +module.exports = Markunread; \ No newline at end of file