Skip to main content

zaun_community_protocol/command/
report.rs

1//! Report commands (specification §8.10). Most respond with a
2//! [`Report`].
3
4use serde::{Deserialize, Serialize};
5
6use super::object::{Outcome, Report, ReportCategory, ReportStatus, ReportTarget};
7use super::{BodyError, Validate, check};
8use crate::ids::{MessageId, ReportId};
9use crate::limits::{PAGE_SMALL, REASON_MAX, REPORT_DESCRIPTION_MAX, REPORT_EVIDENCE_MAX};
10
11/// `report.create` (§8.10).
12#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
13pub struct Create {
14    /// The category.
15    pub category: ReportCategory,
16    /// ≤ 2 000 scalars.
17    #[serde(default, skip_serializing_if = "Option::is_none")]
18    pub description: Option<String>,
19    /// The reported member or message.
20    pub target: ReportTarget,
21    /// 0–20 messages selected by the reporter, each once.
22    pub evidence_message_ids: Vec<MessageId>,
23}
24
25/// `report.create` response.
26#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
27pub struct Created {
28    /// The new report.
29    pub report_id: ReportId,
30}
31
32/// `report.list` (§8.10).
33#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
34pub struct List {
35    /// Only this status.
36    #[serde(default, skip_serializing_if = "Option::is_none")]
37    pub status: Option<ReportStatus>,
38    /// Page start (exclusive).
39    #[serde(default, skip_serializing_if = "Option::is_none")]
40    pub after: Option<ReportId>,
41    /// 1–100.
42    pub limit: u32,
43}
44
45/// `report.list` response: `reports`, `next?` (§8.10).
46#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
47pub struct Page {
48    /// Reports visible to the moderator.
49    pub reports: Vec<Report>,
50    /// The `after` for the next page; absent on the last page.
51    #[serde(default, skip_serializing_if = "Option::is_none")]
52    pub next: Option<ReportId>,
53}
54
55/// `report.get` (§8.10).
56#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
57pub struct Get {
58    /// The report.
59    pub report_id: ReportId,
60    /// The first evidence item whose snapshot to return (0–19, default 0): the previous
61    /// answer's `evidence_next`.
62    #[serde(default, skip_serializing_if = "is_zero")]
63    pub evidence_from: u32,
64}
65
66#[allow(clippy::trivially_copy_pass_by_ref)]
67const fn is_zero(n: &u32) -> bool {
68    *n == 0
69}
70
71/// `report.review` (§8.10): `open` → `reviewing`.
72#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
73pub struct Review {
74    /// The report.
75    pub report_id: ReportId,
76}
77
78/// `report.resolve` (§8.10): `note?` is ≤ 512 scalars.
79#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
80pub struct Resolve {
81    /// The report.
82    pub report_id: ReportId,
83    /// The outcome.
84    pub outcome: Outcome,
85    /// ≤ 512 scalars.
86    #[serde(default, skip_serializing_if = "Option::is_none")]
87    pub note: Option<String>,
88}
89
90impl Validate for Create {
91    fn validate(&self) -> Result<(), BodyError> {
92        check::opt_text(
93            "description",
94            self.description.as_deref(),
95            (0, REPORT_DESCRIPTION_MAX),
96        )?;
97        check::count(
98            "evidence_message_ids",
99            self.evidence_message_ids.len(),
100            REPORT_EVIDENCE_MAX,
101        )?;
102        check::unique("evidence_message_ids", &self.evidence_message_ids)
103    }
104}
105
106impl Validate for List {
107    fn validate(&self) -> Result<(), BodyError> {
108        check::page(self.limit, PAGE_SMALL)
109    }
110}
111
112impl Validate for Get {
113    fn validate(&self) -> Result<(), BodyError> {
114        let last = u32::try_from(REPORT_EVIDENCE_MAX - 1).unwrap_or(u32::MAX);
115        check::range("evidence_from", self.evidence_from, 0, last)
116    }
117}
118
119impl Validate for Review {
120    fn validate(&self) -> Result<(), BodyError> {
121        Ok(())
122    }
123}
124
125impl Validate for Resolve {
126    fn validate(&self) -> Result<(), BodyError> {
127        check::opt_text("note", self.note.as_deref(), (0, REASON_MAX))
128    }
129}