beeper_desktop_api/client/
chats.rs

1//! Chat-related API operations
2
3use crate::models::{Chat, CreateChatInput, CreateChatOutput, ListChatsOutput};
4use crate::error::Result;
5use super::{BeeperClient, handle_response};
6
7impl BeeperClient {
8    /// Lists all chats sorted by last activity
9    ///
10    /// Combines all accounts into a single paginated list.
11    pub async fn list_chats(
12        &self,
13        cursor: Option<&str>,
14        direction: Option<&str>,
15    ) -> Result<ListChatsOutput> {
16        let mut url = format!("{}/v1/chats", self.get_base_url());
17
18        if let Some(c) = cursor {
19            url.push_str(&format!("?cursor={}", urlencoding::encode(c)));
20            if direction.is_some() {
21                url.push('&');
22            }
23        } else if direction.is_some() {
24            url.push('?');
25        }
26
27        if let Some(d) = direction {
28            url.push_str(&format!("direction={}", d));
29        }
30
31        let response = self
32            .get_http_client()
33            .get(&url)
34            .header("Authorization", self.get_auth_header())
35            .send()
36            .await?;
37
38        handle_response(response).await
39    }
40
41    /// Retrieves details for a specific chat
42    ///
43    /// Returns chat metadata, participants, and latest message
44    pub async fn get_chat(&self, chat_id: &str) -> Result<Chat> {
45        let url = format!("{}/v1/chats/{}", self.get_base_url(), urlencoding::encode(chat_id));
46        let response = self
47            .get_http_client()
48            .get(&url)
49            .header("Authorization", self.get_auth_header())
50            .send()
51            .await?;
52
53        handle_response(response).await
54    }
55
56    /// Creates a new chat
57    ///
58    /// Creates a single or group chat on a specific account using participant IDs
59    pub async fn create_chat(&self, input: CreateChatInput) -> Result<CreateChatOutput> {
60        let url = format!("{}/v1/chats", self.get_base_url());
61        let response = self
62            .get_http_client()
63            .post(&url)
64            .header("Authorization", self.get_auth_header())
65            .json(&input)
66            .send()
67            .await?;
68
69        handle_response(response).await
70    }
71
72    /// Archives or unarchives a chat
73    pub async fn archive_chat(&self, chat_id: &str, archived: bool) -> Result<Chat> {
74        let url = format!("{}/v1/chats/{}/archive", self.get_base_url(), urlencoding::encode(chat_id));
75        let body = serde_json::json!({ "archived": archived });
76
77        let response = self
78            .get_http_client()
79            .post(&url)
80            .header("Authorization", self.get_auth_header())
81            .json(&body)
82            .send()
83            .await?;
84
85        handle_response(response).await
86    }
87
88    /// Sets a reminder for a chat
89    pub async fn set_chat_reminder(&self, chat_id: &str, timestamp: &str) -> Result<Chat> {
90        let url = format!(
91            "{}/v1/chats/{}/reminders",
92            self.get_base_url(),
93            urlencoding::encode(chat_id)
94        );
95        let body = serde_json::json!({ "timestamp": timestamp });
96
97        let response = self
98            .get_http_client()
99            .post(&url)
100            .header("Authorization", self.get_auth_header())
101            .json(&body)
102            .send()
103            .await?;
104
105        handle_response(response).await
106    }
107
108    /// Clears a reminder from a chat
109    pub async fn clear_chat_reminder(&self, chat_id: &str) -> Result<Chat> {
110        let url = format!(
111            "{}/v1/chats/{}/reminders",
112            self.get_base_url(),
113            urlencoding::encode(chat_id)
114        );
115
116        let response = self
117            .get_http_client()
118            .delete(&url)
119            .header("Authorization", self.get_auth_header())
120            .send()
121            .await?;
122
123        handle_response(response).await
124    }
125}