use std::{collections::HashMap, sync::Arc}; use lampada::{Logic, error::ReadError}; use stanza::client::Stanza; use tokio::sync::{Mutex, mpsc, oneshot}; use crate::{Command, UpdateMessage, db::Db}; mod abort; mod connect; mod connection_error; mod disconnect; mod offline; mod online; mod process_stanza; #[derive(Clone)] pub struct ClientLogic { db: Db, pending: Arc>>>>, update_sender: mpsc::Sender, } impl ClientLogic { pub fn new( db: Db, pending: Arc>>>>, update_sender: mpsc::Sender, ) -> Self { Self { db, pending, update_sender, } } pub fn db(&self) -> &Db { &self.db } pub fn pending(&self) -> &Mutex>>> { &self.pending.as_ref() } pub fn update_sender(&self) -> &mpsc::Sender { &self.update_sender } } impl Logic for ClientLogic { type Cmd = Command; // pub async fn handle_stream_error(self, error) {} // stanza errors (recoverable) // pub async fn handle_error(self, error: Error) {} // when it aborts, must clear iq map no matter what async fn handle_connect(self, connection: lampada::Connected) { connect::handle_connect(self, connection).await; } async fn handle_disconnect(self, connection: lampada::Connected) { disconnect::handle_disconnect(self, connection).await; } async fn handle_stanza( self, stanza: ::stanza::client::Stanza, connection: lampada::Connected, supervisor: lampada::SupervisorSender, ) { process_stanza::handle_stanza(self, stanza, connection, supervisor).await; } async fn handle_online(self, command: Self::Cmd, connection: lampada::Connected) { online::handle_online(self, command, connection).await; } async fn handle_offline(self, command: Self::Cmd) { offline::handle_offline(self, command).await; } async fn on_abort(self) { abort::on_abort(self).await; } async fn handle_connection_error(self, error: lampada::error::ConnectionError) { connection_error::handle_connection_error(self, error).await; } }