mastodon_api\methods/
markers.rs

1use crate::MastodonClient;
2use crate::error::Result;
3use crate::models::Marker;
4use std::collections::HashMap;
5
6/// Handler for marker-related API endpoints.
7pub struct MarkersHandler<'a> {
8    client: &'a MastodonClient,
9}
10
11impl<'a> MarkersHandler<'a> {
12    /// Creates a new `MarkersHandler` for the given client.
13    pub fn new(client: &'a MastodonClient) -> Self {
14        Self { client }
15    }
16
17    /// Fetches markers for the given timelines.
18    ///
19    /// Parameters:
20    /// - `timelines`: A list of timelines to fetch markers for (e.g., "home", "notifications").
21    ///
22    /// Returns:
23    /// - `Result<HashMap<String, Marker>>`: A map of timeline names to markers.
24    ///
25    /// Corresponds to `GET /api/v1/markers`.
26    pub async fn get(&self, timelines: &[&str]) -> Result<HashMap<String, Marker>> {
27        let url = format!("{}/api/v1/markers", self.client.base_url());
28        let mut req = self.client.http_client().get(&url);
29        for timeline in timelines {
30            req = req.query(&[("timeline[]", timeline)]);
31        }
32        self.client.send(req).await
33    }
34
35    /// Updates or creates a marker for a timeline.
36    ///
37    /// Parameters:
38    /// - `timeline`: The timeline to update ("home" or "notifications").
39    /// - `last_read_id`: The ID of the status to mark as read.
40    ///
41    /// Returns:
42    /// - `Result<Marker>`: The updated marker.
43    ///
44    /// Corresponds to `POST /api/v1/markers`.
45    pub async fn update(&self, timeline: &str, last_read_id: &str) -> Result<Marker> {
46        let url = format!("{}/api/v1/markers", self.client.base_url());
47        let mut params = HashMap::new();
48        let mut inner_params = HashMap::new();
49        inner_params.insert("last_read_id", last_read_id);
50        params.insert(timeline, inner_params);
51
52        let req = self.client.http_client().post(&url).form(&params);
53        let results: HashMap<String, Marker> = self.client.send(req).await?;
54        results
55            .get(timeline)
56            .cloned()
57            .ok_or_else(|| crate::error::MastodonError::ApiError {
58                status: reqwest::StatusCode::OK,
59                message: format!("Marker for {} not returned", timeline),
60            })
61    }
62}