Skip to main content

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            .map_err(|e| super::utils::map_request_error(e, self.get_base_url()))?;
38
39        handle_response(response).await
40    }
41
42    /// Retrieves details for a specific chat
43    ///
44    /// Returns chat metadata, participants, and latest message
45    pub async fn get_chat(&self, chat_id: &str) -> Result<Chat> {
46        let url = format!("{}/v1/chats/{}", self.get_base_url(), urlencoding::encode(chat_id));
47        let response = self
48            .get_http_client()
49            .get(&url)
50            .header("Authorization", self.get_auth_header())
51            .send()
52            .await
53            .map_err(|e| super::utils::map_request_error(e, self.get_base_url()))?;
54
55        handle_response(response).await
56    }
57
58    /// Creates a new chat
59    ///
60    /// Creates a single or group chat on a specific account using participant IDs
61    pub async fn create_chat(&self, input: CreateChatInput) -> Result<CreateChatOutput> {
62        let url = format!("{}/v1/chats", self.get_base_url());
63        let response = self
64            .get_http_client()
65            .post(&url)
66            .header("Authorization", self.get_auth_header())
67            .json(&input)
68            .send()
69            .await
70            .map_err(|e| super::utils::map_request_error(e, self.get_base_url()))?;
71
72        handle_response(response).await
73    }
74
75    /// Archives or unarchives a chat
76    pub async fn archive_chat(&self, chat_id: &str, archived: bool) -> Result<Chat> {
77        let url = format!("{}/v1/chats/{}/archive", self.get_base_url(), urlencoding::encode(chat_id));
78        let body = serde_json::json!({ "archived": archived });
79
80        let response = self
81            .get_http_client()
82            .post(&url)
83            .header("Authorization", self.get_auth_header())
84            .json(&body)
85            .send()
86            .await
87            .map_err(|e| super::utils::map_request_error(e, self.get_base_url()))?;
88
89        handle_response(response).await
90    }
91
92    /// Sets a reminder for a chat
93    pub async fn set_chat_reminder(&self, chat_id: &str, timestamp: &str) -> Result<Chat> {
94        let url = format!(
95            "{}/v1/chats/{}/reminders",
96            self.get_base_url(),
97            urlencoding::encode(chat_id)
98        );
99        let body = serde_json::json!({ "timestamp": timestamp });
100
101        let response = self
102            .get_http_client()
103            .post(&url)
104            .header("Authorization", self.get_auth_header())
105            .json(&body)
106            .send()
107            .await
108            .map_err(|e| super::utils::map_request_error(e, self.get_base_url()))?;
109
110        handle_response(response).await
111    }
112
113    /// Clears a reminder from a chat
114    pub async fn clear_chat_reminder(&self, chat_id: &str) -> Result<Chat> {
115        let url = format!(
116            "{}/v1/chats/{}/reminders",
117            self.get_base_url(),
118            urlencoding::encode(chat_id)
119        );
120
121        let response = self
122            .get_http_client()
123            .delete(&url)
124            .header("Authorization", self.get_auth_header())
125            .send()
126            .await
127            .map_err(|e| super::utils::map_request_error(e, self.get_base_url()))?;
128
129        handle_response(response).await
130    }
131}