Join community from search
Browse files at this commit413fd1928f29874afb0e3db761fc71a3b10ab119Parent: aa1a10f- feat
- join community from search
3 changed files +178 -1
- crates/client/src/gui/chat.rs +163 -0
- crates/client/src/network.rs +1 -0
- crates/client/src/request.rs +14 -1
crates/client/src/gui/chat.rsmodified+163 -0
| @@ -26,6 +26,9 @@ pub struct Chat { | |||
| 26 | message_input: Entity<InputState>, | 26 | message_input: Entity<InputState>, |
| 27 | community_name_input: Entity<InputState>, | 27 | community_name_input: Entity<InputState>, |
| 28 | creating_community: bool, | 28 | creating_community: bool, |
| 29 | + community_search_input: Entity<InputState>, | ||
| 30 | + community_search_results: Vec<Community>, | ||
| 31 | + joining_community: bool, | ||
| 29 | } | 32 | } |
| 30 | 33 | ||
| 31 | impl Chat { | 34 | impl Chat { |
| @@ -97,6 +100,24 @@ impl Chat { | |||
| 97 | ) | 100 | ) |
| 98 | .detach(); | 101 | .detach(); |
| 99 | 102 | ||
| 103 | + let community_search_input = cx.new(|cx| { | ||
| 104 | + let mut input_state = InputState::new(window, cx); | ||
| 105 | + input_state.set_placeholder("Community Name...", window, cx); | ||
| 106 | + | ||
| 107 | + input_state | ||
| 108 | + }); | ||
| 109 | + | ||
| 110 | + cx.subscribe_in( | ||
| 111 | + &community_search_input, | ||
| 112 | + window, | ||
| 113 | + |this, input, event, window, cx| { | ||
| 114 | + if matches!(event, InputEvent::PressEnter { .. }) { | ||
| 115 | + search_community_action(this, input, window, cx); | ||
| 116 | + } | ||
| 117 | + }, | ||
| 118 | + ) | ||
| 119 | + .detach(); | ||
| 120 | + | ||
| 100 | Self { | 121 | Self { |
| 101 | communities: vec![], | 122 | communities: vec![], |
| 102 | selected_community_id: None, | 123 | selected_community_id: None, |
| @@ -105,6 +126,9 @@ impl Chat { | |||
| 105 | message_input, | 126 | message_input, |
| 106 | community_name_input, | 127 | community_name_input, |
| 107 | creating_community: false, | 128 | creating_community: false, |
| 129 | + community_search_input, | ||
| 130 | + community_search_results: vec![], | ||
| 131 | + joining_community: false, | ||
| 108 | } | 132 | } |
| 109 | } | 133 | } |
| 110 | 134 | ||
| @@ -148,12 +172,22 @@ impl Chat { | |||
| 148 | self.selected_community_id = selected_community_id | 172 | self.selected_community_id = selected_community_id |
| 149 | } | 173 | } |
| 150 | 174 | ||
| 175 | + pub fn update_searched_community_list(&mut self, communities: Vec<Community>) { | ||
| 176 | + self.community_search_results = communities; | ||
| 177 | + } | ||
| 178 | + | ||
| 179 | + pub fn set_joining_community(&mut self, joining_community: bool) { | ||
| 180 | + self.joining_community = joining_community | ||
| 181 | + } | ||
| 182 | + | ||
| 151 | pub fn reset(&mut self) { | 183 | pub fn reset(&mut self) { |
| 152 | self.communities.clear(); | 184 | self.communities.clear(); |
| 153 | self.selected_community_id = None; | 185 | self.selected_community_id = None; |
| 154 | self.users.clear(); | 186 | self.users.clear(); |
| 155 | self.chat_messages.clear(); | 187 | self.chat_messages.clear(); |
| 156 | self.creating_community = false; | 188 | self.creating_community = false; |
| 189 | + self.community_search_results.clear(); | ||
| 190 | + self.joining_community = false; | ||
| 157 | } | 191 | } |
| 158 | } | 192 | } |
| 159 | 193 | ||
| @@ -204,6 +238,42 @@ fn create_community_action( | |||
| 204 | .detach(); | 238 | .detach(); |
| 205 | } | 239 | } |
| 206 | 240 | ||
| 241 | +fn search_community_action( | ||
| 242 | + this: &mut Yumush, | ||
| 243 | + input: &Entity<InputState>, | ||
| 244 | + window: &mut Window, | ||
| 245 | + cx: &mut Context<'_, Yumush>, | ||
| 246 | +) { | ||
| 247 | + let community_name = input.read(cx).text().to_string(); | ||
| 248 | + | ||
| 249 | + if let Err(error_value) = validate_community_name(&community_name) { | ||
| 250 | + window.push_notification(Notification::error(error_value.to_string()), cx); | ||
| 251 | + | ||
| 252 | + return; | ||
| 253 | + } | ||
| 254 | + | ||
| 255 | + let network = this.network.clone(); | ||
| 256 | + | ||
| 257 | + cx.spawn_in(window, async move |this, cx| { | ||
| 258 | + let communities = match network.search_community(&community_name).await { | ||
| 259 | + Ok(communities) => communities, | ||
| 260 | + Err(error_value) => { | ||
| 261 | + let _ = this.update_in(cx, |_, window, cx| { | ||
| 262 | + window.push_notification(Notification::error(error_value.to_string()), cx); | ||
| 263 | + }); | ||
| 264 | + | ||
| 265 | + return; | ||
| 266 | + } | ||
| 267 | + }; | ||
| 268 | + | ||
| 269 | + let _ = this.update(cx, |yumush, cx| { | ||
| 270 | + yumush.chat.update_searched_community_list(communities); | ||
| 271 | + cx.notify(); | ||
| 272 | + }); | ||
| 273 | + }) | ||
| 274 | + .detach(); | ||
| 275 | +} | ||
| 276 | + | ||
| 207 | impl Yumush { | 277 | impl Yumush { |
| 208 | pub fn chat_page(&self, cx: &mut Context<Self>) -> impl IntoElement { | 278 | pub fn chat_page(&self, cx: &mut Context<Self>) -> impl IntoElement { |
| 209 | let entity = cx.entity(); | 279 | let entity = cx.entity(); |
| @@ -254,6 +324,31 @@ impl Yumush { | |||
| 254 | ); | 324 | ); |
| 255 | } | 325 | } |
| 256 | 326 | ||
| 327 | + cx.notify(); | ||
| 328 | + }); | ||
| 329 | + }, | ||
| 330 | + ) | ||
| 331 | + }) | ||
| 332 | + .child({ | ||
| 333 | + let entity = entity.clone(); | ||
| 334 | + | ||
| 335 | + Button::new("join_community_button").label("Join").on_click( | ||
| 336 | + move |_, window, cx| { | ||
| 337 | + entity.update(cx, |yumush, cx| { | ||
| 338 | + yumush.chat.joining_community = | ||
| 339 | + !yumush.chat.joining_community; | ||
| 340 | + | ||
| 341 | + if yumush.chat.joining_community { | ||
| 342 | + yumush.chat.community_search_input.update( | ||
| 343 | + cx, | ||
| 344 | + |input_state, cx| { | ||
| 345 | + input_state.focus(window, cx); | ||
| 346 | + }, | ||
| 347 | + ); | ||
| 348 | + } else { | ||
| 349 | + yumush.chat.update_searched_community_list(vec![]); | ||
| 350 | + } | ||
| 351 | + | ||
| 257 | cx.notify(); | 352 | cx.notify(); |
| 258 | }); | 353 | }); |
| 259 | }, | 354 | }, |
| @@ -265,6 +360,39 @@ impl Yumush { | |||
| 265 | .creating_community | 360 | .creating_community |
| 266 | .then(|| Input::new(&self.chat.community_name_input)), | 361 | .then(|| Input::new(&self.chat.community_name_input)), |
| 267 | ) | 362 | ) |
| 363 | + .children( | ||
| 364 | + self.chat | ||
| 365 | + .joining_community | ||
| 366 | + .then(|| Input::new(&self.chat.community_search_input)), | ||
| 367 | + ) | ||
| 368 | + .children( | ||
| 369 | + self.chat | ||
| 370 | + .community_search_results | ||
| 371 | + .iter() | ||
| 372 | + .filter(|result| { | ||
| 373 | + !self.chat.communities.iter().any(|community| { | ||
| 374 | + community.get_community_id() == result.get_community_id() | ||
| 375 | + }) | ||
| 376 | + }) | ||
| 377 | + .enumerate() | ||
| 378 | + .map(|(index, result)| { | ||
| 379 | + let community_id = result.get_community_id().to_string(); | ||
| 380 | + let entity = entity.clone(); | ||
| 381 | + | ||
| 382 | + div() | ||
| 383 | + .id(("community_search_result", index)) | ||
| 384 | + .p_1() | ||
| 385 | + .cursor_pointer() | ||
| 386 | + .bg(rgb(0x1e1f22)) | ||
| 387 | + .text_color(rgb(0x949ba4)) | ||
| 388 | + .child(result.get_community_name().to_string()) | ||
| 389 | + .on_click(move |_, window, cx| { | ||
| 390 | + entity.update(cx, |yumush, cx| { | ||
| 391 | + yumush.join_community(&community_id, window, cx); | ||
| 392 | + }) | ||
| 393 | + }) | ||
| 394 | + }), | ||
| 395 | + ) | ||
| 268 | .children(self.chat.communities.iter().enumerate().map( | 396 | .children(self.chat.communities.iter().enumerate().map( |
| 269 | |(index, community)| { | 397 | |(index, community)| { |
| 270 | let community_id = community.get_community_id().to_string(); | 398 | let community_id = community.get_community_id().to_string(); |
| @@ -459,6 +587,41 @@ impl Yumush { | |||
| 459 | }) | 587 | }) |
| 460 | .detach(); | 588 | .detach(); |
| 461 | } | 589 | } |
| 590 | + | ||
| 591 | + pub fn join_community(&self, community_id: &str, window: &mut Window, cx: &mut Context<Self>) { | ||
| 592 | + let Some(user) = self.get_user() else { | ||
| 593 | + return; | ||
| 594 | + }; | ||
| 595 | + | ||
| 596 | + let network = self.network.clone(); | ||
| 597 | + let community_id = community_id.to_string(); | ||
| 598 | + | ||
| 599 | + cx.spawn_in(window, async move |this, cx| { | ||
| 600 | + if let Err(error_value) = network | ||
| 601 | + .join_community(user.get_user_id(), &community_id) | ||
| 602 | + .await | ||
| 603 | + { | ||
| 604 | + let _ = this.update_in(cx, |_, window, cx| { | ||
| 605 | + window.push_notification(Notification::error(error_value.to_string()), cx); | ||
| 606 | + }); | ||
| 607 | + | ||
| 608 | + return; | ||
| 609 | + } | ||
| 610 | + | ||
| 611 | + let _ = this.update_in(cx, |yumush, window, cx| { | ||
| 612 | + yumush.chat.set_joining_community(false); | ||
| 613 | + yumush.chat.update_searched_community_list(vec![]); | ||
| 614 | + yumush.chat.set_selected_community_id(Some(community_id)); | ||
| 615 | + yumush.chat.update_user_list(vec![]); | ||
| 616 | + yumush.get_communities(window, cx); | ||
| 617 | + yumush.get_chat_users(window, cx); | ||
| 618 | + yumush.get_chat_messages(window, cx); | ||
| 619 | + | ||
| 620 | + cx.notify(); | ||
| 621 | + }); | ||
| 622 | + }) | ||
| 623 | + .detach(); | ||
| 624 | + } | ||
| 462 | } | 625 | } |
| 463 | 626 | ||
| 464 | fn render_message(message: &Message, users: &[User]) -> impl IntoElement { | 627 | fn render_message(message: &Message, users: &[User]) -> impl IntoElement { |
crates/client/src/network.rsmodified+1 -0
| @@ -109,6 +109,7 @@ network_commands! { | |||
| 109 | IsUserIn => is_user_in(user_id, community_id) -> bool, | 109 | IsUserIn => is_user_in(user_id, community_id) -> bool, |
| 110 | UsersIn => users_in(community_id) -> Vec<String>, | 110 | UsersIn => users_in(community_id) -> Vec<String>, |
| 111 | CommunityOf => community_of(user_id) -> Vec<String>, | 111 | CommunityOf => community_of(user_id) -> Vec<String>, |
| 112 | + SearchCommunity => search_community(community_name) -> Vec<Community>, | ||
| 112 | } | 113 | } |
| 113 | 114 | ||
| 114 | #[derive(Debug)] | 115 | #[derive(Debug)] |
crates/client/src/request.rsmodified+14 -1
| @@ -6,7 +6,9 @@ use common::{ | |||
| 6 | network::Network, | 6 | network::Network, |
| 7 | request::{ | 7 | request::{ |
| 8 | Authentication, Deauthentication, Request, | 8 | Authentication, Deauthentication, Request, |
| 9 | - community::{CreateCommunity, DeleteCommunity, ReadCommunity, UpdateCommunity}, | 9 | + community::{ |
| 10 | + CreateCommunity, DeleteCommunity, ReadCommunity, SearchCommunity, UpdateCommunity, | ||
| 11 | + }, | ||
| 10 | message::{ | 12 | message::{ |
| 11 | CreateMessage, DeleteMessage, ReadMessage, ReadMessageByCommunityIDWithMarkerAndLimit, | 13 | CreateMessage, DeleteMessage, ReadMessage, ReadMessageByCommunityIDWithMarkerAndLimit, |
| 12 | UpdateMessage, | 14 | UpdateMessage, |
| @@ -281,4 +283,15 @@ impl ClientRequest { | |||
| 281 | self.connection | 283 | self.connection |
| 282 | ) | 284 | ) |
| 283 | } | 285 | } |
| 286 | + | ||
| 287 | + pub async fn search_community(&self, community_name: &str) -> Result<Vec<Community>, Error> { | ||
| 288 | + let search_community = SearchCommunity::new(community_name)?; | ||
| 289 | + let request = Request::SearchCommunity(search_community); | ||
| 290 | + | ||
| 291 | + send_request_receive_response_then_classify!( | ||
| 292 | + request, | ||
| 293 | + Response::SearchCommunity, | ||
| 294 | + self.connection | ||
| 295 | + ) | ||
| 296 | + } | ||
| 284 | } | 297 | } |