aboutsummaryrefslogtreecommitdiff
path: root/src/modules/logs.js
diff options
context:
space:
mode:
authorSin-MacBook <[email protected]>2020-08-10 23:44:20 +0200
committerSin-MacBook <[email protected]>2020-08-10 23:44:20 +0200
commit2a53887abba882bf7b63aeda8dfa55fdb3ab8792 (patch)
treead7a95eb41faa6ff13c3142285cdc0eb3ca92183 /src/modules/logs.js
downloadmodmail-2a53887abba882bf7b63aeda8dfa55fdb3ab8792.tar.xz
modmail-2a53887abba882bf7b63aeda8dfa55fdb3ab8792.zip
clean this up when home
Diffstat (limited to 'src/modules/logs.js')
-rw-r--r--src/modules/logs.js69
1 files changed, 69 insertions, 0 deletions
diff --git a/src/modules/logs.js b/src/modules/logs.js
new file mode 100644
index 0000000..0664c07
--- /dev/null
+++ b/src/modules/logs.js
@@ -0,0 +1,69 @@
+const threads = require("../data/threads");
+const moment = require('moment');
+const utils = require("../utils");
+
+const LOG_LINES_PER_PAGE = 10;
+
+module.exports = ({ bot, knex, config, commands }) => {
+ const logsCmd = async (msg, args, thread) => {
+ let userId = args.userId || (thread && thread.user_id);
+ if (! userId) return;
+
+ let userThreads = await threads.getClosedThreadsByUserId(userId);
+
+ // Descending by date
+ userThreads.sort((a, b) => {
+ if (a.created_at > b.created_at) return -1;
+ if (a.created_at < b.created_at) return 1;
+ return 0;
+ });
+
+ // Pagination
+ const totalUserThreads = userThreads.length;
+ const maxPage = Math.ceil(totalUserThreads / LOG_LINES_PER_PAGE);
+ const inputPage = args.page;
+ const page = Math.max(Math.min(inputPage ? parseInt(inputPage, 10) : 1, maxPage), 1); // Clamp page to 1-<max page>
+ const isPaginated = totalUserThreads > LOG_LINES_PER_PAGE;
+ const start = (page - 1) * LOG_LINES_PER_PAGE;
+ const end = page * LOG_LINES_PER_PAGE;
+ userThreads = userThreads.slice((page - 1) * LOG_LINES_PER_PAGE, page * LOG_LINES_PER_PAGE);
+
+ const threadLines = await Promise.all(userThreads.map(async thread => {
+ const logUrl = await thread.getLogUrl();
+ const formattedDate = moment.utc(thread.created_at).format('MMM Do [at] HH:mm [UTC]');
+ return `\`${formattedDate}\`: <${logUrl}>`;
+ }));
+
+ let message = isPaginated
+ ? `**Log files for <@${userId}>** (page **${page}/${maxPage}**, showing logs **${start + 1}-${end}/${totalUserThreads}**):`
+ : `**Log files for <@${userId}>:**`;
+
+ message += `\n${threadLines.join('\n')}`;
+
+ if (isPaginated) {
+ message += `\nTo view more, add a page number to the end of the command`;
+ }
+
+ // Send the list of logs in chunks of 15 lines per message
+ const lines = message.split('\n');
+ const chunks = utils.chunk(lines, 15);
+
+ let root = Promise.resolve();
+ chunks.forEach(lines => {
+ root = root.then(() => msg.channel.createMessage(lines.join('\n')));
+ });
+ };
+
+ commands.addInboxServerCommand('logs', '<userId:userId> [page:number]', logsCmd);
+ commands.addInboxServerCommand('logs', '[page:number]', logsCmd);
+
+ commands.addInboxServerCommand('loglink', [], async (msg, args, thread) => {
+ if (! thread) {
+ thread = await threads.findSuspendedThreadByChannelId(msg.channel.id);
+ if (! thread) return;
+ }
+
+ const logUrl = await thread.getLogUrl();
+ thread.postSystemMessage(`Log URL: ${logUrl}`);
+ });
+};