diff options
author | 2025-03-24 13:24:22 +0000 | |
---|---|---|
committer | 2025-03-24 13:24:22 +0000 | |
commit | 6814c50b7d36b75b71b9f8a76107f9164afaa7fe (patch) | |
tree | 3cb99c896ca4626db31b4fbfbfcb29ac85ab40f8 | |
parent | a58638a6698538f2b7c793b5049a9635a5b03624 (diff) | |
download | macaw-6814c50b7d36b75b71b9f8a76107f9164afaa7fe.tar.gz macaw-6814c50b7d36b75b71b9f8a76107f9164afaa7fe.tar.bz2 macaw-6814c50b7d36b75b71b9f8a76107f9164afaa7fe.zip |
feat: get chats ordered with latest messages
-rw-r--r-- | src/main.rs | 148 |
1 files changed, 89 insertions, 59 deletions
diff --git a/src/main.rs b/src/main.rs index 492a387..7cc2add 100644 --- a/src/main.rs +++ b/src/main.rs @@ -263,25 +263,35 @@ async fn main() -> iced::Result { let luz_handle1 = luz_handle.clone(); let luz_handle2 = luz_handle.clone(); if cfg.auto_connect { - Task::batch([ - Task::batch([ - Task::perform(async move { luz_handle1.get_roster().await }, |result| { - let roster = result.unwrap(); - let mut macaw_roster = HashMap::new(); - for contact in roster { - macaw_roster.insert(contact.user_jid.clone(), contact); - } - Message::Roster(macaw_roster) - }), - Task::perform(async move { luz_handle2.get_chats().await }, |chats| { - let chats = chats.unwrap(); - info!("got chats: {:?}", chats); - Message::GotChats(chats) - }), - ]) - .chain(Task::done(Message::Connect)), - Task::stream(stream), - ]) + Task::batch( + [ + Task::batch([ + Task::perform( + async move { luz_handle1.get_roster().await }, + |result| { + let roster = result.unwrap(); + let mut macaw_roster = HashMap::new(); + for contact in roster { + macaw_roster.insert(contact.user_jid.clone(), contact); + } + Message::Roster(macaw_roster) + }, + ), + Task::perform( + async move { + luz_handle2.get_chats_ordered_with_latest_messages().await + }, + |chats| { + let chats = chats.unwrap(); + info!("got chats: {:?}", chats); + Message::GotChats(chats) + }, + ), + ]) + .chain(Task::done(Message::Connect)), + Task::stream(stream), + ], + ) } else { Task::batch([ Task::perform(async move { luz_handle1.get_roster().await }, |result| { @@ -292,11 +302,14 @@ async fn main() -> iced::Result { } Message::Roster(macaw_roster) }), - Task::perform(async move { luz_handle2.get_chats().await }, |chats| { - let chats = chats.unwrap(); - info!("got chats: {:?}", chats); - Message::GotChats(chats) - }), + Task::perform( + async move { luz_handle2.get_chats_ordered_with_latest_messages().await }, + |chats| { + let chats = chats.unwrap(); + info!("got chats: {:?}", chats); + Message::GotChats(chats) + }, + ), Task::stream(stream), ]) } @@ -367,7 +380,7 @@ pub enum Message { Roster(HashMap<JID, Contact>), Connect, Disconnect, - GotChats(Vec<Chat>), + GotChats(Vec<(Chat, ChatMessage)>), GotMessageHistory(Chat, IndexMap<Uuid, ChatMessage>), ToggleChat(JID), SendMessage(JID, String), @@ -509,15 +522,23 @@ impl Macaw { } Message::Roster(macaw_roster) }), - Task::perform(async move { client2.client.get_chats().await }, |chats| { - let chats = chats.unwrap(); - // let chats: HashMap<JID, (Chat, IndexMap<Uuid, ChatMessage>)> = chats - // .into_iter() - // .map(|chat| (chat.correspondent.clone(), (chat, IndexMap::new()))) - // .collect(); - info!("got chats: {:?}", chats); - Message::GotChats(chats) - }), + Task::perform( + async move { + client2 + .client + .get_chats_ordered_with_latest_messages() + .await + }, + |chats| { + let chats = chats.unwrap(); + // let chats: HashMap<JID, (Chat, IndexMap<Uuid, ChatMessage>)> = chats + // .into_iter() + // .map(|chat| (chat.correspondent.clone(), (chat, IndexMap::new()))) + // .collect(); + info!("got chats: {:?}", chats); + Message::GotChats(chats) + }, + ), ]) .chain(Task::done(Message::Connect)) } else { @@ -530,15 +551,23 @@ impl Macaw { } Message::Roster(macaw_roster) }), - Task::perform(async move { client2.client.get_chats().await }, |chats| { - let chats = chats.unwrap(); - // let chats: HashMap<JID, (Chat, IndexMap<Uuid, ChatMessage>)> = chats - // .into_iter() - // .map(|chat| (chat.correspondent.clone(), (chat, IndexMap::new()))) - // .collect(); - info!("got chats: {:?}", chats); - Message::GotChats(chats) - }), + Task::perform( + async move { + client2 + .client + .get_chats_ordered_with_latest_messages() + .await + }, + |chats| { + let chats = chats.unwrap(); + // let chats: HashMap<JID, (Chat, IndexMap<Uuid, ChatMessage>)> = chats + // .into_iter() + // .map(|chat| (chat.correspondent.clone(), (chat, IndexMap::new()))) + // .collect(); + info!("got chats: {:?}", chats); + Message::GotChats(chats) + }, + ), ]) } } @@ -685,22 +714,23 @@ impl Macaw { }; for chat in chats { self.chats - .insert(chat.correspondent.clone(), (chat.clone(), None)); - let client = client.clone(); - let correspondent = chat.correspondent.clone(); - tasks.push(Task::perform( - // TODO: don't get the entire message history LOL - async move { (chat, client.get_messages(correspondent).await) }, - |result| { - let messages: IndexMap<Uuid, ChatMessage> = result - .1 - .unwrap() - .into_iter() - .map(|message| (message.id.clone(), message)) - .collect(); - Message::GotMessageHistory(result.0, messages) - }, - )) + // TODO: could have a chat with no messages, bad database state + .insert(chat.0.correspondent.clone(), (chat.0.clone(), Some(chat.1))); + // let client = client.clone(); + // let correspondent = chat.correspondent.clone(); + // tasks.push(Task::perform( + // // TODO: don't get the entire message history LOL + // async move { (chat, client.get_messages(correspondent).await) }, + // |result| { + // let messages: IndexMap<Uuid, ChatMessage> = result + // .1 + // .unwrap() + // .into_iter() + // .map(|message| (message.id.clone(), message)) + // .collect(); + // Message::GotMessageHistory(result.0, messages) + // }, + // )) } Task::batch(tasks) // .then(|chats| { |