mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2026-11-19 23:23:12 +09:00
Reports were the last legacy control module, kept in the lua state of each instance. They are now an exp_reports plugin which stores them on the controller: a report is immutable and holds the reported player, who made it, the reason, the instance name and when it was made, and the only operations are get, list, create and delete. In game nothing is stored. The lua module sends each request over ipc, the instance plugin asks the controller and hands the answer back with rcon, and the module prints it to whoever asked if they are still online. Answers are dropped if the instance stopped meanwhile. The module raises on_player_reported with the count and reporters of every report against the player, which report_jail and discord_alerts use, and the commands and player list button call into it. The web ui gets a /reports page: a table with search on the player, reporter and reason, a filter on the instance, delete per row and a create form which reports in the name of the web user. New reports are posted to two optional webhooks configured on the controller, a Discord channel hook as an embed and a generic JSON hook. Failures are logged rather than failing the report. Tests cover the messages, the controller against a real Controller, the instance plugin against a real Instance, and the lua module through the shared harness.
146 lines
4.8 KiB
TypeScript
146 lines
4.8 KiB
TypeScript
import React, { useContext, useState } from "react";
|
|
import { Button, Form, Input, Modal, Popconfirm, Table } from "antd";
|
|
|
|
import {
|
|
ControlContext, PageHeader, PageLayout, notifyErrorHandler, useAccount,
|
|
useColumnSearch, useTableQueryState,
|
|
} from "@clusterio/web_ui";
|
|
|
|
import { ReportCreateRequest, ReportDeleteRequest, ReportRecord } from "../../messages";
|
|
import type { WebPlugin } from "..";
|
|
|
|
const strcmp = new Intl.Collator(undefined, { numeric: true, sensitivity: "base" }).compare;
|
|
|
|
/** Modal which reports a player in the name of the web user. */
|
|
function CreateReportButton() {
|
|
const control = useContext(ControlContext);
|
|
const [open, setOpen] = useState(false);
|
|
const [form] = Form.useForm<{ playerName: string, reason: string }>();
|
|
|
|
async function createReport() {
|
|
const values = await form.validateFields();
|
|
await control.send(new ReportCreateRequest(values.playerName, values.reason));
|
|
form.resetFields();
|
|
setOpen(false);
|
|
}
|
|
|
|
return <>
|
|
<Button type="primary" onClick={() => setOpen(true)}>Create</Button>
|
|
<Modal
|
|
title="Report Player"
|
|
okText="Report"
|
|
open={open}
|
|
onOk={() => { createReport().catch(notifyErrorHandler("Error creating report")); }}
|
|
onCancel={() => setOpen(false)}
|
|
destroyOnHidden
|
|
>
|
|
<Form form={form} layout="vertical">
|
|
<Form.Item name="playerName" label="Player" rules={[{ required: true }]}>
|
|
<Input />
|
|
</Form.Item>
|
|
<Form.Item name="reason" label="Reason" rules={[{ required: true }]}>
|
|
<Input.TextArea rows={3} />
|
|
</Form.Item>
|
|
</Form>
|
|
</Modal>
|
|
</>;
|
|
}
|
|
|
|
/** Table of every report with search on the player, reporter and reason, and a filter on the instance. */
|
|
function ReportsTable() {
|
|
const control = useContext(ControlContext);
|
|
const account = useAccount();
|
|
const plugin = control.plugins.get("exp_reports") as WebPlugin;
|
|
const [reports, synced] = plugin.useReports();
|
|
|
|
const tableState = useTableQueryState<ReportRecord>({
|
|
namespace: "report",
|
|
defaultSortKey: "time",
|
|
pagination: { defaultPageSize: 50 },
|
|
});
|
|
const playerSearch = useColumnSearch<ReportRecord>(tableState, "player", report => report.playerName, "Search players");
|
|
const bySearch = useColumnSearch<ReportRecord>(tableState, "by", report => report.byPlayerName, "Search reporters");
|
|
const reasonSearch = useColumnSearch<ReportRecord>(tableState, "reason", report => report.reason, "Search reasons");
|
|
|
|
const data = [...reports.values()];
|
|
const instanceNames = [...new Set(data.map(report => report.instanceName))].sort(strcmp);
|
|
const instanceFilters = instanceNames.map(name => ({ text: name || "Web UI", value: name }));
|
|
const canDelete = account.hasPermission("exp_reports.report.delete");
|
|
|
|
return <Table
|
|
size="small"
|
|
columns={[
|
|
{
|
|
title: "Player",
|
|
key: "player",
|
|
render: (_, report) => report.playerName,
|
|
sorter: (a, b) => strcmp(a.playerName, b.playerName),
|
|
sortOrder: tableState.sortOrder("player"),
|
|
filteredValue: tableState.filteredValue("player"),
|
|
...playerSearch,
|
|
},
|
|
{
|
|
title: "Reported by",
|
|
key: "by",
|
|
render: (_, report) => report.byPlayerName,
|
|
sorter: (a, b) => strcmp(a.byPlayerName, b.byPlayerName),
|
|
sortOrder: tableState.sortOrder("by"),
|
|
filteredValue: tableState.filteredValue("by"),
|
|
...bySearch,
|
|
},
|
|
{
|
|
title: "Reason",
|
|
key: "reason",
|
|
render: (_, report) => report.reason,
|
|
filteredValue: tableState.filteredValue("reason"),
|
|
...reasonSearch,
|
|
},
|
|
{
|
|
title: "Instance",
|
|
key: "instance",
|
|
render: (_, report) => report.instanceName || "Web UI",
|
|
filters: instanceFilters,
|
|
filteredValue: tableState.filteredValue("instance"),
|
|
onFilter: (value, report) => report.instanceName === value,
|
|
sorter: (a, b) => strcmp(a.instanceName, b.instanceName),
|
|
sortOrder: tableState.sortOrder("instance"),
|
|
},
|
|
{
|
|
title: "Time",
|
|
key: "time",
|
|
render: (_, report) => new Date(report.updatedAtMs).toLocaleString(),
|
|
sorter: (a, b) => a.updatedAtMs - b.updatedAtMs,
|
|
sortOrder: tableState.sortOrder("time"),
|
|
defaultSortOrder: "descend",
|
|
},
|
|
...canDelete ? [{
|
|
key: "actions",
|
|
render: (_: unknown, report: ReportRecord) => <Popconfirm
|
|
title="Delete this report?"
|
|
onConfirm={() => {
|
|
control.send(new ReportDeleteRequest(report.id)).catch(notifyErrorHandler("Error deleting report"));
|
|
}}
|
|
>
|
|
<Button size="small" danger onClick={event => event.stopPropagation()}>Delete</Button>
|
|
</Popconfirm>,
|
|
}] : [],
|
|
]}
|
|
dataSource={data}
|
|
loading={!synced}
|
|
rowKey={report => report.id}
|
|
pagination={tableState.pagination}
|
|
onChange={tableState.onChange}
|
|
/>;
|
|
}
|
|
|
|
export default function ReportsPage() {
|
|
const account = useAccount();
|
|
return <PageLayout nav={[{ name: "Reports" }]}>
|
|
<PageHeader
|
|
title="Reports"
|
|
extra={account.hasPermission("exp_reports.report.create") ? <CreateReportButton /> : undefined}
|
|
/>
|
|
<ReportsTable />
|
|
</PageLayout>;
|
|
}
|