Skip to main content

zulip_api/message/report_message/
request.rs

1use serde::{Deserialize, Serialize};
2
3use crate::{field_access, impl_default, impl_display, ReportType};
4
5/// Represents the request for the `Report Message` Zulip API endpoint.
6///
7/// See [report-message](https://zulip.com/api/report-message) for details.
8#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
9pub struct Request {
10    #[serde(skip)]
11    message_id: u64,
12    report_type: ReportType,
13    description: Option<String>,
14}
15
16impl Request {
17    /// Creates a new [Request].
18    pub const fn new() -> Self {
19        Self {
20            message_id: 0,
21            report_type: ReportType::new(),
22            description: None,
23        }
24    }
25}
26
27field_access! {
28    Request {
29        /// The target message's ID.
30        message_id: u64,
31        /// The reason that best describes why the current user is reporting the target message for moderation.
32        report_type: ReportType,
33    }
34}
35
36field_access! {
37    Request {
38        /// A short description with additional context about why the current user is reporting the target message for moderation.
39        ///
40        /// Clients should limit this string to a maximum length of 1000 characters.
41        ///
42        /// If the `report_type` parameter is ["other"](ReportType::Other), this parameter is required, and its value cannot be an empty string.
43        description: option_cow_deref { str, String },
44    }
45}
46
47impl_default!(Request);
48impl_display!(Request);
49
50#[cfg(test)]
51mod tests {
52    use super::*;
53
54    #[test]
55    fn test_request() {
56        let message_id = 1;
57        let report_type = ReportType::new();
58        let description = "test description";
59
60        let mut request = Request::new()
61            .with_message_id(message_id)
62            .with_report_type(report_type)
63            .with_description(description);
64        let request_json = request.to_string();
65
66        assert_eq!(request.message_id(), message_id);
67        assert_eq!(request.report_type(), report_type);
68        assert_eq!(request.description(), Some(description));
69
70        assert_eq!(serde_json::to_string(&request).unwrap(), request_json);
71        request.set_message_id(0);
72        assert_eq!(
73            serde_json::from_str::<Request>(&request_json).unwrap(),
74            request
75        );
76    }
77}