Add thread suspending

master
Dragory 2018-03-11 22:27:52 +02:00
parent 11c4bc12da
commit b8a5021dd5
7 changed files with 72 additions and 2 deletions

View File

@ -1,5 +1,8 @@
# Changelog # Changelog
## v2.4.0
* Add thread suspending. A modmail thread can now be suspended with `!suspend`. Suspended threads will function as closed until unsuspended with `!unsuspend`.
## v2.3.2 ## v2.3.2
* Auto-close threads if their modmail channel is deleted * Auto-close threads if their modmail channel is deleted

View File

@ -45,6 +45,8 @@ See [CHANGELOG.md](CHANGELOG.md)
`!!shortcut` Reply with a snippet. Replace `shortcut` with the snippet's actual shortcut. `!!shortcut` Reply with a snippet. Replace `shortcut` with the snippet's actual shortcut.
`!move <category>` If `allowMove` is enabled, moves the thread channel to the specified category `!move <category>` If `allowMove` is enabled, moves the thread channel to the specified category
`!loglink` Shows the link to the current thread's log `!loglink` Shows the link to the current thread's log
`!suspend` Suspend a thread. The thread will act as closed and not receive any messages until unsuspended.
`!unsuspend` Unsuspend a thread
To automatically reply without using !reply or !r, enable `alwaysReply` in the config. `alwaysReplyAnon` sets whether to reply anonymously. If you do not wish to reply, it will ignore any message starting in the prefix (which defaults to !), such as !note To automatically reply without using !reply or !r, enable `alwaysReply` in the config. `alwaysReplyAnon` sets whether to reply anonymously. If you do not wish to reply, it will ignore any message starting in the prefix (which defaults to !), such as !note

View File

@ -307,6 +307,11 @@ class Thread {
} }
} }
/**
* @param {String} time
* @param {Eris~User} user
* @returns {Promise<void>}
*/
async scheduleClose(time, user) { async scheduleClose(time, user) {
await knex('threads') await knex('threads')
.where('id', this.id) .where('id', this.id)
@ -317,6 +322,9 @@ class Thread {
}); });
} }
/**
* @returns {Promise<void>}
*/
async cancelScheduledClose() { async cancelScheduledClose() {
await knex('threads') await knex('threads')
.where('id', this.id) .where('id', this.id)
@ -327,6 +335,28 @@ class Thread {
}); });
} }
/**
* @returns {Promise<void>}
*/
async suspend() {
await knex('threads')
.where('id', this.id)
.update({
status: THREAD_STATUS.SUSPENDED
});
}
/**
* @returns {Promise<void>}
*/
async unsuspend() {
await knex('threads')
.where('id', this.id)
.update({
status: THREAD_STATUS.OPEN
});
}
/** /**
* @returns {Promise<String>} * @returns {Promise<String>}
*/ */

View File

@ -1,7 +1,8 @@
module.exports = { module.exports = {
THREAD_STATUS: { THREAD_STATUS: {
OPEN: 1, OPEN: 1,
CLOSED: 2 CLOSED: 2,
SUSPENDED: 3
}, },
THREAD_MESSAGE_TYPE: { THREAD_MESSAGE_TYPE: {

View File

@ -154,6 +154,19 @@ async function findOpenThreadByChannelId(channelId) {
return (thread ? new Thread(thread) : null); return (thread ? new Thread(thread) : null);
} }
/**
* @param {String} channelId
* @returns {Promise<Thread>}
*/
async function findSuspendedThreadByChannelId(channelId) {
const thread = await knex('threads')
.where('channel_id', channelId)
.where('status', THREAD_STATUS.SUSPENDED)
.first();
return (thread ? new Thread(thread) : null);
}
/** /**
* @param {String} userId * @param {String} userId
* @returns {Promise<Thread[]>} * @returns {Promise<Thread[]>}
@ -203,6 +216,7 @@ module.exports = {
findOpenThreadByUserId, findOpenThreadByUserId,
findByChannelId, findByChannelId,
findOpenThreadByChannelId, findOpenThreadByChannelId,
findSuspendedThreadByChannelId,
createNewThreadForUser, createNewThreadForUser,
getClosedThreadsByUserId, getClosedThreadsByUserId,
findOrCreateThreadForUser, findOrCreateThreadForUser,

View File

@ -414,6 +414,26 @@ addInboxServerCommand('loglink', async (msg, args, thread) => {
thread.postNonLogMessage(`Log URL: ${logUrl}`); thread.postNonLogMessage(`Log URL: ${logUrl}`);
}); });
addInboxServerCommand('suspend', async (msg, args, thread) => {
if (! thread) return;
await thread.suspend();
thread.postSystemMessage(`**Thread suspended!** This thread will act as closed until unsuspended with \`!unsuspend\``);
});
addInboxServerCommand('unsuspend', async (msg, args) => {
const thread = await threads.findSuspendedThreadByChannelId(msg.channel.id);
if (! thread) return;
const otherOpenThread = await threads.findOpenThreadByUserId(thread.user_id);
if (otherOpenThread) {
thread.postSystemMessage(`Cannot unsuspend; there is another open thread with this user: <#${otherOpenThread.channel_id}>`);
return;
}
await thread.unsuspend();
thread.postSystemMessage(`**Thread unsuspended!**`);
});
module.exports = { module.exports = {
async start() { async start() {
// Load plugins // Load plugins

View File

@ -14,7 +14,7 @@ function addInboxServerCommand(bot, cmd, commandHandler, opts) {
if (! utils.messageIsOnInboxServer(msg)) return; if (! utils.messageIsOnInboxServer(msg)) return;
if (! utils.isStaff(msg.member)) return; if (! utils.isStaff(msg.member)) return;
const thread = await threads.findByChannelId(msg.channel.id); const thread = await threads.findOpenThreadByChannelId(msg.channel.id);
commandHandler(msg, args, thread); commandHandler(msg, args, thread);
}, opts); }, opts);
} }