function to properly change read state of a thread,

added markunread  command
This commit is contained in:
Erik 2021-09-02 14:38:55 +03:00
parent 652a1f5827
commit 9f97c0c16b
No known key found for this signature in database
GPG Key ID: 7E862371D3409F16
5 changed files with 33 additions and 12 deletions

View File

@ -71,7 +71,7 @@ class ChannelHandler {
} }
async markread(target, channel, staff) { async setReadState(target, channel, staff, state) {
const history = await this.cache.loadModmailHistory(target) const history = await this.cache.loadModmailHistory(target)
.catch((err) => { .catch((err) => {
@ -86,11 +86,13 @@ class ChannelHandler {
error: true, error: true,
msg: `User has no modmail history.` 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.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 {}; return {};
} }
@ -162,7 +164,7 @@ class ChannelHandler {
for (let i = context < len ? context : len; i > 0; i--) { for (let i = context < len ? context : len; i > 0; i--) {
const entry = history[len - i]; const entry = history[len - i];
if (!entry) continue; 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 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)); const mem = await this.modmail.getMember(user.id).catch(this.client.logger.error.bind(this.client.logger));

View File

@ -286,7 +286,7 @@ class Modmail {
} }
async markread(message, args) { async changeReadState(message, args, state = 'read') {
const { author } = message; const { author } = message;
@ -318,14 +318,14 @@ class Modmail {
}; };
user = await this.client.resolveUser(result[0]); 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) { } else if (user) {
const _ch = this.cache.channels[user.id]; const _ch = this.cache.channels[user.id];
if (_ch) channel = await this.client.resolveChannel(_ch); 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.`; } else return `Could not resolve ${id} to a target.`;
@ -345,12 +345,12 @@ class Modmail {
const [userId] = result; const [userId] = result;
user = await this.getUser(userId); 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; 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'; return 'Done';
} }

View File

@ -27,7 +27,7 @@ class Logs extends Command {
const { member, channel } = message; const { member, channel } = message;
const history = await this.client.cache.loadModmailHistory(user.id); const history = await this.client.cache.loadModmailHistory(user.id);
if (!history.length) return 'Not found in modmail DB'; 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 = { const embed = {
author: { author: {

View File

@ -10,7 +10,7 @@ class Markread extends Command {
async execute(message, { args }) { async execute(message, { args }) {
return this.client.modmail.markread(message, args); return this.client.modmail.changeReadState(message, args);
} }

View File

@ -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;