qt-chat-app

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

http_client.rs (4126B)


      1 //! HTTP client operations using the TrailBase client library
      2 //!
      3 //! This module provides type-safe access to the TrailBase API.
      4 
      5 use crate::types::{
      6     Chat, Message, NetworkError, NewChat, NewChatParticipation, NewMessage,
      7     UserChat, BASE_URL,
      8 };
      9 use trailbase_client::{Client, CompareOp, Filter, ListArguments, Pagination};
     10 
     11 /// Create a new TrailBase client
     12 pub fn create_client() -> Client {
     13     Client::new(BASE_URL, None).expect("Failed to create TrailBase client")
     14 }
     15 
     16 /// Perform login and return the auth token
     17 /// Note: This also sets the tokens on the client internally
     18 pub async fn login(client: &Client, email: &str, password: &str) -> Result<String, NetworkError> {
     19     let tokens = client.login(email, password).await?;
     20     Ok(tokens.auth_token)
     21 }
     22 
     23 /// Create a client with pre-existing tokens (for auto-login)
     24 pub fn create_client_with_token(token: &str) -> Client {
     25     use trailbase_client::Tokens;
     26     let tokens = Tokens {
     27         auth_token: token.to_string(),
     28         refresh_token: None,
     29         csrf_token: None,
     30     };
     31     Client::new(BASE_URL, Some(tokens)).expect("Failed to create TrailBase client")
     32 }
     33 
     34 /// Create a new chat and return its ID
     35 pub async fn post_chat(client: &Client, title: &str) -> Result<Chat, NetworkError> {
     36     let new_chat = NewChat {
     37         title: title.to_string(),
     38         id: None,
     39         created_at: None,
     40     };
     41 
     42     let api = client.records("chat");
     43     let id = api.create(&new_chat).await?;
     44 
     45     // Read back the created chat to get full data
     46     let chat: Chat = api.read(&id).await?;
     47     Ok(chat)
     48 }
     49 
     50 /// Create chat participation entry
     51 pub async fn create_chat_participation(client: &Client, chat_id: i64) -> Result<String, NetworkError> {
     52     let new_participation = NewChatParticipation {
     53         chat_id,
     54         id: None,
     55     };
     56 
     57     let api = client.records("chat_participation");
     58     let id = api.create(&new_participation).await?;
     59     Ok(id)
     60 }
     61 
     62 /// Fetch chats with pagination (from user_chats view)
     63 pub async fn fetch_chats(
     64     client: &Client,
     65     cursor: Option<&str>,
     66 ) -> Result<(Vec<UserChat>, Option<String>), NetworkError> {
     67     let api = client.records("user_chats");
     68 
     69     let pagination = Pagination::new()
     70         .with_limit(15)
     71         .with_cursor(cursor.map(|c| c.to_string()));
     72 
     73     let args = ListArguments::new()
     74         .with_pagination(pagination)
     75         .with_order(&["-created_at", "-id"]);
     76 
     77     let response = api.list(args).await?;
     78     Ok((response.records, response.cursor))
     79 }
     80 
     81 /// Post a new message and return it
     82 pub async fn post_message(
     83     client: &Client,
     84     chat_id: i64,
     85     content: &str,
     86 ) -> Result<Message, NetworkError> {
     87     let new_message = NewMessage {
     88         chat_id,
     89         content: content.to_string(),
     90         id: None,
     91         sent_at: None,
     92     };
     93 
     94     let api = client.records("message");
     95     let id = api.create(&new_message).await?;
     96 
     97     // Read back the created message to get full data
     98     let message: Message = api.read(&id).await?;
     99     Ok(message)
    100 }
    101 
    102 /// Fetch messages for a chat with pagination
    103 pub async fn fetch_messages(
    104     client: &Client,
    105     chat_id: i64,
    106     cursor: Option<&str>,
    107 ) -> Result<(Vec<Message>, Option<String>), NetworkError> {
    108     let api = client.records("message");
    109 
    110     let pagination = Pagination::new()
    111         .with_limit(20)
    112         .with_cursor(cursor.map(|c| c.to_string()));
    113 
    114     let filter = Filter::new("chat_id", CompareOp::Equal, chat_id.to_string());
    115 
    116     let args = ListArguments::new()
    117         .with_pagination(pagination)
    118         .with_order(&["-sent_at", "-id"])
    119         .with_filters(filter);
    120 
    121     let response = api.list(args).await?;
    122     Ok((response.records, response.cursor))
    123 }
    124 
    125 /// Update a chat's title
    126 pub async fn update_chat(client: &Client, chat_id: i64, title: &str) -> Result<Chat, NetworkError> {
    127     let api = client.records("chat");
    128 
    129     let update_data = NewChat {
    130         title: title.to_string(),
    131         id: Some(chat_id),
    132         created_at: None,
    133     };
    134 
    135     api.update(&chat_id.to_string(), &update_data).await?;
    136 
    137     // Read back the updated chat
    138     let chat: Chat = api.read(&chat_id.to_string()).await?;
    139     Ok(chat)
    140 }