From c1cb5a4ed7f59550d821a6f9133d668704847417 Mon Sep 17 00:00:00 2001 From: Dragory <2606411+Dragory@users.noreply.github.com> Date: Sun, 1 Dec 2019 00:37:46 +0200 Subject: [PATCH] search: add -ids switch to list result ids; fix reactions from other messages affecting search results --- backend/src/plugins/Utility.ts | 37 +++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/backend/src/plugins/Utility.ts b/backend/src/plugins/Utility.ts index b4153df0..2b1f2e4c 100644 --- a/backend/src/plugins/Utility.ts +++ b/backend/src/plugins/Utility.ts @@ -80,6 +80,8 @@ type TConfigSchema = t.TypeOf; const { performance } = require("perf_hooks"); const SEARCH_RESULTS_PER_PAGE = 15; +const SEARCH_ID_RESULTS_PER_PAGE = 50; + const MAX_CLEAN_COUNT = 150; const MAX_CLEAN_TIME = 1 * DAYS; const CLEAN_COMMAND_DELETE_DELAY = 5000; @@ -417,6 +419,10 @@ export class UtilityPlugin extends ZeppelinPlugin { shortcut: "e", isSwitch: true, }, + { + name: "ids", + isSwitch: true, + }, ], extra: { info: { @@ -446,9 +452,10 @@ export class UtilityPlugin extends ZeppelinPlugin { sort?: string; "case-sensitive"?: boolean; export?: boolean; + ids?: boolean; }, ) { - const formatSearchResultLines = (members: Member[]) => { + const formatSearchResultList = (members: Member[]): string => { const longestId = members.reduce((longest, member) => Math.max(longest, member.id.length), 0); const lines = members.map(member => { const paddedId = member.id.padEnd(longestId, " "); @@ -456,7 +463,11 @@ export class UtilityPlugin extends ZeppelinPlugin { if (member.nick) line += ` (${member.nick})`; return line; }); - return lines; + return lines.join("\n"); + }; + + const formatSearchResultIdList = (members: Member[]): string => { + return members.map(m => m.id).join(" "); }; // If we're exporting the results, we don't need all the fancy schmancy pagination stuff. @@ -467,12 +478,13 @@ export class UtilityPlugin extends ZeppelinPlugin { return this.sendErrorMessage(msg.channel, "No results found"); } - const resultLines = formatSearchResultLines(results.results); + const resultList = args.ids ? formatSearchResultIdList(results.results) : formatSearchResultList(results.results); + const archiveId = await this.archives.create( trimLines(` Search results (total ${results.totalResults}): - ${resultLines.join("\n")} + ${resultList} `), moment().add(1, "hour"), ); @@ -491,6 +503,8 @@ export class UtilityPlugin extends ZeppelinPlugin { let clearReactionsFn = null; let clearReactionsTimeout = null; + const perPage = args.ids ? SEARCH_ID_RESULTS_PER_PAGE : SEARCH_RESULTS_PER_PAGE; + const loadSearchPage = async page => { if (searching) return; searching = true; @@ -505,23 +519,27 @@ export class UtilityPlugin extends ZeppelinPlugin { searchMsgPromise.then(m => (originalSearchMsg = m)); } - const searchResult = await this.performMemberSearch(args, page, SEARCH_RESULTS_PER_PAGE); + const searchResult = await this.performMemberSearch(args, page, perPage); if (searchResult.totalResults === 0) { return this.sendErrorMessage(msg.channel, "No results found"); } const resultWord = searchResult.totalResults === 1 ? "matching member" : "matching members"; const headerText = - searchResult.totalResults > SEARCH_RESULTS_PER_PAGE + searchResult.totalResults > perPage ? trimLines(` **Page ${searchResult.page}** (${searchResult.from}-${searchResult.to}) (total ${searchResult.totalResults}) `) : `Found ${searchResult.totalResults} ${resultWord}`; - const lines = formatSearchResultLines(searchResult.results); + + const resultList = args.ids + ? formatSearchResultIdList(searchResult.results) + : formatSearchResultList(searchResult.results); + const result = trimLines(` ${headerText} \`\`\`js - ${lines.join("\n")} + ${resultList} \`\`\` `); @@ -529,7 +547,7 @@ export class UtilityPlugin extends ZeppelinPlugin { searchMsg.edit(result); // Set up pagination reactions if needed. The reactions are cleared after a timeout. - if (searchResult.totalResults > SEARCH_RESULTS_PER_PAGE) { + if (searchResult.totalResults > perPage) { if (!hasReactions) { hasReactions = true; searchMsg.addReaction("⬅"); @@ -537,6 +555,7 @@ export class UtilityPlugin extends ZeppelinPlugin { searchMsg.addReaction("🔄"); const removeListenerFn = this.on("messageReactionAdd", (rMsg: Message, emoji, userId) => { + if (rMsg.id !== searchMsg.id) return; if (userId !== msg.author.id) return; if (!["⬅", "➡", "🔄"].includes(emoji.name)) return;