yumush
Clone
https://git.tahinli.com/Tahinli/yumush.git
0 watching·0 forks·Rust·AGPL-3.0·7.2 MiB·4b67e80·Tahinli·1 hour ago·400 commits
| 1 | use super::style::{self, radius, size, space, stroke, text}; |
| 2 | use super::text; |
| 3 | use crate::gui::glyph::Fill as _; |
| 4 | use crate::lang::w; |
| 5 | use std::{ |
| 6 | collections::{BTreeMap, HashMap, HashSet, VecDeque}, |
| 7 | sync::Arc, |
| 8 | time::{Duration, Instant}, |
| 9 | }; |
| 10 | |
| 11 | use crate::gui::camera_view::{Stage, Tile}; |
| 12 | use crate::gui::social::InviteLookup; |
| 13 | use crate::gui::voice_stats::{self, SAMPLE_HISTORY}; |
| 14 | use crate::gui::wave::Waves; |
| 15 | use crate::gui::{ |
| 16 | JUMP_HINT, Palette, Tooltip as _, Yumush, avatar, |
| 17 | builder::{BuilderTab, Transfer}, |
| 18 | clickable, community_name, |
| 19 | community_settings::{BanSort, LogSort}, |
| 20 | composer::{self, Composer}, |
| 21 | entity_action, hoverable, |
| 22 | media_view::{self, Media}, |
| 23 | message_actions, millis_time, new_input, notify_error, palette, parts, poll, presence_avatar, |
| 24 | profile::profile_popover, |
| 25 | share_view::Viewer, |
| 26 | sidebar::unread_badge, |
| 27 | snippet, |
| 28 | voice_note::{self, VoiceNote}, |
| 29 | widget::{ok_or_notify, search_text, spawn_done}, |
| 30 | }; |
| 31 | use crate::net::Levels; |
| 32 | use crate::settings::VOLUME; |
| 33 | use crate::share::session::ShareSample; |
| 34 | use common::{ |
| 35 | call::{ |
| 36 | CreateCommunity, DeclineCall, DeleteMessage, GetUser, LeaveCommunity, ListBans, |
| 37 | ListCommunities, ListEdits, ListMembers, ListMessages, ListPins, ListUnreads, |
| 38 | ListVoiceParticipants, PushStats, SearchCommunity, WatchCommunity, |
| 39 | }, |
| 40 | constant::MESSAGE_PAGE_LIMIT, |
| 41 | event::Event, |
| 42 | model::{ |
| 43 | Account, AdminActionKind, AdminLogEntry, AttachmentInfo, Ban, BanScope, CallLog, Community, |
| 44 | CommunityKind, CommunitySummary, CommunityUsage, DayStat, Edit, Friends, Id, Invitation, |
| 45 | InviteCode, LogEntry, Member, Message, MessageHit, Overview, Presence, Rank, Reply, Report, |
| 46 | ReportStatus, ServerBan, SessionInfo, Unread, UpdateClient, Usage, User, VoiceRoom, |
| 47 | mentions, |
| 48 | }, |
| 49 | permission, |
| 50 | share::{Kind, Share}, |
| 51 | }; |
| 52 | use gpui_kit::{ |
| 53 | Anchor, AnimationExt, AnyElement, App, AppContext, Context, Div, Entity, FontWeight, Image, |
| 54 | InteractiveElement, IntoElement, ParentElement, ScrollHandle, StatefulInteractiveElement, |
| 55 | Styled, Subscription, Task, Window, |
| 56 | assets::IconName, |
| 57 | component::{ |
| 58 | Icon, IndexPath, WindowExt, |
| 59 | input::{InputEvent, InputState, Textarea, TextareaState}, |
| 60 | resizable::{h_resizable, resizable_panel, v_resizable}, |
| 61 | searchable_list::SearchableVec, |
| 62 | select::SelectState, |
| 63 | slider::{SliderEvent, SliderState}, |
| 64 | text::TextView, |
| 65 | }, |
| 66 | div, |
| 67 | prelude::FluentBuilder as _, |
| 68 | px, relative, |
| 69 | }; |
| 70 | |
| 71 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 72 | pub enum StatsAt { |
| 73 | Header, |
| 74 | Dock, |
| 75 | } |
| 76 | |
| 77 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 78 | pub enum View { |
| 79 | Chat, |
| 80 | Settings, |
| 81 | CommunitySettings, |
| 82 | Builder, |
| 83 | } |
| 84 | |
| 85 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 86 | pub enum Armed { |
| 87 | LogOut, |
| 88 | Leave(Id), |
| 89 | RemoveFriend(Id), |
| 90 | DeleteUser(Id), |
| 91 | DeleteAccount, |
| 92 | DeleteCommunity, |
| 93 | RestartServer, |
| 94 | } |
| 95 | |
| 96 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 97 | pub enum VoiceState { |
| 98 | Off, |
| 99 | In { |
| 100 | community_id: Id, |
| 101 | muted: bool, |
| 102 | deafened: bool, |
| 103 | since: Instant, |
| 104 | }, |
| 105 | } |
| 106 | |
| 107 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 108 | enum History { |
| 109 | More, |
| 110 | End, |
| 111 | } |
| 112 | |
| 113 | impl History { |
| 114 | fn of_page(length: usize) -> Self { |
| 115 | if length < MESSAGE_PAGE_LIMIT { |
| 116 | History::End |
| 117 | } else { |
| 118 | History::More |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 124 | pub enum ReportTarget { |
| 125 | Message(Id), |
| 126 | User(Id), |
| 127 | } |
| 128 | |
| 129 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 130 | pub enum CardMenu { |
| 131 | Main, |
| 132 | More, |
| 133 | Status, |
| 134 | } |
| 135 | |
| 136 | pub struct Chat { |
| 137 | pub(super) communities: Vec<Community>, |
| 138 | pub(super) selected_community_id: Option<Id>, |
| 139 | pub(super) opened: Option<Community>, |
| 140 | pub(super) ghost: bool, |
| 141 | pub(super) members: Vec<Member>, |
| 142 | pub(super) messages: Vec<Message>, |
| 143 | pub(super) pins: Vec<Message>, |
| 144 | history: History, |
| 145 | pub(super) unread: HashMap<Id, Unread>, |
| 146 | pub(super) unread_anchor: Option<(Id, u32)>, |
| 147 | unseen: u32, |
| 148 | pub(super) muted: HashSet<Id>, |
| 149 | pub(super) pinned: HashSet<Id>, |
| 150 | pub(super) edits: HashMap<Id, Vec<Edit>>, |
| 151 | pub(super) revealed: HashSet<Id>, |
| 152 | pub(super) message_input: Entity<TextareaState>, |
| 153 | pub(super) search_input: Entity<InputState>, |
| 154 | pub(super) friends_open: bool, |
| 155 | pub(super) open_calls: HashSet<Id>, |
| 156 | pub(super) community_search: Entity<InputState>, |
| 157 | pub(super) community_hits: Vec<MessageHit>, |
| 158 | pub(super) found: Option<User>, |
| 159 | pub(super) invite: InviteLookup, |
| 160 | pub(super) sidebar_collapsed: bool, |
| 161 | pub(super) sidebar_toggled: Option<std::time::Instant>, |
| 162 | pub(super) composer: Composer, |
| 163 | pub(super) drafts: HashMap<Id, String>, |
| 164 | pub(super) poll_question: Entity<InputState>, |
| 165 | pub(super) poll_options: Vec<Entity<InputState>>, |
| 166 | pub(super) poll_multiple: bool, |
| 167 | pub(super) poll_public: bool, |
| 168 | pub(super) poll_duration: Option<u32>, |
| 169 | pub(super) plus_menu: composer::PlusMenu, |
| 170 | pub(super) my_votes: HashMap<Id, Vec<u32>>, |
| 171 | pub(super) poll_expanded: bool, |
| 172 | pub(super) poll_page: usize, |
| 173 | scroll: ScrollHandle, |
| 174 | pub(super) jump: Option<Id>, |
| 175 | highlight: Option<(Id, Instant)>, |
| 176 | glide: Option<(Id, gpui_kit::Pixels, Instant)>, |
| 177 | pub(super) view: View, |
| 178 | pub(super) section: Option<&'static str>, |
| 179 | pub(super) username_input: Entity<InputState>, |
| 180 | pub(super) community_name_input: Entity<InputState>, |
| 181 | pub(super) status_input: Entity<InputState>, |
| 182 | pub(super) avatars: HashMap<Id, Arc<Image>>, |
| 183 | pub(super) avatar_requests: HashSet<Id>, |
| 184 | pub(super) users: HashMap<Id, User>, |
| 185 | user_requests: HashSet<Id>, |
| 186 | pub(super) armed: Option<Armed>, |
| 187 | pub(super) bans: Vec<Ban>, |
| 188 | pub(super) ban_search: Entity<InputState>, |
| 189 | pub(super) ban_sort: BanSort, |
| 190 | pub(super) logs: Vec<LogEntry>, |
| 191 | pub(super) log_search: Entity<InputState>, |
| 192 | pub(super) log_sort: LogSort, |
| 193 | pub(super) friends: Friends, |
| 194 | pub(super) invitations: Vec<Invitation>, |
| 195 | pub(super) card_menu: CardMenu, |
| 196 | pub(super) presences: HashMap<Id, Presence>, |
| 197 | pub(super) slowed_until: Option<u64>, |
| 198 | pub(super) pending_builder_delete: Option<Id>, |
| 199 | pub(super) builder_users: Vec<Account>, |
| 200 | pub(super) pending_builder_password: Option<Id>, |
| 201 | pub(super) builder_password: Entity<InputState>, |
| 202 | pub(super) builder_sessions: Option<(Id, Vec<SessionInfo>)>, |
| 203 | pub(super) builder_clients: Vec<UpdateClient>, |
| 204 | pub(super) builder_clients_task: Option<Task<()>>, |
| 205 | pub(super) builder_friends: Vec<User>, |
| 206 | pub(super) pending_builder_rename: Option<Id>, |
| 207 | pub(super) builder_rename: Entity<InputState>, |
| 208 | pub(super) server_name: Entity<InputState>, |
| 209 | pub(super) server_limit: Entity<InputState>, |
| 210 | pub(super) server_user_quota: Entity<InputState>, |
| 211 | pub(super) server_community_quota: Entity<InputState>, |
| 212 | pub(super) server_message_retention: Entity<InputState>, |
| 213 | pub(super) server_attachment_retention: Entity<InputState>, |
| 214 | pub(super) server_announcement: Entity<InputState>, |
| 215 | pub(super) server_max_communities: Entity<InputState>, |
| 216 | pub(super) server_max_message_length: Entity<InputState>, |
| 217 | pub(super) server_max_avatar: Entity<InputState>, |
| 218 | pub(super) server_messages_per_minute: Entity<InputState>, |
| 219 | pub(super) server_logins_per_minute: Entity<InputState>, |
| 220 | pub(super) server_registration_open: bool, |
| 221 | pub(super) server_maintenance: bool, |
| 222 | pub(super) pending_builder_timeout: Option<Id>, |
| 223 | pub(super) builder_timeout: Entity<InputState>, |
| 224 | pub(super) pending_builder_ban: Option<Id>, |
| 225 | pub(super) builder_ban_reason: Entity<InputState>, |
| 226 | pub(super) builder_ban_scope: BanScope, |
| 227 | pub(super) builder_bans: Vec<ServerBan>, |
| 228 | pub(super) builder_invites: Vec<InviteCode>, |
| 229 | pub(super) invite_uses: Entity<InputState>, |
| 230 | pub(super) invite_hours: Entity<InputState>, |
| 231 | pub(super) pending_invite_delete: Option<String>, |
| 232 | pub(super) new_username: Entity<InputState>, |
| 233 | pub(super) new_password: Entity<InputState>, |
| 234 | pub(super) builder_tab: BuilderTab, |
| 235 | pub(super) builder_overview: Option<Overview>, |
| 236 | pub(super) builder_logs: Vec<AdminLogEntry>, |
| 237 | pub(super) builder_stats: Vec<DayStat>, |
| 238 | pub(super) builder_log_actor: Entity<InputState>, |
| 239 | pub(super) builder_log_target: Entity<InputState>, |
| 240 | pub(super) builder_log_action: Entity<SelectState<SearchableVec<&'static str>>>, |
| 241 | pub(super) builder_log_from: Entity<InputState>, |
| 242 | pub(super) builder_log_to: Entity<InputState>, |
| 243 | pub(super) builder_search: Entity<InputState>, |
| 244 | pub(super) builder_friend_search: Entity<InputState>, |
| 245 | pub(super) builder_communities: Vec<CommunitySummary>, |
| 246 | pub(super) builder_reports: Vec<Report>, |
| 247 | pub(super) report_filter: Option<ReportStatus>, |
| 248 | pub(super) open_reports: usize, |
| 249 | pub(super) report: Option<ReportTarget>, |
| 250 | pub(super) report_reason: Entity<InputState>, |
| 251 | pub(super) pending_community_delete: Option<Id>, |
| 252 | pub(super) builder_message_text: Entity<InputState>, |
| 253 | pub(super) builder_message_user: Entity<InputState>, |
| 254 | pub(super) builder_message_community: Entity<InputState>, |
| 255 | pub(super) builder_message_from: Entity<InputState>, |
| 256 | pub(super) builder_message_to: Entity<InputState>, |
| 257 | pub(super) builder_messages: Vec<MessageHit>, |
| 258 | pub(super) builder_attachment_user: Entity<InputState>, |
| 259 | pub(super) builder_attachment_community: Entity<InputState>, |
| 260 | pub(super) builder_usage_users: Vec<Usage>, |
| 261 | pub(super) builder_usage_communities: Vec<CommunityUsage>, |
| 262 | pub(super) builder_attachments: Vec<AttachmentInfo>, |
| 263 | pub(super) pending_attachment_delete: Option<Id>, |
| 264 | pub(super) pending_message_delete: Option<Id>, |
| 265 | pub(super) hover_row: Option<Id>, |
| 266 | pub(super) hover_bar: Option<Id>, |
| 267 | pub(super) reacting: Option<Id>, |
| 268 | pub(super) pending_builder_purge: Option<Id>, |
| 269 | pub(super) transfer: Option<Transfer>, |
| 270 | pub(super) voice: HashMap<Id, Vec<Id>>, |
| 271 | pub(super) voice_state: VoiceState, |
| 272 | pub(super) waves: Waves, |
| 273 | pub(super) server_muted: HashSet<Id>, |
| 274 | pub(super) builder_voice_rooms: Vec<VoiceRoom>, |
| 275 | pub(super) voice_task: Option<Task<()>>, |
| 276 | pub(super) stats_task: Option<Task<()>>, |
| 277 | pub(super) goto: super::jump::Jump, |
| 278 | pub(super) server_stats: Option<common::stats::Stats>, |
| 279 | pub(super) server_stats_task: Option<Task<()>>, |
| 280 | pub(super) user_volume: Option<UserVolume>, |
| 281 | pub(super) stats: common::stats::Stats, |
| 282 | pub(super) followed: Option<(Id, common::stats::Stats)>, |
| 283 | pub(super) share_loss: HashMap<(Id, Kind), VecDeque<(u64, f32)>>, |
| 284 | pub(super) stats_at: Option<StatsAt>, |
| 285 | pub(super) usage_monitor: crate::usage::Monitor, |
| 286 | pub(super) ringing: HashSet<Id>, |
| 287 | pub(super) ring_task: Option<Task<()>>, |
| 288 | pub(super) media: Media, |
| 289 | pub(super) shares: HashMap<Id, Vec<Share>>, |
| 290 | pub(super) viewer: Option<Viewer>, |
| 291 | pub(super) float: super::share_view::Float, |
| 292 | pub(super) tiles: BTreeMap<(Id, Kind), Tile>, |
| 293 | pub(super) camera_task: Option<Task<()>>, |
| 294 | pub(super) stage: Stage, |
| 295 | pub(super) grid_size: (f32, f32), |
| 296 | pub(super) strip_size: (f32, f32), |
| 297 | pub(super) typing: HashMap<Id, HashMap<Id, Instant>>, |
| 298 | pub(super) typing_sent: HashMap<Id, Instant>, |
| 299 | } |
| 300 | |
| 301 | const RING_LIMIT: usize = 15; |
| 302 | const RING_INTERVAL: Duration = Duration::from_secs(2); |
| 303 | const VOICE_SAMPLE_INTERVAL: Duration = Duration::from_secs(1); |
| 304 | const WAVE_INTERVAL: Duration = Duration::from_millis(33); |
| 305 | const DEFAULT_USER_VOLUME: f32 = 100.0; |
| 306 | |
| 307 | pub struct UserVolume { |
| 308 | pub user_id: Id, |
| 309 | pub slider: Entity<SliderState>, |
| 310 | _subscription: Subscription, |
| 311 | } |
| 312 | const DELETE_CONFIRM: Duration = Duration::from_secs(3); |
| 313 | pub const TYPING_SEND_INTERVAL: Duration = Duration::from_secs(3); |
| 314 | pub const TYPING_TTL: Duration = Duration::from_secs(5); |
| 315 | const HIGHLIGHT_TTL: Duration = Duration::from_secs(2); |
| 316 | |
| 317 | impl Chat { |
| 318 | pub fn new(window: &mut Window, cx: &mut Context<Yumush>) -> Self { |
| 319 | let message_input = cx.new(|cx| { |
| 320 | let mut state = TextareaState::new(window, cx) |
| 321 | .auto_grow(1, 10) |
| 322 | .submit_on_enter(true); |
| 323 | state.set_placeholder(w().chat.message, window, cx); |
| 324 | state |
| 325 | }); |
| 326 | cx.subscribe_in( |
| 327 | &message_input, |
| 328 | window, |
| 329 | |this, input, event, window, cx| match event { |
| 330 | InputEvent::PressEnter { shift: false, .. } => { |
| 331 | this.submit_composer(input, window, cx) |
| 332 | } |
| 333 | InputEvent::Change => { |
| 334 | input.update(cx, |state, cx| state.mark(cx)); |
| 335 | this.send_typing(cx) |
| 336 | } |
| 337 | _ => {} |
| 338 | }, |
| 339 | ) |
| 340 | .detach(); |
| 341 | |
| 342 | let jump_input = new_input(w().jump.placeholder, window, cx); |
| 343 | cx.subscribe_in( |
| 344 | &jump_input, |
| 345 | window, |
| 346 | |this, _, event, window, cx| match event { |
| 347 | InputEvent::Change => { |
| 348 | this.chat.goto.cursor = 0; |
| 349 | this.chat.goto.scroll.scroll_to_item(0); |
| 350 | cx.notify(); |
| 351 | } |
| 352 | InputEvent::PressEnter { .. } => this.jump_enter(window, cx), |
| 353 | _ => {} |
| 354 | }, |
| 355 | ) |
| 356 | .detach(); |
| 357 | |
| 358 | let search_input = new_input(w().chat.find_user, window, cx); |
| 359 | cx.subscribe_in( |
| 360 | &search_input, |
| 361 | window, |
| 362 | |this, _, event, window, cx| match event { |
| 363 | InputEvent::Change => this.search_user(window, cx), |
| 364 | InputEvent::PressEnter { .. } => this.open_found(window, cx), |
| 365 | _ => {} |
| 366 | }, |
| 367 | ) |
| 368 | .detach(); |
| 369 | |
| 370 | let community_search = new_input(w().chat.find, window, cx); |
| 371 | cx.subscribe_in( |
| 372 | &community_search, |
| 373 | window, |
| 374 | |this, _, event, window, cx| match event { |
| 375 | InputEvent::PressEnter { .. } => this.search_community(window, cx), |
| 376 | InputEvent::Change => cx.notify(), |
| 377 | _ => {} |
| 378 | }, |
| 379 | ) |
| 380 | .detach(); |
| 381 | |
| 382 | let ban_search = new_input(w().chat.search, window, cx); |
| 383 | let log_search = new_input(w().chat.search, window, cx); |
| 384 | let builder_search = new_input(w().chat.search, window, cx); |
| 385 | let builder_friend_search = new_input(w().chat.search, window, cx); |
| 386 | let builder_password = new_input(w().chat.new_password, window, cx); |
| 387 | builder_password.update(cx, |input, cx| input.set_masked(true, window, cx)); |
| 388 | let new_password = new_input(w().chat.password, window, cx); |
| 389 | new_password.update(cx, |input, cx| input.set_masked(true, window, cx)); |
| 390 | for search in [ |
| 391 | &ban_search, |
| 392 | &log_search, |
| 393 | &builder_search, |
| 394 | &builder_friend_search, |
| 395 | ] { |
| 396 | cx.subscribe_in(search, window, |_, _, event, window, cx| { |
| 397 | if matches!(event, InputEvent::Change) { |
| 398 | window.refresh(); |
| 399 | cx.notify(); |
| 400 | } |
| 401 | }) |
| 402 | .detach(); |
| 403 | } |
| 404 | |
| 405 | Self { |
| 406 | communities: vec![], |
| 407 | selected_community_id: None, |
| 408 | opened: None, |
| 409 | ghost: false, |
| 410 | members: vec![], |
| 411 | bans: vec![], |
| 412 | ban_search, |
| 413 | ban_sort: BanSort::Newest, |
| 414 | logs: vec![], |
| 415 | log_search, |
| 416 | log_sort: LogSort::Newest, |
| 417 | friends: Friends::default(), |
| 418 | invitations: vec![], |
| 419 | messages: vec![], |
| 420 | pins: vec![], |
| 421 | poll_question: new_input(w().chat.question, window, cx), |
| 422 | poll_options: vec![ |
| 423 | composer::new_poll_option(window, cx), |
| 424 | composer::new_poll_option(window, cx), |
| 425 | ], |
| 426 | poll_multiple: false, |
| 427 | poll_public: true, |
| 428 | poll_duration: None, |
| 429 | plus_menu: composer::PlusMenu::Menu, |
| 430 | my_votes: HashMap::new(), |
| 431 | poll_expanded: false, |
| 432 | poll_page: 0, |
| 433 | history: History::End, |
| 434 | scroll: ScrollHandle::new(), |
| 435 | jump: None, |
| 436 | highlight: None, |
| 437 | glide: None, |
| 438 | unread: HashMap::new(), |
| 439 | unread_anchor: None, |
| 440 | unseen: 0, |
| 441 | muted: HashSet::new(), |
| 442 | pinned: HashSet::new(), |
| 443 | edits: HashMap::new(), |
| 444 | revealed: HashSet::new(), |
| 445 | message_input, |
| 446 | search_input, |
| 447 | friends_open: false, |
| 448 | open_calls: HashSet::new(), |
| 449 | community_search, |
| 450 | community_hits: vec![], |
| 451 | found: None, |
| 452 | invite: InviteLookup::Idle, |
| 453 | sidebar_collapsed: false, |
| 454 | sidebar_toggled: None, |
| 455 | view: View::Chat, |
| 456 | section: None, |
| 457 | username_input: editable_input(w().chat.username, Yumush::save_username, window, cx), |
| 458 | community_name_input: editable_input( |
| 459 | w().chat.community_name, |
| 460 | Yumush::save_community_name, |
| 461 | window, |
| 462 | cx, |
| 463 | ), |
| 464 | status_input: editable_input(w().chat.status, Yumush::save_status, window, cx), |
| 465 | avatars: HashMap::new(), |
| 466 | avatar_requests: HashSet::new(), |
| 467 | users: HashMap::new(), |
| 468 | user_requests: HashSet::new(), |
| 469 | armed: None, |
| 470 | card_menu: CardMenu::Main, |
| 471 | presences: HashMap::new(), |
| 472 | slowed_until: None, |
| 473 | pending_builder_delete: None, |
| 474 | builder_users: vec![], |
| 475 | builder_tab: BuilderTab::Overview, |
| 476 | builder_overview: None, |
| 477 | builder_logs: vec![], |
| 478 | builder_stats: vec![], |
| 479 | builder_log_actor: new_input(w().chat.actor, window, cx), |
| 480 | builder_log_target: new_input(w().chat.target, window, cx), |
| 481 | builder_log_action: cx.new(|cx| { |
| 482 | SelectState::new( |
| 483 | SearchableVec::new( |
| 484 | std::iter::once(w().chat.all) |
| 485 | .chain( |
| 486 | AdminActionKind::ALL |
| 487 | .into_iter() |
| 488 | .map(AdminActionKind::as_str), |
| 489 | ) |
| 490 | .collect::<Vec<_>>(), |
| 491 | ), |
| 492 | Some(IndexPath::new(0)), |
| 493 | window, |
| 494 | cx, |
| 495 | ) |
| 496 | }), |
| 497 | builder_log_from: new_input(w().chat.date_from, window, cx), |
| 498 | builder_log_to: new_input(w().chat.date_to, window, cx), |
| 499 | builder_search, |
| 500 | builder_friend_search, |
| 501 | builder_communities: vec![], |
| 502 | pending_community_delete: None, |
| 503 | builder_message_text: new_input(w().chat.text, window, cx), |
| 504 | builder_message_user: new_input(w().chat.user, window, cx), |
| 505 | builder_message_community: new_input(w().chat.community, window, cx), |
| 506 | builder_message_from: new_input(w().chat.date_from, window, cx), |
| 507 | builder_message_to: new_input(w().chat.date_to, window, cx), |
| 508 | builder_messages: vec![], |
| 509 | builder_attachment_user: new_input(w().chat.user, window, cx), |
| 510 | builder_attachment_community: new_input(w().chat.community, window, cx), |
| 511 | builder_usage_users: vec![], |
| 512 | builder_usage_communities: vec![], |
| 513 | builder_attachments: vec![], |
| 514 | pending_attachment_delete: None, |
| 515 | pending_message_delete: None, |
| 516 | hover_row: None, |
| 517 | hover_bar: None, |
| 518 | reacting: None, |
| 519 | pending_builder_purge: None, |
| 520 | transfer: None, |
| 521 | builder_password, |
| 522 | server_name: new_input(w().chat.server_name, window, cx), |
| 523 | server_limit: new_input("", window, cx), |
| 524 | server_user_quota: new_input("", window, cx), |
| 525 | server_community_quota: new_input("", window, cx), |
| 526 | server_message_retention: new_input("", window, cx), |
| 527 | server_attachment_retention: new_input("", window, cx), |
| 528 | server_announcement: new_input(w().chat.announcement, window, cx), |
| 529 | server_max_communities: new_input("", window, cx), |
| 530 | server_max_message_length: new_input(w().chat.max_message_length, window, cx), |
| 531 | server_max_avatar: new_input("", window, cx), |
| 532 | server_messages_per_minute: new_input("", window, cx), |
| 533 | server_logins_per_minute: new_input("", window, cx), |
| 534 | server_registration_open: true, |
| 535 | server_maintenance: false, |
| 536 | pending_builder_timeout: None, |
| 537 | builder_timeout: new_input(w().chat.minutes, window, cx), |
| 538 | pending_builder_ban: None, |
| 539 | builder_ban_reason: new_input(w().chat.reason, window, cx), |
| 540 | builder_ban_scope: BanScope::Account, |
| 541 | builder_bans: vec![], |
| 542 | builder_invites: vec![], |
| 543 | invite_uses: new_input(w().chat.uses, window, cx), |
| 544 | invite_hours: new_input(w().chat.hours, window, cx), |
| 545 | pending_invite_delete: None, |
| 546 | new_username: new_input(w().chat.username, window, cx), |
| 547 | new_password, |
| 548 | pending_builder_password: None, |
| 549 | builder_sessions: None, |
| 550 | builder_clients: Vec::new(), |
| 551 | builder_clients_task: None, |
| 552 | builder_friends: Vec::new(), |
| 553 | pending_builder_rename: None, |
| 554 | builder_rename: new_input(w().chat.new_username, window, cx), |
| 555 | builder_reports: vec![], |
| 556 | report_filter: Some(ReportStatus::Open), |
| 557 | open_reports: 0, |
| 558 | report: None, |
| 559 | report_reason: new_input(w().chat.reason, window, cx), |
| 560 | voice: HashMap::new(), |
| 561 | drafts: HashMap::new(), |
| 562 | voice_state: VoiceState::Off, |
| 563 | waves: Waves::default(), |
| 564 | user_volume: None, |
| 565 | server_muted: HashSet::new(), |
| 566 | builder_voice_rooms: Vec::new(), |
| 567 | voice_task: None, |
| 568 | stats_task: None, |
| 569 | goto: super::jump::Jump { |
| 570 | open: false, |
| 571 | cursor: 0, |
| 572 | input: jump_input, |
| 573 | scroll: ScrollHandle::new(), |
| 574 | }, |
| 575 | server_stats: None, |
| 576 | server_stats_task: None, |
| 577 | stats: common::stats::Stats::default(), |
| 578 | followed: None, |
| 579 | share_loss: HashMap::new(), |
| 580 | stats_at: None, |
| 581 | usage_monitor: { |
| 582 | let monitor = crate::usage::Monitor::default(); |
| 583 | if let Some(dir) = crate::config_dir() { |
| 584 | monitor.watch_disk(dir); |
| 585 | } |
| 586 | monitor |
| 587 | }, |
| 588 | ringing: HashSet::new(), |
| 589 | ring_task: None, |
| 590 | typing: HashMap::new(), |
| 591 | typing_sent: HashMap::new(), |
| 592 | media: Media::default(), |
| 593 | shares: HashMap::new(), |
| 594 | viewer: None, |
| 595 | float: Default::default(), |
| 596 | tiles: BTreeMap::new(), |
| 597 | camera_task: None, |
| 598 | stage: Stage::Strip, |
| 599 | grid_size: (1280., 720.), |
| 600 | strip_size: (1280., 0.), |
| 601 | composer: Composer::Compose { reply: None }, |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | fn unread_where(&self, keep: impl Fn(&CommunityKind) -> bool) -> Unread { |
| 606 | self.communities |
| 607 | .iter() |
| 608 | .filter(|community| !self.muted.contains(&community.id) && keep(&community.kind)) |
| 609 | .filter_map(|community| self.unread.get(&community.id)) |
| 610 | .fold(Unread::default(), |total, unread| Unread { |
| 611 | messages: total.messages + unread.messages, |
| 612 | mentions: total.mentions + unread.mentions, |
| 613 | ..total |
| 614 | }) |
| 615 | } |
| 616 | |
| 617 | pub(super) fn unread_groups(&self) -> Unread { |
| 618 | self.unread_where(|kind| kind.is_group()) |
| 619 | } |
| 620 | |
| 621 | pub(super) fn unread_direct(&self) -> Unread { |
| 622 | self.unread_where(|kind| !kind.is_group()) |
| 623 | } |
| 624 | |
| 625 | pub fn focus(&self, window: &mut Window, cx: &mut Context<Yumush>) { |
| 626 | match self.selected_community_id { |
| 627 | Some(_) => self |
| 628 | .message_input |
| 629 | .update(cx, |input, cx| input.focus(window, cx)), |
| 630 | None => self |
| 631 | .search_input |
| 632 | .update(cx, |input, cx| input.focus(window, cx)), |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | pub(super) fn call_run_end(&self, index: usize) -> Id { |
| 637 | let calls = self.messages[index..] |
| 638 | .iter() |
| 639 | .take_while(|message| message.call.is_some()) |
| 640 | .count(); |
| 641 | self.messages[index + calls.max(1) - 1].id |
| 642 | } |
| 643 | |
| 644 | pub fn name_of(&self, user_id: Id) -> String { |
| 645 | self.author(user_id) |
| 646 | .map_or_else(|| user_id.to_string(), |user| user.username.clone()) |
| 647 | } |
| 648 | |
| 649 | pub fn slow_seconds(&self) -> Option<u64> { |
| 650 | let remaining = self.slowed_until?.saturating_sub(common::time::now_ms()); |
| 651 | (remaining > 0).then(|| remaining.div_ceil(1000)) |
| 652 | } |
| 653 | |
| 654 | pub fn apply_event(&mut self, event: Event, me: Option<Id>, builder: bool) { |
| 655 | match event { |
| 656 | Event::MessageCreated(message) => { |
| 657 | if let Some(users) = self.typing.get_mut(&message.community_id) { |
| 658 | users.remove(&message.user_id); |
| 659 | } |
| 660 | if self.is_selected(message.community_id) { |
| 661 | self.insert_messages([message], me); |
| 662 | } |
| 663 | } |
| 664 | Event::MessageUpdated(message) => { |
| 665 | if self.is_selected(message.community_id) { |
| 666 | self.update_pin(&message); |
| 667 | if let Some(existing) = self |
| 668 | .messages |
| 669 | .iter_mut() |
| 670 | .find(|existing| existing.id == message.id) |
| 671 | { |
| 672 | *existing = message.clone(); |
| 673 | } |
| 674 | let body = match &message.poll { |
| 675 | Some(poll) if message.body.is_empty() => poll.question.clone(), |
| 676 | _ => message.body.clone(), |
| 677 | }; |
| 678 | for quote in self.quotes_of(message.id) { |
| 679 | quote.body = body.clone(); |
| 680 | } |
| 681 | } |
| 682 | } |
| 683 | Event::MyVotes { |
| 684 | message_id, |
| 685 | positions, |
| 686 | } => { |
| 687 | self.my_votes.insert(message_id, positions); |
| 688 | } |
| 689 | Event::MessagesDeleted { community_id, ids } => { |
| 690 | if self.is_selected(community_id) { |
| 691 | for id in ids { |
| 692 | self.remove_message(id, builder); |
| 693 | } |
| 694 | } |
| 695 | } |
| 696 | Event::MemberJoined { |
| 697 | community_id, |
| 698 | member, |
| 699 | } => { |
| 700 | if self.is_selected(community_id) { |
| 701 | self.insert_member(member); |
| 702 | } |
| 703 | } |
| 704 | Event::MemberLeft { |
| 705 | community_id, |
| 706 | user_id, |
| 707 | } => { |
| 708 | if self.is_selected(community_id) { |
| 709 | self.remove_member(user_id); |
| 710 | } |
| 711 | } |
| 712 | Event::RankChanged { |
| 713 | community_id, |
| 714 | user_id, |
| 715 | rank, |
| 716 | } => { |
| 717 | if self.is_selected(community_id) { |
| 718 | self.set_rank(user_id, rank); |
| 719 | } |
| 720 | } |
| 721 | Event::CommunityUpdated(community) | Event::CommunityAdded(community) => { |
| 722 | self.insert_community(community) |
| 723 | } |
| 724 | Event::FriendsUpdated(friends) => { |
| 725 | style::remember( |
| 726 | &friends |
| 727 | .friends |
| 728 | .iter() |
| 729 | .chain(&friends.incoming) |
| 730 | .chain(&friends.outgoing) |
| 731 | .collect::<Vec<_>>(), |
| 732 | ); |
| 733 | self.friends = friends; |
| 734 | } |
| 735 | Event::InvitationsUpdated(invitations) => self.invitations = invitations, |
| 736 | Event::CommunityDeleted { community_id } | Event::Removed { community_id, .. } => { |
| 737 | self.remove_community(community_id) |
| 738 | } |
| 739 | Event::UserDeleted { user_id } => { |
| 740 | self.remove_member(user_id); |
| 741 | self.messages.retain(|message| message.user_id != user_id); |
| 742 | } |
| 743 | Event::UserUpdated(user) => self.update_member(&user), |
| 744 | Event::PresenceChanged { user_id, presence } => { |
| 745 | self.presences.insert(user_id, presence); |
| 746 | } |
| 747 | Event::VoiceJoined { |
| 748 | community_id, |
| 749 | user_id, |
| 750 | } => { |
| 751 | let ids = self.voice.entry(community_id).or_default(); |
| 752 | if !ids.contains(&user_id) { |
| 753 | ids.push(user_id); |
| 754 | } |
| 755 | } |
| 756 | Event::VoiceLeft { |
| 757 | community_id, |
| 758 | user_id, |
| 759 | } => { |
| 760 | if let Some(ids) = self.voice.get_mut(&community_id) { |
| 761 | ids.retain(|id| *id != user_id); |
| 762 | if ids.is_empty() { |
| 763 | self.ringing.remove(&community_id); |
| 764 | } |
| 765 | } |
| 766 | } |
| 767 | Event::VoiceMuted { user_id, muted, .. } => { |
| 768 | if muted { |
| 769 | self.server_muted.insert(user_id); |
| 770 | } else { |
| 771 | self.server_muted.remove(&user_id); |
| 772 | } |
| 773 | } |
| 774 | Event::Typing { |
| 775 | community_id, |
| 776 | user_id, |
| 777 | } => { |
| 778 | self.typing |
| 779 | .entry(community_id) |
| 780 | .or_default() |
| 781 | .insert(user_id, Instant::now()); |
| 782 | } |
| 783 | Event::ShareStarted { |
| 784 | community_id, |
| 785 | share, |
| 786 | } => { |
| 787 | let shares = self.shares.entry(community_id).or_default(); |
| 788 | shares.retain(|existing| { |
| 789 | existing.user_id != share.user_id || existing.kind != share.kind |
| 790 | }); |
| 791 | shares.push(share); |
| 792 | } |
| 793 | Event::ShareStopped { |
| 794 | community_id, |
| 795 | user_id, |
| 796 | kind, |
| 797 | } => { |
| 798 | if let Some(shares) = self.shares.get_mut(&community_id) { |
| 799 | shares.retain(|share| share.user_id != user_id || share.kind != kind); |
| 800 | } |
| 801 | } |
| 802 | Event::StatsWanted(_) => {} |
| 803 | Event::StatsOf { user_id, stats } => { |
| 804 | if let Some(followed) = self.followed.as_mut().filter(|(id, _)| *id == user_id) { |
| 805 | followed.1 = stats; |
| 806 | } |
| 807 | } |
| 808 | Event::ReportCreated | Event::SettingsUpdated(_) | Event::Release => {} |
| 809 | } |
| 810 | } |
| 811 | |
| 812 | pub(super) fn is_selected(&self, community_id: Id) -> bool { |
| 813 | self.selected_community_id == Some(community_id) |
| 814 | } |
| 815 | |
| 816 | pub fn typing_label(&self) -> Option<String> { |
| 817 | let community_id = self.selected_community_id.as_ref()?; |
| 818 | let names: Vec<&str> = self |
| 819 | .typing |
| 820 | .get(community_id)? |
| 821 | .iter() |
| 822 | .filter(|(_, at)| at.elapsed() < TYPING_TTL) |
| 823 | .map(|(user_id, _)| { |
| 824 | self.author(*user_id) |
| 825 | .map_or(w().chat.someone, |user| user.username.as_str()) |
| 826 | }) |
| 827 | .collect(); |
| 828 | match names.as_slice() { |
| 829 | [] => None, |
| 830 | [name] => Some(w().chat.typing_one(name)), |
| 831 | names => Some(w().chat.typing_many(names.len())), |
| 832 | } |
| 833 | } |
| 834 | |
| 835 | fn select(&mut self, community_id: Option<Id>) { |
| 836 | if community_id != self.selected_community_id { |
| 837 | self.unread_anchor = community_id |
| 838 | .as_ref() |
| 839 | .and_then(|community_id| self.unread.get(community_id)) |
| 840 | .filter(|unread| unread.messages > 0) |
| 841 | .and_then(|unread| { |
| 842 | unread |
| 843 | .last_read |
| 844 | .map(|last_read| (last_read, unread.messages)) |
| 845 | }); |
| 846 | self.unseen = 0; |
| 847 | } |
| 848 | if self.opened.as_ref().map(|opened| &opened.id) != community_id.as_ref() { |
| 849 | self.opened = None; |
| 850 | } |
| 851 | if self.view == View::CommunitySettings { |
| 852 | self.view = View::Chat; |
| 853 | self.armed = None; |
| 854 | } |
| 855 | self.selected_community_id = community_id; |
| 856 | self.users.extend( |
| 857 | self.members |
| 858 | .drain(..) |
| 859 | .map(|member| (member.user.id, member.user)), |
| 860 | ); |
| 861 | self.bans.clear(); |
| 862 | self.logs.clear(); |
| 863 | self.armed = None; |
| 864 | self.messages.clear(); |
| 865 | self.jump = None; |
| 866 | self.pins.clear(); |
| 867 | self.poll_expanded = false; |
| 868 | self.poll_page = 0; |
| 869 | self.my_votes.clear(); |
| 870 | self.community_hits.clear(); |
| 871 | self.history = History::End; |
| 872 | self.media.leave(); |
| 873 | self.composer = Composer::Compose { reply: None }; |
| 874 | } |
| 875 | |
| 876 | pub fn joined(&self, community_id: Id) -> bool { |
| 877 | self.communities |
| 878 | .iter() |
| 879 | .chain( |
| 880 | self.opened |
| 881 | .as_ref() |
| 882 | .filter(|opened| opened.kind.is_direct()), |
| 883 | ) |
| 884 | .any(|community| community.id == community_id) |
| 885 | } |
| 886 | |
| 887 | fn quotes_of(&mut self, message_id: Id) -> impl Iterator<Item = &mut common::model::Quote> { |
| 888 | self.messages |
| 889 | .iter_mut() |
| 890 | .filter_map(move |message| match &mut message.reply { |
| 891 | Some(Reply::Quote(quote)) if quote.message_id == message_id => Some(quote), |
| 892 | _ => None, |
| 893 | }) |
| 894 | } |
| 895 | |
| 896 | fn remove_message(&mut self, message_id: Id, builder: bool) { |
| 897 | if builder { |
| 898 | if let Some(message) = self |
| 899 | .messages |
| 900 | .iter_mut() |
| 901 | .find(|existing| existing.id == message_id) |
| 902 | { |
| 903 | message.deleted = true; |
| 904 | message.pinned = false; |
| 905 | } |
| 906 | } else { |
| 907 | self.messages.retain(|existing| existing.id != message_id); |
| 908 | } |
| 909 | for message in &mut self.messages { |
| 910 | message.reply = match message.reply.take() { |
| 911 | Some(Reply::Quote(quote)) if quote.message_id == message_id => { |
| 912 | Some(Reply::Deleted(builder.then_some(quote))) |
| 913 | } |
| 914 | other => other, |
| 915 | }; |
| 916 | } |
| 917 | |
| 918 | match &mut self.composer { |
| 919 | Composer::Select { ids } => ids.retain(|id| *id != message_id), |
| 920 | Composer::Compose { reply } if *reply == Some(message_id) => { |
| 921 | self.composer = Composer::Compose { reply: None }; |
| 922 | } |
| 923 | Composer::Edit { message_id: edited } if *edited == message_id => { |
| 924 | self.composer = Composer::Compose { reply: None }; |
| 925 | } |
| 926 | _ => {} |
| 927 | } |
| 928 | } |
| 929 | |
| 930 | pub fn communities(&self) -> &[Community] { |
| 931 | &self.communities |
| 932 | } |
| 933 | |
| 934 | pub fn notes(&self) -> Option<&Community> { |
| 935 | self.communities |
| 936 | .iter() |
| 937 | .find(|community| matches!(community.kind, CommunityKind::Notes)) |
| 938 | } |
| 939 | |
| 940 | fn insert_messages(&mut self, messages: impl IntoIterator<Item = Message>, me: Option<Id>) { |
| 941 | let newest = self.messages.last().map(|message| message.id); |
| 942 | for message in messages { |
| 943 | self.messages.retain(|existing| existing.id != message.id); |
| 944 | self.messages.push(message); |
| 945 | } |
| 946 | self.messages.sort_by_key(|message| message.id); |
| 947 | if self.messages.last().map(|message| message.id) != newest { |
| 948 | let mine = self |
| 949 | .messages |
| 950 | .last() |
| 951 | .is_some_and(|message| Some(message.user_id) == me); |
| 952 | if newest.is_none() { |
| 953 | match self.unread_divider(me) { |
| 954 | Some(index) => self.scroll_to_index(index), |
| 955 | None => self.scroll.scroll_to_bottom(), |
| 956 | } |
| 957 | } else if !self.away() { |
| 958 | let newest = self.messages.last().map_or(0, |message| message.id); |
| 959 | self.glide = Some((newest, self.scroll.offset().y, Instant::now())); |
| 960 | self.unseen = 0; |
| 961 | } else if mine { |
| 962 | self.jump_to_bottom(); |
| 963 | } else { |
| 964 | self.unseen += 1; |
| 965 | } |
| 966 | } |
| 967 | self.reveal_jump(); |
| 968 | } |
| 969 | |
| 970 | fn reveal_jump(&mut self) { |
| 971 | let Some(message_id) = self.jump.as_ref() else { |
| 972 | return; |
| 973 | }; |
| 974 | let Some(position) = self |
| 975 | .messages |
| 976 | .iter() |
| 977 | .position(|message| &message.id == message_id) |
| 978 | else { |
| 979 | return; |
| 980 | }; |
| 981 | self.scroll_to_index(position); |
| 982 | self.highlight = self |
| 983 | .jump |
| 984 | .take() |
| 985 | .map(|message_id| (message_id, Instant::now())); |
| 986 | } |
| 987 | |
| 988 | fn scroll_to_index(&self, position: usize) { |
| 989 | let spacer = 1; |
| 990 | let older = usize::from(self.history == History::More); |
| 991 | self.scroll.scroll_to_item(spacer + older + position); |
| 992 | } |
| 993 | |
| 994 | fn away(&self) -> bool { |
| 995 | self.scroll.max_offset().y + self.scroll.offset().y > self.scroll.bounds().size.height |
| 996 | } |
| 997 | |
| 998 | pub(super) fn engaged(&self, message_id: Id) -> bool { |
| 999 | self.hover_bar == Some(message_id) || self.reacting == Some(message_id) |
| 1000 | } |
| 1001 | |
| 1002 | pub(super) fn jump_to_bottom(&mut self) { |
| 1003 | self.scroll.scroll_to_bottom(); |
| 1004 | self.unseen = 0; |
| 1005 | } |
| 1006 | |
| 1007 | pub(super) fn settle_scroll(&mut self) { |
| 1008 | if !self.away() { |
| 1009 | self.unseen = 0; |
| 1010 | } |
| 1011 | } |
| 1012 | |
| 1013 | fn unread_covered(&self) -> bool { |
| 1014 | match (&self.unread_anchor, self.messages.first()) { |
| 1015 | (Some((anchor, _)), Some(first)) => self.history == History::End || first.id <= *anchor, |
| 1016 | _ => true, |
| 1017 | } |
| 1018 | } |
| 1019 | |
| 1020 | fn unread_divider(&self, me: Option<Id>) -> Option<usize> { |
| 1021 | let (anchor, _) = self.unread_anchor?; |
| 1022 | if !self.unread_covered() { |
| 1023 | return None; |
| 1024 | } |
| 1025 | self.messages |
| 1026 | .iter() |
| 1027 | .position(|message| message.id > anchor && Some(message.user_id) != me) |
| 1028 | } |
| 1029 | |
| 1030 | pub(super) fn mark_read(&mut self, community_id: Id, message_id: Id) { |
| 1031 | let unread = self.unread.entry(community_id).or_insert_with(|| Unread { |
| 1032 | community_id, |
| 1033 | ..Default::default() |
| 1034 | }); |
| 1035 | unread.messages = 0; |
| 1036 | unread.mentions = 0; |
| 1037 | unread.last_read = Some(message_id); |
| 1038 | } |
| 1039 | |
| 1040 | fn add_page(&mut self, page: Vec<Message>, me: Option<Id>) { |
| 1041 | self.history = History::of_page(page.len()); |
| 1042 | self.insert_messages(page, me); |
| 1043 | } |
| 1044 | |
| 1045 | fn insert_member(&mut self, member: Member) { |
| 1046 | style::remember(&[&member.user]); |
| 1047 | self.remove_member(member.user.id); |
| 1048 | self.members.push(member); |
| 1049 | self.members.sort_by_key(|member| member.user.id); |
| 1050 | } |
| 1051 | |
| 1052 | pub(super) fn remove_member(&mut self, user_id: Id) { |
| 1053 | if let Some(index) = self |
| 1054 | .members |
| 1055 | .iter() |
| 1056 | .position(|member| member.user.id == user_id) |
| 1057 | { |
| 1058 | let member = self.members.remove(index); |
| 1059 | self.users.insert(member.user.id, member.user); |
| 1060 | } |
| 1061 | } |
| 1062 | |
| 1063 | fn set_rank(&mut self, user_id: Id, rank: Rank) { |
| 1064 | if let Some(member) = self |
| 1065 | .members |
| 1066 | .iter_mut() |
| 1067 | .find(|member| member.user.id == user_id) |
| 1068 | { |
| 1069 | member.rank = rank; |
| 1070 | } |
| 1071 | } |
| 1072 | |
| 1073 | pub fn community(&self, community_id: Id) -> Option<&Community> { |
| 1074 | self.communities |
| 1075 | .iter() |
| 1076 | .chain(self.opened.as_ref()) |
| 1077 | .find(|community| community.id == community_id) |
| 1078 | } |
| 1079 | |
| 1080 | pub fn selected_community(&self) -> Option<&Community> { |
| 1081 | self.community(self.selected_community_id?) |
| 1082 | } |
| 1083 | |
| 1084 | fn member_entry(&self, user_id: Id) -> Option<&Member> { |
| 1085 | self.members.iter().find(|member| member.user.id == user_id) |
| 1086 | } |
| 1087 | |
| 1088 | pub fn member(&self, user_id: Id) -> Option<&User> { |
| 1089 | self.member_entry(user_id).map(|member| &member.user) |
| 1090 | } |
| 1091 | |
| 1092 | pub fn author(&self, user_id: Id) -> Option<&User> { |
| 1093 | self.member(user_id).or_else(|| self.users.get(&user_id)) |
| 1094 | } |
| 1095 | |
| 1096 | fn missing_authors(&self) -> Vec<Id> { |
| 1097 | let mut ids: Vec<Id> = if self.members.is_empty() { |
| 1098 | vec![] |
| 1099 | } else { |
| 1100 | self.messages |
| 1101 | .iter() |
| 1102 | .chain(&self.pins) |
| 1103 | .flat_map(|message| { |
| 1104 | std::iter::once(message.user_id) |
| 1105 | .chain( |
| 1106 | message |
| 1107 | .reply |
| 1108 | .as_ref() |
| 1109 | .and_then(Reply::quote) |
| 1110 | .map(|quote| quote.user_id), |
| 1111 | ) |
| 1112 | .chain(message.poll.iter().flat_map(|poll| { |
| 1113 | poll.options |
| 1114 | .iter() |
| 1115 | .flat_map(|option| option.voter_ids.iter().copied()) |
| 1116 | })) |
| 1117 | }) |
| 1118 | .collect() |
| 1119 | }; |
| 1120 | if let Some(community_id) = self.voice_community() { |
| 1121 | ids.extend_from_slice(self.voice_ids(community_id)); |
| 1122 | } |
| 1123 | let mut missing: Vec<Id> = ids |
| 1124 | .into_iter() |
| 1125 | .filter(|id| self.author(*id).is_none() && !self.user_requests.contains(id)) |
| 1126 | .collect(); |
| 1127 | missing.sort(); |
| 1128 | missing.dedup(); |
| 1129 | missing |
| 1130 | } |
| 1131 | |
| 1132 | pub fn rank_of(&self, user: &User) -> Option<Rank> { |
| 1133 | let rank = self.member_entry(user.id).map(|member| member.rank); |
| 1134 | |
| 1135 | permission::effective_rank(user.role, rank) |
| 1136 | } |
| 1137 | |
| 1138 | pub fn rank_of_id(&self, user_id: Id) -> Option<Rank> { |
| 1139 | self.member(user_id).and_then(|user| self.rank_of(user)) |
| 1140 | } |
| 1141 | |
| 1142 | pub fn can_pin(&self, me: &User) -> bool { |
| 1143 | let direct = self |
| 1144 | .selected_community() |
| 1145 | .is_some_and(|community| community.kind.is_direct()); |
| 1146 | permission::can_pin(self.rank_of(me), direct) |
| 1147 | } |
| 1148 | |
| 1149 | fn update_pin(&mut self, message: &Message) { |
| 1150 | self.pins.retain(|pin| pin.id != message.id); |
| 1151 | if message.pinned { |
| 1152 | self.pins.push(message.clone()); |
| 1153 | self.pins.sort_by_key(|pin| std::cmp::Reverse(pin.id)); |
| 1154 | } |
| 1155 | } |
| 1156 | |
| 1157 | pub fn can_delete(&self, message: &Message, me: &User) -> bool { |
| 1158 | permission::can_delete_message( |
| 1159 | self.rank_of(me), |
| 1160 | self.rank_of_id(message.user_id), |
| 1161 | message.user_id == me.id, |
| 1162 | ) |
| 1163 | } |
| 1164 | |
| 1165 | pub fn update_member(&mut self, user: &User) { |
| 1166 | style::remember(&[user]); |
| 1167 | let peers = self |
| 1168 | .communities |
| 1169 | .iter_mut() |
| 1170 | .filter_map(|community| match &mut community.kind { |
| 1171 | CommunityKind::Direct { peer } => Some(peer), |
| 1172 | CommunityKind::Group | CommunityKind::Notes => None, |
| 1173 | }); |
| 1174 | let known = self |
| 1175 | .friends |
| 1176 | .friends |
| 1177 | .iter_mut() |
| 1178 | .chain(self.friends.incoming.iter_mut()) |
| 1179 | .chain(self.friends.outgoing.iter_mut()) |
| 1180 | .chain( |
| 1181 | self.invitations |
| 1182 | .iter_mut() |
| 1183 | .map(|invitation| &mut invitation.inviter), |
| 1184 | ) |
| 1185 | .chain(self.members.iter_mut().map(|member| &mut member.user)) |
| 1186 | .chain(self.users.values_mut()) |
| 1187 | .chain(peers); |
| 1188 | for existing in known.filter(|existing| existing.id == user.id) { |
| 1189 | *existing = user.clone(); |
| 1190 | } |
| 1191 | } |
| 1192 | |
| 1193 | pub(super) fn insert_community(&mut self, community: Community) { |
| 1194 | self.communities |
| 1195 | .retain(|existing| existing.id != community.id); |
| 1196 | self.communities.push(community); |
| 1197 | self.communities.sort_by_key(|community| community.id); |
| 1198 | } |
| 1199 | |
| 1200 | pub(super) fn remove_community(&mut self, community_id: Id) { |
| 1201 | self.communities |
| 1202 | .retain(|community| community.id != community_id); |
| 1203 | self.unread.remove(&community_id); |
| 1204 | self.voice.remove(&community_id); |
| 1205 | if self.in_voice(community_id) { |
| 1206 | self.set_voice_off(); |
| 1207 | } |
| 1208 | if self.is_selected(community_id) { |
| 1209 | self.select(None); |
| 1210 | } |
| 1211 | } |
| 1212 | |
| 1213 | fn replace_communities(&mut self, communities: Vec<Community>) { |
| 1214 | self.communities = communities; |
| 1215 | |
| 1216 | let selected_present = self |
| 1217 | .communities |
| 1218 | .iter() |
| 1219 | .chain(self.opened.as_ref()) |
| 1220 | .any(|community| self.is_selected(community.id)); |
| 1221 | if !selected_present { |
| 1222 | self.select(None); |
| 1223 | } |
| 1224 | |
| 1225 | if self.voice_community().is_some_and(|id| !self.joined(id)) { |
| 1226 | self.set_voice_off(); |
| 1227 | } |
| 1228 | } |
| 1229 | |
| 1230 | pub fn reset(&mut self) { |
| 1231 | self.communities.clear(); |
| 1232 | self.presences.clear(); |
| 1233 | self.slowed_until = None; |
| 1234 | self.friends = Friends::default(); |
| 1235 | self.invitations.clear(); |
| 1236 | self.found = None; |
| 1237 | self.select(None); |
| 1238 | self.unread.clear(); |
| 1239 | self.muted.clear(); |
| 1240 | self.builder_users.clear(); |
| 1241 | self.builder_tab = BuilderTab::Overview; |
| 1242 | self.builder_overview = None; |
| 1243 | self.builder_logs.clear(); |
| 1244 | self.builder_stats.clear(); |
| 1245 | self.builder_communities.clear(); |
| 1246 | self.builder_reports.clear(); |
| 1247 | self.report_filter = Some(ReportStatus::Open); |
| 1248 | self.open_reports = 0; |
| 1249 | self.report = None; |
| 1250 | self.pending_community_delete = None; |
| 1251 | self.builder_messages.clear(); |
| 1252 | self.builder_usage_users.clear(); |
| 1253 | self.builder_usage_communities.clear(); |
| 1254 | self.builder_attachments.clear(); |
| 1255 | self.pending_attachment_delete = None; |
| 1256 | self.pending_message_delete = None; |
| 1257 | self.pending_builder_purge = None; |
| 1258 | self.transfer = None; |
| 1259 | self.pending_builder_delete = None; |
| 1260 | self.pending_builder_password = None; |
| 1261 | self.builder_sessions = None; |
| 1262 | self.builder_clients.clear(); |
| 1263 | self.builder_clients_task = None; |
| 1264 | self.pending_builder_rename = None; |
| 1265 | self.pending_builder_timeout = None; |
| 1266 | self.pending_builder_ban = None; |
| 1267 | self.builder_ban_scope = BanScope::Account; |
| 1268 | self.builder_bans.clear(); |
| 1269 | self.builder_invites.clear(); |
| 1270 | self.pending_invite_delete = None; |
| 1271 | self.view = View::Chat; |
| 1272 | self.armed = None; |
| 1273 | self.voice.clear(); |
| 1274 | self.server_muted.clear(); |
| 1275 | self.builder_voice_rooms.clear(); |
| 1276 | self.ringing.clear(); |
| 1277 | self.ring_task = None; |
| 1278 | self.ghost = false; |
| 1279 | self.drafts.clear(); |
| 1280 | self.shares.clear(); |
| 1281 | self.typing.clear(); |
| 1282 | self.typing_sent.clear(); |
| 1283 | self.users.clear(); |
| 1284 | self.user_requests.clear(); |
| 1285 | self.user_volume = None; |
| 1286 | self.card_menu = CardMenu::Main; |
| 1287 | self.set_voice_off(); |
| 1288 | } |
| 1289 | |
| 1290 | pub fn voice_community(&self) -> Option<Id> { |
| 1291 | match &self.voice_state { |
| 1292 | VoiceState::Off => None, |
| 1293 | VoiceState::In { community_id, .. } => Some(*community_id), |
| 1294 | } |
| 1295 | } |
| 1296 | |
| 1297 | pub fn voice_room(&self) -> Option<&Community> { |
| 1298 | self.community(self.voice_community()?) |
| 1299 | } |
| 1300 | |
| 1301 | pub fn in_voice(&self, community_id: Id) -> bool { |
| 1302 | self.voice_community() == Some(community_id) |
| 1303 | } |
| 1304 | |
| 1305 | pub fn voice_flags(&self) -> Option<(bool, bool)> { |
| 1306 | match &self.voice_state { |
| 1307 | VoiceState::Off => None, |
| 1308 | VoiceState::In { |
| 1309 | muted, deafened, .. |
| 1310 | } => Some((*muted, *deafened)), |
| 1311 | } |
| 1312 | } |
| 1313 | |
| 1314 | pub fn voice_enabled(&self, community_id: Id) -> bool { |
| 1315 | self.community(community_id) |
| 1316 | .is_some_and(|community| community.voice_enabled) |
| 1317 | } |
| 1318 | |
| 1319 | pub fn share_of(&self, community_id: Id, user_id: Id, kind: Kind) -> Option<&Share> { |
| 1320 | self.shares |
| 1321 | .get(&community_id)? |
| 1322 | .iter() |
| 1323 | .find(|share| share.user_id == user_id && share.kind == kind) |
| 1324 | } |
| 1325 | |
| 1326 | pub fn voice_ids(&self, community_id: Id) -> &[Id] { |
| 1327 | self.voice |
| 1328 | .get(&community_id) |
| 1329 | .map(Vec::as_slice) |
| 1330 | .unwrap_or_default() |
| 1331 | } |
| 1332 | |
| 1333 | pub fn set_voice_off(&mut self) { |
| 1334 | self.voice_state = VoiceState::Off; |
| 1335 | self.waves.clear(); |
| 1336 | self.voice_task = None; |
| 1337 | self.stats.voice.clear(); |
| 1338 | self.stats.traffic.clear(); |
| 1339 | self.stats.share = ShareSample::default(); |
| 1340 | self.share_loss.clear(); |
| 1341 | } |
| 1342 | |
| 1343 | pub fn record_share(&mut self, sample: ShareSample) { |
| 1344 | for (kind, stat) in &sample.sending { |
| 1345 | tracing::debug!( |
| 1346 | target: "metrics", |
| 1347 | ?kind, |
| 1348 | kbps = stat.kbps, |
| 1349 | fps = stat.fps, |
| 1350 | gap50 = stat.gap_ms.0, |
| 1351 | gap95 = stat.gap_ms.1, |
| 1352 | "send" |
| 1353 | ); |
| 1354 | } |
| 1355 | for (_, kind, stat) in &sample.watching { |
| 1356 | tracing::debug!( |
| 1357 | target: "metrics", |
| 1358 | ?kind, |
| 1359 | kbps = stat.kbps, |
| 1360 | fps = stat.fps, |
| 1361 | loss = stat.loss, |
| 1362 | keyframes = stat.keyframes, |
| 1363 | latency = stat.latency_ms, |
| 1364 | buffer = stat.buffer_ms, |
| 1365 | nacks = stat.nacks, |
| 1366 | shown = stat.shown_fps, |
| 1367 | dropped = stat.dropped, |
| 1368 | interval50 = stat.interval_ms.0, |
| 1369 | interval95 = stat.interval_ms.1, |
| 1370 | swing = stat.swing_ms, |
| 1371 | "watch" |
| 1372 | ); |
| 1373 | } |
| 1374 | self.share_loss.retain(|key, _| { |
| 1375 | sample |
| 1376 | .watching |
| 1377 | .iter() |
| 1378 | .any(|(user_id, kind, _)| (*user_id, *kind) == *key) |
| 1379 | }); |
| 1380 | for (user_id, kind, stats) in &sample.watching { |
| 1381 | let history = self.share_loss.entry((*user_id, *kind)).or_default(); |
| 1382 | history.push_back((common::time::now_ms(), stats.loss)); |
| 1383 | while history.len() > SAMPLE_HISTORY { |
| 1384 | history.pop_front(); |
| 1385 | } |
| 1386 | } |
| 1387 | self.stats.share = sample; |
| 1388 | } |
| 1389 | } |
| 1390 | |
| 1391 | fn empty_state(palette: Palette) -> impl IntoElement { |
| 1392 | div() |
| 1393 | .flex_1() |
| 1394 | .flex() |
| 1395 | .items_center() |
| 1396 | .justify_center() |
| 1397 | .gap(px(space::SM)) |
| 1398 | .text_color(palette.muted) |
| 1399 | .child( |
| 1400 | div() |
| 1401 | .px(px(space::XS)) |
| 1402 | .rounded(px(radius::SM)) |
| 1403 | .border_1() |
| 1404 | .border_color(palette.border) |
| 1405 | .text_size(text(text::CAPTION)) |
| 1406 | .child(JUMP_HINT), |
| 1407 | ) |
| 1408 | .child(w().jump.placeholder) |
| 1409 | } |
| 1410 | |
| 1411 | pub(super) fn create_community( |
| 1412 | this: &mut Yumush, |
| 1413 | name: String, |
| 1414 | window: &mut Window, |
| 1415 | cx: &mut Context<Yumush>, |
| 1416 | ) { |
| 1417 | let network = this.network.clone(); |
| 1418 | |
| 1419 | cx.spawn_in(window, async move |this, cx| { |
| 1420 | let Some(community) = ok_or_notify(network.call(CreateCommunity { name }).await, &this, cx) |
| 1421 | else { |
| 1422 | return; |
| 1423 | }; |
| 1424 | |
| 1425 | let _ = this.update_in(cx, |yumush, window, cx| { |
| 1426 | window.close_dialog(cx); |
| 1427 | yumush.enter_community(community, window, cx); |
| 1428 | }); |
| 1429 | }) |
| 1430 | .detach(); |
| 1431 | } |
| 1432 | |
| 1433 | impl Yumush { |
| 1434 | pub(super) fn toggle_stats(&mut self, at: StatsAt) { |
| 1435 | self.chat.stats_at = (self.chat.stats_at != Some(at)).then_some(at); |
| 1436 | self.refresh_usage(); |
| 1437 | } |
| 1438 | |
| 1439 | pub(super) fn refresh_usage(&mut self) { |
| 1440 | if let Some(usage) = self.chat.usage_monitor.latest() { |
| 1441 | self.chat.stats.usage = Some(usage); |
| 1442 | } |
| 1443 | } |
| 1444 | |
| 1445 | pub(super) fn chat_page(&self, cx: &mut Context<Self>) -> impl IntoElement { |
| 1446 | let palette = palette(cx); |
| 1447 | let entity = cx.entity(); |
| 1448 | |
| 1449 | div() |
| 1450 | .flex() |
| 1451 | .flex_row() |
| 1452 | .size_full() |
| 1453 | .when(self.chat.view == View::Chat, |page| { |
| 1454 | page.child(self.sidebar(&entity, palette)) |
| 1455 | }) |
| 1456 | .relative() |
| 1457 | .on_mouse_move({ |
| 1458 | let entity = entity.clone(); |
| 1459 | move |event, window, cx| { |
| 1460 | entity.update(cx, |yumush, cx| yumush.float_moved(event, window, cx)) |
| 1461 | } |
| 1462 | }) |
| 1463 | .child(self.main_panel(&entity, palette, cx)) |
| 1464 | .children(self.floating_viewer(&entity, palette)) |
| 1465 | .children(self.fullscreen_viewer(&entity, palette)) |
| 1466 | } |
| 1467 | |
| 1468 | fn main_panel(&self, entity: &Entity<Self>, palette: Palette, cx: &App) -> impl IntoElement { |
| 1469 | let themed = palette; |
| 1470 | |
| 1471 | div() |
| 1472 | .flex_1() |
| 1473 | .min_w_0() |
| 1474 | .flex() |
| 1475 | .flex_col() |
| 1476 | .relative() |
| 1477 | .bg(palette.window) |
| 1478 | .overflow_hidden() |
| 1479 | .children(self.announcement_banner(entity, palette)) |
| 1480 | .children(self.call_banner(entity, palette)) |
| 1481 | .map(|panel| match self.chat.view { |
| 1482 | View::Settings => panel.child(self.settings_page(entity, palette, cx)), |
| 1483 | View::Builder => panel.child(self.builder_page(entity, palette, cx)), |
| 1484 | View::CommunitySettings => { |
| 1485 | panel.child(self.community_settings_page(entity, palette, cx)) |
| 1486 | } |
| 1487 | View::Chat => match self.chat.selected_community_id { |
| 1488 | Some(_) => { |
| 1489 | let viewer = self.inline_viewer(entity, themed); |
| 1490 | let grid = viewer |
| 1491 | .is_none() |
| 1492 | .then(|| { |
| 1493 | self.stage_grid(entity, themed, cx) |
| 1494 | .map(IntoElement::into_any_element) |
| 1495 | }) |
| 1496 | .flatten(); |
| 1497 | panel |
| 1498 | .child(self.header(entity, themed)) |
| 1499 | .children(self.stage_strip(entity, themed)) |
| 1500 | .children(self.unread_bar(entity, themed)) |
| 1501 | .children(self.poll_banner(entity, themed)) |
| 1502 | .child(self.chat_body(entity, themed, viewer, grid, cx)) |
| 1503 | } |
| 1504 | None => panel.child(empty_state(palette)), |
| 1505 | }, |
| 1506 | }) |
| 1507 | .children(self.stats_sheet(palette)) |
| 1508 | .children(self.dock(entity, palette, cx)) |
| 1509 | } |
| 1510 | |
| 1511 | fn chat_body( |
| 1512 | &self, |
| 1513 | entity: &Entity<Self>, |
| 1514 | palette: Palette, |
| 1515 | viewer: Option<Div>, |
| 1516 | grid: Option<AnyElement>, |
| 1517 | cx: &App, |
| 1518 | ) -> AnyElement { |
| 1519 | let chat = || { |
| 1520 | div() |
| 1521 | .size_full() |
| 1522 | .flex() |
| 1523 | .flex_col() |
| 1524 | .child(self.message_area(entity, palette)) |
| 1525 | .child(self.composer(entity, palette, cx)) |
| 1526 | }; |
| 1527 | let members = members_column(entity, self, palette); |
| 1528 | match (viewer, grid, members) { |
| 1529 | (None, Some(grid), Some(members)) => div() |
| 1530 | .flex_1() |
| 1531 | .min_h_0() |
| 1532 | .child( |
| 1533 | h_resizable("stage_split") |
| 1534 | .child( |
| 1535 | resizable_panel() |
| 1536 | .size_range(px(size::PAGE)..gpui_kit::Pixels::MAX) |
| 1537 | .child(div().size_full().flex().flex_col().child(grid)), |
| 1538 | ) |
| 1539 | .child( |
| 1540 | resizable_panel() |
| 1541 | .size(px(size::MARGIN_CHAT)) |
| 1542 | .size_range(px(size::MARGIN)..px(size::MARGIN * 2.)) |
| 1543 | .fixed() |
| 1544 | .child( |
| 1545 | v_resizable("side_split") |
| 1546 | .child( |
| 1547 | resizable_panel() |
| 1548 | .size_range( |
| 1549 | px(size::ROW * 2.)..gpui_kit::Pixels::MAX, |
| 1550 | ) |
| 1551 | .child(members.size_full()), |
| 1552 | ) |
| 1553 | .child( |
| 1554 | resizable_panel() |
| 1555 | .size_range( |
| 1556 | px(size::THUMB * 2.)..gpui_kit::Pixels::MAX, |
| 1557 | ) |
| 1558 | .child(chat()), |
| 1559 | ), |
| 1560 | ), |
| 1561 | ), |
| 1562 | ) |
| 1563 | .into_any_element(), |
| 1564 | (viewer, grid, members) => div() |
| 1565 | .flex_1() |
| 1566 | .min_h_0() |
| 1567 | .flex() |
| 1568 | .child(div().flex_1().min_w_0().flex().flex_col().map(|panel| { |
| 1569 | match (viewer, grid) { |
| 1570 | (Some(viewer), _) => panel.child(viewer), |
| 1571 | (None, Some(grid)) => panel.child(grid), |
| 1572 | (None, None) => panel.child(chat()), |
| 1573 | } |
| 1574 | })) |
| 1575 | .children(members.map(|members| { |
| 1576 | div() |
| 1577 | .w(px(size::MARGIN)) |
| 1578 | .flex_none() |
| 1579 | .h_full() |
| 1580 | .flex() |
| 1581 | .flex_col() |
| 1582 | .border_l_1() |
| 1583 | .border_color(palette.border) |
| 1584 | .child(members.flex_1().min_h_0()) |
| 1585 | })) |
| 1586 | .into_any_element(), |
| 1587 | } |
| 1588 | } |
| 1589 | |
| 1590 | fn stats_sheet(&self, palette: Palette) -> Option<impl IntoElement> { |
| 1591 | let at = self |
| 1592 | .chat |
| 1593 | .stats_at |
| 1594 | .filter(|_| self.chat.voice_community().is_some())?; |
| 1595 | Some( |
| 1596 | super::parts::surface(palette.tokens) |
| 1597 | .id("stats_sheet") |
| 1598 | .absolute() |
| 1599 | .right(px(space::LG)) |
| 1600 | .map(|sheet| match at { |
| 1601 | StatsAt::Header => sheet.top(px(size::ROW)), |
| 1602 | StatsAt::Dock => sheet.bottom(px(size::ROW + space::MD + space::SM)), |
| 1603 | }) |
| 1604 | .max_w(relative(0.95)) |
| 1605 | .max_h(relative(0.8)) |
| 1606 | .overflow_y_scroll() |
| 1607 | .p(px(match at { |
| 1608 | StatsAt::Header => space::XL, |
| 1609 | StatsAt::Dock => space::LG, |
| 1610 | })) |
| 1611 | .child(voice_stats::stats_panel( |
| 1612 | &self.chat.stats, |
| 1613 | &|id| self.chat.name_of(id), |
| 1614 | match at { |
| 1615 | StatsAt::Header => voice_stats::Detail::Full, |
| 1616 | StatsAt::Dock => voice_stats::Detail::Connection, |
| 1617 | }, |
| 1618 | palette, |
| 1619 | )), |
| 1620 | ) |
| 1621 | } |
| 1622 | |
| 1623 | fn announcement_banner(&self, entity: &Entity<Self>, palette: Palette) -> Option<Div> { |
| 1624 | let text = self.announcement()?; |
| 1625 | |
| 1626 | Some( |
| 1627 | accent_bar(palette) |
| 1628 | .child(super::glyph::line( |
| 1629 | text.to_string().into(), |
| 1630 | super::text(super::style::text::UI), |
| 1631 | )) |
| 1632 | .child( |
| 1633 | clickable( |
| 1634 | "dismiss_announcement", |
| 1635 | palette, |
| 1636 | entity_action(entity, |yumush, _, cx| yumush.dismiss_announcement(cx)), |
| 1637 | ) |
| 1638 | .child("×"), |
| 1639 | ), |
| 1640 | ) |
| 1641 | } |
| 1642 | |
| 1643 | fn header(&self, entity: &Entity<Self>, palette: Palette) -> impl IntoElement { |
| 1644 | let community = self.chat.selected_community(); |
| 1645 | let name = community.map(community_name).unwrap_or_default(); |
| 1646 | let group = community.is_some_and(|community| community.kind.is_group()); |
| 1647 | let voice = self.voice_toggle(entity, palette); |
| 1648 | let peer = community.and_then(|community| match &community.kind { |
| 1649 | CommunityKind::Direct { peer } => Some(self.chat.author(peer.id).unwrap_or(peer)), |
| 1650 | _ => None, |
| 1651 | }); |
| 1652 | let room = community.filter(|_| group); |
| 1653 | let title = super::parts::words( |
| 1654 | name.to_string(), |
| 1655 | super::parts::Text::Title, |
| 1656 | super::parts::Ink::Paper, |
| 1657 | palette.tokens, |
| 1658 | ); |
| 1659 | |
| 1660 | div() |
| 1661 | .h(px(size::ROW + space::MD)) |
| 1662 | .px(px(space::XL)) |
| 1663 | .flex() |
| 1664 | .items_center() |
| 1665 | .gap(px(space::SM)) |
| 1666 | .child(match (peer, room) { |
| 1667 | (Some(peer), _) => super::profile::profile_popover( |
| 1668 | "header_peer", |
| 1669 | Anchor::TopLeft, |
| 1670 | peer, |
| 1671 | entity, |
| 1672 | div() |
| 1673 | .cursor_pointer() |
| 1674 | .flex() |
| 1675 | .items_center() |
| 1676 | .gap(px(space::SM)) |
| 1677 | .child(super::avatar( |
| 1678 | palette, |
| 1679 | peer, |
| 1680 | px(size::AVATAR), |
| 1681 | super::text(super::style::text::CAPTION), |
| 1682 | &self.chat.avatars, |
| 1683 | )) |
| 1684 | .child(title), |
| 1685 | ) |
| 1686 | .into_any_element(), |
| 1687 | (None, Some(room)) => super::community_card::community_popover( |
| 1688 | room, |
| 1689 | entity, |
| 1690 | div() |
| 1691 | .cursor_pointer() |
| 1692 | .flex() |
| 1693 | .items_center() |
| 1694 | .gap(px(space::SM)) |
| 1695 | .child(super::community_icon( |
| 1696 | palette, |
| 1697 | room, |
| 1698 | px(size::AVATAR), |
| 1699 | super::text(super::style::text::CAPTION), |
| 1700 | &self.chat.avatars, |
| 1701 | )) |
| 1702 | .child(title), |
| 1703 | ) |
| 1704 | .into_any_element(), |
| 1705 | _ => div() |
| 1706 | .flex() |
| 1707 | .items_center() |
| 1708 | .gap(px(space::SM)) |
| 1709 | .children(community.map(|community| { |
| 1710 | super::zoomable( |
| 1711 | entity, |
| 1712 | "header_picture", |
| 1713 | super::community_icon( |
| 1714 | palette, |
| 1715 | community, |
| 1716 | px(size::AVATAR), |
| 1717 | super::text(super::style::text::CAPTION), |
| 1718 | &self.chat.avatars, |
| 1719 | ), |
| 1720 | super::Zoom::new( |
| 1721 | super::picture_of(community, &self.chat.avatars), |
| 1722 | !community.kind.is_group(), |
| 1723 | name.to_string(), |
| 1724 | ), |
| 1725 | ) |
| 1726 | })) |
| 1727 | .child(title) |
| 1728 | .into_any_element(), |
| 1729 | }) |
| 1730 | .child(div().flex_1()) |
| 1731 | .children( |
| 1732 | community |
| 1733 | .is_some() |
| 1734 | .then(|| self.stage_controls(entity, palette)) |
| 1735 | .flatten(), |
| 1736 | ) |
| 1737 | .children( |
| 1738 | community |
| 1739 | .is_some_and(|community| self.chat.joined(community.id)) |
| 1740 | .then(|| { |
| 1741 | message_actions::search_popover( |
| 1742 | &self.chat, |
| 1743 | self.settings.clock(), |
| 1744 | entity, |
| 1745 | palette, |
| 1746 | ) |
| 1747 | }), |
| 1748 | ) |
| 1749 | .children( |
| 1750 | (community.is_some() && !self.chat.pins.is_empty()) |
| 1751 | .then(|| message_actions::pins_popover(&self.chat, entity, palette)), |
| 1752 | ) |
| 1753 | .children(self.calling_hint(palette)) |
| 1754 | .children(voice.is_some().then(|| { |
| 1755 | div() |
| 1756 | .w(px(stroke::LINE)) |
| 1757 | .h(px(size::CHIP)) |
| 1758 | .flex_none() |
| 1759 | .bg(palette.tokens.thread()) |
| 1760 | })) |
| 1761 | .children(voice) |
| 1762 | } |
| 1763 | |
| 1764 | fn message_area(&self, entity: &Entity<Self>, palette: Palette) -> impl IntoElement { |
| 1765 | div() |
| 1766 | .flex() |
| 1767 | .flex_col() |
| 1768 | .flex_1() |
| 1769 | .min_h_0() |
| 1770 | .relative() |
| 1771 | .child( |
| 1772 | div() |
| 1773 | .absolute() |
| 1774 | .left(px(space::XL)) |
| 1775 | .top_0() |
| 1776 | .bottom_0() |
| 1777 | .w(px(stroke::LINE)) |
| 1778 | .bg(palette.border), |
| 1779 | ) |
| 1780 | .child(self.message_list(entity, palette)) |
| 1781 | .children(self.jump_bottom(entity, palette)) |
| 1782 | } |
| 1783 | |
| 1784 | fn unread_bar(&self, entity: &Entity<Self>, palette: Palette) -> Option<impl IntoElement> { |
| 1785 | let (anchor, count) = self.chat.unread_anchor?; |
| 1786 | if self.chat.unread_covered() { |
| 1787 | return None; |
| 1788 | } |
| 1789 | Some( |
| 1790 | accent_bar(palette) |
| 1791 | .child(w().chat.new_messages(count)) |
| 1792 | .child( |
| 1793 | div() |
| 1794 | .flex() |
| 1795 | .gap(px(space::SM)) |
| 1796 | .child( |
| 1797 | clickable( |
| 1798 | "jump_unread", |
| 1799 | palette, |
| 1800 | entity_action(entity, move |yumush, window, cx| { |
| 1801 | yumush.go_to_message(anchor, window, cx) |
| 1802 | }), |
| 1803 | ) |
| 1804 | .child("↑"), |
| 1805 | ) |
| 1806 | .child( |
| 1807 | clickable( |
| 1808 | "dismiss_unread", |
| 1809 | palette, |
| 1810 | entity_action(entity, |yumush, _, cx| { |
| 1811 | yumush.chat.unread_anchor = None; |
| 1812 | cx.notify(); |
| 1813 | }), |
| 1814 | ) |
| 1815 | .child("×"), |
| 1816 | ), |
| 1817 | ), |
| 1818 | ) |
| 1819 | } |
| 1820 | |
| 1821 | fn jump_bottom(&self, entity: &Entity<Self>, palette: Palette) -> Option<impl IntoElement> { |
| 1822 | if !self.chat.away() { |
| 1823 | return None; |
| 1824 | } |
| 1825 | let unseen = self.chat.unseen; |
| 1826 | Some( |
| 1827 | clickable( |
| 1828 | "jump_bottom", |
| 1829 | palette, |
| 1830 | entity_action(entity, |yumush, _, cx| { |
| 1831 | yumush.chat.jump_to_bottom(); |
| 1832 | cx.notify(); |
| 1833 | }), |
| 1834 | ) |
| 1835 | .absolute() |
| 1836 | .right(px(space::LG)) |
| 1837 | .bottom(px(space::MD)) |
| 1838 | .size(px(size::CONTROL)) |
| 1839 | .flex() |
| 1840 | .items_center() |
| 1841 | .justify_center() |
| 1842 | .rounded_full() |
| 1843 | .border_1() |
| 1844 | .border_color(palette.border) |
| 1845 | .bg(palette.window) |
| 1846 | .child(Icon::new(IconName::ArrowDown).size(px(size::ICON))) |
| 1847 | .children((unseen > 0).then(|| { |
| 1848 | div() |
| 1849 | .absolute() |
| 1850 | .top(px(-space::SM)) |
| 1851 | .right(px(-space::SM)) |
| 1852 | .children(unread_badge( |
| 1853 | palette, |
| 1854 | false, |
| 1855 | Some(&Unread { |
| 1856 | messages: unseen, |
| 1857 | ..Default::default() |
| 1858 | }), |
| 1859 | )) |
| 1860 | })), |
| 1861 | ) |
| 1862 | } |
| 1863 | |
| 1864 | fn message_list(&self, entity: &Entity<Self>, palette: Palette) -> AnyElement { |
| 1865 | let divider = self |
| 1866 | .chat |
| 1867 | .unread_divider(self.user.as_ref().map(|user| user.id)); |
| 1868 | let entity_scroll = entity.clone(); |
| 1869 | let glide = self |
| 1870 | .chat |
| 1871 | .glide |
| 1872 | .filter(|(_, _, at)| at.elapsed() < GLIDE + GLIDE_SLACK); |
| 1873 | let list = div() |
| 1874 | .id("chat_messages") |
| 1875 | .track_scroll(&self.chat.scroll) |
| 1876 | .flex() |
| 1877 | .flex_col() |
| 1878 | .flex_1() |
| 1879 | .min_w_0() |
| 1880 | .overflow_y_scroll() |
| 1881 | .pl(px(space::XL + space::LG)) |
| 1882 | .pr(px(space::XL)) |
| 1883 | .py(px(space::LG)) |
| 1884 | .gap(px(space::XXS)) |
| 1885 | .on_scroll(move |_, window, _| { |
| 1886 | let entity = entity_scroll.clone(); |
| 1887 | window.on_next_frame(move |_, cx| { |
| 1888 | entity.update(cx, |yumush, cx| { |
| 1889 | yumush.chat.settle_scroll(); |
| 1890 | cx.notify(); |
| 1891 | }) |
| 1892 | }); |
| 1893 | }) |
| 1894 | .child(div().flex_1()) |
| 1895 | .children((self.chat.history == History::More).then(|| older_button(entity, palette))) |
| 1896 | .children( |
| 1897 | self.chat |
| 1898 | .messages |
| 1899 | .iter() |
| 1900 | .enumerate() |
| 1901 | .filter(|(index, message)| { |
| 1902 | message.call.is_none() |
| 1903 | || divider == Some(*index) |
| 1904 | || self |
| 1905 | .chat |
| 1906 | .messages |
| 1907 | .get(index + 1) |
| 1908 | .is_none_or(|next| next.call.is_none()) |
| 1909 | || self |
| 1910 | .chat |
| 1911 | .open_calls |
| 1912 | .contains(&self.chat.call_run_end(*index)) |
| 1913 | }) |
| 1914 | .map(|(index, message)| { |
| 1915 | let calls = self.chat.messages[..=index] |
| 1916 | .iter() |
| 1917 | .rev() |
| 1918 | .take_while(|message| message.call.is_some()) |
| 1919 | .count(); |
| 1920 | let start = index + 1 - calls.max(1); |
| 1921 | let calls = self.chat.messages[start..=index] |
| 1922 | .iter() |
| 1923 | .filter(|message| message.call == Some(CallLog::Started)) |
| 1924 | .count(); |
| 1925 | let first = start == 0 || { |
| 1926 | let previous = &self.chat.messages[start - 1]; |
| 1927 | match message.call { |
| 1928 | Some(_) => previous.call.is_none(), |
| 1929 | None => { |
| 1930 | previous.user_id != message.user_id || previous.call.is_some() |
| 1931 | } |
| 1932 | } |
| 1933 | }; |
| 1934 | let run = match message.call { |
| 1935 | Some(_) |
| 1936 | if self |
| 1937 | .chat |
| 1938 | .open_calls |
| 1939 | .contains(&self.chat.call_run_end(index)) => |
| 1940 | { |
| 1941 | if self.chat.call_run_end(index) == message.id { |
| 1942 | CallRun::Open |
| 1943 | } else { |
| 1944 | CallRun::Single |
| 1945 | } |
| 1946 | } |
| 1947 | _ if calls > 1 => CallRun::Folded(calls), |
| 1948 | _ => CallRun::Single, |
| 1949 | }; |
| 1950 | let first = first |
| 1951 | && !(message.call.is_some() |
| 1952 | && index != start |
| 1953 | && self |
| 1954 | .chat |
| 1955 | .open_calls |
| 1956 | .contains(&self.chat.call_run_end(index))); |
| 1957 | let row = render_message( |
| 1958 | message, |
| 1959 | first, |
| 1960 | start > 0, |
| 1961 | run, |
| 1962 | &self.chat, |
| 1963 | self.user.as_ref(), |
| 1964 | self.settings.clock(), |
| 1965 | entity, |
| 1966 | palette, |
| 1967 | ); |
| 1968 | let row = match glide { |
| 1969 | Some((id, _, _)) if id == message.id => div() |
| 1970 | .child(row) |
| 1971 | .with_animation( |
| 1972 | format!("chat_arrive_{id}"), |
| 1973 | gpui_kit::Animation::new(GLIDE), |
| 1974 | |row, delta| row.opacity(delta), |
| 1975 | ) |
| 1976 | .into_any_element(), |
| 1977 | _ => row, |
| 1978 | }; |
| 1979 | if divider == Some(index) { |
| 1980 | div() |
| 1981 | .flex() |
| 1982 | .flex_col() |
| 1983 | .child(new_divider(palette)) |
| 1984 | .child(row) |
| 1985 | .into_any_element() |
| 1986 | } else { |
| 1987 | row |
| 1988 | } |
| 1989 | }), |
| 1990 | ); |
| 1991 | let Some((id, from, _)) = glide else { |
| 1992 | return list.into_any_element(); |
| 1993 | }; |
| 1994 | let scroll = self.chat.scroll.clone(); |
| 1995 | list.with_animation( |
| 1996 | format!("chat_glide_{id}"), |
| 1997 | gpui_kit::Animation::new(GLIDE).with_easing(gpui_kit::ease_out_quint()), |
| 1998 | move |list, delta| { |
| 1999 | let to = -scroll.max_offset().y; |
| 2000 | let x = scroll.offset().x; |
| 2001 | scroll.set_offset(gpui_kit::point(x, from + (to - from) * delta)); |
| 2002 | list |
| 2003 | }, |
| 2004 | ) |
| 2005 | .into_any_element() |
| 2006 | } |
| 2007 | |
| 2008 | fn composer(&self, entity: &Entity<Self>, palette: Palette, cx: &App) -> AnyElement { |
| 2009 | if let Composer::Select { ids } = &self.chat.composer { |
| 2010 | return div() |
| 2011 | .p(px(space::LG)) |
| 2012 | .child(composer::selection_bar(ids, entity, palette)) |
| 2013 | .into_any_element(); |
| 2014 | } |
| 2015 | |
| 2016 | let input = self.chat.message_input.clone(); |
| 2017 | let pending = media_view::pending_row(&self.chat.media, entity, palette); |
| 2018 | let bar = composer::mode_bar(&self.chat, entity, palette); |
| 2019 | let entity = entity.clone(); |
| 2020 | |
| 2021 | if let VoiceNote::Recording { |
| 2022 | started, levels, .. |
| 2023 | } = &self.chat.media.voice_note |
| 2024 | { |
| 2025 | return div() |
| 2026 | .p(px(space::LG)) |
| 2027 | .children(pending) |
| 2028 | .child(voice_note::recording_row( |
| 2029 | *started, levels, &entity, palette, |
| 2030 | )) |
| 2031 | .into_any_element(); |
| 2032 | } |
| 2033 | |
| 2034 | let ready = self.chat.message_input.read(cx).text().len() > 0 |
| 2035 | || !self.chat.media.pending.is_empty(); |
| 2036 | |
| 2037 | composer::format_actions( |
| 2038 | &self.chat.message_input, |
| 2039 | div() |
| 2040 | .p(px(space::LG)) |
| 2041 | .border_t_1() |
| 2042 | .border_color(palette.border), |
| 2043 | ) |
| 2044 | .children(pending) |
| 2045 | .children(bar) |
| 2046 | .children(self.chat.typing_label().map(|label| { |
| 2047 | div() |
| 2048 | .pl(px(space::XS)) |
| 2049 | .pb(px(space::XS)) |
| 2050 | .text_size(text(text::CAPTION)) |
| 2051 | .text_color(palette.muted) |
| 2052 | .child(label) |
| 2053 | })) |
| 2054 | .child( |
| 2055 | div() |
| 2056 | .min_h(px(size::ROW)) |
| 2057 | .pl(px(space::XS)) |
| 2058 | .pr(px(space::XS)) |
| 2059 | .py(px(space::XS)) |
| 2060 | .flex() |
| 2061 | .items_center() |
| 2062 | .gap(px(space::SM)) |
| 2063 | .child(composer::plus_popover(&self.chat, &entity, palette)) |
| 2064 | .child( |
| 2065 | Textarea::new(&self.chat.message_input) |
| 2066 | .token(super::glyph::token) |
| 2067 | .on_paste({ |
| 2068 | let entity = entity.clone(); |
| 2069 | move |item, window, cx| { |
| 2070 | entity.update(cx, |yumush, cx| yumush.paste_files(item, window, cx)) |
| 2071 | } |
| 2072 | }) |
| 2073 | .appearance(false) |
| 2074 | .text_size(text(text::BODY)) |
| 2075 | .text_color(palette.text) |
| 2076 | .flex_1() |
| 2077 | .min_w_0(), |
| 2078 | ) |
| 2079 | .child(super::glyph::picker_button( |
| 2080 | "composer_emoji", |
| 2081 | &entity, |
| 2082 | palette, |
| 2083 | super::glyph::insert_into(self.chat.message_input.clone()), |
| 2084 | )) |
| 2085 | .when(!ready, |this| { |
| 2086 | this.child(voice_note::mic_button(&entity, palette)) |
| 2087 | }) |
| 2088 | .when(ready, |this| { |
| 2089 | this.child( |
| 2090 | clickable("send_button", palette, move |window, cx| { |
| 2091 | entity |
| 2092 | .update(cx, |yumush, cx| yumush.submit_composer(&input, window, cx)) |
| 2093 | }) |
| 2094 | .tip(w().chat.send) |
| 2095 | .size(px(size::CONTROL_SM)) |
| 2096 | .flex() |
| 2097 | .items_center() |
| 2098 | .justify_center() |
| 2099 | .text_color(palette.text) |
| 2100 | .child(super::parts::small_icon(IconName::ArrowUp)), |
| 2101 | ) |
| 2102 | }), |
| 2103 | ) |
| 2104 | .into_any_element() |
| 2105 | } |
| 2106 | |
| 2107 | pub(super) fn select_community( |
| 2108 | &mut self, |
| 2109 | community_id: Id, |
| 2110 | window: &mut Window, |
| 2111 | cx: &mut Context<Self>, |
| 2112 | ) { |
| 2113 | if self.chat.view == View::Chat && self.chat.is_selected(community_id) { |
| 2114 | return; |
| 2115 | } |
| 2116 | self.show_view(View::Chat, window, cx); |
| 2117 | self.stop_playing(cx); |
| 2118 | self.save_draft(cx); |
| 2119 | self.chat.select(Some(community_id)); |
| 2120 | if let Some(me) = &self.user |
| 2121 | && self.settings.last_chats.get(&me.id) != Some(&community_id) |
| 2122 | { |
| 2123 | self.settings.last_chats.insert(me.id, community_id); |
| 2124 | crate::settings::save(&self.settings); |
| 2125 | } |
| 2126 | self.restore_draft(community_id, window, cx); |
| 2127 | self.chat.focus(window, cx); |
| 2128 | self.load_members(window, cx); |
| 2129 | self.load_pins(window, cx); |
| 2130 | self.load_voice(community_id, cx); |
| 2131 | self.load_shares(community_id, cx); |
| 2132 | self.load_messages(None, window, cx); |
| 2133 | cx.notify(); |
| 2134 | } |
| 2135 | |
| 2136 | pub(super) fn enter_community( |
| 2137 | &mut self, |
| 2138 | community: Community, |
| 2139 | window: &mut Window, |
| 2140 | cx: &mut Context<Self>, |
| 2141 | ) { |
| 2142 | self.chat.friends_open = false; |
| 2143 | let community_id = community.id; |
| 2144 | let listed = self |
| 2145 | .chat |
| 2146 | .communities |
| 2147 | .iter() |
| 2148 | .any(|known| known.id == community_id); |
| 2149 | if community.kind.is_direct() && !listed { |
| 2150 | self.chat.opened = Some(community); |
| 2151 | } else { |
| 2152 | self.chat.insert_community(community); |
| 2153 | } |
| 2154 | self.request_social_avatars(cx); |
| 2155 | self.select_community(community_id, window, cx); |
| 2156 | } |
| 2157 | |
| 2158 | pub(super) fn reload(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 2159 | self.chat.voice.clear(); |
| 2160 | self.chat.shares.clear(); |
| 2161 | self.load_communities(window, cx); |
| 2162 | self.load_social(window, cx); |
| 2163 | |
| 2164 | if let Some(community_id) = self.chat.selected_community_id { |
| 2165 | self.load_members(window, cx); |
| 2166 | self.chat.messages.clear(); |
| 2167 | self.load_messages(None, window, cx); |
| 2168 | self.load_voice(community_id, cx); |
| 2169 | self.load_shares(community_id, cx); |
| 2170 | } |
| 2171 | } |
| 2172 | |
| 2173 | pub(super) fn refresh_communities(&self, cx: &mut Context<Self>) { |
| 2174 | let network = self.network.clone(); |
| 2175 | cx.spawn(async move |this, cx| { |
| 2176 | let Ok(communities) = network.call(ListCommunities).await else { |
| 2177 | return; |
| 2178 | }; |
| 2179 | let _ = this.update(cx, |yumush, cx| { |
| 2180 | let known: HashSet<Id> = yumush.chat.communities.iter().map(|c| c.id).collect(); |
| 2181 | yumush.chat.replace_communities(communities); |
| 2182 | let calls: Vec<Id> = yumush |
| 2183 | .chat |
| 2184 | .communities |
| 2185 | .iter() |
| 2186 | .filter(|community| !known.contains(&community.id)) |
| 2187 | .filter(|community| { |
| 2188 | community.kind.is_direct() |
| 2189 | && !yumush.chat.in_voice(community.id) |
| 2190 | && yumush |
| 2191 | .chat |
| 2192 | .voice_ids(community.id) |
| 2193 | .iter() |
| 2194 | .any(|id| !yumush.is_me(*id)) |
| 2195 | }) |
| 2196 | .map(|community| community.id) |
| 2197 | .collect(); |
| 2198 | yumush.chat.ringing.extend(calls); |
| 2199 | yumush.update_ringing(cx); |
| 2200 | yumush.request_social_avatars(cx); |
| 2201 | cx.notify(); |
| 2202 | }); |
| 2203 | }) |
| 2204 | .detach(); |
| 2205 | } |
| 2206 | |
| 2207 | pub(super) fn load_communities(&self, window: &mut Window, cx: &mut Context<Self>) { |
| 2208 | let network = self.network.clone(); |
| 2209 | |
| 2210 | cx.spawn_in(window, async move |this, cx| { |
| 2211 | let Some(communities) = ok_or_notify(network.call(ListCommunities).await, &this, cx) |
| 2212 | else { |
| 2213 | return; |
| 2214 | }; |
| 2215 | let unreads = network.call(ListUnreads).await.unwrap_or_default(); |
| 2216 | |
| 2217 | let _ = this.update_in(cx, |yumush, window, cx| { |
| 2218 | yumush.chat.unread = unreads |
| 2219 | .into_iter() |
| 2220 | .map(|unread| (unread.community_id, unread)) |
| 2221 | .collect(); |
| 2222 | yumush.chat.replace_communities(communities); |
| 2223 | yumush.request_social_avatars(cx); |
| 2224 | |
| 2225 | if yumush.chat.selected_community_id.is_none() { |
| 2226 | let last = yumush |
| 2227 | .user |
| 2228 | .as_ref() |
| 2229 | .and_then(|me| yumush.settings.last_chats.get(&me.id)) |
| 2230 | .copied(); |
| 2231 | let communities = &yumush.chat.communities; |
| 2232 | let start = last |
| 2233 | .filter(|last| communities.iter().any(|community| community.id == *last)) |
| 2234 | .or_else(|| communities.first().map(|community| community.id)); |
| 2235 | if let Some(community_id) = start { |
| 2236 | yumush.select_community(community_id, window, cx); |
| 2237 | } |
| 2238 | } |
| 2239 | cx.notify(); |
| 2240 | }); |
| 2241 | }) |
| 2242 | .detach(); |
| 2243 | } |
| 2244 | |
| 2245 | fn search_community(&self, window: &mut Window, cx: &mut Context<Self>) { |
| 2246 | let Some(community_id) = self.chat.selected_community_id else { |
| 2247 | return; |
| 2248 | }; |
| 2249 | let text = search_text(&self.chat.community_search, cx); |
| 2250 | if text.is_empty() { |
| 2251 | return; |
| 2252 | } |
| 2253 | let network = self.network.clone(); |
| 2254 | |
| 2255 | cx.spawn_in(window, async move |this, cx| { |
| 2256 | let Some(hits) = ok_or_notify( |
| 2257 | network.call(SearchCommunity { community_id, text }).await, |
| 2258 | &this, |
| 2259 | cx, |
| 2260 | ) else { |
| 2261 | return; |
| 2262 | }; |
| 2263 | |
| 2264 | let _ = this.update(cx, |yumush, cx| { |
| 2265 | if yumush.chat.is_selected(community_id) { |
| 2266 | yumush.chat.community_hits = hits; |
| 2267 | cx.notify(); |
| 2268 | } |
| 2269 | }); |
| 2270 | }) |
| 2271 | .detach(); |
| 2272 | } |
| 2273 | |
| 2274 | fn load_pins(&self, window: &mut Window, cx: &mut Context<Self>) { |
| 2275 | let Some(community_id) = self.chat.selected_community_id else { |
| 2276 | return; |
| 2277 | }; |
| 2278 | let network = self.network.clone(); |
| 2279 | |
| 2280 | cx.spawn_in(window, async move |this, cx| { |
| 2281 | let Some(pins) = ok_or_notify(network.call(ListPins { community_id }).await, &this, cx) |
| 2282 | else { |
| 2283 | return; |
| 2284 | }; |
| 2285 | |
| 2286 | let _ = this.update(cx, |yumush, cx| { |
| 2287 | if yumush.chat.is_selected(community_id) { |
| 2288 | yumush.chat.pins = pins; |
| 2289 | yumush.request_poll_votes(cx); |
| 2290 | cx.notify(); |
| 2291 | } |
| 2292 | }); |
| 2293 | }) |
| 2294 | .detach(); |
| 2295 | } |
| 2296 | |
| 2297 | fn load_members(&self, window: &mut Window, cx: &mut Context<Self>) { |
| 2298 | let Some(community_id) = self.chat.selected_community_id else { |
| 2299 | return; |
| 2300 | }; |
| 2301 | let network = self.network.clone(); |
| 2302 | |
| 2303 | cx.spawn_in(window, async move |this, cx| { |
| 2304 | let Some(members) = |
| 2305 | ok_or_notify(network.call(ListMembers { community_id }).await, &this, cx) |
| 2306 | else { |
| 2307 | return; |
| 2308 | }; |
| 2309 | |
| 2310 | let _ = this.update_in(cx, |yumush, window, cx| { |
| 2311 | if yumush.chat.is_selected(community_id) { |
| 2312 | for member in &members { |
| 2313 | yumush.request_user_avatar(&member.user, cx); |
| 2314 | yumush |
| 2315 | .chat |
| 2316 | .users |
| 2317 | .insert(member.user.id, member.user.clone()); |
| 2318 | } |
| 2319 | style::remember( |
| 2320 | &members |
| 2321 | .iter() |
| 2322 | .map(|member| &member.user) |
| 2323 | .collect::<Vec<_>>(), |
| 2324 | ); |
| 2325 | let old = std::mem::replace(&mut yumush.chat.members, members); |
| 2326 | yumush |
| 2327 | .chat |
| 2328 | .users |
| 2329 | .extend(old.into_iter().map(|member| (member.user.id, member.user))); |
| 2330 | yumush.load_bans(window, cx); |
| 2331 | yumush.request_authors(cx); |
| 2332 | cx.notify(); |
| 2333 | } |
| 2334 | }); |
| 2335 | }) |
| 2336 | .detach(); |
| 2337 | } |
| 2338 | |
| 2339 | pub(super) fn request_authors(&mut self, cx: &mut Context<Self>) { |
| 2340 | let missing = self.chat.missing_authors(); |
| 2341 | if missing.is_empty() { |
| 2342 | return; |
| 2343 | } |
| 2344 | self.chat.user_requests.extend(missing.iter().copied()); |
| 2345 | let network = self.network.clone(); |
| 2346 | |
| 2347 | cx.spawn(async move |this, cx| { |
| 2348 | for user_id in missing { |
| 2349 | let Ok(user) = network.call(GetUser { user_id }).await else { |
| 2350 | continue; |
| 2351 | }; |
| 2352 | let _ = this.update(cx, |yumush, cx| { |
| 2353 | yumush.request_user_avatar(&user, cx); |
| 2354 | style::remember(&[&user]); |
| 2355 | yumush.chat.users.insert(user.id, user); |
| 2356 | cx.notify(); |
| 2357 | }); |
| 2358 | } |
| 2359 | }) |
| 2360 | .detach(); |
| 2361 | } |
| 2362 | |
| 2363 | pub(super) fn load_bans(&self, window: &mut Window, cx: &mut Context<Self>) { |
| 2364 | if !permission::can_unban(self.actor_rank()) { |
| 2365 | return; |
| 2366 | } |
| 2367 | let Some(community_id) = self.chat.selected_community_id else { |
| 2368 | return; |
| 2369 | }; |
| 2370 | let network = self.network.clone(); |
| 2371 | |
| 2372 | cx.spawn_in(window, async move |this, cx| { |
| 2373 | let Some(bans) = ok_or_notify(network.call(ListBans { community_id }).await, &this, cx) |
| 2374 | else { |
| 2375 | return; |
| 2376 | }; |
| 2377 | |
| 2378 | let _ = this.update(cx, |yumush, cx| { |
| 2379 | if yumush.chat.is_selected(community_id) { |
| 2380 | for ban in &bans { |
| 2381 | yumush.request_user_avatar(&ban.user, cx); |
| 2382 | } |
| 2383 | yumush.chat.bans = bans; |
| 2384 | cx.notify(); |
| 2385 | } |
| 2386 | }); |
| 2387 | }) |
| 2388 | .detach(); |
| 2389 | } |
| 2390 | |
| 2391 | pub(super) fn go_to_message( |
| 2392 | &mut self, |
| 2393 | message_id: Id, |
| 2394 | window: &mut Window, |
| 2395 | cx: &mut Context<Self>, |
| 2396 | ) { |
| 2397 | self.chat.jump = Some(message_id); |
| 2398 | self.chat.reveal_jump(); |
| 2399 | self.continue_jump(window, cx); |
| 2400 | cx.notify(); |
| 2401 | } |
| 2402 | |
| 2403 | fn continue_jump(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 2404 | if self.chat.jump.is_none() { |
| 2405 | Self::fade_highlight(cx); |
| 2406 | } else if self.chat.history == History::More { |
| 2407 | self.load_older_messages(window, cx); |
| 2408 | } else { |
| 2409 | self.chat.jump = None; |
| 2410 | } |
| 2411 | } |
| 2412 | |
| 2413 | fn fade_highlight(cx: &mut Context<Self>) { |
| 2414 | cx.spawn(async move |this, cx| { |
| 2415 | cx.background_executor().timer(HIGHLIGHT_TTL).await; |
| 2416 | let _ = this.update(cx, |_, cx| cx.notify()); |
| 2417 | }) |
| 2418 | .detach(); |
| 2419 | } |
| 2420 | |
| 2421 | fn load_older_messages(&self, window: &mut Window, cx: &mut Context<Self>) { |
| 2422 | let Some(oldest_id) = self.chat.messages.first().map(|message| message.id) else { |
| 2423 | return; |
| 2424 | }; |
| 2425 | self.load_messages(Some(oldest_id), window, cx); |
| 2426 | } |
| 2427 | |
| 2428 | fn load_messages(&self, before: Option<Id>, window: &mut Window, cx: &mut Context<Self>) { |
| 2429 | let Some(community_id) = self.chat.selected_community_id else { |
| 2430 | return; |
| 2431 | }; |
| 2432 | let network = self.network.clone(); |
| 2433 | let latest = before.is_none() && window.is_window_active(); |
| 2434 | |
| 2435 | cx.spawn_in(window, async move |this, cx| { |
| 2436 | if before.is_none() { |
| 2437 | let _ = network |
| 2438 | .call(WatchCommunity { |
| 2439 | community_id: Some(community_id), |
| 2440 | }) |
| 2441 | .await; |
| 2442 | } |
| 2443 | let Some(page) = ok_or_notify( |
| 2444 | network |
| 2445 | .call(ListMessages { |
| 2446 | community_id, |
| 2447 | before, |
| 2448 | }) |
| 2449 | .await, |
| 2450 | &this, |
| 2451 | cx, |
| 2452 | ) else { |
| 2453 | return; |
| 2454 | }; |
| 2455 | |
| 2456 | let _ = this.update_in(cx, |yumush, window, cx| { |
| 2457 | if yumush.chat.is_selected(community_id) { |
| 2458 | let newest = page.last().map(|message| message.id); |
| 2459 | let seeking = yumush.chat.jump.is_some(); |
| 2460 | yumush |
| 2461 | .chat |
| 2462 | .add_page(page, yumush.user.as_ref().map(|user| user.id)); |
| 2463 | if seeking { |
| 2464 | yumush.continue_jump(window, cx); |
| 2465 | } |
| 2466 | if let Some(message_id) = newest.filter(|_| latest) { |
| 2467 | yumush.mark_read(community_id, message_id, cx); |
| 2468 | } |
| 2469 | yumush.request_media(cx); |
| 2470 | yumush.request_poll_votes(cx); |
| 2471 | yumush.request_authors(cx); |
| 2472 | cx.notify(); |
| 2473 | } |
| 2474 | }); |
| 2475 | }) |
| 2476 | .detach(); |
| 2477 | } |
| 2478 | |
| 2479 | pub(super) fn request_message_delete( |
| 2480 | &mut self, |
| 2481 | message_id: Id, |
| 2482 | window: &mut Window, |
| 2483 | cx: &mut Context<Self>, |
| 2484 | ) { |
| 2485 | if self.chat.pending_message_delete.as_ref() == Some(&message_id) { |
| 2486 | self.chat.pending_message_delete = None; |
| 2487 | self.delete_message(message_id, window, cx); |
| 2488 | } else { |
| 2489 | self.chat.pending_message_delete = Some(message_id); |
| 2490 | cx.spawn(async move |this, cx| { |
| 2491 | cx.background_executor().timer(DELETE_CONFIRM).await; |
| 2492 | let _ = this.update(cx, |yumush, cx| { |
| 2493 | if yumush.chat.pending_message_delete.as_ref() == Some(&message_id) { |
| 2494 | yumush.chat.pending_message_delete = None; |
| 2495 | cx.notify(); |
| 2496 | } |
| 2497 | }); |
| 2498 | }) |
| 2499 | .detach(); |
| 2500 | } |
| 2501 | cx.notify(); |
| 2502 | } |
| 2503 | |
| 2504 | pub(super) fn delete_message( |
| 2505 | &self, |
| 2506 | message_id: Id, |
| 2507 | window: &mut Window, |
| 2508 | cx: &mut Context<Self>, |
| 2509 | ) { |
| 2510 | let network = self.network.clone(); |
| 2511 | spawn_done(window, cx, async move { |
| 2512 | network.call(DeleteMessage { message_id }).await |
| 2513 | }); |
| 2514 | } |
| 2515 | |
| 2516 | pub(super) fn load_edits(&self, message_id: Id, cx: &mut Context<Self>) { |
| 2517 | let network = self.network.clone(); |
| 2518 | cx.spawn(async move |this, cx| { |
| 2519 | let edits = match network.call(ListEdits { message_id }).await { |
| 2520 | Ok(edits) => edits, |
| 2521 | Err(error) => { |
| 2522 | tracing::warn!("list edits failed: {error}"); |
| 2523 | return; |
| 2524 | } |
| 2525 | }; |
| 2526 | let _ = this.update(cx, |yumush, cx| { |
| 2527 | yumush.chat.edits.insert(message_id, edits); |
| 2528 | cx.notify(); |
| 2529 | }); |
| 2530 | }) |
| 2531 | .detach(); |
| 2532 | } |
| 2533 | |
| 2534 | pub(super) fn leave_community( |
| 2535 | &self, |
| 2536 | community_id: Id, |
| 2537 | window: &mut Window, |
| 2538 | cx: &mut Context<Self>, |
| 2539 | ) { |
| 2540 | let network = self.network.clone(); |
| 2541 | |
| 2542 | cx.spawn_in(window, async move |this, cx| { |
| 2543 | if let Err(error) = network.call(LeaveCommunity { community_id }).await { |
| 2544 | notify_error(&this, cx, error); |
| 2545 | return; |
| 2546 | } |
| 2547 | |
| 2548 | let _ = this.update(cx, |yumush, cx| { |
| 2549 | yumush.leave_voice_in(community_id, cx); |
| 2550 | yumush.chat.remove_community(community_id); |
| 2551 | cx.notify(); |
| 2552 | }); |
| 2553 | }) |
| 2554 | .detach(); |
| 2555 | } |
| 2556 | |
| 2557 | fn load_voice(&self, community_id: Id, cx: &mut Context<Self>) { |
| 2558 | let network = self.network.clone(); |
| 2559 | |
| 2560 | cx.spawn(async move |this, cx| { |
| 2561 | let Ok(participants) = network.call(ListVoiceParticipants { community_id }).await |
| 2562 | else { |
| 2563 | return; |
| 2564 | }; |
| 2565 | |
| 2566 | let _ = this.update(cx, |yumush, cx| { |
| 2567 | yumush.chat.voice.insert(community_id, participants); |
| 2568 | yumush.request_authors(cx); |
| 2569 | cx.notify(); |
| 2570 | }); |
| 2571 | }) |
| 2572 | .detach(); |
| 2573 | } |
| 2574 | |
| 2575 | pub(super) fn toggle_voice( |
| 2576 | &mut self, |
| 2577 | community_id: Id, |
| 2578 | window: &mut Window, |
| 2579 | cx: &mut Context<Self>, |
| 2580 | ) { |
| 2581 | if self.chat.in_voice(community_id) { |
| 2582 | self.end_voice(cx); |
| 2583 | return; |
| 2584 | } |
| 2585 | |
| 2586 | let (muted, deafened) = self.chat.voice_flags().unwrap_or((false, false)); |
| 2587 | let was_in = self.chat.voice_community().is_some(); |
| 2588 | if was_in { |
| 2589 | self.chat.set_voice_off(); |
| 2590 | } |
| 2591 | self.join_voice(community_id, (muted, deafened), was_in, window, cx); |
| 2592 | } |
| 2593 | |
| 2594 | fn join_voice( |
| 2595 | &mut self, |
| 2596 | community_id: Id, |
| 2597 | (muted, deafened): (bool, bool), |
| 2598 | was_in: bool, |
| 2599 | window: &mut Window, |
| 2600 | cx: &mut Context<Self>, |
| 2601 | ) { |
| 2602 | let network = self.network.clone(); |
| 2603 | let voice_settings = self.settings.voice.clone(); |
| 2604 | |
| 2605 | cx.spawn_in(window, async move |this, cx| { |
| 2606 | if was_in { |
| 2607 | let _ = network.leave_voice().await; |
| 2608 | } |
| 2609 | |
| 2610 | match network.join_voice(community_id, &voice_settings).await { |
| 2611 | Ok(participants) => { |
| 2612 | network.set_deafened(deafened); |
| 2613 | let _ = this.update_in(cx, |yumush, _, cx| { |
| 2614 | let muted = |
| 2615 | muted || yumush.is_server_muted() || yumush.hidden_in(community_id); |
| 2616 | yumush.network.set_muted(muted); |
| 2617 | yumush.chat.voice.insert(community_id, participants); |
| 2618 | yumush.request_authors(cx); |
| 2619 | yumush.load_shares(community_id, cx); |
| 2620 | yumush.chat.voice_state = VoiceState::In { |
| 2621 | community_id, |
| 2622 | muted, |
| 2623 | deafened, |
| 2624 | since: Instant::now(), |
| 2625 | }; |
| 2626 | yumush.start_voice_poll(cx); |
| 2627 | cx.notify(); |
| 2628 | }); |
| 2629 | } |
| 2630 | Err(error) => { |
| 2631 | let _ = this.update(cx, |yumush, cx| { |
| 2632 | yumush.chat.set_voice_off(); |
| 2633 | cx.notify(); |
| 2634 | }); |
| 2635 | notify_error(&this, cx, error); |
| 2636 | } |
| 2637 | } |
| 2638 | }) |
| 2639 | .detach(); |
| 2640 | } |
| 2641 | |
| 2642 | pub(super) fn end_voice(&mut self, cx: &mut Context<Self>) { |
| 2643 | let Some(community_id) = self.chat.voice_community() else { |
| 2644 | return; |
| 2645 | }; |
| 2646 | self.chat.set_voice_off(); |
| 2647 | let network = self.network.clone(); |
| 2648 | |
| 2649 | cx.spawn(async move |this, cx| { |
| 2650 | let _ = network.leave_voice().await; |
| 2651 | let _ = this.update(cx, |yumush, cx| { |
| 2652 | yumush.load_voice(community_id, cx); |
| 2653 | yumush.load_shares(community_id, cx); |
| 2654 | }); |
| 2655 | }) |
| 2656 | .detach(); |
| 2657 | } |
| 2658 | |
| 2659 | pub(super) fn refresh_voice(&mut self, cx: &mut Context<Self>) { |
| 2660 | let Some(community_id) = self.chat.voice_community() else { |
| 2661 | return; |
| 2662 | }; |
| 2663 | self.load_voice(community_id, cx); |
| 2664 | self.load_shares(community_id, cx); |
| 2665 | } |
| 2666 | |
| 2667 | pub(super) fn leave_voice_in(&mut self, community_id: Id, cx: &mut Context<Self>) { |
| 2668 | if self.chat.in_voice(community_id) { |
| 2669 | self.end_voice(cx); |
| 2670 | } |
| 2671 | } |
| 2672 | |
| 2673 | pub(super) fn is_server_muted(&self) -> bool { |
| 2674 | self.user |
| 2675 | .as_ref() |
| 2676 | .is_some_and(|me| self.chat.server_muted.contains(&me.id)) |
| 2677 | } |
| 2678 | |
| 2679 | fn hidden_in(&self, community_id: Id) -> bool { |
| 2680 | self.chat.ghost || !self.chat.joined(community_id) |
| 2681 | } |
| 2682 | |
| 2683 | pub(super) fn hidden_in_voice(&self) -> bool { |
| 2684 | self.chat |
| 2685 | .voice_community() |
| 2686 | .is_some_and(|community_id| self.hidden_in(community_id)) |
| 2687 | } |
| 2688 | |
| 2689 | pub(super) fn toggle_mute(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 2690 | if self.is_server_muted() { |
| 2691 | return; |
| 2692 | } |
| 2693 | let Some((muted, _)) = self.chat.voice_flags() else { |
| 2694 | return; |
| 2695 | }; |
| 2696 | if muted && self.hidden_in_voice() { |
| 2697 | self.confirm_ghost_speak(window, cx); |
| 2698 | return; |
| 2699 | } |
| 2700 | self.set_muted(!muted, cx); |
| 2701 | } |
| 2702 | |
| 2703 | pub(super) fn set_muted(&mut self, value: bool, cx: &mut Context<Self>) { |
| 2704 | self.set_call_muted(value); |
| 2705 | cx.notify(); |
| 2706 | } |
| 2707 | |
| 2708 | pub(super) fn set_call_muted(&mut self, value: bool) { |
| 2709 | if let VoiceState::In { muted, .. } = &mut self.chat.voice_state { |
| 2710 | *muted = value; |
| 2711 | self.network.set_muted(value); |
| 2712 | } |
| 2713 | } |
| 2714 | |
| 2715 | fn confirm_ghost_speak(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 2716 | let entity = cx.entity(); |
| 2717 | window.open_dialog(cx, move |dialog, _, cx| { |
| 2718 | let palette = super::palette(cx); |
| 2719 | let speak = entity.clone(); |
| 2720 | |
| 2721 | dialog.title(w().chat.ghost_title).child( |
| 2722 | div() |
| 2723 | .flex() |
| 2724 | .flex_col() |
| 2725 | .gap(px(space::LG)) |
| 2726 | .child( |
| 2727 | div() |
| 2728 | .text_size(text(text::LABEL)) |
| 2729 | .text_color(palette.muted) |
| 2730 | .child(w().chat.ghost_hint), |
| 2731 | ) |
| 2732 | .child( |
| 2733 | div() |
| 2734 | .flex() |
| 2735 | .justify_end() |
| 2736 | .gap(px(space::SM)) |
| 2737 | .child(parts::button( |
| 2738 | "ghost_speak_cancel", |
| 2739 | w().chat.cancel, |
| 2740 | parts::Press::Plain, |
| 2741 | palette.tokens, |
| 2742 | |window, cx| window.close_dialog(cx), |
| 2743 | )) |
| 2744 | .child(parts::button( |
| 2745 | "ghost_speak_confirm", |
| 2746 | w().chat.speak, |
| 2747 | parts::Press::Primary, |
| 2748 | palette.tokens, |
| 2749 | move |window, cx| { |
| 2750 | window.close_dialog(cx); |
| 2751 | speak.update(cx, |yumush, cx| yumush.set_muted(false, cx)); |
| 2752 | }, |
| 2753 | )), |
| 2754 | ), |
| 2755 | ) |
| 2756 | }); |
| 2757 | } |
| 2758 | |
| 2759 | pub(super) fn toggle_deafen(&mut self, cx: &mut Context<Self>) { |
| 2760 | if let VoiceState::In { deafened, .. } = &mut self.chat.voice_state { |
| 2761 | *deafened = !*deafened; |
| 2762 | self.network.set_deafened(*deafened); |
| 2763 | } |
| 2764 | cx.notify(); |
| 2765 | } |
| 2766 | |
| 2767 | pub(super) fn start_stats_push(&mut self, cx: &mut Context<Self>) { |
| 2768 | let network = self.network.clone(); |
| 2769 | self.chat.stats_task = Some(cx.spawn(async move |this, cx| { |
| 2770 | loop { |
| 2771 | let Ok(stats) = this.update(cx, |yumush, _| { |
| 2772 | yumush.refresh_usage(); |
| 2773 | yumush.chat.stats.clone() |
| 2774 | }) else { |
| 2775 | break; |
| 2776 | }; |
| 2777 | let _ = network.call(PushStats { stats }).await; |
| 2778 | cx.background_executor().timer(VOICE_SAMPLE_INTERVAL).await; |
| 2779 | } |
| 2780 | })); |
| 2781 | } |
| 2782 | |
| 2783 | fn start_voice_poll(&mut self, cx: &mut Context<Self>) { |
| 2784 | let network = self.network.clone(); |
| 2785 | |
| 2786 | self.chat.voice_task = Some(cx.spawn(async move |this, cx| { |
| 2787 | let mut sampled = Instant::now(); |
| 2788 | loop { |
| 2789 | cx.background_executor().timer(WAVE_INTERVAL).await; |
| 2790 | let levels = network.levels(); |
| 2791 | let due = sampled.elapsed() >= VOICE_SAMPLE_INTERVAL; |
| 2792 | if due { |
| 2793 | sampled = Instant::now(); |
| 2794 | } |
| 2795 | let sample = due.then(|| network.voice_sample()).flatten(); |
| 2796 | let share = due.then(|| network.share_sample()); |
| 2797 | let polling = this.update(cx, |yumush, cx| { |
| 2798 | if yumush.chat.voice_community().is_none() { |
| 2799 | return false; |
| 2800 | } |
| 2801 | if let (Some(sample), Some(share)) = (&sample, &share) { |
| 2802 | let up = sample.traffic.up_kbps |
| 2803 | + share |
| 2804 | .sending |
| 2805 | .iter() |
| 2806 | .map(|(_, layer)| layer.kbps as f32) |
| 2807 | .sum::<f32>(); |
| 2808 | let down = sample.traffic.down_kbps |
| 2809 | + share |
| 2810 | .watching |
| 2811 | .iter() |
| 2812 | .map(|(_, _, stats)| stats.kbps as f32) |
| 2813 | .sum::<f32>(); |
| 2814 | let traffic = &mut yumush.chat.stats.traffic; |
| 2815 | traffic.push_back((sample.at_ms, up, down)); |
| 2816 | while traffic.len() > SAMPLE_HISTORY { |
| 2817 | traffic.pop_front(); |
| 2818 | } |
| 2819 | } |
| 2820 | if sample.is_some() { |
| 2821 | yumush.refresh_usage(); |
| 2822 | } |
| 2823 | if let Some(sample) = sample { |
| 2824 | let samples = &mut yumush.chat.stats.voice; |
| 2825 | tracing::debug!( |
| 2826 | target: "metrics", |
| 2827 | up = sample.traffic.up_kbps, |
| 2828 | down = sample.traffic.down_kbps, |
| 2829 | loss = sample.traffic.loss, |
| 2830 | buffer = sample.traffic.buffer_ms, |
| 2831 | "voice" |
| 2832 | ); |
| 2833 | samples.push_back(sample); |
| 2834 | while samples.len() > SAMPLE_HISTORY { |
| 2835 | samples.pop_front(); |
| 2836 | } |
| 2837 | cx.notify(); |
| 2838 | } |
| 2839 | if let Some(share) = share { |
| 2840 | yumush.chat.record_share(share); |
| 2841 | cx.notify(); |
| 2842 | } |
| 2843 | if due && yumush.chat.stats_at.is_some() { |
| 2844 | yumush.refresh_usage(); |
| 2845 | } |
| 2846 | yumush.update_waves(levels, cx); |
| 2847 | true |
| 2848 | }); |
| 2849 | if !matches!(polling, Ok(true)) { |
| 2850 | break; |
| 2851 | } |
| 2852 | } |
| 2853 | })); |
| 2854 | } |
| 2855 | |
| 2856 | pub(super) fn open_user_volume(&mut self, user_id: Id, cx: &mut Context<Self>) { |
| 2857 | let in_call = self |
| 2858 | .chat |
| 2859 | .voice_community() |
| 2860 | .is_some_and(|community_id| self.chat.voice_ids(community_id).contains(&user_id)); |
| 2861 | if !in_call || self.is_me(user_id) { |
| 2862 | self.chat.user_volume = None; |
| 2863 | return; |
| 2864 | } |
| 2865 | let volume = self.user_volume(user_id); |
| 2866 | let slider = cx.new(|_| { |
| 2867 | SliderState::new() |
| 2868 | .min(*VOLUME.start()) |
| 2869 | .max(*VOLUME.end()) |
| 2870 | .default_value(volume) |
| 2871 | }); |
| 2872 | let subscription = cx.subscribe(&slider, move |this, _, event: &SliderEvent, cx| { |
| 2873 | let SliderEvent::Change(value) = event else { |
| 2874 | return; |
| 2875 | }; |
| 2876 | let volumes = &mut this.settings.voice.user_volumes; |
| 2877 | match value.start() { |
| 2878 | volume if volume == DEFAULT_USER_VOLUME => volumes.remove(&user_id), |
| 2879 | volume => volumes.insert(user_id, volume), |
| 2880 | }; |
| 2881 | this.commit_settings(cx); |
| 2882 | }); |
| 2883 | self.chat.user_volume = Some(UserVolume { |
| 2884 | user_id, |
| 2885 | slider, |
| 2886 | _subscription: subscription, |
| 2887 | }); |
| 2888 | } |
| 2889 | |
| 2890 | pub(super) fn toggle_user_mute( |
| 2891 | &mut self, |
| 2892 | user_id: Id, |
| 2893 | window: &mut Window, |
| 2894 | cx: &mut Context<Self>, |
| 2895 | ) { |
| 2896 | let volume = if self.user_volume(user_id) == 0. { |
| 2897 | DEFAULT_USER_VOLUME |
| 2898 | } else { |
| 2899 | 0. |
| 2900 | }; |
| 2901 | let volumes = &mut self.settings.voice.user_volumes; |
| 2902 | match volume { |
| 2903 | DEFAULT_USER_VOLUME => volumes.remove(&user_id), |
| 2904 | volume => volumes.insert(user_id, volume), |
| 2905 | }; |
| 2906 | if let Some(user_volume) = self.chat.user_volume.as_ref() { |
| 2907 | user_volume |
| 2908 | .slider |
| 2909 | .update(cx, |slider, cx| slider.set_value(volume, window, cx)); |
| 2910 | } |
| 2911 | self.commit_settings(cx); |
| 2912 | } |
| 2913 | |
| 2914 | pub(super) fn user_volume(&self, user_id: Id) -> f32 { |
| 2915 | self.settings |
| 2916 | .voice |
| 2917 | .user_volumes |
| 2918 | .get(&user_id) |
| 2919 | .copied() |
| 2920 | .unwrap_or(DEFAULT_USER_VOLUME) |
| 2921 | } |
| 2922 | |
| 2923 | fn update_waves(&mut self, levels: Levels, cx: &mut Context<Self>) { |
| 2924 | let deafened = self |
| 2925 | .chat |
| 2926 | .voice_flags() |
| 2927 | .is_some_and(|(_, deafened)| deafened); |
| 2928 | let me = self.user.as_ref().map(|user| user.id); |
| 2929 | if self.chat.waves.update(levels, me, deafened) { |
| 2930 | cx.notify(); |
| 2931 | } |
| 2932 | } |
| 2933 | |
| 2934 | fn voice_toggle(&self, entity: &Entity<Self>, palette: Palette) -> Option<impl IntoElement> { |
| 2935 | let community_id = self.chat.selected_community_id?; |
| 2936 | let direct = self.is_direct(community_id); |
| 2937 | let in_voice = self.chat.in_voice(community_id); |
| 2938 | if !direct || !(in_voice || self.can_join_voice(community_id)) { |
| 2939 | return None; |
| 2940 | } |
| 2941 | |
| 2942 | let t = palette.tokens; |
| 2943 | let users = self.voice_users(community_id); |
| 2944 | let (icon, label) = match (direct, in_voice) { |
| 2945 | (true, false) => (IconName::Phone, w().chat.call), |
| 2946 | (true, true) => (IconName::PhoneOff, w().chat.hang_up), |
| 2947 | (false, false) => (IconName::Headphones, w().chat.join), |
| 2948 | (false, true) => (IconName::Headphones, w().chat.leave), |
| 2949 | }; |
| 2950 | let ink = if in_voice { |
| 2951 | super::parts::Ink::Bad |
| 2952 | } else if users.is_empty() { |
| 2953 | super::parts::Ink::Dim |
| 2954 | } else { |
| 2955 | super::parts::Ink::Paper |
| 2956 | }; |
| 2957 | let entity = entity.clone(); |
| 2958 | let on_click = move |window: &mut Window, cx: &mut App| { |
| 2959 | entity.update(cx, |yumush, cx| { |
| 2960 | yumush.toggle_voice(community_id, window, cx) |
| 2961 | }) |
| 2962 | }; |
| 2963 | let hov = t.hov(); |
| 2964 | |
| 2965 | Some( |
| 2966 | div() |
| 2967 | .id(format!("voice_toggle_{direct}_{in_voice}")) |
| 2968 | .h(px(size::CONTROL_SM)) |
| 2969 | .px(px(space::MD)) |
| 2970 | .flex() |
| 2971 | .items_center() |
| 2972 | .gap(px(space::SM)) |
| 2973 | .rounded(px(radius::MD)) |
| 2974 | .cursor_pointer() |
| 2975 | .hover(move |style| style.bg(hov)) |
| 2976 | .text_color(ink.color(t)) |
| 2977 | .on_click(move |_, window, cx| on_click(window, cx)) |
| 2978 | .child(super::parts::small_icon(icon)) |
| 2979 | .child(super::parts::words(label, super::parts::Text::Ui, ink, t)), |
| 2980 | ) |
| 2981 | } |
| 2982 | |
| 2983 | fn can_join_voice(&self, community_id: Id) -> bool { |
| 2984 | permission::can_join_voice(self.actor_rank()) |
| 2985 | && (self.is_builder() || self.chat.voice_enabled(community_id)) |
| 2986 | && self |
| 2987 | .chat |
| 2988 | .notes() |
| 2989 | .is_none_or(|notes| notes.id != community_id) |
| 2990 | } |
| 2991 | |
| 2992 | fn voice_users(&self, community_id: Id) -> Vec<&User> { |
| 2993 | self.chat |
| 2994 | .voice_ids(community_id) |
| 2995 | .iter() |
| 2996 | .filter(|id| !(self.chat.ghost && self.is_me(**id))) |
| 2997 | .filter_map(|id| self.chat.member(*id)) |
| 2998 | .collect() |
| 2999 | } |
| 3000 | |
| 3001 | fn calling_hint(&self, palette: Palette) -> Option<Div> { |
| 3002 | let community = self.chat.selected_community()?; |
| 3003 | let direct = self.is_direct(community.id); |
| 3004 | let alone = self.chat.in_voice(community.id) |
| 3005 | && self |
| 3006 | .chat |
| 3007 | .voice_ids(community.id) |
| 3008 | .iter() |
| 3009 | .all(|id| self.is_me(*id)); |
| 3010 | |
| 3011 | (direct && alone).then(|| { |
| 3012 | div() |
| 3013 | .text_color(palette.muted) |
| 3014 | .text_size(text(text::CAPTION)) |
| 3015 | .child(w().chat.calling) |
| 3016 | }) |
| 3017 | } |
| 3018 | |
| 3019 | fn call_banner(&self, entity: &Entity<Self>, palette: Palette) -> Option<Div> { |
| 3020 | let (community_id, caller) = self.incoming_call()?; |
| 3021 | let accept_id = community_id; |
| 3022 | |
| 3023 | Some( |
| 3024 | accent_bar(palette) |
| 3025 | .child( |
| 3026 | div() |
| 3027 | .flex() |
| 3028 | .items_center() |
| 3029 | .gap(px(space::SM)) |
| 3030 | .child(super::parts::small_icon(IconName::PhoneIncoming)) |
| 3031 | .child(w().chat.is_calling(&caller.username)), |
| 3032 | ) |
| 3033 | .child( |
| 3034 | div() |
| 3035 | .flex() |
| 3036 | .gap(px(space::SM)) |
| 3037 | .child( |
| 3038 | clickable( |
| 3039 | "accept_call", |
| 3040 | palette, |
| 3041 | entity_action(entity, move |yumush, window, cx| { |
| 3042 | yumush.accept_call(accept_id, window, cx) |
| 3043 | }), |
| 3044 | ) |
| 3045 | .text_color(palette.ok) |
| 3046 | .font_weight(FontWeight::MEDIUM) |
| 3047 | .px(px(space::SM)) |
| 3048 | .rounded(px(radius::SM)) |
| 3049 | .child(w().chat.accept), |
| 3050 | ) |
| 3051 | .child( |
| 3052 | clickable( |
| 3053 | "decline_call", |
| 3054 | palette, |
| 3055 | entity_action(entity, move |yumush, window, cx| { |
| 3056 | yumush.decline_call(community_id, window, cx) |
| 3057 | }), |
| 3058 | ) |
| 3059 | .text_color(palette.danger) |
| 3060 | .font_weight(FontWeight::MEDIUM) |
| 3061 | .px(px(space::SM)) |
| 3062 | .rounded(px(radius::SM)) |
| 3063 | .child(w().chat.decline), |
| 3064 | ), |
| 3065 | ), |
| 3066 | ) |
| 3067 | } |
| 3068 | |
| 3069 | fn incoming_call(&self) -> Option<(Id, User)> { |
| 3070 | self.chat.communities().iter().find_map(|community| { |
| 3071 | let CommunityKind::Direct { peer } = &community.kind else { |
| 3072 | return None; |
| 3073 | }; |
| 3074 | let calling = self.user.is_some() |
| 3075 | && self |
| 3076 | .chat |
| 3077 | .voice_ids(community.id) |
| 3078 | .iter() |
| 3079 | .any(|id| !self.is_me(*id)); |
| 3080 | let ringing = calling |
| 3081 | && !self.chat.in_voice(community.id) |
| 3082 | && self.chat.ringing.contains(&community.id); |
| 3083 | ringing.then(|| (community.id, peer.clone())) |
| 3084 | }) |
| 3085 | } |
| 3086 | |
| 3087 | fn ring_allowed(&self, community_id: Id) -> bool { |
| 3088 | self.user |
| 3089 | .as_ref() |
| 3090 | .is_some_and(|me| self.presence_of(me.id) == Presence::Online) |
| 3091 | && !self.chat.muted.contains(&community_id) |
| 3092 | } |
| 3093 | |
| 3094 | pub(super) fn update_ringing(&mut self, cx: &mut Context<Self>) { |
| 3095 | if self.incoming_call().is_none() { |
| 3096 | self.chat.ring_task = None; |
| 3097 | return; |
| 3098 | } |
| 3099 | if self.chat.ring_task.is_some() { |
| 3100 | return; |
| 3101 | } |
| 3102 | |
| 3103 | self.chat.ring_task = Some(cx.spawn(async move |this, cx| { |
| 3104 | for _ in 0..RING_LIMIT { |
| 3105 | let ringing = this.update(cx, |yumush, _| match yumush.incoming_call() { |
| 3106 | Some((community_id, _)) => { |
| 3107 | if yumush.ring_allowed(community_id) { |
| 3108 | yumush.network.play_ring(&yumush.settings.voice); |
| 3109 | } |
| 3110 | true |
| 3111 | } |
| 3112 | None => false, |
| 3113 | }); |
| 3114 | if !matches!(ringing, Ok(true)) { |
| 3115 | break; |
| 3116 | } |
| 3117 | cx.background_executor().timer(RING_INTERVAL).await; |
| 3118 | } |
| 3119 | })); |
| 3120 | } |
| 3121 | |
| 3122 | fn accept_call(&mut self, community_id: Id, window: &mut Window, cx: &mut Context<Self>) { |
| 3123 | self.chat.ringing.remove(&community_id); |
| 3124 | self.select_community(community_id, window, cx); |
| 3125 | self.toggle_voice(community_id, window, cx); |
| 3126 | self.update_ringing(cx); |
| 3127 | } |
| 3128 | |
| 3129 | fn decline_call(&mut self, community_id: Id, window: &mut Window, cx: &mut Context<Self>) { |
| 3130 | self.chat.ringing.remove(&community_id); |
| 3131 | self.update_ringing(cx); |
| 3132 | cx.notify(); |
| 3133 | |
| 3134 | let network = self.network.clone(); |
| 3135 | spawn_done(window, cx, async move { |
| 3136 | network.call(DeclineCall { community_id }).await |
| 3137 | }); |
| 3138 | } |
| 3139 | |
| 3140 | fn stage_controls(&self, entity: &Entity<Self>, palette: Palette) -> Option<impl IntoElement> { |
| 3141 | let community_id = self.chat.selected_community_id?; |
| 3142 | if self.voice_users(community_id).is_empty() || !self.in_stage_chat() { |
| 3143 | return None; |
| 3144 | } |
| 3145 | let current = self.chat.stage; |
| 3146 | |
| 3147 | Some( |
| 3148 | div() |
| 3149 | .ml(px(space::SM)) |
| 3150 | .flex() |
| 3151 | .items_center() |
| 3152 | .gap(px(space::XXS)) |
| 3153 | .child(stage_button( |
| 3154 | entity, |
| 3155 | palette, |
| 3156 | Stage::Strip, |
| 3157 | current == Stage::Strip, |
| 3158 | IconName::GalleryHorizontal, |
| 3159 | "Strip", |
| 3160 | w().chat.strip, |
| 3161 | )) |
| 3162 | .child(stage_button( |
| 3163 | entity, |
| 3164 | palette, |
| 3165 | Stage::Grid, |
| 3166 | current == Stage::Grid, |
| 3167 | IconName::LayoutGrid, |
| 3168 | "Grid", |
| 3169 | w().chat.grid, |
| 3170 | )) |
| 3171 | .child(stats_button( |
| 3172 | entity, |
| 3173 | palette, |
| 3174 | self.chat.stats_at == Some(StatsAt::Header), |
| 3175 | )), |
| 3176 | ) |
| 3177 | } |
| 3178 | } |
| 3179 | |
| 3180 | struct MemberRow { |
| 3181 | user: User, |
| 3182 | in_voice: bool, |
| 3183 | muted: bool, |
| 3184 | presence: Presence, |
| 3185 | } |
| 3186 | |
| 3187 | fn members_column( |
| 3188 | entity: &Entity<Yumush>, |
| 3189 | yumush: &Yumush, |
| 3190 | palette: Palette, |
| 3191 | ) -> Option<gpui_kit::Stateful<Div>> { |
| 3192 | let chat = &yumush.chat; |
| 3193 | let community_id = chat.selected_community_id?; |
| 3194 | let group = chat |
| 3195 | .selected_community() |
| 3196 | .is_some_and(|community| community.kind.is_group()); |
| 3197 | if !group { |
| 3198 | return None; |
| 3199 | } |
| 3200 | let voice = chat.voice_ids(community_id); |
| 3201 | let members: Vec<MemberRow> = chat |
| 3202 | .members |
| 3203 | .iter() |
| 3204 | .map(|member| MemberRow { |
| 3205 | user: member.user.clone(), |
| 3206 | in_voice: voice.contains(&member.user.id), |
| 3207 | muted: chat.server_muted.contains(&member.user.id), |
| 3208 | presence: yumush.presence_of(member.user.id), |
| 3209 | }) |
| 3210 | .collect(); |
| 3211 | let avatars = &chat.avatars; |
| 3212 | let entity = entity.clone(); |
| 3213 | let row = |MemberRow { |
| 3214 | user, |
| 3215 | in_voice, |
| 3216 | muted, |
| 3217 | presence, |
| 3218 | }: &MemberRow| { |
| 3219 | let watch = |kind: Kind| { |
| 3220 | (*in_voice && chat.share_of(community_id, user.id, kind).is_some()).then(|| { |
| 3221 | super::sidebar::watch_pill( |
| 3222 | &entity, |
| 3223 | user.id, |
| 3224 | kind, |
| 3225 | yumush.is_watching(user.id, kind), |
| 3226 | (kind == Kind::Screen && chat.waves.share_audible(user.id)) |
| 3227 | .then(|| chat.waves.share(user.id)) |
| 3228 | .flatten(), |
| 3229 | palette, |
| 3230 | ) |
| 3231 | }) |
| 3232 | }; |
| 3233 | let line = in_voice.then(|| { |
| 3234 | let speaking = chat.waves.speaking.contains(&user.id); |
| 3235 | div() |
| 3236 | .flex_1() |
| 3237 | .min_w(px(size::CONTROL)) |
| 3238 | .h(px(space::LG)) |
| 3239 | .child(super::parts::voice_line( |
| 3240 | chat.waves.voice(user.id), |
| 3241 | if speaking { |
| 3242 | super::parts::Voice::Live(palette.tokens.person(user.id)) |
| 3243 | } else { |
| 3244 | super::parts::Voice::Quiet |
| 3245 | }, |
| 3246 | palette.tokens, |
| 3247 | )) |
| 3248 | }); |
| 3249 | div() |
| 3250 | .flex() |
| 3251 | .items_center() |
| 3252 | .gap(px(space::XS)) |
| 3253 | .child( |
| 3254 | div() |
| 3255 | .min_w_0() |
| 3256 | .when(!in_voice, |this| this.flex_1()) |
| 3257 | .child(profile_popover( |
| 3258 | format!("member_{}", user.id), |
| 3259 | Anchor::RightCenter, |
| 3260 | user, |
| 3261 | &entity, |
| 3262 | hoverable( |
| 3263 | format!("member_trigger_{}", user.id), |
| 3264 | div() |
| 3265 | .h(px(size::CONTROL)) |
| 3266 | .px(px(space::SM)) |
| 3267 | .flex() |
| 3268 | .items_center() |
| 3269 | .gap(px(space::SM)) |
| 3270 | .rounded(px(radius::MD)) |
| 3271 | .child(presence_avatar( |
| 3272 | avatar( |
| 3273 | palette, |
| 3274 | user, |
| 3275 | px(size::ICON), |
| 3276 | text(text::CAPTION), |
| 3277 | avatars, |
| 3278 | ), |
| 3279 | *presence, |
| 3280 | px(size::ICON), |
| 3281 | palette, |
| 3282 | )) |
| 3283 | .child( |
| 3284 | div() |
| 3285 | .flex_1() |
| 3286 | .min_w_0() |
| 3287 | .flex() |
| 3288 | .flex_col() |
| 3289 | .child( |
| 3290 | div() |
| 3291 | .flex() |
| 3292 | .items_center() |
| 3293 | .gap(px(space::SM)) |
| 3294 | .child(user.username.clone()) |
| 3295 | .children(muted.then(|| { |
| 3296 | super::parts::small_icon(IconName::MicOff) |
| 3297 | .text_color(palette.danger) |
| 3298 | })), |
| 3299 | ) |
| 3300 | .children((!user.status.is_empty() && !in_voice).then( |
| 3301 | || { |
| 3302 | div() |
| 3303 | .text_size(text(text::CAPTION)) |
| 3304 | .text_color(palette.muted) |
| 3305 | .min_w_0() |
| 3306 | .child(super::glyph::line( |
| 3307 | user.status.clone().into(), |
| 3308 | super::text(super::style::text::CAPTION), |
| 3309 | )) |
| 3310 | }, |
| 3311 | )), |
| 3312 | ), |
| 3313 | palette, |
| 3314 | ), |
| 3315 | )), |
| 3316 | ) |
| 3317 | .children(line) |
| 3318 | .children(watch(Kind::Camera)) |
| 3319 | .children(watch(Kind::Screen)) |
| 3320 | }; |
| 3321 | let section = |title: &'static str, rows: Vec<&MemberRow>| { |
| 3322 | (!rows.is_empty()).then(|| { |
| 3323 | div() |
| 3324 | .flex() |
| 3325 | .flex_col() |
| 3326 | .child( |
| 3327 | super::parts::caption_head(format!("{title} · {}", rows.len()), palette.tokens) |
| 3328 | .pb(px(space::XS)), |
| 3329 | ) |
| 3330 | .children(rows.into_iter().map(row)) |
| 3331 | }) |
| 3332 | }; |
| 3333 | let in_call: Vec<&MemberRow> = members.iter().filter(|row| row.in_voice).collect(); |
| 3334 | let join = (!chat.in_voice(community_id) && yumush.can_join_voice(community_id)).then(|| { |
| 3335 | let action = entity_action(&entity, move |yumush, window, cx| { |
| 3336 | yumush.toggle_voice(community_id, window, cx) |
| 3337 | }); |
| 3338 | super::parts::pill("margin_join", palette.tokens, action) |
| 3339 | .self_start() |
| 3340 | .gap(px(space::SM)) |
| 3341 | .text_color(palette.tokens.ground()) |
| 3342 | .font_weight(FontWeight::SEMIBOLD) |
| 3343 | .child(super::parts::small_icon(IconName::Headphones)) |
| 3344 | .child(w().chat.join_voice) |
| 3345 | }); |
| 3346 | let call = (!in_call.is_empty() || join.is_some()).then(|| { |
| 3347 | div() |
| 3348 | .flex() |
| 3349 | .flex_col() |
| 3350 | .gap(px(space::SM)) |
| 3351 | .children(join) |
| 3352 | .children(section(w().chat.in_voice, in_call)) |
| 3353 | }); |
| 3354 | let online = members |
| 3355 | .iter() |
| 3356 | .filter(|row| !row.in_voice && row.presence != Presence::Offline) |
| 3357 | .collect(); |
| 3358 | let offline = members |
| 3359 | .iter() |
| 3360 | .filter(|row| !row.in_voice && row.presence == Presence::Offline) |
| 3361 | .collect(); |
| 3362 | |
| 3363 | Some( |
| 3364 | div() |
| 3365 | .id("members_column") |
| 3366 | .overflow_y_scroll() |
| 3367 | .px(px(space::SM)) |
| 3368 | .py(px(space::MD)) |
| 3369 | .flex() |
| 3370 | .flex_col() |
| 3371 | .gap(px(space::LG)) |
| 3372 | .child(room_head(yumush, palette)) |
| 3373 | .children(call) |
| 3374 | .children(section(w().common.presence_online, online)) |
| 3375 | .children(section(w().common.presence_offline, offline)), |
| 3376 | ) |
| 3377 | } |
| 3378 | |
| 3379 | const GLIDE: Duration = Duration::from_millis(240); |
| 3380 | const GLIDE_SLACK: Duration = Duration::from_millis(100); |
| 3381 | |
| 3382 | fn room_head(yumush: &Yumush, palette: Palette) -> Div { |
| 3383 | let chat = &yumush.chat; |
| 3384 | let online = chat |
| 3385 | .members |
| 3386 | .iter() |
| 3387 | .filter(|member| yumush.is_online(member.user.id)) |
| 3388 | .count(); |
| 3389 | |
| 3390 | div().px(px(space::SM)).child(super::parts::words( |
| 3391 | w().community_card.people(chat.members.len(), online), |
| 3392 | super::parts::Text::Caption, |
| 3393 | super::parts::Ink::Dim, |
| 3394 | palette.tokens, |
| 3395 | )) |
| 3396 | } |
| 3397 | |
| 3398 | fn accent_bar(palette: Palette) -> Div { |
| 3399 | div() |
| 3400 | .flex() |
| 3401 | .items_center() |
| 3402 | .justify_between() |
| 3403 | .gap(px(space::SM)) |
| 3404 | .px(px(space::MD)) |
| 3405 | .py(px(space::XS)) |
| 3406 | .text_size(text(text::LABEL)) |
| 3407 | .text_color(palette.text) |
| 3408 | .border_b_1() |
| 3409 | .border_color(palette.border) |
| 3410 | } |
| 3411 | |
| 3412 | fn thread_dot(mine: bool, palette: Palette) -> Div { |
| 3413 | let dot = div() |
| 3414 | .absolute() |
| 3415 | .left(px(-(space::LG + space::XS))) |
| 3416 | .top(px(space::SM)) |
| 3417 | .size(px(space::SM)) |
| 3418 | .rounded_full(); |
| 3419 | if mine { |
| 3420 | dot.bg(palette.text) |
| 3421 | } else { |
| 3422 | dot.border_1() |
| 3423 | .border_color(palette.tokens.faint()) |
| 3424 | .bg(palette.window) |
| 3425 | } |
| 3426 | } |
| 3427 | |
| 3428 | fn new_divider(palette: Palette) -> impl IntoElement { |
| 3429 | div() |
| 3430 | .flex() |
| 3431 | .items_center() |
| 3432 | .gap(px(space::SM)) |
| 3433 | .my(px(space::SM)) |
| 3434 | .child(div().flex_1().h(px(stroke::LINE)).bg(palette.accent)) |
| 3435 | .child( |
| 3436 | div() |
| 3437 | .text_size(text(text::CAPTION)) |
| 3438 | .text_color(palette.accent) |
| 3439 | .child(w().chat.new), |
| 3440 | ) |
| 3441 | } |
| 3442 | |
| 3443 | fn older_button(entity: &Entity<Yumush>, palette: Palette) -> impl IntoElement { |
| 3444 | let entity = entity.clone(); |
| 3445 | |
| 3446 | clickable("load_older_button", palette, move |window, cx| { |
| 3447 | entity.update(cx, |yumush, cx| yumush.load_older_messages(window, cx)); |
| 3448 | }) |
| 3449 | .self_center() |
| 3450 | .h(px(size::CONTROL_SM)) |
| 3451 | .px(px(space::SM)) |
| 3452 | .flex() |
| 3453 | .items_center() |
| 3454 | .rounded(px(radius::MD)) |
| 3455 | .text_color(palette.muted) |
| 3456 | .child("↑") |
| 3457 | } |
| 3458 | |
| 3459 | fn call_duration(seconds: u64) -> String { |
| 3460 | let (hours, minutes, seconds) = (seconds / 3600, seconds % 3600 / 60, seconds % 60); |
| 3461 | if hours > 0 { |
| 3462 | format!("{hours}h {minutes}m") |
| 3463 | } else if minutes > 0 { |
| 3464 | format!("{minutes}m {seconds}s") |
| 3465 | } else { |
| 3466 | format!("{seconds}s") |
| 3467 | } |
| 3468 | } |
| 3469 | |
| 3470 | #[derive(Clone, Copy)] |
| 3471 | enum CallRun { |
| 3472 | Single, |
| 3473 | Folded(usize), |
| 3474 | Open, |
| 3475 | } |
| 3476 | |
| 3477 | fn render_call( |
| 3478 | message: &Message, |
| 3479 | call: CallLog, |
| 3480 | run: CallRun, |
| 3481 | spaced: bool, |
| 3482 | chat: &Chat, |
| 3483 | me: Option<&User>, |
| 3484 | entity: &Entity<Yumush>, |
| 3485 | palette: Palette, |
| 3486 | ) -> AnyElement { |
| 3487 | let mine = me.is_some_and(|me| me.id == message.user_id); |
| 3488 | let author = chat |
| 3489 | .author(message.user_id) |
| 3490 | .map_or(w().chat.unknown, |user| user.username.as_str()); |
| 3491 | let message_id = message.id; |
| 3492 | let entity = entity.clone(); |
| 3493 | let toggle = match run { |
| 3494 | CallRun::Single => None, |
| 3495 | CallRun::Folded(calls) => Some( |
| 3496 | div() |
| 3497 | .hover(|style| style.underline()) |
| 3498 | .child(format!("\u{a0}· {}", w().chat.calls(calls))), |
| 3499 | ), |
| 3500 | CallRun::Open => Some( |
| 3501 | div() |
| 3502 | .pl(px(space::XS)) |
| 3503 | .child(super::parts::small_icon(IconName::ChevronUp)), |
| 3504 | ), |
| 3505 | }; |
| 3506 | call_line(call, author, mine, palette) |
| 3507 | .children(toggle.map(|toggle| { |
| 3508 | toggle |
| 3509 | .id(format!("calls_{message_id}")) |
| 3510 | .cursor_pointer() |
| 3511 | .text_color(palette.tokens.faint()) |
| 3512 | .on_click(move |_, _, cx| { |
| 3513 | entity.update(cx, |yumush, cx| { |
| 3514 | if !yumush.chat.open_calls.remove(&message_id) { |
| 3515 | yumush.chat.open_calls.insert(message_id); |
| 3516 | } |
| 3517 | if !yumush.chat.away() { |
| 3518 | yumush.chat.jump_to_bottom(); |
| 3519 | } |
| 3520 | cx.notify(); |
| 3521 | }) |
| 3522 | }) |
| 3523 | })) |
| 3524 | .id(format!("message_row_{}", message.id)) |
| 3525 | .relative() |
| 3526 | .when(spaced, |this| this.mt(px(space::LG))) |
| 3527 | .into_any_element() |
| 3528 | } |
| 3529 | |
| 3530 | pub(super) fn call_line(call: CallLog, author: &str, mine: bool, palette: Palette) -> Div { |
| 3531 | let w = &w().chat; |
| 3532 | let (icon, color) = match call { |
| 3533 | CallLog::Started | CallLog::Ended { .. } => (IconName::Phone, palette.muted), |
| 3534 | CallLog::Declined => (IconName::PhoneOff, palette.danger), |
| 3535 | CallLog::Missed => (IconName::PhoneMissed, palette.danger), |
| 3536 | }; |
| 3537 | let label = match call { |
| 3538 | CallLog::Started if mine => w.call_started_mine.to_string(), |
| 3539 | CallLog::Started => w.call_started(author), |
| 3540 | CallLog::Declined if mine => w.call_declined_mine.to_string(), |
| 3541 | CallLog::Declined => w.call_declined(author), |
| 3542 | CallLog::Missed if mine => w.call_missed_mine.to_string(), |
| 3543 | CallLog::Missed => w.call_missed(author), |
| 3544 | CallLog::Ended { seconds } => w.call_ended(call_duration(seconds)), |
| 3545 | }; |
| 3546 | div() |
| 3547 | .relative() |
| 3548 | .flex() |
| 3549 | .items_center() |
| 3550 | .text_size(text(text::CAPTION)) |
| 3551 | .text_color(palette.muted) |
| 3552 | .child( |
| 3553 | div() |
| 3554 | .absolute() |
| 3555 | .left(px( |
| 3556 | -(space::LG + space::XS) - (size::ICON_SM - space::SM) / 2. |
| 3557 | )) |
| 3558 | .bg(palette.window) |
| 3559 | .child(super::parts::small_icon(icon).text_color(color)), |
| 3560 | ) |
| 3561 | .child(label) |
| 3562 | } |
| 3563 | |
| 3564 | fn render_message( |
| 3565 | message: &Message, |
| 3566 | first: bool, |
| 3567 | spaced: bool, |
| 3568 | run: CallRun, |
| 3569 | chat: &Chat, |
| 3570 | me: Option<&User>, |
| 3571 | clock: &'static str, |
| 3572 | entity: &Entity<Yumush>, |
| 3573 | palette: Palette, |
| 3574 | ) -> AnyElement { |
| 3575 | if let Some(call) = message.call { |
| 3576 | return render_call( |
| 3577 | message, |
| 3578 | call, |
| 3579 | run, |
| 3580 | first && spaced, |
| 3581 | chat, |
| 3582 | me, |
| 3583 | entity, |
| 3584 | palette, |
| 3585 | ); |
| 3586 | } |
| 3587 | let builder = me.is_some_and(|me| permission::is_builder(me.role)); |
| 3588 | let collapsed = message.deleted && !chat.revealed.contains(&message.id); |
| 3589 | let author = chat.author(message.user_id); |
| 3590 | let username = author.map_or(w().chat.unknown, |user| user.username.as_str()); |
| 3591 | let name = div().flex().items_center().gap(px(space::SM)).child( |
| 3592 | div() |
| 3593 | .id(format!("author_name_{}", message.id)) |
| 3594 | .font_weight(FontWeight::SEMIBOLD) |
| 3595 | .text_color(palette.text) |
| 3596 | .when(author.is_some(), |this| { |
| 3597 | this.hover(|style| style.underline()) |
| 3598 | }) |
| 3599 | .child(username.to_string()), |
| 3600 | ); |
| 3601 | let mentioned = me.is_some_and(|me| mentions(&message.body, &me.username)); |
| 3602 | let mine = me.is_some_and(|me| me.id == message.user_id); |
| 3603 | let group = format!("message_{}", message.id); |
| 3604 | let selecting = matches!(chat.composer, Composer::Select { .. }); |
| 3605 | let selected = matches!(&chat.composer, Composer::Select { ids } if ids.contains(&message.id)); |
| 3606 | let highlighted = chat |
| 3607 | .highlight |
| 3608 | .as_ref() |
| 3609 | .is_some_and(|(id, at)| id == &message.id && at.elapsed() < HIGHLIGHT_TTL); |
| 3610 | |
| 3611 | let row = div() |
| 3612 | .id(format!("message_row_{}", message.id)) |
| 3613 | .group(group.clone()) |
| 3614 | .on_hover({ |
| 3615 | let entity = entity.clone(); |
| 3616 | let id = message.id; |
| 3617 | move |hovered, _, cx| { |
| 3618 | entity.update(cx, |yumush, cx| { |
| 3619 | if message_actions::track(&mut yumush.chat.hover_row, id, *hovered) { |
| 3620 | cx.notify(); |
| 3621 | } |
| 3622 | }); |
| 3623 | } |
| 3624 | }) |
| 3625 | .relative() |
| 3626 | .flex() |
| 3627 | .flex_col() |
| 3628 | .min_w_0() |
| 3629 | .hover(|style| style.bg(palette.hover)) |
| 3630 | .when(chat.engaged(message.id), |this| this.bg(palette.hover)) |
| 3631 | .when(message.deleted, |this| { |
| 3632 | this.text_color(palette.tokens.faint()) |
| 3633 | }) |
| 3634 | .when(first && spaced && !selecting, |this| this.mt(px(space::LG))) |
| 3635 | .when(highlighted, |this| this.bg(palette.tokens.press())) |
| 3636 | .when(mentioned, |this| { |
| 3637 | this.border_l_2().border_color(palette.accent) |
| 3638 | }) |
| 3639 | .when(first, |this| { |
| 3640 | let author = match author { |
| 3641 | Some(user) => profile_popover( |
| 3642 | format!("author_{}", message.id), |
| 3643 | Anchor::LeftCenter, |
| 3644 | user, |
| 3645 | entity, |
| 3646 | name.cursor_pointer(), |
| 3647 | ) |
| 3648 | .into_any_element(), |
| 3649 | None => name.into_any_element(), |
| 3650 | }; |
| 3651 | |
| 3652 | this.child( |
| 3653 | div() |
| 3654 | .relative() |
| 3655 | .flex() |
| 3656 | .items_center() |
| 3657 | .gap(px(space::SM)) |
| 3658 | .mb(px(space::XXS)) |
| 3659 | .child(thread_dot(if selecting { selected } else { mine }, palette)) |
| 3660 | .child(author) |
| 3661 | .child( |
| 3662 | div() |
| 3663 | .text_size(text(text::CAPTION)) |
| 3664 | .text_color(palette.muted) |
| 3665 | .child(millis_time(message.created_at, clock)), |
| 3666 | ), |
| 3667 | ) |
| 3668 | }) |
| 3669 | .child( |
| 3670 | div() |
| 3671 | .relative() |
| 3672 | .min_w_0() |
| 3673 | .flex() |
| 3674 | .items_center() |
| 3675 | .gap(px(space::SM)) |
| 3676 | .when(selecting && !first, |this| { |
| 3677 | this.child(thread_dot(selected, palette)) |
| 3678 | }) |
| 3679 | .child( |
| 3680 | div() |
| 3681 | .min_w_0() |
| 3682 | .max_w(px(size::PAGE)) |
| 3683 | .when(message.body.contains("```"), |this| this.w_full()) |
| 3684 | .flex() |
| 3685 | .flex_col() |
| 3686 | .gap(px(space::SM)) |
| 3687 | .when(collapsed, |this| { |
| 3688 | this.child(message_actions::reveal_deleted(message.id, entity, palette)) |
| 3689 | }) |
| 3690 | .when(!collapsed, |this| { |
| 3691 | this.children(message_actions::quote_line( |
| 3692 | message, chat, entity, palette, |
| 3693 | )) |
| 3694 | .when(!message.body.is_empty(), |this| { |
| 3695 | this.child( |
| 3696 | div() |
| 3697 | .min_w_0() |
| 3698 | .when(message.body.contains("```"), |this| this.w_full()) |
| 3699 | .flex() |
| 3700 | .flex_wrap() |
| 3701 | .items_baseline() |
| 3702 | .gap(px(space::XS)) |
| 3703 | .text_size(text(text::BODY)) |
| 3704 | .line_height(relative(1.55)) |
| 3705 | .text_color(palette.body) |
| 3706 | .child( |
| 3707 | div() |
| 3708 | .min_w_0() |
| 3709 | .when(message.body.contains("```"), |this| { |
| 3710 | this.w_full() |
| 3711 | }) |
| 3712 | .child( |
| 3713 | TextView::markdown( |
| 3714 | format!("message-body-{}", message.id), |
| 3715 | crate::emoji::markdown(&hard_breaks( |
| 3716 | &message.body, |
| 3717 | )), |
| 3718 | ) |
| 3719 | .markdown_extensions(super::glyph::extensions()) |
| 3720 | .selectable(true), |
| 3721 | ), |
| 3722 | ) |
| 3723 | .when(message.edited || message.deleted, |this| { |
| 3724 | this.child( |
| 3725 | div() |
| 3726 | .flex() |
| 3727 | .gap(px(space::SM)) |
| 3728 | .text_size(text(text::CAPTION)) |
| 3729 | .text_color(palette.muted) |
| 3730 | .children(message.edited.then(|| { |
| 3731 | message_actions::edited_mark( |
| 3732 | message, builder, entity, palette, |
| 3733 | ) |
| 3734 | })) |
| 3735 | .children(message.deleted.then(|| { |
| 3736 | message_actions::collapse_deleted( |
| 3737 | message.id, entity, |
| 3738 | ) |
| 3739 | })), |
| 3740 | ) |
| 3741 | }), |
| 3742 | ) |
| 3743 | }) |
| 3744 | .children(media_view::attachments( |
| 3745 | &message.attachments, |
| 3746 | &chat.media, |
| 3747 | entity, |
| 3748 | palette, |
| 3749 | )) |
| 3750 | .children(snippet::snippet_card(message, clock, chat, entity, palette)) |
| 3751 | .children(message.poll.as_ref().map(|poll| { |
| 3752 | poll::poll_card( |
| 3753 | poll, |
| 3754 | poll::PollMode::Live(message), |
| 3755 | me, |
| 3756 | chat, |
| 3757 | entity, |
| 3758 | palette, |
| 3759 | ) |
| 3760 | })) |
| 3761 | .children(media_view::embeds(message, &chat.media, palette)) |
| 3762 | .children( |
| 3763 | (!message.reactions.is_empty()).then(|| { |
| 3764 | message_actions::reactions(message, me, entity, palette) |
| 3765 | }), |
| 3766 | ) |
| 3767 | }), |
| 3768 | ) |
| 3769 | .children(message.pinned.then(|| message_actions::pin_mark(palette))) |
| 3770 | .children((!selecting && !message.deleted).then(|| { |
| 3771 | div() |
| 3772 | .absolute() |
| 3773 | .right_0() |
| 3774 | .bottom(gpui_kit::relative(1.)) |
| 3775 | .child(message_actions::actions(message, chat, me, entity, palette)) |
| 3776 | })), |
| 3777 | ) |
| 3778 | .children( |
| 3779 | (chat.report == Some(ReportTarget::Message(message.id))) |
| 3780 | .then(|| message_actions::report_bar(&chat.report_reason, entity, palette)), |
| 3781 | ); |
| 3782 | |
| 3783 | if !selecting { |
| 3784 | return row.into_any_element(); |
| 3785 | } |
| 3786 | |
| 3787 | let message_id = message.id; |
| 3788 | let select_entity = entity.clone(); |
| 3789 | |
| 3790 | div() |
| 3791 | .id(format!("select_row_{}", message.id)) |
| 3792 | .when(first && spaced, |this| this.mt(px(space::LG))) |
| 3793 | .cursor_pointer() |
| 3794 | .on_click(move |_, _, cx| { |
| 3795 | select_entity.update(cx, |yumush, cx| yumush.toggle_select(message_id, cx)); |
| 3796 | }) |
| 3797 | .child(row) |
| 3798 | .into_any_element() |
| 3799 | } |
| 3800 | |
| 3801 | fn editable_input( |
| 3802 | placeholder: &'static str, |
| 3803 | save: fn(&mut Yumush, &mut Window, &mut Context<Yumush>), |
| 3804 | window: &mut Window, |
| 3805 | cx: &mut Context<Yumush>, |
| 3806 | ) -> Entity<InputState> { |
| 3807 | let input = new_input(placeholder, window, cx); |
| 3808 | cx.subscribe_in( |
| 3809 | &input, |
| 3810 | window, |
| 3811 | move |this, _, event, window, cx| match event { |
| 3812 | InputEvent::PressEnter { .. } => save(this, window, cx), |
| 3813 | InputEvent::Change => cx.notify(), |
| 3814 | _ => {} |
| 3815 | }, |
| 3816 | ) |
| 3817 | .detach(); |
| 3818 | input |
| 3819 | } |
| 3820 | |
| 3821 | fn hard_breaks(body: &str) -> String { |
| 3822 | let mut fenced = false; |
| 3823 | let lines: Vec<String> = body |
| 3824 | .lines() |
| 3825 | .map(|line| { |
| 3826 | if line.trim_start().starts_with("```") { |
| 3827 | fenced = !fenced; |
| 3828 | return line.to_string(); |
| 3829 | } |
| 3830 | match (fenced, line.trim().is_empty()) { |
| 3831 | (true, _) => line.to_string(), |
| 3832 | (false, true) => "\u{a0} ".to_string(), |
| 3833 | (false, false) => format!("{line} "), |
| 3834 | } |
| 3835 | }) |
| 3836 | .collect(); |
| 3837 | lines.join("\n") |
| 3838 | } |
| 3839 | |
| 3840 | fn stats_button(entity: &Entity<Yumush>, palette: Palette, open: bool) -> impl IntoElement { |
| 3841 | clickable( |
| 3842 | format!("stats_toggle_{open}"), |
| 3843 | palette, |
| 3844 | entity_action(entity, |yumush: &mut Yumush, _, cx| { |
| 3845 | yumush.toggle_stats(StatsAt::Header); |
| 3846 | cx.notify(); |
| 3847 | }), |
| 3848 | ) |
| 3849 | .tip(if open { |
| 3850 | w().chat.stats_hide |
| 3851 | } else { |
| 3852 | w().chat.stats |
| 3853 | }) |
| 3854 | .size(px(size::CONTROL_SM)) |
| 3855 | .flex() |
| 3856 | .items_center() |
| 3857 | .justify_center() |
| 3858 | .rounded(px(radius::MD)) |
| 3859 | .when(open, |this| this.bg(palette.tokens.press())) |
| 3860 | .text_color(if open { palette.text } else { palette.muted }) |
| 3861 | .child(super::parts::small_icon(IconName::Activity)) |
| 3862 | } |
| 3863 | |
| 3864 | fn stage_button( |
| 3865 | entity: &Entity<Yumush>, |
| 3866 | palette: Palette, |
| 3867 | layout: Stage, |
| 3868 | active: bool, |
| 3869 | icon: IconName, |
| 3870 | name: &'static str, |
| 3871 | label: &'static str, |
| 3872 | ) -> impl IntoElement { |
| 3873 | clickable( |
| 3874 | format!("stage_{name}_{active}"), |
| 3875 | palette, |
| 3876 | entity_action(entity, move |yumush: &mut Yumush, _, cx| { |
| 3877 | let next = if active { Stage::Hidden } else { layout }; |
| 3878 | yumush.set_stage(next, cx) |
| 3879 | }), |
| 3880 | ) |
| 3881 | .tip(if active { w().chat.hide } else { label }) |
| 3882 | .size(px(size::CONTROL_SM)) |
| 3883 | .flex() |
| 3884 | .items_center() |
| 3885 | .justify_center() |
| 3886 | .rounded(px(radius::MD)) |
| 3887 | .when(active, |this| this.bg(palette.tokens.press())) |
| 3888 | .text_color(if active { |
| 3889 | palette.accent |
| 3890 | } else { |
| 3891 | palette.muted |
| 3892 | }) |
| 3893 | .child(super::parts::small_icon(icon)) |
| 3894 | } |