From 40a20f94c10353560b78103c6394b0845c8e91c8 Mon Sep 17 00:00:00 2001 From: Dragory Date: Sat, 24 Nov 2018 19:14:12 +0200 Subject: [PATCH] ModActions: fix errors when listing too many cases --- src/plugins/ModActions.ts | 18 ++++++++++++++---- src/plugins/Utility.ts | 8 +++++--- src/utils.ts | 40 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 7 deletions(-) diff --git a/src/plugins/ModActions.ts b/src/plugins/ModActions.ts index 9e969e1f..f404c31b 100644 --- a/src/plugins/ModActions.ts +++ b/src/plugins/ModActions.ts @@ -5,6 +5,8 @@ import humanizeDuration from "humanize-duration"; import chunk from "lodash.chunk"; import { GuildCases } from "../data/GuildCases"; import { + chunkLines, + chunkMessageLines, convertDelayStringToMS, DBDateFormat, disableLinkPreviews, @@ -862,6 +864,11 @@ export class ModActionsPlugin extends Plugin { msg.channel.createMessage("No cases found for the specified user!"); } else { if (args.expanded && args.expanded.startsWith("expand")) { + if (cases.length > 8) { + msg.channel.createMessage("Too many cases for expanded view. Please use compact view instead."); + return; + } + // Expanded view (= individual case embeds) for (const theCase of cases) { await this.displayCase(theCase.id, msg.channel.id); @@ -889,13 +896,16 @@ export class ModActionsPlugin extends Plugin { const finalMessage = trimLines(` Cases for **${userName}**: - + ${lines.join("\n")} - - Use \`${prefix}case \` to see more info about individual cases + + Use \`${prefix}case \` to see more info about individual cases `); - msg.channel.createMessage(finalMessage); + const finalMessageChunks = chunkMessageLines(finalMessage); + for (const msgChunk of finalMessageChunks) { + msg.channel.createMessage(msgChunk); + } } } } diff --git a/src/plugins/Utility.ts b/src/plugins/Utility.ts index 41345201..5c8bae6d 100644 --- a/src/plugins/Utility.ts +++ b/src/plugins/Utility.ts @@ -259,18 +259,20 @@ export class UtilityPlugin extends Plugin { const cases = await this.cases.getByUserId(args.userId); if (cases.length > 0) { cases.sort((a, b) => { - return a.created_at < b.created_at ? -1 : 1; + return a.created_at < b.created_at ? 1 : -1; }); - const caseSummaries = cases.map(c => { + const caseSummary = cases.slice(0, 3).map(c => { return `${CaseTypes[c.type]} (#${c.case_number})`; }); + const summaryText = cases.length > 3 ? "Last 3 cases" : "Summary"; + embed.fields.push({ name: "Cases", value: trimLines(` Total cases: ${cases.length} - Summary: ${caseSummaries.join(", ")} + ${summaryText}: ${caseSummary.join(", ")} `) }); } diff --git a/src/utils.ts b/src/utils.ts index 04dbae25..b874aaf7 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -229,4 +229,44 @@ export function disableCodeBlocks(content: string): string { return content.replace(/`/g, "`\u200b"); } +export function chunkLines(str: string, maxChunkLength = 2000): string[] { + if (str.length < maxChunkLength) { + return [str]; + } + + const chunks = []; + + while (str.length) { + if (str.length <= maxChunkLength) { + chunks.push(str); + break; + } + + const slice = str.slice(0, maxChunkLength); + + const lastLineBreakIndex = slice.lastIndexOf("\n"); + if (lastLineBreakIndex === -1) { + chunks.push(str.slice(0, maxChunkLength)); + str = str.slice(maxChunkLength); + } else { + chunks.push(str.slice(0, lastLineBreakIndex)); + str = str.slice(lastLineBreakIndex + 1); + } + } + + return chunks; +} + +/** + * Chunks a long message to multiple smaller messages, retaining leading and trailing line breaks + */ +export function chunkMessageLines(str: string): string[] { + const chunks = chunkLines(str, 1999); + return chunks.map(chunk => { + if (chunk[0] === "\n") chunk = "\u200b" + chunk; + if (chunk[chunk.length - 1] === "\n") chunk = chunk + "\u200b"; + return chunk; + }); +} + export const DBDateFormat = "YYYY-MM-DD HH:mm:ss";