aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: b2308fb7ecd051ec3db2460e86946f3cfc7775f4 (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
use std::collections::{HashMap, HashSet};

use iced::futures::{SinkExt, Stream, StreamExt};
use iced::widget::{button, column, row, text, text_input};
use iced::{stream, Element, Subscription, Task, Theme};
use jid::JID;
use luz::chat::{Chat, Message as ChatMessage};
use luz::presence::{Offline, Presence};
use luz::CommandMessage;
use luz::{roster::Contact, user::User, LuzHandle, UpdateMessage};
use tokio::sync::oneshot;
use tokio_stream::wrappers::ReceiverStream;

#[derive(Default)]
pub struct Macaw {
    client: Option<LuzHandle>,
    roster: HashMap<JID, Contact>,
    users: HashMap<JID, User>,
    presences: HashMap<JID, Presence>,
    chats: HashMap<JID, (Chat, Vec<ChatMessage>)>,
    subscription_requests: HashSet<JID>,
    connection_status: Option<Presence>,
}

fn main() -> iced::Result {
    tracing_subscriber::fmt::init();

    iced::application("Macaw", Macaw::update, Macaw::view)
        .subscription(Macaw::subscription)
        .run()
}

#[derive(Debug, Clone)]
enum Message {
    ClientCreated(LuzHandle),
    Luz(UpdateMessage),
    Roster(HashMap<JID, Contact>),
    Connect,
    Disconnect,
    OpenChat(JID),
}

impl Macaw {
    fn stream() -> impl Stream<Item = Message> {
        stream::channel(100, |mut output| async {
            let (luz, recv) = LuzHandle::new(
                "test@blos.sm".try_into().unwrap(),
                "slayed".to_string(),
                "./macaw.db",
            )
            .await
            .unwrap();
            output.send(Message::ClientCreated(luz)).await;
            let stream = ReceiverStream::new(recv);
            let stream = stream.map(|message| Message::Luz(message)).map(Ok);
            stream.forward(output).await;
        })
    }

    fn subscription(&self) -> Subscription<Message> {
        Subscription::run(Macaw::stream)
    }

    fn update(&mut self, message: Message) -> Task<Message> {
        match message {
            Message::Luz(update_message) => match update_message {
                UpdateMessage::Error(error) => {
                    tracing::error!("Luz error: {:?}", error);
                    Task::none()
                }
                UpdateMessage::Online(online, vec) => {
                    self.connection_status = Some(Presence::Online(online));
                    let mut roster = HashMap::new();
                    for contact in vec {
                        roster.insert(contact.user_jid.clone(), contact);
                    }
                    self.roster = roster;
                    Task::none()
                }
                UpdateMessage::Offline(offline) => {
                    self.connection_status = Some(Presence::Offline(offline));
                    Task::none()
                }
                UpdateMessage::FullRoster(vec) => {
                    let mut macaw_roster = HashMap::new();
                    for contact in vec {
                        macaw_roster.insert(contact.user_jid.clone(), contact);
                    }
                    self.roster = macaw_roster;
                    Task::none()
                }
                UpdateMessage::RosterUpdate(contact) => {
                    self.roster.insert(contact.user_jid.clone(), contact);
                    Task::none()
                }
                UpdateMessage::RosterDelete(jid) => {
                    self.roster.remove(&jid);
                    Task::none()
                }
                UpdateMessage::Presence { from, presence } => {
                    self.presences.insert(from, presence);
                    Task::none()
                }
                UpdateMessage::Message { to, message } => {
                    if let Some((_chat, message_history)) = self.chats.get_mut(&to) {
                        message_history.push(message);
                    } else {
                        let chat = Chat {
                            correspondent: to.clone(),
                        };
                        let message_history = vec![message];
                        self.chats.insert(to, (chat, message_history));
                    }
                    Task::none()
                }
                UpdateMessage::SubscriptionRequest(jid) => {
                    // TODO: subscription requests
                    Task::none()
                }
            },
            Message::ClientCreated(luz_handle) => {
                let cloned: LuzHandle = luz_handle.clone();
                self.client = Some(cloned);
                let (send, recv) = oneshot::channel();
                Task::perform(
                    async move {
                        luz_handle.send(CommandMessage::GetRoster(send)).await;
                        recv.await
                    },
                    |result| {
                        let roster = result.unwrap().unwrap();
                        let mut macaw_roster = HashMap::new();
                        for contact in roster {
                            macaw_roster.insert(contact.user_jid.clone(), contact);
                        }
                        Message::Roster(macaw_roster)
                    },
                )
            }
            Message::Roster(hash_map) => {
                self.roster = hash_map;
                Task::none()
            }
            Message::Connect => {
                let client = self.client.clone();
                Task::future(async move {
                    client.clone().unwrap().send(CommandMessage::Connect).await;
                })
                .discard()
            }
            Message::Disconnect => {
                let client = self.client.clone();
                Task::future(async move {
                    client
                        .clone()
                        .unwrap()
                        .send(CommandMessage::Disconnect(Offline::default()))
                        .await;
                })
                .discard()
            }
            Message::OpenChat(jid) => todo!(),
        }
    }

    fn view(&self) -> Element<Message> {
        let mut contacts: Vec<Element<Message>> = Vec::new();
        for (_, contact) in &self.roster {
            contacts.push(
                button(match &contact.user_jid.localpart {
                    Some(u) => u,
                    None => "no username",
                })
                .on_press(Message::OpenChat(contact.user_jid.clone()))
                .into(),
            );
        }
        let column = column(contacts);
        let connection_status = match &self.connection_status {
            Some(s) => match s {
                Presence::Online(online) => "connected",
                Presence::Offline(offline) => "disconnected",
            },
            None => "no account",
        };
        column![
            row![
                text("test@blos.sm"),
                text(connection_status),
                button("connect").on_press(Message::Connect),
                button("disconnect").on_press(Message::Disconnect)
            ],
            text("Buddy List:"),
            //
            //
            column,
        ]
        .into()
    }

    fn theme(&self) -> Theme {
        Theme::Dark
    }
}