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 conversation;
|
||||||
|
pub mod message;
|
||||||
pub mod user;
|
pub mod user;
|
||||||
pub mod workspace;
|
pub mod workspace;
|
||||||
|
|||||||
@@ -12,6 +12,8 @@ pub fn run() {
|
|||||||
commands::user::login,
|
commands::user::login,
|
||||||
commands::workspace::list_workspaces,
|
commands::workspace::list_workspaces,
|
||||||
commands::conversation::list_conversations,
|
commands::conversation::list_conversations,
|
||||||
|
commands::message::list_messages,
|
||||||
|
commands::message::send_message,
|
||||||
])
|
])
|
||||||
.run(tauri::generate_context!())
|
.run(tauri::generate_context!())
|
||||||
.expect("error while running tauri application");
|
.expect("error while running tauri application");
|
||||||
|
|||||||
Reference in New Issue
Block a user