Add verbose and simple options to logs

master
Dragory 2020-05-25 01:54:26 +03:00
parent d23bc05bcb
commit f8ddf97b53
No known key found for this signature in database
GPG Key ID: 5F387BA66DF8AAC1
2 changed files with 74 additions and 18 deletions

View File

@ -64,6 +64,24 @@ module.exports = ({ bot, knex, config, commands }) => {
} }
const logUrl = await thread.getLogUrl(); const logUrl = await thread.getLogUrl();
thread.postSystemMessage(`Log URL: ${logUrl}`); const query = [];
if (args.verbose) query.push('verbose=1');
if (args.simple) query.push('simple=1');
let qs = query.length ? `?${query.join('&')}` : '';
thread.postSystemMessage(`Log URL: ${logUrl}${qs}`);
}, {
options: [
{
name: 'verbose',
shortcut: 'v',
isSwitch: true,
},
{
name: 'simple',
shortcut: 's',
isSwitch: true,
},
],
}); });
}; };

View File

@ -2,6 +2,7 @@ const http = require('http');
const mime = require('mime'); const mime = require('mime');
const url = require('url'); const url = require('url');
const fs = require('fs'); const fs = require('fs');
const qs = require('querystring');
const moment = require('moment'); const moment = require('moment');
const config = require('../config'); const config = require('../config');
const threads = require('../data/threads'); const threads = require('../data/threads');
@ -14,14 +15,26 @@ function notfound(res) {
res.end('Page Not Found'); res.end('Page Not Found');
} }
async function serveLogs(res, pathParts) { async function serveLogs(req, res, pathParts, query) {
const threadId = pathParts[pathParts.length - 1]; const threadId = pathParts[pathParts.length - 1];
if (threadId.match(/^[0-9a-f\-]+$/) === null) return notfound(res); if (threadId.match(/^[0-9a-f\-]+$/) === null) return notfound(res);
const thread = await threads.findById(threadId); const thread = await threads.findById(threadId);
if (! thread) return notfound(res); if (! thread) return notfound(res);
const threadMessages = await thread.getThreadMessages(); let threadMessages = await thread.getThreadMessages();
if (query.simple) {
threadMessages = threadMessages.filter(message => {
return (
message.message_type !== THREAD_MESSAGE_TYPE.SYSTEM
&& message.message_type !== THREAD_MESSAGE_TYPE.SYSTEM_TO_USER
&& message.message_type !== THREAD_MESSAGE_TYPE.CHAT
&& message.message_type !== THREAD_MESSAGE_TYPE.COMMAND
);
});
}
const lines = threadMessages.map(message => { const lines = threadMessages.map(message => {
// Legacy messages are the entire log in one message, so just serve them as they are // Legacy messages are the entire log in one message, so just serve them as they are
if (message.message_type === THREAD_MESSAGE_TYPE.LEGACY) { if (message.message_type === THREAD_MESSAGE_TYPE.LEGACY) {
@ -30,6 +43,16 @@ async function serveLogs(res, pathParts) {
let line = `[${moment.utc(message.created_at).format('YYYY-MM-DD HH:mm:ss')}]`; let line = `[${moment.utc(message.created_at).format('YYYY-MM-DD HH:mm:ss')}]`;
if (query.verbose) {
if (message.dm_channel_id) {
line += ` [DM CHA ${message.dm_channel_id}]`;
}
if (message.dm_message_id) {
line += ` [DM MSG ${message.dm_message_id}]`;
}
}
if (message.message_type === THREAD_MESSAGE_TYPE.FROM_USER) { if (message.message_type === THREAD_MESSAGE_TYPE.FROM_USER) {
line += ` [FROM USER] [${message.user_name}] ${message.body}`; line += ` [FROM USER] [${message.user_name}] ${message.body}`;
} else if (message.message_type === THREAD_MESSAGE_TYPE.TO_USER) { } else if (message.message_type === THREAD_MESSAGE_TYPE.TO_USER) {
@ -49,11 +72,25 @@ async function serveLogs(res, pathParts) {
return line; return line;
}); });
res.setHeader('Content-Type', 'text/plain; charset=UTF-8'); const openedAt = moment(thread.created_at).format('YYYY-MM-DD HH:mm:ss');
res.end(lines.join('\n')); let header = `# ModMail thread with ${thread.user_name} (${thread.user_id}) started at ${openedAt}.`;
header += `\n# All times are in UTC+0.`;
if (! query.simple) {
header += `\n# Add ?simple=1 to the end of the log link to view only messages from the user and replies.`;
} }
function serveAttachments(res, pathParts) { if (! query.verbose) {
header += `\n# Add ?verbose=1 to the end of the log link to view message IDs.`;
}
const fullResponse = header + '\n\n' + lines.join('\n');
res.setHeader('Content-Type', 'text/plain; charset=UTF-8');
res.end(fullResponse);
}
function serveAttachments(req, res, pathParts) {
const desiredFilename = pathParts[pathParts.length - 1]; const desiredFilename = pathParts[pathParts.length - 1];
const id = pathParts[pathParts.length - 2]; const id = pathParts[pathParts.length - 2];
@ -78,12 +115,13 @@ function serveAttachments(res, pathParts) {
module.exports = () => { module.exports = () => {
const server = http.createServer((req, res) => { const server = http.createServer((req, res) => {
const parsedUrl = url.parse(`http://${req.url}`); const parsedUrl = url.parse(`http://${req.url}`);
const pathParts = parsedUrl.path.split('/').filter(v => v !== ''); const pathParts = parsedUrl.pathname.split('/').filter(v => v !== '');
const query = qs.parse(parsedUrl.query);
if (parsedUrl.path.startsWith('/logs/')) { if (parsedUrl.pathname.startsWith('/logs/')) {
serveLogs(res, pathParts); serveLogs(req, res, pathParts, query);
} else if (parsedUrl.path.startsWith('/attachments/')) { } else if (parsedUrl.pathname.startsWith('/attachments/')) {
serveAttachments(res, pathParts); serveAttachments(req, res, pathParts, query);
} else { } else {
notfound(res); notfound(res);
} }