From 1fbaca83f5037434c34e00f4b831cea555466610 Mon Sep 17 00:00:00 2001 From: GreenAsJade Date: Wed, 27 Sep 2023 18:00:23 +0930 Subject: [PATCH] wip: small refactor to make the moderator action `vote` use the same route as other moderator actions. tsx needed for report_manager to pop up a toast. --- .../{report_manager.ts => report_manager.tsx} | 45 +++++++++++++++++-- .../ModerationActionSelector.tsx | 1 + src/views/ReportsCenter/ViewReport.tsx | 33 ++++++-------- 3 files changed, 57 insertions(+), 22 deletions(-) rename src/lib/{report_manager.ts => report_manager.tsx} (88%) diff --git a/src/lib/report_manager.ts b/src/lib/report_manager.tsx similarity index 88% rename from src/lib/report_manager.ts rename to src/lib/report_manager.tsx index 9605e80a9e..c2a5527393 100644 --- a/src/lib/report_manager.ts +++ b/src/lib/report_manager.tsx @@ -20,19 +20,28 @@ * which is used by our IncidentReportTracker widget and our ReportsCenter view. */ +import * as React from "react"; import * as data from "data"; import * as preferences from "preferences"; +import { toast } from "toast"; import { alert } from "swal_config"; import { socket } from "sockets"; +import { pgettext } from "translate"; import { ReportedConversation } from "Report"; import { PlayerCacheEntry } from "player_cache"; import { EventEmitter } from "eventemitter3"; import { emitNotification } from "Notifications"; import { browserHistory } from "ogsHistory"; import { get, post } from "requests"; +import { MOD_POWER_HANDLE_SCORE_CHEAT } from "./misc"; export const DAILY_REPORT_GOAL = 10; +interface Vote { + voter_id: number; + action: string; +} + export interface Report { // TBD put this into /models, in a suitable namespace? // TBD: relationship between this and SeverToClient['incident-report'] @@ -66,7 +75,7 @@ export interface Report { automod_to_reported?: string; available_actions: Array; // community moderator actions - voters: Array; // community moderators who've voted on this report + voters: Vote[]; // votes from community moderators on this report unclaim: () => void; claim: () => void; @@ -152,7 +161,10 @@ class ReportManager extends EventEmitter { } } - if (report.state === "resolved" || (report.voters && report.voters.includes(user.id))) { + if ( + report.state === "resolved" || + report.voters?.some((vote) => vote.voter_id === user.id) + ) { delete this.active_incident_reports[report.id]; } else { this.active_incident_reports[report.id] = report; @@ -195,7 +207,18 @@ class ReportManager extends EventEmitter { if (this.getIgnored(report.id)) { return false; } - if (!user.is_moderator && !(report.report_type === "score_cheating")) { + if (!user.is_moderator && !user.moderator_powers) { + return false; + } + if ( + !user.is_moderator && + user.moderator_powers && + (!( + report.report_type === "score_cheating" && + user.moderator_powers & MOD_POWER_HANDLE_SCORE_CHEAT + ) || + report.voters?.some((vote) => vote.voter_id === user.id)) + ) { return false; } return !report.moderator || report.moderator?.id === user.id; @@ -329,6 +352,22 @@ class ReportManager extends EventEmitter { this.updateIncidentReport(res); return res; } + public async vote(report_id: number, voted_action: string) { + console.log(voted_action); + const res = await post(`moderation/incident/${report_id}`, { + action: "vote", // darn, yes, two different uses of the word "action" collide here + voted_action: voted_action, + }).then((res) => { + toast( +
+ {pgettext("Thanking a community moderator for voting", "Submitted, thanks!")} +
, + 2000, + ); + return res; + }); + this.updateIncidentReport(res); + } public getHandledTodayCount(): number { return data.get("user").reports_handled_today || 0; diff --git a/src/views/ReportsCenter/ModerationActionSelector.tsx b/src/views/ReportsCenter/ModerationActionSelector.tsx index d4b155d7ec..1dcbb349e2 100644 --- a/src/views/ReportsCenter/ModerationActionSelector.tsx +++ b/src/views/ReportsCenter/ModerationActionSelector.tsx @@ -65,6 +65,7 @@ export function ModerationActionSelector({ checked={selectedOption === a} value={a} onChange={updateSelectedAction} + disabled={!enable} /> diff --git a/src/views/ReportsCenter/ViewReport.tsx b/src/views/ReportsCenter/ViewReport.tsx index bd410937c8..e17c758b75 100644 --- a/src/views/ReportsCenter/ViewReport.tsx +++ b/src/views/ReportsCenter/ViewReport.tsx @@ -18,7 +18,6 @@ import * as React from "react"; import * as moment from "moment"; import Select from "react-select"; -import { toast } from "toast"; import { useUser } from "hooks"; import { report_categories } from "Report"; import { report_manager, Report } from "report_manager"; @@ -230,23 +229,6 @@ export function ViewReport({ report_id, reports, onChange }: ViewReportProps): J } }; - const onActionSubmitted = (action: string) => { - console.log(action); - post(`moderation/action_vote/${report.id}`, { action: action }) - .then(() => { - toast( -
- {pgettext( - "Thanking a community moderator for voting", - "Submitted, thanks!", - )} -
, - 2000, - ); - }) - .catch(errorAlerter); - }; - return (
@@ -437,7 +419,10 @@ export function ViewReport({ report_id, reports, onChange }: ViewReportProps): J
{ + void report_manager.vote(report.id, action); + next(); + }} enable={claimed_by_me} />
@@ -462,6 +447,16 @@ export function ViewReport({ report_id, reports, onChange }: ViewReportProps): J )} + {report.voters.length > 0 && ( + <> +

{_("Voters:")}

+ {report.voters.map((vote) => ( +
  • + : {vote.action} +
  • + ))} + + )}