mastodon_api\methods/
filters.rs

1use crate::MastodonClient;
2use crate::error::Result;
3use crate::models::Filter;
4use serde::Serialize;
5
6/// Handler for content filter API endpoints.
7pub struct FiltersHandler<'a> {
8    client: &'a MastodonClient,
9}
10
11/// Parameters for creating a new content filter.
12#[derive(Serialize)]
13pub struct CreateFilterParams {
14    /// The keyword or phrase to filter.
15    pub phrase: String,
16    /// The contexts in which to apply the filter (e.g., "home", "notifications", "public", "thread").
17    pub context: Vec<String>,
18    /// Whether the filter should be applied server-side (irreversible) or just hint the client.
19    pub irreversible: bool,
20    /// Whether to match only whole words.
21    pub whole_word: bool,
22    /// Number of seconds from now until the filter expires.
23    pub expires_in: Option<u32>,
24}
25
26impl<'a> FiltersHandler<'a> {
27    /// Creates a new `FiltersHandler` for the given client.
28    ///
29    /// Parameters:
30    /// - `client`: The client to use for making requests.
31    ///
32    /// Returns:
33    /// - `FiltersHandler`: The created filters handler.
34    pub fn new(client: &'a MastodonClient) -> Self {
35        Self { client }
36    }
37
38    /// Fetches all content filters for the authenticated user.
39    ///
40    /// Returns:
41    /// - `Result<Vec<Filter>>`: The fetched filters.
42    ///
43    /// Corresponds to `GET /api/v1/filters`.
44    pub async fn list(&self) -> Result<Vec<Filter>> {
45        let url = format!("{}/api/v1/filters", self.client.base_url());
46        let req = self.client.http_client().get(&url);
47        self.client.send(req).await
48    }
49
50    /// Creates a new content filter.
51    ///
52    /// Parameters:
53    /// - `params`: The parameters for the filter to create.
54    ///
55    /// Returns:
56    /// - `Result<Filter>`: The created filter.
57    ///
58    /// Corresponds to `POST /api/v1/filters`.
59    pub async fn create(&self, params: &CreateFilterParams) -> Result<Filter> {
60        let url = format!("{}/api/v1/filters", self.client.base_url());
61        let req = self.client.http_client().post(&url).json(params);
62        self.client.send(req).await
63    }
64
65    /// Deletes a content filter by its ID.
66    ///
67    /// Parameters:
68    /// - `id`: The ID of the filter to delete.
69    ///
70    /// Returns:
71    /// - `Result<()>`: The deleted filter.
72    ///
73    /// Corresponds to `DELETE /api/v1/filters/:id`.
74    pub async fn delete(&self, id: &str) -> Result<()> {
75        let url = format!("{}/api/v1/filters/{}", self.client.base_url(), id);
76        let req = self.client.http_client().delete(&url);
77        let _: serde_json::Value = self.client.send(req).await?;
78        Ok(())
79    }
80}