2017-12-24 15:04:08 -05:00
|
|
|
const Eris = require('eris');
|
|
|
|
const transliterate = require('transliteration');
|
|
|
|
const moment = require('moment');
|
|
|
|
const uuid = require('uuid');
|
2017-12-31 19:16:05 -05:00
|
|
|
const humanizeDuration = require('humanize-duration');
|
2017-12-24 15:04:08 -05:00
|
|
|
|
|
|
|
const knex = require('../knex');
|
|
|
|
const config = require('../config');
|
2017-12-31 19:16:05 -05:00
|
|
|
const utils = require('../utils');
|
2017-12-24 15:04:08 -05:00
|
|
|
|
2017-12-31 19:16:05 -05:00
|
|
|
const Thread = require('./Thread');
|
|
|
|
const {THREAD_STATUS} = require('./constants');
|
2017-12-24 15:04:08 -05:00
|
|
|
|
2018-02-14 01:53:34 -05:00
|
|
|
/**
|
|
|
|
* @param {String} id
|
|
|
|
* @returns {Promise<Thread>}
|
|
|
|
*/
|
2018-02-11 14:54:30 -05:00
|
|
|
async function findById(id) {
|
|
|
|
const thread = await knex('threads')
|
|
|
|
.where('id', id)
|
|
|
|
.first();
|
|
|
|
|
|
|
|
return (thread ? new Thread(thread) : null);
|
|
|
|
}
|
|
|
|
|
2017-12-24 15:04:08 -05:00
|
|
|
/**
|
2017-12-31 19:16:05 -05:00
|
|
|
* @param {String} userId
|
2017-12-24 15:04:08 -05:00
|
|
|
* @returns {Promise<Thread>}
|
|
|
|
*/
|
2017-12-31 19:16:05 -05:00
|
|
|
async function findOpenThreadByUserId(userId) {
|
2017-12-24 15:04:08 -05:00
|
|
|
const thread = await knex('threads')
|
2017-12-31 19:16:05 -05:00
|
|
|
.where('user_id', userId)
|
2017-12-24 15:04:08 -05:00
|
|
|
.where('status', THREAD_STATUS.OPEN)
|
2018-02-11 14:54:30 -05:00
|
|
|
.first();
|
2017-12-24 15:04:08 -05:00
|
|
|
|
2017-12-31 19:16:05 -05:00
|
|
|
return (thread ? new Thread(thread) : null);
|
|
|
|
}
|
2017-12-24 15:04:08 -05:00
|
|
|
|
2018-04-21 08:38:21 -04:00
|
|
|
function getHeaderGuildInfo(member) {
|
|
|
|
return {
|
|
|
|
nickname: member.nick || member.user.username,
|
|
|
|
joinDate: humanizeDuration(Date.now() - member.joinedAt, {largest: 2, round: true})
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-12-31 19:16:05 -05:00
|
|
|
/**
|
|
|
|
* Creates a new modmail thread for the specified user
|
|
|
|
* @param {Eris.User} user
|
2018-04-07 19:56:30 -04:00
|
|
|
* @param {Boolean} quiet If true, doesn't ping mentionRole or reply with responseMessage
|
2017-12-31 19:16:05 -05:00
|
|
|
* @returns {Promise<Thread>}
|
|
|
|
* @throws {Error}
|
|
|
|
*/
|
2018-04-07 19:56:30 -04:00
|
|
|
async function createNewThreadForUser(user, quiet = false) {
|
2017-12-31 19:16:05 -05:00
|
|
|
const existingThread = await findOpenThreadByUserId(user.id);
|
|
|
|
if (existingThread) {
|
|
|
|
throw new Error('Attempted to create a new thread for a user with an existing open thread!');
|
2017-12-24 15:04:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Use the user's name+discrim for the thread channel's name
|
|
|
|
// Channel names are particularly picky about what characters they allow, so we gotta do some clean-up
|
|
|
|
let cleanName = transliterate.slugify(user.username);
|
|
|
|
if (cleanName === '') cleanName = 'unknown';
|
|
|
|
cleanName = cleanName.slice(0, 95); // Make sure the discrim fits
|
|
|
|
|
|
|
|
const channelName = `${cleanName}-${user.discriminator}`;
|
|
|
|
|
|
|
|
console.log(`[NOTE] Creating new thread channel ${channelName}`);
|
|
|
|
|
|
|
|
// Attempt to create the inbox channel for this thread
|
|
|
|
let createdChannel;
|
|
|
|
try {
|
2018-02-11 14:54:30 -05:00
|
|
|
createdChannel = await utils.getInboxGuild().createChannel(channelName, null, 'New ModMail thread', config.newThreadCategoryId);
|
2017-12-24 15:04:08 -05:00
|
|
|
} catch (err) {
|
|
|
|
console.error(`Error creating modmail channel for ${user.username}#${user.discriminator}!`);
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save the new thread in the database
|
2017-12-31 19:16:05 -05:00
|
|
|
const newThreadId = await createThreadInDB({
|
2017-12-24 15:04:08 -05:00
|
|
|
status: THREAD_STATUS.OPEN,
|
|
|
|
user_id: user.id,
|
|
|
|
user_name: `${user.username}#${user.discriminator}`,
|
|
|
|
channel_id: createdChannel.id,
|
|
|
|
created_at: moment.utc().format('YYYY-MM-DD HH:mm:ss')
|
|
|
|
});
|
|
|
|
|
2017-12-31 19:16:05 -05:00
|
|
|
const newThread = await findById(newThreadId);
|
|
|
|
|
2018-04-07 19:56:30 -04:00
|
|
|
if (! quiet) {
|
|
|
|
// Ping moderators of the new thread
|
|
|
|
if (config.mentionRole) {
|
|
|
|
await newThread.postNonLogMessage({
|
|
|
|
content: `${utils.getInboxMention()}New modmail thread (${newThread.user_name})`,
|
|
|
|
disableEveryone: false
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send auto-reply to the user
|
|
|
|
if (config.responseMessage) {
|
|
|
|
newThread.postToUser(config.responseMessage);
|
|
|
|
}
|
2018-02-14 01:53:34 -05:00
|
|
|
}
|
|
|
|
|
2017-12-31 19:16:05 -05:00
|
|
|
// Post some info to the beginning of the new thread
|
2018-04-21 08:38:21 -04:00
|
|
|
const infoHeaderItems = [];
|
|
|
|
|
|
|
|
// Account age
|
|
|
|
const accountAge = humanizeDuration(Date.now() - user.createdAt, {largest: 2, round: true});
|
|
|
|
infoHeaderItems.push(`ACCOUNT AGE **${accountAge}**`);
|
|
|
|
|
|
|
|
// User id
|
|
|
|
infoHeaderItems.push(`ID **${user.id}**`);
|
|
|
|
|
|
|
|
let infoHeader = infoHeaderItems.join(', ');
|
|
|
|
|
|
|
|
// Guild info
|
|
|
|
const guildInfoHeaderItems = new Map();
|
2018-05-03 12:37:50 -04:00
|
|
|
const mainGuilds = utils.getMainGuilds();
|
|
|
|
|
|
|
|
mainGuilds.forEach(guild => {
|
2018-04-21 08:38:21 -04:00
|
|
|
const member = guild.members.get(user.id);
|
|
|
|
if (! member) return;
|
2017-12-24 15:04:08 -05:00
|
|
|
|
2018-04-21 08:38:21 -04:00
|
|
|
const {nickname, joinDate} = getHeaderGuildInfo(member);
|
|
|
|
guildInfoHeaderItems.set(guild.name, [
|
|
|
|
`NICKNAME **${nickname}**`,
|
|
|
|
`JOINED **${joinDate}** ago`
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
guildInfoHeaderItems.forEach((items, guildName) => {
|
2018-05-03 12:37:50 -04:00
|
|
|
if (mainGuilds.length === 1) {
|
2018-04-21 08:38:21 -04:00
|
|
|
infoHeader += `\n${items.join(', ')}`;
|
|
|
|
} else {
|
|
|
|
infoHeader += `\n**[${guildName}]** ${items.join(', ')}`;
|
|
|
|
}
|
|
|
|
});
|
2017-12-31 19:16:05 -05:00
|
|
|
|
|
|
|
const userLogCount = await getClosedThreadCountByUserId(user.id);
|
2018-04-21 08:38:21 -04:00
|
|
|
if (userLogCount > 0) {
|
|
|
|
infoHeader += `\n\nThis user has **${userLogCount}** previous modmail threads. Use \`${config.prefix}logs\` to see them.`;
|
|
|
|
}
|
|
|
|
|
|
|
|
infoHeader += '\n────────────────';
|
2017-12-31 19:16:05 -05:00
|
|
|
|
|
|
|
await newThread.postSystemMessage(infoHeader);
|
|
|
|
|
|
|
|
// Return the thread
|
|
|
|
return newThread;
|
2017-12-24 15:04:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new thread row in the database
|
|
|
|
* @param {Object} data
|
|
|
|
* @returns {Promise<String>} The ID of the created thread
|
|
|
|
*/
|
2017-12-31 19:16:05 -05:00
|
|
|
async function createThreadInDB(data) {
|
2017-12-24 15:04:08 -05:00
|
|
|
const threadId = uuid.v4();
|
|
|
|
const now = moment.utc().format('YYYY-MM-DD HH:mm:ss');
|
|
|
|
const finalData = Object.assign({created_at: now, is_legacy: 0}, data, {id: threadId});
|
|
|
|
|
2017-12-31 19:16:05 -05:00
|
|
|
await knex('threads').insert(finalData);
|
2017-12-24 15:04:08 -05:00
|
|
|
|
|
|
|
return threadId;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {String} channelId
|
|
|
|
* @returns {Promise<Thread>}
|
|
|
|
*/
|
2017-12-31 19:16:05 -05:00
|
|
|
async function findByChannelId(channelId) {
|
2017-12-24 15:04:08 -05:00
|
|
|
const thread = await knex('threads')
|
|
|
|
.where('channel_id', channelId)
|
|
|
|
.first();
|
|
|
|
|
|
|
|
return (thread ? new Thread(thread) : null);
|
|
|
|
}
|
|
|
|
|
2018-02-11 14:54:30 -05:00
|
|
|
/**
|
|
|
|
* @param {String} channelId
|
|
|
|
* @returns {Promise<Thread>}
|
|
|
|
*/
|
|
|
|
async function findOpenThreadByChannelId(channelId) {
|
|
|
|
const thread = await knex('threads')
|
|
|
|
.where('channel_id', channelId)
|
|
|
|
.where('status', THREAD_STATUS.OPEN)
|
|
|
|
.first();
|
|
|
|
|
|
|
|
return (thread ? new Thread(thread) : null);
|
|
|
|
}
|
|
|
|
|
2018-03-11 16:27:52 -04:00
|
|
|
/**
|
|
|
|
* @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);
|
|
|
|
}
|
|
|
|
|
2017-12-24 15:04:08 -05:00
|
|
|
/**
|
2017-12-31 19:16:05 -05:00
|
|
|
* @param {String} userId
|
|
|
|
* @returns {Promise<Thread[]>}
|
2017-12-24 15:04:08 -05:00
|
|
|
*/
|
2017-12-31 19:16:05 -05:00
|
|
|
async function getClosedThreadsByUserId(userId) {
|
|
|
|
const threads = await knex('threads')
|
|
|
|
.where('status', THREAD_STATUS.CLOSED)
|
|
|
|
.where('user_id', userId)
|
|
|
|
.select();
|
|
|
|
|
|
|
|
return threads.map(thread => new Thread(thread));
|
2017-12-24 15:04:08 -05:00
|
|
|
}
|
|
|
|
|
2017-12-31 19:16:05 -05:00
|
|
|
/**
|
|
|
|
* @param {String} userId
|
|
|
|
* @returns {Promise<number>}
|
|
|
|
*/
|
|
|
|
async function getClosedThreadCountByUserId(userId) {
|
|
|
|
const row = await knex('threads')
|
|
|
|
.where('status', THREAD_STATUS.CLOSED)
|
|
|
|
.where('user_id', userId)
|
|
|
|
.first(knex.raw('COUNT(id) AS thread_count'));
|
2017-12-24 15:04:08 -05:00
|
|
|
|
2017-12-31 19:16:05 -05:00
|
|
|
return parseInt(row.thread_count, 10);
|
|
|
|
}
|
|
|
|
|
2018-02-11 14:54:30 -05:00
|
|
|
async function findOrCreateThreadForUser(user) {
|
|
|
|
const existingThread = await findOpenThreadByUserId(user.id);
|
|
|
|
if (existingThread) return existingThread;
|
|
|
|
|
|
|
|
return createNewThreadForUser(user);
|
|
|
|
}
|
|
|
|
|
2018-03-11 15:32:14 -04:00
|
|
|
async function getThreadsThatShouldBeClosed() {
|
|
|
|
const now = moment.utc().format('YYYY-MM-DD HH:mm:ss');
|
|
|
|
const threads = await knex('threads')
|
|
|
|
.where('status', THREAD_STATUS.OPEN)
|
|
|
|
.whereNotNull('scheduled_close_at')
|
|
|
|
.where('scheduled_close_at', '<=', now)
|
2018-03-11 16:36:52 -04:00
|
|
|
.whereNotNull('scheduled_close_at')
|
2018-03-11 15:32:14 -04:00
|
|
|
.select();
|
|
|
|
|
|
|
|
return threads.map(thread => new Thread(thread));
|
|
|
|
}
|
|
|
|
|
2017-12-31 19:16:05 -05:00
|
|
|
module.exports = {
|
2018-02-11 14:54:30 -05:00
|
|
|
findById,
|
2017-12-31 19:16:05 -05:00
|
|
|
findOpenThreadByUserId,
|
|
|
|
findByChannelId,
|
2018-02-11 14:54:30 -05:00
|
|
|
findOpenThreadByChannelId,
|
2018-03-11 16:27:52 -04:00
|
|
|
findSuspendedThreadByChannelId,
|
2017-12-31 19:16:05 -05:00
|
|
|
createNewThreadForUser,
|
|
|
|
getClosedThreadsByUserId,
|
2018-02-11 14:54:30 -05:00
|
|
|
findOrCreateThreadForUser,
|
2018-03-11 15:32:14 -04:00
|
|
|
getThreadsThatShouldBeClosed,
|
2018-02-11 14:54:30 -05:00
|
|
|
createThreadInDB
|
2017-12-24 15:04:08 -05:00
|
|
|
};
|