Skip to main content

kick_api/api/
moderation.rs

1use crate::error::{KickApiError, Result};
2use crate::models::{BanRequest, UnbanRequest};
3use reqwest;
4
5/// Moderation API - handles ban/unban endpoints
6///
7/// Scopes required: `moderation:ban`
8pub struct ModerationApi<'a> {
9    client: &'a reqwest::Client,
10    token: &'a Option<String>,
11    base_url: &'a str,
12}
13
14impl<'a> ModerationApi<'a> {
15    /// Create a new ModerationApi instance
16    pub(crate) fn new(
17        client: &'a reqwest::Client,
18        token: &'a Option<String>,
19        base_url: &'a str,
20    ) -> Self {
21        Self {
22            client,
23            token,
24            base_url,
25        }
26    }
27
28    /// Ban or timeout a user in a channel
29    ///
30    /// If `duration` is provided in the request, this is a timeout (temporary ban).
31    /// If `duration` is `None`, this is a permanent ban.
32    ///
33    /// Requires OAuth token with `moderation:ban` scope
34    ///
35    /// # Example
36    /// ```no_run
37    /// use kick_api::BanRequest;
38    ///
39    /// // Permanent ban
40    /// let request = BanRequest {
41    ///     broadcaster_user_id: 12345,
42    ///     user_id: 67890,
43    ///     reason: Some("Breaking rules".to_string()),
44    ///     duration: None,
45    /// };
46    /// client.moderation().ban(request).await?;
47    /// ```
48    pub async fn ban(&self, request: BanRequest) -> Result<()> {
49        super::require_token(self.token)?;
50
51        let url = format!("{}/moderation/bans", self.base_url);
52        let request = self
53            .client
54            .post(&url)
55            .header("Accept", "*/*")
56            .bearer_auth(self.token.as_ref().unwrap())
57            .json(&request);
58        let response = crate::http::send_with_retry(self.client, request).await?;
59
60        if response.status().is_success() {
61            Ok(())
62        } else {
63            Err(KickApiError::ApiError(format!(
64                "Failed to ban user: {}",
65                response.status()
66            )))
67        }
68    }
69
70    /// Unban a user in a channel
71    ///
72    /// Requires OAuth token with `moderation:ban` scope
73    ///
74    /// # Example
75    /// ```no_run
76    /// use kick_api::UnbanRequest;
77    ///
78    /// let request = UnbanRequest {
79    ///     broadcaster_user_id: 12345,
80    ///     user_id: 67890,
81    /// };
82    /// client.moderation().unban(request).await?;
83    /// ```
84    pub async fn unban(&self, request: UnbanRequest) -> Result<()> {
85        super::require_token(self.token)?;
86
87        let url = format!("{}/moderation/bans", self.base_url);
88        let request = self
89            .client
90            .delete(&url)
91            .header("Accept", "*/*")
92            .bearer_auth(self.token.as_ref().unwrap())
93            .json(&request);
94        let response = crate::http::send_with_retry(self.client, request).await?;
95
96        if response.status().is_success() {
97            Ok(())
98        } else {
99            Err(KickApiError::ApiError(format!(
100                "Failed to unban user: {}",
101                response.status()
102            )))
103        }
104    }
105
106}