Skip to content

Commit

Permalink
wip: small refactor to make the moderator action vote use the same …
Browse files Browse the repository at this point in the history
…route as other moderator actions. tsx needed for report_manager to pop up a toast.
  • Loading branch information
GreenAsJade committed Sep 27, 2023
1 parent 2ad8a85 commit 1fbaca8
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 22 deletions.
45 changes: 42 additions & 3 deletions src/lib/report_manager.ts → src/lib/report_manager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down Expand Up @@ -66,7 +75,7 @@ export interface Report {
automod_to_reported?: string;

available_actions: Array<string>; // community moderator actions
voters: Array<number>; // community moderators who've voted on this report
voters: Vote[]; // votes from community moderators on this report

unclaim: () => void;
claim: () => void;
Expand Down Expand Up @@ -152,7 +161,10 @@ class ReportManager extends EventEmitter<Events> {
}
}

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;
Expand Down Expand Up @@ -195,7 +207,18 @@ class ReportManager extends EventEmitter<Events> {
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;
Expand Down Expand Up @@ -329,6 +352,22 @@ class ReportManager extends EventEmitter<Events> {
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(
<div>
{pgettext("Thanking a community moderator for voting", "Submitted, thanks!")}
</div>,
2000,
);
return res;
});
this.updateIncidentReport(res);
}

public getHandledTodayCount(): number {
return data.get("user").reports_handled_today || 0;
Expand Down
1 change: 1 addition & 0 deletions src/views/ReportsCenter/ModerationActionSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export function ModerationActionSelector({
checked={selectedOption === a}
value={a}
onChange={updateSelectedAction}
disabled={!enable}
/>
<label htmlFor={a}>{ACTION_PROMPTS[a]}</label>
</div>
Expand Down
33 changes: 14 additions & 19 deletions src/views/ReportsCenter/ViewReport.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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(
<div>
{pgettext(
"Thanking a community moderator for voting",
"Submitted, thanks!",
)}
</div>,
2000,
);
})
.catch(errorAlerter);
};

return (
<div id="ViewReport">
<div className="header">
Expand Down Expand Up @@ -437,7 +419,10 @@ export function ViewReport({ report_id, reports, onChange }: ViewReportProps): J
<div className="voting">
<ModerationActionSelector
report={report}
submit={onActionSubmitted}
submit={(action) => {
void report_manager.vote(report.id, action);
next();
}}
enable={claimed_by_me}
/>
</div>
Expand All @@ -462,6 +447,16 @@ export function ViewReport({ report_id, reports, onChange }: ViewReportProps): J
</ul>
</>
)}
{report.voters.length > 0 && (
<>
<h4>{_("Voters:")}</h4>
{report.voters.map((vote) => (
<li key={vote.voter_id}>
<Player user={vote.voter_id} />: {vote.action}
</li>
))}
</>
)}
</div>

<div className="actions-right">
Expand Down

0 comments on commit 1fbaca8

Please sign in to comment.