forge
Explore
Log in

Don't you dare go Hollow

yumush

Clone
★ 0

0 watching0 forksRustAGPL-3.06.8 MiB27ceb05Tahinli8 hours ago399 commits

report.rs126 lines, 3.4 KiBHistoryRawDownload
1use common::{
2 constant::LOG_PAGE_LIMIT,
3 model::{Id, Report, ReportStatus},
4 time::now_ms,
5};
6
7use super::{Connection, all, log::optional_user, one, run};
8use crate::error::ServerError;
9
10struct Raw {
11 id: Id,
12 created_at: u64,
13 reporter_id: Option<Id>,
14 target_id: Option<Id>,
15 community: Option<String>,
16 message_id: Option<Id>,
17 community_id: Option<Id>,
18 body: Option<String>,
19 reason: String,
20 status: ReportStatus,
21 handled_by: Option<Id>,
22}
23
24pub async fn is_open(
25 reporter_id: Id,
26 target_id: Id,
27 message_id: Option<Id>,
28 db: &Connection,
29) -> Result<bool, ServerError> {
30 one(
31 db,
32 "SELECT EXISTS (SELECT 1 FROM report WHERE reporter_id = ?1 AND target_id = ?2 AND message_id IS ?3 AND status = ?4)",
33 (reporter_id, target_id, message_id, ReportStatus::Open.as_str()),
34 |r| r.get(),
35 )
36 .await
37}
38
39pub async fn insert(
40 reporter_id: Id,
41 target_id: Id,
42 message_id: Option<Id>,
43 community_id: Option<Id>,
44 body: Option<&str>,
45 reason: &str,
46 db: &Connection,
47) -> Result<(), ServerError> {
48 run(
49 db,
50 "INSERT INTO report (created_at, reporter_id, target_id, message_id, community_id, body, reason, status) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)",
51 (
52 now_ms(),
53 reporter_id,
54 target_id,
55 message_id,
56 community_id,
57 body,
58 reason,
59 ReportStatus::Open.as_str(),
60 ),
61 )
62 .await?;
63
64 Ok(())
65}
66
67pub async fn list(
68 status: Option<ReportStatus>,
69 db: &Connection,
70) -> Result<Vec<Report>, ServerError> {
71 let raws = all(
72 db,
73 "SELECT r.id, r.created_at, r.reporter_id, r.target_id, c.name, r.message_id, r.community_id, r.body, r.reason, r.status, r.handled_by FROM report r LEFT JOIN community c ON c.id = r.community_id WHERE ?1 IS NULL OR r.status = ?1 ORDER BY r.id DESC LIMIT ?2",
74 (status.map(ReportStatus::as_str), LOG_PAGE_LIMIT),
75 |r| {
76 Ok(Raw {
77 id: r.get()?,
78 created_at: r.get()?,
79 reporter_id: r.get()?,
80 target_id: r.get()?,
81 community: r.get()?,
82 message_id: r.get()?,
83 community_id: r.get()?,
84 body: r.get()?,
85 reason: r.get()?,
86 status: r.named()?,
87 handled_by: r.get()?,
88 })
89 },
90 )
91 .await?;
92
93 let mut reports = Vec::new();
94 for raw in raws {
95 reports.push(Report {
96 id: raw.id,
97 created_at: raw.created_at,
98 reporter: optional_user(raw.reporter_id, db).await?,
99 target: optional_user(raw.target_id, db).await?,
100 message_id: raw.message_id,
101 community_id: raw.community_id,
102 community: raw.community,
103 body: raw.body,
104 reason: raw.reason,
105 status: raw.status,
106 handled_by: optional_user(raw.handled_by, db).await?,
107 });
108 }
109
110 Ok(reports)
111}
112
113pub async fn handle(
114 report_id: Id,
115 status: ReportStatus,
116 handled_by: Id,
117 db: &Connection,
118) -> Result<Option<Id>, ServerError> {
119 one(
120 db,
121 "UPDATE report SET status = ?2, handled_by = ?3 WHERE id = ?1 AND status = ?4 RETURNING target_id",
122 (report_id, status.as_str(), handled_by, ReportStatus::Open.as_str()),
123 |r| r.get(),
124 )
125 .await
126}