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)]
44#[allow(deprecated)]
45mod tests {
46    use super::*;
47    use wacore_binary::jid::Jid;
48
49    #[test]
50    fn test_spam_flow_as_str() {
51        assert_eq!(SpamFlow::MessageMenu.as_str(), "MessageMenu");
52        assert_eq!(
53            SpamFlow::GroupSpamBannerReport.as_str(),
54            "GroupSpamBannerReport"
55        );
56        assert_eq!(SpamFlow::ContactInfo.as_str(), "ContactInfo");
57    }
58
59    #[test]
60    fn test_build_spam_list_node_basic() {
61        let request = SpamReportRequest {
62            message_id: "TEST123".to_string(),
63            message_timestamp: 1234567890,
64            spam_flow: SpamFlow::MessageMenu,
65            ..Default::default()
66        };
67
68        let node = build_spam_list_node(&request);
69
70        assert_eq!(node.tag, "spam_list");
71        assert_eq!(node.attrs().string("spam_flow"), "MessageMenu");
72
73        let message = node
74            .get_optional_child_by_tag(&["message"])
75            .expect("spam_list node should have message child");
76        assert_eq!(message.attrs().string("id"), "TEST123");
77        assert_eq!(message.attrs().string("t"), "1234567890");
78    }
79
80    #[test]
81    fn test_build_spam_list_node_with_raw_message() {
82        let request = SpamReportRequest {
83            message_id: "TEST456".to_string(),
84            message_timestamp: 1234567890,
85            from_jid: Some(Jid::pn("5511999887766")),
86            spam_flow: SpamFlow::MessageMenu,
87            raw_message: Some(vec![0x01, 0x02, 0x03]),
88            media_type: Some("image".to_string()),
89            ..Default::default()
90        };
91
92        let node = build_spam_list_node(&request);
93        let message = node
94            .get_optional_child_by_tag(&["message"])
95            .expect("spam_list node should have message child");
96        let raw = message
97            .get_optional_child_by_tag(&["raw"])
98            .expect("message node should have raw child");
99
100        assert_eq!(raw.attrs().string("v"), "3");
101        assert_eq!(raw.attrs().string("mediatype"), "image");
102    }
103
104    #[test]
105    fn test_build_spam_list_node_group() {
106        let request = SpamReportRequest {
107            message_id: "TEST789".to_string(),
108            message_timestamp: 1234567890,
109            group_jid: Some(Jid::group("120363025918861132")),
110            group_subject: Some("Test Group".to_string()),
111            participant_jid: Some(Jid::pn("5511999887766")),
112            spam_flow: SpamFlow::GroupInfoReport,
113            ..Default::default()
114        };
115
116        let node = build_spam_list_node(&request);
117
118        assert_eq!(node.attrs().string("spam_flow"), "GroupInfoReport");
119        assert_eq!(node.attrs().string("jid"), "120363025918861132@g.us");
120        assert_eq!(node.attrs().string("subject"), "Test Group");
121    }
122}