mastodon_api\methods/
conversations.rs

1use crate::MastodonClient;
2use crate::error::Result;
3use crate::models::Conversation;
4
5/// Handler for direct conversation (DM) API endpoints.
6pub struct ConversationsHandler<'a> {
7    client: &'a MastodonClient,
8}
9
10impl<'a> ConversationsHandler<'a> {
11    /// Creates a new `ConversationsHandler` for the given client.
12    ///
13    /// Parameters:
14    /// - `client`: The client to use for making requests.
15    ///
16    /// Returns:
17    /// - `ConversationsHandler`: The created conversations handler.
18    pub fn new(client: &'a MastodonClient) -> Self {
19        Self { client }
20    }
21
22    /// Fetches a list of the authenticated user's direct conversations.
23    ///
24    /// Returns:
25    /// - `Result<Vec<Conversation>>`: The fetched conversations.
26    ///
27    /// Corresponds to `GET /api/v1/conversations`.
28    pub async fn list(&self) -> Result<Vec<Conversation>> {
29        let url = format!("{}/api/v1/conversations", self.client.base_url());
30        let req = self.client.http_client().get(&url);
31        self.client.send(req).await
32    }
33
34    /// Deletes a conversation by its ID.
35    ///
36    /// Parameters:
37    /// - `id`: The ID of the conversation to delete.
38    ///
39    /// Returns:
40    /// - `Result<()>`: The deleted conversation.
41    ///
42    /// Corresponds to `DELETE /api/v1/conversations/:id`.
43    pub async fn delete(&self, id: &str) -> Result<()> {
44        let url = format!("{}/api/v1/conversations/{}", self.client.base_url(), id);
45        let req = self.client.http_client().delete(&url);
46        let _: serde_json::Value = self.client.send(req).await?;
47        Ok(())
48    }
49
50    /// Marks a conversation as read.
51    ///
52    /// Parameters:
53    /// - `id`: The ID of the conversation to mark as read.
54    ///
55    /// Returns:
56    /// - `Result<Conversation>`: The marked conversation.
57    ///
58    /// Corresponds to `POST /api/v1/conversations/:id/read`.
59    pub async fn mark_as_read(&self, id: &str) -> Result<Conversation> {
60        let url = format!(
61            "{}/api/v1/conversations/{}/read",
62            self.client.base_url(),
63            id
64        );
65        let req = self.client.http_client().post(&url);
66        self.client.send(req).await
67    }
68}