aboutsummaryrefslogtreecommitdiffstats
path: root/filamento/src/logic/connect.rs
diff options
context:
space:
mode:
Diffstat (limited to 'filamento/src/logic/connect.rs')
-rw-r--r--filamento/src/logic/connect.rs91
1 files changed, 91 insertions, 0 deletions
diff --git a/filamento/src/logic/connect.rs b/filamento/src/logic/connect.rs
new file mode 100644
index 0000000..4dc789e
--- /dev/null
+++ b/filamento/src/logic/connect.rs
@@ -0,0 +1,91 @@
+use lampada::{Connected, Logic, error::WriteError};
+use tokio::sync::oneshot;
+use tracing::debug;
+
+use crate::{
+ Command, UpdateMessage,
+ error::{ConnectionJobError, Error, RosterError},
+ presence::{Online, PresenceType},
+};
+
+use super::ClientLogic;
+
+pub async fn handle_connect(logic: ClientLogic, connection: Connected) {
+ let (send, recv) = oneshot::channel();
+ debug!("getting roster");
+ logic
+ .clone()
+ .handle_online(Command::GetRoster(send), connection.clone())
+ .await;
+ debug!("sent roster req");
+ let roster = recv.await;
+ debug!("got roster");
+ match roster {
+ Ok(r) => match r {
+ Ok(roster) => {
+ let online = logic.db().read_cached_status().await;
+ let online = match online {
+ Ok(online) => online,
+ Err(e) => {
+ let _ = logic
+ .update_sender()
+ .send(UpdateMessage::Error(Error::Connecting(
+ ConnectionJobError::StatusCacheError(e.into()),
+ )))
+ .await;
+ Online::default()
+ }
+ };
+ let (send, recv) = oneshot::channel();
+ logic
+ .clone()
+ .handle_online(
+ Command::SendPresence(None, PresenceType::Online(online.clone()), send),
+ connection,
+ )
+ .await;
+ let set_status = recv.await;
+ match set_status {
+ Ok(s) => match s {
+ Ok(()) => {
+ let _ = logic
+ .update_sender()
+ .send(UpdateMessage::Online(online, roster))
+ .await;
+ }
+ Err(e) => {
+ let _ = logic
+ .update_sender()
+ .send(UpdateMessage::Error(Error::Connecting(e.into())))
+ .await;
+ }
+ },
+ Err(e) => {
+ let _ = logic
+ .update_sender()
+ .send(UpdateMessage::Error(Error::Connecting(
+ ConnectionJobError::SendPresence(WriteError::Actor(e.into())),
+ )))
+ .await;
+ }
+ }
+ }
+ Err(e) => {
+ let _ = logic
+ .update_sender()
+ .send(UpdateMessage::Error(Error::Connecting(e.into())))
+ .await;
+ }
+ },
+ Err(e) => {
+ let _ = logic
+ .update_sender()
+ .send(UpdateMessage::Error(Error::Connecting(
+ ConnectionJobError::RosterRetreival(RosterError::Write(WriteError::Actor(
+ e.into(),
+ ))),
+ )))
+ .await;
+ }
+ }
+}