diff options
Diffstat (limited to 'filamento/src/logic/offline.rs')
-rw-r--r-- | filamento/src/logic/offline.rs | 72 |
1 files changed, 40 insertions, 32 deletions
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(()) } |