yumush
Clone
https://git.tahinli.com/Tahinli/yumush.git
0 watching·0 forks·Rust·AGPL-3.0·6.8 MiB·27ceb05·Tahinli·8 hours ago·399 commits
| 1 | mod access; |
| 2 | mod backups; |
| 3 | mod communities; |
| 4 | mod log; |
| 5 | mod messages; |
| 6 | mod overview; |
| 7 | pub(super) mod releases; |
| 8 | mod reports; |
| 9 | mod server; |
| 10 | mod storage; |
| 11 | mod ui; |
| 12 | mod users; |
| 13 | mod voice; |
| 14 | |
| 15 | use crate::lang::{Label as _, w}; |
| 16 | use common::model::Id; |
| 17 | use std::{fmt::Display, future::Future, path::PathBuf}; |
| 18 | |
| 19 | use common::{ |
| 20 | call::{ListUsers, SetGhost}, |
| 21 | model::{Member, Role}, |
| 22 | permission, |
| 23 | }; |
| 24 | use gpui_kit::{ |
| 25 | AnyElement, App, Context, Entity, IntoElement, ParentElement, Styled, Window, assets::IconName, |
| 26 | component::input::InputState, div, px, |
| 27 | }; |
| 28 | |
| 29 | use self::ui::icon_button; |
| 30 | use super::{ |
| 31 | Palette, Yumush, |
| 32 | chat::View, |
| 33 | entity_action, notify_error, |
| 34 | parts::{self, Press}, |
| 35 | settings_page::{frame, heading}, |
| 36 | style::{size, space, text}, |
| 37 | text, |
| 38 | widget::search_text, |
| 39 | }; |
| 40 | use crate::net::NetworkHandle; |
| 41 | |
| 42 | pub(super) fn save_directory() -> PathBuf { |
| 43 | dirs::download_dir().unwrap_or_else(std::env::temp_dir) |
| 44 | } |
| 45 | |
| 46 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 47 | pub(super) enum BuilderTab { |
| 48 | Overview, |
| 49 | Users, |
| 50 | Communities, |
| 51 | Messages, |
| 52 | Voice, |
| 53 | Storage, |
| 54 | Access, |
| 55 | Reports, |
| 56 | Server, |
| 57 | Releases, |
| 58 | Log, |
| 59 | } |
| 60 | |
| 61 | impl BuilderTab { |
| 62 | pub(super) const ALL: [Self; 11] = [ |
| 63 | Self::Overview, |
| 64 | Self::Users, |
| 65 | Self::Communities, |
| 66 | Self::Messages, |
| 67 | Self::Voice, |
| 68 | Self::Storage, |
| 69 | Self::Access, |
| 70 | Self::Reports, |
| 71 | Self::Server, |
| 72 | Self::Releases, |
| 73 | Self::Log, |
| 74 | ]; |
| 75 | |
| 76 | pub(super) fn id(self) -> &'static str { |
| 77 | match self { |
| 78 | Self::Overview => "overview", |
| 79 | Self::Users => "users", |
| 80 | Self::Communities => "communities", |
| 81 | Self::Messages => "messages", |
| 82 | Self::Voice => "voice", |
| 83 | Self::Storage => "storage", |
| 84 | Self::Access => "access", |
| 85 | Self::Reports => "reports", |
| 86 | Self::Server => "server", |
| 87 | Self::Releases => "releases", |
| 88 | Self::Log => "log", |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | pub(super) fn label(self) -> &'static str { |
| 93 | let w = &w().builder; |
| 94 | match self { |
| 95 | Self::Overview => w.overview, |
| 96 | Self::Users => w.users, |
| 97 | Self::Communities => w.communities, |
| 98 | Self::Messages => crate::lang::w().common.messages, |
| 99 | Self::Voice => w.voice, |
| 100 | Self::Storage => w.storage, |
| 101 | Self::Access => w.access, |
| 102 | Self::Reports => w.reports, |
| 103 | Self::Server => w.server, |
| 104 | Self::Releases => w.releases, |
| 105 | Self::Log => w.log, |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | pub(super) struct Transfer { |
| 111 | pub(super) community_id: Id, |
| 112 | pub(super) members: Vec<Member>, |
| 113 | pub(super) pending: Option<Id>, |
| 114 | } |
| 115 | |
| 116 | impl Yumush { |
| 117 | pub(super) fn builder_page( |
| 118 | &self, |
| 119 | entity: &Entity<Self>, |
| 120 | palette: Palette, |
| 121 | cx: &App, |
| 122 | ) -> impl IntoElement { |
| 123 | let tab = self.chat.builder_tab; |
| 124 | let title = tab.label(); |
| 125 | let body = match tab { |
| 126 | BuilderTab::Overview => self |
| 127 | .overview_rows(palette) |
| 128 | .into_iter() |
| 129 | .map(IntoElement::into_any_element) |
| 130 | .collect(), |
| 131 | BuilderTab::Users => self.users_body(entity, palette, cx), |
| 132 | BuilderTab::Communities => self.communities_body(entity, palette, cx), |
| 133 | BuilderTab::Messages => self.messages_body(entity, palette), |
| 134 | BuilderTab::Voice => self.voice_body(entity, palette), |
| 135 | BuilderTab::Storage => self.storage_body(entity, palette), |
| 136 | BuilderTab::Access => self.access_body(entity, palette), |
| 137 | BuilderTab::Reports => self.reports_body(entity, palette), |
| 138 | BuilderTab::Server => self.server_body(entity, palette), |
| 139 | BuilderTab::Releases => self.releases_body(entity, palette), |
| 140 | BuilderTab::Log => self.log_body(entity, palette), |
| 141 | }; |
| 142 | let mut sections = vec![ |
| 143 | heading(title, self.tab_actions(tab, entity, palette, cx), palette).into_any_element(), |
| 144 | ]; |
| 145 | sections.extend(body); |
| 146 | |
| 147 | frame( |
| 148 | "builder_scroll", |
| 149 | entity, |
| 150 | palette, |
| 151 | Role::Builder.label(), |
| 152 | BuilderTab::ALL |
| 153 | .map(|tab| { |
| 154 | self.builder_nav_item(tab, entity, palette) |
| 155 | .into_any_element() |
| 156 | }) |
| 157 | .into_iter() |
| 158 | .collect(), |
| 159 | vec![self.ghost_toggle(entity, palette).into_any_element()], |
| 160 | sections, |
| 161 | ) |
| 162 | } |
| 163 | |
| 164 | fn tab_actions( |
| 165 | &self, |
| 166 | tab: BuilderTab, |
| 167 | entity: &Entity<Self>, |
| 168 | palette: Palette, |
| 169 | cx: &App, |
| 170 | ) -> Vec<AnyElement> { |
| 171 | match tab { |
| 172 | BuilderTab::Voice => vec![ |
| 173 | parts::button( |
| 174 | "refresh_voice_button".to_string(), |
| 175 | w().builder.refresh, |
| 176 | Press::Plain, |
| 177 | palette.tokens, |
| 178 | entity_action(entity, |yumush, window, cx| { |
| 179 | yumush.load_voice_rooms(window, cx) |
| 180 | }), |
| 181 | ) |
| 182 | .into_any_element(), |
| 183 | ], |
| 184 | BuilderTab::Server if self.server_form(cx) != self.server => vec![ |
| 185 | crate::gui::parts::button( |
| 186 | "save_server", |
| 187 | w().builder.save, |
| 188 | crate::gui::parts::Press::Plain, |
| 189 | palette.tokens, |
| 190 | entity_action(entity, |yumush, window, cx| { |
| 191 | yumush.save_server_settings(window, cx) |
| 192 | }), |
| 193 | ) |
| 194 | .into_any_element(), |
| 195 | ], |
| 196 | BuilderTab::Releases => vec![ |
| 197 | parts::button( |
| 198 | "add_release_files".to_string(), |
| 199 | w().builder_release.add_files, |
| 200 | Press::Plain, |
| 201 | palette.tokens, |
| 202 | entity_action(entity, |yumush, window, cx| { |
| 203 | yumush.pick_release_files(window, cx) |
| 204 | }), |
| 205 | ) |
| 206 | .into_any_element(), |
| 207 | ], |
| 208 | BuilderTab::Log => vec![ |
| 209 | icon_button( |
| 210 | "export_builder_log".to_string(), |
| 211 | IconName::Download, |
| 212 | w().builder.export, |
| 213 | palette.muted, |
| 214 | palette, |
| 215 | entity_action(entity, |yumush, window, cx| { |
| 216 | yumush.export_builder_log(window, cx) |
| 217 | }), |
| 218 | ) |
| 219 | .into_any_element(), |
| 220 | ], |
| 221 | _ => Vec::new(), |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | fn builder_nav_item( |
| 226 | &self, |
| 227 | tab: BuilderTab, |
| 228 | entity: &Entity<Self>, |
| 229 | palette: Palette, |
| 230 | ) -> impl IntoElement { |
| 231 | let open = (tab == BuilderTab::Reports) |
| 232 | .then_some(self.chat.open_reports) |
| 233 | .filter(|count| *count > 0); |
| 234 | |
| 235 | super::parts::index_item( |
| 236 | format!("builder_tab_{}", tab.id()), |
| 237 | tab.label(), |
| 238 | self.chat.builder_tab == tab, |
| 239 | palette.tokens, |
| 240 | entity_action(entity, move |yumush, window, cx| { |
| 241 | yumush.select_builder_tab(tab, window, cx) |
| 242 | }), |
| 243 | ) |
| 244 | .children(open.map(|count| { |
| 245 | div() |
| 246 | .ml(px(space::SM)) |
| 247 | .text_size(text(text::CAPTION)) |
| 248 | .text_color(palette.danger) |
| 249 | .child(count.to_string()) |
| 250 | })) |
| 251 | } |
| 252 | |
| 253 | fn ghost_toggle(&self, entity: &Entity<Self>, palette: Palette) -> impl IntoElement { |
| 254 | let enabled = self.chat.ghost; |
| 255 | let toggle = entity_action(entity, move |yumush, window, cx| { |
| 256 | yumush.set_ghost(!enabled, window, cx) |
| 257 | }); |
| 258 | |
| 259 | div() |
| 260 | .h(px(size::CONTROL)) |
| 261 | .flex() |
| 262 | .items_center() |
| 263 | .justify_between() |
| 264 | .text_size(text(text::LABEL)) |
| 265 | .text_color(palette.muted) |
| 266 | .child(w().builder.ghost) |
| 267 | .child(super::parts::toggle( |
| 268 | "ghost_switch", |
| 269 | enabled, |
| 270 | palette.tokens, |
| 271 | toggle, |
| 272 | )) |
| 273 | } |
| 274 | |
| 275 | pub(super) fn is_builder(&self) -> bool { |
| 276 | self.user |
| 277 | .as_ref() |
| 278 | .is_some_and(|me| permission::is_builder(me.role)) |
| 279 | } |
| 280 | |
| 281 | pub(super) fn load_builder(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 282 | self.load_open_reports(window, cx); |
| 283 | match self.chat.builder_tab { |
| 284 | BuilderTab::Overview => self.load_overview(window, cx), |
| 285 | BuilderTab::Users => self.load_users(window, cx), |
| 286 | BuilderTab::Communities => self.load_builder_communities(window, cx), |
| 287 | BuilderTab::Messages => { |
| 288 | self.load_users(window, cx); |
| 289 | self.load_builder_communities(window, cx); |
| 290 | } |
| 291 | BuilderTab::Storage => { |
| 292 | self.load_users(window, cx); |
| 293 | self.load_builder_communities(window, cx); |
| 294 | self.load_storage(window, cx); |
| 295 | } |
| 296 | BuilderTab::Voice => { |
| 297 | self.load_voice_rooms(window, cx); |
| 298 | self.load_builder_communities(window, cx); |
| 299 | } |
| 300 | BuilderTab::Access => self.load_access(window, cx), |
| 301 | BuilderTab::Reports => self.load_reports(window, cx), |
| 302 | BuilderTab::Server => {} |
| 303 | BuilderTab::Releases => self.load_releases(window, cx), |
| 304 | BuilderTab::Log => { |
| 305 | self.load_users(window, cx); |
| 306 | self.load_builder_logs(window, cx); |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | pub(super) fn report_created(&self, window: &mut Window, cx: &mut Context<Self>) { |
| 312 | self.load_open_reports(window, cx); |
| 313 | if self.chat.view == View::Builder && self.chat.builder_tab == BuilderTab::Reports { |
| 314 | self.load_reports(window, cx); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | pub(super) fn select_builder_tab( |
| 319 | &mut self, |
| 320 | tab: BuilderTab, |
| 321 | window: &mut Window, |
| 322 | cx: &mut Context<Self>, |
| 323 | ) { |
| 324 | self.chat.builder_tab = tab; |
| 325 | if tab == BuilderTab::Server { |
| 326 | self.fill_server_form(window, cx); |
| 327 | } |
| 328 | self.load_builder(window, cx); |
| 329 | cx.notify(); |
| 330 | } |
| 331 | |
| 332 | pub(super) fn fetch_builder<T, E, Fut>( |
| 333 | &self, |
| 334 | window: &mut Window, |
| 335 | cx: &mut Context<Self>, |
| 336 | fetch: impl FnOnce(NetworkHandle) -> Fut + 'static, |
| 337 | apply: impl FnOnce(&mut Self, T, &mut Context<Self>) + 'static, |
| 338 | ) where |
| 339 | T: 'static, |
| 340 | E: Display + 'static, |
| 341 | Fut: Future<Output = Result<T, E>> + 'static, |
| 342 | { |
| 343 | if !self.is_builder() { |
| 344 | return; |
| 345 | } |
| 346 | let network = self.network.clone(); |
| 347 | |
| 348 | cx.spawn_in(window, async move |this, cx| { |
| 349 | let value = match fetch(network).await { |
| 350 | Ok(value) => value, |
| 351 | Err(error) => { |
| 352 | notify_error(&this, cx, error); |
| 353 | return; |
| 354 | } |
| 355 | }; |
| 356 | |
| 357 | let _ = this.update(cx, |yumush, cx| apply(yumush, value, cx)); |
| 358 | }) |
| 359 | .detach(); |
| 360 | } |
| 361 | |
| 362 | pub(super) fn load_users(&self, window: &mut Window, cx: &mut Context<Self>) { |
| 363 | self.fetch_builder( |
| 364 | window, |
| 365 | cx, |
| 366 | |network| async move { network.call(ListUsers).await }, |
| 367 | |yumush, accounts, cx| { |
| 368 | for account in &accounts { |
| 369 | yumush.request_user_avatar(&account.user, cx); |
| 370 | } |
| 371 | let ghost = yumush |
| 372 | .user |
| 373 | .as_ref() |
| 374 | .and_then(|me| accounts.iter().find(|account| account.user.id == me.id)) |
| 375 | .is_some_and(|account| account.ghost); |
| 376 | yumush.chat.builder_users = accounts; |
| 377 | yumush.set_ghost_state(ghost); |
| 378 | cx.notify(); |
| 379 | }, |
| 380 | ); |
| 381 | } |
| 382 | |
| 383 | pub(super) fn releases_changed(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 384 | if self.chat.view == View::Builder && self.chat.builder_tab == BuilderTab::Releases { |
| 385 | self.load_releases(window, cx); |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | pub(super) fn voice_changed(&self, window: &mut Window, cx: &mut Context<Self>) { |
| 390 | if self.chat.view == View::Builder && self.chat.builder_tab == BuilderTab::Voice { |
| 391 | self.load_voice_rooms(window, cx); |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | fn resolve_user(&self, username: &str) -> Option<Id> { |
| 396 | self.chat |
| 397 | .builder_users |
| 398 | .iter() |
| 399 | .find(|account| account.user.username.to_lowercase() == username) |
| 400 | .map(|account| account.user.id) |
| 401 | } |
| 402 | |
| 403 | fn resolve_community(&self, name: &str) -> Option<Id> { |
| 404 | self.chat |
| 405 | .builder_communities |
| 406 | .iter() |
| 407 | .find(|summary| { |
| 408 | summary.community.kind.is_group() && summary.community.name.to_lowercase() == name |
| 409 | }) |
| 410 | .map(|summary| summary.community.id) |
| 411 | } |
| 412 | |
| 413 | pub(super) fn filter_user(&self, input: &Entity<InputState>, cx: &App) -> Option<Option<Id>> { |
| 414 | let name = search_text(input, cx); |
| 415 | if name.is_empty() { |
| 416 | return Some(None); |
| 417 | } |
| 418 | self.resolve_user(&name).map(Some) |
| 419 | } |
| 420 | |
| 421 | pub(super) fn filter_community( |
| 422 | &self, |
| 423 | input: &Entity<InputState>, |
| 424 | cx: &App, |
| 425 | ) -> Option<Option<Id>> { |
| 426 | let name = search_text(input, cx); |
| 427 | if name.is_empty() { |
| 428 | return Some(None); |
| 429 | } |
| 430 | self.resolve_community(&name).map(Some) |
| 431 | } |
| 432 | |
| 433 | fn set_ghost_state(&mut self, ghost: bool) { |
| 434 | self.chat.ghost = ghost; |
| 435 | let Some(me) = self.user.as_ref() else { |
| 436 | return; |
| 437 | }; |
| 438 | if let Some(account) = self |
| 439 | .chat |
| 440 | .builder_users |
| 441 | .iter_mut() |
| 442 | .find(|account| account.user.id == me.id) |
| 443 | { |
| 444 | account.ghost = ghost; |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | fn set_ghost(&self, enabled: bool, window: &mut Window, cx: &mut Context<Self>) { |
| 449 | let network = self.network.clone(); |
| 450 | |
| 451 | cx.spawn_in(window, async move |this, cx| { |
| 452 | if enabled { |
| 453 | let _ = network.stop_share().await; |
| 454 | let _ = network.stop_camera().await; |
| 455 | } |
| 456 | match network.call(SetGhost { enabled }).await { |
| 457 | Ok(()) => { |
| 458 | let _ = this.update(cx, |yumush, cx| { |
| 459 | yumush.set_ghost_state(enabled); |
| 460 | if enabled { |
| 461 | yumush.set_muted(true, cx); |
| 462 | } |
| 463 | cx.notify(); |
| 464 | }); |
| 465 | } |
| 466 | Err(error) => notify_error(&this, cx, error), |
| 467 | } |
| 468 | }) |
| 469 | .detach(); |
| 470 | } |
| 471 | } |