From f6f1c29fc1dad5c8dbf7f3e7894b5c1477954d4e Mon Sep 17 00:00:00 2001 From: Dragory Date: Tue, 15 Jan 2019 04:15:22 +0200 Subject: [PATCH] Add user id and show recent cases in 'member joined with prior records' log entry --- src/data/DefaultLogMessages.json | 2 +- src/data/GuildCases.ts | 30 ++++++++++++++++++++++++++++++ src/plugins/Logs.ts | 21 +++++++++++++++++++-- src/plugins/ModActions.ts | 25 ++----------------------- 4 files changed, 52 insertions(+), 26 deletions(-) diff --git a/src/data/DefaultLogMessages.json b/src/data/DefaultLogMessages.json index 11cf9fc3..8f1b57a7 100644 --- a/src/data/DefaultLogMessages.json +++ b/src/data/DefaultLogMessages.json @@ -45,5 +45,5 @@ "MASSBAN": "⚒ **{mod.username}#{mod.discriminator}** massbanned {count} users", - "MEMBER_JOIN_WITH_PRIOR_RECORDS": "⚠ **{member.user.username}#{member.user.discriminator}** joined with prior records ({caseCount} case(s))" + "MEMBER_JOIN_WITH_PRIOR_RECORDS": "⚠ **{member.user.username}#{member.user.discriminator}** (`{member.id}`) joined with prior records. Recent cases:\n{recentCaseSummary}" } diff --git a/src/data/GuildCases.ts b/src/data/GuildCases.ts index 0a09a28a..e3cbc696 100644 --- a/src/data/GuildCases.ts +++ b/src/data/GuildCases.ts @@ -2,6 +2,10 @@ import { Case } from "./entities/Case"; import { CaseNote } from "./entities/CaseNote"; import { BaseRepository } from "./BaseRepository"; import { getRepository, In, Repository } from "typeorm"; +import { disableLinkPreviews } from "../utils"; +import { CaseTypes } from "./CaseTypes"; + +const CASE_SUMMARY_REASON_MAX_LENGTH = 300; export class GuildCases extends BaseRepository { private cases: Repository; @@ -82,4 +86,30 @@ export class GuildCases extends BaseRepository { case_id: caseId }); } + + getSummaryText(theCase: Case) { + const firstNote = theCase.notes[0]; + let reason = firstNote ? firstNote.body : ""; + + if (reason.length > CASE_SUMMARY_REASON_MAX_LENGTH) { + const match = reason.slice(CASE_SUMMARY_REASON_MAX_LENGTH, 100).match(/(?:[.,!?\s]|$)/); + const nextWhitespaceIndex = match ? CASE_SUMMARY_REASON_MAX_LENGTH + match.index : CASE_SUMMARY_REASON_MAX_LENGTH; + if (nextWhitespaceIndex < reason.length) { + reason = reason.slice(0, nextWhitespaceIndex - 1) + "..."; + } + } + + reason = disableLinkPreviews(reason); + + let line = `Case \`#${theCase.case_number}\` __${CaseTypes[theCase.type]}__ ${reason}`; + if (theCase.notes.length > 1) { + line += ` *(+${theCase.notes.length - 1} ${theCase.notes.length === 2 ? "note" : "notes"})*`; + } + + if (theCase.is_hidden) { + line += " *(hidden)*"; + } + + return line; + } } diff --git a/src/plugins/Logs.ts b/src/plugins/Logs.ts index daed6c0e..69bca158 100644 --- a/src/plugins/Logs.ts +++ b/src/plugins/Logs.ts @@ -134,12 +134,29 @@ export class LogsPlugin extends Plugin { account_age: accountAge }); - const cases = (await this.cases.getByUserId(member.id)).filter(c => !c.is_hidden); + const cases = (await this.cases.with("notes").getByUserId(member.id)).filter(c => !c.is_hidden); + cases.sort((a, b) => (a.created_at > b.created_at ? -1 : 1)); if (cases.length) { + const recentCaseLines = []; + const recentCases = cases.slice(0, 2); + for (const theCase of recentCases) { + recentCaseLines.push(this.cases.getSummaryText(theCase)); + } + + let recentCaseSummary = recentCaseLines.join("\n"); + if (recentCases.length < cases.length) { + const remaining = cases.length - recentCases.length; + if (remaining === 1) { + recentCaseSummary += `\n*+${remaining} case*`; + } else { + recentCaseSummary += `\n*+${remaining} cases*`; + } + } + this.guildLogs.log(LogType.MEMBER_JOIN_WITH_PRIOR_RECORDS, { member: stripObjectToScalars(member, ["user"]), - caseCount: cases.length + recentCaseSummary }); } } diff --git a/src/plugins/ModActions.ts b/src/plugins/ModActions.ts index d5deb9a0..e231fcf6 100644 --- a/src/plugins/ModActions.ts +++ b/src/plugins/ModActions.ts @@ -922,29 +922,8 @@ export class ModActionsPlugin extends ZeppelinPlugin { const lines = []; for (const theCase of casesToDisplay) { theCase.notes.sort((a, b) => (a.created_at > b.created_at ? 1 : -1)); - const firstNote = theCase.notes[0]; - let reason = firstNote ? firstNote.body : ""; - - if (reason.length > CASE_LIST_REASON_MAX_LENGTH) { - const match = reason.slice(CASE_LIST_REASON_MAX_LENGTH, 100).match(/(?:[.,!?\s]|$)/); - const nextWhitespaceIndex = match ? CASE_LIST_REASON_MAX_LENGTH + match.index : CASE_LIST_REASON_MAX_LENGTH; - if (nextWhitespaceIndex < reason.length) { - reason = reason.slice(0, nextWhitespaceIndex - 1) + "..."; - } - } - - reason = disableLinkPreviews(reason); - - let line = `Case \`#${theCase.case_number}\` __${CaseTypes[theCase.type]}__ ${reason}`; - if (theCase.notes.length > 1) { - line += ` *(+${theCase.notes.length - 1} ${theCase.notes.length === 2 ? "note" : "notes"})*`; - } - - if (theCase.is_hidden) { - line += " *(hidden)*"; - } - - lines.push(line); + const caseSummary = this.cases.getSummaryText(theCase); + lines.push(caseSummary); } if (!showHidden && hiddenCases.length) {