diff --git a/crates/client/src/gui/chat.rs b/crates/client/src/gui/chat.rs index fc685b2..5853944 100644 --- a/crates/client/src/gui/chat.rs +++ b/crates/client/src/gui/chat.rs @@ -1,5 +1,6 @@ use common::{ community::Community, + constant::NEWEST_MESSAGE_MARKER, message::Message, user::User, validate::{validate_community_name, validate_message_body}, @@ -114,7 +115,15 @@ impl Chat { } pub fn push_message(&mut self, message: Message) { - self.chat_messages.push(message); + self.merge_messages(vec![message]); + } + + fn merge_messages(&mut self, messages: Vec) { + self.chat_messages.extend(messages); + self.chat_messages + .sort_by(|left, right| left.get_id().cmp(right.get_id())); + self.chat_messages + .dedup_by(|left, right| left.get_id() == right.get_id()); } pub fn update_user_list(&mut self, users: Vec) { @@ -188,6 +197,7 @@ fn create_community_action( yumush.chat.update_user_list(vec![]); yumush.get_communities(window, cx); yumush.get_chat_users(window, cx); + yumush.get_chat_messages(window, cx); cx.notify(); }); }) @@ -279,6 +289,7 @@ impl Yumush { )); yumush.chat.update_user_list(vec![]); yumush.get_chat_users(window, cx); + yumush.get_chat_messages(window, cx); cx.notify(); }) }) @@ -382,6 +393,39 @@ impl Yumush { .detach(); } + pub fn get_chat_messages(&self, window: &mut Window, cx: &mut Context) { + let network = self.network.clone(); + + let Some(community_id) = self.chat.get_selected_community_id() else { + return; + }; + + cx.spawn_in(window, async move |this, cx| { + let messages = match network + .read_message_by_community_id_with_marker_and_limit( + community_id.as_str(), + NEWEST_MESSAGE_MARKER, + ) + .await + { + Ok(messages) => messages, + Err(error_value) => { + let _ = this.update_in(cx, |_, window, cx| { + window.push_notification(Notification::error(error_value.to_string()), cx); + }); + + return; + } + }; + + let _ = this.update(cx, |yumush, cx| { + yumush.chat.merge_messages(messages); + cx.notify(); + }); + }) + .detach(); + } + pub fn get_communities(&self, window: &mut Window, cx: &mut Context) { let Some(user) = self.get_user() else { return; diff --git a/crates/client/src/network.rs b/crates/client/src/network.rs index dbabb20..90f94f3 100644 --- a/crates/client/src/network.rs +++ b/crates/client/src/network.rs @@ -103,6 +103,7 @@ network_commands! { ReadMessage => read_message(message_id) -> Message, UpdateMessage => update_message(message_id, message_body) -> Message, DeleteMessage => delete_message(message_id) -> Message, + ReadMessageByCommunityIDWithMarkerAndLimit => read_message_by_community_id_with_marker_and_limit(community_id, marker) -> Vec, JoinCommunity => join_community(user_id, community_id) -> (), LeaveCommunity => leave_community(user_id, community_id) -> (), IsUserIn => is_user_in(user_id, community_id) -> bool, diff --git a/crates/client/src/request.rs b/crates/client/src/request.rs index 098baf5..dfea73d 100644 --- a/crates/client/src/request.rs +++ b/crates/client/src/request.rs @@ -1,12 +1,16 @@ use common::{ community::Community, + constant::MESSAGE_PAGE_LIMIT, error::Error, message::Message, network::Network, request::{ Authentication, Deauthentication, Request, community::{CreateCommunity, DeleteCommunity, ReadCommunity, UpdateCommunity}, - message::{CreateMessage, DeleteMessage, ReadMessage, UpdateMessage}, + message::{ + CreateMessage, DeleteMessage, ReadMessage, ReadMessageByCommunityIDWithMarkerAndLimit, + UpdateMessage, + }, user::{CreateUser, DeleteUser, ReadUser, UpdateUser}, user_community::{CommunityOf, IsUserIn, JoinCommunity, LeaveCommunity, UsersIn}, }, @@ -209,6 +213,28 @@ impl ClientRequest { ) } + pub async fn read_message_by_community_id_with_marker_and_limit( + &self, + community_id: &str, + marker: &str, + ) -> Result, Error> { + let read_message_by_community_id_with_marker_and_limit = + ReadMessageByCommunityIDWithMarkerAndLimit::new( + community_id, + marker, + MESSAGE_PAGE_LIMIT, + ); + let request = Request::ReadMessageByCommunityIDWithMarkerAndLimit( + read_message_by_community_id_with_marker_and_limit, + ); + + send_request_receive_response_then_classify!( + request, + Response::ReadMessageByCommunityIDWithMarkerAndLimit, + self.connection + ) + } + pub async fn join_community(&self, user_id: &str, community_id: &str) -> Result<(), Error> { let join_community = JoinCommunity::new(user_id, community_id); let request = Request::JoinCommunity(join_community);