Skip to main content

whatsapp_rust/
spam_report.rs

1//! Spam reporting feature.
2//!
3//! Types and IQ specification are defined in `wacore::iq::spam_report`.
4
5use crate::client::Client;
6use crate::request::IqError;
7use wacore::iq::spam_report::SpamReportSpec;
8
9// Re-export types from wacore
10pub use wacore::types::{SpamFlow, SpamReportRequest, SpamReportResult, build_spam_list_node};
11
12impl Client {
13    /// Send a spam report to WhatsApp.
14    ///
15    /// This sends a `spam_list` IQ stanza to report one or more messages as spam.
16    ///
17    /// # Arguments
18    /// * `request` - The spam report request containing message details
19    ///
20    /// # Returns
21    /// * `Ok(SpamReportResult)` - If the report was successfully submitted
22    /// * `Err` - If there was an error sending or processing the report
23    ///
24    /// # Example
25    /// ```rust,ignore
26    /// let result = client.send_spam_report(SpamReportRequest {
27    ///     message_id: "MSG_ID".to_string(),
28    ///     message_timestamp: 1234567890,
29    ///     from_jid: Some(sender_jid),
30    ///     spam_flow: SpamFlow::MessageMenu,
31    ///     ..Default::default()
32    /// }).await?;
33    /// ```
34    pub async fn send_spam_report(
35        &self,
36        request: SpamReportRequest,
37    ) -> Result<SpamReportResult, IqError> {
38        let spec = SpamReportSpec::new(request);
39        self.execute(spec).await
40    }
41}
42
43#[cfg(test)]
44mod tests {
45    use super::*;
46    use wacore_binary::jid::Jid;
47
48    #[test]
49    fn test_spam_flow_as_str() {
50        assert_eq!(SpamFlow::MessageMenu.as_str(), "MessageMenu");
51        assert_eq!(
52            SpamFlow::GroupSpamBannerReport.as_str(),
53            "GroupSpamBannerReport"
54        );
55        assert_eq!(SpamFlow::ContactInfo.as_str(), "ContactInfo");
56    }
57
58    #[test]
59    fn test_build_spam_list_node_basic() {
60        let request = SpamReportRequest {
61            message_id: "TEST123".to_string(),
62            message_timestamp: 1234567890,
63            spam_flow: SpamFlow::MessageMenu,
64            ..Default::default()
65        };
66
67        let node = build_spam_list_node(&request);
68
69        assert_eq!(node.tag, "spam_list");
70        assert!(
71            node.attrs
72                .get("spam_flow")
73                .is_some_and(|v| v == "MessageMenu")
74        );
75
76        let message = node
77            .get_optional_child_by_tag(&["message"])
78            .expect("spam_list node should have message child");
79        assert!(message.attrs.get("id").is_some_and(|v| v == "TEST123"));
80        assert!(message.attrs.get("t").is_some_and(|v| v == "1234567890"));
81    }
82
83    #[test]
84    fn test_build_spam_list_node_with_raw_message() {
85        let request = SpamReportRequest {
86            message_id: "TEST456".to_string(),
87            message_timestamp: 1234567890,
88            from_jid: Some(Jid::pn("5511999887766")),
89            spam_flow: SpamFlow::MessageMenu,
90            raw_message: Some(vec![0x01, 0x02, 0x03]),
91            media_type: Some("image".to_string()),
92            ..Default::default()
93        };
94
95        let node = build_spam_list_node(&request);
96        let message = node
97            .get_optional_child_by_tag(&["message"])
98            .expect("spam_list node should have message child");
99        let raw = message
100            .get_optional_child_by_tag(&["raw"])
101            .expect("message node should have raw child");
102
103        assert!(raw.attrs.get("v").is_some_and(|v| v == "3"));
104        assert!(raw.attrs.get("mediatype").is_some_and(|v| v == "image"));
105    }
106
107    #[test]
108    fn test_build_spam_list_node_group() {
109        let request = SpamReportRequest {
110            message_id: "TEST789".to_string(),
111            message_timestamp: 1234567890,
112            group_jid: Some(Jid::group("120363025918861132")),
113            group_subject: Some("Test Group".to_string()),
114            participant_jid: Some(Jid::pn("5511999887766")),
115            spam_flow: SpamFlow::GroupInfoReport,
116            ..Default::default()
117        };
118
119        let node = build_spam_list_node(&request);
120
121        assert!(
122            node.attrs
123                .get("spam_flow")
124                .is_some_and(|v| v == "GroupInfoReport")
125        );
126        assert!(
127            node.attrs
128                .get("jid")
129                .is_some_and(|v| v == "120363025918861132@g.us")
130        );
131        assert!(node.attrs.get("subject").is_some_and(|v| v == "Test Group"));
132    }
133}