forge
Explore
Log in

Don't you dare go Hollow

Load message history on community select

Browse files at this commit

4e8ba238008d86b92e024dcde7ad6e96080a5705Ahmet Kaan GümüşParent: 18f0ac4
  • feat
    • load message history on community select
Show full message
✨ feat(client): load message history on community select

Messages merge by id rather than assuming page adjacency, so repeat
fetches and pushes arriving mid-fetch are idempotent.
crates/client/src/gui/chat.rsmodified+45 -1
@@ -1,5 +1,6 @@
1 use common::{1 use common::{
2 community::Community,2 community::Community,
3+ constant::NEWEST_MESSAGE_MARKER,
3 message::Message,4 message::Message,
4 user::User,5 user::User,
5 validate::{validate_community_name, validate_message_body},6 validate::{validate_community_name, validate_message_body},
@@ -114,7 +115,15 @@ impl Chat {
114 }115 }
115 116
116 pub fn push_message(&mut self, message: Message) {117 pub fn push_message(&mut self, message: Message) {
117- self.chat_messages.push(message);118+ self.merge_messages(vec![message]);
119+ }
120+
121+ fn merge_messages(&mut self, messages: Vec<Message>) {
122+ self.chat_messages.extend(messages);
123+ self.chat_messages
124+ .sort_by(|left, right| left.get_id().cmp(right.get_id()));
125+ self.chat_messages
126+ .dedup_by(|left, right| left.get_id() == right.get_id());
118 }127 }
119 128
120 pub fn update_user_list(&mut self, users: Vec<User>) {129 pub fn update_user_list(&mut self, users: Vec<User>) {
@@ -188,6 +197,7 @@ fn create_community_action(
188 yumush.chat.update_user_list(vec![]);197 yumush.chat.update_user_list(vec![]);
189 yumush.get_communities(window, cx);198 yumush.get_communities(window, cx);
190 yumush.get_chat_users(window, cx);199 yumush.get_chat_users(window, cx);
200+ yumush.get_chat_messages(window, cx);
191 cx.notify();201 cx.notify();
192 });202 });
193 })203 })
@@ -279,6 +289,7 @@ impl Yumush {
279 ));289 ));
280 yumush.chat.update_user_list(vec![]);290 yumush.chat.update_user_list(vec![]);
281 yumush.get_chat_users(window, cx);291 yumush.get_chat_users(window, cx);
292+ yumush.get_chat_messages(window, cx);
282 cx.notify();293 cx.notify();
283 })294 })
284 })295 })
@@ -382,6 +393,39 @@ impl Yumush {
382 .detach();393 .detach();
383 }394 }
384 395
396+ pub fn get_chat_messages(&self, window: &mut Window, cx: &mut Context<Self>) {
397+ let network = self.network.clone();
398+
399+ let Some(community_id) = self.chat.get_selected_community_id() else {
400+ return;
401+ };
402+
403+ cx.spawn_in(window, async move |this, cx| {
404+ let messages = match network
405+ .read_message_by_community_id_with_marker_and_limit(
406+ community_id.as_str(),
407+ NEWEST_MESSAGE_MARKER,
408+ )
409+ .await
410+ {
411+ Ok(messages) => messages,
412+ Err(error_value) => {
413+ let _ = this.update_in(cx, |_, window, cx| {
414+ window.push_notification(Notification::error(error_value.to_string()), cx);
415+ });
416+
417+ return;
418+ }
419+ };
420+
421+ let _ = this.update(cx, |yumush, cx| {
422+ yumush.chat.merge_messages(messages);
423+ cx.notify();
424+ });
425+ })
426+ .detach();
427+ }
428+
385 pub fn get_communities(&self, window: &mut Window, cx: &mut Context<Self>) {429 pub fn get_communities(&self, window: &mut Window, cx: &mut Context<Self>) {
386 let Some(user) = self.get_user() else {430 let Some(user) = self.get_user() else {
387 return;431 return;
crates/client/src/network.rsmodified+1 -0
@@ -103,6 +103,7 @@ network_commands! {
103 ReadMessage => read_message(message_id) -> Message,103 ReadMessage => read_message(message_id) -> Message,
104 UpdateMessage => update_message(message_id, message_body) -> Message,104 UpdateMessage => update_message(message_id, message_body) -> Message,
105 DeleteMessage => delete_message(message_id) -> Message,105 DeleteMessage => delete_message(message_id) -> Message,
106+ ReadMessageByCommunityIDWithMarkerAndLimit => read_message_by_community_id_with_marker_and_limit(community_id, marker) -> Vec<Message>,
106 JoinCommunity => join_community(user_id, community_id) -> (),107 JoinCommunity => join_community(user_id, community_id) -> (),
107 LeaveCommunity => leave_community(user_id, community_id) -> (),108 LeaveCommunity => leave_community(user_id, community_id) -> (),
108 IsUserIn => is_user_in(user_id, community_id) -> bool,109 IsUserIn => is_user_in(user_id, community_id) -> bool,
crates/client/src/request.rsmodified+27 -1
@@ -1,12 +1,16 @@
1 use common::{1 use common::{
2 community::Community,2 community::Community,
3+ constant::MESSAGE_PAGE_LIMIT,
3 error::Error,4 error::Error,
4 message::Message,5 message::Message,
5 network::Network,6 network::Network,
6 request::{7 request::{
7 Authentication, Deauthentication, Request,8 Authentication, Deauthentication, Request,
8 community::{CreateCommunity, DeleteCommunity, ReadCommunity, UpdateCommunity},9 community::{CreateCommunity, DeleteCommunity, ReadCommunity, UpdateCommunity},
9- message::{CreateMessage, DeleteMessage, ReadMessage, UpdateMessage},10+ message::{
11+ CreateMessage, DeleteMessage, ReadMessage, ReadMessageByCommunityIDWithMarkerAndLimit,
12+ UpdateMessage,
13+ },
10 user::{CreateUser, DeleteUser, ReadUser, UpdateUser},14 user::{CreateUser, DeleteUser, ReadUser, UpdateUser},
11 user_community::{CommunityOf, IsUserIn, JoinCommunity, LeaveCommunity, UsersIn},15 user_community::{CommunityOf, IsUserIn, JoinCommunity, LeaveCommunity, UsersIn},
12 },16 },
@@ -209,6 +213,28 @@ impl ClientRequest {
209 )213 )
210 }214 }
211 215
216+ pub async fn read_message_by_community_id_with_marker_and_limit(
217+ &self,
218+ community_id: &str,
219+ marker: &str,
220+ ) -> Result<Vec<Message>, Error> {
221+ let read_message_by_community_id_with_marker_and_limit =
222+ ReadMessageByCommunityIDWithMarkerAndLimit::new(
223+ community_id,
224+ marker,
225+ MESSAGE_PAGE_LIMIT,
226+ );
227+ let request = Request::ReadMessageByCommunityIDWithMarkerAndLimit(
228+ read_message_by_community_id_with_marker_and_limit,
229+ );
230+
231+ send_request_receive_response_then_classify!(
232+ request,
233+ Response::ReadMessageByCommunityIDWithMarkerAndLimit,
234+ self.connection
235+ )
236+ }
237+
212 pub async fn join_community(&self, user_id: &str, community_id: &str) -> Result<(), Error> {238 pub async fn join_community(&self, user_id: &str, community_id: &str) -> Result<(), Error> {
213 let join_community = JoinCommunity::new(user_id, community_id);239 let join_community = JoinCommunity::new(user_id, community_id);
214 let request = Request::JoinCommunity(join_community);240 let request = Request::JoinCommunity(join_community);