aboutsummaryrefslogtreecommitdiffstats
path: root/filamento/src/logic/offline.rs
blob: e864f22a36d650322663fff98d8939a8847e566a (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
use lampada::error::WriteError;

use crate::{
    Command,
    error::{DatabaseError, Error, RosterError, StatusError},
    presence::Online,
    roster::Contact,
};

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 = handle_get_roster(logic).await;
            sender.send(roster);
        }
        Command::GetChats(sender) => {
            let chats = handle_get_chats(logic).await;
            sender.send(chats);
        }
        Command::GetChatsOrdered(sender) => {
            let chats = handle_get_chats_ordered(logic).await;
            sender.send(chats);
        }
        Command::GetChatsOrderedWithLatestMessages(sender) => {
            let chats = handle_get_chats_ordered_with_latest_messages(logic).await;
            sender.send(chats);
        }
        Command::GetChat(jid, sender) => {
            let chats = handle_get_chat(logic, jid).await;
            sender.send(chats);
        }
        Command::GetMessages(jid, sender) => {
            let messages = handle_get_messages(logic, jid).await;
            sender.send(messages);
        }
        Command::DeleteChat(jid, sender) => {
            let result = handle_delete_chat(logic, jid).await;
            sender.send(result);
        }
        Command::DeleteMessage(uuid, sender) => {
            let result = handle_delete_messaage(logic, uuid).await;
            sender.send(result);
        }
        Command::GetUser(jid, sender) => {
            let user = handle_get_user(logic, jid).await;
            sender.send(user);
        }
        // TODO: offline queue to modify roster
        Command::AddContact(_jid, sender) => {
            sender.send(Err(RosterError::Write(WriteError::Disconnected)));
        }
        Command::BuddyRequest(_jid, sender) => {
            sender.send(Err(WriteError::Disconnected));
        }
        Command::SubscriptionRequest(_jid, sender) => {
            sender.send(Err(WriteError::Disconnected));
        }
        Command::AcceptBuddyRequest(_jid, sender) => {
            sender.send(Err(WriteError::Disconnected));
        }
        Command::AcceptSubscriptionRequest(_jid, sender) => {
            sender.send(Err(WriteError::Disconnected));
        }
        Command::UnsubscribeFromContact(_jid, sender) => {
            sender.send(Err(WriteError::Disconnected));
        }
        Command::UnsubscribeContact(_jid, sender) => {
            sender.send(Err(WriteError::Disconnected));
        }
        Command::UnfriendContact(_jid, sender) => {
            sender.send(Err(WriteError::Disconnected));
        }
        Command::DeleteContact(_jid, sender) => {
            sender.send(Err(RosterError::Write(WriteError::Disconnected)));
        }
        Command::UpdateContact(_jid, _contact_update, sender) => {
            sender.send(Err(RosterError::Write(WriteError::Disconnected)));
        }
        Command::SetStatus(online, sender) => {
            let result = handle_set_status(logic, online).await;
            sender.send(result);
        }
        // TODO: offline message queue
        Command::SendMessage(_jid, _body, sender) => {
            sender.send(Err(WriteError::Disconnected));
        }
        Command::SendPresence(_jid, _presence, sender) => {
            sender.send(Err(WriteError::Disconnected));
        }
    }
    Ok(())
}