feat: add message Tauri commands
This commit is contained in:
49
src-tauri/src/commands/message.rs
Normal file
49
src-tauri/src/commands/message.rs
Normal file
@@ -0,0 +1,49 @@
|
||||
use crate::models::Message;
|
||||
use crate::state::{AppState, BASE_URL};
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn list_messages(
|
||||
state: tauri::State<'_, AppState>,
|
||||
conversation_id: String,
|
||||
) -> Result<Vec<Message>, String> {
|
||||
state
|
||||
.client
|
||||
.get(format!("{}/conversations/{}/messages", BASE_URL, conversation_id))
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| e.to_string())?
|
||||
.json()
|
||||
.await
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn send_message(
|
||||
state: tauri::State<'_, AppState>,
|
||||
conversation_id: String,
|
||||
content: String,
|
||||
) -> Result<Message, String> {
|
||||
let user_id = state
|
||||
.user_id
|
||||
.lock()
|
||||
.unwrap()
|
||||
.clone()
|
||||
.ok_or_else(|| "Not logged in".to_string())?;
|
||||
|
||||
let body = serde_json::json!({
|
||||
"conversation_id": conversation_id,
|
||||
"user_id": user_id,
|
||||
"content": content,
|
||||
});
|
||||
|
||||
state
|
||||
.client
|
||||
.post(format!("{}/messages", BASE_URL))
|
||||
.json(&body)
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| e.to_string())?
|
||||
.json()
|
||||
.await
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
pub mod conversation;
|
||||
pub mod message;
|
||||
pub mod user;
|
||||
pub mod workspace;
|
||||
|
||||
Reference in New Issue
Block a user