aboutsummaryrefslogtreecommitdiffstats
path: root/filamento/src/logic/connect.rs
blob: d7b9feec2e541d4fb7fec5d1966e85f1757d5765 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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) => {
                        logic
                            .handle_error(ConnectionJobError::StatusCacheError(e.into()).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) => logic.handle_error(Error::Connecting(e.into())).await,
                    },
                    Err(e) => {
                        logic
                            .handle_error(
                                ConnectionJobError::SendPresence(WriteError::Actor(e.into()))
                                    .into(),
                            )
                            .await;
                    }
                }
            }
            Err(e) => {
                logic.handle_error(Error::Connecting(e.into())).await;
            }
        },
        Err(e) => {
            logic
                .handle_error(Error::Connecting(ConnectionJobError::RosterRetreival(
                    RosterError::Write(WriteError::Actor(e.into())),
                )))
                .await;
        }
    }
}