Skip to main content

roboat/chat/
mod.rs

1use crate::{Client, RoboatError};
2use reqwest::header;
3
4mod request_types;
5
6const UNREAD_CONVERSATION_COUNT_API: &str =
7    "https://chat.roblox.com/v2/get-unread-conversation-count";
8
9impl Client {
10    /// Fetches the number of unread chats/conversations using <https://chat.roblox.com/v2/get-unread-conversation-count>.
11    /// Keep in mind that these are not the same as "messages".
12    ///
13    /// # Notes
14    /// * Requires a valid roblosecurity.
15    ///
16    /// # Errors
17    /// * All errors under [Standard Errors](#standard-errors).
18    /// * All errors under [Auth Required Errors](#auth-required-errors).
19    ///
20    /// # Example
21    /// ```no_run
22    /// use roboat::ClientBuilder;
23    ///
24    /// const ROBLOSECURITY: &str = "roblosecurity";
25    ///
26    /// # #[tokio::main]
27    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
28    /// let client = ClientBuilder::new().roblosecurity(ROBLOSECURITY.to_string()).build();
29    ///
30    /// let count = client.unread_conversation_count().await?;
31    ///
32    /// println!("Unread message count: {}", count);
33    /// # Ok(())
34    /// # }
35    /// ```
36    pub async fn unread_conversation_count(&self) -> Result<u64, RoboatError> {
37        let cookie_string = self.cookie_string()?;
38
39        let request_result = self
40            .reqwest_client
41            .get(UNREAD_CONVERSATION_COUNT_API)
42            .header(header::COOKIE, cookie_string)
43            .send()
44            .await;
45
46        let response = Self::validate_request_result(request_result).await?;
47        let raw = Self::parse_to_raw::<request_types::UnreadMessageCountResponse>(response).await?;
48
49        Ok(raw.count)
50    }
51}