-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogsearch.js
82 lines (73 loc) · 2.76 KB
/
logsearch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
module.exports = function ({ knex, commands, logs, threads, config }) {
const logsAreLinks = config.logStorage === "local" ? true : false;
const actualLogsearchCommand = async (msg, args, thread) => {
const userIdToSearch = args.userId || (thread && thread.user_id);
let globalSearch = false;
if (userIdToSearch === "global") {
globalSearch = true;
}
const toSearch = args["toSearch"];
let matchingMessages;
if (globalSearch) {
matchingMessages = await knex
.distinct("*")
.from("thread_messages")
.innerJoin("threads", "threads.id", "thread_messages.thread_id")
.where("body", "like", `%${toSearch}%`)
.whereNot("message_type", 1)
.whereNot("message_type", 6)
.whereNot("message_type", 7);
} else {
matchingMessages = await knex
.distinct("*")
.from("thread_messages")
.innerJoin("threads", "threads.id", "thread_messages.thread_id")
.where("threads.user_id", userIdToSearch)
.where("body", "like", `%${toSearch}%`)
.whereNot("message_type", 1)
.whereNot("message_type", 6)
.whereNot("message_type", 7);
}
let formatted = "";
const handledUrls = [];
if (logsAreLinks) {
for (const matchingThread of matchingMessages) {
if (handledUrls.includes(matchingThread.thread_id)) continue;
handledUrls.push(matchingThread.thread_id);
const urlThread = await threads.findById(matchingThread.thread_id);
formatted += `${await logs.getLogUrl(urlThread)} (Thread #${matchingThread.thread_number}, ${urlThread.user_name})\n`;
}
} else {
for (const matchingThread of matchingMessages) {
if (handledUrls.includes(matchingThread.thread_id)) continue;
handledUrls.push(matchingThread.thread_id);
formatted += `Thread #${matchingThread.thread_number} (${matchingThread.user_name})\n`;
}
}
if (matchingMessages.length === 0) {
msg.channel.createMessage({
content: `No logs contain the text \`${toSearch}\``,
messageReferenceID: msg.id,
});
} else {
let toSend = `The following ${handledUrls.length} logs contain the text \`${toSearch}\`:\n${formatted}`;
toSend = toSend.match(/(.|[\r\n]){1,1900}[\n$]/g);
for (const chunk of toSend) {
msg.channel.createMessage({ content: chunk });
}
}
};
commands.addInboxServerCommand(
"logsearch",
[
{ name: "userId", type: "string", required: true },
{ name: "toSearch", type: "string", required: true, catchAll: true },
],
actualLogsearchCommand
);
commands.addInboxThreadCommand(
"logsearch",
[{ name: "toSearch", type: "string", required: true, catchAll: true }],
actualLogsearchCommand
);
};