forge
Explore
Log in

Don't you dare go Hollow

Add community list to chat

Browse files at this commit

c066e894fdf4c2a9a952e500153eafccac3fec7bAhmet Kaan GümüşParent: b621aee
  • feat
    • add community list to chat
Show full message
✨ feat(client): add community list to chat

Sidebar lists the user's communities and selects which one to send to, replacing the hardcoded community id. Messages filter by selection, senders render as usernames, and a 3s poll refreshes both lists.
crates/client/src/event.rsadded+39 -0
@@ -0,0 +1,39 @@
1+use std::time::Duration;
2+
3+use gpui::{Context, Window};
4+
5+use crate::gui::Yumush;
6+
7+const CHECK_INTERVAL: Duration = Duration::from_secs(3);
8+
9+impl Yumush {
10+ pub fn time_based_repetitive_checks_after_login(
11+ &self,
12+ window: &mut Window,
13+ cx: &mut Context<Self>,
14+ ) {
15+ cx.spawn_in(window, async move |this, cx| {
16+ loop {
17+ cx.background_executor().timer(CHECK_INTERVAL).await;
18+
19+ let keep_going = this.update_in(cx, |yumush, window, cx| {
20+ if !yumush.is_logged_in() {
21+ return false;
22+ }
23+
24+ if yumush.is_connected() {
25+ yumush.get_chat_users(window, cx);
26+ yumush.get_communities(window, cx);
27+ }
28+
29+ true
30+ });
31+
32+ if !matches!(keep_going, Ok(true)) {
33+ break;
34+ }
35+ }
36+ })
37+ .detach();
38+ }
39+}
crates/client/src/gui.rsmodified+30 -8
@@ -35,10 +35,10 @@ impl Yumush {
35 window: &mut gpui::Window,35 window: &mut gpui::Window,
36 cx: &mut gpui::Context<Self>,36 cx: &mut gpui::Context<Self>,
37 ) -> Self {37 ) -> Self {
38- cx.spawn(async move |this, cx| {38+ cx.spawn_in(window, async move |this, cx| {
39 while let Some(event) = network_event_receiver.recv().await {39 while let Some(event) = network_event_receiver.recv().await {
40- let updated = this.update(cx, |yumush, cx| {40+ let updated = this.update_in(cx, |yumush, window, cx| {
41- yumush.handle_network_event(event, cx);41+ yumush.handle_network_event(event, window, cx);
42 });42 });
43 43
44 if updated.is_err() {44 if updated.is_err() {
@@ -60,7 +60,6 @@ impl Yumush {
60 }60 }
61 61
62 fn set_user(&mut self, user: User) {62 fn set_user(&mut self, user: User) {
63- self.chat.push_user(&user);
64 self.user = Some(user);63 self.user = Some(user);
65 }64 }
66 65
@@ -72,6 +71,14 @@ impl Yumush {
72 self.user = None;71 self.user = None;
73 }72 }
74 73
74+ pub fn is_connected(&self) -> bool {
75+ self.connected
76+ }
77+
78+ pub fn is_logged_in(&self) -> bool {
79+ self.user.is_some()
80+ }
81+
75 fn get_username(&self) -> Option<&str> {82 fn get_username(&self) -> Option<&str> {
76 self.user.as_ref().map(|user| user.get_username())83 self.user.as_ref().map(|user| user.get_username())
77 }84 }
@@ -83,6 +90,7 @@ impl Yumush {
83 cx: &mut gpui::Context<Self>,90 cx: &mut gpui::Context<Self>,
84 ) {91 ) {
85 self.current_route = new_route;92 self.current_route = new_route;
93+ cx.notify();
86 94
87 match self.current_route {95 match self.current_route {
88 Route::Register => self.register.focus(window, cx),96 Route::Register => self.register.focus(window, cx),
@@ -91,12 +99,26 @@ impl Yumush {
91 }99 }
92 }100 }
93 101
94- fn handle_network_event(&mut self, event: NetworkEvent, cx: &mut gpui::Context<Self>) {102+ fn handle_network_event(
103+ &mut self,
104+ event: NetworkEvent,
105+ window: &mut gpui::Window,
106+ cx: &mut gpui::Context<Self>,
107+ ) {
95 match event {108 match event {
96- NetworkEvent::Connected => self.connected = true,109+ NetworkEvent::Connected(user) => {
110+ self.connected = true;
111+ match user {
112+ Some(user) => self.set_user(user),
113+ None if self.is_logged_in() => {
114+ self.reset_user();
115+ self.change_page(Route::Login, window, cx);
116+ }
117+ None => {}
118+ }
119+ }
97 NetworkEvent::ConnectionFailed(_) | NetworkEvent::Disconnected(_) => {120 NetworkEvent::ConnectionFailed(_) | NetworkEvent::Disconnected(_) => {
98 self.connected = false;121 self.connected = false;
99- self.reset_user();
100 }122 }
101 NetworkEvent::MessageReceived(message) => self.chat.push_message(message),123 NetworkEvent::MessageReceived(message) => self.chat.push_message(message),
102 }124 }
@@ -126,7 +148,7 @@ impl Render for Yumush {
126 .child(match self.current_route {148 .child(match self.current_route {
127 Route::Register => self.register_page(cx).into_any_element(),149 Route::Register => self.register_page(cx).into_any_element(),
128 Route::Login => self.login_page(cx).into_any_element(),150 Route::Login => self.login_page(cx).into_any_element(),
129- Route::Chat => self.chat_page().into_any_element(),151+ Route::Chat => self.chat_page(cx).into_any_element(),
130 }),152 }),
131 )153 )
132 .child(Root::read(window, cx).notification.clone())154 .child(Root::read(window, cx).notification.clone())
crates/client/src/gui/chat.rsmodified+155 -12
@@ -12,7 +12,8 @@ use gpui_component::{
12 use crate::gui::Yumush;12 use crate::gui::Yumush;
13 13
14 pub struct Chat {14 pub struct Chat {
15- community: Community,15+ communities: Vec<Community>,
16+ selected_community: Option<String>,
16 users: Vec<User>,17 users: Vec<User>,
17 chat_messages: Vec<Message>,18 chat_messages: Vec<Message>,
18 message_input: Entity<InputState>,19 message_input: Entity<InputState>,
@@ -27,9 +28,6 @@ impl Chat {
27 input_state28 input_state
28 });29 });
29 30
30- let community = Community::new("oko1k3hk3r21dhqa20az", "The Community");
31-
32- let community = || community.clone();
33 cx.subscribe_in(&message_input, window, |this, input, event, window, cx| {31 cx.subscribe_in(&message_input, window, |this, input, event, window, cx| {
34 if matches!(event, InputEvent::PressEnter { .. }) {32 if matches!(event, InputEvent::PressEnter { .. }) {
35 let message_body = input.read(cx).text().to_string();33 let message_body = input.read(cx).text().to_string();
@@ -45,7 +43,10 @@ impl Chat {
45 };43 };
46 44
47 let network = this.network.clone();45 let network = this.network.clone();
48- let community_id = this.chat.community.get_community_id().to_string();46+ let Some(community_id) = this.chat.selected_community.clone() else {
47+ window.push_notification(Notification::error("No Community Selected"), cx);
48+ return;
49+ };
49 50
50 cx.spawn_in(window, async move |this, cx| {51 cx.spawn_in(window, async move |this, cx| {
51 if let Err(error_value) = network52 if let Err(error_value) = network
@@ -70,7 +71,8 @@ impl Chat {
70 .detach();71 .detach();
71 72
72 Self {73 Self {
73- community: community(),74+ communities: vec![],
75+ selected_community: None,
74 users: vec![],76 users: vec![],
75 chat_messages: vec![],77 chat_messages: vec![],
76 message_input,78 message_input,
@@ -87,17 +89,76 @@ impl Chat {
87 self.chat_messages.push(message);89 self.chat_messages.push(message);
88 }90 }
89 91
90- pub fn push_user(&mut self, user: &User) {92+ pub fn update_user_list(&mut self, users: Vec<User>) {
91- self.users.push(user.to_owned());93+ self.users = users;
94+ }
95+
96+ pub fn update_community_list(&mut self, communities: Vec<Community>) {
97+ if self.selected_community.is_none() {
98+ self.selected_community = communities
99+ .first()
100+ .map(|community| community.get_community_id().to_string());
101+ }
102+
103+ self.communities = communities;
92 }104 }
93 }105 }
94 106
95 impl Yumush {107 impl Yumush {
96- pub fn chat_page(&self) -> impl IntoElement {108+ pub fn chat_page(&self, cx: &mut Context<Self>) -> impl IntoElement {
109+ let entity = cx.entity();
97 div()110 div()
98 .flex()111 .flex()
99 .flex_row()112 .flex_row()
100 .size_full()113 .size_full()
114+ .child(
115+ div()
116+ .id("community_list")
117+ .h_full()
118+ .flex()
119+ .flex_col()
120+ .flex_shrink_0()
121+ .w_1_5()
122+ .border_r_1()
123+ .border_color(rgb(0x2f3136))
124+ .bg(rgb(0x2b2d31))
125+ .p_2()
126+ .gap_1()
127+ .child(
128+ div()
129+ .text_color(rgb(0x949ba4))
130+ .font_weight(FontWeight::SEMIBOLD)
131+ .child("Communities"),
132+ )
133+ .children(self.chat.communities.iter().enumerate().map(
134+ |(index, community)| {
135+ let community_id = community.get_community_id().to_string();
136+ let selected =
137+ self.chat.selected_community.as_deref() == Some(&community_id);
138+ let entity = entity.clone();
139+
140+ div()
141+ .id(("community", index))
142+ .p_1()
143+ .cursor_pointer()
144+ .bg(if selected {
145+ rgb(0x404249)
146+ } else {
147+ rgb(0x2b2d31)
148+ })
149+ .child(community.get_community_name().to_string())
150+ .on_click(move |_, window, cx| {
151+ entity.update(cx, |yumush, cx| {
152+ yumush.chat.selected_community =
153+ Some(community_id.to_owned());
154+ yumush.chat.update_user_list(vec![]);
155+ yumush.get_chat_users(window, cx);
156+ cx.notify();
157+ })
158+ })
159+ },
160+ )),
161+ )
101 .child(162 .child(
102 div()163 div()
103 .flex()164 .flex()
@@ -114,7 +175,16 @@ impl Yumush {
114 .overflow_y_scroll()175 .overflow_y_scroll()
115 .p_2()176 .p_2()
116 .gap_1()177 .gap_1()
117- .children(self.chat.chat_messages.iter().map(render_message)),178+ .children(
179+ self.chat
180+ .chat_messages
181+ .iter()
182+ .filter(|message| {
183+ Some(message.get_community_id())
184+ == self.chat.selected_community.as_deref()
185+ })
186+ .map(|message| render_message(message, &self.chat.users)),
187+ ),
118 )188 )
119 .child(189 .child(
120 div()190 div()
@@ -151,9 +221,82 @@ impl Yumush {
151 ),221 ),
152 )222 )
153 }223 }
224+
225+ pub fn get_chat_users(&self, window: &mut Window, cx: &mut Context<Self>) {
226+ let network = self.network.clone();
227+
228+ let Some(community_id) = self.chat.selected_community.to_owned() else {
229+ return;
230+ };
231+
232+ cx.spawn_in(window, async move |this, cx| {
233+ let user_ids = match network.users_in(&community_id).await {
234+ Ok(user_ids) => user_ids,
235+ Err(error_value) => {
236+ let _ = this.update_in(cx, |_, window, cx| {
237+ window.push_notification(Notification::error(error_value.to_string()), cx);
238+ });
239+
240+ return;
241+ }
242+ };
243+
244+ let mut users = Vec::with_capacity(user_ids.len());
245+ for user_id in &user_ids {
246+ if let Ok(user) = network.read_user(user_id).await {
247+ users.push(user);
248+ }
249+ }
250+
251+ let _ = this.update(cx, |yumush, cx| {
252+ yumush.chat.update_user_list(users);
253+ cx.notify();
254+ });
255+ })
256+ .detach();
257+ }
258+
259+ pub fn get_communities(&self, window: &mut Window, cx: &mut Context<Self>) {
260+ let Some(user) = self.get_user() else {
261+ return;
262+ };
263+
264+ let network = self.network.clone();
265+
266+ cx.spawn_in(window, async move |this, cx| {
267+ let community_ids = match network.community_of(user.get_user_id()).await {
268+ Ok(community_ids) => community_ids,
269+ Err(error_value) => {
270+ let _ = this.update_in(cx, |_, window, cx| {
271+ window.push_notification(Notification::error(error_value.to_string()), cx);
272+ });
273+
274+ return;
275+ }
276+ };
277+
278+ let mut communities = Vec::with_capacity(community_ids.len());
279+ for community_id in community_ids {
280+ if let Ok(community) = network.read_community(&community_id).await {
281+ communities.push(community);
282+ }
283+ }
284+
285+ let _ = this.update(cx, |yumush, cx| {
286+ yumush.chat.update_community_list(communities);
287+ cx.notify();
288+ });
289+ })
290+ .detach();
291+ }
154 }292 }
155 293
156-fn render_message(message: &Message) -> impl IntoElement {294+fn render_message(message: &Message, users: &[User]) -> impl IntoElement {
295+ let username = users
296+ .iter()
297+ .find(|user| user.get_user_id() == message.get_user_id())
298+ .map(User::get_username)
299+ .unwrap_or(message.get_user_id());
157 div()300 div()
158 .flex()301 .flex()
159 .flex_col()302 .flex_col()
@@ -162,7 +305,7 @@ fn render_message(message: &Message) -> impl IntoElement {
162 div()305 div()
163 .text_color(rgb(0x7289da))306 .text_color(rgb(0x7289da))
164 .font_weight(FontWeight::BOLD)307 .font_weight(FontWeight::BOLD)
165- .child(message.get_user_id().to_string()),308+ .child(username.to_string()),
166 )309 )
167 .child(div().pl_2().child(message.get_message_body().to_string()))310 .child(div().pl_2().child(message.get_message_body().to_string()))
168 }311 }
crates/client/src/gui/login.rsmodified+1 -0
@@ -55,6 +55,7 @@ fn login_action(
55 Ok(user) => {55 Ok(user) => {
56 yumush.set_user(user);56 yumush.set_user(user);
57 yumush.change_page(Route::Chat, window, cx);57 yumush.change_page(Route::Chat, window, cx);
58+ yumush.time_based_repetitive_checks_after_login(window, cx);
58 }59 }
59 Err(error_value) => {60 Err(error_value) => {
60 window.push_notification(Notification::error(error_value.to_string()), cx);61 window.push_notification(Notification::error(error_value.to_string()), cx);
crates/client/src/lib.rsmodified+1 -0
@@ -1,5 +1,6 @@
1 use common::utils::TOML;1 use common::utils::TOML;
2 2
3+pub mod event;
3 pub mod gui;4 pub mod gui;
4 pub mod network;5 pub mod network;
5 mod request;6 mod request;