diff options
author | 2025-03-27 19:21:14 +0000 | |
---|---|---|
committer | 2025-03-27 19:21:14 +0000 | |
commit | 97ef9274ac5e0e9aa6d6c842477051de80b9d82d (patch) | |
tree | 279ec226592366382070f44981a6868b455a488c | |
parent | a367aca33fecc03270b5b9ad2a6a21281d760fd8 (diff) | |
download | luz-97ef9274ac5e0e9aa6d6c842477051de80b9d82d.tar.gz luz-97ef9274ac5e0e9aa6d6c842477051de80b9d82d.tar.bz2 luz-97ef9274ac5e0e9aa6d6c842477051de80b9d82d.zip |
refactor(filamento): handle_offline logic
-rw-r--r-- | filamento/src/db.rs | 4 | ||||
-rw-r--r-- | filamento/src/logic/offline.rs | 72 | ||||
-rw-r--r-- | filamento/src/logic/online.rs | 6 | ||||
-rw-r--r-- | filamento/src/logic/process_stanza.rs | 3 | ||||
-rw-r--r-- | lampada/src/error.rs | 2 |
5 files changed, 46 insertions, 41 deletions
diff --git a/filamento/src/db.rs b/filamento/src/db.rs index f34fea6..1054ec2 100644 --- a/filamento/src/db.rs +++ b/filamento/src/db.rs @@ -1,12 +1,12 @@ use std::{collections::HashSet, path::Path}; use jid::JID; -use sqlx::{Error, SqlitePool, migrate}; +use sqlx::{SqlitePool, migrate}; use uuid::Uuid; use crate::{ chat::{Chat, Message}, - error::{DatabaseError, DatabaseOpenError}, + error::{DatabaseError as Error, DatabaseOpenError}, presence::Online, roster::Contact, user::User, diff --git a/filamento/src/logic/offline.rs b/filamento/src/logic/offline.rs index 17a60f3..e864f22 100644 --- a/filamento/src/logic/offline.rs +++ b/filamento/src/logic/offline.rs @@ -2,62 +2,73 @@ use lampada::error::WriteError; use crate::{ Command, - error::{RosterError, StatusError}, + error::{DatabaseError, Error, RosterError, StatusError}, + presence::Online, + roster::Contact, }; -use super::ClientLogic; +use super::{ + ClientLogic, + local_only::{ + handle_delete_chat, handle_delete_messaage, handle_get_chat, handle_get_chats, + handle_get_chats_ordered, handle_get_chats_ordered_with_latest_messages, + handle_get_messages, handle_get_user, + }, +}; pub async fn handle_offline(logic: ClientLogic, command: Command) { + let result = handle_offline_result(&logic, command).await; + match result { + Ok(_) => {} + Err(e) => logic.handle_error(e).await, + } +} + +pub async fn handle_set_status(logic: &ClientLogic, online: Online) -> Result<(), StatusError> { + logic.db().upsert_cached_status(online).await?; + Ok(()) +} + +pub async fn handle_get_roster(logic: &ClientLogic) -> Result<Vec<Contact>, RosterError> { + Ok(logic.db().read_cached_roster().await?) +} + +pub async fn handle_offline_result(logic: &ClientLogic, command: Command) -> Result<(), Error> { match command { Command::GetRoster(sender) => { - let roster = logic.db().read_cached_roster().await; - match roster { - Ok(roster) => { - let _ = sender.send(Ok(roster)); - } - Err(e) => { - let _ = sender.send(Err(RosterError::Cache(e.into()))); - } - } + let roster = handle_get_roster(logic).await; + sender.send(roster); } Command::GetChats(sender) => { - let chats = logic.db().read_chats().await.map_err(|e| e.into()); + let chats = handle_get_chats(logic).await; sender.send(chats); } Command::GetChatsOrdered(sender) => { - let chats = logic.db().read_chats_ordered().await.map_err(|e| e.into()); + let chats = handle_get_chats_ordered(logic).await; sender.send(chats); } Command::GetChatsOrderedWithLatestMessages(sender) => { - let chats = logic - .db() - .read_chats_ordered_with_latest_messages() - .await - .map_err(|e| e.into()); + let chats = handle_get_chats_ordered_with_latest_messages(logic).await; sender.send(chats); } Command::GetChat(jid, sender) => { - let chats = logic.db().read_chat(jid).await.map_err(|e| e.into()); + let chats = handle_get_chat(logic, jid).await; sender.send(chats); } Command::GetMessages(jid, sender) => { - let messages = logic - .db() - .read_message_history(jid) - .await - .map_err(|e| e.into()); + let messages = handle_get_messages(logic, jid).await; sender.send(messages); } Command::DeleteChat(jid, sender) => { - let result = logic.db().delete_chat(jid).await.map_err(|e| e.into()); + let result = handle_delete_chat(logic, jid).await; sender.send(result); } Command::DeleteMessage(uuid, sender) => { - let result = logic.db().delete_message(uuid).await.map_err(|e| e.into()); + let result = handle_delete_messaage(logic, uuid).await; sender.send(result); } Command::GetUser(jid, sender) => { - let user = logic.db().read_user(jid).await.map_err(|e| e.into()); + let user = handle_get_user(logic, jid).await; sender.send(user); } // TODO: offline queue to modify roster @@ -92,11 +103,7 @@ pub async fn handle_offline(logic: ClientLogic, command: Command) { sender.send(Err(RosterError::Write(WriteError::Disconnected))); } Command::SetStatus(online, sender) => { - let result = logic - .db() - .upsert_cached_status(online) - .await - .map_err(|e| StatusError::Cache(e.into())); + let result = handle_set_status(logic, online).await; sender.send(result); } // TODO: offline message queue @@ -107,4 +114,5 @@ pub async fn handle_offline(logic: ClientLogic, command: Command) { sender.send(Err(WriteError::Disconnected)); } } + Ok(()) } diff --git a/filamento/src/logic/online.rs b/filamento/src/logic/online.rs index 8bbeaa5..05d3f2b 100644 --- a/filamento/src/logic/online.rs +++ b/filamento/src/logic/online.rs @@ -454,11 +454,7 @@ pub async fn handle_set_status( connection: Connected, online: Online, ) -> Result<(), StatusError> { - logic - .db() - .upsert_cached_status(online.clone()) - .await - .map_err(|e| DatabaseError(e.into()))?; + logic.db().upsert_cached_status(online.clone()).await?; connection .write_handle() .write(Stanza::Presence(online.into_stanza(None))) diff --git a/filamento/src/logic/process_stanza.rs b/filamento/src/logic/process_stanza.rs index 660da16..b1bc830 100644 --- a/filamento/src/logic/process_stanza.rs +++ b/filamento/src/logic/process_stanza.rs @@ -69,8 +69,7 @@ pub async fn recv_message( logic .db() .create_message_with_user_resource_and_chat(message.clone(), from.clone()) - .await - .map_err(|e| DatabaseError(e.into()))?; + .await?; message.from = message.from.as_bare(); from = from.as_bare(); Ok(Some(UpdateMessage::Message { to: from, message })) diff --git a/lampada/src/error.rs b/lampada/src/error.rs index 384d1ee..8104155 100644 --- a/lampada/src/error.rs +++ b/lampada/src/error.rs @@ -53,6 +53,8 @@ pub enum ReadError { XML(#[from] peanuts::Error), #[error("lost connection")] LostConnection, + #[error("actor: {0}")] + Actor(#[from] ActorError), } #[derive(Debug, Error, Clone)] |