shared/domain/resource/
report.rs

1//! Types for Resource short codes for sharing
2use chrono::{DateTime, Utc};
3use macros::make_path_parts;
4use serde::{Deserialize, Serialize};
5use strum_macros::EnumIter;
6use uuid::Uuid;
7
8use crate::api::endpoints::PathPart;
9
10use super::ResourceId;
11
12wrap_uuid! {
13    /// Wrapper type around [`Uuid`](Uuid), represents the ID of a curation comment.
14    pub struct ReportId
15}
16
17make_path_parts!(GetResourceReportPath => "/v1/resource/{}/report/{}" => ResourceId, ReportId);
18
19/// Resource report details
20#[derive(Serialize, Deserialize, Clone, Debug)]
21#[cfg_attr(feature = "backend", derive(sqlx::Type))]
22#[serde(rename_all = "camelCase")]
23pub struct ResourceReport {
24    /// Id of report
25    pub id: ReportId,
26
27    /// Id of reported resource
28    pub resource_id: ResourceId,
29
30    /// Type of     report
31    pub report_type: ResourceReportType,
32
33    /// Optional id of reporter
34    #[serde(skip_serializing_if = "Option::is_none")]
35    pub reporter_id: Option<Uuid>,
36
37    /// Optional name for reporter
38    #[serde(skip_serializing_if = "Option::is_none")]
39    pub reporter_name: Option<String>,
40
41    /// Optional email of reporter
42    #[serde(skip_serializing_if = "Option::is_none")]
43    pub reporter_email: Option<String>,
44
45    /// When report was submitted
46    pub created_at: DateTime<Utc>,
47}
48
49make_path_parts!(CreateResourceReportPath => "/v1/resource/{}/report" => ResourceId);
50
51/// Request for reporting a resource
52#[derive(Serialize, Deserialize, Debug)]
53#[serde(rename_all = "camelCase")]
54pub struct CreateResourceReport {
55    /// Description of the resource.
56    pub report_type: ResourceReportType,
57}
58
59/// Request for reporting a resource
60#[derive(Serialize, Deserialize, Debug)]
61#[serde(rename_all = "camelCase")]
62pub struct ResourceReportEmail {
63    /// Display name of the resource.
64    pub display_name: String,
65
66    /// Report type of the report.
67    pub report_type: ResourceReportType,
68
69    /// Optional name for reporter
70    pub reporter_name: Option<String>,
71
72    /// Optional email of reporter
73    pub reporter_email: Option<String>,
74
75    /// Creator name of resource
76    pub creator_name: String,
77}
78
79/// Type of report
80#[derive(Serialize, Deserialize, Copy, Clone, Eq, PartialEq, Debug, EnumIter)]
81#[cfg_attr(feature = "backend", derive(sqlx::Type))]
82#[serde(rename_all = "camelCase")]
83#[repr(i16)]
84pub enum ResourceReportType {
85    #[allow(missing_docs)]
86    Offensive = 0,
87    #[allow(missing_docs)]
88    CopyrightInfringement = 1,
89    #[allow(missing_docs)]
90    Spam = 2,
91    #[allow(missing_docs)]
92    Privacy = 3,
93    #[allow(missing_docs)]
94    ResourceNotPlaying = 4,
95    #[allow(missing_docs)]
96    Other = 5,
97}
98
99impl ResourceReportType {
100    #[allow(missing_docs)]
101    pub fn as_str(&self) -> &'static str {
102        match self {
103            ResourceReportType::Offensive => "Offensive",
104            ResourceReportType::CopyrightInfringement => "Copyright Infringement",
105            ResourceReportType::Spam => "Spam",
106            ResourceReportType::Privacy => "Privacy",
107            ResourceReportType::ResourceNotPlaying => "Resource Can't Be Viewed",
108            ResourceReportType::Other => "Other",
109        }
110    }
111
112    #[allow(missing_docs)]
113    pub fn to_value_str(&self) -> String {
114        serde_json::to_string(&self).unwrap()
115    }
116
117    #[allow(missing_docs)]
118    pub fn from_value_str(s: &str) -> Self {
119        serde_json::from_str(s).unwrap()
120    }
121}