Add thread numbers

cshd
Dragory 2020-11-01 21:41:03 +02:00
parent f6825376c0
commit 280fad36f7
No known key found for this signature in database
GPG Key ID: 5F387BA66DF8AAC1
5 changed files with 226 additions and 176 deletions

View File

@ -0,0 +1,12 @@
exports.up = async function(knex) {
await knex.schema.table("threads", table => {
table.integer("thread_number");
table.unique("thread_number");
});
};
exports.down = async function(knex) {
await knex.schema.table("threads", table => {
table.dropColumn("thread_number");
});
};

View File

@ -0,0 +1,16 @@
exports.up = async function(knex) {
const threads = await knex.table("threads")
.orderBy("created_at", "ASC")
.select(["id"]);
let threadNumber = 0;
for (const { id } of threads) {
await knex.table("threads")
.where("id", id)
.update({ thread_number: ++threadNumber });
}
};
exports.down = async function(knex) {
// Nothing
};

View File

@ -19,6 +19,18 @@ const {THREAD_STATUS, DISOCRD_CHANNEL_TYPES} = require("./constants");
const MINUTES = 60 * 1000; const MINUTES = 60 * 1000;
const HOURS = 60 * MINUTES; const HOURS = 60 * MINUTES;
let threadCreationQueue = Promise.resolve();
function _addToThreadCreationQueue(fn) {
threadCreationQueue = threadCreationQueue
.then(fn)
.catch(err => {
console.error(`Error while creating thread: ${err.message}`);
});
return threadCreationQueue;
}
/** /**
* @param {String} id * @param {String} id
* @returns {Promise<Thread>} * @returns {Promise<Thread>}
@ -69,6 +81,7 @@ function getHeaderGuildInfo(member) {
* @throws {Error} * @throws {Error}
*/ */
async function createNewThreadForUser(user, opts = {}) { async function createNewThreadForUser(user, opts = {}) {
return _addToThreadCreationQueue(async () => {
const quiet = opts.quiet != null ? opts.quiet : false; const quiet = opts.quiet != null ? opts.quiet : false;
const ignoreRequirements = opts.ignoreRequirements != null ? opts.ignoreRequirements : false; const ignoreRequirements = opts.ignoreRequirements != null ? opts.ignoreRequirements : false;
const ignoreHooks = opts.ignoreHooks != null ? opts.ignoreHooks : false; const ignoreHooks = opts.ignoreHooks != null ? opts.ignoreHooks : false;
@ -267,6 +280,7 @@ async function createNewThreadForUser(user, opts = {}) {
// Return the thread // Return the thread
return newThread; return newThread;
});
} }
/** /**
@ -277,7 +291,15 @@ async function createNewThreadForUser(user, opts = {}) {
async function createThreadInDB(data) { async function createThreadInDB(data) {
const threadId = uuid.v4(); const threadId = uuid.v4();
const now = moment.utc().format("YYYY-MM-DD HH:mm:ss"); const now = moment.utc().format("YYYY-MM-DD HH:mm:ss");
const finalData = Object.assign({created_at: now, is_legacy: 0}, data, {id: threadId}); const latestThreadNumberRow = await knex("threads")
.orderBy("thread_number", "DESC")
.first();
const latestThreadNumber = latestThreadNumberRow ? latestThreadNumberRow.thread_number : 0;
const finalData = Object.assign(
{created_at: now, is_legacy: 0},
data,
{id: threadId, thread_number: latestThreadNumber + 1}
);
await knex("threads").insert(finalData); await knex("threads").insert(finalData);

View File

@ -44,7 +44,7 @@ module.exports = ({ bot, knex, config, commands }) => {
await thread.close(false, thread.scheduled_close_silent); await thread.close(false, thread.scheduled_close_silent);
await sendCloseNotification(thread, `Modmail thread with ${thread.user_name} (${thread.user_id}) was closed as scheduled by ${thread.scheduled_close_name}`); await sendCloseNotification(thread, `Modmail thread #${thread.thread_number} with ${thread.user_name} (${thread.user_id}) was closed as scheduled by ${thread.scheduled_close_name}`);
} }
} }
@ -145,7 +145,7 @@ module.exports = ({ bot, knex, config, commands }) => {
await thread.close(suppressSystemMessages, silentClose); await thread.close(suppressSystemMessages, silentClose);
await sendCloseNotification(thread, `Modmail thread with ${thread.user_name} (${thread.user_id}) was closed by ${closedBy}`); await sendCloseNotification(thread, `Modmail thread #${thread.thread_number} with ${thread.user_name} (${thread.user_id}) was closed by ${closedBy}`);
}); });
// Auto-close threads if their channel is deleted // Auto-close threads if their channel is deleted
@ -164,6 +164,6 @@ module.exports = ({ bot, knex, config, commands }) => {
await thread.close(true); await thread.close(true);
await sendCloseNotification(thread, `Modmail thread with ${thread.user_name} (${thread.user_id}) was closed automatically because the channel was deleted`); await sendCloseNotification(thread, `Modmail thread #${thread.thread_number} with ${thread.user_name} (${thread.user_id}) was closed automatically because the channel was deleted`);
}); });
}; };

View File

@ -48,7 +48,7 @@ module.exports = ({ bot, knex, config, commands, hooks }) => {
? `<${addOptQueryStringToUrl(logUrl, args)}>` ? `<${addOptQueryStringToUrl(logUrl, args)}>`
: `View log with \`${config.prefix}log ${thread.id}\`` : `View log with \`${config.prefix}log ${thread.id}\``
const formattedDate = moment.utc(thread.created_at).format("MMM Do [at] HH:mm [UTC]"); const formattedDate = moment.utc(thread.created_at).format("MMM Do [at] HH:mm [UTC]");
return `\`${formattedDate}\`: ${formattedLogUrl}`; return `\`#${thread.thread_number}\` \`${formattedDate}\`: ${formattedLogUrl}`;
})); }));
let message = isPaginated let message = isPaginated