Redesign builder page and unify single sources of truth
Browse files at this commita16328af9f56169936cba18dcd11d741019c8fabParent: 1184462- refactor
- redesign builder page and unify single sources of truth
44 changed files +5050 -3703
- crates/client/Cargo.toml +12 -12
- crates/client/src/gui.rs +36 -12
- crates/client/src/gui/admin.rs +0 -3224
- crates/client/src/gui/builder.rs +402 -0
- crates/client/src/gui/builder/access.rs +317 -0
- crates/client/src/gui/builder/communities.rs +474 -0
- crates/client/src/gui/builder/log.rs +322 -0
- crates/client/src/gui/builder/messages.rs +250 -0
- crates/client/src/gui/builder/overview.rs +176 -0
- crates/client/src/gui/builder/reports.rs +393 -0
- crates/client/src/gui/builder/server.rs +288 -0
- crates/client/src/gui/builder/storage.rs +338 -0
- crates/client/src/gui/builder/ui.rs +280 -0
- crates/client/src/gui/builder/users.rs +912 -0
- crates/client/src/gui/builder/voice.rs +240 -0
- crates/client/src/gui/chat.rs +162 -150
- crates/client/src/gui/community_settings.rs +2 -1
- crates/client/src/gui/media_view.rs +4 -7
- crates/client/src/gui/profile.rs +5 -7
- crates/client/src/gui/sidebar.rs +4 -4
- crates/client/src/gui/snippet.rs +2 -1
- crates/client/src/gui/social.rs +2 -2
- crates/client/src/settings.rs +11 -13
- crates/client/src/voice/audio.rs +4 -2
- crates/client/src/voice/audio/pipewire.rs +4 -5
- crates/client/src/voice/audio/wasapi.rs +4 -5
- crates/client/src/voice/engine.rs +3 -3
- crates/common/src/constant.rs +4 -0
- crates/common/src/model.rs +230 -91
- crates/common/src/permission.rs +12 -4
- crates/server/src/admin_log.rs +46 -43
- crates/server/src/attachment.rs +4 -4
- crates/server/src/authentication.rs +4 -4
- crates/server/src/community.rs +8 -10
- crates/server/src/friend.rs +3 -3
- crates/server/src/invite.rs +3 -3
- crates/server/src/log.rs +7 -22
- crates/server/src/overview.rs +5 -5
- crates/server/src/report.rs +10 -9
- crates/server/src/request.rs +28 -28
- crates/server/src/retention.rs +1 -1
- crates/server/src/server_ban.rs +3 -3
- crates/server/src/user.rs +33 -23
- crates/server/src/user_community.rs +2 -2
crates/client/Cargo.tomlmodified+12 -12
| @@ -9,18 +9,18 @@ tokio.workspace = true | |||
| 9 | quinn = { workspace = true, features = ["rustls-ring", "platform-verifier"] } | 9 | quinn = { workspace = true, features = ["rustls-ring", "platform-verifier"] } |
| 10 | 10 | ||
| 11 | gpui-kit = "0.11.0" | 11 | gpui-kit = "0.11.0" |
| 12 | -ec-opus = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "e7e5914914a6a7d37093879f4b4e76b56542ad76" } | 12 | +ec-opus = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "e84467678af472c83966325ec66cfc8f46083dc1" } |
| 13 | -ec-core = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "e7e5914914a6a7d37093879f4b4e76b56542ad76" } | 13 | +ec-core = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "e84467678af472c83966325ec66cfc8f46083dc1" } |
| 14 | -ec-probe = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "e7e5914914a6a7d37093879f4b4e76b56542ad76" } | 14 | +ec-probe = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "e84467678af472c83966325ec66cfc8f46083dc1" } |
| 15 | -ec-mp4 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "e7e5914914a6a7d37093879f4b4e76b56542ad76" } | 15 | +ec-mp4 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "e84467678af472c83966325ec66cfc8f46083dc1" } |
| 16 | -ec-matroska = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "e7e5914914a6a7d37093879f4b4e76b56542ad76" } | 16 | +ec-matroska = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "e84467678af472c83966325ec66cfc8f46083dc1" } |
| 17 | -ec-ogg = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "e7e5914914a6a7d37093879f4b4e76b56542ad76" } | 17 | +ec-ogg = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "e84467678af472c83966325ec66cfc8f46083dc1" } |
| 18 | -ec-h264 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "e7e5914914a6a7d37093879f4b4e76b56542ad76" } | 18 | +ec-h264 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "e84467678af472c83966325ec66cfc8f46083dc1" } |
| 19 | -ec-vp8 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "e7e5914914a6a7d37093879f4b4e76b56542ad76" } | 19 | +ec-vp8 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "e84467678af472c83966325ec66cfc8f46083dc1" } |
| 20 | -ec-aac = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "e7e5914914a6a7d37093879f4b4e76b56542ad76" } | 20 | +ec-aac = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "e84467678af472c83966325ec66cfc8f46083dc1" } |
| 21 | -ec-mp3 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "e7e5914914a6a7d37093879f4b4e76b56542ad76" } | 21 | +ec-mp3 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "e84467678af472c83966325ec66cfc8f46083dc1" } |
| 22 | -ec-vorbis = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "e7e5914914a6a7d37093879f4b4e76b56542ad76" } | 22 | +ec-vorbis = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "e84467678af472c83966325ec66cfc8f46083dc1" } |
| 23 | -ec-flac = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "e7e5914914a6a7d37093879f4b4e76b56542ad76" } | 23 | +ec-flac = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "e84467678af472c83966325ec66cfc8f46083dc1" } |
| 24 | ureq = "3.5.2" | 24 | ureq = "3.5.2" |
| 25 | bytes = "1.12.2" | 25 | bytes = "1.12.2" |
| 26 | tracing = "0.1.44" | 26 | tracing = "0.1.44" |
crates/client/src/gui.rsmodified+36 -12
| @@ -34,8 +34,8 @@ use crate::{ | |||
| 34 | settings::{Look, Scale, Settings, ThemeChoice}, | 34 | settings::{Look, Scale, Settings, ThemeChoice}, |
| 35 | }; | 35 | }; |
| 36 | 36 | ||
| 37 | -mod admin; | ||
| 38 | mod auth; | 37 | mod auth; |
| 38 | +mod builder; | ||
| 39 | mod chat; | 39 | mod chat; |
| 40 | mod community_settings; | 40 | mod community_settings; |
| 41 | mod composer; | 41 | mod composer; |
| @@ -275,6 +275,28 @@ pub fn community_icon( | |||
| 275 | } | 275 | } |
| 276 | } | 276 | } |
| 277 | 277 | ||
| 278 | +pub const KB: f64 = 1024.0; | ||
| 279 | +pub const MB: f64 = KB * 1024.0; | ||
| 280 | +const GB: f64 = MB * 1024.0; | ||
| 281 | + | ||
| 282 | +pub const STAMP: &str = "%b %-d, %H:%M"; | ||
| 283 | +pub const YEAR_STAMP: &str = "%b %-d %Y, %H:%M"; | ||
| 284 | +pub const CLOCK: &str = "%H:%M"; | ||
| 285 | +pub const DAY: &str = "%b %-d"; | ||
| 286 | +pub const DAY_YEAR: &str = "%b %-d, %Y"; | ||
| 287 | +pub const DATE: &str = "%b %-d %Y"; | ||
| 288 | + | ||
| 289 | +pub fn storage(bytes: u64) -> String { | ||
| 290 | + let bytes = bytes as f64; | ||
| 291 | + if bytes >= GB { | ||
| 292 | + format!("{:.1} GB", bytes / GB) | ||
| 293 | + } else if bytes >= MB { | ||
| 294 | + format!("{:.1} MB", bytes / MB) | ||
| 295 | + } else { | ||
| 296 | + format!("{:.1} KB", bytes / KB) | ||
| 297 | + } | ||
| 298 | +} | ||
| 299 | + | ||
| 278 | pub fn id_time(id: &str, format: &str) -> String { | 300 | pub fn id_time(id: &str, format: &str) -> String { |
| 279 | created_at_ms(id) | 301 | created_at_ms(id) |
| 280 | .map(|millis| millis_time(millis, format)) | 302 | .map(|millis| millis_time(millis, format)) |
| @@ -388,12 +410,11 @@ impl RenderOnce for Trigger { | |||
| 388 | } | 410 | } |
| 389 | 411 | ||
| 390 | pub fn rank_tag(rank: Option<Rank>, palette: Palette) -> Option<Div> { | 412 | pub fn rank_tag(rank: Option<Rank>, palette: Palette) -> Option<Div> { |
| 391 | - let (label, color) = match rank? { | 413 | + let rank = rank?; |
| 392 | - Rank::Owner => ("Owner", palette.muted), | 414 | + let color = match rank { |
| 393 | - Rank::Admin => ("Admin", palette.muted), | ||
| 394 | - Rank::Moderator => ("Mod", palette.muted), | ||
| 395 | Rank::Member => return None, | 415 | Rank::Member => return None, |
| 396 | - Rank::Builder => ("Builder", palette.text), | 416 | + Rank::Builder => palette.text, |
| 417 | + _ => palette.muted, | ||
| 397 | }; | 418 | }; |
| 398 | 419 | ||
| 399 | Some( | 420 | Some( |
| @@ -404,7 +425,7 @@ pub fn rank_tag(rank: Option<Rank>, palette: Palette) -> Option<Div> { | |||
| 404 | .bg(palette.hover) | 425 | .bg(palette.hover) |
| 405 | .text_size(text(11.)) | 426 | .text_size(text(11.)) |
| 406 | .text_color(color) | 427 | .text_color(color) |
| 407 | - .child(label), | 428 | + .child(rank.label()), |
| 408 | ) | 429 | ) |
| 409 | } | 430 | } |
| 410 | 431 | ||
| @@ -611,8 +632,8 @@ impl Yumush { | |||
| 611 | .status_input | 632 | .status_input |
| 612 | .update(cx, |input, cx| input.set_value(status, window, cx)); | 633 | .update(cx, |input, cx| input.set_value(status, window, cx)); |
| 613 | } | 634 | } |
| 614 | - if view == View::Admin { | 635 | + if view == View::Builder { |
| 615 | - self.load_admin(window, cx); | 636 | + self.load_builder(window, cx); |
| 616 | } | 637 | } |
| 617 | if view == View::CommunitySettings { | 638 | if view == View::CommunitySettings { |
| 618 | let name = self | 639 | let name = self |
| @@ -636,7 +657,7 @@ impl Yumush { | |||
| 636 | 657 | ||
| 637 | pub fn themed(&self, cx: &App) -> Palette { | 658 | pub fn themed(&self, cx: &App) -> Palette { |
| 638 | let community = match self.chat.view { | 659 | let community = match self.chat.view { |
| 639 | - View::Settings | View::Admin => None, | 660 | + View::Settings | View::Builder => None, |
| 640 | _ => self.chat.selected_community(), | 661 | _ => self.chat.selected_community(), |
| 641 | }; | 662 | }; |
| 642 | palette(cx).for_community(community, &self.chat.hues) | 663 | palette(cx).for_community(community, &self.chat.hues) |
| @@ -689,7 +710,7 @@ impl Yumush { | |||
| 689 | if self.is_me(&user.id) { | 710 | if self.is_me(&user.id) { |
| 690 | self.user = Some(user.clone()); | 711 | self.user = Some(user.clone()); |
| 691 | self.accounts.rename(user); | 712 | self.accounts.rename(user); |
| 692 | - if !self.is_builder() && self.chat.view == View::Admin { | 713 | + if !self.is_builder() && self.chat.view == View::Builder { |
| 693 | self.chat.view = View::Chat; | 714 | self.chat.view = View::Chat; |
| 694 | } | 715 | } |
| 695 | } | 716 | } |
| @@ -746,7 +767,10 @@ impl Yumush { | |||
| 746 | } | 767 | } |
| 747 | 768 | ||
| 748 | pub(super) fn dismiss_announcement(&mut self, cx: &mut Context<Self>) { | 769 | pub(super) fn dismiss_announcement(&mut self, cx: &mut Context<Self>) { |
| 749 | - self.dismissed_announcement = self.server.as_ref().map(|settings| settings.announcement.clone()); | 770 | + self.dismissed_announcement = self |
| 771 | + .server | ||
| 772 | + .as_ref() | ||
| 773 | + .map(|settings| settings.announcement.clone()); | ||
| 750 | cx.notify(); | 774 | cx.notify(); |
| 751 | } | 775 | } |
| 752 | 776 | ||
crates/client/src/gui/admin.rsdeleted+0 -3224
Diff too large; view file
| @@ -1,3224 +0,0 @@ | |||
| 1 | -use std::future::Future; | ||
| 2 | - | ||
| 3 | -use super::text; | ||
| 4 | -use crate::network::NetworkHandle; | ||
| 5 | -use common::{ | ||
| 6 | - error::Error, | ||
| 7 | - model::{ | ||
| 8 | - Account, AdminAction, AdminLogEntry, AttachmentInfo, Community, CommunityKind, | ||
| 9 | - CommunitySummary, | ||
| 10 | - DayStat, InviteCode, Member, MessageHit, Rank, Report, ReportStatus, Role, ServerBan, | ||
| 11 | - ServerSettings, SessionInfo, User, action_detail, action_name, | ||
| 12 | - }, | ||
| 13 | - permission, | ||
| 14 | - time::{DAY_MILLIS, created_at_ms, now_ms}, | ||
| 15 | -}; | ||
| 16 | -use gpui_kit::{ | ||
| 17 | - AnyElement, App, ClipboardItem, Context, Div, Entity, FontWeight, InteractiveElement, | ||
| 18 | - IntoElement, ParentElement, StatefulInteractiveElement, Styled, Window, | ||
| 19 | - component::{ | ||
| 20 | - WindowExt, | ||
| 21 | - input::{Input, InputState}, | ||
| 22 | - notification::Notification, | ||
| 23 | - select::Select, | ||
| 24 | - }, | ||
| 25 | - div, | ||
| 26 | - prelude::FluentBuilder as _, | ||
| 27 | - px, | ||
| 28 | -}; | ||
| 29 | -use jiff::{Timestamp, civil::Date, tz::TimeZone}; | ||
| 30 | - | ||
| 31 | -use super::{ | ||
| 32 | - Palette, Yumush, | ||
| 33 | - auth::field, | ||
| 34 | - avatar, | ||
| 35 | - chat::View, | ||
| 36 | - clickable, community_icon, | ||
| 37 | - community_settings::{search_text, user_name}, | ||
| 38 | - entity_action, id_time, millis_time, notify_error, primary_button, | ||
| 39 | - settings_page::{page, row, section, segment, segment_track, segmented}, | ||
| 40 | -}; | ||
| 41 | - | ||
| 42 | -const ADMIN_TABS: [(AdminTab, &str); 10] = [ | ||
| 43 | - (AdminTab::Overview, "Overview"), | ||
| 44 | - (AdminTab::Users, "Users"), | ||
| 45 | - (AdminTab::Communities, "Communities"), | ||
| 46 | - (AdminTab::Messages, "Messages"), | ||
| 47 | - (AdminTab::Voice, "Voice"), | ||
| 48 | - (AdminTab::Storage, "Storage"), | ||
| 49 | - (AdminTab::Access, "Access"), | ||
| 50 | - (AdminTab::Reports, "Reports"), | ||
| 51 | - (AdminTab::Server, "Server"), | ||
| 52 | - (AdminTab::Log, "Log"), | ||
| 53 | -]; | ||
| 54 | - | ||
| 55 | -const REPORT_FILTERS: [(Option<ReportStatus>, &str); 4] = [ | ||
| 56 | - (Some(ReportStatus::Open), "Open"), | ||
| 57 | - (Some(ReportStatus::Resolved), "Resolved"), | ||
| 58 | - (Some(ReportStatus::Dismissed), "Dismissed"), | ||
| 59 | - (None, "All"), | ||
| 60 | -]; | ||
| 61 | - | ||
| 62 | -const KB: f64 = 1024.0; | ||
| 63 | -const MB: f64 = KB * 1024.0; | ||
| 64 | -const GB: f64 = MB * 1024.0; | ||
| 65 | - | ||
| 66 | -#[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
| 67 | -pub(super) enum AdminTab { | ||
| 68 | - Overview, | ||
| 69 | - Users, | ||
| 70 | - Communities, | ||
| 71 | - Messages, | ||
| 72 | - Voice, | ||
| 73 | - Storage, | ||
| 74 | - Access, | ||
| 75 | - Reports, | ||
| 76 | - Server, | ||
| 77 | - Log, | ||
| 78 | -} | ||
| 79 | - | ||
| 80 | -pub(super) struct Transfer { | ||
| 81 | - pub(super) community_id: String, | ||
| 82 | - pub(super) members: Vec<Member>, | ||
| 83 | - pub(super) pending: Option<String>, | ||
| 84 | -} | ||
| 85 | - | ||
| 86 | -fn storage(bytes: u64) -> String { | ||
| 87 | - let bytes = bytes as f64; | ||
| 88 | - if bytes >= GB { | ||
| 89 | - format!("{:.1} GB", bytes / GB) | ||
| 90 | - } else if bytes >= MB { | ||
| 91 | - format!("{:.1} MB", bytes / MB) | ||
| 92 | - } else { | ||
| 93 | - format!("{:.1} KB", bytes / KB) | ||
| 94 | - } | ||
| 95 | -} | ||
| 96 | - | ||
| 97 | -fn megabytes_bytes(text: &str) -> u64 { | ||
| 98 | - text.trim() | ||
| 99 | - .parse::<u64>() | ||
| 100 | - .ok() | ||
| 101 | - .and_then(|megabytes| megabytes.checked_mul(MB as u64)) | ||
| 102 | - .unwrap_or(0) | ||
| 103 | -} | ||
| 104 | - | ||
| 105 | -fn kilobytes_bytes(text: &str) -> u64 { | ||
| 106 | - text.trim() | ||
| 107 | - .parse::<u64>() | ||
| 108 | - .ok() | ||
| 109 | - .and_then(|kilobytes| kilobytes.checked_mul(KB as u64)) | ||
| 110 | - .unwrap_or(0) | ||
| 111 | -} | ||
| 112 | - | ||
| 113 | -fn number(text: &str) -> u32 { | ||
| 114 | - text.trim().parse().unwrap_or(0) | ||
| 115 | -} | ||
| 116 | - | ||
| 117 | -fn admin_log_text(entry: &AdminLogEntry) -> String { | ||
| 118 | - let actor = user_name(&entry.actor); | ||
| 119 | - let target = user_name(&entry.target); | ||
| 120 | - let community = entry.community.as_deref().unwrap_or_default(); | ||
| 121 | - | ||
| 122 | - match &entry.action { | ||
| 123 | - AdminAction::RoleChanged { role } => format!("{actor} set {target} to {}", role.as_str()), | ||
| 124 | - AdminAction::UserDeleted => format!("{actor} deleted {target}"), | ||
| 125 | - AdminAction::UserSuspended => format!("{actor} suspended {target}"), | ||
| 126 | - AdminAction::UserUnsuspended => format!("{actor} unsuspended {target}"), | ||
| 127 | - AdminAction::SessionsRevoked => format!("{actor} revoked sessions of {target}"), | ||
| 128 | - AdminAction::PasswordReset => format!("{actor} reset password of {target}"), | ||
| 129 | - AdminAction::ProfileCleared => format!("{actor} cleared profile of {target}"), | ||
| 130 | - AdminAction::UserCreated => format!("{actor} created {target}"), | ||
| 131 | - AdminAction::CommunityDeleted => format!("{actor} deleted {community}"), | ||
| 132 | - AdminAction::OwnershipTransferred => { | ||
| 133 | - format!("{actor} transferred {community} to {target}") | ||
| 134 | - } | ||
| 135 | - AdminAction::SettingsChanged => format!("{actor} changed settings"), | ||
| 136 | - AdminAction::UserRenamed { from } => format!("{actor} renamed {from} to {target}"), | ||
| 137 | - AdminAction::UserTimedOut { minutes: 0 } => format!("{actor} cleared timeout of {target}"), | ||
| 138 | - AdminAction::UserTimedOut { minutes } => { | ||
| 139 | - format!("{actor} timed out {target} for {minutes} min") | ||
| 140 | - } | ||
| 141 | - AdminAction::UserBanned { .. } => format!("{actor} banned {target}"), | ||
| 142 | - AdminAction::BanRemoved => format!("{actor} removed a ban"), | ||
| 143 | - AdminAction::InviteCreated => format!("{actor} created an invite code"), | ||
| 144 | - AdminAction::InviteDeleted => format!("{actor} deleted an invite code"), | ||
| 145 | - AdminAction::ReportHandled { status } => { | ||
| 146 | - format!("{actor} marked a report of {target} as {}", status.as_str()) | ||
| 147 | - } | ||
| 148 | - AdminAction::MessageDeleted => { | ||
| 149 | - format!("{actor} deleted a message of {target} in {community}") | ||
| 150 | - } | ||
| 151 | - AdminAction::UserPurged { messages } => { | ||
| 152 | - format!("{actor} purged {target}, {messages} messages") | ||
| 153 | - } | ||
| 154 | - AdminAction::VoiceKicked => format!("{actor} kicked {target} from voice"), | ||
| 155 | - AdminAction::VoiceMuted { muted: true } => format!("{actor} muted {target} in voice"), | ||
| 156 | - AdminAction::VoiceMuted { muted: false } => format!("{actor} unmuted {target} in voice"), | ||
| 157 | - AdminAction::CommunityVoiceSet { enabled: true } => { | ||
| 158 | - format!("{actor} enabled voice in {community}") | ||
| 159 | - } | ||
| 160 | - AdminAction::CommunityVoiceSet { enabled: false } => { | ||
| 161 | - format!("{actor} disabled voice in {community}") | ||
| 162 | - } | ||
| 163 | - AdminAction::AttachmentDeleted => { | ||
| 164 | - format!("{actor} deleted an attachment of {target} in {community}") | ||
| 165 | - } | ||
| 166 | - AdminAction::GhostEnabled => format!("{actor} enabled ghost mode"), | ||
| 167 | - AdminAction::GhostDisabled => format!("{actor} disabled ghost mode"), | ||
| 168 | - AdminAction::GhostVisit => format!("{actor} visited {community} as ghost"), | ||
| 169 | - AdminAction::GhostVoice { seconds } => { | ||
| 170 | - format!("{actor} listened in {community} as ghost for {seconds}s") | ||
| 171 | - } | ||
| 172 | - } | ||
| 173 | -} | ||
| 174 | - | ||
| 175 | -fn seen_text(last_seen: Option<u64>) -> String { | ||
| 176 | - let Some(last_seen) = last_seen else { | ||
| 177 | - return "never".to_string(); | ||
| 178 | - }; | ||
| 179 | - let minutes = now_ms().saturating_sub(last_seen) / 60_000; | ||
| 180 | - | ||
| 181 | - match minutes { | ||
| 182 | - 0 => "now".to_string(), | ||
| 183 | - 1..60 => format!("{minutes}m ago"), | ||
| 184 | - 60..1440 => format!("{}h ago", minutes / 60), | ||
| 185 | - _ => millis_time(last_seen, "%b %-d"), | ||
| 186 | - } | ||
| 187 | -} | ||
| 188 | - | ||
| 189 | -fn csv_field(text: &str) -> String { | ||
| 190 | - format!("\"{}\"", text.replace('"', "\"\"")) | ||
| 191 | -} | ||
| 192 | - | ||
| 193 | -fn admin_log_csv(entries: &[AdminLogEntry]) -> String { | ||
| 194 | - let mut lines = vec!["time,actor,action,target,community,detail".to_string()]; | ||
| 195 | - for entry in entries { | ||
| 196 | - let time = created_at_ms(&entry.id) | ||
| 197 | - .and_then(|millis| Timestamp::from_millisecond(millis as i64).ok()) | ||
| 198 | - .map(|timestamp| timestamp.to_string()) | ||
| 199 | - .unwrap_or_default(); | ||
| 200 | - let actor = entry | ||
| 201 | - .actor | ||
| 202 | - .as_ref() | ||
| 203 | - .map_or_else(String::new, |user| user.username.clone()); | ||
| 204 | - let target = entry | ||
| 205 | - .target | ||
| 206 | - .as_ref() | ||
| 207 | - .map_or_else(String::new, |user| user.username.clone()); | ||
| 208 | - let fields = [ | ||
| 209 | - time, | ||
| 210 | - actor, | ||
| 211 | - action_name(&entry.action).to_string(), | ||
| 212 | - target, | ||
| 213 | - entry.community.clone().unwrap_or_default(), | ||
| 214 | - action_detail(&entry.action).unwrap_or_default(), | ||
| 215 | - ]; | ||
| 216 | - lines.push( | ||
| 217 | - fields | ||
| 218 | - .iter() | ||
| 219 | - .map(|field| csv_field(field)) | ||
| 220 | - .collect::<Vec<_>>() | ||
| 221 | - .join(","), | ||
| 222 | - ); | ||
| 223 | - } | ||
| 224 | - | ||
| 225 | - lines.join("\n") | ||
| 226 | -} | ||
| 227 | - | ||
| 228 | -fn day_label(day: u64) -> String { | ||
| 229 | - Timestamp::from_millisecond((day * DAY_MILLIS) as i64) | ||
| 230 | - .map(|timestamp| timestamp.strftime("%b %-d").to_string()) | ||
| 231 | - .unwrap_or_default() | ||
| 232 | -} | ||
| 233 | - | ||
| 234 | -fn bar_chart( | ||
| 235 | - title: &'static str, | ||
| 236 | - stats: &[DayStat], | ||
| 237 | - value: fn(&DayStat) -> u64, | ||
| 238 | - palette: Palette, | ||
| 239 | -) -> Div { | ||
| 240 | - let values: Vec<u64> = stats.iter().map(value).collect(); | ||
| 241 | - let maximum = values.iter().copied().max().unwrap_or_default(); | ||
| 242 | - let first = stats.first().map(|stat| day_label(stat.day)).unwrap_or_default(); | ||
| 243 | - let last = stats.last().map(|stat| day_label(stat.day)).unwrap_or_default(); | ||
| 244 | - | ||
| 245 | - div() | ||
| 246 | - .flex() | ||
| 247 | - .flex_col() | ||
| 248 | - .gap(px(4.)) | ||
| 249 | - .child( | ||
| 250 | - div() | ||
| 251 | - .flex() | ||
| 252 | - .justify_between() | ||
| 253 | - .text_size(text(11.)) | ||
| 254 | - .text_color(palette.muted) | ||
| 255 | - .child(title) | ||
| 256 | - .child(maximum.to_string()), | ||
| 257 | - ) | ||
| 258 | - .child( | ||
| 259 | - div() | ||
| 260 | - .h(px(48.)) | ||
| 261 | - .flex() | ||
| 262 | - .items_end() | ||
| 263 | - .gap(px(2.)) | ||
| 264 | - .children(values.iter().map(|&value| { | ||
| 265 | - let height = if maximum == 0 { | ||
| 266 | - 0. | ||
| 267 | - } else { | ||
| 268 | - 48. * value as f32 / maximum as f32 | ||
| 269 | - }; | ||
| 270 | - | ||
| 271 | - div() | ||
| 272 | - .flex_1() | ||
| 273 | - .max_w(px(12.)) | ||
| 274 | - .h(px(height)) | ||
| 275 | - .rounded_t(px(2.)) | ||
| 276 | - .bg(palette.accent) | ||
| 277 | - })), | ||
| 278 | - ) | ||
| 279 | - .child( | ||
| 280 | - div() | ||
| 281 | - .flex() | ||
| 282 | - .justify_between() | ||
| 283 | - .text_size(text(10.)) | ||
| 284 | - .text_color(palette.muted) | ||
| 285 | - .child(first) | ||
| 286 | - .child(last), | ||
| 287 | - ) | ||
| 288 | -} | ||
| 289 | - | ||
| 290 | -fn admin_log_row(entry: &AdminLogEntry, palette: Palette) -> Div { | ||
| 291 | - div() | ||
| 292 | - .min_h(px(40.)) | ||
| 293 | - .px(px(12.)) | ||
| 294 | - .flex() | ||
| 295 | - .items_center() | ||
| 296 | - .gap(px(8.)) | ||
| 297 | - .child( | ||
| 298 | - div() | ||
| 299 | - .flex_1() | ||
| 300 | - .min_w_0() | ||
| 301 | - .truncate() | ||
| 302 | - .child(admin_log_text(entry)), | ||
| 303 | - ) | ||
| 304 | - .child( | ||
| 305 | - div() | ||
| 306 | - .flex_shrink_0() | ||
| 307 | - .text_size(text(11.)) | ||
| 308 | - .text_color(palette.muted) | ||
| 309 | - .child(id_time(&entry.id, "%b %-d, %H:%M")), | ||
| 310 | - ) | ||
| 311 | -} | ||
| 312 | - | ||
| 313 | -fn invite_row( | ||
| 314 | - invite: &InviteCode, | ||
| 315 | - pending: bool, | ||
| 316 | - entity: &Entity<Yumush>, | ||
| 317 | - palette: Palette, | ||
| 318 | -) -> Div { | ||
| 319 | - let copy_code = invite.code.clone(); | ||
| 320 | - let delete_code = invite.code.clone(); | ||
| 321 | - let expiry = invite.expires_at.map_or_else( | ||
| 322 | - || "no expiry".to_string(), | ||
| 323 | - |at| format!("until {}", millis_time(at, "%b %-d, %H:%M")), | ||
| 324 | - ); | ||
| 325 | - | ||
| 326 | - div() | ||
| 327 | - .min_h(px(44.)) | ||
| 328 | - .px(px(12.)) | ||
| 329 | - .flex() | ||
| 330 | - .items_center() | ||
| 331 | - .gap(px(8.)) | ||
| 332 | - .child( | ||
| 333 | - div() | ||
| 334 | - .flex_1() | ||
| 335 | - .min_w_0() | ||
| 336 | - .flex() | ||
| 337 | - .flex_col() | ||
| 338 | - .child( | ||
| 339 | - div() | ||
| 340 | - .font_weight(FontWeight::SEMIBOLD) | ||
| 341 | - .child(invite.code.clone()), | ||
| 342 | - ) | ||
| 343 | - .child( | ||
| 344 | - div() | ||
| 345 | - .text_size(text(11.)) | ||
| 346 | - .text_color(palette.muted) | ||
| 347 | - .truncate() | ||
| 348 | - .child(format!("{} left · {expiry}", invite.uses_left)), | ||
| 349 | - ), | ||
| 350 | - ) | ||
| 351 | - .child(admin_button( | ||
| 352 | - format!("copy_invite_{}", invite.code), | ||
| 353 | - "Copy", | ||
| 354 | - palette, | ||
| 355 | - entity_action(entity, move |yumush, _, cx| { | ||
| 356 | - yumush.copy_invite(©_code, cx) | ||
| 357 | - }), | ||
| 358 | - )) | ||
| 359 | - .child(danger_button( | ||
| 360 | - format!("delete_invite_{}", invite.code), | ||
| 361 | - "Delete", | ||
| 362 | - pending, | ||
| 363 | - palette, | ||
| 364 | - entity_action(entity, move |yumush, window, cx| { | ||
| 365 | - yumush.delete_invite(&delete_code, window, cx) | ||
| 366 | - }), | ||
| 367 | - )) | ||
| 368 | -} | ||
| 369 | - | ||
| 370 | -fn ban_row(ban: &ServerBan, entity: &Entity<Yumush>, palette: Palette) -> Div { | ||
| 371 | - let remove_id = ban.id.clone(); | ||
| 372 | - let detail = format!( | ||
| 373 | - "{} · {}", | ||
| 374 | - ban.kind.as_str(), | ||
| 375 | - ban.reason.as_deref().unwrap_or_default() | ||
| 376 | - ); | ||
| 377 | - | ||
| 378 | - div() | ||
| 379 | - .min_h(px(44.)) | ||
| 380 | - .px(px(12.)) | ||
| 381 | - .flex() | ||
| 382 | - .items_center() | ||
| 383 | - .gap(px(8.)) | ||
| 384 | - .child( | ||
| 385 | - div() | ||
| 386 | - .flex_1() | ||
| 387 | - .min_w_0() | ||
| 388 | - .flex() | ||
| 389 | - .flex_col() | ||
| 390 | - .child(div().truncate().child(ban.value.clone())) | ||
| 391 | - .child( | ||
| 392 | - div() | ||
| 393 | - .text_size(text(11.)) | ||
| 394 | - .text_color(palette.muted) | ||
| 395 | - .truncate() | ||
| 396 | - .child(detail), | ||
| 397 | - ), | ||
| 398 | - ) | ||
| 399 | - .child(admin_button( | ||
| 400 | - format!("remove_ban_{}", ban.id), | ||
| 401 | - "Remove", | ||
| 402 | - palette, | ||
| 403 | - entity_action(entity, move |yumush, window, cx| { | ||
| 404 | - yumush.remove_ban(&remove_id, window, cx) | ||
| 405 | - }), | ||
| 406 | - )) | ||
| 407 | -} | ||
| 408 | - | ||
| 409 | -fn danger_button( | ||
| 410 | - id: String, | ||
| 411 | - label: &'static str, | ||
| 412 | - pending: bool, | ||
| 413 | - palette: Palette, | ||
| 414 | - action: impl Fn(&mut Window, &mut App) + 'static, | ||
| 415 | -) -> impl IntoElement { | ||
| 416 | - clickable(id, palette, action) | ||
| 417 | - .h(px(28.)) | ||
| 418 | - .px(px(8.)) | ||
| 419 | - .flex() | ||
| 420 | - .items_center() | ||
| 421 | - .rounded(px(6.)) | ||
| 422 | - .text_size(text(13.)) | ||
| 423 | - .border_1() | ||
| 424 | - .border_color(palette.danger.opacity(0.5)) | ||
| 425 | - .text_color(palette.danger) | ||
| 426 | - .when(pending, |this| { | ||
| 427 | - this.bg(palette.danger).text_color(palette.on_accent) | ||
| 428 | - }) | ||
| 429 | - .child(if pending { "Confirm" } else { label }) | ||
| 430 | -} | ||
| 431 | - | ||
| 432 | -fn day_millis(text: &str, end_of_day: bool) -> Option<u64> { | ||
| 433 | - let date: Date = text.parse().ok()?; | ||
| 434 | - let date = if end_of_day { | ||
| 435 | - date.tomorrow().ok()? | ||
| 436 | - } else { | ||
| 437 | - date | ||
| 438 | - }; | ||
| 439 | - let millis = date | ||
| 440 | - .to_zoned_datetime(TimeZone::system()) | ||
| 441 | - .ok()? | ||
| 442 | - .timestamp() | ||
| 443 | - .as_millisecond(); | ||
| 444 | - | ||
| 445 | - u64::try_from(millis - i64::from(end_of_day)).ok() | ||
| 446 | -} | ||
| 447 | - | ||
| 448 | -fn message_hit_row( | ||
| 449 | - hit: &MessageHit, | ||
| 450 | - pending: bool, | ||
| 451 | - entity: &Entity<Yumush>, | ||
| 452 | - palette: Palette, | ||
| 453 | -) -> Div { | ||
| 454 | - let message_id = hit.message.id.clone(); | ||
| 455 | - | ||
| 456 | - div() | ||
| 457 | - .px(px(12.)) | ||
| 458 | - .py(px(6.)) | ||
| 459 | - .flex() | ||
| 460 | - .items_center() | ||
| 461 | - .gap(px(8.)) | ||
| 462 | - .child( | ||
| 463 | - div() | ||
| 464 | - .flex_1() | ||
| 465 | - .min_w_0() | ||
| 466 | - .flex() | ||
| 467 | - .flex_col() | ||
| 468 | - .child( | ||
| 469 | - div() | ||
| 470 | - .text_size(text(11.)) | ||
| 471 | - .text_color(palette.muted) | ||
| 472 | - .truncate() | ||
| 473 | - .child(format!( | ||
| 474 | - "{} · {} · {}", | ||
| 475 | - id_time(&hit.message.id, "%b %-d %Y, %H:%M"), | ||
| 476 | - hit.community, | ||
| 477 | - user_name(&hit.author) | ||
| 478 | - )), | ||
| 479 | - ) | ||
| 480 | - .child(div().truncate().child(hit.message.body.clone())), | ||
| 481 | - ) | ||
| 482 | - .child(danger_button( | ||
| 483 | - format!("delete_message_{}", hit.message.id), | ||
| 484 | - "Delete", | ||
| 485 | - pending, | ||
| 486 | - palette, | ||
| 487 | - entity_action(entity, move |yumush, window, cx| { | ||
| 488 | - yumush.delete_admin_message(&message_id, window, cx) | ||
| 489 | - }), | ||
| 490 | - )) | ||
| 491 | -} | ||
| 492 | - | ||
| 493 | -fn usage_row(name: &str, bytes: u64, count: u64, palette: Palette) -> Div { | ||
| 494 | - div() | ||
| 495 | - .px(px(12.)) | ||
| 496 | - .py(px(6.)) | ||
| 497 | - .flex() | ||
| 498 | - .items_center() | ||
| 499 | - .gap(px(8.)) | ||
| 500 | - .child( | ||
| 501 | - div() | ||
| 502 | - .flex_1() | ||
| 503 | - .min_w_0() | ||
| 504 | - .truncate() | ||
| 505 | - .child(name.to_string()), | ||
| 506 | - ) | ||
| 507 | - .child(div().text_size(text(13.)).child(storage(bytes))) | ||
| 508 | - .child( | ||
| 509 | - div() | ||
| 510 | - .w(px(60.)) | ||
| 511 | - .text_size(text(13.)) | ||
| 512 | - .text_color(palette.muted) | ||
| 513 | - .child(count.to_string()), | ||
| 514 | - ) | ||
| 515 | -} | ||
| 516 | - | ||
| 517 | -fn attachment_row( | ||
| 518 | - info: &AttachmentInfo, | ||
| 519 | - pending: bool, | ||
| 520 | - entity: &Entity<Yumush>, | ||
| 521 | - palette: Palette, | ||
| 522 | -) -> Div { | ||
| 523 | - let attachment_id = info.attachment.id.clone(); | ||
| 524 | - | ||
| 525 | - div() | ||
| 526 | - .px(px(12.)) | ||
| 527 | - .py(px(6.)) | ||
| 528 | - .flex() | ||
| 529 | - .items_center() | ||
| 530 | - .gap(px(8.)) | ||
| 531 | - .child( | ||
| 532 | - div() | ||
| 533 | - .flex_1() | ||
| 534 | - .min_w_0() | ||
| 535 | - .flex() | ||
| 536 | - .flex_col() | ||
| 537 | - .child(div().truncate().child(info.attachment.name.clone())) | ||
| 538 | - .child( | ||
| 539 | - div() | ||
| 540 | - .text_size(text(11.)) | ||
| 541 | - .text_color(palette.muted) | ||
| 542 | - .truncate() | ||
| 543 | - .child(format!( | ||
| 544 | - "{} · {} · {}", | ||
| 545 | - storage(info.attachment.size), | ||
| 546 | - user_name(&info.uploader), | ||
| 547 | - info.community | ||
| 548 | - )), | ||
| 549 | - ), | ||
| 550 | - ) | ||
| 551 | - .child(danger_button( | ||
| 552 | - format!("delete_attachment_{}", info.attachment.id), | ||
| 553 | - "Delete", | ||
| 554 | - pending, | ||
| 555 | - palette, | ||
| 556 | - entity_action(entity, move |yumush, window, cx| { | ||
| 557 | - yumush.delete_admin_attachment(&attachment_id, window, cx) | ||
| 558 | - }), | ||
| 559 | - )) | ||
| 560 | -} | ||
| 561 | - | ||
| 562 | -fn accent_button( | ||
| 563 | - id: &'static str, | ||
| 564 | - label: &'static str, | ||
| 565 | - palette: Palette, | ||
| 566 | - action: impl Fn(&mut Window, &mut App) + 'static, | ||
| 567 | -) -> impl IntoElement { | ||
| 568 | - primary_button(id, palette, action) | ||
| 569 | - .h(px(28.)) | ||
| 570 | - .px(px(12.)) | ||
| 571 | - .flex() | ||
| 572 | - .items_center() | ||
| 573 | - .rounded(px(6.)) | ||
| 574 | - .text_size(text(13.)) | ||
| 575 | - .child(label) | ||
| 576 | -} | ||
| 577 | - | ||
| 578 | -fn admin_button( | ||
| 579 | - id: String, | ||
| 580 | - label: &'static str, | ||
| 581 | - palette: Palette, | ||
| 582 | - action: impl Fn(&mut Window, &mut App) + 'static, | ||
| 583 | -) -> impl IntoElement { | ||
| 584 | - clickable(id, palette, action) | ||
| 585 | - .h(px(28.)) | ||
| 586 | - .px(px(8.)) | ||
| 587 | - .flex() | ||
| 588 | - .items_center() | ||
| 589 | - .rounded(px(6.)) | ||
| 590 | - .text_size(text(13.)) | ||
| 591 | - .text_color(palette.muted) | ||
| 592 | - .child(label) | ||
| 593 | -} | ||
| 594 | - | ||
| 595 | -impl Yumush { | ||
| 596 | - pub(super) fn is_builder(&self) -> bool { | ||
| 597 | - self.user | ||
| 598 | - .as_ref() | ||
| 599 | - .is_some_and(|me| me.role == Role::Builder) | ||
| 600 | - } | ||
| 601 | - | ||
| 602 | - pub(super) fn load_admin(&self, window: &mut Window, cx: &mut Context<Self>) { | ||
| 603 | - self.load_open_reports(window, cx); | ||
| 604 | - match self.chat.admin_tab { | ||
| 605 | - AdminTab::Overview => self.load_overview(window, cx), | ||
| 606 | - AdminTab::Users => self.load_users(window, cx), | ||
| 607 | - AdminTab::Communities => self.load_admin_communities(window, cx), | ||
| 608 | - AdminTab::Messages => {} | ||
| 609 | - AdminTab::Storage => { | ||
| 610 | - self.load_users(window, cx); | ||
| 611 | - self.load_admin_communities(window, cx); | ||
| 612 | - self.load_storage(window, cx); | ||
| 613 | - } | ||
| 614 | - AdminTab::Voice => self.load_voice_rooms(window, cx), | ||
| 615 | - AdminTab::Access => self.load_access(window, cx), | ||
| 616 | - AdminTab::Reports => self.load_reports(window, cx), | ||
| 617 | - AdminTab::Server => {} | ||
| 618 | - AdminTab::Log => { | ||
| 619 | - self.load_users(window, cx); | ||
| 620 | - self.load_admin_logs(window, cx); | ||
| 621 | - } | ||
| 622 | - } | ||
| 623 | - } | ||
| 624 | - | ||
| 625 | - pub(super) fn report_created(&self, window: &mut Window, cx: &mut Context<Self>) { | ||
| 626 | - self.load_open_reports(window, cx); | ||
| 627 | - if self.chat.view == View::Admin && self.chat.admin_tab == AdminTab::Reports { | ||
| 628 | - self.load_reports(window, cx); | ||
| 629 | - } | ||
| 630 | - } | ||
| 631 | - | ||
| 632 | - fn load_open_reports(&self, window: &mut Window, cx: &mut Context<Self>) { | ||
| 633 | - if !self.is_builder() { | ||
| 634 | - return; | ||
| 635 | - } | ||
| 636 | - let network = self.network.clone(); | ||
| 637 | - | ||
| 638 | - cx.spawn_in(window, async move |this, cx| { | ||
| 639 | - let count = match network.reports(Some(ReportStatus::Open)).await { | ||
| 640 | - Ok(reports) => reports.len(), | ||
| 641 | - Err(error) => { | ||
| 642 | - notify_error(&this, cx, error); | ||
| 643 | - return; | ||
| 644 | - } | ||
| 645 | - }; | ||
| 646 | - | ||
| 647 | - let _ = this.update(cx, |yumush, cx| { | ||
| 648 | - yumush.chat.open_reports = count; | ||
| 649 | - cx.notify(); | ||
| 650 | - }); | ||
| 651 | - }) | ||
| 652 | - .detach(); | ||
| 653 | - } | ||
| 654 | - | ||
| 655 | - fn load_reports(&self, window: &mut Window, cx: &mut Context<Self>) { | ||
| 656 | - if !self.is_builder() { | ||
| 657 | - return; | ||
| 658 | - } | ||
| 659 | - let status = self.chat.report_filter; | ||
| 660 | - let network = self.network.clone(); | ||
| 661 | - | ||
| 662 | - cx.spawn_in(window, async move |this, cx| { | ||
| 663 | - let reports = match network.reports(status).await { | ||
| 664 | - Ok(reports) => reports, | ||
| 665 | - Err(error) => { | ||
| 666 | - notify_error(&this, cx, error); | ||
| 667 | - return; | ||
| 668 | - } | ||
| 669 | - }; | ||
| 670 | - | ||
| 671 | - let _ = this.update(cx, |yumush, cx| { | ||
| 672 | - yumush.chat.admin_reports = reports; | ||
| 673 | - cx.notify(); | ||
| 674 | - }); | ||
| 675 | - }) | ||
| 676 | - .detach(); | ||
| 677 | - } | ||
| 678 | - | ||
| 679 | - fn refresh_reports(&self, window: &mut Window, cx: &mut Context<Self>) { | ||
| 680 | - self.load_reports(window, cx); | ||
| 681 | - self.load_open_reports(window, cx); | ||
| 682 | - } | ||
| 683 | - | ||
| 684 | - fn select_report_filter( | ||
| 685 | - &mut self, | ||
| 686 | - status: Option<ReportStatus>, | ||
| 687 | - window: &mut Window, | ||
| 688 | - cx: &mut Context<Self>, | ||
| 689 | - ) { | ||
| 690 | - self.chat.report_filter = status; | ||
| 691 | - self.load_reports(window, cx); | ||
| 692 | - cx.notify(); | ||
| 693 | - } | ||
| 694 | - | ||
| 695 | - fn report_action<Fut>( | ||
| 696 | - &mut self, | ||
| 697 | - window: &mut Window, | ||
| 698 | - cx: &mut Context<Self>, | ||
| 699 | - action: impl FnOnce(NetworkHandle) -> Fut + 'static, | ||
| 700 | - ) where | ||
| 701 | - Fut: Future<Output = Result<(), Error>> + 'static, | ||
| 702 | - { | ||
| 703 | - let network = self.network.clone(); | ||
| 704 | - | ||
| 705 | - cx.spawn_in(window, async move |this, cx| match action(network).await { | ||
| 706 | - Ok(()) => { | ||
| 707 | - let _ = this.update_in(cx, |yumush, window, cx| yumush.refresh_reports(window, cx)); | ||
| 708 | - } | ||
| 709 | - Err(error) => notify_error(&this, cx, error), | ||
| 710 | - }) | ||
| 711 | - .detach(); | ||
| 712 | - } | ||
| 713 | - | ||
| 714 | - fn handle_report( | ||
| 715 | - &mut self, | ||
| 716 | - report_id: &str, | ||
| 717 | - status: ReportStatus, | ||
| 718 | - window: &mut Window, | ||
| 719 | - cx: &mut Context<Self>, | ||
| 720 | - ) { | ||
| 721 | - let report_id = report_id.to_string(); | ||
| 722 | - self.report_action(window, cx, move |network| async move { | ||
| 723 | - network.handle_report(&report_id, status).await | ||
| 724 | - }); | ||
| 725 | - } | ||
| 726 | - | ||
| 727 | - fn delete_report_message( | ||
| 728 | - &mut self, | ||
| 729 | - message_id: &str, | ||
| 730 | - window: &mut Window, | ||
| 731 | - cx: &mut Context<Self>, | ||
| 732 | - ) { | ||
| 733 | - let message_id = message_id.to_string(); | ||
| 734 | - self.report_action(window, cx, move |network| async move { | ||
| 735 | - network.delete_message(&message_id).await | ||
| 736 | - }); | ||
| 737 | - } | ||
| 738 | - | ||
| 739 | - fn timeout_report_target( | ||
| 740 | - &mut self, | ||
| 741 | - user_id: &str, | ||
| 742 | - window: &mut Window, | ||
| 743 | - cx: &mut Context<Self>, | ||
| 744 | - ) { | ||
| 745 | - let user_id = user_id.to_string(); | ||
| 746 | - self.report_action(window, cx, move |network| async move { | ||
| 747 | - network.timeout_user(&user_id, 60).await | ||
| 748 | - }); | ||
| 749 | - } | ||
| 750 | - | ||
| 751 | - fn ban_report_target( | ||
| 752 | - &mut self, | ||
| 753 | - user_id: &str, | ||
| 754 | - reason: &str, | ||
| 755 | - window: &mut Window, | ||
| 756 | - cx: &mut Context<Self>, | ||
| 757 | - ) { | ||
| 758 | - let user_id = user_id.to_string(); | ||
| 759 | - let reason = reason.to_string(); | ||
| 760 | - self.report_action(window, cx, move |network| async move { | ||
| 761 | - network | ||
| 762 | - .ban_user(&user_id, Some(reason.as_str()), false) | ||
| 763 | - .await | ||
| 764 | - }); | ||
| 765 | - } | ||
| 766 | - | ||
| 767 | - fn select_admin_tab(&mut self, tab: AdminTab, window: &mut Window, cx: &mut Context<Self>) { | ||
| 768 | - self.chat.admin_tab = tab; | ||
| 769 | - if tab == AdminTab::Server { | ||
| 770 | - self.fill_server_form(window, cx); | ||
| 771 | - } | ||
| 772 | - self.load_admin(window, cx); | ||
| 773 | - cx.notify(); | ||
| 774 | - } | ||
| 775 | - | ||
| 776 | - fn fill_server_form(&mut self, window: &mut Window, cx: &mut Context<Self>) { | ||
| 777 | - let Some(settings) = self.server.clone() else { | ||
| 778 | - return; | ||
| 779 | - }; | ||
| 780 | - self.chat.server_registration_open = settings.registration_open; | ||
| 781 | - self.chat.server_maintenance = settings.maintenance; | ||
| 782 | - self.chat.server_name.update(cx, |input, cx| { | ||
| 783 | - input.set_value(settings.name.as_str(), window, cx) | ||
| 784 | - }); | ||
| 785 | - self.chat.server_limit.update(cx, |input, cx| { | ||
| 786 | - input.set_value( | ||
| 787 | - (settings.max_attachment_bytes / MB as u64) | ||
| 788 | - .to_string() | ||
| 789 | - .as_str(), | ||
| 790 | - window, | ||
| 791 | - cx, | ||
| 792 | - ) | ||
| 793 | - }); | ||
| 794 | - self.chat.server_user_quota.update(cx, |input, cx| { | ||
| 795 | - input.set_value( | ||
| 796 | - (settings.user_quota_bytes / MB as u64) | ||
| 797 | - .to_string() | ||
| 798 | - .as_str(), | ||
| 799 | - window, | ||
| 800 | - cx, | ||
| 801 | - ) | ||
| 802 | - }); | ||
| 803 | - self.chat.server_community_quota.update(cx, |input, cx| { | ||
| 804 | - input.set_value( | ||
| 805 | - (settings.community_quota_bytes / MB as u64) | ||
| 806 | - .to_string() | ||
| 807 | - .as_str(), | ||
| 808 | - window, | ||
| 809 | - cx, | ||
| 810 | - ) | ||
| 811 | - }); | ||
| 812 | - self.chat.server_message_retention.update(cx, |input, cx| { | ||
| 813 | - input.set_value(settings.message_retention_days.to_string().as_str(), window, cx) | ||
| 814 | - }); | ||
| 815 | - self.chat | ||
| 816 | - .server_attachment_retention | ||
| 817 | - .update(cx, |input, cx| { | ||
| 818 | - input.set_value( | ||
| 819 | - settings.attachment_retention_days.to_string().as_str(), | ||
| 820 | - window, | ||
| 821 | - cx, | ||
| 822 | - ) | ||
| 823 | - }); | ||
| 824 | - self.chat.server_announcement.update(cx, |input, cx| { | ||
| 825 | - input.set_value(settings.announcement.as_str(), window, cx) | ||
| 826 | - }); | ||
| 827 | - self.chat.server_max_communities.update(cx, |input, cx| { | ||
| 828 | - input.set_value(settings.max_communities_per_user.to_string().as_str(), window, cx) | ||
| 829 | - }); | ||
| 830 | - self.chat.server_max_message_length.update(cx, |input, cx| { | ||
| 831 | - input.set_value(settings.max_message_length.to_string().as_str(), window, cx) | ||
| 832 | - }); | ||
| 833 | - self.chat.server_max_avatar.update(cx, |input, cx| { | ||
| 834 | - input.set_value( | ||
| 835 | - (settings.max_avatar_bytes / KB as u64).to_string().as_str(), | ||
| 836 | - window, | ||
| 837 | - cx, | ||
| 838 | - ) | ||
| 839 | - }); | ||
| 840 | - self.chat.server_messages_per_minute.update(cx, |input, cx| { | ||
| 841 | - input.set_value(settings.messages_per_minute.to_string().as_str(), window, cx) | ||
| 842 | - }); | ||
| 843 | - self.chat.server_logins_per_minute.update(cx, |input, cx| { | ||
| 844 | - input.set_value(settings.logins_per_minute.to_string().as_str(), window, cx) | ||
| 845 | - }); | ||
| 846 | - } | ||
| 847 | - | ||
| 848 | - fn save_server_settings(&mut self, window: &mut Window, cx: &mut Context<Self>) { | ||
| 849 | - let name = self.chat.server_name.read(cx).text().to_string(); | ||
| 850 | - let max_attachment_bytes = | ||
| 851 | - megabytes_bytes(&self.chat.server_limit.read(cx).text().to_string()); | ||
| 852 | - let user_quota_bytes = | ||
| 853 | - megabytes_bytes(&self.chat.server_user_quota.read(cx).text().to_string()); | ||
| 854 | - let community_quota_bytes = | ||
| 855 | - megabytes_bytes(&self.chat.server_community_quota.read(cx).text().to_string()); | ||
| 856 | - let message_retention_days = | ||
| 857 | - number(&self.chat.server_message_retention.read(cx).text().to_string()); | ||
| 858 | - let attachment_retention_days = | ||
| 859 | - number(&self.chat.server_attachment_retention.read(cx).text().to_string()); | ||
| 860 | - let announcement = self.chat.server_announcement.read(cx).text().to_string(); | ||
| 861 | - let max_communities_per_user = | ||
| 862 | - number(&self.chat.server_max_communities.read(cx).text().to_string()); | ||
| 863 | - let max_message_length = | ||
| 864 | - number(&self.chat.server_max_message_length.read(cx).text().to_string()); | ||
| 865 | - let max_avatar_bytes = kilobytes_bytes(&self.chat.server_max_avatar.read(cx).text().to_string()); | ||
| 866 | - let messages_per_minute = | ||
| 867 | - number(&self.chat.server_messages_per_minute.read(cx).text().to_string()); | ||
| 868 | - let logins_per_minute = | ||
| 869 | - number(&self.chat.server_logins_per_minute.read(cx).text().to_string()); | ||
| 870 | - let settings = ServerSettings { | ||
| 871 | - name, | ||
| 872 | - registration_open: self.chat.server_registration_open, | ||
| 873 | - max_attachment_bytes, | ||
| 874 | - maintenance: self.chat.server_maintenance, | ||
| 875 | - user_quota_bytes, | ||
| 876 | - community_quota_bytes, | ||
| 877 | - message_retention_days, | ||
| 878 | - attachment_retention_days, | ||
| 879 | - announcement, | ||
| 880 | - max_communities_per_user, | ||
| 881 | - max_message_length, | ||
| 882 | - max_avatar_bytes, | ||
| 883 | - messages_per_minute, | ||
| 884 | - logins_per_minute, | ||
| 885 | - }; | ||
| 886 | - let network = self.network.clone(); | ||
| 887 | - | ||
| 888 | - cx.spawn_in(window, async move |this, cx| { | ||
| 889 | - match network.update_server_settings(settings).await { | ||
| 890 | - Ok(settings) => { | ||
| 891 | - let _ = this.update(cx, |yumush, cx| { | ||
| 892 | - yumush.server = Some(settings); | ||
| 893 | - cx.notify(); | ||
| 894 | - }); | ||
| 895 | - } | ||
| 896 | - Err(error) => notify_error(&this, cx, error), | ||
| 897 | - } | ||
| 898 | - }) | ||
| 899 | - .detach(); | ||
| 900 | - } | ||
| 901 | - | ||
| 902 | - fn submit_new_user(&mut self, window: &mut Window, cx: &mut Context<Self>) { | ||
| 903 | - let username = self.chat.new_username.read(cx).text().to_string(); | ||
| 904 | - let password = self.chat.new_password.read(cx).text().to_string(); | ||
| 905 | - self.chat | ||
| 906 | - .new_password | ||
| 907 | - .update(cx, |input, cx| input.set_value("", window, cx)); | ||
| 908 | - let network = self.network.clone(); | ||
| 909 | - | ||
| 910 | - cx.spawn_in(window, async move |this, cx| { | ||
| 911 | - match network.create_user(&username, &password).await { | ||
| 912 | - Ok(_) => { | ||
| 913 | - let _ = this.update_in(cx, |yumush, window, cx| { | ||
| 914 | - yumush | ||
| 915 | - .chat | ||
| 916 | - .new_username | ||
| 917 | - .update(cx, |input, cx| input.set_value("", window, cx)); | ||
| 918 | - yumush.load_users(window, cx); | ||
| 919 | - }); | ||
| 920 | - } | ||
| 921 | - Err(error) => notify_error(&this, cx, error), | ||
| 922 | - } | ||
| 923 | - }) | ||
| 924 | - .detach(); | ||
| 925 | - } | ||
| 926 | - | ||
| 927 | - fn load_overview(&self, window: &mut Window, cx: &mut Context<Self>) { | ||
| 928 | - if !self.is_builder() { | ||
| 929 | - return; | ||
| 930 | - } | ||
| 931 | - let network = self.network.clone(); | ||
| 932 | - | ||
| 933 | - cx.spawn_in(window, async move |this, cx| { | ||
| 934 | - let loaded = async { | ||
| 935 | - Ok::<_, Error>((network.overview().await?, network.daily_stats(30).await?)) | ||
| 936 | - } | ||
| 937 | - .await; | ||
| 938 | - let (overview, stats) = match loaded { | ||
| 939 | - Ok(loaded) => loaded, | ||
| 940 | - Err(error) => { | ||
| 941 | - notify_error(&this, cx, error); | ||
| 942 | - return; | ||
| 943 | - } | ||
| 944 | - }; | ||
| 945 | - | ||
| 946 | - let _ = this.update(cx, |yumush, cx| { | ||
| 947 | - yumush.chat.admin_overview = Some(overview); | ||
| 948 | - yumush.chat.admin_stats = stats; | ||
| 949 | - cx.notify(); | ||
| 950 | - }); | ||
| 951 | - }) | ||
| 952 | - .detach(); | ||
| 953 | - } | ||
| 954 | - | ||
| 955 | - fn load_admin_logs(&self, window: &mut Window, cx: &mut Context<Self>) { | ||
| 956 | - if !self.is_builder() { | ||
| 957 | - return; | ||
| 958 | - } | ||
| 959 | - let (Some(actor_id), Some(target_id)) = ( | ||
| 960 | - self.log_user(&self.chat.admin_log_actor, cx), | ||
| 961 | - self.log_user(&self.chat.admin_log_target, cx), | ||
| 962 | - ) else { | ||
| 963 | - window.push_notification(Notification::error("Unknown user"), cx); | ||
| 964 | - return; | ||
| 965 | - }; | ||
| 966 | - let action = self | ||
| 967 | - .chat | ||
| 968 | - .admin_log_action | ||
| 969 | - .read(cx) | ||
| 970 | - .selected_value() | ||
| 971 | - .copied() | ||
| 972 | - .filter(|name| *name != "All") | ||
| 973 | - .map(str::to_string); | ||
| 974 | - let after_ms = day_millis(&search_text(&self.chat.admin_log_from, cx), false); | ||
| 975 | - let before_ms = day_millis(&search_text(&self.chat.admin_log_to, cx), true); | ||
| 976 | - let network = self.network.clone(); | ||
| 977 | - | ||
| 978 | - cx.spawn_in(window, async move |this, cx| { | ||
| 979 | - let entries = match network | ||
| 980 | - .admin_logs(actor_id, target_id, action, after_ms, before_ms) | ||
| 981 | - .await | ||
| 982 | - { | ||
| 983 | - Ok(entries) => entries, | ||
| 984 | - Err(error) => { | ||
| 985 | - notify_error(&this, cx, error); | ||
| 986 | - return; | ||
| 987 | - } | ||
| 988 | - }; | ||
| 989 | - | ||
| 990 | - let _ = this.update(cx, |yumush, cx| { | ||
| 991 | - yumush.chat.admin_logs = entries; | ||
| 992 | - cx.notify(); | ||
| 993 | - }); | ||
| 994 | - }) | ||
| 995 | - .detach(); | ||
| 996 | - } | ||
| 997 | - | ||
| 998 | - pub(super) fn load_users(&self, window: &mut Window, cx: &mut Context<Self>) { | ||
| 999 | - if !self.is_builder() { | ||
| 1000 | - return; | ||
| 1001 | - } | ||
| 1002 | - let network = self.network.clone(); | ||
| 1003 | - | ||
| 1004 | - cx.spawn_in(window, async move |this, cx| { | ||
| 1005 | - let accounts = match network.users().await { | ||
| 1006 | - Ok(accounts) => accounts, | ||
| 1007 | - Err(error) => { | ||
| 1008 | - notify_error(&this, cx, error); | ||
| 1009 | - return; | ||
| 1010 | - } | ||
| 1011 | - }; | ||
| 1012 | - | ||
| 1013 | - let _ = this.update(cx, |yumush, cx| { | ||
| 1014 | - for account in &accounts { | ||
| 1015 | - yumush.request_user_avatar(&account.user, cx); | ||
| 1016 | - } | ||
| 1017 | - yumush.chat.ghost = yumush | ||
| 1018 | - .user | ||
| 1019 | - .as_ref() | ||
| 1020 | - .and_then(|me| accounts.iter().find(|account| account.user.id == me.id)) | ||
| 1021 | - .is_some_and(|account| account.ghost); | ||
| 1022 | - yumush.chat.admin_users = accounts; | ||
| 1023 | - cx.notify(); | ||
| 1024 | - }); | ||
| 1025 | - }) | ||
| 1026 | - .detach(); | ||
| 1027 | - } | ||
| 1028 | - | ||
| 1029 | - pub(super) fn voice_changed(&self, window: &mut Window, cx: &mut Context<Self>) { | ||
| 1030 | - if self.chat.view == View::Admin && self.chat.admin_tab == AdminTab::Voice { | ||
| 1031 | - self.load_voice_rooms(window, cx); | ||
| 1032 | - } | ||
| 1033 | - } | ||
| 1034 | - | ||
| 1035 | - fn load_voice_rooms(&self, window: &mut Window, cx: &mut Context<Self>) { | ||
| 1036 | - if !self.is_builder() { | ||
| 1037 | - return; | ||
| 1038 | - } | ||
| 1039 | - let network = self.network.clone(); | ||
| 1040 | - | ||
| 1041 | - cx.spawn_in(window, async move |this, cx| { | ||
| 1042 | - let rooms = match network.voice_rooms().await { | ||
| 1043 | - Ok(rooms) => rooms, | ||
| 1044 | - Err(error) => { | ||
| 1045 | - notify_error(&this, cx, error); | ||
| 1046 | - return; | ||
| 1047 | - } | ||
| 1048 | - }; | ||
| 1049 | - | ||
| 1050 | - let _ = this.update(cx, |yumush, cx| { | ||
| 1051 | - yumush.chat.admin_voice_rooms = rooms; | ||
| 1052 | - cx.notify(); | ||
| 1053 | - }); | ||
| 1054 | - }) | ||
| 1055 | - .detach(); | ||
| 1056 | - } | ||
| 1057 | - | ||
| 1058 | - fn voice_action<Fut>( | ||
| 1059 | - &mut self, | ||
| 1060 | - window: &mut Window, | ||
| 1061 | - cx: &mut Context<Self>, | ||
| 1062 | - action: impl FnOnce(NetworkHandle) -> Fut + 'static, | ||
| 1063 | - ) where | ||
| 1064 | - Fut: Future<Output = Result<(), Error>> + 'static, | ||
| 1065 | - { | ||
| 1066 | - let network = self.network.clone(); | ||
| 1067 | - | ||
| 1068 | - cx.spawn_in(window, async move |this, cx| match action(network).await { | ||
| 1069 | - Ok(()) => { | ||
| 1070 | - let _ = | ||
| 1071 | - this.update_in(cx, |yumush, window, cx| yumush.load_voice_rooms(window, cx)); | ||
| 1072 | - } | ||
| 1073 | - Err(error) => notify_error(&this, cx, error), | ||
| 1074 | - }) | ||
| 1075 | - .detach(); | ||
| 1076 | - } | ||
| 1077 | - | ||
| 1078 | - fn set_server_mute( | ||
| 1079 | - &mut self, | ||
| 1080 | - user_id: &str, | ||
| 1081 | - muted: bool, | ||
| 1082 | - window: &mut Window, | ||
| 1083 | - cx: &mut Context<Self>, | ||
| 1084 | - ) { | ||
| 1085 | - let user_id = user_id.to_string(); | ||
| 1086 | - self.voice_action(window, cx, move |network| async move { | ||
| 1087 | - network.server_mute(&user_id, muted).await | ||
| 1088 | - }); | ||
| 1089 | - } | ||
| 1090 | - | ||
| 1091 | - fn kick_from_voice(&mut self, user_id: &str, window: &mut Window, cx: &mut Context<Self>) { | ||
| 1092 | - let user_id = user_id.to_string(); | ||
| 1093 | - self.voice_action(window, cx, move |network| async move { | ||
| 1094 | - network.kick_from_voice(&user_id).await | ||
| 1095 | - }); | ||
| 1096 | - } | ||
| 1097 | - | ||
| 1098 | - fn set_admin_community_voice( | ||
| 1099 | - &mut self, | ||
| 1100 | - community_id: &str, | ||
| 1101 | - enabled: bool, | ||
| 1102 | - window: &mut Window, | ||
| 1103 | - cx: &mut Context<Self>, | ||
| 1104 | - ) { | ||
| 1105 | - let network = self.network.clone(); | ||
| 1106 | - let community_id = community_id.to_string(); | ||
| 1107 | - | ||
| 1108 | - cx.spawn_in(window, async move |this, cx| { | ||
| 1109 | - match network.set_community_voice(&community_id, enabled).await { | ||
| 1110 | - Ok(_) => { | ||
| 1111 | - let _ = this.update_in(cx, |yumush, window, cx| { | ||
| 1112 | - yumush.load_admin_communities(window, cx) | ||
| 1113 | - }); | ||
| 1114 | - } | ||
| 1115 | - Err(error) => notify_error(&this, cx, error), | ||
| 1116 | - } | ||
| 1117 | - }) | ||
| 1118 | - .detach(); | ||
| 1119 | - } | ||
| 1120 | - | ||
| 1121 | - fn load_admin_communities(&self, window: &mut Window, cx: &mut Context<Self>) { | ||
| 1122 | - if !self.is_builder() { | ||
| 1123 | - return; | ||
| 1124 | - } | ||
| 1125 | - let network = self.network.clone(); | ||
| 1126 | - | ||
| 1127 | - cx.spawn_in(window, async move |this, cx| { | ||
| 1128 | - let summaries = match network.all_communities().await { | ||
| 1129 | - Ok(summaries) => summaries, | ||
| 1130 | - Err(error) => { | ||
| 1131 | - notify_error(&this, cx, error); | ||
| 1132 | - return; | ||
| 1133 | - } | ||
| 1134 | - }; | ||
| 1135 | - | ||
| 1136 | - let _ = this.update(cx, |yumush, cx| { | ||
| 1137 | - for summary in &summaries { | ||
| 1138 | - if let Some(avatar_id) = &summary.community.avatar_id { | ||
| 1139 | - yumush.request_avatar(avatar_id, cx); | ||
| 1140 | - } | ||
| 1141 | - if let Some(owner) = &summary.owner { | ||
| 1142 | - yumush.request_user_avatar(owner, cx); | ||
| 1143 | - } | ||
| 1144 | - } | ||
| 1145 | - yumush.chat.admin_communities = summaries; | ||
| 1146 | - cx.notify(); | ||
| 1147 | - }); | ||
| 1148 | - }) | ||
| 1149 | - .detach(); | ||
| 1150 | - } | ||
| 1151 | - | ||
| 1152 | - fn delete_admin_community( | ||
| 1153 | - &mut self, | ||
| 1154 | - community_id: &str, | ||
| 1155 | - window: &mut Window, | ||
| 1156 | - cx: &mut Context<Self>, | ||
| 1157 | - ) { | ||
| 1158 | - if self.chat.pending_community_delete.as_deref() != Some(community_id) { | ||
| 1159 | - self.chat.pending_community_delete = Some(community_id.to_string()); | ||
| 1160 | - cx.notify(); | ||
| 1161 | - return; | ||
| 1162 | - } | ||
| 1163 | - self.chat.pending_community_delete = None; | ||
| 1164 | - let network = self.network.clone(); | ||
| 1165 | - let community_id = community_id.to_string(); | ||
| 1166 | - | ||
| 1167 | - cx.spawn_in(window, async move |this, cx| { | ||
| 1168 | - match network.delete_community(&community_id).await { | ||
| 1169 | - Ok(()) => { | ||
| 1170 | - let _ = this.update_in(cx, |yumush, window, cx| { | ||
| 1171 | - yumush.chat.remove_community(&community_id); | ||
| 1172 | - yumush.load_admin_communities(window, cx); | ||
| 1173 | - }); | ||
| 1174 | - } | ||
| 1175 | - Err(error) => notify_error(&this, cx, error), | ||
| 1176 | - } | ||
| 1177 | - }) | ||
| 1178 | - .detach(); | ||
| 1179 | - } | ||
| 1180 | - | ||
| 1181 | - fn open_transfer(&mut self, community_id: String, window: &mut Window, cx: &mut Context<Self>) { | ||
| 1182 | - self.chat.transfer = Some(Transfer { | ||
| 1183 | - community_id: community_id.clone(), | ||
| 1184 | - members: Vec::new(), | ||
| 1185 | - pending: None, | ||
| 1186 | - }); | ||
| 1187 | - let entity = cx.entity(); | ||
| 1188 | - window.open_dialog(cx, move |dialog, _, cx| { | ||
| 1189 | - let yumush = entity.read(cx); | ||
| 1190 | - let palette = yumush.themed(cx); | ||
| 1191 | - let rows: Vec<AnyElement> = yumush.transfer_rows(&entity, palette); | ||
| 1192 | - | ||
| 1193 | - dialog.title("Transfer ownership").child( | ||
| 1194 | - div() | ||
| 1195 | - .id("transfer_scroll") | ||
| 1196 | - .max_h(px(360.)) | ||
| 1197 | - .overflow_y_scroll() | ||
| 1198 | - .child(div().flex().flex_col().children(rows)), | ||
| 1199 | - ) | ||
| 1200 | - }); | ||
| 1201 | - self.load_transfer_members(community_id, window, cx); | ||
| 1202 | - } | ||
| 1203 | - | ||
| 1204 | - fn load_transfer_members( | ||
| 1205 | - &self, | ||
| 1206 | - community_id: String, | ||
| 1207 | - window: &mut Window, | ||
| 1208 | - cx: &mut Context<Self>, | ||
| 1209 | - ) { | ||
| 1210 | - let network = self.network.clone(); | ||
| 1211 | - | ||
| 1212 | - cx.spawn_in(window, async move |this, cx| { | ||
| 1213 | - let members = match network.members(&community_id).await { | ||
| 1214 | - Ok(members) => members, | ||
| 1215 | - Err(error) => { | ||
| 1216 | - notify_error(&this, cx, error); | ||
| 1217 | - return; | ||
| 1218 | - } | ||
| 1219 | - }; | ||
| 1220 | - | ||
| 1221 | - let _ = this.update_in(cx, |yumush, window, cx| { | ||
| 1222 | - for member in &members { | ||
| 1223 | - yumush.request_user_avatar(&member.user, cx); | ||
| 1224 | - } | ||
| 1225 | - let Some(transfer) = yumush.chat.transfer.as_mut() else { | ||
| 1226 | - return; | ||
| 1227 | - }; | ||
| 1228 | - if transfer.community_id == community_id { | ||
| 1229 | - transfer.members = members; | ||
| 1230 | - window.refresh(); | ||
| 1231 | - cx.notify(); | ||
| 1232 | - } | ||
| 1233 | - }); | ||
| 1234 | - }) | ||
| 1235 | - .detach(); | ||
| 1236 | - } | ||
| 1237 | - | ||
| 1238 | - fn transfer_rows(&self, entity: &Entity<Self>, palette: Palette) -> Vec<AnyElement> { | ||
| 1239 | - let Some(transfer) = &self.chat.transfer else { | ||
| 1240 | - return Vec::new(); | ||
| 1241 | - }; | ||
| 1242 | - | ||
| 1243 | - transfer | ||
| 1244 | - .members | ||
| 1245 | - .iter() | ||
| 1246 | - .filter(|member| member.rank != Rank::Owner) | ||
| 1247 | - .map(|member| { | ||
| 1248 | - let pending = transfer.pending.as_deref() == Some(member.user.id.as_str()); | ||
| 1249 | - self.transfer_row(member, pending, entity, palette) | ||
| 1250 | - .into_any_element() | ||
| 1251 | - }) | ||
| 1252 | - .collect() | ||
| 1253 | - } | ||
| 1254 | - | ||
| 1255 | - fn transfer_to(&mut self, user_id: &str, window: &mut Window, cx: &mut Context<Self>) { | ||
| 1256 | - let Some(transfer) = self.chat.transfer.as_mut() else { | ||
| 1257 | - return; | ||
| 1258 | - }; | ||
| 1259 | - if transfer.pending.as_deref() != Some(user_id) { | ||
| 1260 | - transfer.pending = Some(user_id.to_string()); | ||
| 1261 | - window.refresh(); | ||
| 1262 | - cx.notify(); | ||
| 1263 | - return; | ||
| 1264 | - } | ||
| 1265 | - let community_id = transfer.community_id.clone(); | ||
| 1266 | - let network = self.network.clone(); | ||
| 1267 | - let user_id = user_id.to_string(); | ||
| 1268 | - | ||
| 1269 | - cx.spawn_in(window, async move |this, cx| { | ||
| 1270 | - match network.transfer_ownership(&community_id, &user_id).await { | ||
| 1271 | - Ok(()) => { | ||
| 1272 | - let _ = this.update_in(cx, |yumush, window, cx| { | ||
| 1273 | - yumush.chat.transfer = None; | ||
| 1274 | - window.close_dialog(cx); | ||
| 1275 | - yumush.load_admin_communities(window, cx); | ||
| 1276 | - }); | ||
| 1277 | - } | ||
| 1278 | - Err(error) => notify_error(&this, cx, error), | ||
| 1279 | - } | ||
| 1280 | - }) | ||
| 1281 | - .detach(); | ||
| 1282 | - } | ||
| 1283 | - | ||
| 1284 | - fn set_admin_role( | ||
| 1285 | - &mut self, | ||
| 1286 | - user_id: &str, | ||
| 1287 | - role: Role, | ||
| 1288 | - window: &mut Window, | ||
| 1289 | - cx: &mut Context<Self>, | ||
| 1290 | - ) { | ||
| 1291 | - let network = self.network.clone(); | ||
| 1292 | - let user_id = user_id.to_string(); | ||
| 1293 | - | ||
| 1294 | - cx.spawn_in(window, async move |this, cx| { | ||
| 1295 | - match network.set_role(&user_id, role).await { | ||
| 1296 | - Ok(user) => { | ||
| 1297 | - let _ = this.update(cx, |yumush, cx| { | ||
| 1298 | - let index = yumush | ||
| 1299 | - .chat | ||
| 1300 | - .admin_users | ||
| 1301 | - .iter() | ||
| 1302 | - .position(|existing| existing.user.id == user.id); | ||
| 1303 | - if let Some(index) = index { | ||
| 1304 | - yumush.chat.admin_users[index].user = user; | ||
| 1305 | - } | ||
| 1306 | - cx.notify(); | ||
| 1307 | - }); | ||
| 1308 | - } | ||
| 1309 | - Err(error) => notify_error(&this, cx, error), | ||
| 1310 | - } | ||
| 1311 | - }) | ||
| 1312 | - .detach(); | ||
| 1313 | - } | ||
| 1314 | - | ||
| 1315 | - fn delete_admin_user(&mut self, user_id: &str, window: &mut Window, cx: &mut Context<Self>) { | ||
| 1316 | - if self.chat.pending_admin_delete.as_deref() != Some(user_id) { | ||
| 1317 | - self.chat.pending_admin_delete = Some(user_id.to_string()); | ||
| 1318 | - cx.notify(); | ||
| 1319 | - return; | ||
| 1320 | - } | ||
| 1321 | - self.chat.pending_admin_delete = None; | ||
| 1322 | - let network = self.network.clone(); | ||
| 1323 | - let user_id = user_id.to_string(); | ||
| 1324 | - | ||
| 1325 | - cx.spawn_in(window, async move |this, cx| { | ||
| 1326 | - match network.delete_user(&user_id).await { | ||
| 1327 | - Ok(()) => { | ||
| 1328 | - let _ = this.update(cx, |yumush, cx| { | ||
| 1329 | - yumush | ||
| 1330 | - .chat | ||
| 1331 | - .admin_users | ||
| 1332 | - .retain(|account| account.user.id != user_id); | ||
| 1333 | - cx.notify(); | ||
| 1334 | - }); | ||
| 1335 | - } | ||
| 1336 | - Err(error) => notify_error(&this, cx, error), | ||
| 1337 | - } | ||
| 1338 | - }) | ||
| 1339 | - .detach(); | ||
| 1340 | - } | ||
| 1341 | - | ||
| 1342 | - fn admin_user_action<Fut>( | ||
| 1343 | - &mut self, | ||
| 1344 | - window: &mut Window, | ||
| 1345 | - cx: &mut Context<Self>, | ||
| 1346 | - action: impl FnOnce(NetworkHandle) -> Fut + 'static, | ||
| 1347 | - ) where | ||
| 1348 | - Fut: Future<Output = Result<(), Error>> + 'static, | ||
| 1349 | - { | ||
| 1350 | - let network = self.network.clone(); | ||
| 1351 | - | ||
| 1352 | - cx.spawn_in(window, async move |this, cx| match action(network).await { | ||
| 1353 | - Ok(()) => { | ||
| 1354 | - let _ = this.update_in(cx, |yumush, window, cx| yumush.load_users(window, cx)); | ||
| 1355 | - } | ||
| 1356 | - Err(error) => notify_error(&this, cx, error), | ||
| 1357 | - }) | ||
| 1358 | - .detach(); | ||
| 1359 | - } | ||
| 1360 | - | ||
| 1361 | - fn suspend_admin_user( | ||
| 1362 | - &mut self, | ||
| 1363 | - user_id: &str, | ||
| 1364 | - suspended: bool, | ||
| 1365 | - window: &mut Window, | ||
| 1366 | - cx: &mut Context<Self>, | ||
| 1367 | - ) { | ||
| 1368 | - let user_id = user_id.to_string(); | ||
| 1369 | - self.admin_user_action(window, cx, move |network| async move { | ||
| 1370 | - network.suspend_user(&user_id, suspended).await | ||
| 1371 | - }); | ||
| 1372 | - } | ||
| 1373 | - | ||
| 1374 | - fn revoke_admin_sessions( | ||
| 1375 | - &mut self, | ||
| 1376 | - user_id: &str, | ||
| 1377 | - window: &mut Window, | ||
| 1378 | - cx: &mut Context<Self>, | ||
| 1379 | - ) { | ||
| 1380 | - let user_id = user_id.to_string(); | ||
| 1381 | - self.admin_user_action(window, cx, move |network| async move { | ||
| 1382 | - network.revoke_sessions(&user_id).await | ||
| 1383 | - }); | ||
| 1384 | - } | ||
| 1385 | - | ||
| 1386 | - fn clear_admin_profile(&mut self, user_id: &str, window: &mut Window, cx: &mut Context<Self>) { | ||
| 1387 | - let user_id = user_id.to_string(); | ||
| 1388 | - self.admin_user_action(window, cx, move |network| async move { | ||
| 1389 | - network.clear_profile(&user_id).await.map(|_| ()) | ||
| 1390 | - }); | ||
| 1391 | - } | ||
| 1392 | - | ||
| 1393 | - fn toggle_admin_password(&mut self, user_id: &str, cx: &mut Context<Self>) { | ||
| 1394 | - if self.chat.pending_admin_password.as_deref() == Some(user_id) { | ||
| 1395 | - self.chat.pending_admin_password = None; | ||
| 1396 | - } else { | ||
| 1397 | - self.chat.pending_admin_password = Some(user_id.to_string()); | ||
| 1398 | - } | ||
| 1399 | - cx.notify(); | ||
| 1400 | - } | ||
| 1401 | - | ||
| 1402 | - fn reset_admin_password(&mut self, window: &mut Window, cx: &mut Context<Self>) { | ||
| 1403 | - let Some(user_id) = self.chat.pending_admin_password.take() else { | ||
| 1404 | - return; | ||
| 1405 | - }; | ||
| 1406 | - let password = self.chat.admin_password.read(cx).text().to_string(); | ||
| 1407 | - self.chat | ||
| 1408 | - .admin_password | ||
| 1409 | - .update(cx, |input, cx| input.set_value("", window, cx)); | ||
| 1410 | - self.admin_user_action(window, cx, move |network| async move { | ||
| 1411 | - network.reset_password(&user_id, &password).await | ||
| 1412 | - }); | ||
| 1413 | - } | ||
| 1414 | - | ||
| 1415 | - fn load_sessions(&self, user_id: String, window: &mut Window, cx: &mut Context<Self>) { | ||
| 1416 | - let network = self.network.clone(); | ||
| 1417 | - | ||
| 1418 | - cx.spawn_in(window, async move |this, cx| { | ||
| 1419 | - match network.sessions(&user_id).await { | ||
| 1420 | - Ok(sessions) => { | ||
| 1421 | - let _ = this.update(cx, |yumush, cx| { | ||
| 1422 | - yumush.chat.admin_sessions = Some((user_id, sessions)); | ||
| 1423 | - cx.notify(); | ||
| 1424 | - }); | ||
| 1425 | - } | ||
| 1426 | - Err(error) => notify_error(&this, cx, error), | ||
| 1427 | - } | ||
| 1428 | - }) | ||
| 1429 | - .detach(); | ||
| 1430 | - } | ||
| 1431 | - | ||
| 1432 | - fn toggle_admin_sessions( | ||
| 1433 | - &mut self, | ||
| 1434 | - user_id: &str, | ||
| 1435 | - window: &mut Window, | ||
| 1436 | - cx: &mut Context<Self>, | ||
| 1437 | - ) { | ||
| 1438 | - if self | ||
| 1439 | - .chat | ||
| 1440 | - .admin_sessions | ||
| 1441 | - .as_ref() | ||
| 1442 | - .is_some_and(|(open, _)| open == user_id) | ||
| 1443 | - { | ||
| 1444 | - self.chat.admin_sessions = None; | ||
| 1445 | - cx.notify(); | ||
| 1446 | - return; | ||
| 1447 | - } | ||
| 1448 | - self.load_sessions(user_id.to_string(), window, cx); | ||
| 1449 | - } | ||
| 1450 | - | ||
| 1451 | - fn revoke_admin_session( | ||
| 1452 | - &mut self, | ||
| 1453 | - user_id: &str, | ||
| 1454 | - session_id: &str, | ||
| 1455 | - window: &mut Window, | ||
| 1456 | - cx: &mut Context<Self>, | ||
| 1457 | - ) { | ||
| 1458 | - let network = self.network.clone(); | ||
| 1459 | - let user_id = user_id.to_string(); | ||
| 1460 | - let session_id = session_id.to_string(); | ||
| 1461 | - | ||
| 1462 | - cx.spawn_in(window, async move |this, cx| { | ||
| 1463 | - match network.revoke_session(&user_id, &session_id).await { | ||
| 1464 | - Ok(()) => { | ||
| 1465 | - let _ = this.update_in(cx, |yumush, window, cx| { | ||
| 1466 | - yumush.load_sessions(user_id, window, cx) | ||
| 1467 | - }); | ||
| 1468 | - } | ||
| 1469 | - Err(error) => notify_error(&this, cx, error), | ||
| 1470 | - } | ||
| 1471 | - }) | ||
| 1472 | - .detach(); | ||
| 1473 | - } | ||
| 1474 | - | ||
| 1475 | - fn toggle_admin_rename(&mut self, user_id: &str, cx: &mut Context<Self>) { | ||
| 1476 | - if self.chat.pending_admin_rename.as_deref() == Some(user_id) { | ||
| 1477 | - self.chat.pending_admin_rename = None; | ||
| 1478 | - } else { | ||
| 1479 | - self.chat.pending_admin_rename = Some(user_id.to_string()); | ||
| 1480 | - } | ||
| 1481 | - cx.notify(); | ||
| 1482 | - } | ||
| 1483 | - | ||
| 1484 | - fn rename_admin_user(&mut self, window: &mut Window, cx: &mut Context<Self>) { | ||
| 1485 | - let Some(user_id) = self.chat.pending_admin_rename.take() else { | ||
| 1486 | - return; | ||
| 1487 | - }; | ||
| 1488 | - let username = self.chat.admin_rename.read(cx).text().to_string(); | ||
| 1489 | - self.chat | ||
| 1490 | - .admin_rename | ||
| 1491 | - .update(cx, |input, cx| input.set_value("", window, cx)); | ||
| 1492 | - self.admin_user_action(window, cx, move |network| async move { | ||
| 1493 | - network.rename_user(&user_id, &username).await.map(|_| ()) | ||
| 1494 | - }); | ||
| 1495 | - } | ||
| 1496 | - | ||
| 1497 | - fn toggle_admin_timeout(&mut self, user_id: &str, cx: &mut Context<Self>) { | ||
| 1498 | - if self.chat.pending_admin_timeout.as_deref() == Some(user_id) { | ||
| 1499 | - self.chat.pending_admin_timeout = None; | ||
| 1500 | - } else { | ||
| 1501 | - self.chat.pending_admin_timeout = Some(user_id.to_string()); | ||
| 1502 | - } | ||
| 1503 | - cx.notify(); | ||
| 1504 | - } | ||
| 1505 | - | ||
| 1506 | - fn confirm_admin_timeout(&mut self, window: &mut Window, cx: &mut Context<Self>) { | ||
| 1507 | - let Some(user_id) = self.chat.pending_admin_timeout.clone() else { | ||
| 1508 | - return; | ||
| 1509 | - }; | ||
| 1510 | - let minutes = self.chat.admin_timeout.read(cx).text().to_string(); | ||
| 1511 | - let Ok(minutes) = minutes.trim().parse::<u32>() else { | ||
| 1512 | - return; | ||
| 1513 | - }; | ||
| 1514 | - self.chat.pending_admin_timeout = None; | ||
| 1515 | - self.chat | ||
| 1516 | - .admin_timeout | ||
| 1517 | - .update(cx, |input, cx| input.set_value("", window, cx)); | ||
| 1518 | - self.admin_user_action(window, cx, move |network| async move { | ||
| 1519 | - network.timeout_user(&user_id, minutes).await | ||
| 1520 | - }); | ||
| 1521 | - } | ||
| 1522 | - | ||
| 1523 | - fn toggle_admin_ban(&mut self, user_id: &str, cx: &mut Context<Self>) { | ||
| 1524 | - if self.chat.pending_admin_ban.as_deref() == Some(user_id) { | ||
| 1525 | - self.chat.pending_admin_ban = None; | ||
| 1526 | - } else { | ||
| 1527 | - self.chat.pending_admin_ban = Some(user_id.to_string()); | ||
| 1528 | - } | ||
| 1529 | - cx.notify(); | ||
| 1530 | - } | ||
| 1531 | - | ||
| 1532 | - fn toggle_admin_ban_ip(&mut self, cx: &mut Context<Self>) { | ||
| 1533 | - self.chat.admin_ban_ip = !self.chat.admin_ban_ip; | ||
| 1534 | - cx.notify(); | ||
| 1535 | - } | ||
| 1536 | - | ||
| 1537 | - fn confirm_admin_ban(&mut self, window: &mut Window, cx: &mut Context<Self>) { | ||
| 1538 | - let Some(user_id) = self.chat.pending_admin_ban.take() else { | ||
| 1539 | - return; | ||
| 1540 | - }; | ||
| 1541 | - let reason = self.chat.admin_ban_reason.read(cx).text().to_string(); | ||
| 1542 | - let reason = reason.trim().to_string(); | ||
| 1543 | - let reason = (!reason.is_empty()).then_some(reason); | ||
| 1544 | - let ip = self.chat.admin_ban_ip; | ||
| 1545 | - self.chat | ||
| 1546 | - .admin_ban_reason | ||
| 1547 | - .update(cx, |input, cx| input.set_value("", window, cx)); | ||
| 1548 | - self.admin_user_action(window, cx, move |network| async move { | ||
| 1549 | - network.ban_user(&user_id, reason.as_deref(), ip).await | ||
| 1550 | - }); | ||
| 1551 | - } | ||
| 1552 | - | ||
| 1553 | - fn load_access(&self, window: &mut Window, cx: &mut Context<Self>) { | ||
| 1554 | - self.load_invites(window, cx); | ||
| 1555 | - self.load_server_bans(window, cx); | ||
| 1556 | - } | ||
| 1557 | - | ||
| 1558 | - fn load_invites(&self, window: &mut Window, cx: &mut Context<Self>) { | ||
| 1559 | - if !self.is_builder() { | ||
| 1560 | - return; | ||
| 1561 | - } | ||
| 1562 | - let network = self.network.clone(); | ||
| 1563 | - | ||
| 1564 | - cx.spawn_in(window, async move |this, cx| { | ||
| 1565 | - let codes = match network.invite_codes().await { | ||
| 1566 | - Ok(codes) => codes, | ||
| 1567 | - Err(error) => { | ||
| 1568 | - notify_error(&this, cx, error); | ||
| 1569 | - return; | ||
| 1570 | - } | ||
| 1571 | - }; | ||
| 1572 | - | ||
| 1573 | - let _ = this.update(cx, |yumush, cx| { | ||
| 1574 | - yumush.chat.admin_invites = codes; | ||
| 1575 | - cx.notify(); | ||
| 1576 | - }); | ||
| 1577 | - }) | ||
| 1578 | - .detach(); | ||
| 1579 | - } | ||
| 1580 | - | ||
| 1581 | - fn load_server_bans(&self, window: &mut Window, cx: &mut Context<Self>) { | ||
| 1582 | - if !self.is_builder() { | ||
| 1583 | - return; | ||
| 1584 | - } | ||
| 1585 | - let network = self.network.clone(); | ||
| 1586 | - | ||
| 1587 | - cx.spawn_in(window, async move |this, cx| { | ||
| 1588 | - let bans = match network.server_bans().await { | ||
| 1589 | - Ok(bans) => bans, | ||
| 1590 | - Err(error) => { | ||
| 1591 | - notify_error(&this, cx, error); | ||
| 1592 | - return; | ||
| 1593 | - } | ||
| 1594 | - }; | ||
| 1595 | - | ||
| 1596 | - let _ = this.update(cx, |yumush, cx| { | ||
| 1597 | - yumush.chat.admin_bans = bans; | ||
| 1598 | - cx.notify(); | ||
| 1599 | - }); | ||
| 1600 | - }) | ||
| 1601 | - .detach(); | ||
| 1602 | - } | ||
| 1603 | - | ||
| 1604 | - fn create_invite(&mut self, window: &mut Window, cx: &mut Context<Self>) { | ||
| 1605 | - let uses = self.chat.invite_uses.read(cx).text().to_string(); | ||
| 1606 | - let Ok(uses) = uses.trim().parse::<u32>() else { | ||
| 1607 | - return; | ||
| 1608 | - }; | ||
| 1609 | - let hours = self.chat.invite_hours.read(cx).text().to_string(); | ||
| 1610 | - let hours = match hours.trim() { | ||
| 1611 | - "" => None, | ||
| 1612 | - text => match text.parse::<u32>() { | ||
| 1613 | - Ok(hours) => Some(hours), | ||
| 1614 | - Err(_) => return, | ||
| 1615 | - }, | ||
| 1616 | - }; | ||
| 1617 | - let network = self.network.clone(); | ||
| 1618 | - | ||
| 1619 | - cx.spawn_in(window, async move |this, cx| { | ||
| 1620 | - match network.create_invite_code(uses, hours).await { | ||
| 1621 | - Ok(_) => { | ||
| 1622 | - let _ = | ||
| 1623 | - this.update_in(cx, |yumush, window, cx| yumush.load_invites(window, cx)); | ||
| 1624 | - } | ||
| 1625 | - Err(error) => notify_error(&this, cx, error), | ||
| 1626 | - } | ||
| 1627 | - }) | ||
| 1628 | - .detach(); | ||
| 1629 | - } | ||
| 1630 | - | ||
| 1631 | - fn delete_invite(&mut self, code: &str, window: &mut Window, cx: &mut Context<Self>) { | ||
| 1632 | - if self.chat.pending_invite_delete.as_deref() != Some(code) { | ||
| 1633 | - self.chat.pending_invite_delete = Some(code.to_string()); | ||
| 1634 | - cx.notify(); | ||
| 1635 | - return; | ||
| 1636 | - } | ||
| 1637 | - self.chat.pending_invite_delete = None; | ||
| 1638 | - let network = self.network.clone(); | ||
| 1639 | - let code = code.to_string(); | ||
| 1640 | - | ||
| 1641 | - cx.spawn_in(window, async move |this, cx| { | ||
| 1642 | - match network.delete_invite_code(&code).await { | ||
| 1643 | - Ok(()) => { | ||
| 1644 | - let _ = | ||
| 1645 | - this.update_in(cx, |yumush, window, cx| yumush.load_invites(window, cx)); | ||
| 1646 | - } | ||
| 1647 | - Err(error) => notify_error(&this, cx, error), | ||
| 1648 | - } | ||
| 1649 | - }) | ||
| 1650 | - .detach(); | ||
| 1651 | - } | ||
| 1652 | - | ||
| 1653 | - fn copy_invite(&self, code: &str, cx: &mut Context<Self>) { | ||
| 1654 | - cx.write_to_clipboard(ClipboardItem::new_string(code.to_string())); | ||
| 1655 | - } | ||
| 1656 | - | ||
| 1657 | - fn remove_ban(&mut self, id: &str, window: &mut Window, cx: &mut Context<Self>) { | ||
| 1658 | - let network = self.network.clone(); | ||
| 1659 | - let id = id.to_string(); | ||
| 1660 | - | ||
| 1661 | - cx.spawn_in(window, async move |this, cx| { | ||
| 1662 | - match network.remove_server_ban(&id).await { | ||
| 1663 | - Ok(()) => { | ||
| 1664 | - let _ = this | ||
| 1665 | - .update_in(cx, |yumush, window, cx| yumush.load_server_bans(window, cx)); | ||
| 1666 | - } | ||
| 1667 | - Err(error) => notify_error(&this, cx, error), | ||
| 1668 | - } | ||
| 1669 | - }) | ||
| 1670 | - .detach(); | ||
| 1671 | - } | ||
| 1672 | - | ||
| 1673 | - fn messages_body(&self, entity: &Entity<Self>, palette: Palette) -> Vec<AnyElement> { | ||
| 1674 | - let filters = div() | ||
| 1675 | - .px(px(12.)) | ||
| 1676 | - .py(px(8.)) | ||
| 1677 | - .flex() | ||
| 1678 | - .flex_col() | ||
| 1679 | - .gap(px(8.)) | ||
| 1680 | - .child(field(palette, &self.chat.admin_message_text)) | ||
| 1681 | - .child(field(palette, &self.chat.admin_message_user)) | ||
| 1682 | - .child(field(palette, &self.chat.admin_message_community)) | ||
| 1683 | - .child( | ||
| 1684 | - div() | ||
| 1685 | - .flex() | ||
| 1686 | - .gap(px(8.)) | ||
| 1687 | - .child( | ||
| 1688 | - div() | ||
| 1689 | - .flex_1() | ||
| 1690 | - .min_w_0() | ||
| 1691 | - .child(field(palette, &self.chat.admin_message_from)), | ||
| 1692 | - ) | ||
| 1693 | - .child( | ||
| 1694 | - div() | ||
| 1695 | - .flex_1() | ||
| 1696 | - .min_w_0() | ||
| 1697 | - .child(field(palette, &self.chat.admin_message_to)), | ||
| 1698 | - ), | ||
| 1699 | - ) | ||
| 1700 | - .child(div().flex().justify_end().child(accent_button( | ||
| 1701 | - "search_messages", | ||
| 1702 | - "Search", | ||
| 1703 | - palette, | ||
| 1704 | - entity_action(entity, |yumush, window, cx| { | ||
| 1705 | - yumush.search_messages(window, cx) | ||
| 1706 | - }), | ||
| 1707 | - ))); | ||
| 1708 | - let rows: Vec<Div> = self | ||
| 1709 | - .chat | ||
| 1710 | - .admin_messages | ||
| 1711 | - .iter() | ||
| 1712 | - .map(|hit| { | ||
| 1713 | - let pending = | ||
| 1714 | - self.chat.pending_message_delete.as_deref() == Some(hit.message.id.as_str()); | ||
| 1715 | - message_hit_row(hit, pending, entity, palette) | ||
| 1716 | - }) | ||
| 1717 | - .collect(); | ||
| 1718 | - | ||
| 1719 | - vec![ | ||
| 1720 | - filters.into_any_element(), | ||
| 1721 | - section("Messages", palette, rows).into_any_element(), | ||
| 1722 | - ] | ||
| 1723 | - } | ||
| 1724 | - | ||
| 1725 | - fn storage_body(&self, entity: &Entity<Self>, palette: Palette) -> Vec<AnyElement> { | ||
| 1726 | - let users: Vec<Div> = self | ||
| 1727 | - .chat | ||
| 1728 | - .admin_usage_users | ||
| 1729 | - .iter() | ||
| 1730 | - .map(|usage| usage_row(&usage.user.username, usage.bytes, usage.count, palette)) | ||
| 1731 | - .collect(); | ||
| 1732 | - let communities: Vec<Div> = self | ||
| 1733 | - .chat | ||
| 1734 | - .admin_usage_communities | ||
| 1735 | - .iter() | ||
| 1736 | - .map(|usage| usage_row(&usage.community, usage.bytes, usage.count, palette)) | ||
| 1737 | - .collect(); | ||
| 1738 | - let filters = div() | ||
| 1739 | - .px(px(12.)) | ||
| 1740 | - .py(px(8.)) | ||
| 1741 | - .flex() | ||
| 1742 | - .flex_col() | ||
| 1743 | - .gap(px(8.)) | ||
| 1744 | - .child( | ||
| 1745 | - div() | ||
| 1746 | - .flex() | ||
| 1747 | - .gap(px(8.)) | ||
| 1748 | - .child( | ||
| 1749 | - div() | ||
| 1750 | - .flex_1() | ||
| 1751 | - .min_w_0() | ||
| 1752 | - .child(field(palette, &self.chat.admin_attachment_user)), | ||
| 1753 | - ) | ||
| 1754 | - .child( | ||
| 1755 | - div() | ||
| 1756 | - .flex_1() | ||
| 1757 | - .min_w_0() | ||
| 1758 | - .child(field(palette, &self.chat.admin_attachment_community)), | ||
| 1759 | - ), | ||
| 1760 | - ) | ||
| 1761 | - .child(div().flex().justify_end().child(accent_button( | ||
| 1762 | - "search_attachments", | ||
| 1763 | - "Search", | ||
| 1764 | - palette, | ||
| 1765 | - entity_action(entity, |yumush, window, cx| { | ||
| 1766 | - yumush.search_attachments(window, cx) | ||
| 1767 | - }), | ||
| 1768 | - ))); | ||
| 1769 | - let attachments: Vec<Div> = self | ||
| 1770 | - .chat | ||
| 1771 | - .admin_attachments | ||
| 1772 | - .iter() | ||
| 1773 | - .map(|info| { | ||
| 1774 | - let pending = self.chat.pending_attachment_delete.as_deref() | ||
| 1775 | - == Some(info.attachment.id.as_str()); | ||
| 1776 | - attachment_row(info, pending, entity, palette) | ||
| 1777 | - }) | ||
| 1778 | - .collect(); | ||
| 1779 | - | ||
| 1780 | - vec![ | ||
| 1781 | - section("Users", palette, users).into_any_element(), | ||
| 1782 | - section("Communities", palette, communities).into_any_element(), | ||
| 1783 | - filters.into_any_element(), | ||
| 1784 | - section("Attachments", palette, attachments).into_any_element(), | ||
| 1785 | - ] | ||
| 1786 | - } | ||
| 1787 | - | ||
| 1788 | - fn search_messages(&mut self, window: &mut Window, cx: &mut Context<Self>) { | ||
| 1789 | - let text = search_text(&self.chat.admin_message_text, cx); | ||
| 1790 | - let username = search_text(&self.chat.admin_message_user, cx); | ||
| 1791 | - let community = search_text(&self.chat.admin_message_community, cx); | ||
| 1792 | - let user_id = self.resolve_user(&username); | ||
| 1793 | - let community_id = self.resolve_community(&community); | ||
| 1794 | - let after_ms = day_millis(&search_text(&self.chat.admin_message_from, cx), false); | ||
| 1795 | - let before_ms = day_millis(&search_text(&self.chat.admin_message_to, cx), true); | ||
| 1796 | - let text = (!text.is_empty()).then_some(text); | ||
| 1797 | - let network = self.network.clone(); | ||
| 1798 | - | ||
| 1799 | - cx.spawn_in(window, async move |this, cx| { | ||
| 1800 | - let hits = match network | ||
| 1801 | - .search_messages(text, user_id, community_id, after_ms, before_ms) | ||
| 1802 | - .await | ||
| 1803 | - { | ||
| 1804 | - Ok(hits) => hits, | ||
| 1805 | - Err(error) => { | ||
| 1806 | - notify_error(&this, cx, error); | ||
| 1807 | - return; | ||
| 1808 | - } | ||
| 1809 | - }; | ||
| 1810 | - | ||
| 1811 | - let _ = this.update(cx, |yumush, cx| { | ||
| 1812 | - yumush.chat.admin_messages = hits; | ||
| 1813 | - cx.notify(); | ||
| 1814 | - }); | ||
| 1815 | - }) | ||
| 1816 | - .detach(); | ||
| 1817 | - } | ||
| 1818 | - | ||
| 1819 | - fn delete_admin_message( | ||
| 1820 | - &mut self, | ||
| 1821 | - message_id: &str, | ||
| 1822 | - window: &mut Window, | ||
| 1823 | - cx: &mut Context<Self>, | ||
| 1824 | - ) { | ||
| 1825 | - if self.chat.pending_message_delete.as_deref() != Some(message_id) { | ||
| 1826 | - self.chat.pending_message_delete = Some(message_id.to_string()); | ||
| 1827 | - cx.notify(); | ||
| 1828 | - return; | ||
| 1829 | - } | ||
| 1830 | - self.chat.pending_message_delete = None; | ||
| 1831 | - let network = self.network.clone(); | ||
| 1832 | - let message_id = message_id.to_string(); | ||
| 1833 | - | ||
| 1834 | - cx.spawn_in(window, async move |this, cx| { | ||
| 1835 | - match network.delete_message(&message_id).await { | ||
| 1836 | - Ok(()) => { | ||
| 1837 | - let _ = this.update(cx, |yumush, cx| { | ||
| 1838 | - yumush | ||
| 1839 | - .chat | ||
| 1840 | - .admin_messages | ||
| 1841 | - .retain(|hit| hit.message.id != message_id); | ||
| 1842 | - cx.notify(); | ||
| 1843 | - }); | ||
| 1844 | - } | ||
| 1845 | - Err(error) => notify_error(&this, cx, error), | ||
| 1846 | - } | ||
| 1847 | - }) | ||
| 1848 | - .detach(); | ||
| 1849 | - } | ||
| 1850 | - | ||
| 1851 | - fn purge_admin_user(&mut self, user_id: &str, window: &mut Window, cx: &mut Context<Self>) { | ||
| 1852 | - if self.chat.pending_admin_purge.as_deref() != Some(user_id) { | ||
| 1853 | - self.chat.pending_admin_purge = Some(user_id.to_string()); | ||
| 1854 | - cx.notify(); | ||
| 1855 | - return; | ||
| 1856 | - } | ||
| 1857 | - self.chat.pending_admin_purge = None; | ||
| 1858 | - let network = self.network.clone(); | ||
| 1859 | - let user_id = user_id.to_string(); | ||
| 1860 | - | ||
| 1861 | - cx.spawn_in(window, async move |this, cx| { | ||
| 1862 | - match network.purge_user(&user_id).await { | ||
| 1863 | - Ok(()) => { | ||
| 1864 | - let _ = this.update_in(cx, |yumush, window, cx| { | ||
| 1865 | - yumush | ||
| 1866 | - .chat | ||
| 1867 | - .admin_messages | ||
| 1868 | - .retain(|hit| hit.message.user_id != user_id); | ||
| 1869 | - yumush.load_users(window, cx); | ||
| 1870 | - }); | ||
| 1871 | - } | ||
| 1872 | - Err(error) => notify_error(&this, cx, error), | ||
| 1873 | - } | ||
| 1874 | - }) | ||
| 1875 | - .detach(); | ||
| 1876 | - } | ||
| 1877 | - | ||
| 1878 | - fn log_user(&self, input: &Entity<InputState>, cx: &App) -> Option<Option<String>> { | ||
| 1879 | - let name = search_text(input, cx); | ||
| 1880 | - if name.is_empty() { | ||
| 1881 | - return Some(None); | ||
| 1882 | - } | ||
| 1883 | - | ||
| 1884 | - self.resolve_user(&name).map(Some) | ||
| 1885 | - } | ||
| 1886 | - | ||
| 1887 | - fn export_admin_log(&self, window: &mut Window, cx: &mut Context<Self>) { | ||
| 1888 | - let Some(directory) = dirs::download_dir() else { | ||
| 1889 | - window.push_notification(Notification::error("No download folder"), cx); | ||
| 1890 | - return; | ||
| 1891 | - }; | ||
| 1892 | - let name = format!( | ||
| 1893 | - "yumush-admin-log-{}.csv", | ||
| 1894 | - millis_time(now_ms(), "%Y%m%d-%H%M%S") | ||
| 1895 | - ); | ||
| 1896 | - let path = directory.join(name); | ||
| 1897 | - match std::fs::write(&path, admin_log_csv(&self.chat.admin_logs)) { | ||
| 1898 | - Ok(()) => window.push_notification( | ||
| 1899 | - Notification::success(path.display().to_string()), | ||
| 1900 | - cx, | ||
| 1901 | - ), | ||
| 1902 | - Err(error) => window.push_notification(Notification::error(error.to_string()), cx), | ||
| 1903 | - } | ||
| 1904 | - } | ||
| 1905 | - | ||
| 1906 | - fn resolve_user(&self, username: &str) -> Option<String> { | ||
| 1907 | - self.chat | ||
| 1908 | - .admin_users | ||
| 1909 | - .iter() | ||
| 1910 | - .find(|account| account.user.username.to_lowercase() == username) | ||
| 1911 | - .map(|account| account.user.id.clone()) | ||
| 1912 | - } | ||
| 1913 | - | ||
| 1914 | - fn resolve_community(&self, name: &str) -> Option<String> { | ||
| 1915 | - self.chat | ||
| 1916 | - .admin_communities | ||
| 1917 | - .iter() | ||
| 1918 | - .find(|summary| { | ||
| 1919 | - summary.community.kind == CommunityKind::Group | ||
| 1920 | - && summary.community.name.to_lowercase() == name | ||
| 1921 | - }) | ||
| 1922 | - .map(|summary| summary.community.id.clone()) | ||
| 1923 | - } | ||
| 1924 | - | ||
| 1925 | - fn load_storage(&self, window: &mut Window, cx: &mut Context<Self>) { | ||
| 1926 | - if !self.is_builder() { | ||
| 1927 | - return; | ||
| 1928 | - } | ||
| 1929 | - self.search_attachments(window, cx); | ||
| 1930 | - let network = self.network.clone(); | ||
| 1931 | - | ||
| 1932 | - cx.spawn_in(window, async move |this, cx| { | ||
| 1933 | - let (users, communities) = match network.storage_usage().await { | ||
| 1934 | - Ok(usage) => usage, | ||
| 1935 | - Err(error) => { | ||
| 1936 | - notify_error(&this, cx, error); | ||
| 1937 | - return; | ||
| 1938 | - } | ||
| 1939 | - }; | ||
| 1940 | - | ||
| 1941 | - let _ = this.update(cx, |yumush, cx| { | ||
| 1942 | - yumush.chat.admin_usage_users = users; | ||
| 1943 | - yumush.chat.admin_usage_communities = communities; | ||
| 1944 | - cx.notify(); | ||
| 1945 | - }); | ||
| 1946 | - }) | ||
| 1947 | - .detach(); | ||
| 1948 | - } | ||
| 1949 | - | ||
| 1950 | - fn search_attachments(&self, window: &mut Window, cx: &mut Context<Self>) { | ||
| 1951 | - let username = search_text(&self.chat.admin_attachment_user, cx); | ||
| 1952 | - let community = search_text(&self.chat.admin_attachment_community, cx); | ||
| 1953 | - let user_id = self.resolve_user(&username); | ||
| 1954 | - let community_id = self.resolve_community(&community); | ||
| 1955 | - let network = self.network.clone(); | ||
| 1956 | - | ||
| 1957 | - cx.spawn_in(window, async move |this, cx| { | ||
| 1958 | - let attachments = match network.attachments(user_id, community_id).await { | ||
| 1959 | - Ok(attachments) => attachments, | ||
| 1960 | - Err(error) => { | ||
| 1961 | - notify_error(&this, cx, error); | ||
| 1962 | - return; | ||
| 1963 | - } | ||
| 1964 | - }; | ||
| 1965 | - | ||
| 1966 | - let _ = this.update(cx, |yumush, cx| { | ||
| 1967 | - yumush.chat.admin_attachments = attachments; | ||
| 1968 | - cx.notify(); | ||
| 1969 | - }); | ||
| 1970 | - }) | ||
| 1971 | - .detach(); | ||
| 1972 | - } | ||
| 1973 | - | ||
| 1974 | - fn delete_admin_attachment( | ||
| 1975 | - &mut self, | ||
| 1976 | - attachment_id: &str, | ||
| 1977 | - window: &mut Window, | ||
| 1978 | - cx: &mut Context<Self>, | ||
| 1979 | - ) { | ||
| 1980 | - if self.chat.pending_attachment_delete.as_deref() != Some(attachment_id) { | ||
| 1981 | - self.chat.pending_attachment_delete = Some(attachment_id.to_string()); | ||
| 1982 | - cx.notify(); | ||
| 1983 | - return; | ||
| 1984 | - } | ||
| 1985 | - self.chat.pending_attachment_delete = None; | ||
| 1986 | - let network = self.network.clone(); | ||
| 1987 | - let attachment_id = attachment_id.to_string(); | ||
| 1988 | - | ||
| 1989 | - cx.spawn_in(window, async move |this, cx| { | ||
| 1990 | - match network.delete_attachment(&attachment_id).await { | ||
| 1991 | - Ok(()) => { | ||
| 1992 | - let _ = this.update_in(cx, |yumush, window, cx| { | ||
| 1993 | - yumush.load_storage(window, cx); | ||
| 1994 | - }); | ||
| 1995 | - } | ||
| 1996 | - Err(error) => notify_error(&this, cx, error), | ||
| 1997 | - } | ||
| 1998 | - }) | ||
| 1999 | - .detach(); | ||
| 2000 | - } | ||
crates/client/src/gui/builder.rsadded+402 -0
| @@ -0,0 +1,402 @@ | |||
| 1 | +mod access; | ||
| 2 | +mod communities; | ||
| 3 | +mod log; | ||
| 4 | +mod messages; | ||
| 5 | +mod overview; | ||
| 6 | +mod reports; | ||
| 7 | +mod server; | ||
| 8 | +mod storage; | ||
| 9 | +mod ui; | ||
| 10 | +mod users; | ||
| 11 | +mod voice; | ||
| 12 | + | ||
| 13 | +use common::model::{CommunityKind, Member, Role}; | ||
| 14 | +use gpui_kit::{ | ||
| 15 | + AnyElement, App, Context, Div, Entity, InteractiveElement, IntoElement, ParentElement, | ||
| 16 | + StatefulInteractiveElement, Styled, Window, | ||
| 17 | + assets::IconName, | ||
| 18 | + component::{Icon, switch::Switch}, | ||
| 19 | + div, | ||
| 20 | + prelude::FluentBuilder as _, | ||
| 21 | + px, | ||
| 22 | +}; | ||
| 23 | + | ||
| 24 | +use self::ui::{accent_button, icon_button, page_heading, plain_button}; | ||
| 25 | +use super::{ | ||
| 26 | + Palette, Yumush, chat::View, clickable, entity_action, notify_error, settings_page::header, | ||
| 27 | + text, | ||
| 28 | +}; | ||
| 29 | + | ||
| 30 | +const BUILDER_TABS: [(BuilderTab, &str, IconName); 10] = [ | ||
| 31 | + (BuilderTab::Overview, "Overview", IconName::LayoutDashboard), | ||
| 32 | + (BuilderTab::Users, "Users", IconName::Users), | ||
| 33 | + (BuilderTab::Communities, "Communities", IconName::Building2), | ||
| 34 | + (BuilderTab::Messages, "Messages", IconName::MessageSquare), | ||
| 35 | + (BuilderTab::Voice, "Voice", IconName::Mic), | ||
| 36 | + (BuilderTab::Storage, "Storage", IconName::HardDrive), | ||
| 37 | + (BuilderTab::Access, "Access", IconName::Key), | ||
| 38 | + (BuilderTab::Reports, "Reports", IconName::Flag), | ||
| 39 | + (BuilderTab::Server, "Server", IconName::Server), | ||
| 40 | + (BuilderTab::Log, "Log", IconName::ScrollText), | ||
| 41 | +]; | ||
| 42 | + | ||
| 43 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
| 44 | +pub(super) enum BuilderTab { | ||
| 45 | + Overview, | ||
| 46 | + Users, | ||
| 47 | + Communities, | ||
| 48 | + Messages, | ||
| 49 | + Voice, | ||
| 50 | + Storage, | ||
| 51 | + Access, | ||
| 52 | + Reports, | ||
| 53 | + Server, | ||
| 54 | + Log, | ||
| 55 | +} | ||
| 56 | + | ||
| 57 | +pub(super) struct Transfer { | ||
| 58 | + pub(super) community_id: String, | ||
| 59 | + pub(super) members: Vec<Member>, | ||
| 60 | + pub(super) pending: Option<String>, | ||
| 61 | +} | ||
| 62 | + | ||
| 63 | +impl Yumush { | ||
| 64 | + pub(super) fn builder_page( | ||
| 65 | + &self, | ||
| 66 | + entity: &Entity<Self>, | ||
| 67 | + palette: Palette, | ||
| 68 | + cx: &App, | ||
| 69 | + ) -> impl IntoElement { | ||
| 70 | + let tab = self.chat.builder_tab; | ||
| 71 | + let title = BUILDER_TABS | ||
| 72 | + .iter() | ||
| 73 | + .find(|(candidate, _, _)| *candidate == tab) | ||
| 74 | + .map_or("", |(_, label, _)| label); | ||
| 75 | + let body = match tab { | ||
| 76 | + BuilderTab::Overview => self | ||
| 77 | + .overview_rows(palette) | ||
| 78 | + .into_iter() | ||
| 79 | + .map(IntoElement::into_any_element) | ||
| 80 | + .collect(), | ||
| 81 | + BuilderTab::Users => self.users_body(entity, palette, cx), | ||
| 82 | + BuilderTab::Communities => self.communities_body(entity, palette, cx), | ||
| 83 | + BuilderTab::Messages => self.messages_body(entity, palette), | ||
| 84 | + BuilderTab::Voice => self.voice_body(entity, palette), | ||
| 85 | + BuilderTab::Storage => self.storage_body(entity, palette), | ||
| 86 | + BuilderTab::Access => self.access_body(entity, palette), | ||
| 87 | + BuilderTab::Reports => self.reports_body(entity, palette), | ||
| 88 | + BuilderTab::Server => self.server_body(entity, palette), | ||
| 89 | + BuilderTab::Log => self.log_body(entity, palette), | ||
| 90 | + }; | ||
| 91 | + let mut sections = vec![ | ||
| 92 | + page_heading(title, self.tab_actions(tab, entity, palette), palette).into_any_element(), | ||
| 93 | + ]; | ||
| 94 | + sections.extend(body); | ||
| 95 | + | ||
| 96 | + div() | ||
| 97 | + .flex_1() | ||
| 98 | + .min_w_0() | ||
| 99 | + .min_h_0() | ||
| 100 | + .flex() | ||
| 101 | + .flex_col() | ||
| 102 | + .child(header(entity, palette, Role::Builder.label())) | ||
| 103 | + .child( | ||
| 104 | + div() | ||
| 105 | + .flex_1() | ||
| 106 | + .min_h_0() | ||
| 107 | + .flex() | ||
| 108 | + .child(self.builder_rail(entity, palette)) | ||
| 109 | + .child( | ||
| 110 | + div() | ||
| 111 | + .id("builder_scroll") | ||
| 112 | + .flex_1() | ||
| 113 | + .min_w_0() | ||
| 114 | + .min_h_0() | ||
| 115 | + .overflow_y_scroll() | ||
| 116 | + .child( | ||
| 117 | + div() | ||
| 118 | + .w_full() | ||
| 119 | + .max_w(px(960.)) | ||
| 120 | + .mx_auto() | ||
| 121 | + .px(px(32.)) | ||
| 122 | + .py(px(28.)) | ||
| 123 | + .flex() | ||
| 124 | + .flex_col() | ||
| 125 | + .gap(px(20.)) | ||
| 126 | + .children(sections), | ||
| 127 | + ), | ||
| 128 | + ), | ||
| 129 | + ) | ||
| 130 | + } | ||
| 131 | + | ||
| 132 | + fn tab_actions( | ||
| 133 | + &self, | ||
| 134 | + tab: BuilderTab, | ||
| 135 | + entity: &Entity<Self>, | ||
| 136 | + palette: Palette, | ||
| 137 | + ) -> Vec<AnyElement> { | ||
| 138 | + match tab { | ||
| 139 | + BuilderTab::Voice => vec![ | ||
| 140 | + plain_button( | ||
| 141 | + "refresh_voice_button".to_string(), | ||
| 142 | + "Refresh", | ||
| 143 | + palette, | ||
| 144 | + entity_action(entity, |yumush, window, cx| { | ||
| 145 | + yumush.load_voice_rooms(window, cx) | ||
| 146 | + }), | ||
| 147 | + ) | ||
| 148 | + .into_any_element(), | ||
| 149 | + ], | ||
| 150 | + BuilderTab::Server => vec![ | ||
| 151 | + accent_button( | ||
| 152 | + "save_server", | ||
| 153 | + "Save", | ||
| 154 | + palette, | ||
| 155 | + entity_action(entity, |yumush, window, cx| { | ||
| 156 | + yumush.save_server_settings(window, cx) | ||
| 157 | + }), | ||
| 158 | + ) | ||
| 159 | + .into_any_element(), | ||
| 160 | + ], | ||
| 161 | + BuilderTab::Log => vec![ | ||
| 162 | + icon_button( | ||
| 163 | + "export_builder_log".to_string(), | ||
| 164 | + IconName::Download, | ||
| 165 | + "Export", | ||
| 166 | + palette.muted, | ||
| 167 | + palette, | ||
| 168 | + entity_action(entity, |yumush, window, cx| { | ||
| 169 | + yumush.export_builder_log(window, cx) | ||
| 170 | + }), | ||
| 171 | + ) | ||
| 172 | + .into_any_element(), | ||
| 173 | + ], | ||
| 174 | + _ => Vec::new(), | ||
| 175 | + } | ||
| 176 | + } | ||
| 177 | + | ||
| 178 | + fn builder_rail(&self, entity: &Entity<Self>, palette: Palette) -> Div { | ||
| 179 | + div() | ||
| 180 | + .w(px(200.)) | ||
| 181 | + .flex_shrink_0() | ||
| 182 | + .flex() | ||
| 183 | + .flex_col() | ||
| 184 | + .gap(px(2.)) | ||
| 185 | + .p(px(8.)) | ||
| 186 | + .bg(palette.panel) | ||
| 187 | + .border_r_1() | ||
| 188 | + .border_color(palette.border) | ||
| 189 | + .children( | ||
| 190 | + BUILDER_TABS.map(|(tab, label, icon)| { | ||
| 191 | + self.builder_nav_item(tab, label, icon, entity, palette) | ||
| 192 | + }), | ||
| 193 | + ) | ||
| 194 | + .child(div().flex_1()) | ||
| 195 | + .child(self.ghost_toggle(entity, palette)) | ||
| 196 | + } | ||
| 197 | + | ||
| 198 | + fn builder_nav_item( | ||
| 199 | + &self, | ||
| 200 | + tab: BuilderTab, | ||
| 201 | + label: &'static str, | ||
| 202 | + icon: IconName, | ||
| 203 | + entity: &Entity<Self>, | ||
| 204 | + palette: Palette, | ||
| 205 | + ) -> impl IntoElement { | ||
| 206 | + let active = self.chat.builder_tab == tab; | ||
| 207 | + let color = if active { palette.text } else { palette.muted }; | ||
| 208 | + let open = (tab == BuilderTab::Reports) | ||
| 209 | + .then_some(self.chat.open_reports) | ||
| 210 | + .filter(|count| *count > 0); | ||
| 211 | + | ||
| 212 | + clickable( | ||
| 213 | + format!("builder_tab_{label}"), | ||
| 214 | + palette, | ||
| 215 | + entity_action(entity, move |yumush, window, cx| { | ||
| 216 | + yumush.select_builder_tab(tab, window, cx) | ||
| 217 | + }), | ||
| 218 | + ) | ||
| 219 | + .h(px(32.)) | ||
| 220 | + .px(px(10.)) | ||
| 221 | + .flex() | ||
| 222 | + .items_center() | ||
| 223 | + .gap(px(8.)) | ||
| 224 | + .rounded(px(6.)) | ||
| 225 | + .text_size(text(13.)) | ||
| 226 | + .text_color(color) | ||
| 227 | + .when(active, |this| this.bg(palette.hover)) | ||
| 228 | + .child(Icon::new(icon).w(px(14.)).h(px(14.)).text_color(color)) | ||
| 229 | + .child(div().flex_1().min_w_0().truncate().child(label)) | ||
| 230 | + .children(open.map(|count| { | ||
| 231 | + div() | ||
| 232 | + .px(px(6.)) | ||
| 233 | + .rounded_full() | ||
| 234 | + .bg(palette.accent) | ||
| 235 | + .text_color(palette.on_accent) | ||
| 236 | + .text_size(text(11.)) | ||
| 237 | + .child(count.to_string()) | ||
| 238 | + })) | ||
| 239 | + } | ||
| 240 | + | ||
| 241 | + fn ghost_toggle(&self, entity: &Entity<Self>, palette: Palette) -> impl IntoElement { | ||
| 242 | + let enabled = self.chat.ghost; | ||
| 243 | + let toggle = entity_action(entity, move |yumush, window, cx| { | ||
| 244 | + yumush.set_ghost(!enabled, window, cx) | ||
| 245 | + }); | ||
| 246 | + | ||
| 247 | + div() | ||
| 248 | + .h(px(32.)) | ||
| 249 | + .px(px(10.)) | ||
| 250 | + .flex() | ||
| 251 | + .items_center() | ||
| 252 | + .justify_between() | ||
| 253 | + .text_size(text(13.)) | ||
| 254 | + .text_color(palette.muted) | ||
| 255 | + .child("Ghost") | ||
| 256 | + .child( | ||
| 257 | + Switch::new("ghost_switch") | ||
| 258 | + .checked(enabled) | ||
| 259 | + .on_change(move |_, window, cx| toggle(window, cx)), | ||
| 260 | + ) | ||
| 261 | + } | ||
| 262 | + | ||
| 263 | + pub(super) fn is_builder(&self) -> bool { | ||
| 264 | + self.user | ||
| 265 | + .as_ref() | ||
| 266 | + .is_some_and(|me| me.role == Role::Builder) | ||
| 267 | + } | ||
| 268 | + | ||
| 269 | + pub(super) fn load_builder(&self, window: &mut Window, cx: &mut Context<Self>) { | ||
| 270 | + self.load_open_reports(window, cx); | ||
| 271 | + match self.chat.builder_tab { | ||
| 272 | + BuilderTab::Overview => self.load_overview(window, cx), | ||
| 273 | + BuilderTab::Users => self.load_users(window, cx), | ||
| 274 | + BuilderTab::Communities => self.load_builder_communities(window, cx), | ||
| 275 | + BuilderTab::Messages => {} | ||
| 276 | + BuilderTab::Storage => { | ||
| 277 | + self.load_users(window, cx); | ||
| 278 | + self.load_builder_communities(window, cx); | ||
| 279 | + self.load_storage(window, cx); | ||
| 280 | + } | ||
| 281 | + BuilderTab::Voice => { | ||
| 282 | + self.load_voice_rooms(window, cx); | ||
| 283 | + self.load_builder_communities(window, cx); | ||
| 284 | + } | ||
| 285 | + BuilderTab::Access => self.load_access(window, cx), | ||
| 286 | + BuilderTab::Reports => self.load_reports(window, cx), | ||
| 287 | + BuilderTab::Server => {} | ||
| 288 | + BuilderTab::Log => { | ||
| 289 | + self.load_users(window, cx); | ||
| 290 | + self.load_builder_logs(window, cx); | ||
| 291 | + } | ||
| 292 | + } | ||
| 293 | + } | ||
| 294 | + | ||
| 295 | + pub(super) fn report_created(&self, window: &mut Window, cx: &mut Context<Self>) { | ||
| 296 | + self.load_open_reports(window, cx); | ||
| 297 | + if self.chat.view == View::Builder && self.chat.builder_tab == BuilderTab::Reports { | ||
| 298 | + self.load_reports(window, cx); | ||
| 299 | + } | ||
| 300 | + } | ||
| 301 | + | ||
| 302 | + fn select_builder_tab(&mut self, tab: BuilderTab, window: &mut Window, cx: &mut Context<Self>) { | ||
| 303 | + self.chat.builder_tab = tab; | ||
| 304 | + if tab == BuilderTab::Server { | ||
| 305 | + self.fill_server_form(window, cx); | ||
| 306 | + } | ||
| 307 | + self.load_builder(window, cx); | ||
| 308 | + cx.notify(); | ||
| 309 | + } | ||
| 310 | + | ||
| 311 | + pub(super) fn load_users(&self, window: &mut Window, cx: &mut Context<Self>) { | ||
| 312 | + if !self.is_builder() { | ||
| 313 | + return; | ||
| 314 | + } | ||
| 315 | + let network = self.network.clone(); | ||
| 316 | + | ||
| 317 | + cx.spawn_in(window, async move |this, cx| { | ||
| 318 | + let accounts = match network.users().await { | ||
| 319 | + Ok(accounts) => accounts, | ||
| 320 | + Err(error) => { | ||
| 321 | + notify_error(&this, cx, error); | ||
| 322 | + return; | ||
| 323 | + } | ||
| 324 | + }; | ||
| 325 | + | ||
| 326 | + let _ = this.update(cx, |yumush, cx| { | ||
| 327 | + for account in &accounts { | ||
| 328 | + yumush.request_user_avatar(&account.user, cx); | ||
| 329 | + } | ||
| 330 | + let ghost = yumush | ||
| 331 | + .user | ||
| 332 | + .as_ref() | ||
| 333 | + .and_then(|me| accounts.iter().find(|account| account.user.id == me.id)) | ||
| 334 | + .is_some_and(|account| account.ghost); | ||
| 335 | + yumush.chat.builder_users = accounts; | ||
| 336 | + yumush.set_ghost_state(ghost); | ||
| 337 | + cx.notify(); | ||
| 338 | + }); | ||
| 339 | + }) | ||
| 340 | + .detach(); | ||
| 341 | + } | ||
| 342 | + | ||
| 343 | + pub(super) fn voice_changed(&self, window: &mut Window, cx: &mut Context<Self>) { | ||
| 344 | + if self.chat.view == View::Builder && self.chat.builder_tab == BuilderTab::Voice { | ||
| 345 | + self.load_voice_rooms(window, cx); | ||
| 346 | + } | ||
| 347 | + } | ||
| 348 | + | ||
| 349 | + fn resolve_user(&self, username: &str) -> Option<String> { | ||
| 350 | + self.chat | ||
| 351 | + .builder_users | ||
| 352 | + .iter() | ||
| 353 | + .find(|account| account.user.username.to_lowercase() == username) | ||
| 354 | + .map(|account| account.user.id.clone()) | ||
| 355 | + } | ||
| 356 | + | ||
| 357 | + fn resolve_community(&self, name: &str) -> Option<String> { | ||
| 358 | + self.chat | ||
| 359 | + .builder_communities | ||
| 360 | + .iter() | ||
| 361 | + .find(|summary| { | ||
| 362 | + summary.community.kind == CommunityKind::Group | ||
| 363 | + && summary.community.name.to_lowercase() == name | ||
| 364 | + }) | ||
| 365 | + .map(|summary| summary.community.id.clone()) | ||
| 366 | + } | ||
| 367 | + | ||
| 368 | + fn set_ghost_state(&mut self, ghost: bool) { | ||
| 369 | + self.chat.ghost = ghost; | ||
| 370 | + let Some(me) = self.user.as_ref() else { | ||
| 371 | + return; | ||
| 372 | + }; | ||
| 373 | + if let Some(account) = self | ||
| 374 | + .chat | ||
| 375 | + .builder_users | ||
| 376 | + .iter_mut() | ||
| 377 | + .find(|account| account.user.id == me.id) | ||
| 378 | + { | ||
| 379 | + account.ghost = ghost; | ||
| 380 | + } | ||
| 381 | + } | ||
| 382 | + | ||
| 383 | + fn set_ghost(&self, enabled: bool, window: &mut Window, cx: &mut Context<Self>) { | ||
| 384 | + let network = self.network.clone(); | ||
| 385 | + | ||
| 386 | + cx.spawn_in(window, async move |this, cx| { | ||
| 387 | + match network.set_ghost(enabled).await { | ||
| 388 | + Ok(()) => { | ||
| 389 | + let _ = this.update(cx, |yumush, cx| { | ||
| 390 | + yumush.set_ghost_state(enabled); | ||
| 391 | + if enabled { | ||
| 392 | + yumush.force_mute(cx); | ||
| 393 | + } | ||
| 394 | + cx.notify(); | ||
| 395 | + }); | ||
| 396 | + } | ||
| 397 | + Err(error) => notify_error(&this, cx, error), | ||
| 398 | + } | ||
| 399 | + }) | ||
| 400 | + .detach(); | ||
| 401 | + } | ||
| 402 | +} | ||
crates/client/src/gui/builder/access.rsadded+317 -0
| @@ -0,0 +1,317 @@ | |||
| 1 | +use common::model::{InviteCode, ServerBan, ServerBanKind}; | ||
| 2 | +use gpui_kit::{ | ||
| 3 | + AnyElement, ClipboardItem, Context, Div, Entity, FontWeight, IntoElement, ParentElement, | ||
| 4 | + Styled, Window, assets::IconName, div, prelude::FluentBuilder as _, px, | ||
| 5 | +}; | ||
| 6 | + | ||
| 7 | +use super::ui::{accent_button, card, card_header, icon_button, pill, toolbar}; | ||
| 8 | +use crate::gui::STAMP; | ||
| 9 | +use crate::gui::{Palette, Yumush, auth::field, entity_action, millis_time, notify_error, text}; | ||
| 10 | + | ||
| 11 | +fn list_card(header: Div, rows: Vec<Div>, empty: &str, palette: Palette) -> Div { | ||
| 12 | + let body = if rows.is_empty() { | ||
| 13 | + div() | ||
| 14 | + .py(px(24.)) | ||
| 15 | + .flex() | ||
| 16 | + .justify_center() | ||
| 17 | + .text_size(text(12.)) | ||
| 18 | + .text_color(palette.muted) | ||
| 19 | + .child(empty.to_string()) | ||
| 20 | + } else { | ||
| 21 | + div() | ||
| 22 | + .flex() | ||
| 23 | + .flex_col() | ||
| 24 | + .children(rows.into_iter().enumerate().map(|(index, row)| { | ||
| 25 | + row.when(index > 0, |row| { | ||
| 26 | + row.border_t_1().border_color(palette.border) | ||
| 27 | + }) | ||
| 28 | + })) | ||
| 29 | + }; | ||
| 30 | + | ||
| 31 | + card(palette).child(header).child(body) | ||
| 32 | +} | ||
| 33 | + | ||
| 34 | +fn invite_row( | ||
| 35 | + invite: &InviteCode, | ||
| 36 | + pending: bool, | ||
| 37 | + entity: &Entity<Yumush>, | ||
| 38 | + palette: Palette, | ||
| 39 | +) -> Div { | ||
| 40 | + let copy_code = invite.code.clone(); | ||
| 41 | + let delete_code = invite.code.clone(); | ||
| 42 | + let expiry = invite.expires_at.map_or_else( | ||
| 43 | + || "never".to_string(), | ||
| 44 | + |at| format!("expires {}", millis_time(at, STAMP)), | ||
| 45 | + ); | ||
| 46 | + let (delete_icon, delete_tip, delete_color) = if pending { | ||
| 47 | + (IconName::Check, "Confirm", palette.on_accent) | ||
| 48 | + } else { | ||
| 49 | + (IconName::Trash, "Delete", palette.danger) | ||
| 50 | + }; | ||
| 51 | + | ||
| 52 | + div() | ||
| 53 | + .min_h(px(44.)) | ||
| 54 | + .px(px(14.)) | ||
| 55 | + .py(px(10.)) | ||
| 56 | + .flex() | ||
| 57 | + .items_center() | ||
| 58 | + .gap(px(10.)) | ||
| 59 | + .child( | ||
| 60 | + div() | ||
| 61 | + .flex_1() | ||
| 62 | + .min_w_0() | ||
| 63 | + .flex() | ||
| 64 | + .flex_col() | ||
| 65 | + .child( | ||
| 66 | + div() | ||
| 67 | + .font_weight(FontWeight::MEDIUM) | ||
| 68 | + .truncate() | ||
| 69 | + .child(invite.code.clone()), | ||
| 70 | + ) | ||
| 71 | + .child( | ||
| 72 | + div() | ||
| 73 | + .text_size(text(11.)) | ||
| 74 | + .text_color(palette.muted) | ||
| 75 | + .truncate() | ||
| 76 | + .child(format!( | ||
| 77 | + "{} {} left · {expiry}", | ||
| 78 | + invite.uses_left, | ||
| 79 | + if invite.uses_left == 1 { "use" } else { "uses" } | ||
| 80 | + )), | ||
| 81 | + ), | ||
| 82 | + ) | ||
| 83 | + .child(icon_button( | ||
| 84 | + format!("copy_invite_{}", invite.code), | ||
| 85 | + IconName::Copy, | ||
| 86 | + "Copy", | ||
| 87 | + palette.muted, | ||
| 88 | + palette, | ||
| 89 | + entity_action(entity, move |yumush, _, cx| { | ||
| 90 | + yumush.copy_invite(©_code, cx) | ||
| 91 | + }), | ||
| 92 | + )) | ||
| 93 | + .child(icon_button( | ||
| 94 | + format!("delete_invite_{}", invite.code), | ||
| 95 | + delete_icon, | ||
| 96 | + delete_tip, | ||
| 97 | + delete_color, | ||
| 98 | + palette, | ||
| 99 | + entity_action(entity, move |yumush, window, cx| { | ||
| 100 | + yumush.delete_invite(&delete_code, window, cx) | ||
| 101 | + }), | ||
| 102 | + )) | ||
| 103 | +} | ||
| 104 | + | ||
| 105 | +fn ban_row(ban: &ServerBan, entity: &Entity<Yumush>, palette: Palette) -> Div { | ||
| 106 | + let remove_id = ban.id.clone(); | ||
| 107 | + let kind = match ban.kind { | ||
| 108 | + ServerBanKind::Ip => "IP", | ||
| 109 | + ServerBanKind::Username => "Username", | ||
| 110 | + }; | ||
| 111 | + | ||
| 112 | + div() | ||
| 113 | + .min_h(px(44.)) | ||
| 114 | + .px(px(14.)) | ||
| 115 | + .py(px(10.)) | ||
| 116 | + .flex() | ||
| 117 | + .items_center() | ||
| 118 | + .gap(px(10.)) | ||
| 119 | + .child(pill(kind, palette.text, palette)) | ||
| 120 | + .child( | ||
| 121 | + div() | ||
| 122 | + .flex_1() | ||
| 123 | + .min_w_0() | ||
| 124 | + .flex() | ||
| 125 | + .flex_col() | ||
| 126 | + .child( | ||
| 127 | + div() | ||
| 128 | + .font_weight(FontWeight::MEDIUM) | ||
| 129 | + .truncate() | ||
| 130 | + .child(ban.value.clone()), | ||
| 131 | + ) | ||
| 132 | + .children(ban.reason.as_ref().map(|reason| { | ||
| 133 | + div() | ||
| 134 | + .text_size(text(11.)) | ||
| 135 | + .text_color(palette.muted) | ||
| 136 | + .truncate() | ||
| 137 | + .child(reason.clone()) | ||
| 138 | + })), | ||
| 139 | + ) | ||
| 140 | + .child(icon_button( | ||
| 141 | + format!("remove_ban_{}", ban.id), | ||
| 142 | + IconName::X, | ||
| 143 | + "Remove", | ||
| 144 | + palette.muted, | ||
| 145 | + palette, | ||
| 146 | + entity_action(entity, move |yumush, window, cx| { | ||
| 147 | + yumush.remove_ban(&remove_id, window, cx) | ||
| 148 | + }), | ||
| 149 | + )) | ||
| 150 | +} | ||
| 151 | + | ||
| 152 | +impl Yumush { | ||
| 153 | + pub(super) fn load_access(&self, window: &mut Window, cx: &mut Context<Self>) { | ||
| 154 | + self.load_invites(window, cx); | ||
| 155 | + self.load_server_bans(window, cx); | ||
| 156 | + } | ||
| 157 | + | ||
| 158 | + pub(super) fn load_invites(&self, window: &mut Window, cx: &mut Context<Self>) { | ||
| 159 | + if !self.is_builder() { | ||
| 160 | + return; | ||
| 161 | + } | ||
| 162 | + let network = self.network.clone(); | ||
| 163 | + | ||
| 164 | + cx.spawn_in(window, async move |this, cx| { | ||
| 165 | + let codes = match network.invite_codes().await { | ||
| 166 | + Ok(codes) => codes, | ||
| 167 | + Err(error) => { | ||
| 168 | + notify_error(&this, cx, error); | ||
| 169 | + return; | ||
| 170 | + } | ||
| 171 | + }; | ||
| 172 | + | ||
| 173 | + let _ = this.update(cx, |yumush, cx| { | ||
| 174 | + yumush.chat.builder_invites = codes; | ||
| 175 | + cx.notify(); | ||
| 176 | + }); | ||
| 177 | + }) | ||
| 178 | + .detach(); | ||
| 179 | + } | ||
| 180 | + | ||
| 181 | + pub(super) fn load_server_bans(&self, window: &mut Window, cx: &mut Context<Self>) { | ||
| 182 | + if !self.is_builder() { | ||
| 183 | + return; | ||
| 184 | + } | ||
| 185 | + let network = self.network.clone(); | ||
| 186 | + | ||
| 187 | + cx.spawn_in(window, async move |this, cx| { | ||
| 188 | + let bans = match network.server_bans().await { | ||
| 189 | + Ok(bans) => bans, | ||
| 190 | + Err(error) => { | ||
| 191 | + notify_error(&this, cx, error); | ||
| 192 | + return; | ||
| 193 | + } | ||
| 194 | + }; | ||
| 195 | + | ||
| 196 | + let _ = this.update(cx, |yumush, cx| { | ||
| 197 | + yumush.chat.builder_bans = bans; | ||
| 198 | + cx.notify(); | ||
| 199 | + }); | ||
| 200 | + }) | ||
| 201 | + .detach(); | ||
| 202 | + } | ||
| 203 | + | ||
| 204 | + pub(super) fn create_invite(&mut self, window: &mut Window, cx: &mut Context<Self>) { | ||
| 205 | + let uses = self.chat.invite_uses.read(cx).text().to_string(); | ||
| 206 | + let Ok(uses) = uses.trim().parse::<u32>() else { | ||
| 207 | + return; | ||
| 208 | + }; | ||
| 209 | + let hours = self.chat.invite_hours.read(cx).text().to_string(); | ||
| 210 | + let hours = match hours.trim() { | ||
| 211 | + "" => None, | ||
| 212 | + text => match text.parse::<u32>() { | ||
| 213 | + Ok(hours) => Some(hours), | ||
| 214 | + Err(_) => return, | ||
| 215 | + }, | ||
| 216 | + }; | ||
| 217 | + let network = self.network.clone(); | ||
| 218 | + | ||
| 219 | + cx.spawn_in(window, async move |this, cx| { | ||
| 220 | + match network.create_invite_code(uses, hours).await { | ||
| 221 | + Ok(_) => { | ||
| 222 | + let _ = | ||
| 223 | + this.update_in(cx, |yumush, window, cx| yumush.load_invites(window, cx)); | ||
| 224 | + } | ||
| 225 | + Err(error) => notify_error(&this, cx, error), | ||
| 226 | + } | ||
| 227 | + }) | ||
| 228 | + .detach(); | ||
| 229 | + } | ||
| 230 | + | ||
| 231 | + pub(super) fn delete_invite( | ||
| 232 | + &mut self, | ||
| 233 | + code: &str, | ||
| 234 | + window: &mut Window, | ||
| 235 | + cx: &mut Context<Self>, | ||
| 236 | + ) { | ||
| 237 | + if self.chat.pending_invite_delete.as_deref() != Some(code) { | ||
| 238 | + self.chat.pending_invite_delete = Some(code.to_string()); | ||
| 239 | + cx.notify(); | ||
| 240 | + return; | ||
| 241 | + } | ||
| 242 | + self.chat.pending_invite_delete = None; | ||
| 243 | + let network = self.network.clone(); | ||
| 244 | + let code = code.to_string(); | ||
| 245 | + | ||
| 246 | + cx.spawn_in(window, async move |this, cx| { | ||
| 247 | + match network.delete_invite_code(&code).await { | ||
| 248 | + Ok(()) => { | ||
| 249 | + let _ = | ||
| 250 | + this.update_in(cx, |yumush, window, cx| yumush.load_invites(window, cx)); | ||
| 251 | + } | ||
| 252 | + Err(error) => notify_error(&this, cx, error), | ||
| 253 | + } | ||
| 254 | + }) | ||
| 255 | + .detach(); | ||
| 256 | + } | ||
| 257 | + | ||
| 258 | + pub(super) fn copy_invite(&self, code: &str, cx: &mut Context<Self>) { | ||
| 259 | + cx.write_to_clipboard(ClipboardItem::new_string(code.to_string())); | ||
| 260 | + } | ||
| 261 | + | ||
| 262 | + pub(super) fn remove_ban(&mut self, id: &str, window: &mut Window, cx: &mut Context<Self>) { | ||
| 263 | + let network = self.network.clone(); | ||
| 264 | + let id = id.to_string(); | ||
| 265 | + | ||
| 266 | + cx.spawn_in(window, async move |this, cx| { | ||
| 267 | + match network.remove_server_ban(&id).await { | ||
| 268 | + Ok(()) => { | ||
| 269 | + let _ = this | ||
| 270 | + .update_in(cx, |yumush, window, cx| yumush.load_server_bans(window, cx)); | ||
| 271 | + } | ||
| 272 | + Err(error) => notify_error(&this, cx, error), | ||
| 273 | + } | ||
| 274 | + }) | ||
| 275 | + .detach(); | ||
| 276 | + } | ||
| 277 | + | ||
| 278 | + pub(super) fn access_body(&self, entity: &Entity<Self>, palette: Palette) -> Vec<AnyElement> { | ||
| 279 | + let controls = toolbar(vec![ | ||
| 280 | + div() | ||
| 281 | + .w(px(80.)) | ||
| 282 | + .child(field(palette, &self.chat.invite_uses)) | ||
| 283 | + .into_any_element(), | ||
| 284 | + div() | ||
| 285 | + .w(px(90.)) | ||
| 286 | + .child(field(palette, &self.chat.invite_hours)) | ||
| 287 | + .into_any_element(), | ||
| 288 | + accent_button( | ||
| 289 | + "create_invite", | ||
| 290 | + "Create", | ||
| 291 | + palette, | ||
| 292 | + entity_action(entity, |yumush, window, cx| { | ||
| 293 | + yumush.create_invite(window, cx) | ||
| 294 | + }), | ||
| 295 | + ) | ||
| 296 | + .into_any_element(), | ||
| 297 | + ]) | ||
| 298 | + .into_any_element(); | ||
| 299 | + let invite_header = card_header("Invite codes", vec![controls], palette); | ||
| 300 | + let invites = self.chat.builder_invites.iter().map(|invite| { | ||
| 301 | + let pending = self.chat.pending_invite_delete.as_deref() == Some(invite.code.as_str()); | ||
| 302 | + invite_row(invite, pending, entity, palette) | ||
| 303 | + }); | ||
| 304 | + let bans = self | ||
| 305 | + .chat | ||
| 306 | + .builder_bans | ||
| 307 | + .iter() | ||
| 308 | + .map(|ban| ban_row(ban, entity, palette)); | ||
| 309 | + let ban_header = card_header("Bans", Vec::new(), palette); | ||
| 310 | + | ||
| 311 | + vec![ | ||
| 312 | + list_card(invite_header, invites.collect(), "No invite codes", palette) | ||
| 313 | + .into_any_element(), | ||
| 314 | + list_card(ban_header, bans.collect(), "No bans", palette).into_any_element(), | ||
| 315 | + ] | ||
| 316 | + } | ||
| 317 | +} | ||
crates/client/src/gui/builder/communities.rsadded+474 -0
| @@ -0,0 +1,474 @@ | |||
| 1 | +use common::model::{Community, CommunitySummary, Member, Rank}; | ||
| 2 | +use gpui_kit::{ | ||
| 3 | + AnyElement, App, Context, Div, Entity, FontWeight, IntoElement, ParentElement, Styled, Window, | ||
| 4 | + assets::IconName, component::input::Input, div, prelude::FluentBuilder as _, px, | ||
| 5 | +}; | ||
| 6 | + | ||
| 7 | +use super::{ | ||
| 8 | + Transfer, | ||
| 9 | + ui::{card, icon_button, pill, plain_button}, | ||
| 10 | +}; | ||
| 11 | +use crate::gui::{ | ||
| 12 | + Palette, Yumush, avatar, chat::View, community_icon, community_settings::search_text, | ||
| 13 | + entity_action, notify_error, text, | ||
| 14 | +}; | ||
| 15 | + | ||
| 16 | +fn rows_body(rows: Vec<Div>, empty: &str, palette: Palette) -> Div { | ||
| 17 | + if rows.is_empty() { | ||
| 18 | + return div() | ||
| 19 | + .py(px(24.)) | ||
| 20 | + .flex() | ||
| 21 | + .justify_center() | ||
| 22 | + .text_size(text(12.)) | ||
| 23 | + .text_color(palette.muted) | ||
| 24 | + .child(empty.to_string()); | ||
| 25 | + } | ||
| 26 | + div() | ||
| 27 | + .flex() | ||
| 28 | + .flex_col() | ||
| 29 | + .children(rows.into_iter().enumerate().map(|(index, row)| { | ||
| 30 | + row.when(index > 0, |row| { | ||
| 31 | + row.border_t_1().border_color(palette.border) | ||
| 32 | + }) | ||
| 33 | + })) | ||
| 34 | +} | ||
| 35 | + | ||
| 36 | +impl Yumush { | ||
| 37 | + pub(super) fn set_builder_community_voice( | ||
| 38 | + &mut self, | ||
| 39 | + community_id: &str, | ||
| 40 | + enabled: bool, | ||
| 41 | + window: &mut Window, | ||
| 42 | + cx: &mut Context<Self>, | ||
| 43 | + ) { | ||
| 44 | + let network = self.network.clone(); | ||
| 45 | + let community_id = community_id.to_string(); | ||
| 46 | + | ||
| 47 | + cx.spawn_in(window, async move |this, cx| { | ||
| 48 | + match network.set_community_voice(&community_id, enabled).await { | ||
| 49 | + Ok(_) => { | ||
| 50 | + let _ = this.update_in(cx, |yumush, window, cx| { | ||
| 51 | + yumush.load_builder_communities(window, cx) | ||
| 52 | + }); | ||
| 53 | + } | ||
| 54 | + Err(error) => notify_error(&this, cx, error), | ||
| 55 | + } | ||
| 56 | + }) | ||
| 57 | + .detach(); | ||
| 58 | + } | ||
| 59 | + | ||
| 60 | + pub(super) fn load_builder_communities(&self, window: &mut Window, cx: &mut Context<Self>) { | ||
| 61 | + if !self.is_builder() { | ||
| 62 | + return; | ||
| 63 | + } | ||
| 64 | + let network = self.network.clone(); | ||
| 65 | + | ||
| 66 | + cx.spawn_in(window, async move |this, cx| { | ||
| 67 | + let summaries = match network.all_communities().await { | ||
| 68 | + Ok(summaries) => summaries, | ||
| 69 | + Err(error) => { | ||
| 70 | + notify_error(&this, cx, error); | ||
| 71 | + return; | ||
| 72 | + } | ||
| 73 | + }; | ||
| 74 | + | ||
| 75 | + let _ = this.update(cx, |yumush, cx| { | ||
| 76 | + for summary in &summaries { | ||
| 77 | + if let Some(avatar_id) = &summary.community.avatar_id { | ||
| 78 | + yumush.request_avatar(avatar_id, cx); | ||
| 79 | + } | ||
| 80 | + if let Some(owner) = &summary.owner { | ||
| 81 | + yumush.request_user_avatar(owner, cx); | ||
| 82 | + } | ||
| 83 | + } | ||
| 84 | + yumush.chat.builder_communities = summaries; | ||
| 85 | + cx.notify(); | ||
| 86 | + }); | ||
| 87 | + }) | ||
| 88 | + .detach(); | ||
| 89 | + } | ||
| 90 | + | ||
| 91 | + pub(super) fn delete_builder_community( | ||
| 92 | + &mut self, | ||
| 93 | + community_id: &str, | ||
| 94 | + window: &mut Window, | ||
| 95 | + cx: &mut Context<Self>, | ||
| 96 | + ) { | ||
| 97 | + if self.chat.pending_community_delete.as_deref() != Some(community_id) { | ||
| 98 | + self.chat.pending_community_delete = Some(community_id.to_string()); | ||
| 99 | + cx.notify(); | ||
| 100 | + return; | ||
| 101 | + } | ||
| 102 | + self.chat.pending_community_delete = None; | ||
| 103 | + let network = self.network.clone(); | ||
| 104 | + let community_id = community_id.to_string(); | ||
| 105 | + | ||
| 106 | + cx.spawn_in(window, async move |this, cx| { | ||
| 107 | + match network.delete_community(&community_id).await { | ||
| 108 | + Ok(()) => { | ||
| 109 | + let _ = this.update_in(cx, |yumush, window, cx| { | ||
| 110 | + yumush.chat.remove_community(&community_id); | ||
| 111 | + yumush.load_builder_communities(window, cx); | ||
| 112 | + }); | ||
| 113 | + } | ||
| 114 | + Err(error) => notify_error(&this, cx, error), | ||
| 115 | + } | ||
| 116 | + }) | ||
| 117 | + .detach(); | ||
| 118 | + } | ||
| 119 | + | ||
| 120 | + pub(super) fn open_transfer( | ||
| 121 | + &mut self, | ||
| 122 | + community_id: String, | ||
| 123 | + window: &mut Window, | ||
| 124 | + cx: &mut Context<Self>, | ||
| 125 | + ) { | ||
| 126 | + if self | ||
| 127 | + .chat | ||
| 128 | + .transfer | ||
| 129 | + .as_ref() | ||
| 130 | + .is_some_and(|transfer| transfer.community_id == community_id) | ||
| 131 | + { | ||
| 132 | + self.chat.transfer = None; | ||
| 133 | + cx.notify(); | ||
| 134 | + return; | ||
| 135 | + } | ||
| 136 | + self.chat.transfer = Some(Transfer { | ||
| 137 | + community_id: community_id.clone(), | ||
| 138 | + members: Vec::new(), | ||
| 139 | + pending: None, | ||
| 140 | + }); | ||
| 141 | + cx.notify(); | ||
| 142 | + self.load_transfer_members(community_id, window, cx); | ||
| 143 | + } | ||
| 144 | + | ||
| 145 | + pub(super) fn load_transfer_members( | ||
| 146 | + &self, | ||
| 147 | + community_id: String, | ||
| 148 | + window: &mut Window, | ||
| 149 | + cx: &mut Context<Self>, | ||
| 150 | + ) { | ||
| 151 | + let network = self.network.clone(); | ||
| 152 | + | ||
| 153 | + cx.spawn_in(window, async move |this, cx| { | ||
| 154 | + let members = match network.members(&community_id).await { | ||
| 155 | + Ok(members) => members, | ||
| 156 | + Err(error) => { | ||
| 157 | + notify_error(&this, cx, error); | ||
| 158 | + return; | ||
| 159 | + } | ||
| 160 | + }; | ||
| 161 | + | ||
| 162 | + let _ = this.update_in(cx, |yumush, window, cx| { | ||
| 163 | + for member in &members { | ||
| 164 | + yumush.request_user_avatar(&member.user, cx); | ||
| 165 | + } | ||
| 166 | + let Some(transfer) = yumush.chat.transfer.as_mut() else { | ||
| 167 | + return; | ||
| 168 | + }; | ||
| 169 | + if transfer.community_id == community_id { | ||
| 170 | + transfer.members = members; | ||
| 171 | + window.refresh(); | ||
| 172 | + cx.notify(); | ||
| 173 | + } | ||
| 174 | + }); | ||
| 175 | + }) | ||
| 176 | + .detach(); | ||
| 177 | + } | ||
| 178 | + | ||
| 179 | + pub(super) fn transfer_rows(&self, entity: &Entity<Self>, palette: Palette) -> Vec<AnyElement> { | ||
| 180 | + let Some(transfer) = &self.chat.transfer else { | ||
| 181 | + return Vec::new(); | ||
| 182 | + }; | ||
| 183 | + | ||
| 184 | + transfer | ||
| 185 | + .members | ||
| 186 | + .iter() | ||
| 187 | + .filter(|member| member.rank != Rank::Owner) | ||
| 188 | + .map(|member| { | ||
| 189 | + let pending = transfer.pending.as_deref() == Some(member.user.id.as_str()); | ||
| 190 | + self.transfer_row(member, pending, entity, palette) | ||
| 191 | + .into_any_element() | ||
| 192 | + }) | ||
| 193 | + .collect() | ||
| 194 | + } | ||
| 195 | + | ||
| 196 | + pub(super) fn transfer_to( | ||
| 197 | + &mut self, | ||
| 198 | + user_id: &str, | ||
| 199 | + window: &mut Window, | ||
| 200 | + cx: &mut Context<Self>, | ||
| 201 | + ) { | ||
| 202 | + let Some(transfer) = self.chat.transfer.as_mut() else { | ||
| 203 | + return; | ||
| 204 | + }; | ||
| 205 | + if transfer.pending.as_deref() != Some(user_id) { | ||
| 206 | + transfer.pending = Some(user_id.to_string()); | ||
| 207 | + window.refresh(); | ||
| 208 | + cx.notify(); | ||
| 209 | + return; | ||
| 210 | + } | ||
| 211 | + let community_id = transfer.community_id.clone(); | ||
| 212 | + let network = self.network.clone(); | ||
| 213 | + let user_id = user_id.to_string(); | ||
| 214 | + | ||
| 215 | + cx.spawn_in(window, async move |this, cx| { | ||
| 216 | + match network.transfer_ownership(&community_id, &user_id).await { | ||
| 217 | + Ok(()) => { | ||
| 218 | + let _ = this.update_in(cx, |yumush, window, cx| { | ||
| 219 | + yumush.chat.transfer = None; | ||
| 220 | + yumush.load_builder_communities(window, cx); | ||
| 221 | + }); | ||
| 222 | + } | ||
| 223 | + Err(error) => notify_error(&this, cx, error), | ||
| 224 | + } | ||
| 225 | + }) | ||
| 226 | + .detach(); | ||
| 227 | + } | ||
| 228 | + | ||
| 229 | + pub(super) fn open_builder_community( | ||
| 230 | + &mut self, | ||
| 231 | + community: Community, | ||
| 232 | + window: &mut Window, | ||
| 233 | + cx: &mut Context<Self>, | ||
| 234 | + ) { | ||
| 235 | + let community_id = community.id.clone(); | ||
| 236 | + self.chat.opened = Some(community); | ||
| 237 | + self.show_view(View::Chat, window, cx); | ||
| 238 | + self.select_community(community_id, window, cx); | ||
| 239 | + } | ||
| 240 | + | ||
| 241 | + pub(super) fn communities_body( | ||
| 242 | + &self, | ||
| 243 | + entity: &Entity<Self>, | ||
| 244 | + palette: Palette, | ||
| 245 | + cx: &App, | ||
| 246 | + ) -> Vec<AnyElement> { | ||
| 247 | + let query = search_text(&self.chat.builder_search, cx); | ||
| 248 | + let rows: Vec<Div> = self | ||
| 249 | + .chat | ||
| 250 | + .builder_communities | ||
| 251 | + .iter() | ||
| 252 | + .filter(|summary| { | ||
| 253 | + summary | ||
| 254 | + .community | ||
| 255 | + .name | ||
| 256 | + .to_lowercase() | ||
| 257 | + .contains(query.as_str()) | ||
| 258 | + }) | ||
| 259 | + .map(|summary| self.community_row(summary, entity, palette)) | ||
| 260 | + .collect(); | ||
| 261 | + | ||
| 262 | + vec![ | ||
| 263 | + card(palette) | ||
| 264 | + .child( | ||
| 265 | + div() | ||
| 266 | + .px(px(14.)) | ||
| 267 | + .py(px(10.)) | ||
| 268 | + .flex() | ||
| 269 | + .items_center() | ||
| 270 | + .border_b_1() | ||
| 271 | + .border_color(palette.border) | ||
| 272 | + .child( | ||
| 273 | + div() | ||
| 274 | + .w(px(240.)) | ||
| 275 | + .child(Input::new(&self.chat.builder_search)), | ||
| 276 | + ), | ||
| 277 | + ) | ||
| 278 | + .child(rows_body(rows, "No communities", palette)) | ||
| 279 | + .into_any_element(), | ||
| 280 | + ] | ||
| 281 | + } | ||
| 282 | + | ||
| 283 | + pub(super) fn transfer_row( | ||
| 284 | + &self, | ||
| 285 | + member: &Member, | ||
| 286 | + pending: bool, | ||
| 287 | + entity: &Entity<Self>, | ||
| 288 | + palette: Palette, | ||
| 289 | + ) -> Div { | ||
| 290 | + let user_id = member.user.id.clone(); | ||
| 291 | + | ||
| 292 | + div() | ||
| 293 | + .h(px(40.)) | ||
| 294 | + .px(px(12.)) | ||
| 295 | + .flex() | ||
| 296 | + .items_center() | ||
| 297 | + .gap(px(8.)) | ||
| 298 | + .child(avatar( | ||
| 299 | + palette, | ||
| 300 | + &member.user, | ||
| 301 | + px(20.), | ||
| 302 | + text(10.), | ||
| 303 | + &self.chat.avatars, | ||
| 304 | + )) | ||
| 305 | + .child( | ||
| 306 | + div() | ||
| 307 | + .flex_1() | ||
| 308 | + .min_w_0() | ||
| 309 | + .truncate() | ||
| 310 | + .child(member.user.username.clone()), | ||
| 311 | + ) | ||
| 312 | + .child(plain_button( | ||
| 313 | + format!("transfer_to_{}", member.user.id), | ||
| 314 | + if pending { "Confirm" } else { "Transfer" }, | ||
| 315 | + palette, | ||
| 316 | + entity_action(entity, move |yumush, window, cx| { | ||
| 317 | + yumush.transfer_to(&user_id, window, cx) | ||
| 318 | + }), | ||
| 319 | + )) | ||
| 320 | + } | ||
| 321 | + | ||
| 322 | + pub(super) fn community_row( | ||
| 323 | + &self, | ||
| 324 | + summary: &CommunitySummary, | ||
| 325 | + entity: &Entity<Self>, | ||
| 326 | + palette: Palette, | ||
| 327 | + ) -> Div { | ||
| 328 | + let community = &summary.community; | ||
| 329 | + let opened = community.clone(); | ||
| 330 | + let id = community.id.clone(); | ||
| 331 | + let log_id = id.clone(); | ||
| 332 | + let transfer_id = id.clone(); | ||
| 333 | + let delete_id = id.clone(); | ||
| 334 | + let voice_id = id.clone(); | ||
| 335 | + let voice_enabled = community.voice_enabled; | ||
| 336 | + let pending_delete = self.chat.pending_community_delete.as_deref() == Some(id.as_str()); | ||
| 337 | + let expanded = self | ||
| 338 | + .chat | ||
| 339 | + .transfer | ||
| 340 | + .as_ref() | ||
| 341 | + .is_some_and(|transfer| transfer.community_id == id); | ||
| 342 | + let owner = summary.owner.as_ref().map_or(String::new(), |owner| { | ||
| 343 | + format!(" · owner {}", owner.username) | ||
| 344 | + }); | ||
| 345 | + let meta = format!("{} members{owner}", summary.members); | ||
| 346 | + | ||
| 347 | + let row = div() | ||
| 348 | + .min_h(px(48.)) | ||
| 349 | + .px(px(14.)) | ||
| 350 | + .py(px(8.)) | ||
| 351 | + .flex() | ||
| 352 | + .items_center() | ||
| 353 | + .gap(px(10.)) | ||
| 354 | + .child(community_icon( | ||
| 355 | + palette, | ||
| 356 | + community, | ||
| 357 | + px(28.), | ||
| 358 | + text(11.), | ||
| 359 | + &self.chat.avatars, | ||
| 360 | + )) | ||
| 361 | + .child( | ||
| 362 | + div() | ||
| 363 | + .flex_1() | ||
| 364 | + .min_w_0() | ||
| 365 | + .flex() | ||
| 366 | + .flex_col() | ||
| 367 | + .child( | ||
| 368 | + div() | ||
| 369 | + .text_size(text(13.)) | ||
| 370 | + .font_weight(FontWeight::MEDIUM) | ||
| 371 | + .text_color(palette.text) | ||
| 372 | + .truncate() | ||
| 373 | + .child(community.name.clone()), | ||
| 374 | + ) | ||
| 375 | + .child( | ||
| 376 | + div() | ||
| 377 | + .text_size(text(11.)) | ||
| 378 | + .text_color(palette.muted) | ||
| 379 | + .truncate() | ||
| 380 | + .child(meta), | ||
| 381 | + ), | ||
| 382 | + ) | ||
| 383 | + .children((!voice_enabled).then(|| pill("Voice off", palette.muted, palette))) | ||
| 384 | + .child(icon_button( | ||
| 385 | + format!("open_community_{id}"), | ||
| 386 | + IconName::ArrowUpRight, | ||
| 387 | + "Open", | ||
| 388 | + palette.muted, | ||
| 389 | + palette, | ||
| 390 | + entity_action(entity, move |yumush, window, cx| { | ||
| 391 | + yumush.open_builder_community(opened.clone(), window, cx) | ||
| 392 | + }), | ||
| 393 | + )) | ||
| 394 | + .child(icon_button( | ||
| 395 | + format!("log_community_{id}"), | ||
| 396 | + IconName::ScrollText, | ||
| 397 | + "Log", | ||
| 398 | + palette.muted, | ||
| 399 | + palette, | ||
| 400 | + entity_action(entity, move |yumush, window, cx| { | ||
| 401 | + yumush.show_logs(log_id.clone(), window, cx) | ||
| 402 | + }), | ||
| 403 | + )) | ||
| 404 | + .child(icon_button( | ||
| 405 | + format!("voice_community_{id}"), | ||
| 406 | + if voice_enabled { | ||
| 407 | + IconName::Phone | ||
| 408 | + } else { | ||
| 409 | + IconName::PhoneOff | ||
| 410 | + }, | ||
| 411 | + if voice_enabled { | ||
| 412 | + "Voice on" | ||
| 413 | + } else { | ||
| 414 | + "Voice off" | ||
| 415 | + }, | ||
| 416 | + palette.muted, | ||
| 417 | + palette, | ||
| 418 | + entity_action(entity, move |yumush, window, cx| { | ||
| 419 | + yumush.set_builder_community_voice(&voice_id, !voice_enabled, window, cx) | ||
| 420 | + }), | ||
| 421 | + )) | ||
| 422 | + .child(icon_button( | ||
| 423 | + format!("transfer_community_{id}"), | ||
| 424 | + IconName::ArrowRightLeft, | ||
| 425 | + "Transfer", | ||
| 426 | + if expanded { | ||
| 427 | + palette.text | ||
| 428 | + } else { | ||
| 429 | + palette.muted | ||
| 430 | + }, | ||
| 431 | + palette, | ||
| 432 | + entity_action(entity, move |yumush, window, cx| { | ||
| 433 | + yumush.open_transfer(transfer_id.clone(), window, cx) | ||
| 434 | + }), | ||
| 435 | + )) | ||
| 436 | + .child( | ||
| 437 | + icon_button( | ||
| 438 | + format!("delete_community_{id}"), | ||
| 439 | + IconName::Trash, | ||
| 440 | + if pending_delete { | ||
| 441 | + "Confirm delete" | ||
| 442 | + } else { | ||
| 443 | + "Delete" | ||
| 444 | + }, | ||
| 445 | + if pending_delete { | ||
| 446 | + palette.on_accent | ||
| 447 | + } else { | ||
| 448 | + palette.danger | ||
| 449 | + }, | ||
| 450 | + palette, | ||
| 451 | + entity_action(entity, move |yumush, window, cx| { | ||
| 452 | + yumush.delete_builder_community(&delete_id, window, cx) | ||
| 453 | + }), | ||
| 454 | + ) | ||
| 455 | + .when(pending_delete, |this| this.bg(palette.danger)), | ||
| 456 | + ); | ||
| 457 | + | ||
| 458 | + div() | ||
| 459 | + .flex() | ||
| 460 | + .flex_col() | ||
| 461 | + .child(row) | ||
| 462 | + .children(expanded.then(|| { | ||
| 463 | + div().px(px(14.)).pb(px(10.)).child( | ||
| 464 | + div() | ||
| 465 | + .flex() | ||
| 466 | + .flex_col() | ||
| 467 | + .rounded(px(6.)) | ||
| 468 | + .border_1() | ||
| 469 | + .border_color(palette.border) | ||
| 470 | + .children(self.transfer_rows(entity, palette)), | ||
| 471 | + ) | ||
| 472 | + })) | ||
| 473 | + } | ||
| 474 | +} | ||
crates/client/src/gui/builder/log.rsadded+322 -0
| @@ -0,0 +1,322 @@ | |||
| 1 | +use common::{ | ||
| 2 | + model::{AdminAction, AdminLogEntry, action_detail, action_name}, | ||
| 3 | + time::{created_at_ms, now_ms}, | ||
| 4 | +}; | ||
| 5 | +use gpui_kit::{ | ||
| 6 | + AnyElement, App, Context, Div, Entity, IntoElement, ParentElement, Styled, Window, | ||
| 7 | + assets::IconName, | ||
| 8 | + component::{Icon, WindowExt, input::InputState, notification::Notification, select::Select}, | ||
| 9 | + div, | ||
| 10 | + prelude::FluentBuilder as _, | ||
| 11 | + px, | ||
| 12 | +}; | ||
| 13 | +use jiff::Timestamp; | ||
| 14 | + | ||
| 15 | +use super::ui::{accent_button, card, day_millis, list_row, pill, toolbar}; | ||
| 16 | +use crate::gui::STAMP; | ||
| 17 | +use crate::gui::{ | ||
| 18 | + Palette, Yumush, | ||
| 19 | + auth::field, | ||
| 20 | + community_settings::{search_text, user_name}, | ||
| 21 | + entity_action, id_time, millis_time, notify_error, text, | ||
| 22 | +}; | ||
| 23 | + | ||
| 24 | +fn builder_log_text(entry: &AdminLogEntry) -> String { | ||
| 25 | + let actor = user_name(&entry.actor); | ||
| 26 | + let target = user_name(&entry.target); | ||
| 27 | + let community = entry.community.as_deref().unwrap_or_default(); | ||
| 28 | + | ||
| 29 | + match &entry.action { | ||
| 30 | + AdminAction::RoleChanged { role } => format!("{actor} set {target} to {}", role.as_str()), | ||
| 31 | + AdminAction::UserDeleted => format!("{actor} deleted {target}"), | ||
| 32 | + AdminAction::UserSuspended => format!("{actor} suspended {target}"), | ||
| 33 | + AdminAction::UserUnsuspended => format!("{actor} unsuspended {target}"), | ||
| 34 | + AdminAction::SessionsRevoked => format!("{actor} revoked sessions of {target}"), | ||
| 35 | + AdminAction::PasswordReset => format!("{actor} reset password of {target}"), | ||
| 36 | + AdminAction::ProfileCleared => format!("{actor} cleared profile of {target}"), | ||
| 37 | + AdminAction::UserCreated => format!("{actor} created {target}"), | ||
| 38 | + AdminAction::CommunityDeleted => format!("{actor} deleted {community}"), | ||
| 39 | + AdminAction::OwnershipTransferred => { | ||
| 40 | + format!("{actor} transferred {community} to {target}") | ||
| 41 | + } | ||
| 42 | + AdminAction::SettingsChanged => format!("{actor} changed settings"), | ||
| 43 | + AdminAction::UserRenamed { from } => format!("{actor} renamed {from} to {target}"), | ||
| 44 | + AdminAction::UserTimedOut { minutes: 0 } => format!("{actor} cleared timeout of {target}"), | ||
| 45 | + AdminAction::UserTimedOut { minutes } => { | ||
| 46 | + format!("{actor} timed out {target} for {minutes} min") | ||
| 47 | + } | ||
| 48 | + AdminAction::UserBanned { .. } => format!("{actor} banned {target}"), | ||
| 49 | + AdminAction::BanRemoved => format!("{actor} removed a ban"), | ||
| 50 | + AdminAction::InviteCreated => format!("{actor} created an invite code"), | ||
| 51 | + AdminAction::InviteDeleted => format!("{actor} deleted an invite code"), | ||
| 52 | + AdminAction::ReportHandled { status } => { | ||
| 53 | + format!("{actor} marked a report of {target} as {}", status.as_str()) | ||
| 54 | + } | ||
| 55 | + AdminAction::MessageDeleted => { | ||
| 56 | + format!("{actor} deleted a message of {target} in {community}") | ||
| 57 | + } | ||
| 58 | + AdminAction::UserPurged { messages } => { | ||
| 59 | + format!("{actor} purged {target}, {messages} messages") | ||
| 60 | + } | ||
| 61 | + AdminAction::VoiceKicked => format!("{actor} kicked {target} from voice"), | ||
| 62 | + AdminAction::VoiceMuted { muted: true } => format!("{actor} muted {target} in voice"), | ||
| 63 | + AdminAction::VoiceMuted { muted: false } => format!("{actor} unmuted {target} in voice"), | ||
| 64 | + AdminAction::CommunityVoiceSet { enabled: true } => { | ||
| 65 | + format!("{actor} enabled voice in {community}") | ||
| 66 | + } | ||
| 67 | + AdminAction::CommunityVoiceSet { enabled: false } => { | ||
| 68 | + format!("{actor} disabled voice in {community}") | ||
| 69 | + } | ||
| 70 | + AdminAction::AttachmentDeleted => { | ||
| 71 | + format!("{actor} deleted an attachment of {target} in {community}") | ||
| 72 | + } | ||
| 73 | + AdminAction::GhostEnabled => format!("{actor} enabled ghost mode"), | ||
| 74 | + AdminAction::GhostDisabled => format!("{actor} disabled ghost mode"), | ||
| 75 | + AdminAction::GhostVisit => format!("{actor} visited {community} as ghost"), | ||
| 76 | + AdminAction::GhostVoice { seconds } => { | ||
| 77 | + format!("{actor} listened in {community} as ghost for {seconds}s") | ||
| 78 | + } | ||
| 79 | + } | ||
| 80 | +} | ||
| 81 | + | ||
| 82 | +fn csv_field(text: &str) -> String { | ||
| 83 | + format!("\"{}\"", text.replace('"', "\"\"")) | ||
| 84 | +} | ||
| 85 | + | ||
| 86 | +fn builder_log_csv(entries: &[AdminLogEntry]) -> String { | ||
| 87 | + let mut lines = vec!["time,actor,action,target,community,detail".to_string()]; | ||
| 88 | + for entry in entries { | ||
| 89 | + let time = created_at_ms(&entry.id) | ||
| 90 | + .and_then(|millis| Timestamp::from_millisecond(millis as i64).ok()) | ||
| 91 | + .map(|timestamp| timestamp.to_string()) | ||
| 92 | + .unwrap_or_default(); | ||
| 93 | + let actor = entry | ||
| 94 | + .actor | ||
| 95 | + .as_ref() | ||
| 96 | + .map_or_else(String::new, |user| user.username.clone()); | ||
| 97 | + let target = entry | ||
| 98 | + .target | ||
| 99 | + .as_ref() | ||
| 100 | + .map_or_else(String::new, |user| user.username.clone()); | ||
| 101 | + let fields = [ | ||
| 102 | + time, | ||
| 103 | + actor, | ||
| 104 | + action_name(&entry.action).to_string(), | ||
| 105 | + target, | ||
| 106 | + entry.community.clone().unwrap_or_default(), | ||
| 107 | + action_detail(&entry.action).unwrap_or_default(), | ||
| 108 | + ]; | ||
| 109 | + lines.push( | ||
| 110 | + fields | ||
| 111 | + .iter() | ||
| 112 | + .map(|field| csv_field(field)) | ||
| 113 | + .collect::<Vec<_>>() | ||
| 114 | + .join(","), | ||
| 115 | + ); | ||
| 116 | + } | ||
| 117 | + | ||
| 118 | + lines.join("\n") | ||
| 119 | +} | ||
| 120 | + | ||
| 121 | +fn action_icon(action: &AdminAction) -> IconName { | ||
| 122 | + match action { | ||
| 123 | + AdminAction::UserDeleted | ||
| 124 | + | AdminAction::CommunityDeleted | ||
| 125 | + | AdminAction::MessageDeleted | ||
| 126 | + | AdminAction::AttachmentDeleted | ||
| 127 | + | AdminAction::UserPurged { .. } => IconName::Trash, | ||
| 128 | + AdminAction::UserBanned { .. } | ||
| 129 | + | AdminAction::BanRemoved | ||
| 130 | + | AdminAction::UserSuspended | ||
| 131 | + | AdminAction::UserUnsuspended | ||
| 132 | + | AdminAction::UserTimedOut { .. } => IconName::Ban, | ||
| 133 | + AdminAction::RoleChanged { .. } | ||
| 134 | + | AdminAction::OwnershipTransferred | ||
| 135 | + | AdminAction::UserRenamed { .. } | ||
| 136 | + | AdminAction::ProfileCleared | ||
| 137 | + | AdminAction::PasswordReset | ||
| 138 | + | AdminAction::SessionsRevoked => IconName::UserCog, | ||
| 139 | + AdminAction::UserCreated => IconName::UserPlus, | ||
| 140 | + AdminAction::VoiceKicked | AdminAction::VoiceMuted { .. } => IconName::MicOff, | ||
| 141 | + AdminAction::CommunityVoiceSet { .. } => IconName::Mic, | ||
| 142 | + AdminAction::InviteCreated | AdminAction::InviteDeleted => IconName::Key, | ||
| 143 | + AdminAction::ReportHandled { .. } => IconName::Flag, | ||
| 144 | + AdminAction::SettingsChanged => IconName::Settings, | ||
| 145 | + AdminAction::GhostEnabled | ||
| 146 | + | AdminAction::GhostDisabled | ||
| 147 | + | AdminAction::GhostVisit | ||
| 148 | + | AdminAction::GhostVoice { .. } => IconName::Eye, | ||
| 149 | + } | ||
| 150 | +} | ||
| 151 | + | ||
| 152 | +fn fixed(width: f32, control: impl IntoElement) -> AnyElement { | ||
| 153 | + div() | ||
| 154 | + .w(px(width)) | ||
| 155 | + .flex_shrink_0() | ||
| 156 | + .child(control) | ||
| 157 | + .into_any_element() | ||
| 158 | +} | ||
| 159 | + | ||
| 160 | +fn builder_log_row(entry: &AdminLogEntry, palette: Palette) -> Div { | ||
| 161 | + list_row() | ||
| 162 | + .child( | ||
| 163 | + Icon::new(action_icon(&entry.action)) | ||
| 164 | + .w(px(14.)) | ||
| 165 | + .h(px(14.)) | ||
| 166 | + .text_color(palette.muted), | ||
| 167 | + ) | ||
| 168 | + .child( | ||
| 169 | + div() | ||
| 170 | + .flex_1() | ||
| 171 | + .min_w_0() | ||
| 172 | + .flex() | ||
| 173 | + .flex_col() | ||
| 174 | + .gap(px(2.)) | ||
| 175 | + .child( | ||
| 176 | + div() | ||
| 177 | + .truncate() | ||
| 178 | + .text_size(text(13.)) | ||
| 179 | + .text_color(palette.text) | ||
| 180 | + .child(builder_log_text(entry)), | ||
| 181 | + ) | ||
| 182 | + .child( | ||
| 183 | + div() | ||
| 184 | + .text_size(text(11.)) | ||
| 185 | + .text_color(palette.muted) | ||
| 186 | + .child(id_time(&entry.id, STAMP)), | ||
| 187 | + ), | ||
| 188 | + ) | ||
| 189 | + .children( | ||
| 190 | + entry | ||
| 191 | + .community | ||
| 192 | + .as_deref() | ||
| 193 | + .map(|community| pill(community, palette.accent, palette)), | ||
| 194 | + ) | ||
| 195 | +} | ||
| 196 | + | ||
| 197 | +impl Yumush { | ||
| 198 | + pub(super) fn load_builder_logs(&self, window: &mut Window, cx: &mut Context<Self>) { | ||
| 199 | + if !self.is_builder() { | ||
| 200 | + return; | ||
| 201 | + } | ||
| 202 | + let (Some(actor_id), Some(target_id)) = ( | ||
| 203 | + self.log_user(&self.chat.builder_log_actor, cx), | ||
| 204 | + self.log_user(&self.chat.builder_log_target, cx), | ||
| 205 | + ) else { | ||
| 206 | + window.push_notification(Notification::error("Unknown user"), cx); | ||
| 207 | + return; | ||
| 208 | + }; | ||
| 209 | + let action = self | ||
| 210 | + .chat | ||
| 211 | + .builder_log_action | ||
| 212 | + .read(cx) | ||
| 213 | + .selected_value() | ||
| 214 | + .copied() | ||
| 215 | + .filter(|name| *name != "All") | ||
| 216 | + .map(str::to_string); | ||
| 217 | + let after_ms = day_millis(&search_text(&self.chat.builder_log_from, cx), false); | ||
| 218 | + let before_ms = day_millis(&search_text(&self.chat.builder_log_to, cx), true); | ||
| 219 | + let network = self.network.clone(); | ||
| 220 | + | ||
| 221 | + cx.spawn_in(window, async move |this, cx| { | ||
| 222 | + let entries = match network | ||
| 223 | + .admin_logs(actor_id, target_id, action, after_ms, before_ms) | ||
| 224 | + .await | ||
| 225 | + { | ||
| 226 | + Ok(entries) => entries, | ||
| 227 | + Err(error) => { | ||
| 228 | + notify_error(&this, cx, error); | ||
| 229 | + return; | ||
| 230 | + } | ||
| 231 | + }; | ||
| 232 | + | ||
| 233 | + let _ = this.update(cx, |yumush, cx| { | ||
| 234 | + yumush.chat.builder_logs = entries; | ||
| 235 | + cx.notify(); | ||
| 236 | + }); | ||
| 237 | + }) | ||
| 238 | + .detach(); | ||
| 239 | + } | ||
| 240 | + | ||
| 241 | + pub(super) fn log_user(&self, input: &Entity<InputState>, cx: &App) -> Option<Option<String>> { | ||
| 242 | + let name = search_text(input, cx); | ||
| 243 | + if name.is_empty() { | ||
| 244 | + return Some(None); | ||
| 245 | + } | ||
| 246 | + | ||
| 247 | + self.resolve_user(&name).map(Some) | ||
| 248 | + } | ||
| 249 | + | ||
| 250 | + pub(super) fn export_builder_log(&self, window: &mut Window, cx: &mut Context<Self>) { | ||
| 251 | + let Some(directory) = dirs::download_dir() else { | ||
| 252 | + window.push_notification(Notification::error("No download folder"), cx); | ||
| 253 | + return; | ||
| 254 | + }; | ||
| 255 | + let name = format!( | ||
| 256 | + "yumush-admin-log-{}.csv", | ||
| 257 | + millis_time(now_ms(), "%Y%m%d-%H%M%S") | ||
| 258 | + ); | ||
| 259 | + let path = directory.join(name); | ||
| 260 | + match std::fs::write(&path, builder_log_csv(&self.chat.builder_logs)) { | ||
| 261 | + Ok(()) => { | ||
| 262 | + window.push_notification(Notification::success(path.display().to_string()), cx) | ||
| 263 | + } | ||
| 264 | + Err(error) => window.push_notification(Notification::error(error.to_string()), cx), | ||
| 265 | + } | ||
| 266 | + } | ||
| 267 | + | ||
| 268 | + pub(super) fn log_body(&self, entity: &Entity<Self>, palette: Palette) -> Vec<AnyElement> { | ||
| 269 | + let filters = toolbar(vec![ | ||
| 270 | + fixed(130., field(palette, &self.chat.builder_log_actor)), | ||
| 271 | + fixed(130., field(palette, &self.chat.builder_log_target)), | ||
| 272 | + fixed(160., Select::new(&self.chat.builder_log_action)), | ||
| 273 | + fixed(150., field(palette, &self.chat.builder_log_from)), | ||
| 274 | + fixed(150., field(palette, &self.chat.builder_log_to)), | ||
| 275 | + accent_button( | ||
| 276 | + "apply_builder_log", | ||
| 277 | + "Apply", | ||
| 278 | + palette, | ||
| 279 | + entity_action(entity, |yumush, window, cx| { | ||
| 280 | + yumush.load_builder_logs(window, cx) | ||
| 281 | + }), | ||
| 282 | + ) | ||
| 283 | + .into_any_element(), | ||
| 284 | + ]); | ||
| 285 | + let rows: Vec<Div> = self | ||
| 286 | + .chat | ||
| 287 | + .builder_logs | ||
| 288 | + .iter() | ||
| 289 | + .enumerate() | ||
| 290 | + .map(|(index, entry)| { | ||
| 291 | + builder_log_row(entry, palette).when(index > 0, |row| { | ||
| 292 | + row.border_t_1().border_color(palette.border) | ||
| 293 | + }) | ||
| 294 | + }) | ||
| 295 | + .collect(); | ||
| 296 | + let body = if rows.is_empty() { | ||
| 297 | + div() | ||
| 298 | + .py(px(24.)) | ||
| 299 | + .flex() | ||
| 300 | + .justify_center() | ||
| 301 | + .text_size(text(12.)) | ||
| 302 | + .text_color(palette.muted) | ||
| 303 | + .child("No entries") | ||
| 304 | + } else { | ||
| 305 | + div().flex().flex_col().children(rows) | ||
| 306 | + }; | ||
| 307 | + | ||
| 308 | + vec![ | ||
| 309 | + card(palette) | ||
| 310 | + .child( | ||
| 311 | + div() | ||
| 312 | + .px(px(14.)) | ||
| 313 | + .py(px(10.)) | ||
| 314 | + .border_b_1() | ||
| 315 | + .border_color(palette.border) | ||
| 316 | + .child(filters), | ||
| 317 | + ) | ||
| 318 | + .child(body) | ||
| 319 | + .into_any_element(), | ||
| 320 | + ] | ||
| 321 | + } | ||
| 322 | +} | ||
crates/client/src/gui/builder/messages.rsadded+250 -0
| @@ -0,0 +1,250 @@ | |||
| 1 | +use common::model::MessageHit; | ||
| 2 | +use gpui_kit::{ | ||
| 3 | + AnyElement, Context, Div, Entity, FontWeight, IntoElement, ParentElement, Styled, Window, | ||
| 4 | + assets::IconName, div, prelude::FluentBuilder as _, px, | ||
| 5 | +}; | ||
| 6 | + | ||
| 7 | +use super::ui::{accent_button, card, day_millis, icon_button, list_card, list_row}; | ||
| 8 | +use crate::gui::YEAR_STAMP; | ||
| 9 | +use crate::gui::{ | ||
| 10 | + Palette, Yumush, | ||
| 11 | + auth::field, | ||
| 12 | + avatar, | ||
| 13 | + community_settings::{search_text, user_name}, | ||
| 14 | + entity_action, id_time, notify_error, text, | ||
| 15 | +}; | ||
| 16 | + | ||
| 17 | +fn fixed(width: f32, control: impl IntoElement) -> Div { | ||
| 18 | + div().w(px(width)).flex_shrink_0().child(control) | ||
| 19 | +} | ||
| 20 | + | ||
| 21 | +fn message_hit_row( | ||
| 22 | + hit: &MessageHit, | ||
| 23 | + pending: bool, | ||
| 24 | + purge_pending: bool, | ||
| 25 | + picture: Div, | ||
| 26 | + entity: &Entity<Yumush>, | ||
| 27 | + palette: Palette, | ||
| 28 | +) -> Div { | ||
| 29 | + let message_id = hit.message.id.clone(); | ||
| 30 | + let delete = icon_button( | ||
| 31 | + format!("delete_message_{}", hit.message.id), | ||
| 32 | + IconName::Trash, | ||
| 33 | + if pending { "Confirm" } else { "Delete" }, | ||
| 34 | + palette.danger, | ||
| 35 | + palette, | ||
| 36 | + entity_action(entity, move |yumush, window, cx| { | ||
| 37 | + yumush.delete_builder_message(&message_id, window, cx) | ||
| 38 | + }), | ||
| 39 | + ) | ||
| 40 | + .when(pending, |this| { | ||
| 41 | + this.bg(palette.danger).text_color(palette.on_accent) | ||
| 42 | + }); | ||
| 43 | + let purge = hit.author.as_ref().map(|author| { | ||
| 44 | + let user_id = author.id.clone(); | ||
| 45 | + icon_button( | ||
| 46 | + format!("purge_message_user_{}", author.id), | ||
| 47 | + IconName::UserX, | ||
| 48 | + if purge_pending { | ||
| 49 | + "Confirm purge" | ||
| 50 | + } else { | ||
| 51 | + "Purge user" | ||
| 52 | + }, | ||
| 53 | + palette.danger, | ||
| 54 | + palette, | ||
| 55 | + entity_action(entity, move |yumush, window, cx| { | ||
| 56 | + yumush.purge_builder_user(&user_id, window, cx) | ||
| 57 | + }), | ||
| 58 | + ) | ||
| 59 | + .when(purge_pending, |this| { | ||
| 60 | + this.bg(palette.danger).text_color(palette.on_accent) | ||
| 61 | + }) | ||
| 62 | + }); | ||
| 63 | + | ||
| 64 | + list_row() | ||
| 65 | + .child(picture) | ||
| 66 | + .child( | ||
| 67 | + div() | ||
| 68 | + .flex_1() | ||
| 69 | + .min_w_0() | ||
| 70 | + .flex() | ||
| 71 | + .flex_col() | ||
| 72 | + .gap(px(2.)) | ||
| 73 | + .child( | ||
| 74 | + div() | ||
| 75 | + .flex() | ||
| 76 | + .items_center() | ||
| 77 | + .gap(px(6.)) | ||
| 78 | + .min_w_0() | ||
| 79 | + .child( | ||
| 80 | + div() | ||
| 81 | + .min_w_0() | ||
| 82 | + .truncate() | ||
| 83 | + .text_size(text(13.)) | ||
| 84 | + .font_weight(FontWeight::MEDIUM) | ||
| 85 | + .text_color(palette.text) | ||
| 86 | + .child(user_name(&hit.author).to_string()), | ||
| 87 | + ) | ||
| 88 | + .child( | ||
| 89 | + div() | ||
| 90 | + .min_w_0() | ||
| 91 | + .truncate() | ||
| 92 | + .text_size(text(11.)) | ||
| 93 | + .text_color(palette.muted) | ||
| 94 | + .child(hit.community.clone()), | ||
| 95 | + ) | ||
| 96 | + .child( | ||
| 97 | + div() | ||
| 98 | + .flex_shrink_0() | ||
| 99 | + .text_size(text(11.)) | ||
| 100 | + .text_color(palette.muted) | ||
| 101 | + .child(id_time(&hit.message.id, YEAR_STAMP)), | ||
| 102 | + ), | ||
| 103 | + ) | ||
| 104 | + .child( | ||
| 105 | + div() | ||
| 106 | + .text_size(text(13.)) | ||
| 107 | + .text_color(palette.body) | ||
| 108 | + .line_clamp(2) | ||
| 109 | + .child(hit.message.body.clone()), | ||
| 110 | + ), | ||
| 111 | + ) | ||
| 112 | + .child( | ||
| 113 | + div() | ||
| 114 | + .flex() | ||
| 115 | + .items_center() | ||
| 116 | + .gap(px(4.)) | ||
| 117 | + .flex_shrink_0() | ||
| 118 | + .child(delete) | ||
| 119 | + .children(purge), | ||
| 120 | + ) | ||
| 121 | +} | ||
| 122 | + | ||
| 123 | +impl Yumush { | ||
| 124 | + pub(super) fn messages_body(&self, entity: &Entity<Self>, palette: Palette) -> Vec<AnyElement> { | ||
| 125 | + let filters = div() | ||
| 126 | + .px(px(14.)) | ||
| 127 | + .py(px(12.)) | ||
| 128 | + .flex() | ||
| 129 | + .flex_col() | ||
| 130 | + .gap(px(8.)) | ||
| 131 | + .child( | ||
| 132 | + div().flex().child( | ||
| 133 | + div() | ||
| 134 | + .flex_1() | ||
| 135 | + .min_w_0() | ||
| 136 | + .child(field(palette, &self.chat.builder_message_text)), | ||
| 137 | + ), | ||
| 138 | + ) | ||
| 139 | + .child( | ||
| 140 | + div() | ||
| 141 | + .flex() | ||
| 142 | + .items_center() | ||
| 143 | + .gap(px(8.)) | ||
| 144 | + .child(fixed(180., field(palette, &self.chat.builder_message_user))) | ||
| 145 | + .child(fixed( | ||
| 146 | + 180., | ||
| 147 | + field(palette, &self.chat.builder_message_community), | ||
| 148 | + )) | ||
| 149 | + .child(fixed(150., field(palette, &self.chat.builder_message_from))) | ||
| 150 | + .child(fixed(150., field(palette, &self.chat.builder_message_to))) | ||
| 151 | + .child(accent_button( | ||
| 152 | + "search_messages", | ||
| 153 | + "Search", | ||
| 154 | + palette, | ||
| 155 | + entity_action(entity, |yumush, window, cx| { | ||
| 156 | + yumush.search_messages(window, cx) | ||
| 157 | + }), | ||
| 158 | + )), | ||
| 159 | + ); | ||
| 160 | + let rows: Vec<Div> = self | ||
| 161 | + .chat | ||
| 162 | + .builder_messages | ||
| 163 | + .iter() | ||
| 164 | + .map(|hit| { | ||
| 165 | + let pending = | ||
| 166 | + self.chat.pending_message_delete.as_deref() == Some(hit.message.id.as_str()); | ||
| 167 | + let purge_pending = hit.author.as_ref().is_some_and(|author| { | ||
| 168 | + self.chat.pending_builder_purge.as_deref() == Some(author.id.as_str()) | ||
| 169 | + }); | ||
| 170 | + let picture = match &hit.author { | ||
| 171 | + Some(author) => avatar(palette, author, px(28.), text(11.), &self.chat.avatars), | ||
| 172 | + None => div() | ||
| 173 | + .size(px(28.)) | ||
| 174 | + .flex_shrink_0() | ||
| 175 | + .rounded_full() | ||
| 176 | + .bg(palette.hover), | ||
| 177 | + }; | ||
| 178 | + message_hit_row(hit, pending, purge_pending, picture, entity, palette) | ||
| 179 | + }) | ||
| 180 | + .collect(); | ||
| 181 | + | ||
| 182 | + vec![ | ||
| 183 | + card(palette).child(filters).into_any_element(), | ||
| 184 | + list_card(None, rows, "No messages", palette).into_any_element(), | ||
| 185 | + ] | ||
| 186 | + } | ||
| 187 | + | ||
| 188 | + pub(super) fn search_messages(&mut self, window: &mut Window, cx: &mut Context<Self>) { | ||
| 189 | + let text = search_text(&self.chat.builder_message_text, cx); | ||
| 190 | + let username = search_text(&self.chat.builder_message_user, cx); | ||
| 191 | + let community = search_text(&self.chat.builder_message_community, cx); | ||
| 192 | + let user_id = self.resolve_user(&username); | ||
| 193 | + let community_id = self.resolve_community(&community); | ||
| 194 | + let after_ms = day_millis(&search_text(&self.chat.builder_message_from, cx), false); | ||
| 195 | + let before_ms = day_millis(&search_text(&self.chat.builder_message_to, cx), true); | ||
| 196 | + let text = (!text.is_empty()).then_some(text); | ||
| 197 | + let network = self.network.clone(); | ||
| 198 | + | ||
| 199 | + cx.spawn_in(window, async move |this, cx| { | ||
| 200 | + let hits = match network | ||
| 201 | + .search_messages(text, user_id, community_id, after_ms, before_ms) | ||
| 202 | + .await | ||
| 203 | + { | ||
| 204 | + Ok(hits) => hits, | ||
| 205 | + Err(error) => { | ||
| 206 | + notify_error(&this, cx, error); | ||
| 207 | + return; | ||
| 208 | + } | ||
| 209 | + }; | ||
| 210 | + | ||
| 211 | + let _ = this.update(cx, |yumush, cx| { | ||
| 212 | + yumush.chat.builder_messages = hits; | ||
| 213 | + cx.notify(); | ||
| 214 | + }); | ||
| 215 | + }) | ||
| 216 | + .detach(); | ||
| 217 | + } | ||
| 218 | + | ||
| 219 | + pub(super) fn delete_builder_message( | ||
| 220 | + &mut self, | ||
| 221 | + message_id: &str, | ||
| 222 | + window: &mut Window, | ||
| 223 | + cx: &mut Context<Self>, | ||
| 224 | + ) { | ||
| 225 | + if self.chat.pending_message_delete.as_deref() != Some(message_id) { | ||
| 226 | + self.chat.pending_message_delete = Some(message_id.to_string()); | ||
| 227 | + cx.notify(); | ||
| 228 | + return; | ||
| 229 | + } | ||
| 230 | + self.chat.pending_message_delete = None; | ||
| 231 | + let network = self.network.clone(); | ||
| 232 | + let message_id = message_id.to_string(); | ||
| 233 | + | ||
| 234 | + cx.spawn_in(window, async move |this, cx| { | ||
| 235 | + match network.delete_message(&message_id).await { | ||
| 236 | + Ok(()) => { | ||
| 237 | + let _ = this.update(cx, |yumush, cx| { | ||
| 238 | + yumush | ||
| 239 | + .chat | ||
| 240 | + .builder_messages | ||
| 241 | + .retain(|hit| hit.message.id != message_id); | ||
| 242 | + cx.notify(); | ||
| 243 | + }); | ||
| 244 | + } | ||
| 245 | + Err(error) => notify_error(&this, cx, error), | ||
| 246 | + } | ||
| 247 | + }) | ||
| 248 | + .detach(); | ||
| 249 | + } | ||
| 250 | +} | ||
crates/client/src/gui/builder/overview.rsadded+176 -0
| @@ -0,0 +1,176 @@ | |||
| 1 | +use common::{error::Error, model::DayStat, time::DAY_MILLIS}; | ||
| 2 | +use gpui_kit::{Context, Div, IntoElement, ParentElement, Styled, Window, div, px}; | ||
| 3 | +use jiff::Timestamp; | ||
| 4 | + | ||
| 5 | +use super::ui::{card, card_header, stat_tile}; | ||
| 6 | +use crate::gui::{DAY, Palette, Yumush, notify_error, storage, text}; | ||
| 7 | + | ||
| 8 | +const CHART_HEIGHT: f32 = 80.; | ||
| 9 | + | ||
| 10 | +fn day_label(day: u64) -> String { | ||
| 11 | + Timestamp::from_millisecond((day * DAY_MILLIS) as i64) | ||
| 12 | + .map(|timestamp| timestamp.strftime(DAY).to_string()) | ||
| 13 | + .unwrap_or_default() | ||
| 14 | +} | ||
| 15 | + | ||
| 16 | +fn tile_row(tiles: &[(&str, String)], palette: Palette) -> Div { | ||
| 17 | + div().flex().gap(px(12.)).children( | ||
| 18 | + tiles | ||
| 19 | + .iter() | ||
| 20 | + .map(|(label, value)| stat_tile(label, value, palette).flex_1().min_w(px(200.))), | ||
| 21 | + ) | ||
| 22 | +} | ||
| 23 | + | ||
| 24 | +fn bar_chart( | ||
| 25 | + title: &'static str, | ||
| 26 | + stats: &[DayStat], | ||
| 27 | + value: fn(&DayStat) -> u64, | ||
| 28 | + summary: u64, | ||
| 29 | + palette: Palette, | ||
| 30 | +) -> Div { | ||
| 31 | + let values: Vec<u64> = stats.iter().map(value).collect(); | ||
| 32 | + let maximum = values.iter().copied().max().unwrap_or_default(); | ||
| 33 | + let first = stats | ||
| 34 | + .first() | ||
| 35 | + .map(|stat| day_label(stat.day)) | ||
| 36 | + .unwrap_or_default(); | ||
| 37 | + let last = stats | ||
| 38 | + .last() | ||
| 39 | + .map(|stat| day_label(stat.day)) | ||
| 40 | + .unwrap_or_default(); | ||
| 41 | + | ||
| 42 | + card(palette) | ||
| 43 | + .flex_1() | ||
| 44 | + .min_w_0() | ||
| 45 | + .child(card_header( | ||
| 46 | + title, | ||
| 47 | + vec![ | ||
| 48 | + div() | ||
| 49 | + .text_size(text(11.)) | ||
| 50 | + .text_color(palette.muted) | ||
| 51 | + .child(summary.to_string()) | ||
| 52 | + .into_any_element(), | ||
| 53 | + ], | ||
| 54 | + palette, | ||
| 55 | + )) | ||
| 56 | + .child( | ||
| 57 | + div() | ||
| 58 | + .h(px(140.)) | ||
| 59 | + .px(px(14.)) | ||
| 60 | + .py(px(12.)) | ||
| 61 | + .flex() | ||
| 62 | + .flex_col() | ||
| 63 | + .gap(px(4.)) | ||
| 64 | + .child( | ||
| 65 | + div() | ||
| 66 | + .text_size(text(10.)) | ||
| 67 | + .text_color(palette.muted) | ||
| 68 | + .child(maximum.to_string()), | ||
| 69 | + ) | ||
| 70 | + .child( | ||
| 71 | + div() | ||
| 72 | + .h(px(CHART_HEIGHT)) | ||
| 73 | + .flex() | ||
| 74 | + .items_end() | ||
| 75 | + .gap(px(2.)) | ||
| 76 | + .children(values.iter().map(|&value| { | ||
| 77 | + let (height, color) = if value == 0 { | ||
| 78 | + (2., palette.hover) | ||
| 79 | + } else { | ||
| 80 | + ( | ||
| 81 | + (CHART_HEIGHT * value as f32 / maximum as f32).max(2.), | ||
| 82 | + palette.accent.opacity(0.85), | ||
| 83 | + ) | ||
| 84 | + }; | ||
| 85 | + | ||
| 86 | + div().flex_1().h(px(height)).rounded_t(px(2.)).bg(color) | ||
| 87 | + })), | ||
| 88 | + ) | ||
| 89 | + .child( | ||
| 90 | + div() | ||
| 91 | + .pt(px(4.)) | ||
| 92 | + .border_t_1() | ||
| 93 | + .border_color(palette.border) | ||
| 94 | + .flex() | ||
| 95 | + .justify_between() | ||
| 96 | + .text_size(text(11.)) | ||
| 97 | + .text_color(palette.muted) | ||
| 98 | + .child(first) | ||
| 99 | + .child(last), | ||
| 100 | + ), | ||
| 101 | + ) | ||
| 102 | +} | ||
| 103 | + | ||
| 104 | +impl Yumush { | ||
| 105 | + pub(super) fn load_overview(&self, window: &mut Window, cx: &mut Context<Self>) { | ||
| 106 | + if !self.is_builder() { | ||
| 107 | + return; | ||
| 108 | + } | ||
| 109 | + let network = self.network.clone(); | ||
| 110 | + | ||
| 111 | + cx.spawn_in(window, async move |this, cx| { | ||
| 112 | + let loaded = async { | ||
| 113 | + Ok::<_, Error>((network.overview().await?, network.daily_stats(30).await?)) | ||
| 114 | + } | ||
| 115 | + .await; | ||
| 116 | + let (overview, stats) = match loaded { | ||
| 117 | + Ok(loaded) => loaded, | ||
| 118 | + Err(error) => { | ||
| 119 | + notify_error(&this, cx, error); | ||
| 120 | + return; | ||
| 121 | + } | ||
| 122 | + }; | ||
| 123 | + | ||
| 124 | + let _ = this.update(cx, |yumush, cx| { | ||
| 125 | + yumush.chat.builder_overview = Some(overview); | ||
| 126 | + yumush.chat.builder_stats = stats; | ||
| 127 | + cx.notify(); | ||
| 128 | + }); | ||
| 129 | + }) | ||
| 130 | + .detach(); | ||
| 131 | + } | ||
| 132 | + | ||
| 133 | + pub(super) fn overview_rows(&self, palette: Palette) -> Vec<Div> { | ||
| 134 | + let Some(overview) = &self.chat.builder_overview else { | ||
| 135 | + return Vec::new(); | ||
| 136 | + }; | ||
| 137 | + let stats = &self.chat.builder_stats; | ||
| 138 | + let active = stats.last().map_or(0, |stat| stat.active_users); | ||
| 139 | + let messages = stats.iter().map(|stat| stat.messages).sum::<u64>(); | ||
| 140 | + | ||
| 141 | + let tiles = [ | ||
| 142 | + ("Users", overview.users.to_string()), | ||
| 143 | + ("Builders", overview.builders.to_string()), | ||
| 144 | + ("Communities", overview.communities.to_string()), | ||
| 145 | + ("Online", overview.online.to_string()), | ||
| 146 | + ("In voice", overview.voice.to_string()), | ||
| 147 | + ("Storage", storage(overview.storage_bytes)), | ||
| 148 | + ]; | ||
| 149 | + | ||
| 150 | + let mut rows: Vec<Div> = tiles | ||
| 151 | + .chunks(3) | ||
| 152 | + .map(|chunk| tile_row(chunk, palette)) | ||
| 153 | + .collect(); | ||
| 154 | + rows.push( | ||
| 155 | + div() | ||
| 156 | + .flex() | ||
| 157 | + .gap(px(12.)) | ||
| 158 | + .child(bar_chart( | ||
| 159 | + "Active users", | ||
| 160 | + stats, | ||
| 161 | + |stat| stat.active_users, | ||
| 162 | + active, | ||
| 163 | + palette, | ||
| 164 | + )) | ||
| 165 | + .child(bar_chart( | ||
| 166 | + "Messages", | ||
| 167 | + stats, | ||
| 168 | + |stat| stat.messages, | ||
| 169 | + messages, | ||
| 170 | + palette, | ||
| 171 | + )), | ||
| 172 | + ); | ||
| 173 | + | ||
| 174 | + rows | ||
| 175 | + } | ||
| 176 | +} | ||
crates/client/src/gui/builder/reports.rsadded+393 -0
| @@ -0,0 +1,393 @@ | |||
| 1 | +use std::future::Future; | ||
| 2 | + | ||
| 3 | +use common::{ | ||
| 4 | + error::Error, | ||
| 5 | + model::{Report, ReportStatus}, | ||
| 6 | +}; | ||
| 7 | +use gpui_kit::{ | ||
| 8 | + AnyElement, Context, Div, Entity, FontWeight, Hsla, IntoElement, ParentElement, Styled, Window, | ||
| 9 | + div, px, | ||
| 10 | +}; | ||
| 11 | + | ||
| 12 | +use super::ui::{danger_button, list_card, pill, plain_button}; | ||
| 13 | +use crate::gui::STAMP; | ||
| 14 | +use crate::{ | ||
| 15 | + gui::{ | ||
| 16 | + Palette, Yumush, avatar, badge, clickable, | ||
| 17 | + community_settings::user_name, | ||
| 18 | + entity_action, id_time, notify_error, | ||
| 19 | + settings_page::{segment, segment_track}, | ||
| 20 | + text, | ||
| 21 | + }, | ||
| 22 | + network::NetworkHandle, | ||
| 23 | +}; | ||
| 24 | + | ||
| 25 | +const REPORT_FILTERS: [(Option<ReportStatus>, &str); 4] = [ | ||
| 26 | + (Some(ReportStatus::Open), "Open"), | ||
| 27 | + (Some(ReportStatus::Resolved), "Resolved"), | ||
| 28 | + (Some(ReportStatus::Dismissed), "Dismissed"), | ||
| 29 | + (None, "All"), | ||
| 30 | +]; | ||
| 31 | + | ||
| 32 | +fn status_label(status: ReportStatus) -> &'static str { | ||
| 33 | + match status { | ||
| 34 | + ReportStatus::Open => "Open", | ||
| 35 | + ReportStatus::Resolved => "Resolved", | ||
| 36 | + ReportStatus::Dismissed => "Dismissed", | ||
| 37 | + } | ||
| 38 | +} | ||
| 39 | + | ||
| 40 | +fn status_color(status: ReportStatus, palette: Palette) -> Hsla { | ||
| 41 | + match status { | ||
| 42 | + ReportStatus::Open => palette.accent, | ||
| 43 | + ReportStatus::Resolved => palette.ok, | ||
| 44 | + ReportStatus::Dismissed => palette.muted, | ||
| 45 | + } | ||
| 46 | +} | ||
| 47 | + | ||
| 48 | +impl Yumush { | ||
| 49 | + pub(super) fn load_open_reports(&self, window: &mut Window, cx: &mut Context<Self>) { | ||
| 50 | + if !self.is_builder() { | ||
| 51 | + return; | ||
| 52 | + } | ||
| 53 | + let network = self.network.clone(); | ||
| 54 | + | ||
| 55 | + cx.spawn_in(window, async move |this, cx| { | ||
| 56 | + let count = match network.reports(Some(ReportStatus::Open)).await { | ||
| 57 | + Ok(reports) => reports.len(), | ||
| 58 | + Err(error) => { | ||
| 59 | + notify_error(&this, cx, error); | ||
| 60 | + return; | ||
| 61 | + } | ||
| 62 | + }; | ||
| 63 | + | ||
| 64 | + let _ = this.update(cx, |yumush, cx| { | ||
| 65 | + yumush.chat.open_reports = count; | ||
| 66 | + cx.notify(); | ||
| 67 | + }); | ||
| 68 | + }) | ||
| 69 | + .detach(); | ||
| 70 | + } | ||
| 71 | + | ||
| 72 | + pub(super) fn load_reports(&self, window: &mut Window, cx: &mut Context<Self>) { | ||
| 73 | + if !self.is_builder() { | ||
| 74 | + return; | ||
| 75 | + } | ||
| 76 | + let status = self.chat.report_filter; | ||
| 77 | + let network = self.network.clone(); | ||
| 78 | + | ||
| 79 | + cx.spawn_in(window, async move |this, cx| { | ||
| 80 | + let reports = match network.reports(status).await { | ||
| 81 | + Ok(reports) => reports, | ||
| 82 | + Err(error) => { | ||
| 83 | + notify_error(&this, cx, error); | ||
| 84 | + return; | ||
| 85 | + } | ||
| 86 | + }; | ||
| 87 | + | ||
| 88 | + let _ = this.update(cx, |yumush, cx| { | ||
| 89 | + yumush.chat.builder_reports = reports; | ||
| 90 | + cx.notify(); | ||
| 91 | + }); | ||
| 92 | + }) | ||
| 93 | + .detach(); | ||
| 94 | + } | ||
| 95 | + | ||
| 96 | + pub(super) fn refresh_reports(&self, window: &mut Window, cx: &mut Context<Self>) { | ||
| 97 | + self.load_reports(window, cx); | ||
| 98 | + self.load_open_reports(window, cx); | ||
| 99 | + } | ||
| 100 | + | ||
| 101 | + pub(super) fn select_report_filter( | ||
| 102 | + &mut self, | ||
| 103 | + status: Option<ReportStatus>, | ||
| 104 | + window: &mut Window, | ||
| 105 | + cx: &mut Context<Self>, | ||
| 106 | + ) { | ||
| 107 | + self.chat.report_filter = status; | ||
| 108 | + self.load_reports(window, cx); | ||
| 109 | + cx.notify(); | ||
| 110 | + } | ||
| 111 | + | ||
| 112 | + pub(super) fn report_action<Fut>( | ||
| 113 | + &mut self, | ||
| 114 | + window: &mut Window, | ||
| 115 | + cx: &mut Context<Self>, | ||
| 116 | + action: impl FnOnce(NetworkHandle) -> Fut + 'static, | ||
| 117 | + ) where | ||
| 118 | + Fut: Future<Output = Result<(), Error>> + 'static, | ||
| 119 | + { | ||
| 120 | + let network = self.network.clone(); | ||
| 121 | + | ||
| 122 | + cx.spawn_in(window, async move |this, cx| match action(network).await { | ||
| 123 | + Ok(()) => { | ||
| 124 | + let _ = this.update_in(cx, |yumush, window, cx| yumush.refresh_reports(window, cx)); | ||
| 125 | + } | ||
| 126 | + Err(error) => notify_error(&this, cx, error), | ||
| 127 | + }) | ||
| 128 | + .detach(); | ||
| 129 | + } | ||
| 130 | + | ||
| 131 | + pub(super) fn handle_report( | ||
| 132 | + &mut self, | ||
| 133 | + report_id: &str, | ||
| 134 | + status: ReportStatus, | ||
| 135 | + window: &mut Window, | ||
| 136 | + cx: &mut Context<Self>, | ||
| 137 | + ) { | ||
| 138 | + let report_id = report_id.to_string(); | ||
| 139 | + self.report_action(window, cx, move |network| async move { | ||
| 140 | + network.handle_report(&report_id, status).await | ||
| 141 | + }); | ||
| 142 | + } | ||
| 143 | + | ||
| 144 | + pub(super) fn delete_report_message( | ||
| 145 | + &mut self, | ||
| 146 | + message_id: &str, | ||
| 147 | + window: &mut Window, | ||
| 148 | + cx: &mut Context<Self>, | ||
| 149 | + ) { | ||
| 150 | + let message_id = message_id.to_string(); | ||
| 151 | + self.report_action(window, cx, move |network| async move { | ||
| 152 | + network.delete_message(&message_id).await | ||
| 153 | + }); | ||
| 154 | + } | ||
| 155 | + | ||
| 156 | + pub(super) fn timeout_report_target( | ||
| 157 | + &mut self, | ||
| 158 | + user_id: &str, | ||
| 159 | + window: &mut Window, | ||
| 160 | + cx: &mut Context<Self>, | ||
| 161 | + ) { | ||
| 162 | + let user_id = user_id.to_string(); | ||
| 163 | + self.report_action(window, cx, move |network| async move { | ||
| 164 | + network.timeout_user(&user_id, 60).await | ||
| 165 | + }); | ||
| 166 | + } | ||
| 167 | + | ||
| 168 | + pub(super) fn ban_report_target( | ||
| 169 | + &mut self, | ||
| 170 | + user_id: &str, | ||
| 171 | + reason: &str, | ||
| 172 | + window: &mut Window, | ||
| 173 | + cx: &mut Context<Self>, | ||
| 174 | + ) { | ||
| 175 | + let user_id = user_id.to_string(); | ||
| 176 | + let reason = reason.to_string(); | ||
| 177 | + self.report_action(window, cx, move |network| async move { | ||
| 178 | + network | ||
| 179 | + .ban_user(&user_id, Some(reason.as_str()), false) | ||
| 180 | + .await | ||
| 181 | + }); | ||
| 182 | + } | ||
| 183 | + | ||
| 184 | + pub(super) fn report_filters(&self, entity: &Entity<Self>, palette: Palette) -> Div { | ||
| 185 | + div() | ||
| 186 | + .flex() | ||
| 187 | + .child( | ||
| 188 | + segment_track(palette).children(REPORT_FILTERS.map(|(status, label)| { | ||
| 189 | + segment( | ||
| 190 | + clickable( | ||
| 191 | + label, | ||
| 192 | + palette, | ||
| 193 | + entity_action(entity, move |yumush, window, cx| { | ||
| 194 | + yumush.select_report_filter(status, window, cx) | ||
| 195 | + }), | ||
| 196 | + ), | ||
| 197 | + self.chat.report_filter == status, | ||
| 198 | + palette, | ||
| 199 | + ) | ||
| 200 | + .px(px(10.)) | ||
| 201 | + .child(label) | ||
| 202 | + })), | ||
| 203 | + ) | ||
| 204 | + } | ||
| 205 | + | ||
| 206 | + pub(super) fn reports_body(&self, entity: &Entity<Self>, palette: Palette) -> Vec<AnyElement> { | ||
| 207 | + let rows: Vec<Div> = self | ||
| 208 | + .chat | ||
| 209 | + .builder_reports | ||
| 210 | + .iter() | ||
| 211 | + .map(|report| self.report_row(report, entity, palette)) | ||
| 212 | + .collect(); | ||
| 213 | + | ||
| 214 | + vec![ | ||
| 215 | + self.report_filters(entity, palette).into_any_element(), | ||
| 216 | + list_card(None, rows, "No reports", palette).into_any_element(), | ||
| 217 | + ] | ||
| 218 | + } | ||
| 219 | + | ||
| 220 | + pub(super) fn report_row( | ||
| 221 | + &self, | ||
| 222 | + report: &Report, | ||
| 223 | + entity: &Entity<Self>, | ||
| 224 | + palette: Palette, | ||
| 225 | + ) -> Div { | ||
| 226 | + let target_avatar = match &report.target { | ||
| 227 | + Some(user) => { | ||
| 228 | + avatar(palette, user, px(20.), text(9.), &self.chat.avatars).into_any_element() | ||
| 229 | + } | ||
| 230 | + None => badge(palette, &report.id, "someone", px(20.), text(9.)).into_any_element(), | ||
| 231 | + }; | ||
| 232 | + let head = div() | ||
| 233 | + .flex_1() | ||
| 234 | + .min_w_0() | ||
| 235 | + .flex() | ||
| 236 | + .items_center() | ||
| 237 | + .gap(px(8.)) | ||
| 238 | + .child(target_avatar) | ||
| 239 | + .child( | ||
| 240 | + div() | ||
| 241 | + .font_weight(FontWeight::MEDIUM) | ||
| 242 | + .truncate() | ||
| 243 | + .child(user_name(&report.target).to_string()), | ||
| 244 | + ) | ||
| 245 | + .child(pill( | ||
| 246 | + status_label(report.status), | ||
| 247 | + status_color(report.status, palette), | ||
| 248 | + palette, | ||
| 249 | + )) | ||
| 250 | + .child( | ||
| 251 | + div() | ||
| 252 | + .min_w_0() | ||
| 253 | + .text_size(text(11.)) | ||
| 254 | + .text_color(palette.muted) | ||
| 255 | + .truncate() | ||
| 256 | + .child(format!("reported by {}", user_name(&report.reporter))), | ||
| 257 | + ); | ||
| 258 | + let meta = div() | ||
| 259 | + .flex_shrink_0() | ||
| 260 | + .text_size(text(11.)) | ||
| 261 | + .text_color(palette.muted) | ||
| 262 | + .child(format!( | ||
| 263 | + "{} · {}", | ||
| 264 | + report.community.as_deref().unwrap_or("deleted community"), | ||
| 265 | + id_time(&report.id, STAMP) | ||
| 266 | + )); | ||
| 267 | + let actions = self.report_actions(report, entity, palette); | ||
| 268 | + let handled = (report.status != ReportStatus::Open).then(|| { | ||
| 269 | + div() | ||
| 270 | + .text_size(text(11.)) | ||
| 271 | + .text_color(palette.muted) | ||
| 272 | + .child(format!("by {}", user_name(&report.handled_by))) | ||
| 273 | + }); | ||
| 274 | + | ||
| 275 | + div() | ||
| 276 | + .px(px(14.)) | ||
| 277 | + .py(px(10.)) | ||
| 278 | + .flex() | ||
| 279 | + .flex_col() | ||
| 280 | + .gap(px(6.)) | ||
| 281 | + .child( | ||
| 282 | + div() | ||
| 283 | + .flex() | ||
| 284 | + .items_center() | ||
| 285 | + .gap(px(8.)) | ||
| 286 | + .child(head) | ||
| 287 | + .child(meta), | ||
| 288 | + ) | ||
| 289 | + .child( | ||
| 290 | + div() | ||
| 291 | + .text_size(text(13.)) | ||
| 292 | + .text_color(palette.body) | ||
| 293 | + .child(report.reason.clone()), | ||
| 294 | + ) | ||
| 295 | + .children(report.body.as_ref().map(|body| { | ||
| 296 | + div() | ||
| 297 | + .px(px(8.)) | ||
| 298 | + .py(px(6.)) | ||
| 299 | + .rounded(px(6.)) | ||
| 300 | + .bg(palette.window) | ||
| 301 | + .text_size(text(12.)) | ||
| 302 | + .text_color(palette.muted) | ||
| 303 | + .line_clamp(2) | ||
| 304 | + .child(body.clone()) | ||
| 305 | + })) | ||
| 306 | + .children(handled) | ||
| 307 | + .children((!actions.is_empty()).then(|| { | ||
| 308 | + div() | ||
| 309 | + .flex() | ||
| 310 | + .flex_wrap() | ||
| 311 | + .items_center() | ||
| 312 | + .gap(px(4.)) | ||
| 313 | + .children(actions) | ||
| 314 | + })) | ||
| 315 | + } | ||
| 316 | + | ||
| 317 | + pub(super) fn report_actions( | ||
| 318 | + &self, | ||
| 319 | + report: &Report, | ||
| 320 | + entity: &Entity<Self>, | ||
| 321 | + palette: Palette, | ||
| 322 | + ) -> Vec<AnyElement> { | ||
| 323 | + if report.status != ReportStatus::Open { | ||
| 324 | + return Vec::new(); | ||
| 325 | + } | ||
| 326 | + let resolve_id = report.id.clone(); | ||
| 327 | + let dismiss_id = report.id.clone(); | ||
| 328 | + let mut buttons = vec![ | ||
| 329 | + plain_button( | ||
| 330 | + format!("resolve_report_{}", report.id), | ||
| 331 | + "Resolve", | ||
| 332 | + palette, | ||
| 333 | + entity_action(entity, move |yumush, window, cx| { | ||
| 334 | + yumush.handle_report(&resolve_id, ReportStatus::Resolved, window, cx) | ||
| 335 | + }), | ||
| 336 | + ) | ||
| 337 | + .into_any_element(), | ||
| 338 | + plain_button( | ||
| 339 | + format!("dismiss_report_{}", report.id), | ||
| 340 | + "Dismiss", | ||
| 341 | + palette, | ||
| 342 | + entity_action(entity, move |yumush, window, cx| { | ||
| 343 | + yumush.handle_report(&dismiss_id, ReportStatus::Dismissed, window, cx) | ||
| 344 | + }), | ||
| 345 | + ) | ||
| 346 | + .into_any_element(), | ||
| 347 | + ]; | ||
| 348 | + if let (Some(message_id), Some(_)) = (&report.message_id, &report.community_id) { | ||
| 349 | + let message_id = message_id.clone(); | ||
| 350 | + buttons.push( | ||
| 351 | + plain_button( | ||
| 352 | + format!("delete_report_message_{}", report.id), | ||
| 353 | + "Delete message", | ||
| 354 | + palette, | ||
| 355 | + entity_action(entity, move |yumush, window, cx| { | ||
| 356 | + yumush.delete_report_message(&message_id, window, cx) | ||
| 357 | + }), | ||
| 358 | + ) | ||
| 359 | + .into_any_element(), | ||
| 360 | + ); | ||
| 361 | + } | ||
| 362 | + if let Some(target) = &report.target { | ||
| 363 | + let timeout_id = target.id.clone(); | ||
| 364 | + let ban_id = target.id.clone(); | ||
| 365 | + let ban_reason = report.reason.clone(); | ||
| 366 | + buttons.push( | ||
| 367 | + plain_button( | ||
| 368 | + format!("timeout_report_{}", report.id), | ||
| 369 | + "Timeout 1h", | ||
| 370 | + palette, | ||
| 371 | + entity_action(entity, move |yumush, window, cx| { | ||
| 372 | + yumush.timeout_report_target(&timeout_id, window, cx) | ||
| 373 | + }), | ||
| 374 | + ) | ||
| 375 | + .into_any_element(), | ||
| 376 | + ); | ||
| 377 | + buttons.push( | ||
| 378 | + danger_button( | ||
| 379 | + format!("ban_report_{}", report.id), | ||
| 380 | + "Ban", | ||
| 381 | + false, | ||
| 382 | + palette, | ||
| 383 | + entity_action(entity, move |yumush, window, cx| { | ||
| 384 | + yumush.ban_report_target(&ban_id, &ban_reason, window, cx) | ||
| 385 | + }), | ||
| 386 | + ) | ||
| 387 | + .into_any_element(), | ||
| 388 | + ); | ||
| 389 | + } | ||
| 390 | + | ||
| 391 | + buttons | ||
| 392 | + } | ||
| 393 | +} | ||
crates/client/src/gui/builder/server.rsadded+288 -0
| @@ -0,0 +1,288 @@ | |||
| 1 | +use common::model::ServerSettings; | ||
| 2 | +use gpui_kit::{AnyElement, Context, Entity, IntoElement, Window}; | ||
| 3 | + | ||
| 4 | +use super::ui::{ | ||
| 5 | + field_row, kilobytes_bytes, kilobytes_text, list_card, megabytes_bytes, megabytes_text, number, | ||
| 6 | +}; | ||
| 7 | +use crate::gui::{Palette, Yumush, auth::field, notify_error, settings_page::segmented}; | ||
| 8 | + | ||
| 9 | +impl Yumush { | ||
| 10 | + pub(super) fn fill_server_form(&mut self, window: &mut Window, cx: &mut Context<Self>) { | ||
| 11 | + let Some(settings) = self.server.clone() else { | ||
| 12 | + return; | ||
| 13 | + }; | ||
| 14 | + self.chat.server_registration_open = settings.registration_open; | ||
| 15 | + self.chat.server_maintenance = settings.maintenance; | ||
| 16 | + self.chat.server_name.update(cx, |input, cx| { | ||
| 17 | + input.set_value(settings.name.as_str(), window, cx) | ||
| 18 | + }); | ||
| 19 | + self.chat.server_limit.update(cx, |input, cx| { | ||
| 20 | + input.set_value( | ||
| 21 | + megabytes_text(settings.max_attachment_bytes).as_str(), | ||
| 22 | + window, | ||
| 23 | + cx, | ||
| 24 | + ) | ||
| 25 | + }); | ||
| 26 | + self.chat.server_user_quota.update(cx, |input, cx| { | ||
| 27 | + input.set_value( | ||
| 28 | + megabytes_text(settings.user_quota_bytes).as_str(), | ||
| 29 | + window, | ||
| 30 | + cx, | ||
| 31 | + ) | ||
| 32 | + }); | ||
| 33 | + self.chat.server_community_quota.update(cx, |input, cx| { | ||
| 34 | + input.set_value( | ||
| 35 | + megabytes_text(settings.community_quota_bytes).as_str(), | ||
| 36 | + window, | ||
| 37 | + cx, | ||
| 38 | + ) | ||
| 39 | + }); | ||
| 40 | + self.chat.server_message_retention.update(cx, |input, cx| { | ||
| 41 | + input.set_value( | ||
| 42 | + settings.message_retention_days.to_string().as_str(), | ||
| 43 | + window, | ||
| 44 | + cx, | ||
| 45 | + ) | ||
| 46 | + }); | ||
| 47 | + self.chat | ||
| 48 | + .server_attachment_retention | ||
| 49 | + .update(cx, |input, cx| { | ||
| 50 | + input.set_value( | ||
| 51 | + settings.attachment_retention_days.to_string().as_str(), | ||
| 52 | + window, | ||
| 53 | + cx, | ||
| 54 | + ) | ||
| 55 | + }); | ||
| 56 | + self.chat.server_announcement.update(cx, |input, cx| { | ||
| 57 | + input.set_value(settings.announcement.as_str(), window, cx) | ||
| 58 | + }); | ||
| 59 | + self.chat.server_max_communities.update(cx, |input, cx| { | ||
| 60 | + input.set_value( | ||
| 61 | + settings.max_communities_per_user.to_string().as_str(), | ||
| 62 | + window, | ||
| 63 | + cx, | ||
| 64 | + ) | ||
| 65 | + }); | ||
| 66 | + self.chat.server_max_message_length.update(cx, |input, cx| { | ||
| 67 | + input.set_value(settings.max_message_length.to_string().as_str(), window, cx) | ||
| 68 | + }); | ||
| 69 | + self.chat.server_max_avatar.update(cx, |input, cx| { | ||
| 70 | + input.set_value( | ||
| 71 | + kilobytes_text(settings.max_avatar_bytes).as_str(), | ||
| 72 | + window, | ||
| 73 | + cx, | ||
| 74 | + ) | ||
| 75 | + }); | ||
| 76 | + self.chat | ||
| 77 | + .server_messages_per_minute | ||
| 78 | + .update(cx, |input, cx| { | ||
| 79 | + input.set_value( | ||
| 80 | + settings.messages_per_minute.to_string().as_str(), | ||
| 81 | + window, | ||
| 82 | + cx, | ||
| 83 | + ) | ||
| 84 | + }); | ||
| 85 | + self.chat.server_logins_per_minute.update(cx, |input, cx| { | ||
| 86 | + input.set_value(settings.logins_per_minute.to_string().as_str(), window, cx) | ||
| 87 | + }); | ||
| 88 | + } | ||
| 89 | + | ||
| 90 | + pub(super) fn save_server_settings(&mut self, window: &mut Window, cx: &mut Context<Self>) { | ||
| 91 | + let name = self.chat.server_name.read(cx).text().to_string(); | ||
| 92 | + let max_attachment_bytes = | ||
| 93 | + megabytes_bytes(&self.chat.server_limit.read(cx).text().to_string()); | ||
| 94 | + let user_quota_bytes = | ||
| 95 | + megabytes_bytes(&self.chat.server_user_quota.read(cx).text().to_string()); | ||
| 96 | + let community_quota_bytes = | ||
| 97 | + megabytes_bytes(&self.chat.server_community_quota.read(cx).text().to_string()); | ||
| 98 | + let message_retention_days = number( | ||
| 99 | + &self | ||
| 100 | + .chat | ||
| 101 | + .server_message_retention | ||
| 102 | + .read(cx) | ||
| 103 | + .text() | ||
| 104 | + .to_string(), | ||
| 105 | + ); | ||
| 106 | + let attachment_retention_days = number( | ||
| 107 | + &self | ||
| 108 | + .chat | ||
| 109 | + .server_attachment_retention | ||
| 110 | + .read(cx) | ||
| 111 | + .text() | ||
| 112 | + .to_string(), | ||
| 113 | + ); | ||
| 114 | + let announcement = self.chat.server_announcement.read(cx).text().to_string(); | ||
| 115 | + let max_communities_per_user = | ||
| 116 | + number(&self.chat.server_max_communities.read(cx).text().to_string()); | ||
| 117 | + let max_message_length = number( | ||
| 118 | + &self | ||
| 119 | + .chat | ||
| 120 | + .server_max_message_length | ||
| 121 | + .read(cx) | ||
| 122 | + .text() | ||
| 123 | + .to_string(), | ||
| 124 | + ); | ||
| 125 | + let max_avatar_bytes = | ||
| 126 | + kilobytes_bytes(&self.chat.server_max_avatar.read(cx).text().to_string()); | ||
| 127 | + let messages_per_minute = number( | ||
| 128 | + &self | ||
| 129 | + .chat | ||
| 130 | + .server_messages_per_minute | ||
| 131 | + .read(cx) | ||
| 132 | + .text() | ||
| 133 | + .to_string(), | ||
| 134 | + ); | ||
| 135 | + let logins_per_minute = number( | ||
| 136 | + &self | ||
| 137 | + .chat | ||
| 138 | + .server_logins_per_minute | ||
| 139 | + .read(cx) | ||
| 140 | + .text() | ||
| 141 | + .to_string(), | ||
| 142 | + ); | ||
| 143 | + let settings = ServerSettings { | ||
| 144 | + name, | ||
| 145 | + registration_open: self.chat.server_registration_open, | ||
| 146 | + max_attachment_bytes, | ||
| 147 | + maintenance: self.chat.server_maintenance, | ||
| 148 | + user_quota_bytes, | ||
| 149 | + community_quota_bytes, | ||
| 150 | + message_retention_days, | ||
| 151 | + attachment_retention_days, | ||
| 152 | + announcement, | ||
| 153 | + max_communities_per_user, | ||
| 154 | + max_message_length, | ||
| 155 | + max_avatar_bytes, | ||
| 156 | + messages_per_minute, | ||
| 157 | + logins_per_minute, | ||
| 158 | + }; | ||
| 159 | + let network = self.network.clone(); | ||
| 160 | + | ||
| 161 | + cx.spawn_in(window, async move |this, cx| { | ||
| 162 | + match network.update_server_settings(settings).await { | ||
| 163 | + Ok(settings) => { | ||
| 164 | + let _ = this.update(cx, |yumush, cx| { | ||
| 165 | + yumush.server = Some(settings); | ||
| 166 | + cx.notify(); | ||
| 167 | + }); | ||
| 168 | + } | ||
| 169 | + Err(error) => notify_error(&this, cx, error), | ||
| 170 | + } | ||
| 171 | + }) | ||
| 172 | + .detach(); | ||
| 173 | + } | ||
| 174 | + | ||
| 175 | + pub(super) fn server_body(&self, entity: &Entity<Self>, palette: Palette) -> Vec<AnyElement> { | ||
| 176 | + let general = vec![ | ||
| 177 | + field_row( | ||
| 178 | + "Name", | ||
| 179 | + None, | ||
| 180 | + field(palette, &self.chat.server_name), | ||
| 181 | + palette, | ||
| 182 | + ), | ||
| 183 | + field_row( | ||
| 184 | + "Registration", | ||
| 185 | + None, | ||
| 186 | + segmented( | ||
| 187 | + [(true, "Open"), (false, "Closed")], | ||
| 188 | + self.chat.server_registration_open, | ||
| 189 | + entity, | ||
| 190 | + palette, | ||
| 191 | + |yumush, open, _| yumush.chat.server_registration_open = open, | ||
| 192 | + ), | ||
| 193 | + palette, | ||
| 194 | + ), | ||
| 195 | + field_row( | ||
| 196 | + "Maintenance", | ||
| 197 | + None, | ||
| 198 | + segmented( | ||
| 199 | + [(false, "Off"), (true, "On")], | ||
| 200 | + self.chat.server_maintenance, | ||
| 201 | + entity, | ||
| 202 | + palette, | ||
| 203 | + |yumush, on, _| yumush.chat.server_maintenance = on, | ||
| 204 | + ), | ||
| 205 | + palette, | ||
| 206 | + ), | ||
| 207 | + field_row( | ||
| 208 | + "Announcement", | ||
| 209 | + None, | ||
| 210 | + field(palette, &self.chat.server_announcement), | ||
| 211 | + palette, | ||
| 212 | + ), | ||
| 213 | + ]; | ||
| 214 | + let limits = vec![ | ||
| 215 | + field_row( | ||
| 216 | + "Max attachment (MB)", | ||
| 217 | + None, | ||
| 218 | + field(palette, &self.chat.server_limit), | ||
| 219 | + palette, | ||
| 220 | + ), | ||
| 221 | + field_row( | ||
| 222 | + "Max avatar (KB)", | ||
| 223 | + None, | ||
| 224 | + field(palette, &self.chat.server_max_avatar), | ||
| 225 | + palette, | ||
| 226 | + ), | ||
| 227 | + field_row( | ||
| 228 | + "Max message length", | ||
| 229 | + None, | ||
| 230 | + field(palette, &self.chat.server_max_message_length), | ||
| 231 | + palette, | ||
| 232 | + ), | ||
| 233 | + field_row( | ||
| 234 | + "Communities per user", | ||
| 235 | + Some("0 = unlimited"), | ||
| 236 | + field(palette, &self.chat.server_max_communities), | ||
| 237 | + palette, | ||
| 238 | + ), | ||
| 239 | + ]; | ||
| 240 | + let storage = vec![ | ||
| 241 | + field_row( | ||
| 242 | + "User quota (MB)", | ||
| 243 | + Some("0 = unlimited"), | ||
| 244 | + field(palette, &self.chat.server_user_quota), | ||
| 245 | + palette, | ||
| 246 | + ), | ||
| 247 | + field_row( | ||
| 248 | + "Community quota (MB)", | ||
| 249 | + Some("0 = unlimited"), | ||
| 250 | + field(palette, &self.chat.server_community_quota), | ||
| 251 | + palette, | ||
| 252 | + ), | ||
| 253 | + field_row( | ||
| 254 | + "Message retention (days)", | ||
| 255 | + Some("0 = off"), | ||
| 256 | + field(palette, &self.chat.server_message_retention), | ||
| 257 | + palette, | ||
| 258 | + ), | ||
| 259 | + field_row( | ||
| 260 | + "Attachment retention (days)", | ||
| 261 | + Some("0 = off"), | ||
| 262 | + field(palette, &self.chat.server_attachment_retention), | ||
| 263 | + palette, | ||
| 264 | + ), | ||
| 265 | + ]; | ||
| 266 | + let rate = vec![ | ||
| 267 | + field_row( | ||
| 268 | + "Messages / min", | ||
| 269 | + Some("0 = off"), | ||
| 270 | + field(palette, &self.chat.server_messages_per_minute), | ||
| 271 | + palette, | ||
| 272 | + ), | ||
| 273 | + field_row( | ||
| 274 | + "Logins / min", | ||
| 275 | + Some("0 = off"), | ||
| 276 | + field(palette, &self.chat.server_logins_per_minute), | ||
| 277 | + palette, | ||
| 278 | + ), | ||
| 279 | + ]; | ||
| 280 | + | ||
| 281 | + vec![ | ||
| 282 | + list_card(Some("General"), general, "", palette).into_any_element(), | ||
| 283 | + list_card(Some("Limits"), limits, "", palette).into_any_element(), | ||
| 284 | + list_card(Some("Storage"), storage, "", palette).into_any_element(), | ||
| 285 | + list_card(Some("Rate limits"), rate, "", palette).into_any_element(), | ||
| 286 | + ] | ||
| 287 | + } | ||
| 288 | +} | ||
crates/client/src/gui/builder/storage.rsadded+338 -0
| @@ -0,0 +1,338 @@ | |||
| 1 | +use common::model::AttachmentInfo; | ||
| 2 | +use gpui_kit::{ | ||
| 3 | + AnyElement, Context, Div, Entity, FontWeight, IntoElement, ParentElement, Styled, Window, | ||
| 4 | + assets::IconName, component::Icon, div, prelude::FluentBuilder as _, px, relative, | ||
| 5 | +}; | ||
| 6 | + | ||
| 7 | +use super::ui::{accent_button, card, card_header, list_card, list_row, toolbar}; | ||
| 8 | +use crate::gui::{ | ||
| 9 | + DATE, Palette, Tooltip, Yumush, | ||
| 10 | + auth::field, | ||
| 11 | + clickable, | ||
| 12 | + community_settings::{search_text, user_name}, | ||
| 13 | + entity_action, id_time, notify_error, storage, text, | ||
| 14 | +}; | ||
| 15 | + | ||
| 16 | +fn fixed(width: f32, control: impl IntoElement) -> Div { | ||
| 17 | + div().w(px(width)).flex_shrink_0().child(control) | ||
| 18 | +} | ||
| 19 | + | ||
| 20 | +fn share(bytes: u64, max: u64) -> f32 { | ||
| 21 | + if max == 0 { | ||
| 22 | + 0. | ||
| 23 | + } else { | ||
| 24 | + bytes as f32 / max as f32 | ||
| 25 | + } | ||
| 26 | +} | ||
| 27 | + | ||
| 28 | +fn usage_row(name: &str, bytes: u64, count: u64, fraction: f32, palette: Palette) -> Div { | ||
| 29 | + div() | ||
| 30 | + .px(px(14.)) | ||
| 31 | + .py(px(8.)) | ||
| 32 | + .flex() | ||
| 33 | + .flex_col() | ||
| 34 | + .gap(px(6.)) | ||
| 35 | + .child( | ||
| 36 | + div() | ||
| 37 | + .flex() | ||
| 38 | + .items_center() | ||
| 39 | + .gap(px(8.)) | ||
| 40 | + .child( | ||
| 41 | + div() | ||
| 42 | + .flex_1() | ||
| 43 | + .min_w_0() | ||
| 44 | + .truncate() | ||
| 45 | + .text_size(text(13.)) | ||
| 46 | + .text_color(palette.text) | ||
| 47 | + .child(name.to_string()), | ||
| 48 | + ) | ||
| 49 | + .child( | ||
| 50 | + div() | ||
| 51 | + .flex_shrink_0() | ||
| 52 | + .text_size(text(13.)) | ||
| 53 | + .font_weight(FontWeight::MEDIUM) | ||
| 54 | + .text_color(palette.text) | ||
| 55 | + .child(storage(bytes)), | ||
| 56 | + ) | ||
| 57 | + .child( | ||
| 58 | + div() | ||
| 59 | + .w(px(48.)) | ||
| 60 | + .flex_shrink_0() | ||
| 61 | + .flex() | ||
| 62 | + .justify_end() | ||
| 63 | + .text_size(text(11.)) | ||
| 64 | + .text_color(palette.muted) | ||
| 65 | + .child(count.to_string()), | ||
| 66 | + ), | ||
| 67 | + ) | ||
| 68 | + .child( | ||
| 69 | + div().h(px(3.)).rounded(px(3.)).bg(palette.hover).child( | ||
| 70 | + div() | ||
| 71 | + .h_full() | ||
| 72 | + .w(relative(fraction)) | ||
| 73 | + .rounded(px(3.)) | ||
| 74 | + .bg(palette.accent), | ||
| 75 | + ), | ||
| 76 | + ) | ||
| 77 | +} | ||
| 78 | + | ||
| 79 | +fn attachment_row( | ||
| 80 | + info: &AttachmentInfo, | ||
| 81 | + pending: bool, | ||
| 82 | + entity: &Entity<Yumush>, | ||
| 83 | + palette: Palette, | ||
| 84 | +) -> Div { | ||
| 85 | + let attachment_id = info.attachment.id.clone(); | ||
| 86 | + let delete = clickable( | ||
| 87 | + format!("delete_attachment_{}", info.attachment.id), | ||
| 88 | + palette, | ||
| 89 | + entity_action(entity, move |yumush, window, cx| { | ||
| 90 | + yumush.delete_builder_attachment(&attachment_id, window, cx) | ||
| 91 | + }), | ||
| 92 | + ) | ||
| 93 | + .tip(if pending { "Confirm" } else { "Delete" }) | ||
| 94 | + .size(px(26.)) | ||
| 95 | + .flex() | ||
| 96 | + .flex_shrink_0() | ||
| 97 | + .items_center() | ||
| 98 | + .justify_center() | ||
| 99 | + .rounded(px(6.)) | ||
| 100 | + .text_color(palette.danger) | ||
| 101 | + .when(pending, |this| { | ||
| 102 | + this.bg(palette.danger).text_color(palette.on_accent) | ||
| 103 | + }) | ||
| 104 | + .child(Icon::new(IconName::Trash).w(px(14.)).h(px(14.))); | ||
| 105 | + | ||
| 106 | + list_row() | ||
| 107 | + .child( | ||
| 108 | + Icon::new(IconName::File) | ||
| 109 | + .w(px(16.)) | ||
| 110 | + .h(px(16.)) | ||
| 111 | + .flex_shrink_0() | ||
| 112 | + .text_color(palette.muted), | ||
| 113 | + ) | ||
| 114 | + .child( | ||
| 115 | + div() | ||
| 116 | + .flex_1() | ||
| 117 | + .min_w_0() | ||
| 118 | + .flex() | ||
| 119 | + .flex_col() | ||
| 120 | + .gap(px(2.)) | ||
| 121 | + .child( | ||
| 122 | + div() | ||
| 123 | + .truncate() | ||
| 124 | + .text_size(text(13.)) | ||
| 125 | + .text_color(palette.text) | ||
| 126 | + .child(info.attachment.name.clone()), | ||
| 127 | + ) | ||
| 128 | + .child( | ||
| 129 | + div() | ||
| 130 | + .truncate() | ||
| 131 | + .text_size(text(11.)) | ||
| 132 | + .text_color(palette.muted) | ||
| 133 | + .child(format!( | ||
| 134 | + "{} · {} · {}", | ||
| 135 | + user_name(&info.uploader), | ||
| 136 | + info.community, | ||
| 137 | + id_time(&info.attachment.id, DATE) | ||
| 138 | + )), | ||
| 139 | + ), | ||
| 140 | + ) | ||
| 141 | + .child( | ||
| 142 | + div() | ||
| 143 | + .flex_shrink_0() | ||
| 144 | + .text_size(text(12.)) | ||
| 145 | + .text_color(palette.muted) | ||
| 146 | + .child(storage(info.attachment.size)), | ||
| 147 | + ) | ||
| 148 | + .child(delete) | ||
| 149 | +} | ||
| 150 | + | ||
| 151 | +impl Yumush { | ||
| 152 | + pub(super) fn storage_body(&self, entity: &Entity<Self>, palette: Palette) -> Vec<AnyElement> { | ||
| 153 | + let user_max = self | ||
| 154 | + .chat | ||
| 155 | + .builder_usage_users | ||
| 156 | + .iter() | ||
| 157 | + .map(|usage| usage.bytes) | ||
| 158 | + .max() | ||
| 159 | + .unwrap_or(0); | ||
| 160 | + let community_max = self | ||
| 161 | + .chat | ||
| 162 | + .builder_usage_communities | ||
| 163 | + .iter() | ||
| 164 | + .map(|usage| usage.bytes) | ||
| 165 | + .max() | ||
| 166 | + .unwrap_or(0); | ||
| 167 | + let users: Vec<Div> = self | ||
| 168 | + .chat | ||
| 169 | + .builder_usage_users | ||
| 170 | + .iter() | ||
| 171 | + .map(|usage| { | ||
| 172 | + usage_row( | ||
| 173 | + &usage.user.username, | ||
| 174 | + usage.bytes, | ||
| 175 | + usage.count, | ||
| 176 | + share(usage.bytes, user_max), | ||
| 177 | + palette, | ||
| 178 | + ) | ||
| 179 | + }) | ||
| 180 | + .collect(); | ||
| 181 | + let communities: Vec<Div> = self | ||
| 182 | + .chat | ||
| 183 | + .builder_usage_communities | ||
| 184 | + .iter() | ||
| 185 | + .map(|usage| { | ||
| 186 | + usage_row( | ||
| 187 | + &usage.community, | ||
| 188 | + usage.bytes, | ||
| 189 | + usage.count, | ||
| 190 | + share(usage.bytes, community_max), | ||
| 191 | + palette, | ||
| 192 | + ) | ||
| 193 | + }) | ||
| 194 | + .collect(); | ||
| 195 | + let usage = | ||
| 196 | + div() | ||
| 197 | + .flex() | ||
| 198 | + .gap(px(20.)) | ||
| 199 | + .child( | ||
| 200 | + div() | ||
| 201 | + .flex_1() | ||
| 202 | + .min_w_0() | ||
| 203 | + .child(list_card(Some("Users"), users, "No data", palette).h_full()), | ||
| 204 | + ) | ||
| 205 | + .child(div().flex_1().min_w_0().child( | ||
| 206 | + list_card(Some("Communities"), communities, "No data", palette).h_full(), | ||
| 207 | + )); | ||
| 208 | + | ||
| 209 | + let controls = toolbar(vec![ | ||
| 210 | + fixed(160., field(palette, &self.chat.builder_attachment_user)).into_any_element(), | ||
| 211 | + fixed( | ||
| 212 | + 160., | ||
| 213 | + field(palette, &self.chat.builder_attachment_community), | ||
| 214 | + ) | ||
| 215 | + .into_any_element(), | ||
| 216 | + accent_button( | ||
| 217 | + "search_attachments", | ||
| 218 | + "Search", | ||
| 219 | + palette, | ||
| 220 | + entity_action(entity, |yumush, window, cx| { | ||
| 221 | + yumush.search_attachments(window, cx) | ||
| 222 | + }), | ||
| 223 | + ) | ||
| 224 | + .into_any_element(), | ||
| 225 | + ]) | ||
| 226 | + .into_any_element(); | ||
| 227 | + let rows: Vec<Div> = self | ||
| 228 | + .chat | ||
| 229 | + .builder_attachments | ||
| 230 | + .iter() | ||
| 231 | + .map(|info| { | ||
| 232 | + let pending = self.chat.pending_attachment_delete.as_deref() | ||
| 233 | + == Some(info.attachment.id.as_str()); | ||
| 234 | + attachment_row(info, pending, entity, palette) | ||
| 235 | + }) | ||
| 236 | + .collect(); | ||
| 237 | + let body = if rows.is_empty() { | ||
| 238 | + div() | ||
| 239 | + .py(px(24.)) | ||
| 240 | + .flex() | ||
| 241 | + .justify_center() | ||
| 242 | + .text_size(text(12.)) | ||
| 243 | + .text_color(palette.muted) | ||
| 244 | + .child("No attachments") | ||
| 245 | + } else { | ||
| 246 | + div() | ||
| 247 | + .flex() | ||
| 248 | + .flex_col() | ||
| 249 | + .children(rows.into_iter().enumerate().map(|(index, row)| { | ||
| 250 | + row.when(index > 0, |row| { | ||
| 251 | + row.border_t_1().border_color(palette.border) | ||
| 252 | + }) | ||
| 253 | + })) | ||
| 254 | + }; | ||
| 255 | + let attachments = card(palette) | ||
| 256 | + .child(card_header("Attachments", vec![controls], palette)) | ||
| 257 | + .child(body); | ||
| 258 | + | ||
| 259 | + vec![usage.into_any_element(), attachments.into_any_element()] | ||
| 260 | + } | ||
| 261 | + | ||
| 262 | + pub(super) fn load_storage(&self, window: &mut Window, cx: &mut Context<Self>) { | ||
| 263 | + if !self.is_builder() { | ||
| 264 | + return; | ||
| 265 | + } | ||
| 266 | + self.search_attachments(window, cx); | ||
| 267 | + let network = self.network.clone(); | ||
| 268 | + | ||
| 269 | + cx.spawn_in(window, async move |this, cx| { | ||
| 270 | + let (users, communities) = match network.storage_usage().await { | ||
| 271 | + Ok(usage) => usage, | ||
| 272 | + Err(error) => { | ||
| 273 | + notify_error(&this, cx, error); | ||
| 274 | + return; | ||
| 275 | + } | ||
| 276 | + }; | ||
| 277 | + | ||
| 278 | + let _ = this.update(cx, |yumush, cx| { | ||
| 279 | + yumush.chat.builder_usage_users = users; | ||
| 280 | + yumush.chat.builder_usage_communities = communities; | ||
| 281 | + cx.notify(); | ||
| 282 | + }); | ||
| 283 | + }) | ||
| 284 | + .detach(); | ||
| 285 | + } | ||
| 286 | + | ||
| 287 | + pub(super) fn search_attachments(&self, window: &mut Window, cx: &mut Context<Self>) { | ||
| 288 | + let username = search_text(&self.chat.builder_attachment_user, cx); | ||
| 289 | + let community = search_text(&self.chat.builder_attachment_community, cx); | ||
| 290 | + let user_id = self.resolve_user(&username); | ||
| 291 | + let community_id = self.resolve_community(&community); | ||
| 292 | + let network = self.network.clone(); | ||
| 293 | + | ||
| 294 | + cx.spawn_in(window, async move |this, cx| { | ||
| 295 | + let attachments = match network.attachments(user_id, community_id).await { | ||
| 296 | + Ok(attachments) => attachments, | ||
| 297 | + Err(error) => { | ||
| 298 | + notify_error(&this, cx, error); | ||
| 299 | + return; | ||
| 300 | + } | ||
| 301 | + }; | ||
| 302 | + | ||
| 303 | + let _ = this.update(cx, |yumush, cx| { | ||
| 304 | + yumush.chat.builder_attachments = attachments; | ||
| 305 | + cx.notify(); | ||
| 306 | + }); | ||
| 307 | + }) | ||
| 308 | + .detach(); | ||
| 309 | + } | ||
| 310 | + | ||
| 311 | + pub(super) fn delete_builder_attachment( | ||
| 312 | + &mut self, | ||
| 313 | + attachment_id: &str, | ||
| 314 | + window: &mut Window, | ||
| 315 | + cx: &mut Context<Self>, | ||
| 316 | + ) { | ||
| 317 | + if self.chat.pending_attachment_delete.as_deref() != Some(attachment_id) { | ||
| 318 | + self.chat.pending_attachment_delete = Some(attachment_id.to_string()); | ||
| 319 | + cx.notify(); | ||
| 320 | + return; | ||
| 321 | + } | ||
| 322 | + self.chat.pending_attachment_delete = None; | ||
| 323 | + let network = self.network.clone(); | ||
| 324 | + let attachment_id = attachment_id.to_string(); | ||
| 325 | + | ||
| 326 | + cx.spawn_in(window, async move |this, cx| { | ||
| 327 | + match network.delete_attachment(&attachment_id).await { | ||
| 328 | + Ok(()) => { | ||
| 329 | + let _ = this.update_in(cx, |yumush, window, cx| { | ||
| 330 | + yumush.load_storage(window, cx); | ||
| 331 | + }); | ||
| 332 | + } | ||
| 333 | + Err(error) => notify_error(&this, cx, error), | ||
| 334 | + } | ||
| 335 | + }) | ||
| 336 | + .detach(); | ||
| 337 | + } | ||
| 338 | +} | ||
crates/client/src/gui/builder/ui.rsadded+280 -0
| @@ -0,0 +1,280 @@ | |||
| 1 | +use crate::gui::{KB, MB, Palette, Tooltip as _, clickable, primary_button, text}; | ||
| 2 | +use gpui_kit::{ | ||
| 3 | + AnyElement, App, Div, FontWeight, Hsla, IntoElement, ParentElement, Stateful, Styled, Window, | ||
| 4 | + assets::IconName, component::Icon, div, prelude::FluentBuilder as _, px, | ||
| 5 | +}; | ||
| 6 | +use jiff::{civil::Date, tz::TimeZone}; | ||
| 7 | + | ||
| 8 | +pub(super) fn megabytes_bytes(text: &str) -> u64 { | ||
| 9 | + text.trim() | ||
| 10 | + .parse::<u64>() | ||
| 11 | + .ok() | ||
| 12 | + .and_then(|megabytes| megabytes.checked_mul(MB as u64)) | ||
| 13 | + .unwrap_or(0) | ||
| 14 | +} | ||
| 15 | + | ||
| 16 | +pub(super) fn kilobytes_bytes(text: &str) -> u64 { | ||
| 17 | + text.trim() | ||
| 18 | + .parse::<u64>() | ||
| 19 | + .ok() | ||
| 20 | + .and_then(|kilobytes| kilobytes.checked_mul(KB as u64)) | ||
| 21 | + .unwrap_or(0) | ||
| 22 | +} | ||
| 23 | + | ||
| 24 | +pub(super) fn megabytes_text(bytes: u64) -> String { | ||
| 25 | + (bytes / MB as u64).to_string() | ||
| 26 | +} | ||
| 27 | + | ||
| 28 | +pub(super) fn kilobytes_text(bytes: u64) -> String { | ||
| 29 | + (bytes / KB as u64).to_string() | ||
| 30 | +} | ||
| 31 | + | ||
| 32 | +pub(super) fn number(text: &str) -> u32 { | ||
| 33 | + text.trim().parse().unwrap_or(0) | ||
| 34 | +} | ||
| 35 | + | ||
| 36 | +pub(super) fn icon_button( | ||
| 37 | + id: String, | ||
| 38 | + icon: IconName, | ||
| 39 | + tip: &'static str, | ||
| 40 | + color: Hsla, | ||
| 41 | + palette: Palette, | ||
| 42 | + action: impl Fn(&mut Window, &mut App) + 'static, | ||
| 43 | +) -> Stateful<Div> { | ||
| 44 | + clickable(id, palette, action) | ||
| 45 | + .tip(tip) | ||
| 46 | + .size(px(26.)) | ||
| 47 | + .flex_shrink_0() | ||
| 48 | + .flex() | ||
| 49 | + .items_center() | ||
| 50 | + .justify_center() | ||
| 51 | + .rounded(px(6.)) | ||
| 52 | + .text_color(color) | ||
| 53 | + .child(Icon::new(icon).w(px(14.)).h(px(14.))) | ||
| 54 | +} | ||
| 55 | + | ||
| 56 | +pub(super) fn danger_button( | ||
| 57 | + id: String, | ||
| 58 | + label: &'static str, | ||
| 59 | + pending: bool, | ||
| 60 | + palette: Palette, | ||
| 61 | + action: impl Fn(&mut Window, &mut App) + 'static, | ||
| 62 | +) -> impl IntoElement { | ||
| 63 | + clickable(id, palette, action) | ||
| 64 | + .h(px(28.)) | ||
| 65 | + .px(px(12.)) | ||
| 66 | + .flex() | ||
| 67 | + .items_center() | ||
| 68 | + .rounded(px(6.)) | ||
| 69 | + .text_size(text(12.)) | ||
| 70 | + .font_weight(FontWeight::MEDIUM) | ||
| 71 | + .border_1() | ||
| 72 | + .border_color(palette.danger.opacity(0.5)) | ||
| 73 | + .text_color(palette.danger) | ||
| 74 | + .when(pending, |this| { | ||
| 75 | + this.bg(palette.danger).text_color(palette.on_accent) | ||
| 76 | + }) | ||
| 77 | + .child(if pending { "Confirm" } else { label }) | ||
| 78 | +} | ||
| 79 | + | ||
| 80 | +pub(super) fn day_millis(text: &str, end_of_day: bool) -> Option<u64> { | ||
| 81 | + let date: Date = text.parse().ok()?; | ||
| 82 | + let date = if end_of_day { | ||
| 83 | + date.tomorrow().ok()? | ||
| 84 | + } else { | ||
| 85 | + date | ||
| 86 | + }; | ||
| 87 | + let millis = date | ||
| 88 | + .to_zoned_datetime(TimeZone::system()) | ||
| 89 | + .ok()? | ||
| 90 | + .timestamp() | ||
| 91 | + .as_millisecond(); | ||
| 92 | + | ||
| 93 | + u64::try_from(millis - i64::from(end_of_day)).ok() | ||
| 94 | +} | ||
| 95 | + | ||
| 96 | +pub(super) fn accent_button( | ||
| 97 | + id: &'static str, | ||
| 98 | + label: &'static str, | ||
| 99 | + palette: Palette, | ||
| 100 | + action: impl Fn(&mut Window, &mut App) + 'static, | ||
| 101 | +) -> impl IntoElement { | ||
| 102 | + primary_button(id, palette, action) | ||
| 103 | + .h(px(28.)) | ||
| 104 | + .px(px(12.)) | ||
| 105 | + .flex() | ||
| 106 | + .items_center() | ||
| 107 | + .rounded(px(6.)) | ||
| 108 | + .text_size(text(12.)) | ||
| 109 | + .font_weight(FontWeight::MEDIUM) | ||
| 110 | + .child(label) | ||
| 111 | +} | ||
| 112 | + | ||
| 113 | +pub(super) fn plain_button( | ||
| 114 | + id: String, | ||
| 115 | + label: &'static str, | ||
| 116 | + palette: Palette, | ||
| 117 | + action: impl Fn(&mut Window, &mut App) + 'static, | ||
| 118 | +) -> impl IntoElement { | ||
| 119 | + clickable(id, palette, action) | ||
| 120 | + .h(px(28.)) | ||
| 121 | + .px(px(12.)) | ||
| 122 | + .flex() | ||
| 123 | + .items_center() | ||
| 124 | + .rounded(px(6.)) | ||
| 125 | + .text_size(text(12.)) | ||
| 126 | + .font_weight(FontWeight::MEDIUM) | ||
| 127 | + .text_color(palette.muted) | ||
| 128 | + .child(label) | ||
| 129 | +} | ||
| 130 | + | ||
| 131 | +pub(super) fn card(palette: Palette) -> Div { | ||
| 132 | + div() | ||
| 133 | + .flex() | ||
| 134 | + .flex_col() | ||
| 135 | + .rounded(px(10.)) | ||
| 136 | + .border_1() | ||
| 137 | + .border_color(palette.border) | ||
| 138 | + .bg(palette.panel) | ||
| 139 | + .overflow_hidden() | ||
| 140 | +} | ||
| 141 | + | ||
| 142 | +pub(super) fn card_header(title: &str, actions: Vec<AnyElement>, palette: Palette) -> Div { | ||
| 143 | + div() | ||
| 144 | + .px(px(14.)) | ||
| 145 | + .py(px(10.)) | ||
| 146 | + .flex() | ||
| 147 | + .items_center() | ||
| 148 | + .justify_between() | ||
| 149 | + .gap(px(8.)) | ||
| 150 | + .border_b_1() | ||
| 151 | + .border_color(palette.border) | ||
| 152 | + .child( | ||
| 153 | + div() | ||
| 154 | + .text_size(text(13.)) | ||
| 155 | + .font_weight(FontWeight::MEDIUM) | ||
| 156 | + .text_color(palette.text) | ||
| 157 | + .child(title.to_string()), | ||
| 158 | + ) | ||
| 159 | + .children(actions) | ||
| 160 | +} | ||
| 161 | + | ||
| 162 | +pub(super) fn list_card(title: Option<&str>, rows: Vec<Div>, empty: &str, palette: Palette) -> Div { | ||
| 163 | + let body = if rows.is_empty() { | ||
| 164 | + div() | ||
| 165 | + .py(px(24.)) | ||
| 166 | + .flex() | ||
| 167 | + .justify_center() | ||
| 168 | + .text_size(text(12.)) | ||
| 169 | + .text_color(palette.muted) | ||
| 170 | + .child(empty.to_string()) | ||
| 171 | + } else { | ||
| 172 | + div() | ||
| 173 | + .flex() | ||
| 174 | + .flex_col() | ||
| 175 | + .children(rows.into_iter().enumerate().map(|(index, row)| { | ||
| 176 | + row.when(index > 0, |row| { | ||
| 177 | + row.border_t_1().border_color(palette.border) | ||
| 178 | + }) | ||
| 179 | + })) | ||
| 180 | + }; | ||
| 181 | + | ||
| 182 | + card(palette) | ||
| 183 | + .children(title.map(|title| card_header(title, Vec::new(), palette))) | ||
| 184 | + .child(body) | ||
| 185 | +} | ||
| 186 | + | ||
| 187 | +pub(super) fn list_row() -> Div { | ||
| 188 | + div() | ||
| 189 | + .min_h(px(44.)) | ||
| 190 | + .px(px(14.)) | ||
| 191 | + .py(px(10.)) | ||
| 192 | + .flex() | ||
| 193 | + .items_center() | ||
| 194 | + .gap(px(10.)) | ||
| 195 | +} | ||
| 196 | + | ||
| 197 | +pub(super) fn toolbar(items: Vec<AnyElement>) -> Div { | ||
| 198 | + div() | ||
| 199 | + .flex() | ||
| 200 | + .flex_wrap() | ||
| 201 | + .items_center() | ||
| 202 | + .gap(px(8.)) | ||
| 203 | + .children(items) | ||
| 204 | +} | ||
| 205 | + | ||
| 206 | +pub(super) fn stat_tile(label: &str, value: &str, palette: Palette) -> Div { | ||
| 207 | + card(palette) | ||
| 208 | + .p(px(14.)) | ||
| 209 | + .gap(px(4.)) | ||
| 210 | + .child( | ||
| 211 | + div() | ||
| 212 | + .text_size(text(22.)) | ||
| 213 | + .font_weight(FontWeight::SEMIBOLD) | ||
| 214 | + .text_color(palette.text) | ||
| 215 | + .child(value.to_string()), | ||
| 216 | + ) | ||
| 217 | + .child( | ||
| 218 | + div() | ||
| 219 | + .text_size(text(12.)) | ||
| 220 | + .text_color(palette.muted) | ||
| 221 | + .child(label.to_string()), | ||
| 222 | + ) | ||
| 223 | +} | ||
| 224 | + | ||
| 225 | +pub(super) fn field_row( | ||
| 226 | + label: &str, | ||
| 227 | + hint: Option<&str>, | ||
| 228 | + control: impl IntoElement, | ||
| 229 | + palette: Palette, | ||
| 230 | +) -> Div { | ||
| 231 | + list_row() | ||
| 232 | + .justify_between() | ||
| 233 | + .child( | ||
| 234 | + div() | ||
| 235 | + .flex_1() | ||
| 236 | + .min_w_0() | ||
| 237 | + .flex() | ||
| 238 | + .flex_col() | ||
| 239 | + .child( | ||
| 240 | + div() | ||
| 241 | + .text_size(text(13.)) | ||
| 242 | + .text_color(palette.text) | ||
| 243 | + .child(label.to_string()), | ||
| 244 | + ) | ||
| 245 | + .children(hint.map(|hint| { | ||
| 246 | + div() | ||
| 247 | + .text_size(text(11.)) | ||
| 248 | + .text_color(palette.muted) | ||
| 249 | + .child(hint.to_string()) | ||
| 250 | + })), | ||
| 251 | + ) | ||
| 252 | + .child(div().w(px(220.)).flex_shrink_0().child(control)) | ||
| 253 | +} | ||
| 254 | + | ||
| 255 | +pub(super) fn pill(label: &str, color: Hsla, palette: Palette) -> Div { | ||
| 256 | + div() | ||
| 257 | + .px(px(6.)) | ||
| 258 | + .py(px(1.)) | ||
| 259 | + .rounded(px(4.)) | ||
| 260 | + .bg(palette.hover) | ||
| 261 | + .text_size(text(11.)) | ||
| 262 | + .text_color(color) | ||
| 263 | + .child(label.to_string()) | ||
| 264 | +} | ||
| 265 | + | ||
| 266 | +pub(super) fn page_heading(title: &str, actions: Vec<AnyElement>, palette: Palette) -> Div { | ||
| 267 | + div() | ||
| 268 | + .flex() | ||
| 269 | + .items_center() | ||
| 270 | + .justify_between() | ||
| 271 | + .gap(px(8.)) | ||
| 272 | + .child( | ||
| 273 | + div() | ||
| 274 | + .text_size(text(20.)) | ||
| 275 | + .font_weight(FontWeight::SEMIBOLD) | ||
| 276 | + .text_color(palette.text) | ||
| 277 | + .child(title.to_string()), | ||
| 278 | + ) | ||
| 279 | + .children(actions) | ||
| 280 | +} | ||
crates/client/src/gui/builder/users.rsadded+912 -0
| @@ -0,0 +1,912 @@ | |||
| 1 | +use std::future::Future; | ||
| 2 | + | ||
| 3 | +use common::{ | ||
| 4 | + error::Error, | ||
| 5 | + model::{Account, Role, SessionInfo, User}, | ||
| 6 | + permission, | ||
| 7 | + time::{DAY_MILLIS, HOUR_MILLIS, MINUTE_MILLIS, now_ms}, | ||
| 8 | +}; | ||
| 9 | +use gpui_kit::{ | ||
| 10 | + AnyElement, App, Context, Div, Entity, FontWeight, IntoElement, ParentElement, Styled, Window, | ||
| 11 | + assets::IconName, | ||
| 12 | + component::{Icon, input::Input}, | ||
| 13 | + div, | ||
| 14 | + prelude::FluentBuilder as _, | ||
| 15 | + px, | ||
| 16 | +}; | ||
| 17 | + | ||
| 18 | +use super::ui::{accent_button, card, danger_button, pill, plain_button}; | ||
| 19 | +use crate::{ | ||
| 20 | + gui::{ | ||
| 21 | + DATE, DAY, Palette, Tooltip as _, Yumush, auth::field, avatar, clickable, | ||
| 22 | + community_settings::search_text, entity_action, id_time, millis_time, notify_error, text, | ||
| 23 | + }, | ||
| 24 | + network::NetworkHandle, | ||
| 25 | +}; | ||
| 26 | + | ||
| 27 | +const MINUTES_PER_HOUR: u64 = HOUR_MILLIS / MINUTE_MILLIS; | ||
| 28 | +const MINUTES_PER_DAY: u64 = DAY_MILLIS / MINUTE_MILLIS; | ||
| 29 | + | ||
| 30 | +fn seen_text(last_seen: Option<u64>) -> String { | ||
| 31 | + let Some(last_seen) = last_seen else { | ||
| 32 | + return "never".to_string(); | ||
| 33 | + }; | ||
| 34 | + let minutes = now_ms().saturating_sub(last_seen) / MINUTE_MILLIS; | ||
| 35 | + | ||
| 36 | + match minutes { | ||
| 37 | + 0 => "now".to_string(), | ||
| 38 | + 1..MINUTES_PER_HOUR => format!("{minutes}m ago"), | ||
| 39 | + MINUTES_PER_HOUR..MINUTES_PER_DAY => format!("{}h ago", minutes / MINUTES_PER_HOUR), | ||
| 40 | + _ => millis_time(last_seen, DAY), | ||
| 41 | + } | ||
| 42 | +} | ||
| 43 | + | ||
| 44 | +fn status(label: &str, color: gpui_kit::Hsla, palette: Palette) -> Div { | ||
| 45 | + div().flex_shrink_0().child(pill(label, color, palette)) | ||
| 46 | +} | ||
| 47 | + | ||
| 48 | +fn divider(palette: Palette) -> Div { | ||
| 49 | + div().w(px(1.)).h(px(16.)).mx(px(4.)).bg(palette.border) | ||
| 50 | +} | ||
| 51 | + | ||
| 52 | +impl Yumush { | ||
| 53 | + pub(super) fn submit_new_user(&mut self, window: &mut Window, cx: &mut Context<Self>) { | ||
| 54 | + let username = self.chat.new_username.read(cx).text().to_string(); | ||
| 55 | + let password = self.chat.new_password.read(cx).text().to_string(); | ||
| 56 | + self.chat | ||
| 57 | + .new_password | ||
| 58 | + .update(cx, |input, cx| input.set_value("", window, cx)); | ||
| 59 | + let network = self.network.clone(); | ||
| 60 | + | ||
| 61 | + cx.spawn_in(window, async move |this, cx| { | ||
| 62 | + match network.create_user(&username, &password).await { | ||
| 63 | + Ok(_) => { | ||
| 64 | + let _ = this.update_in(cx, |yumush, window, cx| { | ||
| 65 | + yumush | ||
| 66 | + .chat | ||
| 67 | + .new_username | ||
| 68 | + .update(cx, |input, cx| input.set_value("", window, cx)); | ||
| 69 | + yumush.load_users(window, cx); | ||
| 70 | + }); | ||
| 71 | + } | ||
| 72 | + Err(error) => notify_error(&this, cx, error), | ||
| 73 | + } | ||
| 74 | + }) | ||
| 75 | + .detach(); | ||
| 76 | + } | ||
| 77 | + | ||
| 78 | + pub(super) fn set_builder_role( | ||
| 79 | + &mut self, | ||
| 80 | + user_id: &str, | ||
| 81 | + role: Role, | ||
| 82 | + window: &mut Window, | ||
| 83 | + cx: &mut Context<Self>, | ||
| 84 | + ) { | ||
| 85 | + let network = self.network.clone(); | ||
| 86 | + let user_id = user_id.to_string(); | ||
| 87 | + | ||
| 88 | + cx.spawn_in(window, async move |this, cx| { | ||
| 89 | + match network.set_role(&user_id, role).await { | ||
| 90 | + Ok(user) => { | ||
| 91 | + let _ = this.update(cx, |yumush, cx| { | ||
| 92 | + let index = yumush | ||
| 93 | + .chat | ||
| 94 | + .builder_users | ||
| 95 | + .iter() | ||
| 96 | + .position(|existing| existing.user.id == user.id); | ||
| 97 | + if let Some(index) = index { | ||
| 98 | + yumush.chat.builder_users[index].user = user; | ||
| 99 | + } | ||
| 100 | + cx.notify(); | ||
| 101 | + }); | ||
| 102 | + } | ||
| 103 | + Err(error) => notify_error(&this, cx, error), | ||
| 104 | + } | ||
| 105 | + }) | ||
| 106 | + .detach(); | ||
| 107 | + } | ||
| 108 | + | ||
| 109 | + pub(super) fn delete_builder_user( | ||
| 110 | + &mut self, | ||
| 111 | + user_id: &str, | ||
| 112 | + window: &mut Window, | ||
| 113 | + cx: &mut Context<Self>, | ||
| 114 | + ) { | ||
| 115 | + if self.chat.pending_builder_delete.as_deref() != Some(user_id) { | ||
| 116 | + self.chat.pending_builder_delete = Some(user_id.to_string()); | ||
| 117 | + cx.notify(); | ||
| 118 | + return; | ||
| 119 | + } | ||
| 120 | + self.chat.pending_builder_delete = None; | ||
| 121 | + let network = self.network.clone(); | ||
| 122 | + let user_id = user_id.to_string(); | ||
| 123 | + | ||
| 124 | + cx.spawn_in(window, async move |this, cx| { | ||
| 125 | + match network.delete_user(&user_id).await { | ||
| 126 | + Ok(()) => { | ||
| 127 | + let _ = this.update(cx, |yumush, cx| { | ||
| 128 | + yumush | ||
| 129 | + .chat | ||
| 130 | + .builder_users | ||
| 131 | + .retain(|account| account.user.id != user_id); | ||
| 132 | + cx.notify(); | ||
| 133 | + }); | ||
| 134 | + } | ||
| 135 | + Err(error) => notify_error(&this, cx, error), | ||
| 136 | + } | ||
| 137 | + }) | ||
| 138 | + .detach(); | ||
| 139 | + } | ||
| 140 | + | ||
| 141 | + pub(super) fn builder_user_action<Fut>( | ||
| 142 | + &mut self, | ||
| 143 | + window: &mut Window, | ||
| 144 | + cx: &mut Context<Self>, | ||
| 145 | + action: impl FnOnce(NetworkHandle) -> Fut + 'static, | ||
| 146 | + ) where | ||
| 147 | + Fut: Future<Output = Result<(), Error>> + 'static, | ||
| 148 | + { | ||
| 149 | + let network = self.network.clone(); | ||
| 150 | + | ||
| 151 | + cx.spawn_in(window, async move |this, cx| match action(network).await { | ||
| 152 | + Ok(()) => { | ||
| 153 | + let _ = this.update_in(cx, |yumush, window, cx| yumush.load_users(window, cx)); | ||
| 154 | + } | ||
| 155 | + Err(error) => notify_error(&this, cx, error), | ||
| 156 | + }) | ||
| 157 | + .detach(); | ||
| 158 | + } | ||
| 159 | + | ||
| 160 | + pub(super) fn suspend_builder_user( | ||
| 161 | + &mut self, | ||
| 162 | + user_id: &str, | ||
| 163 | + suspended: bool, | ||
| 164 | + window: &mut Window, | ||
| 165 | + cx: &mut Context<Self>, | ||
| 166 | + ) { | ||
| 167 | + let user_id = user_id.to_string(); | ||
| 168 | + self.builder_user_action(window, cx, move |network| async move { | ||
| 169 | + network.suspend_user(&user_id, suspended).await | ||
| 170 | + }); | ||
| 171 | + } | ||
| 172 | + | ||
| 173 | + pub(super) fn revoke_builder_sessions( | ||
| 174 | + &mut self, | ||
| 175 | + user_id: &str, | ||
| 176 | + window: &mut Window, | ||
| 177 | + cx: &mut Context<Self>, | ||
| 178 | + ) { | ||
| 179 | + let user_id = user_id.to_string(); | ||
| 180 | + self.builder_user_action(window, cx, move |network| async move { | ||
| 181 | + network.revoke_sessions(&user_id).await | ||
| 182 | + }); | ||
| 183 | + } | ||
| 184 | + | ||
| 185 | + pub(super) fn clear_builder_profile( | ||
| 186 | + &mut self, | ||
| 187 | + user_id: &str, | ||
| 188 | + window: &mut Window, | ||
| 189 | + cx: &mut Context<Self>, | ||
| 190 | + ) { | ||
| 191 | + let user_id = user_id.to_string(); | ||
| 192 | + self.builder_user_action(window, cx, move |network| async move { | ||
| 193 | + network.clear_profile(&user_id).await.map(|_| ()) | ||
| 194 | + }); | ||
| 195 | + } | ||
| 196 | + | ||
| 197 | + pub(super) fn toggle_builder_password(&mut self, user_id: &str, cx: &mut Context<Self>) { | ||
| 198 | + if self.chat.pending_builder_password.as_deref() == Some(user_id) { | ||
| 199 | + self.chat.pending_builder_password = None; | ||
| 200 | + } else { | ||
| 201 | + self.chat.pending_builder_password = Some(user_id.to_string()); | ||
| 202 | + } | ||
| 203 | + cx.notify(); | ||
| 204 | + } | ||
| 205 | + | ||
| 206 | + pub(super) fn reset_builder_password(&mut self, window: &mut Window, cx: &mut Context<Self>) { | ||
| 207 | + let Some(user_id) = self.chat.pending_builder_password.take() else { | ||
| 208 | + return; | ||
| 209 | + }; | ||
| 210 | + let password = self.chat.builder_password.read(cx).text().to_string(); | ||
| 211 | + self.chat | ||
| 212 | + .builder_password | ||
| 213 | + .update(cx, |input, cx| input.set_value("", window, cx)); | ||
| 214 | + self.builder_user_action(window, cx, move |network| async move { | ||
| 215 | + network.reset_password(&user_id, &password).await | ||
| 216 | + }); | ||
| 217 | + } | ||
| 218 | + | ||
| 219 | + pub(super) fn load_sessions( | ||
| 220 | + &self, | ||
| 221 | + user_id: String, | ||
| 222 | + window: &mut Window, | ||
| 223 | + cx: &mut Context<Self>, | ||
| 224 | + ) { | ||
| 225 | + let network = self.network.clone(); | ||
| 226 | + | ||
| 227 | + cx.spawn_in(window, async move |this, cx| { | ||
| 228 | + match network.sessions(&user_id).await { | ||
| 229 | + Ok(sessions) => { | ||
| 230 | + let _ = this.update(cx, |yumush, cx| { | ||
| 231 | + yumush.chat.builder_sessions = Some((user_id, sessions)); | ||
| 232 | + cx.notify(); | ||
| 233 | + }); | ||
| 234 | + } | ||
| 235 | + Err(error) => notify_error(&this, cx, error), | ||
| 236 | + } | ||
| 237 | + }) | ||
| 238 | + .detach(); | ||
| 239 | + } | ||
| 240 | + | ||
| 241 | + pub(super) fn toggle_builder_sessions( | ||
| 242 | + &mut self, | ||
| 243 | + user_id: &str, | ||
| 244 | + window: &mut Window, | ||
| 245 | + cx: &mut Context<Self>, | ||
| 246 | + ) { | ||
| 247 | + if self | ||
| 248 | + .chat | ||
| 249 | + .builder_sessions | ||
| 250 | + .as_ref() | ||
| 251 | + .is_some_and(|(open, _)| open == user_id) | ||
| 252 | + { | ||
| 253 | + self.chat.builder_sessions = None; | ||
| 254 | + cx.notify(); | ||
| 255 | + return; | ||
| 256 | + } | ||
| 257 | + self.load_sessions(user_id.to_string(), window, cx); | ||
| 258 | + } | ||
| 259 | + | ||
| 260 | + pub(super) fn revoke_builder_session( | ||
| 261 | + &mut self, | ||
| 262 | + user_id: &str, | ||
| 263 | + session_id: &str, | ||
| 264 | + window: &mut Window, | ||
| 265 | + cx: &mut Context<Self>, | ||
| 266 | + ) { | ||
| 267 | + let network = self.network.clone(); | ||
| 268 | + let user_id = user_id.to_string(); | ||
| 269 | + let session_id = session_id.to_string(); | ||
| 270 | + | ||
| 271 | + cx.spawn_in(window, async move |this, cx| { | ||
| 272 | + match network.revoke_session(&user_id, &session_id).await { | ||
| 273 | + Ok(()) => { | ||
| 274 | + let _ = this.update_in(cx, |yumush, window, cx| { | ||
| 275 | + yumush.load_sessions(user_id, window, cx) | ||
| 276 | + }); | ||
| 277 | + } | ||
| 278 | + Err(error) => notify_error(&this, cx, error), | ||
| 279 | + } | ||
| 280 | + }) | ||
| 281 | + .detach(); | ||
| 282 | + } | ||
| 283 | + | ||
| 284 | + pub(super) fn toggle_builder_rename(&mut self, user_id: &str, cx: &mut Context<Self>) { | ||
| 285 | + if self.chat.pending_builder_rename.as_deref() == Some(user_id) { | ||
| 286 | + self.chat.pending_builder_rename = None; | ||
| 287 | + } else { | ||
| 288 | + self.chat.pending_builder_rename = Some(user_id.to_string()); | ||
| 289 | + } | ||
| 290 | + cx.notify(); | ||
| 291 | + } | ||
| 292 | + | ||
| 293 | + pub(super) fn rename_builder_user(&mut self, window: &mut Window, cx: &mut Context<Self>) { | ||
| 294 | + let Some(user_id) = self.chat.pending_builder_rename.take() else { | ||
| 295 | + return; | ||
| 296 | + }; | ||
| 297 | + let username = self.chat.builder_rename.read(cx).text().to_string(); | ||
| 298 | + self.chat | ||
| 299 | + .builder_rename | ||
| 300 | + .update(cx, |input, cx| input.set_value("", window, cx)); | ||
| 301 | + self.builder_user_action(window, cx, move |network| async move { | ||
| 302 | + network.rename_user(&user_id, &username).await.map(|_| ()) | ||
| 303 | + }); | ||
| 304 | + } | ||
| 305 | + | ||
| 306 | + pub(super) fn toggle_builder_timeout(&mut self, user_id: &str, cx: &mut Context<Self>) { | ||
| 307 | + if self.chat.pending_builder_timeout.as_deref() == Some(user_id) { | ||
| 308 | + self.chat.pending_builder_timeout = None; | ||
| 309 | + } else { | ||
| 310 | + self.chat.pending_builder_timeout = Some(user_id.to_string()); | ||
| 311 | + } | ||
| 312 | + cx.notify(); | ||
| 313 | + } | ||
| 314 | + | ||
| 315 | + pub(super) fn confirm_builder_timeout(&mut self, window: &mut Window, cx: &mut Context<Self>) { | ||
| 316 | + let Some(user_id) = self.chat.pending_builder_timeout.clone() else { | ||
| 317 | + return; | ||
| 318 | + }; | ||
| 319 | + let minutes = self.chat.builder_timeout.read(cx).text().to_string(); | ||
| 320 | + let Ok(minutes) = minutes.trim().parse::<u32>() else { | ||
| 321 | + return; | ||
| 322 | + }; | ||
| 323 | + self.chat.pending_builder_timeout = None; | ||
| 324 | + self.chat | ||
| 325 | + .builder_timeout | ||
| 326 | + .update(cx, |input, cx| input.set_value("", window, cx)); | ||
| 327 | + self.builder_user_action(window, cx, move |network| async move { | ||
| 328 | + network.timeout_user(&user_id, minutes).await | ||
| 329 | + }); | ||
| 330 | + } | ||
| 331 | + | ||
| 332 | + pub(super) fn toggle_builder_ban(&mut self, user_id: &str, cx: &mut Context<Self>) { | ||
| 333 | + if self.chat.pending_builder_ban.as_deref() == Some(user_id) { | ||
| 334 | + self.chat.pending_builder_ban = None; | ||
| 335 | + } else { | ||
| 336 | + self.chat.pending_builder_ban = Some(user_id.to_string()); | ||
| 337 | + } | ||
| 338 | + cx.notify(); | ||
| 339 | + } | ||
| 340 | + | ||
| 341 | + pub(super) fn toggle_builder_ban_ip(&mut self, cx: &mut Context<Self>) { | ||
| 342 | + self.chat.builder_ban_ip = !self.chat.builder_ban_ip; | ||
| 343 | + cx.notify(); | ||
| 344 | + } | ||
| 345 | + | ||
| 346 | + pub(super) fn confirm_builder_ban(&mut self, window: &mut Window, cx: &mut Context<Self>) { | ||
| 347 | + let Some(user_id) = self.chat.pending_builder_ban.take() else { | ||
| 348 | + return; | ||
| 349 | + }; | ||
| 350 | + let reason = self.chat.builder_ban_reason.read(cx).text().to_string(); | ||
| 351 | + let reason = reason.trim().to_string(); | ||
| 352 | + let reason = (!reason.is_empty()).then_some(reason); | ||
| 353 | + let ip = self.chat.builder_ban_ip; | ||
| 354 | + self.chat | ||
| 355 | + .builder_ban_reason | ||
| 356 | + .update(cx, |input, cx| input.set_value("", window, cx)); | ||
| 357 | + self.builder_user_action(window, cx, move |network| async move { | ||
| 358 | + network.ban_user(&user_id, reason.as_deref(), ip).await | ||
| 359 | + }); | ||
| 360 | + } | ||
| 361 | + | ||
| 362 | + pub(super) fn purge_builder_user( | ||
| 363 | + &mut self, | ||
| 364 | + user_id: &str, | ||
| 365 | + window: &mut Window, | ||
| 366 | + cx: &mut Context<Self>, | ||
| 367 | + ) { | ||
| 368 | + if self.chat.pending_builder_purge.as_deref() != Some(user_id) { | ||
| 369 | + self.chat.pending_builder_purge = Some(user_id.to_string()); | ||
| 370 | + cx.notify(); | ||
| 371 | + return; | ||
| 372 | + } | ||
| 373 | + self.chat.pending_builder_purge = None; | ||
| 374 | + let network = self.network.clone(); | ||
| 375 | + let user_id = user_id.to_string(); | ||
| 376 | + | ||
| 377 | + cx.spawn_in(window, async move |this, cx| { | ||
| 378 | + match network.purge_user(&user_id).await { | ||
| 379 | + Ok(()) => { | ||
| 380 | + let _ = this.update_in(cx, |yumush, window, cx| { | ||
| 381 | + yumush | ||
| 382 | + .chat | ||
| 383 | + .builder_messages | ||
| 384 | + .retain(|hit| hit.message.user_id != user_id); | ||
| 385 | + yumush.load_users(window, cx); | ||
| 386 | + }); | ||
| 387 | + } | ||
| 388 | + Err(error) => notify_error(&this, cx, error), | ||
| 389 | + } | ||
| 390 | + }) | ||
| 391 | + .detach(); | ||
| 392 | + } | ||
| 393 | + | ||
| 394 | + pub(super) fn users_body( | ||
| 395 | + &self, | ||
| 396 | + entity: &Entity<Self>, | ||
| 397 | + palette: Palette, | ||
| 398 | + cx: &App, | ||
| 399 | + ) -> Vec<AnyElement> { | ||
| 400 | + let query = search_text(&self.chat.builder_search, cx); | ||
| 401 | + let rows: Vec<Div> = match self.user.as_ref() { | ||
| 402 | + Some(me) => self | ||
| 403 | + .chat | ||
| 404 | + .builder_users | ||
| 405 | + .iter() | ||
| 406 | + .filter(|account| { | ||
| 407 | + account | ||
| 408 | + .user | ||
| 409 | + .username | ||
| 410 | + .to_lowercase() | ||
| 411 | + .contains(query.as_str()) | ||
| 412 | + }) | ||
| 413 | + .map(|account| self.builder_row(account, me, entity, palette)) | ||
| 414 | + .collect(), | ||
| 415 | + None => Vec::new(), | ||
| 416 | + }; | ||
| 417 | + | ||
| 418 | + let toolbar = div() | ||
| 419 | + .px(px(14.)) | ||
| 420 | + .py(px(10.)) | ||
| 421 | + .flex() | ||
| 422 | + .items_center() | ||
| 423 | + .gap(px(8.)) | ||
| 424 | + .border_b_1() | ||
| 425 | + .border_color(palette.border) | ||
| 426 | + .child( | ||
| 427 | + div() | ||
| 428 | + .flex_1() | ||
| 429 | + .min_w_0() | ||
| 430 | + .child(Input::new(&self.chat.builder_search)), | ||
| 431 | + ) | ||
| 432 | + .child( | ||
| 433 | + div() | ||
| 434 | + .w(px(160.)) | ||
| 435 | + .flex_shrink_0() | ||
| 436 | + .child(field(palette, &self.chat.new_username)), | ||
| 437 | + ) | ||
| 438 | + .child( | ||
| 439 | + div() | ||
| 440 | + .w(px(160.)) | ||
| 441 | + .flex_shrink_0() | ||
| 442 | + .child(field(palette, &self.chat.new_password)), | ||
| 443 | + ) | ||
| 444 | + .child(accent_button( | ||
| 445 | + "create_user", | ||
| 446 | + "Create", | ||
| 447 | + palette, | ||
| 448 | + entity_action(entity, |yumush, window, cx| { | ||
| 449 | + yumush.submit_new_user(window, cx) | ||
| 450 | + }), | ||
| 451 | + )); | ||
| 452 | + | ||
| 453 | + let body = if rows.is_empty() { | ||
| 454 | + div() | ||
| 455 | + .py(px(24.)) | ||
| 456 | + .flex() | ||
| 457 | + .justify_center() | ||
| 458 | + .text_size(text(12.)) | ||
| 459 | + .text_color(palette.muted) | ||
| 460 | + .child("No users".to_string()) | ||
| 461 | + } else { | ||
| 462 | + div() | ||
| 463 | + .flex() | ||
| 464 | + .flex_col() | ||
| 465 | + .children(rows.into_iter().enumerate().map(|(index, row)| { | ||
| 466 | + row.when(index > 0, |row| { | ||
| 467 | + row.border_t_1().border_color(palette.border) | ||
| 468 | + }) | ||
| 469 | + })) | ||
| 470 | + }; | ||
| 471 | + | ||
| 472 | + vec![card(palette).child(toolbar).child(body).into_any_element()] | ||
| 473 | + } | ||
| 474 | + | ||
| 475 | + pub(super) fn builder_timeout_row( | ||
| 476 | + &self, | ||
| 477 | + user_id: &str, | ||
| 478 | + entity: &Entity<Self>, | ||
| 479 | + palette: Palette, | ||
| 480 | + ) -> Div { | ||
| 481 | + div() | ||
| 482 | + .h(px(40.)) | ||
| 483 | + .px(px(12.)) | ||
| 484 | + .flex() | ||
| 485 | + .items_center() | ||
| 486 | + .gap(px(8.)) | ||
| 487 | + .child( | ||
| 488 | + div() | ||
| 489 | + .flex_1() | ||
| 490 | + .min_w_0() | ||
| 491 | + .child(field(palette, &self.chat.builder_timeout)), | ||
| 492 | + ) | ||
| 493 | + .child(plain_button( | ||
| 494 | + format!("confirm_timeout_{user_id}"), | ||
| 495 | + "Confirm", | ||
| 496 | + palette, | ||
| 497 | + entity_action(entity, |yumush, window, cx| { | ||
| 498 | + yumush.confirm_builder_timeout(window, cx) | ||
| 499 | + }), | ||
| 500 | + )) | ||
| 501 | + } | ||
| 502 | + | ||
| 503 | + pub(super) fn builder_ban_row( | ||
| 504 | + &self, | ||
| 505 | + user_id: &str, | ||
| 506 | + entity: &Entity<Self>, | ||
| 507 | + palette: Palette, | ||
| 508 | + ) -> Div { | ||
| 509 | + let ip_label = if self.chat.builder_ban_ip { | ||
| 510 | + "IP: on" | ||
| 511 | + } else { | ||
| 512 | + "IP: off" | ||
| 513 | + }; | ||
| 514 | + | ||
| 515 | + div() | ||
| 516 | + .h(px(40.)) | ||
| 517 | + .px(px(12.)) | ||
| 518 | + .flex() | ||
| 519 | + .items_center() | ||
| 520 | + .gap(px(8.)) | ||
| 521 | + .child( | ||
| 522 | + div() | ||
| 523 | + .flex_1() | ||
| 524 | + .min_w_0() | ||
| 525 | + .child(field(palette, &self.chat.builder_ban_reason)), | ||
| 526 | + ) | ||
| 527 | + .child(plain_button( | ||
| 528 | + format!("ban_ip_{user_id}"), | ||
| 529 | + ip_label, | ||
| 530 | + palette, | ||
| 531 | + entity_action(entity, |yumush, _, cx| yumush.toggle_builder_ban_ip(cx)), | ||
| 532 | + )) | ||
| 533 | + .child(plain_button( | ||
| 534 | + format!("confirm_ban_{user_id}"), | ||
| 535 | + "Confirm", | ||
| 536 | + palette, | ||
| 537 | + entity_action(entity, |yumush, window, cx| { | ||
| 538 | + yumush.confirm_builder_ban(window, cx) | ||
| 539 | + }), | ||
| 540 | + )) | ||
| 541 | + } | ||
| 542 | + | ||
| 543 | + pub(super) fn builder_session_row( | ||
| 544 | + &self, | ||
| 545 | + user_id: &str, | ||
| 546 | + session: &SessionInfo, | ||
| 547 | + entity: &Entity<Self>, | ||
| 548 | + palette: Palette, | ||
| 549 | + ) -> Div { | ||
| 550 | + let revoke_user_id = user_id.to_string(); | ||
| 551 | + let session_id = session.id.clone(); | ||
| 552 | + let revoke_button = plain_button( | ||
| 553 | + format!("revoke_session_{}", session.id), | ||
| 554 | + "Revoke", | ||
| 555 | + palette, | ||
| 556 | + entity_action(entity, move |yumush, window, cx| { | ||
| 557 | + yumush.revoke_builder_session(&revoke_user_id, &session_id, window, cx) | ||
| 558 | + }), | ||
| 559 | + ); | ||
| 560 | + let device = if session.device.is_empty() { | ||
| 561 | + "unknown device" | ||
| 562 | + } else { | ||
| 563 | + session.device.as_str() | ||
| 564 | + }; | ||
| 565 | + | ||
| 566 | + div() | ||
| 567 | + .h(px(36.)) | ||
| 568 | + .pl(px(40.)) | ||
| 569 | + .pr(px(12.)) | ||
| 570 | + .flex() | ||
| 571 | + .items_center() | ||
| 572 | + .gap(px(8.)) | ||
| 573 | + .child( | ||
| 574 | + div() | ||
| 575 | + .flex_1() | ||
| 576 | + .min_w_0() | ||
| 577 | + .truncate() | ||
| 578 | + .child(device.to_string()), | ||
| 579 | + ) | ||
| 580 | + .child( | ||
| 581 | + div() | ||
| 582 | + .flex_shrink_0() | ||
| 583 | + .text_size(text(11.)) | ||
| 584 | + .text_color(palette.muted) | ||
| 585 | + .child(seen_text(session.last_seen)), | ||
| 586 | + ) | ||
| 587 | + .child(revoke_button) | ||
| 588 | + } | ||
| 589 | + | ||
| 590 | + pub(super) fn builder_rename_row( | ||
| 591 | + &self, | ||
| 592 | + user_id: &str, | ||
| 593 | + entity: &Entity<Self>, | ||
| 594 | + palette: Palette, | ||
| 595 | + ) -> Div { | ||
| 596 | + div() | ||
| 597 | + .h(px(40.)) | ||
| 598 | + .px(px(12.)) | ||
| 599 | + .flex() | ||
| 600 | + .items_center() | ||
| 601 | + .gap(px(8.)) | ||
| 602 | + .child( | ||
| 603 | + div() | ||
| 604 | + .flex_1() | ||
| 605 | + .min_w_0() | ||
| 606 | + .child(field(palette, &self.chat.builder_rename)), | ||
| 607 | + ) | ||
| 608 | + .child(plain_button( | ||
| 609 | + format!("confirm_rename_{user_id}"), | ||
| 610 | + "Confirm", | ||
| 611 | + palette, | ||
| 612 | + entity_action(entity, |yumush, window, cx| { | ||
| 613 | + yumush.rename_builder_user(window, cx) | ||
| 614 | + }), | ||
| 615 | + )) | ||
| 616 | + } | ||
| 617 | + | ||
| 618 | + pub(super) fn builder_password_row( | ||
| 619 | + &self, | ||
| 620 | + user_id: &str, | ||
| 621 | + entity: &Entity<Self>, | ||
| 622 | + palette: Palette, | ||
| 623 | + ) -> Div { | ||
| 624 | + div() | ||
| 625 | + .h(px(40.)) | ||
| 626 | + .px(px(12.)) | ||
| 627 | + .flex() | ||
| 628 | + .items_center() | ||
| 629 | + .gap(px(8.)) | ||
| 630 | + .child( | ||
| 631 | + div() | ||
| 632 | + .flex_1() | ||
| 633 | + .min_w_0() | ||
| 634 | + .child(field(palette, &self.chat.builder_password)), | ||
| 635 | + ) | ||
| 636 | + .child(plain_button( | ||
| 637 | + format!("confirm_password_{user_id}"), | ||
| 638 | + "Confirm", | ||
| 639 | + palette, | ||
| 640 | + entity_action(entity, |yumush, window, cx| { | ||
| 641 | + yumush.reset_builder_password(window, cx) | ||
| 642 | + }), | ||
| 643 | + )) | ||
| 644 | + } | ||
| 645 | + | ||
| 646 | + pub(super) fn builder_row( | ||
| 647 | + &self, | ||
| 648 | + account: &Account, | ||
| 649 | + me: &User, | ||
| 650 | + entity: &Entity<Self>, | ||
| 651 | + palette: Palette, | ||
| 652 | + ) -> Div { | ||
| 653 | + let user = &account.user; | ||
| 654 | + let is_self = user.id == me.id; | ||
| 655 | + let builder = user.role == Role::Builder; | ||
| 656 | + let manage = permission::can_manage_user(me.role, is_self, user.role); | ||
| 657 | + let can_role = permission::can_set_role(me.role, is_self); | ||
| 658 | + let can_delete = !is_self && permission::can_delete_user(me.role, is_self, user.role); | ||
| 659 | + let has_actions = manage || can_role || can_delete; | ||
| 660 | + let suspended = account.suspended; | ||
| 661 | + let timed_out = account | ||
| 662 | + .suspended_until | ||
| 663 | + .is_some_and(|until| until > now_ms()); | ||
| 664 | + let expanded_sessions = self | ||
| 665 | + .chat | ||
| 666 | + .builder_sessions | ||
| 667 | + .as_ref() | ||
| 668 | + .filter(|(open, _)| *open == user.id) | ||
| 669 | + .map(|(_, sessions)| sessions); | ||
| 670 | + let expanded = expanded_sessions.is_some(); | ||
| 671 | + let pending_delete = self.chat.pending_builder_delete.as_deref() == Some(user.id.as_str()); | ||
| 672 | + let pending_purge = self.chat.pending_builder_purge.as_deref() == Some(user.id.as_str()); | ||
| 673 | + let role_id = user.id.clone(); | ||
| 674 | + let delete_id = user.id.clone(); | ||
| 675 | + let suspend_id = user.id.clone(); | ||
| 676 | + let sessions_id = user.id.clone(); | ||
| 677 | + let password_id = user.id.clone(); | ||
| 678 | + let profile_id = user.id.clone(); | ||
| 679 | + let sessions_list_id = user.id.clone(); | ||
| 680 | + let rename_id = user.id.clone(); | ||
| 681 | + let timeout_id = user.id.clone(); | ||
| 682 | + let ban_id = user.id.clone(); | ||
| 683 | + let purge_id = user.id.clone(); | ||
| 684 | + let joined = id_time(&user.id, DATE); | ||
| 685 | + let seen = seen_text(account.last_seen); | ||
| 686 | + let next_role = if builder { Role::User } else { Role::Builder }; | ||
| 687 | + | ||
| 688 | + let role_button = plain_button( | ||
| 689 | + format!("role_{}", user.id), | ||
| 690 | + if builder { | ||
| 691 | + "Remove builder" | ||
| 692 | + } else { | ||
| 693 | + "Make builder" | ||
| 694 | + }, | ||
| 695 | + palette, | ||
| 696 | + entity_action(entity, move |yumush, window, cx| { | ||
| 697 | + yumush.set_builder_role(&role_id, next_role, window, cx) | ||
| 698 | + }), | ||
| 699 | + ); | ||
| 700 | + | ||
| 701 | + let suspend_button = plain_button( | ||
| 702 | + format!("suspend_user_{}", user.id), | ||
| 703 | + if suspended { "Unsuspend" } else { "Suspend" }, | ||
| 704 | + palette, | ||
| 705 | + entity_action(entity, move |yumush, window, cx| { | ||
| 706 | + yumush.suspend_builder_user(&suspend_id, !suspended, window, cx) | ||
| 707 | + }), | ||
| 708 | + ); | ||
| 709 | + | ||
| 710 | + let sessions_button = plain_button( | ||
| 711 | + format!("sessions_user_{}", user.id), | ||
| 712 | + "Sign out", | ||
| 713 | + palette, | ||
| 714 | + entity_action(entity, move |yumush, window, cx| { | ||
| 715 | + yumush.revoke_builder_sessions(&sessions_id, window, cx) | ||
| 716 | + }), | ||
| 717 | + ); | ||
| 718 | + | ||
| 719 | + let password_button = plain_button( | ||
| 720 | + format!("password_user_{}", user.id), | ||
| 721 | + "Reset password", | ||
| 722 | + palette, | ||
| 723 | + entity_action(entity, move |yumush, _, cx| { | ||
| 724 | + yumush.toggle_builder_password(&password_id, cx) | ||
| 725 | + }), | ||
| 726 | + ); | ||
| 727 | + | ||
| 728 | + let toggle_button = clickable( | ||
| 729 | + format!("sessions_list_user_{}", user.id), | ||
| 730 | + palette, | ||
| 731 | + entity_action(entity, move |yumush, window, cx| { | ||
| 732 | + yumush.toggle_builder_sessions(&sessions_list_id, window, cx) | ||
| 733 | + }), | ||
| 734 | + ) | ||
| 735 | + .tip("Actions") | ||
| 736 | + .size(px(28.)) | ||
| 737 | + .flex_shrink_0() | ||
| 738 | + .flex() | ||
| 739 | + .items_center() | ||
| 740 | + .justify_center() | ||
| 741 | + .rounded(px(6.)) | ||
| 742 | + .text_color(palette.muted) | ||
| 743 | + .when(expanded, |this| this.bg(palette.hover)) | ||
| 744 | + .child(Icon::new(IconName::Ellipsis).w(px(16.)).h(px(16.))); | ||
| 745 | + | ||
| 746 | + let rename_button = plain_button( | ||
| 747 | + format!("rename_user_{}", user.id), | ||
| 748 | + "Rename", | ||
| 749 | + palette, | ||
| 750 | + entity_action(entity, move |yumush, _, cx| { | ||
| 751 | + yumush.toggle_builder_rename(&rename_id, cx) | ||
| 752 | + }), | ||
| 753 | + ); | ||
| 754 | + | ||
| 755 | + let timeout_button = plain_button( | ||
| 756 | + format!("timeout_user_{}", user.id), | ||
| 757 | + "Timeout", | ||
| 758 | + palette, | ||
| 759 | + entity_action(entity, move |yumush, _, cx| { | ||
| 760 | + yumush.toggle_builder_timeout(&timeout_id, cx) | ||
| 761 | + }), | ||
| 762 | + ); | ||
| 763 | + | ||
| 764 | + let ban_button = plain_button( | ||
| 765 | + format!("ban_user_{}", user.id), | ||
| 766 | + "Ban", | ||
| 767 | + palette, | ||
| 768 | + entity_action(entity, move |yumush, _, cx| { | ||
| 769 | + yumush.toggle_builder_ban(&ban_id, cx) | ||
| 770 | + }), | ||
| 771 | + ); | ||
| 772 | + | ||
| 773 | + let profile_button = plain_button( | ||
| 774 | + format!("profile_user_{}", user.id), | ||
| 775 | + "Clear profile", | ||
| 776 | + palette, | ||
| 777 | + entity_action(entity, move |yumush, window, cx| { | ||
| 778 | + yumush.clear_builder_profile(&profile_id, window, cx) | ||
| 779 | + }), | ||
| 780 | + ); | ||
| 781 | + | ||
| 782 | + let purge_button = danger_button( | ||
| 783 | + format!("purge_user_{}", user.id), | ||
| 784 | + "Purge", | ||
| 785 | + pending_purge, | ||
| 786 | + palette, | ||
| 787 | + entity_action(entity, move |yumush, window, cx| { | ||
| 788 | + yumush.purge_builder_user(&purge_id, window, cx) | ||
| 789 | + }), | ||
| 790 | + ); | ||
| 791 | + | ||
| 792 | + let delete_button = danger_button( | ||
| 793 | + format!("delete_user_{}", user.id), | ||
| 794 | + "Delete", | ||
| 795 | + pending_delete, | ||
| 796 | + palette, | ||
| 797 | + entity_action(entity, move |yumush, window, cx| { | ||
| 798 | + yumush.delete_builder_user(&delete_id, window, cx) | ||
| 799 | + }), | ||
| 800 | + ); | ||
| 801 | + | ||
| 802 | + let row = div() | ||
| 803 | + .px(px(14.)) | ||
| 804 | + .py(px(10.)) | ||
| 805 | + .flex() | ||
| 806 | + .items_center() | ||
| 807 | + .gap(px(10.)) | ||
| 808 | + .child(avatar( | ||
| 809 | + palette, | ||
| 810 | + user, | ||
| 811 | + px(28.), | ||
| 812 | + text(11.), | ||
| 813 | + &self.chat.avatars, | ||
| 814 | + )) | ||
| 815 | + .child( | ||
| 816 | + div() | ||
| 817 | + .flex_1() | ||
| 818 | + .min_w_0() | ||
| 819 | + .flex() | ||
| 820 | + .flex_col() | ||
| 821 | + .gap(px(2.)) | ||
| 822 | + .child( | ||
| 823 | + div() | ||
| 824 | + .min_w_0() | ||
| 825 | + .flex() | ||
| 826 | + .items_center() | ||
| 827 | + .gap(px(6.)) | ||
| 828 | + .child( | ||
| 829 | + div() | ||
| 830 | + .min_w_0() | ||
| 831 | + .truncate() | ||
| 832 | + .text_size(text(13.)) | ||
| 833 | + .font_weight(FontWeight::MEDIUM) | ||
| 834 | + .text_color(palette.text) | ||
| 835 | + .child(user.username.clone()), | ||
| 836 | + ) | ||
| 837 | + .when(builder, |this| { | ||
| 838 | + this.child(status("Builder", palette.accent, palette)) | ||
| 839 | + }) | ||
| 840 | + .when(suspended, |this| { | ||
| 841 | + this.child(status("Suspended", palette.danger, palette)) | ||
| 842 | + }) | ||
| 843 | + .when(timed_out, |this| { | ||
| 844 | + this.child(status("Timed out", palette.danger, palette)) | ||
| 845 | + }) | ||
| 846 | + .when(account.ghost, |this| { | ||
| 847 | + this.child(status("Ghost", palette.muted, palette)) | ||
| 848 | + }), | ||
| 849 | + ) | ||
| 850 | + .child( | ||
| 851 | + div() | ||
| 852 | + .min_w_0() | ||
| 853 | + .truncate() | ||
| 854 | + .text_size(text(11.)) | ||
| 855 | + .text_color(palette.muted) | ||
| 856 | + .child(format!("joined {joined} · seen {seen}")), | ||
| 857 | + ), | ||
| 858 | + ) | ||
| 859 | + .children(has_actions.then_some(toggle_button)); | ||
| 860 | + | ||
| 861 | + let panel = expanded_sessions.map(|sessions| { | ||
| 862 | + let open_id = user.id.as_str(); | ||
| 863 | + let password_open = self.chat.pending_builder_password.as_deref() == Some(open_id); | ||
| 864 | + let rename_open = self.chat.pending_builder_rename.as_deref() == Some(open_id); | ||
| 865 | + let timeout_open = self.chat.pending_builder_timeout.as_deref() == Some(open_id); | ||
| 866 | + let ban_open = self.chat.pending_builder_ban.as_deref() == Some(open_id); | ||
| 867 | + | ||
| 868 | + let actions = div() | ||
| 869 | + .px(px(14.)) | ||
| 870 | + .flex() | ||
| 871 | + .flex_wrap() | ||
| 872 | + .items_center() | ||
| 873 | + .gap(px(6.)) | ||
| 874 | + .children(manage.then_some(rename_button)) | ||
| 875 | + .children(manage.then_some(password_button)) | ||
| 876 | + .children(can_role.then_some(role_button)) | ||
| 877 | + .children(manage.then(|| divider(palette))) | ||
| 878 | + .children(manage.then_some(timeout_button)) | ||
| 879 | + .children(manage.then_some(suspend_button)) | ||
| 880 | + .children(manage.then_some(ban_button)) | ||
| 881 | + .children(manage.then_some(sessions_button)) | ||
| 882 | + .children(manage.then(|| divider(palette))) | ||
| 883 | + .children(manage.then_some(profile_button)) | ||
| 884 | + .children(manage.then_some(purge_button)) | ||
| 885 | + .children(can_delete.then_some(delete_button)); | ||
| 886 | + | ||
| 887 | + div() | ||
| 888 | + .flex() | ||
| 889 | + .flex_col() | ||
| 890 | + .gap(px(6.)) | ||
| 891 | + .pt(px(4.)) | ||
| 892 | + .pb(px(10.)) | ||
| 893 | + .bg(palette.window) | ||
| 894 | + .border_t_1() | ||
| 895 | + .border_color(palette.border) | ||
| 896 | + .child(actions) | ||
| 897 | + .children( | ||
| 898 | + password_open.then(|| self.builder_password_row(&user.id, entity, palette)), | ||
| 899 | + ) | ||
| 900 | + .children(rename_open.then(|| self.builder_rename_row(&user.id, entity, palette))) | ||
| 901 | + .children(timeout_open.then(|| self.builder_timeout_row(&user.id, entity, palette))) | ||
| 902 | + .children(ban_open.then(|| self.builder_ban_row(&user.id, entity, palette))) | ||
| 903 | + .children( | ||
| 904 | + sessions.iter().map(|session| { | ||
| 905 | + self.builder_session_row(&user.id, session, entity, palette) | ||
| 906 | + }), | ||
| 907 | + ) | ||
| 908 | + }); | ||
| 909 | + | ||
| 910 | + div().flex().flex_col().child(row).children(panel) | ||
| 911 | + } | ||
| 912 | +} | ||
crates/client/src/gui/builder/voice.rsadded+240 -0
| @@ -0,0 +1,240 @@ | |||
| 1 | +use std::future::Future; | ||
| 2 | + | ||
| 3 | +use common::{ | ||
| 4 | + error::Error, | ||
| 5 | + model::{User, VoiceRoom}, | ||
| 6 | +}; | ||
| 7 | +use gpui_kit::{ | ||
| 8 | + AnyElement, Context, Div, Entity, IntoElement, ParentElement, Styled, Window, assets::IconName, | ||
| 9 | + div, prelude::FluentBuilder as _, px, | ||
| 10 | +}; | ||
| 11 | + | ||
| 12 | +use super::ui::{card, card_header, icon_button, list_card, pill}; | ||
| 13 | +use crate::{ | ||
| 14 | + gui::{Palette, Yumush, avatar, entity_action, notify_error, text}, | ||
| 15 | + network::NetworkHandle, | ||
| 16 | +}; | ||
| 17 | + | ||
| 18 | +fn rows_body(rows: Vec<Div>, empty: &str, palette: Palette) -> Div { | ||
| 19 | + if rows.is_empty() { | ||
| 20 | + return div() | ||
| 21 | + .py(px(24.)) | ||
| 22 | + .flex() | ||
| 23 | + .justify_center() | ||
| 24 | + .text_size(text(12.)) | ||
| 25 | + .text_color(palette.muted) | ||
| 26 | + .child(empty.to_string()); | ||
| 27 | + } | ||
| 28 | + div() | ||
| 29 | + .flex() | ||
| 30 | + .flex_col() | ||
| 31 | + .children(rows.into_iter().enumerate().map(|(index, row)| { | ||
| 32 | + row.when(index > 0, |row| { | ||
| 33 | + row.border_t_1().border_color(palette.border) | ||
| 34 | + }) | ||
| 35 | + })) | ||
| 36 | +} | ||
| 37 | + | ||
| 38 | +impl Yumush { | ||
| 39 | + pub(super) fn load_voice_rooms(&self, window: &mut Window, cx: &mut Context<Self>) { | ||
| 40 | + if !self.is_builder() { | ||
| 41 | + return; | ||
| 42 | + } | ||
| 43 | + let network = self.network.clone(); | ||
| 44 | + | ||
| 45 | + cx.spawn_in(window, async move |this, cx| { | ||
| 46 | + let rooms = match network.voice_rooms().await { | ||
| 47 | + Ok(rooms) => rooms, | ||
| 48 | + Err(error) => { | ||
| 49 | + notify_error(&this, cx, error); | ||
| 50 | + return; | ||
| 51 | + } | ||
| 52 | + }; | ||
| 53 | + | ||
| 54 | + let _ = this.update(cx, |yumush, cx| { | ||
| 55 | + yumush.chat.builder_voice_rooms = rooms; | ||
| 56 | + cx.notify(); | ||
| 57 | + }); | ||
| 58 | + }) | ||
| 59 | + .detach(); | ||
| 60 | + } | ||
| 61 | + | ||
| 62 | + pub(super) fn voice_action<Fut>( | ||
| 63 | + &mut self, | ||
| 64 | + window: &mut Window, | ||
| 65 | + cx: &mut Context<Self>, | ||
| 66 | + action: impl FnOnce(NetworkHandle) -> Fut + 'static, | ||
| 67 | + ) where | ||
| 68 | + Fut: Future<Output = Result<(), Error>> + 'static, | ||
| 69 | + { | ||
| 70 | + let network = self.network.clone(); | ||
| 71 | + | ||
| 72 | + cx.spawn_in(window, async move |this, cx| match action(network).await { | ||
| 73 | + Ok(()) => { | ||
| 74 | + let _ = | ||
| 75 | + this.update_in(cx, |yumush, window, cx| yumush.load_voice_rooms(window, cx)); | ||
| 76 | + } | ||
| 77 | + Err(error) => notify_error(&this, cx, error), | ||
| 78 | + }) | ||
| 79 | + .detach(); | ||
| 80 | + } | ||
| 81 | + | ||
| 82 | + pub(super) fn set_server_mute( | ||
| 83 | + &mut self, | ||
| 84 | + user_id: &str, | ||
| 85 | + muted: bool, | ||
| 86 | + window: &mut Window, | ||
| 87 | + cx: &mut Context<Self>, | ||
| 88 | + ) { | ||
| 89 | + let user_id = user_id.to_string(); | ||
| 90 | + self.voice_action(window, cx, move |network| async move { | ||
| 91 | + network.server_mute(&user_id, muted).await | ||
| 92 | + }); | ||
| 93 | + } | ||
| 94 | + | ||
| 95 | + pub(super) fn kick_from_voice( | ||
| 96 | + &mut self, | ||
| 97 | + user_id: &str, | ||
| 98 | + window: &mut Window, | ||
| 99 | + cx: &mut Context<Self>, | ||
| 100 | + ) { | ||
| 101 | + let user_id = user_id.to_string(); | ||
| 102 | + self.voice_action(window, cx, move |network| async move { | ||
| 103 | + network.kick_from_voice(&user_id).await | ||
| 104 | + }); | ||
| 105 | + } | ||
| 106 | + | ||
| 107 | + pub(super) fn voice_body(&self, entity: &Entity<Self>, palette: Palette) -> Vec<AnyElement> { | ||
| 108 | + let mut body = Vec::new(); | ||
| 109 | + | ||
| 110 | + if self.chat.builder_voice_rooms.is_empty() { | ||
| 111 | + body.push( | ||
| 112 | + list_card(None, Vec::new(), "No active voice rooms", palette).into_any_element(), | ||
| 113 | + ); | ||
| 114 | + } | ||
| 115 | + body.extend(self.chat.builder_voice_rooms.iter().map(|room| { | ||
| 116 | + self.voice_room_card(room, entity, palette) | ||
| 117 | + .into_any_element() | ||
| 118 | + })); | ||
| 119 | + | ||
| 120 | + body | ||
| 121 | + } | ||
| 122 | + | ||
| 123 | + pub(super) fn voice_room_card( | ||
| 124 | + &self, | ||
| 125 | + room: &VoiceRoom, | ||
| 126 | + entity: &Entity<Self>, | ||
| 127 | + palette: Palette, | ||
| 128 | + ) -> Div { | ||
| 129 | + let id = room.community_id.clone(); | ||
| 130 | + let voice = self | ||
| 131 | + .chat | ||
| 132 | + .builder_communities | ||
| 133 | + .iter() | ||
| 134 | + .find(|summary| summary.community.id == id) | ||
| 135 | + .map(|summary| summary.community.voice_enabled); | ||
| 136 | + | ||
| 137 | + let mut actions = | ||
| 138 | + vec![pill(&room.users.len().to_string(), palette.muted, palette).into_any_element()]; | ||
| 139 | + if let Some(enabled) = voice { | ||
| 140 | + actions.push( | ||
| 141 | + icon_button( | ||
| 142 | + format!("room_voice_{id}"), | ||
| 143 | + if enabled { | ||
| 144 | + IconName::Phone | ||
| 145 | + } else { | ||
| 146 | + IconName::PhoneOff | ||
| 147 | + }, | ||
| 148 | + if enabled { "Voice on" } else { "Voice off" }, | ||
| 149 | + palette.muted, | ||
| 150 | + palette, | ||
| 151 | + entity_action(entity, move |yumush, window, cx| { | ||
| 152 | + yumush.set_builder_community_voice(&id, !enabled, window, cx) | ||
| 153 | + }), | ||
| 154 | + ) | ||
| 155 | + .into_any_element(), | ||
| 156 | + ); | ||
| 157 | + } | ||
| 158 | + | ||
| 159 | + let rows = room | ||
| 160 | + .users | ||
| 161 | + .iter() | ||
| 162 | + .map(|user| { | ||
| 163 | + self.voice_user_row( | ||
| 164 | + user, | ||
| 165 | + room.muted.contains(&user.id), | ||
| 166 | + room.ghosts.contains(&user.id), | ||
| 167 | + entity, | ||
| 168 | + palette, | ||
| 169 | + ) | ||
| 170 | + }) | ||
| 171 | + .collect(); | ||
| 172 | + | ||
| 173 | + card(palette) | ||
| 174 | + .child(card_header(&room.community, actions, palette)) | ||
| 175 | + .child(rows_body(rows, "No users", palette)) | ||
| 176 | + } | ||
| 177 | + | ||
| 178 | + pub(super) fn voice_user_row( | ||
| 179 | + &self, | ||
| 180 | + user: &User, | ||
| 181 | + muted: bool, | ||
| 182 | + ghost: bool, | ||
| 183 | + entity: &Entity<Self>, | ||
| 184 | + palette: Palette, | ||
| 185 | + ) -> Div { | ||
| 186 | + let id = user.id.clone(); | ||
| 187 | + let mute_id = id.clone(); | ||
| 188 | + let kick_id = id.clone(); | ||
| 189 | + | ||
| 190 | + div() | ||
| 191 | + .min_h(px(44.)) | ||
| 192 | + .px(px(14.)) | ||
| 193 | + .py(px(8.)) | ||
| 194 | + .flex() | ||
| 195 | + .items_center() | ||
| 196 | + .gap(px(10.)) | ||
| 197 | + .child(avatar( | ||
| 198 | + palette, | ||
| 199 | + user, | ||
| 200 | + px(24.), | ||
| 201 | + text(10.), | ||
| 202 | + &self.chat.avatars, | ||
| 203 | + )) | ||
| 204 | + .child( | ||
| 205 | + div() | ||
| 206 | + .flex_1() | ||
| 207 | + .min_w_0() | ||
| 208 | + .truncate() | ||
| 209 | + .text_size(text(13.)) | ||
| 210 | + .text_color(palette.text) | ||
| 211 | + .child(user.username.clone()), | ||
| 212 | + ) | ||
| 213 | + .children(muted.then(|| pill("Muted", palette.danger, palette))) | ||
| 214 | + .children(ghost.then(|| pill("Ghost", palette.muted, palette))) | ||
| 215 | + .child(icon_button( | ||
| 216 | + format!("voice_mute_{id}"), | ||
| 217 | + if muted { | ||
| 218 | + IconName::Mic | ||
| 219 | + } else { | ||
| 220 | + IconName::MicOff | ||
| 221 | + }, | ||
| 222 | + if muted { "Unmute" } else { "Mute" }, | ||
| 223 | + palette.muted, | ||
| 224 | + palette, | ||
| 225 | + entity_action(entity, move |yumush, window, cx| { | ||
| 226 | + yumush.set_server_mute(&mute_id, !muted, window, cx) | ||
| 227 | + }), | ||
| 228 | + )) | ||
| 229 | + .child(icon_button( | ||
| 230 | + format!("voice_kick_{id}"), | ||
| 231 | + IconName::UserX, | ||
| 232 | + "Kick", | ||
| 233 | + palette.danger, | ||
| 234 | + palette, | ||
| 235 | + entity_action(entity, move |yumush, window, cx| { | ||
| 236 | + yumush.kick_from_voice(&kick_id, window, cx) | ||
| 237 | + }), | ||
| 238 | + )) | ||
| 239 | + } | ||
| 240 | +} | ||
crates/client/src/gui/chat.rsmodified+162 -150
| @@ -1,4 +1,5 @@ | |||
| 1 | use super::text; | 1 | use super::text; |
| 2 | +use crate::gui::CLOCK; | ||
| 2 | use std::{ | 3 | use std::{ |
| 3 | collections::{HashMap, HashSet}, | 4 | collections::{HashMap, HashSet}, |
| 4 | sync::Arc, | 5 | sync::Arc, |
| @@ -7,9 +8,9 @@ use std::{ | |||
| 7 | 8 | ||
| 8 | use crate::gui::social::InviteLookup; | 9 | use crate::gui::social::InviteLookup; |
| 9 | use crate::gui::{ | 10 | use crate::gui::{ |
| 10 | - FIND_HINT, Palette, Tooltip as _, Yumush, | 11 | + FIND_HINT, Palette, Tooltip as _, Yumush, avatar, badge, |
| 11 | - admin::{AdminTab, Transfer}, | 12 | + builder::{BuilderTab, Transfer}, |
| 12 | - avatar, badge, clickable, community_icon, community_name, | 13 | + clickable, community_icon, community_name, |
| 13 | community_settings::{BanSort, LogSort}, | 14 | community_settings::{BanSort, LogSort}, |
| 14 | composer::{self, Composer}, | 15 | composer::{self, Composer}, |
| 15 | entity_action, hoverable, id_time, | 16 | entity_action, hoverable, id_time, |
| @@ -24,16 +25,17 @@ use common::{ | |||
| 24 | constant::MESSAGE_PAGE_LIMIT, | 25 | constant::MESSAGE_PAGE_LIMIT, |
| 25 | event::Event, | 26 | event::Event, |
| 26 | model::{ | 27 | model::{ |
| 27 | - ACTION_NAMES, Account, AdminLogEntry, Ban, Community, CommunityKind, CommunitySummary, | 28 | + ACTION_NAMES, Account, AdminLogEntry, AttachmentInfo, Ban, Community, CommunityKind, |
| 28 | - DayStat, Friends, Invitation, InviteCode, LogEntry, Member, Message, MessageHit, Overview, | 29 | + CommunitySummary, CommunityUsage, DayStat, Friends, Invitation, InviteCode, LogEntry, |
| 29 | - Rank, Report, ReportStatus, ServerBan, SessionInfo, User, VoiceRoom, AttachmentInfo, | 30 | + Member, Message, MessageHit, Overview, Rank, Report, ReportStatus, ServerBan, SessionInfo, |
| 30 | - CommunityUsage, Usage, | 31 | + Usage, User, VoiceRoom, |
| 31 | }, | 32 | }, |
| 32 | permission, | 33 | permission, |
| 33 | }; | 34 | }; |
| 34 | use gpui_kit::{ | 35 | use gpui_kit::{ |
| 35 | - Anchor, AnyElement, App, AppContext, Context, Div, Entity, FontWeight, Image, InteractiveElement, IntoElement, | 36 | + Anchor, AnyElement, App, AppContext, Context, Div, Entity, FontWeight, Image, |
| 36 | - ParentElement, RenderOnce, ScrollHandle, StatefulInteractiveElement, Styled, Task, Window, | 37 | + InteractiveElement, IntoElement, ParentElement, RenderOnce, ScrollHandle, |
| 38 | + StatefulInteractiveElement, Styled, Task, Window, | ||
| 37 | assets::IconName, | 39 | assets::IconName, |
| 38 | base::Selectable, | 40 | base::Selectable, |
| 39 | component::{ | 41 | component::{ |
| @@ -53,7 +55,7 @@ pub enum View { | |||
| 53 | Chat, | 55 | Chat, |
| 54 | Settings, | 56 | Settings, |
| 55 | CommunitySettings, | 57 | CommunitySettings, |
| 56 | - Admin, | 58 | + Builder, |
| 57 | } | 59 | } |
| 58 | 60 | ||
| 59 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | 61 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| @@ -129,13 +131,13 @@ pub struct Chat { | |||
| 129 | pub(super) friends: Friends, | 131 | pub(super) friends: Friends, |
| 130 | pub(super) invitations: Vec<Invitation>, | 132 | pub(super) invitations: Vec<Invitation>, |
| 131 | pub(super) pending_user_delete: Option<String>, | 133 | pub(super) pending_user_delete: Option<String>, |
| 132 | - pub(super) pending_admin_delete: Option<String>, | 134 | + pub(super) pending_builder_delete: Option<String>, |
| 133 | - pub(super) admin_users: Vec<Account>, | 135 | + pub(super) builder_users: Vec<Account>, |
| 134 | - pub(super) pending_admin_password: Option<String>, | 136 | + pub(super) pending_builder_password: Option<String>, |
| 135 | - pub(super) admin_password: Entity<InputState>, | 137 | + pub(super) builder_password: Entity<InputState>, |
| 136 | - pub(super) admin_sessions: Option<(String, Vec<SessionInfo>)>, | 138 | + pub(super) builder_sessions: Option<(String, Vec<SessionInfo>)>, |
| 137 | - pub(super) pending_admin_rename: Option<String>, | 139 | + pub(super) pending_builder_rename: Option<String>, |
| 138 | - pub(super) admin_rename: Entity<InputState>, | 140 | + pub(super) builder_rename: Entity<InputState>, |
| 139 | pub(super) server_name: Entity<InputState>, | 141 | pub(super) server_name: Entity<InputState>, |
| 140 | pub(super) server_limit: Entity<InputState>, | 142 | pub(super) server_limit: Entity<InputState>, |
| 141 | pub(super) server_user_quota: Entity<InputState>, | 143 | pub(super) server_user_quota: Entity<InputState>, |
| @@ -150,55 +152,55 @@ pub struct Chat { | |||
| 150 | pub(super) server_logins_per_minute: Entity<InputState>, | 152 | pub(super) server_logins_per_minute: Entity<InputState>, |
| 151 | pub(super) server_registration_open: bool, | 153 | pub(super) server_registration_open: bool, |
| 152 | pub(super) server_maintenance: bool, | 154 | pub(super) server_maintenance: bool, |
| 153 | - pub(super) pending_admin_timeout: Option<String>, | 155 | + pub(super) pending_builder_timeout: Option<String>, |
| 154 | - pub(super) admin_timeout: Entity<InputState>, | 156 | + pub(super) builder_timeout: Entity<InputState>, |
| 155 | - pub(super) pending_admin_ban: Option<String>, | 157 | + pub(super) pending_builder_ban: Option<String>, |
| 156 | - pub(super) admin_ban_reason: Entity<InputState>, | 158 | + pub(super) builder_ban_reason: Entity<InputState>, |
| 157 | - pub(super) admin_ban_ip: bool, | 159 | + pub(super) builder_ban_ip: bool, |
| 158 | - pub(super) admin_bans: Vec<ServerBan>, | 160 | + pub(super) builder_bans: Vec<ServerBan>, |
| 159 | - pub(super) admin_invites: Vec<InviteCode>, | 161 | + pub(super) builder_invites: Vec<InviteCode>, |
| 160 | pub(super) invite_uses: Entity<InputState>, | 162 | pub(super) invite_uses: Entity<InputState>, |
| 161 | pub(super) invite_hours: Entity<InputState>, | 163 | pub(super) invite_hours: Entity<InputState>, |
| 162 | pub(super) pending_invite_delete: Option<String>, | 164 | pub(super) pending_invite_delete: Option<String>, |
| 163 | pub(super) new_username: Entity<InputState>, | 165 | pub(super) new_username: Entity<InputState>, |
| 164 | pub(super) new_password: Entity<InputState>, | 166 | pub(super) new_password: Entity<InputState>, |
| 165 | - pub(super) admin_tab: AdminTab, | 167 | + pub(super) builder_tab: BuilderTab, |
| 166 | - pub(super) admin_overview: Option<Overview>, | 168 | + pub(super) builder_overview: Option<Overview>, |
| 167 | - pub(super) admin_logs: Vec<AdminLogEntry>, | 169 | + pub(super) builder_logs: Vec<AdminLogEntry>, |
| 168 | - pub(super) admin_stats: Vec<DayStat>, | 170 | + pub(super) builder_stats: Vec<DayStat>, |
| 169 | - pub(super) admin_log_actor: Entity<InputState>, | 171 | + pub(super) builder_log_actor: Entity<InputState>, |
| 170 | - pub(super) admin_log_target: Entity<InputState>, | 172 | + pub(super) builder_log_target: Entity<InputState>, |
| 171 | - pub(super) admin_log_action: Entity<SelectState<SearchableVec<&'static str>>>, | 173 | + pub(super) builder_log_action: Entity<SelectState<SearchableVec<&'static str>>>, |
| 172 | - pub(super) admin_log_from: Entity<InputState>, | 174 | + pub(super) builder_log_from: Entity<InputState>, |
| 173 | - pub(super) admin_log_to: Entity<InputState>, | 175 | + pub(super) builder_log_to: Entity<InputState>, |
| 174 | - pub(super) admin_search: Entity<InputState>, | 176 | + pub(super) builder_search: Entity<InputState>, |
| 175 | - pub(super) admin_communities: Vec<CommunitySummary>, | 177 | + pub(super) builder_communities: Vec<CommunitySummary>, |
| 176 | - pub(super) admin_reports: Vec<Report>, | 178 | + pub(super) builder_reports: Vec<Report>, |
| 177 | pub(super) report_filter: Option<ReportStatus>, | 179 | pub(super) report_filter: Option<ReportStatus>, |
| 178 | pub(super) open_reports: usize, | 180 | pub(super) open_reports: usize, |
| 179 | pub(super) report: Option<ReportTarget>, | 181 | pub(super) report: Option<ReportTarget>, |
| 180 | pub(super) report_reason: Entity<InputState>, | 182 | pub(super) report_reason: Entity<InputState>, |
| 181 | pub(super) pending_community_delete: Option<String>, | 183 | pub(super) pending_community_delete: Option<String>, |
| 182 | - pub(super) admin_message_text: Entity<InputState>, | 184 | + pub(super) builder_message_text: Entity<InputState>, |
| 183 | - pub(super) admin_message_user: Entity<InputState>, | 185 | + pub(super) builder_message_user: Entity<InputState>, |
| 184 | - pub(super) admin_message_community: Entity<InputState>, | 186 | + pub(super) builder_message_community: Entity<InputState>, |
| 185 | - pub(super) admin_message_from: Entity<InputState>, | 187 | + pub(super) builder_message_from: Entity<InputState>, |
| 186 | - pub(super) admin_message_to: Entity<InputState>, | 188 | + pub(super) builder_message_to: Entity<InputState>, |
| 187 | - pub(super) admin_messages: Vec<MessageHit>, | 189 | + pub(super) builder_messages: Vec<MessageHit>, |
| 188 | - pub(super) admin_attachment_user: Entity<InputState>, | 190 | + pub(super) builder_attachment_user: Entity<InputState>, |
| 189 | - pub(super) admin_attachment_community: Entity<InputState>, | 191 | + pub(super) builder_attachment_community: Entity<InputState>, |
| 190 | - pub(super) admin_usage_users: Vec<Usage>, | 192 | + pub(super) builder_usage_users: Vec<Usage>, |
| 191 | - pub(super) admin_usage_communities: Vec<CommunityUsage>, | 193 | + pub(super) builder_usage_communities: Vec<CommunityUsage>, |
| 192 | - pub(super) admin_attachments: Vec<AttachmentInfo>, | 194 | + pub(super) builder_attachments: Vec<AttachmentInfo>, |
| 193 | pub(super) pending_attachment_delete: Option<String>, | 195 | pub(super) pending_attachment_delete: Option<String>, |
| 194 | pub(super) pending_message_delete: Option<String>, | 196 | pub(super) pending_message_delete: Option<String>, |
| 195 | - pub(super) pending_admin_purge: Option<String>, | 197 | + pub(super) pending_builder_purge: Option<String>, |
| 196 | pub(super) transfer: Option<Transfer>, | 198 | pub(super) transfer: Option<Transfer>, |
| 197 | pub(super) voice: HashMap<String, Vec<String>>, | 199 | pub(super) voice: HashMap<String, Vec<String>>, |
| 198 | pub(super) voice_state: VoiceState, | 200 | pub(super) voice_state: VoiceState, |
| 199 | pub(super) speaking: HashSet<String>, | 201 | pub(super) speaking: HashSet<String>, |
| 200 | pub(super) server_muted: HashSet<String>, | 202 | pub(super) server_muted: HashSet<String>, |
| 201 | - pub(super) admin_voice_rooms: Vec<VoiceRoom>, | 203 | + pub(super) builder_voice_rooms: Vec<VoiceRoom>, |
| 202 | pub(super) voice_task: Option<Task<()>>, | 204 | pub(super) voice_task: Option<Task<()>>, |
| 203 | pub(super) media: Media, | 205 | pub(super) media: Media, |
| 204 | } | 206 | } |
| @@ -232,50 +234,54 @@ impl Chat { | |||
| 232 | 234 | ||
| 233 | let ban_search = new_input("Search...", window, cx); | 235 | let ban_search = new_input("Search...", window, cx); |
| 234 | let log_search = new_input("Search...", window, cx); | 236 | let log_search = new_input("Search...", window, cx); |
| 235 | - let admin_search = new_input("Search...", window, cx); | 237 | + let builder_search = new_input("Search...", window, cx); |
| 236 | - let admin_password = new_input("New password...", window, cx); | 238 | + let builder_password = new_input("New password...", window, cx); |
| 237 | - admin_password.update(cx, |input, cx| input.set_masked(true, window, cx)); | 239 | + builder_password.update(cx, |input, cx| input.set_masked(true, window, cx)); |
| 238 | - let admin_rename = new_input("New username...", window, cx); | 240 | + let builder_rename = new_input("New username...", window, cx); |
| 239 | let server_name = new_input("Server name...", window, cx); | 241 | let server_name = new_input("Server name...", window, cx); |
| 240 | - let server_limit = new_input("Max attachment (MB)...", window, cx); | 242 | + let server_limit = new_input("", window, cx); |
| 241 | - let server_user_quota = new_input("0 = off", window, cx); | 243 | + let server_user_quota = new_input("", window, cx); |
| 242 | - let server_community_quota = new_input("0 = off", window, cx); | 244 | + let server_community_quota = new_input("", window, cx); |
| 243 | - let server_message_retention = new_input("0 = off", window, cx); | 245 | + let server_message_retention = new_input("", window, cx); |
| 244 | - let server_attachment_retention = new_input("0 = off", window, cx); | 246 | + let server_attachment_retention = new_input("", window, cx); |
| 245 | let server_announcement = new_input("Announcement...", window, cx); | 247 | let server_announcement = new_input("Announcement...", window, cx); |
| 246 | - let server_max_communities = new_input("0 = unlimited", window, cx); | 248 | + let server_max_communities = new_input("", window, cx); |
| 247 | let server_max_message_length = new_input("Max message length...", window, cx); | 249 | let server_max_message_length = new_input("Max message length...", window, cx); |
| 248 | - let server_max_avatar = new_input("Max avatar (KB)...", window, cx); | 250 | + let server_max_avatar = new_input("", window, cx); |
| 249 | - let server_messages_per_minute = new_input("0 = off", window, cx); | 251 | + let server_messages_per_minute = new_input("", window, cx); |
| 250 | - let server_logins_per_minute = new_input("0 = off", window, cx); | 252 | + let server_logins_per_minute = new_input("", window, cx); |
| 251 | - let admin_timeout = new_input("Minutes...", window, cx); | 253 | + let builder_timeout = new_input("Minutes...", window, cx); |
| 252 | - let admin_ban_reason = new_input("Reason...", window, cx); | 254 | + let builder_ban_reason = new_input("Reason...", window, cx); |
| 253 | let invite_uses = new_input("Uses...", window, cx); | 255 | let invite_uses = new_input("Uses...", window, cx); |
| 254 | - let invite_hours = new_input("Hours (optional)...", window, cx); | 256 | + let invite_hours = new_input("Hours", window, cx); |
| 255 | let new_username = new_input("Username...", window, cx); | 257 | let new_username = new_input("Username...", window, cx); |
| 256 | let new_password = new_input("Password...", window, cx); | 258 | let new_password = new_input("Password...", window, cx); |
| 257 | let report_reason = new_input("Reason...", window, cx); | 259 | let report_reason = new_input("Reason...", window, cx); |
| 258 | - let admin_message_text = new_input("Text...", window, cx); | 260 | + let builder_message_text = new_input("Text...", window, cx); |
| 259 | - let admin_message_user = new_input("User...", window, cx); | 261 | + let builder_message_user = new_input("User...", window, cx); |
| 260 | - let admin_message_community = new_input("Community...", window, cx); | 262 | + let builder_message_community = new_input("Community...", window, cx); |
| 261 | - let admin_message_from = new_input("From YYYY-MM-DD", window, cx); | 263 | + let builder_message_from = new_input("From YYYY-MM-DD", window, cx); |
| 262 | - let admin_message_to = new_input("To YYYY-MM-DD", window, cx); | 264 | + let builder_message_to = new_input("To YYYY-MM-DD", window, cx); |
| 263 | - let admin_attachment_user = new_input("User...", window, cx); | 265 | + let builder_attachment_user = new_input("User...", window, cx); |
| 264 | - let admin_attachment_community = new_input("Community...", window, cx); | 266 | + let builder_attachment_community = new_input("Community...", window, cx); |
| 265 | - let admin_log_actor = new_input("Actor...", window, cx); | 267 | + let builder_log_actor = new_input("Actor...", window, cx); |
| 266 | - let admin_log_target = new_input("Target...", window, cx); | 268 | + let builder_log_target = new_input("Target...", window, cx); |
| 267 | - let admin_log_from = new_input("From YYYY-MM-DD", window, cx); | 269 | + let builder_log_from = new_input("From YYYY-MM-DD", window, cx); |
| 268 | - let admin_log_to = new_input("To YYYY-MM-DD", window, cx); | 270 | + let builder_log_to = new_input("To YYYY-MM-DD", window, cx); |
| 269 | - let admin_log_action = cx.new(|cx| { | 271 | + let builder_log_action = cx.new(|cx| { |
| 270 | SelectState::new( | 272 | SelectState::new( |
| 271 | - SearchableVec::new(std::iter::once("All").chain(ACTION_NAMES).collect::<Vec<_>>()), | 273 | + SearchableVec::new( |
| 274 | + std::iter::once("All") | ||
| 275 | + .chain(ACTION_NAMES) | ||
| 276 | + .collect::<Vec<_>>(), | ||
| 277 | + ), | ||
| 272 | Some(IndexPath::new(0)), | 278 | Some(IndexPath::new(0)), |
| 273 | window, | 279 | window, |
| 274 | cx, | 280 | cx, |
| 275 | ) | 281 | ) |
| 276 | }); | 282 | }); |
| 277 | new_password.update(cx, |input, cx| input.set_masked(true, window, cx)); | 283 | new_password.update(cx, |input, cx| input.set_masked(true, window, cx)); |
| 278 | - for search in [&ban_search, &log_search, &admin_search] { | 284 | + for search in [&ban_search, &log_search, &builder_search] { |
| 279 | cx.subscribe_in(search, window, |_, _, event, window, cx| { | 285 | cx.subscribe_in(search, window, |_, _, event, window, cx| { |
| 280 | if matches!(event, InputEvent::Change) { | 286 | if matches!(event, InputEvent::Change) { |
| 281 | window.refresh(); | 287 | window.refresh(); |
| @@ -319,36 +325,36 @@ impl Chat { | |||
| 319 | user_requests: HashSet::new(), | 325 | user_requests: HashSet::new(), |
| 320 | delete_step: DeleteStep::Idle, | 326 | delete_step: DeleteStep::Idle, |
| 321 | pending_user_delete: None, | 327 | pending_user_delete: None, |
| 322 | - pending_admin_delete: None, | 328 | + pending_builder_delete: None, |
| 323 | - admin_users: vec![], | 329 | + builder_users: vec![], |
| 324 | - admin_tab: AdminTab::Overview, | 330 | + builder_tab: BuilderTab::Overview, |
| 325 | - admin_overview: None, | 331 | + builder_overview: None, |
| 326 | - admin_logs: vec![], | 332 | + builder_logs: vec![], |
| 327 | - admin_stats: vec![], | 333 | + builder_stats: vec![], |
| 328 | - admin_log_actor, | 334 | + builder_log_actor, |
| 329 | - admin_log_target, | 335 | + builder_log_target, |
| 330 | - admin_log_action, | 336 | + builder_log_action, |
| 331 | - admin_log_from, | 337 | + builder_log_from, |
| 332 | - admin_log_to, | 338 | + builder_log_to, |
| 333 | - admin_search, | 339 | + builder_search, |
| 334 | - admin_communities: vec![], | 340 | + builder_communities: vec![], |
| 335 | pending_community_delete: None, | 341 | pending_community_delete: None, |
| 336 | - admin_message_text, | 342 | + builder_message_text, |
| 337 | - admin_message_user, | 343 | + builder_message_user, |
| 338 | - admin_message_community, | 344 | + builder_message_community, |
| 339 | - admin_message_from, | 345 | + builder_message_from, |
| 340 | - admin_message_to, | 346 | + builder_message_to, |
| 341 | - admin_messages: vec![], | 347 | + builder_messages: vec![], |
| 342 | - admin_attachment_user, | 348 | + builder_attachment_user, |
| 343 | - admin_attachment_community, | 349 | + builder_attachment_community, |
| 344 | - admin_usage_users: vec![], | 350 | + builder_usage_users: vec![], |
| 345 | - admin_usage_communities: vec![], | 351 | + builder_usage_communities: vec![], |
| 346 | - admin_attachments: vec![], | 352 | + builder_attachments: vec![], |
| 347 | pending_attachment_delete: None, | 353 | pending_attachment_delete: None, |
| 348 | pending_message_delete: None, | 354 | pending_message_delete: None, |
| 349 | - pending_admin_purge: None, | 355 | + pending_builder_purge: None, |
| 350 | transfer: None, | 356 | transfer: None, |
| 351 | - admin_password, | 357 | + builder_password, |
| 352 | server_name, | 358 | server_name, |
| 353 | server_limit, | 359 | server_limit, |
| 354 | server_user_quota, | 360 | server_user_quota, |
| @@ -363,23 +369,23 @@ impl Chat { | |||
| 363 | server_logins_per_minute, | 369 | server_logins_per_minute, |
| 364 | server_registration_open: true, | 370 | server_registration_open: true, |
| 365 | server_maintenance: false, | 371 | server_maintenance: false, |
| 366 | - pending_admin_timeout: None, | 372 | + pending_builder_timeout: None, |
| 367 | - admin_timeout, | 373 | + builder_timeout, |
| 368 | - pending_admin_ban: None, | 374 | + pending_builder_ban: None, |
| 369 | - admin_ban_reason, | 375 | + builder_ban_reason, |
| 370 | - admin_ban_ip: false, | 376 | + builder_ban_ip: false, |
| 371 | - admin_bans: vec![], | 377 | + builder_bans: vec![], |
| 372 | - admin_invites: vec![], | 378 | + builder_invites: vec![], |
| 373 | invite_uses, | 379 | invite_uses, |
| 374 | invite_hours, | 380 | invite_hours, |
| 375 | pending_invite_delete: None, | 381 | pending_invite_delete: None, |
| 376 | new_username, | 382 | new_username, |
| 377 | new_password, | 383 | new_password, |
| 378 | - pending_admin_password: None, | 384 | + pending_builder_password: None, |
| 379 | - admin_sessions: None, | 385 | + builder_sessions: None, |
| 380 | - pending_admin_rename: None, | 386 | + pending_builder_rename: None, |
| 381 | - admin_rename, | 387 | + builder_rename, |
| 382 | - admin_reports: vec![], | 388 | + builder_reports: vec![], |
| 383 | report_filter: Some(ReportStatus::Open), | 389 | report_filter: Some(ReportStatus::Open), |
| 384 | open_reports: 0, | 390 | open_reports: 0, |
| 385 | report: None, | 391 | report: None, |
| @@ -388,7 +394,7 @@ impl Chat { | |||
| 388 | voice_state: VoiceState::Off, | 394 | voice_state: VoiceState::Off, |
| 389 | speaking: HashSet::new(), | 395 | speaking: HashSet::new(), |
| 390 | server_muted: HashSet::new(), | 396 | server_muted: HashSet::new(), |
| 391 | - admin_voice_rooms: Vec::new(), | 397 | + builder_voice_rooms: Vec::new(), |
| 392 | voice_task: None, | 398 | voice_task: None, |
| 393 | media: Media::default(), | 399 | media: Media::default(), |
| 394 | composer: Composer::Compose { reply: None }, | 400 | composer: Composer::Compose { reply: None }, |
| @@ -738,40 +744,40 @@ impl Chat { | |||
| 738 | self.found = None; | 744 | self.found = None; |
| 739 | self.select(None); | 745 | self.select(None); |
| 740 | self.unread.clear(); | 746 | self.unread.clear(); |
| 741 | - self.admin_users.clear(); | 747 | + self.builder_users.clear(); |
| 742 | - self.admin_tab = AdminTab::Overview; | 748 | + self.builder_tab = BuilderTab::Overview; |
| 743 | - self.admin_overview = None; | 749 | + self.builder_overview = None; |
| 744 | - self.admin_logs.clear(); | 750 | + self.builder_logs.clear(); |
| 745 | - self.admin_stats.clear(); | 751 | + self.builder_stats.clear(); |
| 746 | - self.admin_communities.clear(); | 752 | + self.builder_communities.clear(); |
| 747 | - self.admin_reports.clear(); | 753 | + self.builder_reports.clear(); |
| 748 | self.report_filter = Some(ReportStatus::Open); | 754 | self.report_filter = Some(ReportStatus::Open); |
| 749 | self.open_reports = 0; | 755 | self.open_reports = 0; |
| 750 | self.report = None; | 756 | self.report = None; |
| 751 | self.pending_community_delete = None; | 757 | self.pending_community_delete = None; |
| 752 | - self.admin_messages.clear(); | 758 | + self.builder_messages.clear(); |
| 753 | - self.admin_usage_users.clear(); | 759 | + self.builder_usage_users.clear(); |
| 754 | - self.admin_usage_communities.clear(); | 760 | + self.builder_usage_communities.clear(); |
| 755 | - self.admin_attachments.clear(); | 761 | + self.builder_attachments.clear(); |
| 756 | self.pending_attachment_delete = None; | 762 | self.pending_attachment_delete = None; |
| 757 | self.pending_message_delete = None; | 763 | self.pending_message_delete = None; |
| 758 | - self.pending_admin_purge = None; | 764 | + self.pending_builder_purge = None; |
| 759 | self.transfer = None; | 765 | self.transfer = None; |
| 760 | - self.pending_admin_delete = None; | 766 | + self.pending_builder_delete = None; |
| 761 | - self.pending_admin_password = None; | 767 | + self.pending_builder_password = None; |
| 762 | - self.admin_sessions = None; | 768 | + self.builder_sessions = None; |
| 763 | - self.pending_admin_rename = None; | 769 | + self.pending_builder_rename = None; |
| 764 | - self.pending_admin_timeout = None; | 770 | + self.pending_builder_timeout = None; |
| 765 | - self.pending_admin_ban = None; | 771 | + self.pending_builder_ban = None; |
| 766 | - self.admin_ban_ip = false; | 772 | + self.builder_ban_ip = false; |
| 767 | - self.admin_bans.clear(); | 773 | + self.builder_bans.clear(); |
| 768 | - self.admin_invites.clear(); | 774 | + self.builder_invites.clear(); |
| 769 | self.pending_invite_delete = None; | 775 | self.pending_invite_delete = None; |
| 770 | self.view = View::Chat; | 776 | self.view = View::Chat; |
| 771 | self.delete_step = DeleteStep::Idle; | 777 | self.delete_step = DeleteStep::Idle; |
| 772 | self.voice.clear(); | 778 | self.voice.clear(); |
| 773 | self.server_muted.clear(); | 779 | self.server_muted.clear(); |
| 774 | - self.admin_voice_rooms.clear(); | 780 | + self.builder_voice_rooms.clear(); |
| 775 | self.set_voice_off(); | 781 | self.set_voice_off(); |
| 776 | } | 782 | } |
| 777 | 783 | ||
| @@ -943,7 +949,7 @@ impl Yumush { | |||
| 943 | .children(self.announcement_banner(entity, palette)) | 949 | .children(self.announcement_banner(entity, palette)) |
| 944 | .map(|panel| match self.chat.view { | 950 | .map(|panel| match self.chat.view { |
| 945 | View::Settings => panel.child(self.settings_page(entity, palette, cx)), | 951 | View::Settings => panel.child(self.settings_page(entity, palette, cx)), |
| 946 | - View::Admin => panel.child(self.admin_page(entity, palette, cx)), | 952 | + View::Builder => panel.child(self.builder_page(entity, palette, cx)), |
| 947 | View::CommunitySettings => { | 953 | View::CommunitySettings => { |
| 948 | panel.child(self.community_settings_page(entity, palette, cx)) | 954 | panel.child(self.community_settings_page(entity, palette, cx)) |
| 949 | } | 955 | } |
| @@ -971,12 +977,14 @@ impl Yumush { | |||
| 971 | .bg(palette.accent) | 977 | .bg(palette.accent) |
| 972 | .text_color(palette.on_accent) | 978 | .text_color(palette.on_accent) |
| 973 | .child(text.to_string()) | 979 | .child(text.to_string()) |
| 974 | - .child(clickable( | 980 | + .child( |
| 975 | - "dismiss_announcement", | 981 | + clickable( |
| 976 | - palette, | 982 | + "dismiss_announcement", |
| 977 | - entity_action(entity, |yumush, _, cx| yumush.dismiss_announcement(cx)), | 983 | + palette, |
| 978 | - ) | 984 | + entity_action(entity, |yumush, _, cx| yumush.dismiss_announcement(cx)), |
| 979 | - .child("×")), | 985 | + ) |
| 986 | + .child("×"), | ||
| 987 | + ), | ||
| 980 | ) | 988 | ) |
| 981 | } | 989 | } |
| 982 | 990 | ||
| @@ -1396,7 +1404,7 @@ impl Yumush { | |||
| 1396 | Ok(participants) => { | 1404 | Ok(participants) => { |
| 1397 | network.set_deafened(deafened); | 1405 | network.set_deafened(deafened); |
| 1398 | let _ = this.update_in(cx, |yumush, _, cx| { | 1406 | let _ = this.update_in(cx, |yumush, _, cx| { |
| 1399 | - let muted = muted || yumush.is_server_muted() || yumush.chat.ghost; | 1407 | + let muted = muted || yumush.mic_locked(); |
| 1400 | yumush.network.set_muted(muted); | 1408 | yumush.network.set_muted(muted); |
| 1401 | yumush.chat.voice.insert(community_id.clone(), participants); | 1409 | yumush.chat.voice.insert(community_id.clone(), participants); |
| 1402 | yumush.chat.voice_state = VoiceState::In { | 1410 | yumush.chat.voice_state = VoiceState::In { |
| @@ -1445,6 +1453,10 @@ impl Yumush { | |||
| 1445 | .is_some_and(|me| self.chat.server_muted.contains(&me.id)) | 1453 | .is_some_and(|me| self.chat.server_muted.contains(&me.id)) |
| 1446 | } | 1454 | } |
| 1447 | 1455 | ||
| 1456 | + pub(super) fn mic_locked(&self) -> bool { | ||
| 1457 | + self.is_server_muted() || self.chat.ghost | ||
| 1458 | + } | ||
| 1459 | + | ||
| 1448 | pub(super) fn force_mute(&mut self, cx: &mut Context<Self>) { | 1460 | pub(super) fn force_mute(&mut self, cx: &mut Context<Self>) { |
| 1449 | if let VoiceState::In { muted, .. } = &mut self.chat.voice_state { | 1461 | if let VoiceState::In { muted, .. } = &mut self.chat.voice_state { |
| 1450 | *muted = true; | 1462 | *muted = true; |
| @@ -1454,7 +1466,7 @@ impl Yumush { | |||
| 1454 | } | 1466 | } |
| 1455 | 1467 | ||
| 1456 | fn toggle_mute(&mut self, cx: &mut Context<Self>) { | 1468 | fn toggle_mute(&mut self, cx: &mut Context<Self>) { |
| 1457 | - if self.is_server_muted() || self.chat.ghost { | 1469 | + if self.mic_locked() { |
| 1458 | return; | 1470 | return; |
| 1459 | } | 1471 | } |
| 1460 | if let VoiceState::In { muted, .. } = &mut self.chat.voice_state { | 1472 | if let VoiceState::In { muted, .. } = &mut self.chat.voice_state { |
| @@ -1841,7 +1853,7 @@ fn render_message( | |||
| 1841 | div() | 1853 | div() |
| 1842 | .text_size(text(12.)) | 1854 | .text_size(text(12.)) |
| 1843 | .text_color(palette.muted) | 1855 | .text_color(palette.muted) |
| 1844 | - .child(id_time(&message.id, "%H:%M")), | 1856 | + .child(id_time(&message.id, CLOCK)), |
| 1845 | ), | 1857 | ), |
| 1846 | ) | 1858 | ) |
| 1847 | }) | 1859 | }) |
crates/client/src/gui/community_settings.rsmodified+2 -1
| @@ -1,4 +1,5 @@ | |||
| 1 | use super::text; | 1 | use super::text; |
| 2 | +use crate::gui::STAMP; | ||
| 2 | use std::{collections::HashMap, sync::Arc}; | 3 | use std::{collections::HashMap, sync::Arc}; |
| 3 | 4 | ||
| 4 | use common::{ | 5 | use common::{ |
| @@ -656,7 +657,7 @@ fn log_row(entry: &LogEntry, palette: Palette, avatars: &HashMap<String, Arc<Ima | |||
| 656 | .flex_shrink_0() | 657 | .flex_shrink_0() |
| 657 | .text_size(text(11.)) | 658 | .text_size(text(11.)) |
| 658 | .text_color(palette.muted) | 659 | .text_color(palette.muted) |
| 659 | - .child(id_time(&entry.id, "%b %-d, %H:%M")), | 660 | + .child(id_time(&entry.id, STAMP)), |
| 660 | ) | 661 | ) |
| 661 | } | 662 | } |
| 662 | 663 | ||
crates/client/src/gui/media_view.rsmodified+4 -7
| @@ -6,10 +6,7 @@ use std::{ | |||
| 6 | time::Duration, | 6 | time::Duration, |
| 7 | }; | 7 | }; |
| 8 | 8 | ||
| 9 | -use common::{ | 9 | +use common::model::{Attachment, AttachmentKind, Message}; |
| 10 | - error::size_text, | ||
| 11 | - model::{Attachment, AttachmentKind, Message}, | ||
| 12 | -}; | ||
| 13 | use gpui_kit::{ | 10 | use gpui_kit::{ |
| 14 | App, AppContext, Context, Entity, FontWeight, Image, ImageFormat, InteractiveElement, | 11 | App, AppContext, Context, Entity, FontWeight, Image, ImageFormat, InteractiveElement, |
| 15 | IntoElement, ParentElement, PathPromptOptions, RenderImage, StatefulInteractiveElement, Styled, | 12 | IntoElement, ParentElement, PathPromptOptions, RenderImage, StatefulInteractiveElement, Styled, |
| @@ -25,7 +22,7 @@ use gpui_kit::{ | |||
| 25 | px, | 22 | px, |
| 26 | }; | 23 | }; |
| 27 | 24 | ||
| 28 | -use super::{Palette, Tooltip as _, Yumush, clickable, notify_error}; | 25 | +use super::{Palette, Tooltip as _, Yumush, clickable, notify_error, storage}; |
| 29 | use crate::{ | 26 | use crate::{ |
| 30 | embed::{self, Embed, EmbedKind}, | 27 | embed::{self, Embed, EmbedKind}, |
| 31 | media::Player, | 28 | media::Player, |
| @@ -612,7 +609,7 @@ fn file_view( | |||
| 612 | .h(px(20.)) | 609 | .h(px(20.)) |
| 613 | .text_color(palette.muted), | 610 | .text_color(palette.muted), |
| 614 | ) | 611 | ) |
| 615 | - .child(name_block(attachment, size_text(attachment.size), palette)) | 612 | + .child(name_block(attachment, storage(attachment.size), palette)) |
| 616 | .child(icon_button( | 613 | .child(icon_button( |
| 617 | format!("save_{}", attachment.id), | 614 | format!("save_{}", attachment.id), |
| 618 | IconName::ArrowDown, | 615 | IconName::ArrowDown, |
| @@ -656,7 +653,7 @@ fn player_view( | |||
| 656 | time_text(player.position()), | 653 | time_text(player.position()), |
| 657 | time_text(player.info().duration.unwrap_or_default()) | 654 | time_text(player.info().duration.unwrap_or_default()) |
| 658 | ), | 655 | ), |
| 659 | - None => size_text(attachment.size), | 656 | + None => storage(attachment.size), |
| 660 | }; | 657 | }; |
| 661 | let controls = card(palette) | 658 | let controls = card(palette) |
| 662 | .child(toggle) | 659 | .child(toggle) |
crates/client/src/gui/profile.rsmodified+5 -7
| @@ -1,4 +1,5 @@ | |||
| 1 | use super::text; | 1 | use super::text; |
| 2 | +use crate::gui::DAY_YEAR; | ||
| 2 | use std::{collections::HashMap, sync::Arc}; | 3 | use std::{collections::HashMap, sync::Arc}; |
| 3 | 4 | ||
| 4 | use common::{ | 5 | use common::{ |
| @@ -202,7 +203,7 @@ impl Card { | |||
| 202 | div() | 203 | div() |
| 203 | .text_size(text(12.)) | 204 | .text_size(text(12.)) |
| 204 | .text_color(palette.muted) | 205 | .text_color(palette.muted) |
| 205 | - .child(format!("Joined {}", id_time(&self.user.id, "%b %-d, %Y"))), | 206 | + .child(format!("Joined {}", id_time(&self.user.id, DAY_YEAR))), |
| 206 | ) | 207 | ) |
| 207 | .children( | 208 | .children( |
| 208 | self.reporting | 209 | self.reporting |
| @@ -223,16 +224,13 @@ impl Card { | |||
| 223 | let user_id = self.user.id.clone(); | 224 | let user_id = self.user.id.clone(); |
| 224 | let mut rows = Vec::new(); | 225 | let mut rows = Vec::new(); |
| 225 | 226 | ||
| 226 | - for (rank, label) in [ | 227 | + for rank in [Rank::Member, Rank::Moderator, Rank::Admin] { |
| 227 | - (Rank::Member, "Make member"), | ||
| 228 | - (Rank::Moderator, "Make moderator"), | ||
| 229 | - (Rank::Admin, "Make admin"), | ||
| 230 | - ] { | ||
| 231 | if target != Some(rank) && permission::can_set_rank(actor, target, rank) { | 228 | if target != Some(rank) && permission::can_set_rank(actor, target, rank) { |
| 232 | let user_id = user_id.clone(); | 229 | let user_id = user_id.clone(); |
| 230 | + let label = format!("Make {}", rank.label().to_lowercase()); | ||
| 233 | rows.push( | 231 | rows.push( |
| 234 | menu_row( | 232 | menu_row( |
| 235 | - label, | 233 | + label.clone(), |
| 236 | label, | 234 | label, |
| 237 | IconName::Shield, | 235 | IconName::Shield, |
| 238 | palette.text, | 236 | palette.text, |
crates/client/src/gui/sidebar.rsmodified+4 -4
| @@ -1,6 +1,6 @@ | |||
| 1 | use super::text; | 1 | use super::text; |
| 2 | use common::{ | 2 | use common::{ |
| 3 | - model::{CommunityKind, Invitation, User}, | 3 | + model::{CommunityKind, Invitation, Role, User}, |
| 4 | permission, | 4 | permission, |
| 5 | validate::validate_community_name, | 5 | validate::validate_community_name, |
| 6 | }; | 6 | }; |
| @@ -523,12 +523,12 @@ impl Yumush { | |||
| 523 | ) | 523 | ) |
| 524 | .children(self.is_builder().then(|| { | 524 | .children(self.is_builder().then(|| { |
| 525 | icon_button( | 525 | icon_button( |
| 526 | - "admin_button".to_string(), | 526 | + "builder_button".to_string(), |
| 527 | palette, | 527 | palette, |
| 528 | IconName::Shield, | 528 | IconName::Shield, |
| 529 | - "Admin", | 529 | + Role::Builder.label(), |
| 530 | entity_action(entity, |yumush, window, cx| { | 530 | entity_action(entity, |yumush, window, cx| { |
| 531 | - yumush.show_view(View::Admin, window, cx) | 531 | + yumush.show_view(View::Builder, window, cx) |
| 532 | }), | 532 | }), |
| 533 | ) | 533 | ) |
| 534 | })) | 534 | })) |
crates/client/src/gui/snippet.rsmodified+2 -1
| @@ -1,4 +1,5 @@ | |||
| 1 | use super::text; | 1 | use super::text; |
| 2 | +use crate::gui::CLOCK; | ||
| 2 | use common::model::{Message, Role, SnippetMessage, User}; | 3 | use common::model::{Message, Role, SnippetMessage, User}; |
| 3 | use gpui_kit::{ | 4 | use gpui_kit::{ |
| 4 | Entity, FontWeight, IntoElement, ParentElement, Styled, div, prelude::FluentBuilder as _, px, | 5 | Entity, FontWeight, IntoElement, ParentElement, Styled, div, prelude::FluentBuilder as _, px, |
| @@ -84,7 +85,7 @@ fn snippet_row( | |||
| 84 | div() | 85 | div() |
| 85 | .text_size(text(11.)) | 86 | .text_size(text(11.)) |
| 86 | .text_color(palette.muted) | 87 | .text_color(palette.muted) |
| 87 | - .child(id_time(&item.id, "%H:%M")), | 88 | + .child(id_time(&item.id, CLOCK)), |
| 88 | ), | 89 | ), |
| 89 | ) | 90 | ) |
| 90 | .when(!item.body.is_empty(), |this| { | 91 | .when(!item.body.is_empty(), |this| { |
crates/client/src/gui/social.rsmodified+2 -2
| @@ -2,7 +2,7 @@ use super::text; | |||
| 2 | use std::{fmt::Display, future::Future}; | 2 | use std::{fmt::Display, future::Future}; |
| 3 | 3 | ||
| 4 | use common::{ | 4 | use common::{ |
| 5 | - model::{Community, CommunityKind}, | 5 | + model::{Community, CommunityKind, Rank}, |
| 6 | validate::validate_username, | 6 | validate::validate_username, |
| 7 | }; | 7 | }; |
| 8 | use gpui_kit::{ | 8 | use gpui_kit::{ |
| @@ -410,7 +410,7 @@ fn invite_result( | |||
| 410 | div() | 410 | div() |
| 411 | .text_size(text(13.)) | 411 | .text_size(text(13.)) |
| 412 | .text_color(palette.muted) | 412 | .text_color(palette.muted) |
| 413 | - .child("Member"), | 413 | + .child(Rank::Member.label()), |
| 414 | ) | 414 | ) |
| 415 | .into_any_element(), | 415 | .into_any_element(), |
| 416 | ), | 416 | ), |
crates/client/src/settings.rsmodified+11 -13
| @@ -37,6 +37,8 @@ pub struct Scale(pub FontSize); | |||
| 37 | impl Global for Scale {} | 37 | impl Global for Scale {} |
| 38 | 38 | ||
| 39 | impl FontSize { | 39 | impl FontSize { |
| 40 | + const ALL: [Self; 3] = [FontSize::Small, FontSize::Medium, FontSize::Large]; | ||
| 41 | + | ||
| 40 | fn name(self) -> &'static str { | 42 | fn name(self) -> &'static str { |
| 41 | match self { | 43 | match self { |
| 42 | FontSize::Small => "small", | 44 | FontSize::Small => "small", |
| @@ -46,12 +48,7 @@ impl FontSize { | |||
| 46 | } | 48 | } |
| 47 | 49 | ||
| 48 | fn parse(value: &str) -> Option<Self> { | 50 | fn parse(value: &str) -> Option<Self> { |
| 49 | - match value { | 51 | + Self::ALL.into_iter().find(|size| size.name() == value) |
| 50 | - "small" => Some(FontSize::Small), | ||
| 51 | - "medium" => Some(FontSize::Medium), | ||
| 52 | - "large" => Some(FontSize::Large), | ||
| 53 | - _ => None, | ||
| 54 | - } | ||
| 55 | } | 52 | } |
| 56 | 53 | ||
| 57 | pub fn pixels(self) -> Pixels { | 54 | pub fn pixels(self) -> Pixels { |
| @@ -64,6 +61,13 @@ impl FontSize { | |||
| 64 | } | 61 | } |
| 65 | 62 | ||
| 66 | impl ThemeChoice { | 63 | impl ThemeChoice { |
| 64 | + const ALL: [Self; 4] = [ | ||
| 65 | + ThemeChoice::System, | ||
| 66 | + ThemeChoice::Light, | ||
| 67 | + ThemeChoice::Dark, | ||
| 68 | + ThemeChoice::Black, | ||
| 69 | + ]; | ||
| 70 | + | ||
| 67 | fn name(self) -> &'static str { | 71 | fn name(self) -> &'static str { |
| 68 | match self { | 72 | match self { |
| 69 | ThemeChoice::System => "system", | 73 | ThemeChoice::System => "system", |
| @@ -74,13 +78,7 @@ impl ThemeChoice { | |||
| 74 | } | 78 | } |
| 75 | 79 | ||
| 76 | fn parse(value: &str) -> Option<Self> { | 80 | fn parse(value: &str) -> Option<Self> { |
| 77 | - match value { | 81 | + Self::ALL.into_iter().find(|choice| choice.name() == value) |
| 78 | - "system" => Some(ThemeChoice::System), | ||
| 79 | - "light" => Some(ThemeChoice::Light), | ||
| 80 | - "dark" => Some(ThemeChoice::Dark), | ||
| 81 | - "black" => Some(ThemeChoice::Black), | ||
| 82 | - _ => None, | ||
| 83 | - } | ||
| 84 | } | 82 | } |
| 85 | } | 83 | } |
| 86 | 84 | ||
crates/client/src/voice/audio.rsmodified+4 -2
| @@ -26,6 +26,8 @@ mod backend; | |||
| 26 | const RETRY_AFTER_ERROR: Duration = Duration::from_secs(1); | 26 | const RETRY_AFTER_ERROR: Duration = Duration::from_secs(1); |
| 27 | const RETRY_AFTER_CHANGE: Duration = Duration::from_millis(100); | 27 | const RETRY_AFTER_CHANGE: Duration = Duration::from_millis(100); |
| 28 | const STOP_CHECK: Duration = Duration::from_millis(100); | 28 | const STOP_CHECK: Duration = Duration::from_millis(100); |
| 29 | +const BYTES_PER_FRAME: usize = size_of::<[f32; 2]>(); | ||
| 30 | +const REPORT_FRAMES: usize = VOICE_SAMPLE_RATE as usize / VOICE_FRAME_SAMPLES; | ||
| 29 | 31 | ||
| 30 | #[derive(Debug, Clone, PartialEq, Eq)] | 32 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 31 | pub struct Device { | 33 | pub struct Device { |
| @@ -233,10 +235,10 @@ fn tone(capture: Option<&Capture>, playback: &Playback, stop: &AtomicBool) { | |||
| 233 | energy += sample * sample | 235 | energy += sample * sample |
| 234 | }); | 236 | }); |
| 235 | frames += 1; | 237 | frames += 1; |
| 236 | - if frames == 50 { | 238 | + if frames == REPORT_FRAMES { |
| 237 | info!( | 239 | info!( |
| 238 | "voice tone rms {:.3}", | 240 | "voice tone rms {:.3}", |
| 239 | - (energy / (50 * VOICE_FRAME_SAMPLES) as f32).sqrt() | 241 | + (energy / (REPORT_FRAMES * VOICE_FRAME_SAMPLES) as f32).sqrt() |
| 240 | ); | 242 | ); |
| 241 | energy = 0.0; | 243 | energy = 0.0; |
| 242 | frames = 0; | 244 | frames = 0; |
crates/client/src/voice/audio/pipewire.rsmodified+4 -5
| @@ -3,7 +3,6 @@ use std::{ | |||
| 3 | error::Error, | 3 | error::Error, |
| 4 | rc::Rc, | 4 | rc::Rc, |
| 5 | sync::atomic::{AtomicBool, Ordering}, | 5 | sync::atomic::{AtomicBool, Ordering}, |
| 6 | - time::Duration, | ||
| 7 | }; | 6 | }; |
| 8 | 7 | ||
| 9 | use common::constant::{VOICE_FRAME_SAMPLES, VOICE_SAMPLE_RATE}; | 8 | use common::constant::{VOICE_FRAME_SAMPLES, VOICE_SAMPLE_RATE}; |
| @@ -11,12 +10,12 @@ use pipewire as pw; | |||
| 11 | use pw::{loop_::Timeout, properties::properties, spa, stream::StreamState, types::ObjectType}; | 10 | use pw::{loop_::Timeout, properties::properties, spa, stream::StreamState, types::ObjectType}; |
| 12 | use spa::pod::Pod; | 11 | use spa::pod::Pod; |
| 13 | 12 | ||
| 14 | -use super::{Choice, Device, Devices, Worker, play, record, stereo_bytes, supervise}; | 13 | +use super::{ |
| 14 | + BYTES_PER_FRAME, Choice, Device, Devices, STOP_CHECK, Worker, play, record, stereo_bytes, | ||
| 15 | + supervise, | ||
| 16 | +}; | ||
| 15 | use crate::voice::{Capture, Playback}; | 17 | use crate::voice::{Capture, Playback}; |
| 16 | 18 | ||
| 17 | -const STOP_CHECK: Duration = Duration::from_millis(100); | ||
| 18 | -const BYTES_PER_FRAME: usize = 8; | ||
| 19 | - | ||
| 20 | pub struct Backend { | 19 | pub struct Backend { |
| 21 | _worker: Worker, | 20 | _worker: Worker, |
| 22 | } | 21 | } |
crates/client/src/voice/audio/wasapi.rsmodified+4 -5
| @@ -13,12 +13,11 @@ use wasapi::{ | |||
| 13 | }; | 13 | }; |
| 14 | 14 | ||
| 15 | use super::{ | 15 | use super::{ |
| 16 | - Choice, Device, Devices, Downmix, Worker, play, record, stereo_bytes, stereo_frame, supervise, | 16 | + BYTES_PER_FRAME, Choice, Device, Devices, Downmix, STOP_CHECK, Worker, play, record, |
| 17 | + stereo_bytes, stereo_frame, supervise, | ||
| 17 | }; | 18 | }; |
| 18 | use crate::voice::{Capture, Playback}; | 19 | use crate::voice::{Capture, Playback}; |
| 19 | 20 | ||
| 20 | -const WAIT_MS: u32 = 100; | ||
| 21 | -const BYTES_PER_FRAME: usize = 8; | ||
| 22 | const CHECK_DEFAULT_EVERY: Duration = Duration::from_secs(1); | 21 | const CHECK_DEFAULT_EVERY: Duration = Duration::from_secs(1); |
| 23 | 22 | ||
| 24 | pub struct Backend { | 23 | pub struct Backend { |
| @@ -213,7 +212,7 @@ fn capture_frames( | |||
| 213 | let mut downmix = Downmix::default(); | 212 | let mut downmix = Downmix::default(); |
| 214 | let mut checked = Instant::now(); | 213 | let mut checked = Instant::now(); |
| 215 | while !stop.load(Ordering::Relaxed) { | 214 | while !stop.load(Ordering::Relaxed) { |
| 216 | - let _ = event.wait_for_event(WAIT_MS); | 215 | + let _ = event.wait_for_event(STOP_CHECK.as_millis() as u32); |
| 217 | while reader.get_next_packet_size()?.unwrap_or(0) > 0 { | 216 | while reader.get_next_packet_size()?.unwrap_or(0) > 0 { |
| 218 | reader.read_from_device_to_deque(&mut bytes)?; | 217 | reader.read_from_device_to_deque(&mut bytes)?; |
| 219 | } | 218 | } |
| @@ -270,7 +269,7 @@ fn render_frames( | |||
| 270 | let mut bytes: Vec<u8> = Vec::new(); | 269 | let mut bytes: Vec<u8> = Vec::new(); |
| 271 | let mut checked = Instant::now(); | 270 | let mut checked = Instant::now(); |
| 272 | while !stop.load(Ordering::Relaxed) { | 271 | while !stop.load(Ordering::Relaxed) { |
| 273 | - let _ = event.wait_for_event(WAIT_MS); | 272 | + let _ = event.wait_for_event(STOP_CHECK.as_millis() as u32); |
| 274 | let frames = client.get_available_space_in_frames()? as usize; | 273 | let frames = client.get_available_space_in_frames()? as usize; |
| 275 | bytes.resize(frames * BYTES_PER_FRAME, 0); | 274 | bytes.resize(frames * BYTES_PER_FRAME, 0); |
| 276 | let chunks = bytes.as_chunks_mut::<BYTES_PER_FRAME>().0; | 275 | let chunks = bytes.as_chunks_mut::<BYTES_PER_FRAME>().0; |
crates/client/src/voice/engine.rsmodified+3 -3
| @@ -15,16 +15,16 @@ use crate::settings::Profile; | |||
| 15 | 15 | ||
| 16 | pub const SILENT_DB: f32 = -100.0; | 16 | pub const SILENT_DB: f32 = -100.0; |
| 17 | const JITTER_CAPACITY: usize = 10; | 17 | const JITTER_CAPACITY: usize = 10; |
| 18 | -const FRAME_MS: f32 = 20.0; | 18 | +const FRAME_MS: f32 = VOICE_FRAME_SAMPLES as f32 * 1000.0 / VOICE_SAMPLE_RATE as f32; |
| 19 | const MIN_FRAMES: usize = 1; | 19 | const MIN_FRAMES: usize = 1; |
| 20 | const MAX_FRAMES: usize = 6; | 20 | const MAX_FRAMES: usize = 6; |
| 21 | const DRIFT_MARGIN: usize = 2; | 21 | const DRIFT_MARGIN: usize = 2; |
| 22 | const QUIET: f32 = 0.01; | 22 | const QUIET: f32 = 0.01; |
| 23 | const SPEAKING_WINDOW: Duration = Duration::from_millis(300); | 23 | const SPEAKING_WINDOW: Duration = Duration::from_millis(300); |
| 24 | const RESTART_AFTER: Duration = Duration::from_secs(1); | 24 | const RESTART_AFTER: Duration = Duration::from_secs(1); |
| 25 | -const MAX_DECODE_SAMPLES: usize = 5760; | 25 | +const MAX_DECODE_SAMPLES: usize = VOICE_SAMPLE_RATE as usize * 120 / 1000; |
| 26 | const HANGOVER_FRAMES: u32 = 15; | 26 | const HANGOVER_FRAMES: u32 = 15; |
| 27 | -const FADE_SAMPLES: usize = 240; | 27 | +const FADE_SAMPLES: usize = VOICE_SAMPLE_RATE as usize * 5 / 1000; |
| 28 | const EXPECTED_LOSS_PERC: u8 = 10; | 28 | const EXPECTED_LOSS_PERC: u8 = 10; |
| 29 | 29 | ||
| 30 | fn rms(samples: &[f32]) -> f32 { | 30 | fn rms(samples: &[f32]) -> f32 { |
crates/common/src/constant.rsmodified+4 -0
| @@ -7,6 +7,7 @@ pub const MINIMUM_MESSAGE_LENGTH: usize = 1; | |||
| 7 | pub const MAXIMUM_COMMUNITY_NAME_LENGTH: usize = 16; | 7 | pub const MAXIMUM_COMMUNITY_NAME_LENGTH: usize = 16; |
| 8 | pub const MINIMUM_COMMUNITY_NAME_LENGTH: usize = 3; | 8 | pub const MINIMUM_COMMUNITY_NAME_LENGTH: usize = 3; |
| 9 | pub const MESSAGE_PAGE_LIMIT: usize = 50; | 9 | pub const MESSAGE_PAGE_LIMIT: usize = 50; |
| 10 | +pub const LOG_PAGE_LIMIT: u32 = 500; | ||
| 10 | pub const AVATAR_SIZE: u32 = 128; | 11 | pub const AVATAR_SIZE: u32 = 128; |
| 11 | pub const MAXIMUM_AVATAR_BYTES: usize = 32 * 1024; | 12 | pub const MAXIMUM_AVATAR_BYTES: usize = 32 * 1024; |
| 12 | pub const MAXIMUM_ANNOUNCEMENT_LENGTH: usize = 500; | 13 | pub const MAXIMUM_ANNOUNCEMENT_LENGTH: usize = 500; |
| @@ -27,3 +28,6 @@ pub const MAXIMUM_ATTACHMENT_NAME_LENGTH: usize = 128; | |||
| 27 | pub const THUMBNAIL_SIZE: u32 = 400; | 28 | pub const THUMBNAIL_SIZE: u32 = 400; |
| 28 | pub const ATTACHMENT_CHUNK_BYTES: usize = 64 * 1024; | 29 | pub const ATTACHMENT_CHUNK_BYTES: usize = 64 * 1024; |
| 29 | pub const ATTACHMENT_HEAD_BYTES: usize = 64; | 30 | pub const ATTACHMENT_HEAD_BYTES: usize = 64; |
| 31 | +pub const DEFAULT_MAX_ATTACHMENT_BYTES: u64 = 25 * 1024 * 1024; | ||
| 32 | +pub const DEFAULT_MESSAGES_PER_MINUTE: u32 = 30; | ||
| 33 | +pub const DEFAULT_LOGINS_PER_MINUTE: u32 = 10; | ||
crates/common/src/model.rsmodified+230 -91
| @@ -1,5 +1,10 @@ | |||
| 1 | use bitcode::{Decode, Encode}; | 1 | use bitcode::{Decode, Encode}; |
| 2 | 2 | ||
| 3 | +use crate::constant::{ | ||
| 4 | + DEFAULT_LOGINS_PER_MINUTE, DEFAULT_MAX_ATTACHMENT_BYTES, DEFAULT_MESSAGES_PER_MINUTE, | ||
| 5 | + MAXIMUM_AVATAR_BYTES, MAXIMUM_MESSAGE_LENGTH, | ||
| 6 | +}; | ||
| 7 | + | ||
| 3 | #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Encode, Decode)] | 8 | #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Encode, Decode)] |
| 4 | pub enum Role { | 9 | pub enum Role { |
| 5 | User, | 10 | User, |
| @@ -7,6 +12,8 @@ pub enum Role { | |||
| 7 | } | 12 | } |
| 8 | 13 | ||
| 9 | impl Role { | 14 | impl Role { |
| 15 | + pub const ALL: [Self; 2] = [Role::User, Role::Builder]; | ||
| 16 | + | ||
| 10 | pub fn as_str(self) -> &'static str { | 17 | pub fn as_str(self) -> &'static str { |
| 11 | match self { | 18 | match self { |
| 12 | Role::User => "user", | 19 | Role::User => "user", |
| @@ -14,13 +21,16 @@ impl Role { | |||
| 14 | } | 21 | } |
| 15 | } | 22 | } |
| 16 | 23 | ||
| 17 | - pub fn parse(text: &str) -> Option<Self> { | 24 | + pub fn label(self) -> &'static str { |
| 18 | - match text { | 25 | + match self { |
| 19 | - "user" => Some(Role::User), | 26 | + Role::User => "User", |
| 20 | - "builder" => Some(Role::Builder), | 27 | + Role::Builder => "Builder", |
| 21 | - _ => None, | ||
| 22 | } | 28 | } |
| 23 | } | 29 | } |
| 30 | + | ||
| 31 | + pub fn parse(text: &str) -> Option<Self> { | ||
| 32 | + Self::ALL.into_iter().find(|role| role.as_str() == text) | ||
| 33 | + } | ||
| 24 | } | 34 | } |
| 25 | 35 | ||
| 26 | #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Encode, Decode)] | 36 | #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Encode, Decode)] |
| @@ -33,6 +43,14 @@ pub enum Rank { | |||
| 33 | } | 43 | } |
| 34 | 44 | ||
| 35 | impl Rank { | 45 | impl Rank { |
| 46 | + pub const ALL: [Self; 5] = [ | ||
| 47 | + Rank::Member, | ||
| 48 | + Rank::Moderator, | ||
| 49 | + Rank::Admin, | ||
| 50 | + Rank::Owner, | ||
| 51 | + Rank::Builder, | ||
| 52 | + ]; | ||
| 53 | + | ||
| 36 | pub fn as_str(self) -> &'static str { | 54 | pub fn as_str(self) -> &'static str { |
| 37 | match self { | 55 | match self { |
| 38 | Rank::Member => "member", | 56 | Rank::Member => "member", |
| @@ -43,16 +61,19 @@ impl Rank { | |||
| 43 | } | 61 | } |
| 44 | } | 62 | } |
| 45 | 63 | ||
| 46 | - pub fn parse(text: &str) -> Option<Self> { | 64 | + pub fn label(self) -> &'static str { |
| 47 | - match text { | 65 | + match self { |
| 48 | - "member" => Some(Rank::Member), | 66 | + Rank::Member => "Member", |
| 49 | - "moderator" => Some(Rank::Moderator), | 67 | + Rank::Moderator => "Moderator", |
| 50 | - "admin" => Some(Rank::Admin), | 68 | + Rank::Admin => "Admin", |
| 51 | - "owner" => Some(Rank::Owner), | 69 | + Rank::Owner => "Owner", |
| 52 | - "builder" => Some(Rank::Builder), | 70 | + Rank::Builder => "Builder", |
| 53 | - _ => None, | ||
| 54 | } | 71 | } |
| 55 | } | 72 | } |
| 73 | + | ||
| 74 | + pub fn parse(text: &str) -> Option<Self> { | ||
| 75 | + Self::ALL.into_iter().find(|rank| rank.as_str() == text) | ||
| 76 | + } | ||
| 56 | } | 77 | } |
| 57 | 78 | ||
| 58 | #[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] | 79 | #[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] |
| @@ -179,68 +200,157 @@ pub enum AdminAction { | |||
| 179 | GhostVoice { seconds: u64 }, | 200 | GhostVoice { seconds: u64 }, |
| 180 | } | 201 | } |
| 181 | 202 | ||
| 182 | -pub const ACTION_NAMES: [&str; 28] = [ | 203 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 183 | - "role_changed", | 204 | +pub enum AdminActionKind { |
| 184 | - "user_deleted", | 205 | + RoleChanged, |
| 185 | - "user_suspended", | 206 | + UserDeleted, |
| 186 | - "user_unsuspended", | 207 | + UserSuspended, |
| 187 | - "sessions_revoked", | 208 | + UserUnsuspended, |
| 188 | - "password_reset", | 209 | + SessionsRevoked, |
| 189 | - "profile_cleared", | 210 | + PasswordReset, |
| 190 | - "user_created", | 211 | + ProfileCleared, |
| 191 | - "community_deleted", | 212 | + UserCreated, |
| 192 | - "ownership_transferred", | 213 | + CommunityDeleted, |
| 193 | - "settings_changed", | 214 | + OwnershipTransferred, |
| 194 | - "user_renamed", | 215 | + SettingsChanged, |
| 195 | - "user_timed_out", | 216 | + UserRenamed, |
| 196 | - "user_banned", | 217 | + UserTimedOut, |
| 197 | - "ban_removed", | 218 | + UserBanned, |
| 198 | - "invite_created", | 219 | + BanRemoved, |
| 199 | - "invite_deleted", | 220 | + InviteCreated, |
| 200 | - "report_handled", | 221 | + InviteDeleted, |
| 201 | - "message_deleted", | 222 | + ReportHandled, |
| 202 | - "user_purged", | 223 | + MessageDeleted, |
| 203 | - "voice_kicked", | 224 | + UserPurged, |
| 204 | - "voice_muted", | 225 | + VoiceKicked, |
| 205 | - "community_voice_set", | 226 | + VoiceMuted, |
| 206 | - "attachment_deleted", | 227 | + CommunityVoiceSet, |
| 207 | - "ghost_enabled", | 228 | + AttachmentDeleted, |
| 208 | - "ghost_disabled", | 229 | + GhostEnabled, |
| 209 | - "ghost_visit", | 230 | + GhostDisabled, |
| 210 | - "ghost_voice", | 231 | + GhostVisit, |
| 211 | -]; | 232 | + GhostVoice, |
| 233 | +} | ||
| 234 | + | ||
| 235 | +impl AdminActionKind { | ||
| 236 | + pub const ALL: [Self; 28] = [ | ||
| 237 | + AdminActionKind::RoleChanged, | ||
| 238 | + AdminActionKind::UserDeleted, | ||
| 239 | + AdminActionKind::UserSuspended, | ||
| 240 | + AdminActionKind::UserUnsuspended, | ||
| 241 | + AdminActionKind::SessionsRevoked, | ||
| 242 | + AdminActionKind::PasswordReset, | ||
| 243 | + AdminActionKind::ProfileCleared, | ||
| 244 | + AdminActionKind::UserCreated, | ||
| 245 | + AdminActionKind::CommunityDeleted, | ||
| 246 | + AdminActionKind::OwnershipTransferred, | ||
| 247 | + AdminActionKind::SettingsChanged, | ||
| 248 | + AdminActionKind::UserRenamed, | ||
| 249 | + AdminActionKind::UserTimedOut, | ||
| 250 | + AdminActionKind::UserBanned, | ||
| 251 | + AdminActionKind::BanRemoved, | ||
| 252 | + AdminActionKind::InviteCreated, | ||
| 253 | + AdminActionKind::InviteDeleted, | ||
| 254 | + AdminActionKind::ReportHandled, | ||
| 255 | + AdminActionKind::MessageDeleted, | ||
| 256 | + AdminActionKind::UserPurged, | ||
| 257 | + AdminActionKind::VoiceKicked, | ||
| 258 | + AdminActionKind::VoiceMuted, | ||
| 259 | + AdminActionKind::CommunityVoiceSet, | ||
| 260 | + AdminActionKind::AttachmentDeleted, | ||
| 261 | + AdminActionKind::GhostEnabled, | ||
| 262 | + AdminActionKind::GhostDisabled, | ||
| 263 | + AdminActionKind::GhostVisit, | ||
| 264 | + AdminActionKind::GhostVoice, | ||
| 265 | + ]; | ||
| 266 | + | ||
| 267 | + pub const fn as_str(self) -> &'static str { | ||
| 268 | + match self { | ||
| 269 | + AdminActionKind::RoleChanged => "role_changed", | ||
| 270 | + AdminActionKind::UserDeleted => "user_deleted", | ||
| 271 | + AdminActionKind::UserSuspended => "user_suspended", | ||
| 272 | + AdminActionKind::UserUnsuspended => "user_unsuspended", | ||
| 273 | + AdminActionKind::SessionsRevoked => "sessions_revoked", | ||
| 274 | + AdminActionKind::PasswordReset => "password_reset", | ||
| 275 | + AdminActionKind::ProfileCleared => "profile_cleared", | ||
| 276 | + AdminActionKind::UserCreated => "user_created", | ||
| 277 | + AdminActionKind::CommunityDeleted => "community_deleted", | ||
| 278 | + AdminActionKind::OwnershipTransferred => "ownership_transferred", | ||
| 279 | + AdminActionKind::SettingsChanged => "settings_changed", | ||
| 280 | + AdminActionKind::UserRenamed => "user_renamed", | ||
| 281 | + AdminActionKind::UserTimedOut => "user_timed_out", | ||
| 282 | + AdminActionKind::UserBanned => "user_banned", | ||
| 283 | + AdminActionKind::BanRemoved => "ban_removed", | ||
| 284 | + AdminActionKind::InviteCreated => "invite_created", | ||
| 285 | + AdminActionKind::InviteDeleted => "invite_deleted", | ||
| 286 | + AdminActionKind::ReportHandled => "report_handled", | ||
| 287 | + AdminActionKind::MessageDeleted => "message_deleted", | ||
| 288 | + AdminActionKind::UserPurged => "user_purged", | ||
| 289 | + AdminActionKind::VoiceKicked => "voice_kicked", | ||
| 290 | + AdminActionKind::VoiceMuted => "voice_muted", | ||
| 291 | + AdminActionKind::CommunityVoiceSet => "community_voice_set", | ||
| 292 | + AdminActionKind::AttachmentDeleted => "attachment_deleted", | ||
| 293 | + AdminActionKind::GhostEnabled => "ghost_enabled", | ||
| 294 | + AdminActionKind::GhostDisabled => "ghost_disabled", | ||
| 295 | + AdminActionKind::GhostVisit => "ghost_visit", | ||
| 296 | + AdminActionKind::GhostVoice => "ghost_voice", | ||
| 297 | + } | ||
| 298 | + } | ||
| 299 | + | ||
| 300 | + pub fn parse(text: &str) -> Option<Self> { | ||
| 301 | + Self::ALL.into_iter().find(|kind| kind.as_str() == text) | ||
| 302 | + } | ||
| 303 | +} | ||
| 304 | + | ||
| 305 | +impl AdminAction { | ||
| 306 | + pub fn kind(&self) -> AdminActionKind { | ||
| 307 | + match self { | ||
| 308 | + AdminAction::RoleChanged { .. } => AdminActionKind::RoleChanged, | ||
| 309 | + AdminAction::UserDeleted => AdminActionKind::UserDeleted, | ||
| 310 | + AdminAction::UserSuspended => AdminActionKind::UserSuspended, | ||
| 311 | + AdminAction::UserUnsuspended => AdminActionKind::UserUnsuspended, | ||
| 312 | + AdminAction::SessionsRevoked => AdminActionKind::SessionsRevoked, | ||
| 313 | + AdminAction::PasswordReset => AdminActionKind::PasswordReset, | ||
| 314 | + AdminAction::ProfileCleared => AdminActionKind::ProfileCleared, | ||
| 315 | + AdminAction::UserCreated => AdminActionKind::UserCreated, | ||
| 316 | + AdminAction::CommunityDeleted => AdminActionKind::CommunityDeleted, | ||
| 317 | + AdminAction::OwnershipTransferred => AdminActionKind::OwnershipTransferred, | ||
| 318 | + AdminAction::SettingsChanged => AdminActionKind::SettingsChanged, | ||
| 319 | + AdminAction::UserRenamed { .. } => AdminActionKind::UserRenamed, | ||
| 320 | + AdminAction::UserTimedOut { .. } => AdminActionKind::UserTimedOut, | ||
| 321 | + AdminAction::UserBanned { .. } => AdminActionKind::UserBanned, | ||
| 322 | + AdminAction::BanRemoved => AdminActionKind::BanRemoved, | ||
| 323 | + AdminAction::InviteCreated => AdminActionKind::InviteCreated, | ||
| 324 | + AdminAction::InviteDeleted => AdminActionKind::InviteDeleted, | ||
| 325 | + AdminAction::ReportHandled { .. } => AdminActionKind::ReportHandled, | ||
| 326 | + AdminAction::MessageDeleted => AdminActionKind::MessageDeleted, | ||
| 327 | + AdminAction::UserPurged { .. } => AdminActionKind::UserPurged, | ||
| 328 | + AdminAction::VoiceKicked => AdminActionKind::VoiceKicked, | ||
| 329 | + AdminAction::VoiceMuted { .. } => AdminActionKind::VoiceMuted, | ||
| 330 | + AdminAction::CommunityVoiceSet { .. } => AdminActionKind::CommunityVoiceSet, | ||
| 331 | + AdminAction::AttachmentDeleted => AdminActionKind::AttachmentDeleted, | ||
| 332 | + AdminAction::GhostEnabled => AdminActionKind::GhostEnabled, | ||
| 333 | + AdminAction::GhostDisabled => AdminActionKind::GhostDisabled, | ||
| 334 | + AdminAction::GhostVisit => AdminActionKind::GhostVisit, | ||
| 335 | + AdminAction::GhostVoice { .. } => AdminActionKind::GhostVoice, | ||
| 336 | + } | ||
| 337 | + } | ||
| 338 | +} | ||
| 212 | 339 | ||
| 213 | -pub fn action_name(action: &AdminAction) -> &'static str { | 340 | +const fn action_names() -> [&'static str; 28] { |
| 214 | - match action { | 341 | + let mut names = [""; 28]; |
| 215 | - AdminAction::RoleChanged { .. } => "role_changed", | 342 | + let mut index = 0; |
| 216 | - AdminAction::UserDeleted => "user_deleted", | 343 | + while index < 28 { |
| 217 | - AdminAction::UserSuspended => "user_suspended", | 344 | + names[index] = AdminActionKind::ALL[index].as_str(); |
| 218 | - AdminAction::UserUnsuspended => "user_unsuspended", | 345 | + index += 1; |
| 219 | - AdminAction::SessionsRevoked => "sessions_revoked", | ||
| 220 | - AdminAction::PasswordReset => "password_reset", | ||
| 221 | - AdminAction::ProfileCleared => "profile_cleared", | ||
| 222 | - AdminAction::UserCreated => "user_created", | ||
| 223 | - AdminAction::CommunityDeleted => "community_deleted", | ||
| 224 | - AdminAction::OwnershipTransferred => "ownership_transferred", | ||
| 225 | - AdminAction::SettingsChanged => "settings_changed", | ||
| 226 | - AdminAction::UserRenamed { .. } => "user_renamed", | ||
| 227 | - AdminAction::UserTimedOut { .. } => "user_timed_out", | ||
| 228 | - AdminAction::UserBanned { .. } => "user_banned", | ||
| 229 | - AdminAction::BanRemoved => "ban_removed", | ||
| 230 | - AdminAction::InviteCreated => "invite_created", | ||
| 231 | - AdminAction::InviteDeleted => "invite_deleted", | ||
| 232 | - AdminAction::ReportHandled { .. } => "report_handled", | ||
| 233 | - AdminAction::MessageDeleted => "message_deleted", | ||
| 234 | - AdminAction::UserPurged { .. } => "user_purged", | ||
| 235 | - AdminAction::VoiceKicked => "voice_kicked", | ||
| 236 | - AdminAction::VoiceMuted { .. } => "voice_muted", | ||
| 237 | - AdminAction::CommunityVoiceSet { .. } => "community_voice_set", | ||
| 238 | - AdminAction::AttachmentDeleted => "attachment_deleted", | ||
| 239 | - AdminAction::GhostEnabled => "ghost_enabled", | ||
| 240 | - AdminAction::GhostDisabled => "ghost_disabled", | ||
| 241 | - AdminAction::GhostVisit => "ghost_visit", | ||
| 242 | - AdminAction::GhostVoice { .. } => "ghost_voice", | ||
| 243 | } | 346 | } |
| 347 | + names | ||
| 348 | +} | ||
| 349 | + | ||
| 350 | +pub const ACTION_NAMES: [&str; 28] = action_names(); | ||
| 351 | + | ||
| 352 | +pub fn action_name(action: &AdminAction) -> &'static str { | ||
| 353 | + action.kind().as_str() | ||
| 244 | } | 354 | } |
| 245 | 355 | ||
| 246 | pub fn action_detail(action: &AdminAction) -> Option<String> { | 356 | pub fn action_detail(action: &AdminAction) -> Option<String> { |
| @@ -275,6 +385,12 @@ pub enum ReportStatus { | |||
| 275 | } | 385 | } |
| 276 | 386 | ||
| 277 | impl ReportStatus { | 387 | impl ReportStatus { |
| 388 | + pub const ALL: [Self; 3] = [ | ||
| 389 | + ReportStatus::Open, | ||
| 390 | + ReportStatus::Resolved, | ||
| 391 | + ReportStatus::Dismissed, | ||
| 392 | + ]; | ||
| 393 | + | ||
| 278 | pub fn as_str(self) -> &'static str { | 394 | pub fn as_str(self) -> &'static str { |
| 279 | match self { | 395 | match self { |
| 280 | ReportStatus::Open => "open", | 396 | ReportStatus::Open => "open", |
| @@ -283,14 +399,17 @@ impl ReportStatus { | |||
| 283 | } | 399 | } |
| 284 | } | 400 | } |
| 285 | 401 | ||
| 286 | - pub fn parse(text: &str) -> Option<Self> { | 402 | + pub fn label(self) -> &'static str { |
| 287 | - match text { | 403 | + match self { |
| 288 | - "open" => Some(ReportStatus::Open), | 404 | + ReportStatus::Open => "Open", |
| 289 | - "resolved" => Some(ReportStatus::Resolved), | 405 | + ReportStatus::Resolved => "Resolved", |
| 290 | - "dismissed" => Some(ReportStatus::Dismissed), | 406 | + ReportStatus::Dismissed => "Dismissed", |
| 291 | - _ => None, | ||
| 292 | } | 407 | } |
| 293 | } | 408 | } |
| 409 | + | ||
| 410 | + pub fn parse(text: &str) -> Option<Self> { | ||
| 411 | + Self::ALL.into_iter().find(|status| status.as_str() == text) | ||
| 412 | + } | ||
| 294 | } | 413 | } |
| 295 | 414 | ||
| 296 | #[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] | 415 | #[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] |
| @@ -325,6 +444,27 @@ pub struct ServerSettings { | |||
| 325 | pub logins_per_minute: u32, | 444 | pub logins_per_minute: u32, |
| 326 | } | 445 | } |
| 327 | 446 | ||
| 447 | +impl Default for ServerSettings { | ||
| 448 | + fn default() -> Self { | ||
| 449 | + Self { | ||
| 450 | + name: "yumush".to_string(), | ||
| 451 | + registration_open: true, | ||
| 452 | + max_attachment_bytes: DEFAULT_MAX_ATTACHMENT_BYTES, | ||
| 453 | + maintenance: false, | ||
| 454 | + user_quota_bytes: 0, | ||
| 455 | + community_quota_bytes: 0, | ||
| 456 | + message_retention_days: 0, | ||
| 457 | + attachment_retention_days: 0, | ||
| 458 | + announcement: String::new(), | ||
| 459 | + max_communities_per_user: 0, | ||
| 460 | + max_message_length: MAXIMUM_MESSAGE_LENGTH as u32, | ||
| 461 | + max_avatar_bytes: MAXIMUM_AVATAR_BYTES as u64, | ||
| 462 | + messages_per_minute: DEFAULT_MESSAGES_PER_MINUTE, | ||
| 463 | + logins_per_minute: DEFAULT_LOGINS_PER_MINUTE, | ||
| 464 | + } | ||
| 465 | + } | ||
| 466 | +} | ||
| 467 | + | ||
| 328 | #[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] | 468 | #[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] |
| 329 | pub struct AttachmentInfo { | 469 | pub struct AttachmentInfo { |
| 330 | pub attachment: Attachment, | 470 | pub attachment: Attachment, |
| @@ -354,6 +494,8 @@ pub enum ServerBanKind { | |||
| 354 | } | 494 | } |
| 355 | 495 | ||
| 356 | impl ServerBanKind { | 496 | impl ServerBanKind { |
| 497 | + pub const ALL: [Self; 2] = [ServerBanKind::Username, ServerBanKind::Ip]; | ||
| 498 | + | ||
| 357 | pub fn as_str(self) -> &'static str { | 499 | pub fn as_str(self) -> &'static str { |
| 358 | match self { | 500 | match self { |
| 359 | ServerBanKind::Username => "username", | 501 | ServerBanKind::Username => "username", |
| @@ -362,11 +504,7 @@ impl ServerBanKind { | |||
| 362 | } | 504 | } |
| 363 | 505 | ||
| 364 | pub fn parse(text: &str) -> Option<Self> { | 506 | pub fn parse(text: &str) -> Option<Self> { |
| 365 | - match text { | 507 | + Self::ALL.into_iter().find(|kind| kind.as_str() == text) |
| 366 | - "username" => Some(ServerBanKind::Username), | ||
| 367 | - "ip" => Some(ServerBanKind::Ip), | ||
| 368 | - _ => None, | ||
| 369 | - } | ||
| 370 | } | 508 | } |
| 371 | } | 509 | } |
| 372 | 510 | ||
| @@ -426,6 +564,13 @@ pub enum AttachmentKind { | |||
| 426 | } | 564 | } |
| 427 | 565 | ||
| 428 | impl AttachmentKind { | 566 | impl AttachmentKind { |
| 567 | + pub const ALL: [Self; 4] = [ | ||
| 568 | + AttachmentKind::Image, | ||
| 569 | + AttachmentKind::Video, | ||
| 570 | + AttachmentKind::Audio, | ||
| 571 | + AttachmentKind::File, | ||
| 572 | + ]; | ||
| 573 | + | ||
| 429 | pub fn as_str(self) -> &'static str { | 574 | pub fn as_str(self) -> &'static str { |
| 430 | match self { | 575 | match self { |
| 431 | AttachmentKind::Image => "image", | 576 | AttachmentKind::Image => "image", |
| @@ -436,13 +581,7 @@ impl AttachmentKind { | |||
| 436 | } | 581 | } |
| 437 | 582 | ||
| 438 | pub fn parse(text: &str) -> Option<Self> { | 583 | pub fn parse(text: &str) -> Option<Self> { |
| 439 | - match text { | 584 | + Self::ALL.into_iter().find(|kind| kind.as_str() == text) |
| 440 | - "image" => Some(AttachmentKind::Image), | ||
| 441 | - "video" => Some(AttachmentKind::Video), | ||
| 442 | - "audio" => Some(AttachmentKind::Audio), | ||
| 443 | - "file" => Some(AttachmentKind::File), | ||
| 444 | - _ => None, | ||
| 445 | - } | ||
| 446 | } | 585 | } |
| 447 | } | 586 | } |
| 448 | 587 | ||
crates/common/src/permission.rsmodified+12 -4
| @@ -1,5 +1,13 @@ | |||
| 1 | use crate::model::{Rank, Role}; | 1 | use crate::model::{Rank, Role}; |
| 2 | 2 | ||
| 3 | +pub fn is_builder(role: Role) -> bool { | ||
| 4 | + role == Role::Builder | ||
| 5 | +} | ||
| 6 | + | ||
| 7 | +pub fn bypasses_limits(role: Role) -> bool { | ||
| 8 | + is_builder(role) | ||
| 9 | +} | ||
| 10 | + | ||
| 3 | pub fn effective_rank(role: Role, rank: Option<Rank>) -> Option<Rank> { | 11 | pub fn effective_rank(role: Role, rank: Option<Rank>) -> Option<Rank> { |
| 4 | match role { | 12 | match role { |
| 5 | Role::Builder => Some(Rank::Builder), | 13 | Role::Builder => Some(Rank::Builder), |
| @@ -20,7 +28,7 @@ pub fn can_post(actor: Option<Rank>) -> bool { | |||
| 20 | } | 28 | } |
| 21 | 29 | ||
| 22 | pub fn can_join(role: Role, banned: bool) -> bool { | 30 | pub fn can_join(role: Role, banned: bool) -> bool { |
| 23 | - !banned || role == Role::Builder | 31 | + !banned || is_builder(role) |
| 24 | } | 32 | } |
| 25 | 33 | ||
| 26 | pub fn can_edit_message(is_author: bool) -> bool { | 34 | pub fn can_edit_message(is_author: bool) -> bool { |
| @@ -70,11 +78,11 @@ pub fn can_leave(actor: Option<Rank>) -> bool { | |||
| 70 | } | 78 | } |
| 71 | 79 | ||
| 72 | pub fn can_set_role(actor: Role, is_self: bool) -> bool { | 80 | pub fn can_set_role(actor: Role, is_self: bool) -> bool { |
| 73 | - actor == Role::Builder && !is_self | 81 | + is_builder(actor) && !is_self |
| 74 | } | 82 | } |
| 75 | 83 | ||
| 76 | pub fn can_delete_user(actor: Role, is_self: bool, target: Role) -> bool { | 84 | pub fn can_delete_user(actor: Role, is_self: bool, target: Role) -> bool { |
| 77 | - is_self || (actor == Role::Builder && target != Role::Builder) | 85 | + is_self || can_manage_user(actor, false, target) |
| 78 | } | 86 | } |
| 79 | 87 | ||
| 80 | pub fn can_manage_voice(actor: Option<Rank>) -> bool { | 88 | pub fn can_manage_voice(actor: Option<Rank>) -> bool { |
| @@ -82,7 +90,7 @@ pub fn can_manage_voice(actor: Option<Rank>) -> bool { | |||
| 82 | } | 90 | } |
| 83 | 91 | ||
| 84 | pub fn can_manage_user(actor: Role, is_self: bool, target: Role) -> bool { | 92 | pub fn can_manage_user(actor: Role, is_self: bool, target: Role) -> bool { |
| 85 | - actor == Role::Builder && !is_self && target != Role::Builder | 93 | + is_builder(actor) && !is_self && !is_builder(target) |
| 86 | } | 94 | } |
| 87 | 95 | ||
| 88 | #[cfg(test)] | 96 | #[cfg(test)] |
crates/server/src/admin_log.rsmodified+46 -43
| @@ -1,57 +1,60 @@ | |||
| 1 | use common::{ | 1 | use common::{ |
| 2 | - model::{AdminAction, AdminLogEntry, ReportStatus, Role, action_detail, action_name}, | 2 | + constant::LOG_PAGE_LIMIT, |
| 3 | + model::{ | ||
| 4 | + AdminAction, AdminActionKind, AdminLogEntry, ReportStatus, Role, action_detail, action_name, | ||
| 5 | + }, | ||
| 3 | time, | 6 | time, |
| 4 | }; | 7 | }; |
| 5 | use turso::Connection; | 8 | use turso::Connection; |
| 6 | 9 | ||
| 7 | -use crate::{database::new_id, error::Error, log::user_at}; | 10 | +use crate::{database::new_id, error::Error, user}; |
| 8 | 11 | ||
| 9 | fn action_of(text: &str, detail: Option<String>) -> Option<AdminAction> { | 12 | fn action_of(text: &str, detail: Option<String>) -> Option<AdminAction> { |
| 10 | - match text { | 13 | + let kind = AdminActionKind::parse(text)?; |
| 11 | - "role_changed" => Some(AdminAction::RoleChanged { | 14 | + Some(match kind { |
| 15 | + AdminActionKind::RoleChanged => AdminAction::RoleChanged { | ||
| 12 | role: Role::parse(&detail?)?, | 16 | role: Role::parse(&detail?)?, |
| 13 | - }), | 17 | + }, |
| 14 | - "user_deleted" => Some(AdminAction::UserDeleted), | 18 | + AdminActionKind::UserDeleted => AdminAction::UserDeleted, |
| 15 | - "user_suspended" => Some(AdminAction::UserSuspended), | 19 | + AdminActionKind::UserSuspended => AdminAction::UserSuspended, |
| 16 | - "user_unsuspended" => Some(AdminAction::UserUnsuspended), | 20 | + AdminActionKind::UserUnsuspended => AdminAction::UserUnsuspended, |
| 17 | - "sessions_revoked" => Some(AdminAction::SessionsRevoked), | 21 | + AdminActionKind::SessionsRevoked => AdminAction::SessionsRevoked, |
| 18 | - "password_reset" => Some(AdminAction::PasswordReset), | 22 | + AdminActionKind::PasswordReset => AdminAction::PasswordReset, |
| 19 | - "profile_cleared" => Some(AdminAction::ProfileCleared), | 23 | + AdminActionKind::ProfileCleared => AdminAction::ProfileCleared, |
| 20 | - "user_created" => Some(AdminAction::UserCreated), | 24 | + AdminActionKind::UserCreated => AdminAction::UserCreated, |
| 21 | - "community_deleted" => Some(AdminAction::CommunityDeleted), | 25 | + AdminActionKind::CommunityDeleted => AdminAction::CommunityDeleted, |
| 22 | - "ownership_transferred" => Some(AdminAction::OwnershipTransferred), | 26 | + AdminActionKind::OwnershipTransferred => AdminAction::OwnershipTransferred, |
| 23 | - "settings_changed" => Some(AdminAction::SettingsChanged), | 27 | + AdminActionKind::SettingsChanged => AdminAction::SettingsChanged, |
| 24 | - "user_renamed" => Some(AdminAction::UserRenamed { from: detail? }), | 28 | + AdminActionKind::UserRenamed => AdminAction::UserRenamed { from: detail? }, |
| 25 | - "user_timed_out" => Some(AdminAction::UserTimedOut { | 29 | + AdminActionKind::UserTimedOut => AdminAction::UserTimedOut { |
| 26 | minutes: detail?.parse().ok()?, | 30 | minutes: detail?.parse().ok()?, |
| 27 | - }), | 31 | + }, |
| 28 | - "user_banned" => Some(AdminAction::UserBanned { reason: detail }), | 32 | + AdminActionKind::UserBanned => AdminAction::UserBanned { reason: detail }, |
| 29 | - "ban_removed" => Some(AdminAction::BanRemoved), | 33 | + AdminActionKind::BanRemoved => AdminAction::BanRemoved, |
| 30 | - "invite_created" => Some(AdminAction::InviteCreated), | 34 | + AdminActionKind::InviteCreated => AdminAction::InviteCreated, |
| 31 | - "invite_deleted" => Some(AdminAction::InviteDeleted), | 35 | + AdminActionKind::InviteDeleted => AdminAction::InviteDeleted, |
| 32 | - "report_handled" => Some(AdminAction::ReportHandled { | 36 | + AdminActionKind::ReportHandled => AdminAction::ReportHandled { |
| 33 | status: ReportStatus::parse(&detail?)?, | 37 | status: ReportStatus::parse(&detail?)?, |
| 34 | - }), | 38 | + }, |
| 35 | - "message_deleted" => Some(AdminAction::MessageDeleted), | 39 | + AdminActionKind::MessageDeleted => AdminAction::MessageDeleted, |
| 36 | - "user_purged" => Some(AdminAction::UserPurged { | 40 | + AdminActionKind::UserPurged => AdminAction::UserPurged { |
| 37 | messages: detail?.parse().ok()?, | 41 | messages: detail?.parse().ok()?, |
| 38 | - }), | 42 | + }, |
| 39 | - "voice_kicked" => Some(AdminAction::VoiceKicked), | 43 | + AdminActionKind::VoiceKicked => AdminAction::VoiceKicked, |
| 40 | - "voice_muted" => Some(AdminAction::VoiceMuted { | 44 | + AdminActionKind::VoiceMuted => AdminAction::VoiceMuted { |
| 41 | muted: detail?.parse().ok()?, | 45 | muted: detail?.parse().ok()?, |
| 42 | - }), | 46 | + }, |
| 43 | - "community_voice_set" => Some(AdminAction::CommunityVoiceSet { | 47 | + AdminActionKind::CommunityVoiceSet => AdminAction::CommunityVoiceSet { |
| 44 | enabled: detail?.parse().ok()?, | 48 | enabled: detail?.parse().ok()?, |
| 45 | - }), | 49 | + }, |
| 46 | - "attachment_deleted" => Some(AdminAction::AttachmentDeleted), | 50 | + AdminActionKind::AttachmentDeleted => AdminAction::AttachmentDeleted, |
| 47 | - "ghost_enabled" => Some(AdminAction::GhostEnabled), | 51 | + AdminActionKind::GhostEnabled => AdminAction::GhostEnabled, |
| 48 | - "ghost_disabled" => Some(AdminAction::GhostDisabled), | 52 | + AdminActionKind::GhostDisabled => AdminAction::GhostDisabled, |
| 49 | - "ghost_visit" => Some(AdminAction::GhostVisit), | 53 | + AdminActionKind::GhostVisit => AdminAction::GhostVisit, |
| 50 | - "ghost_voice" => Some(AdminAction::GhostVoice { | 54 | + AdminActionKind::GhostVoice => AdminAction::GhostVoice { |
| 51 | seconds: detail?.parse().ok()?, | 55 | seconds: detail?.parse().ok()?, |
| 52 | - }), | 56 | + }, |
| 53 | - _ => None, | 57 | + }) |
| 54 | - } | ||
| 55 | } | 58 | } |
| 56 | 59 | ||
| 57 | pub async fn record( | 60 | pub async fn record( |
| @@ -89,7 +92,7 @@ pub async fn list( | |||
| 89 | ) -> Result<Vec<AdminLogEntry>, Error> { | 92 | ) -> Result<Vec<AdminLogEntry>, Error> { |
| 90 | let mut rows = conn | 93 | let mut rows = conn |
| 91 | .query( | 94 | .query( |
| 92 | - "SELECT l.id, l.action, l.detail, a.id, a.username, a.role, a.status, a.avatar_id, t.id, t.username, t.role, t.status, t.avatar_id, l.community FROM admin_log l LEFT JOIN user a ON a.id = l.actor_id LEFT JOIN user t ON t.id = l.target_id WHERE (?1 IS NULL OR l.actor_id = ?1) AND (?2 IS NULL OR l.target_id = ?2) AND (?3 IS NULL OR l.action = ?3) AND (?4 IS NULL OR l.id >= ?4) AND (?5 IS NULL OR l.id < ?5) ORDER BY l.id DESC LIMIT 500", | 95 | + &format!("SELECT l.id, l.action, l.detail, {}, {}, l.community FROM admin_log l LEFT JOIN user a ON a.id = l.actor_id LEFT JOIN user t ON t.id = l.target_id WHERE (?1 IS NULL OR l.actor_id = ?1) AND (?2 IS NULL OR l.target_id = ?2) AND (?3 IS NULL OR l.action = ?3) AND (?4 IS NULL OR l.id >= ?4) AND (?5 IS NULL OR l.id < ?5) ORDER BY l.id DESC LIMIT {LOG_PAGE_LIMIT}", user::user_columns("a"), user::user_columns("t")), |
| 93 | ( | 96 | ( |
| 94 | actor_id, | 97 | actor_id, |
| 95 | target_id, | 98 | target_id, |
| @@ -108,8 +111,8 @@ pub async fn list( | |||
| 108 | }; | 111 | }; |
| 109 | entries.push(AdminLogEntry { | 112 | entries.push(AdminLogEntry { |
| 110 | id: row.get(0)?, | 113 | id: row.get(0)?, |
| 111 | - actor: user_at(&row, 3)?, | 114 | + actor: user::from_row_at(&row, 3)?, |
| 112 | - target: user_at(&row, 8)?, | 115 | + target: user::from_row_at(&row, 8)?, |
| 113 | community: row.get(13)?, | 116 | community: row.get(13)?, |
| 114 | action, | 117 | action, |
| 115 | }); | 118 | }); |
crates/server/src/attachment.rsmodified+4 -4
| @@ -21,6 +21,7 @@ use common::{ | |||
| 21 | permission, | 21 | permission, |
| 22 | request::{DownloadRequest, UploadHeader}, | 22 | request::{DownloadRequest, UploadHeader}, |
| 23 | response::Response, | 23 | response::Response, |
| 24 | + time::HOUR_MILLIS, | ||
| 24 | validate::{validate_attachment_name, validate_attachment_size}, | 25 | validate::{validate_attachment_name, validate_attachment_size}, |
| 25 | }; | 26 | }; |
| 26 | use image::{ImageFormat, ImageReader, Limits}; | 27 | use image::{ImageFormat, ImageReader, Limits}; |
| @@ -37,13 +38,12 @@ use crate::{ | |||
| 37 | community, | 38 | community, |
| 38 | database::{self, DB, new_id}, | 39 | database::{self, DB, new_id}, |
| 39 | error::Error, | 40 | error::Error, |
| 40 | - log::user_at, | ||
| 41 | request::{actor_rank, ensure}, | 41 | request::{actor_rank, ensure}, |
| 42 | server_settings, user, | 42 | server_settings, user, |
| 43 | }; | 43 | }; |
| 44 | 44 | ||
| 45 | const COLLECT_INTERVAL: Duration = Duration::from_secs(600); | 45 | const COLLECT_INTERVAL: Duration = Duration::from_secs(600); |
| 46 | -const STALE_AGE: Duration = Duration::from_secs(3600); | 46 | +const STALE_AGE: Duration = Duration::from_millis(HOUR_MILLIS); |
| 47 | const MAXIMUM_IMAGE_SIDE: u32 = 16384; | 47 | const MAXIMUM_IMAGE_SIDE: u32 = 16384; |
| 48 | const MAXIMUM_DECODE_BYTES: u64 = 256 * 1024 * 1024; | 48 | const MAXIMUM_DECODE_BYTES: u64 = 256 * 1024 * 1024; |
| 49 | const LIST_LIMIT: i64 = 200; | 49 | const LIST_LIMIT: i64 = 200; |
| @@ -211,7 +211,7 @@ pub async fn all( | |||
| 211 | ) -> Result<Vec<AttachmentInfo>, Error> { | 211 | ) -> Result<Vec<AttachmentInfo>, Error> { |
| 212 | let mut rows = conn | 212 | let mut rows = conn |
| 213 | .query( | 213 | .query( |
| 214 | - "SELECT a.id, a.name, a.size, a.kind, a.width, a.height, a.community_id, u.id, u.username, u.role, u.status, u.avatar_id FROM attachment a LEFT JOIN user u ON u.id = a.uploader_id WHERE (?1 IS NULL OR a.uploader_id = ?1) AND (?2 IS NULL OR a.community_id = ?2) ORDER BY a.size DESC LIMIT ?3", | 214 | + &format!("SELECT a.id, a.name, a.size, a.kind, a.width, a.height, a.community_id, {} FROM attachment a LEFT JOIN user u ON u.id = a.uploader_id WHERE (?1 IS NULL OR a.uploader_id = ?1) AND (?2 IS NULL OR a.community_id = ?2) ORDER BY a.size DESC LIMIT ?3", user::user_columns("u")), |
| 215 | (user_id, community_id, LIST_LIMIT), | 215 | (user_id, community_id, LIST_LIMIT), |
| 216 | ) | 216 | ) |
| 217 | .await?; | 217 | .await?; |
| @@ -219,7 +219,7 @@ pub async fn all( | |||
| 219 | let mut raw = Vec::new(); | 219 | let mut raw = Vec::new(); |
| 220 | while let Some(row) = rows.next().await? { | 220 | while let Some(row) = rows.next().await? { |
| 221 | let community_id: String = row.get(6)?; | 221 | let community_id: String = row.get(6)?; |
| 222 | - raw.push((from_row(&row)?, user_at(&row, 7)?, community_id)); | 222 | + raw.push((from_row(&row)?, user::from_row_at(&row, 7)?, community_id)); |
| 223 | } | 223 | } |
| 224 | drop(rows); | 224 | drop(rows); |
| 225 | 225 | ||
crates/server/src/authentication.rsmodified+4 -4
| @@ -1,8 +1,8 @@ | |||
| 1 | use argon2::{Argon2, PasswordHasher, PasswordVerifier}; | 1 | use argon2::{Argon2, PasswordHasher, PasswordVerifier}; |
| 2 | use common::{ | 2 | use common::{ |
| 3 | error::Error as CommonError, | 3 | error::Error as CommonError, |
| 4 | - model::{Role, SessionInfo, User}, | 4 | + model::{SessionInfo, User}, |
| 5 | - time, | 5 | + permission, time, |
| 6 | validate::{validate_device, validate_password, validate_username}, | 6 | validate::{validate_device, validate_password, validate_username}, |
| 7 | }; | 7 | }; |
| 8 | use turso::Connection; | 8 | use turso::Connection; |
| @@ -176,7 +176,7 @@ async fn admit(user: &User, conn: &Connection) -> Result<(), Error> { | |||
| 176 | if user::suspended(&user.id, conn).await? { | 176 | if user::suspended(&user.id, conn).await? { |
| 177 | return Err(CommonError::Suspended.into()); | 177 | return Err(CommonError::Suspended.into()); |
| 178 | } | 178 | } |
| 179 | - if user.role != Role::Builder && server_settings::read(conn).await?.maintenance { | 179 | + if !permission::bypasses_limits(user.role) && server_settings::read(conn).await?.maintenance { |
| 180 | return Err(CommonError::Maintenance.into()); | 180 | return Err(CommonError::Maintenance.into()); |
| 181 | } | 181 | } |
| 182 | 182 | ||
| @@ -187,7 +187,7 @@ pub async fn resume(token: &str, conn: &Connection) -> Result<User, Error> { | |||
| 187 | let user = { | 187 | let user = { |
| 188 | let mut rows = conn | 188 | let mut rows = conn |
| 189 | .query( | 189 | .query( |
| 190 | - "SELECT user.id, user.username, user.role, user.status, user.avatar_id FROM session JOIN user ON user.id = session.user_id WHERE session.token = ?1", | 190 | + &format!("SELECT {} FROM session JOIN user ON user.id = session.user_id WHERE session.token = ?1", user::user_columns("user")), |
| 191 | (token,), | 191 | (token,), |
| 192 | ) | 192 | ) |
| 193 | .await?; | 193 | .await?; |
crates/server/src/community.rsmodified+8 -10
| @@ -8,13 +8,15 @@ use turso::{Connection, Row}; | |||
| 8 | use crate::{database::new_id, error::Error, user}; | 8 | use crate::{database::new_id, error::Error, user}; |
| 9 | 9 | ||
| 10 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | 10 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 11 | -enum Kind { | 11 | +pub enum Kind { |
| 12 | Group, | 12 | Group, |
| 13 | Direct, | 13 | Direct, |
| 14 | } | 14 | } |
| 15 | 15 | ||
| 16 | impl Kind { | 16 | impl Kind { |
| 17 | - fn as_str(self) -> &'static str { | 17 | + const ALL: [Self; 2] = [Kind::Group, Kind::Direct]; |
| 18 | + | ||
| 19 | + pub fn as_str(self) -> &'static str { | ||
| 18 | match self { | 20 | match self { |
| 19 | Kind::Group => "group", | 21 | Kind::Group => "group", |
| 20 | Kind::Direct => "direct", | 22 | Kind::Direct => "direct", |
| @@ -22,11 +24,7 @@ impl Kind { | |||
| 22 | } | 24 | } |
| 23 | 25 | ||
| 24 | fn parse(text: &str) -> Option<Self> { | 26 | fn parse(text: &str) -> Option<Self> { |
| 25 | - match text { | 27 | + Self::ALL.into_iter().find(|kind| kind.as_str() == text) |
| 26 | - "group" => Some(Kind::Group), | ||
| 27 | - "direct" => Some(Kind::Direct), | ||
| 28 | - _ => None, | ||
| 29 | - } | ||
| 30 | } | 28 | } |
| 31 | } | 29 | } |
| 32 | 30 | ||
| @@ -49,7 +47,7 @@ fn raw_of(row: &Row) -> Result<Raw, Error> { | |||
| 49 | async fn peer(community_id: &str, viewer_id: &str, conn: &Connection) -> Result<User, Error> { | 47 | async fn peer(community_id: &str, viewer_id: &str, conn: &Connection) -> Result<User, Error> { |
| 50 | let mut rows = conn | 48 | let mut rows = conn |
| 51 | .query( | 49 | .query( |
| 52 | - "SELECT user.id, user.username, user.role, user.status, user.avatar_id FROM direct JOIN user ON user.id = CASE WHEN direct.user_a = ?2 THEN direct.user_b ELSE direct.user_a END WHERE direct.community_id = ?1", | 50 | + &format!("SELECT {} FROM direct JOIN user ON user.id = CASE WHEN direct.user_a = ?2 THEN direct.user_b ELSE direct.user_a END WHERE direct.community_id = ?1", user::user_columns("user")), |
| 53 | (community_id, viewer_id), | 51 | (community_id, viewer_id), |
| 54 | ) | 52 | ) |
| 55 | .await?; | 53 | .await?; |
| @@ -282,8 +280,8 @@ pub async fn voice_enabled(community_id: &str, conn: &Connection) -> Result<bool | |||
| 282 | pub async fn label(community_id: &str, conn: &Connection) -> Result<String, Error> { | 280 | pub async fn label(community_id: &str, conn: &Connection) -> Result<String, Error> { |
| 283 | let mut rows = conn | 281 | let mut rows = conn |
| 284 | .query( | 282 | .query( |
| 285 | - "SELECT CASE WHEN kind = 'direct' THEN 'DM' ELSE name END FROM community WHERE id = ?1", | 283 | + "SELECT CASE WHEN kind = ?2 THEN 'DM' ELSE name END FROM community WHERE id = ?1", |
| 286 | - (community_id,), | 284 | + (community_id, Kind::Direct.as_str()), |
| 287 | ) | 285 | ) |
| 288 | .await?; | 286 | .await?; |
| 289 | let row = rows.next().await?.ok_or(CommonError::NotFound)?; | 287 | let row = rows.next().await?.ok_or(CommonError::NotFound)?; |
crates/server/src/friend.rsmodified+3 -3
| @@ -73,19 +73,19 @@ async fn befriend(me: &str, other: &str, conn: &Connection) -> Result<(), Error> | |||
| 73 | pub async fn state(user_id: &str, conn: &Connection) -> Result<Friends, Error> { | 73 | pub async fn state(user_id: &str, conn: &Connection) -> Result<Friends, Error> { |
| 74 | Ok(Friends { | 74 | Ok(Friends { |
| 75 | friends: users( | 75 | friends: users( |
| 76 | - "SELECT user.id, user.username, user.role, user.status, user.avatar_id FROM user JOIN friend ON friend.friend_id = user.id WHERE friend.user_id = ?1 ORDER BY user.id", | 76 | + &format!("SELECT {} FROM user JOIN friend ON friend.friend_id = user.id WHERE friend.user_id = ?1 ORDER BY user.id", user::user_columns("user")), |
| 77 | user_id, | 77 | user_id, |
| 78 | conn, | 78 | conn, |
| 79 | ) | 79 | ) |
| 80 | .await?, | 80 | .await?, |
| 81 | incoming: users( | 81 | incoming: users( |
| 82 | - "SELECT user.id, user.username, user.role, user.status, user.avatar_id FROM user JOIN friend_request ON friend_request.from_id = user.id WHERE friend_request.to_id = ?1 ORDER BY user.id", | 82 | + &format!("SELECT {} FROM user JOIN friend_request ON friend_request.from_id = user.id WHERE friend_request.to_id = ?1 ORDER BY user.id", user::user_columns("user")), |
| 83 | user_id, | 83 | user_id, |
| 84 | conn, | 84 | conn, |
| 85 | ) | 85 | ) |
| 86 | .await?, | 86 | .await?, |
| 87 | outgoing: users( | 87 | outgoing: users( |
| 88 | - "SELECT user.id, user.username, user.role, user.status, user.avatar_id FROM user JOIN friend_request ON friend_request.to_id = user.id WHERE friend_request.from_id = ?1 ORDER BY user.id", | 88 | + &format!("SELECT {} FROM user JOIN friend_request ON friend_request.to_id = user.id WHERE friend_request.from_id = ?1 ORDER BY user.id", user::user_columns("user")), |
| 89 | user_id, | 89 | user_id, |
| 90 | conn, | 90 | conn, |
| 91 | ) | 91 | ) |
crates/server/src/invite.rsmodified+3 -3
| @@ -5,7 +5,7 @@ use common::{ | |||
| 5 | }; | 5 | }; |
| 6 | use turso::Connection; | 6 | use turso::Connection; |
| 7 | 7 | ||
| 8 | -use crate::{error::Error, log::user_at}; | 8 | +use crate::{error::Error, user}; |
| 9 | 9 | ||
| 10 | const ALPHABET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; | 10 | const ALPHABET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; |
| 11 | const CODE_LENGTH: usize = 8; | 11 | const CODE_LENGTH: usize = 8; |
| @@ -50,7 +50,7 @@ pub async fn create( | |||
| 50 | pub async fn list(conn: &Connection) -> Result<Vec<InviteCode>, Error> { | 50 | pub async fn list(conn: &Connection) -> Result<Vec<InviteCode>, Error> { |
| 51 | let mut rows = conn | 51 | let mut rows = conn |
| 52 | .query( | 52 | .query( |
| 53 | - "SELECT c.code, c.uses_left, c.expires_at, u.id, u.username, u.role, u.status, u.avatar_id FROM invite_code c LEFT JOIN user u ON u.id = c.created_by", | 53 | + &format!("SELECT c.code, c.uses_left, c.expires_at, {} FROM invite_code c LEFT JOIN user u ON u.id = c.created_by", user::user_columns("u")), |
| 54 | (), | 54 | (), |
| 55 | ) | 55 | ) |
| 56 | .await?; | 56 | .await?; |
| @@ -63,7 +63,7 @@ pub async fn list(conn: &Connection) -> Result<Vec<InviteCode>, Error> { | |||
| 63 | code: row.get(0)?, | 63 | code: row.get(0)?, |
| 64 | uses_left: uses_left as u32, | 64 | uses_left: uses_left as u32, |
| 65 | expires_at: expires_at.map(|millis| millis as u64), | 65 | expires_at: expires_at.map(|millis| millis as u64), |
| 66 | - created_by: user_at(&row, 3)?, | 66 | + created_by: user::from_row_at(&row, 3)?, |
| 67 | }); | 67 | }); |
| 68 | } | 68 | } |
| 69 | 69 | ||
crates/server/src/log.rsmodified+7 -22
| @@ -1,10 +1,10 @@ | |||
| 1 | use common::{ | 1 | use common::{ |
| 2 | - error::Error as CommonError, | 2 | + constant::LOG_PAGE_LIMIT, |
| 3 | - model::{LogAction, LogEntry, Rank, Role, User}, | 3 | + model::{LogAction, LogEntry, Rank}, |
| 4 | }; | 4 | }; |
| 5 | -use turso::{Connection, Row}; | 5 | +use turso::Connection; |
| 6 | 6 | ||
| 7 | -use crate::{database::new_id, error::Error}; | 7 | +use crate::{database::new_id, error::Error, user}; |
| 8 | 8 | ||
| 9 | fn parts(action: &LogAction) -> (&'static str, Option<String>) { | 9 | fn parts(action: &LogAction) -> (&'static str, Option<String>) { |
| 10 | match action { | 10 | match action { |
| @@ -43,21 +43,6 @@ fn action_of(text: &str, detail: Option<String>) -> Option<LogAction> { | |||
| 43 | } | 43 | } |
| 44 | } | 44 | } |
| 45 | 45 | ||
| 46 | -pub fn user_at(row: &Row, start: usize) -> Result<Option<User>, Error> { | ||
| 47 | - let Some(id) = row.get::<Option<String>>(start)? else { | ||
| 48 | - return Ok(None); | ||
| 49 | - }; | ||
| 50 | - let role: String = row.get(start + 2)?; | ||
| 51 | - | ||
| 52 | - Ok(Some(User { | ||
| 53 | - id, | ||
| 54 | - username: row.get(start + 1)?, | ||
| 55 | - role: Role::parse(&role).ok_or(CommonError::Server)?, | ||
| 56 | - status: row.get(start + 3)?, | ||
| 57 | - avatar_id: row.get(start + 4)?, | ||
| 58 | - })) | ||
| 59 | -} | ||
| 60 | - | ||
| 61 | pub async fn record( | 46 | pub async fn record( |
| 62 | community_id: &str, | 47 | community_id: &str, |
| 63 | actor_id: Option<&str>, | 48 | actor_id: Option<&str>, |
| @@ -79,7 +64,7 @@ pub async fn record( | |||
| 79 | pub async fn list(community_id: &str, conn: &Connection) -> Result<Vec<LogEntry>, Error> { | 64 | pub async fn list(community_id: &str, conn: &Connection) -> Result<Vec<LogEntry>, Error> { |
| 80 | let mut rows = conn | 65 | let mut rows = conn |
| 81 | .query( | 66 | .query( |
| 82 | - "SELECT l.id, l.action, l.detail, a.id, a.username, a.role, a.status, a.avatar_id, t.id, t.username, t.role, t.status, t.avatar_id FROM community_log l LEFT JOIN user a ON a.id = l.actor_id LEFT JOIN user t ON t.id = l.target_id WHERE l.community_id = ?1 ORDER BY l.id DESC LIMIT 500", | 67 | + &format!("SELECT l.id, l.action, l.detail, {}, {} FROM community_log l LEFT JOIN user a ON a.id = l.actor_id LEFT JOIN user t ON t.id = l.target_id WHERE l.community_id = ?1 ORDER BY l.id DESC LIMIT {LOG_PAGE_LIMIT}", user::user_columns("a"), user::user_columns("t")), |
| 83 | (community_id,), | 68 | (community_id,), |
| 84 | ) | 69 | ) |
| 85 | .await?; | 70 | .await?; |
| @@ -92,8 +77,8 @@ pub async fn list(community_id: &str, conn: &Connection) -> Result<Vec<LogEntry> | |||
| 92 | }; | 77 | }; |
| 93 | entries.push(LogEntry { | 78 | entries.push(LogEntry { |
| 94 | id: row.get(0)?, | 79 | id: row.get(0)?, |
| 95 | - actor: user_at(&row, 3)?, | 80 | + actor: user::from_row_at(&row, 3)?, |
| 96 | - target: user_at(&row, 8)?, | 81 | + target: user::from_row_at(&row, 8)?, |
| 97 | action, | 82 | action, |
| 98 | }); | 83 | }); |
| 99 | } | 84 | } |
crates/server/src/overview.rsmodified+5 -5
| @@ -1,7 +1,7 @@ | |||
| 1 | -use common::model::Overview; | 1 | +use common::model::{Overview, Role}; |
| 2 | use turso::Connection; | 2 | use turso::Connection; |
| 3 | 3 | ||
| 4 | -use crate::{error::Error, network::Presence, voice::Voice}; | 4 | +use crate::{community::Kind, error::Error, network::Presence, voice::Voice}; |
| 5 | 5 | ||
| 6 | pub async fn read( | 6 | pub async fn read( |
| 7 | presence: &Presence, | 7 | presence: &Presence, |
| @@ -12,12 +12,12 @@ pub async fn read( | |||
| 12 | .query( | 12 | .query( |
| 13 | "SELECT | 13 | "SELECT |
| 14 | (SELECT COUNT(*) FROM user), | 14 | (SELECT COUNT(*) FROM user), |
| 15 | - (SELECT COUNT(*) FROM user WHERE role = 'builder'), | 15 | + (SELECT COUNT(*) FROM user WHERE role = ?1), |
| 16 | - (SELECT COUNT(*) FROM community WHERE kind != 'direct'), | 16 | + (SELECT COUNT(*) FROM community WHERE kind != ?2), |
| 17 | (SELECT COALESCE(SUM(size), 0) FROM attachment) | 17 | (SELECT COALESCE(SUM(size), 0) FROM attachment) |
| 18 | + (SELECT COALESCE(SUM(length(data)), 0) FROM avatar) | 18 | + (SELECT COALESCE(SUM(length(data)), 0) FROM avatar) |
| 19 | + (SELECT COALESCE(SUM(length(data)), 0) FROM community_avatar)", | 19 | + (SELECT COALESCE(SUM(length(data)), 0) FROM community_avatar)", |
| 20 | - (), | 20 | + (Role::Builder.as_str(), Kind::Direct.as_str()), |
| 21 | ) | 21 | ) |
| 22 | .await?; | 22 | .await?; |
| 23 | let row = rows | 23 | let row = rows |
crates/server/src/report.rsmodified+10 -9
| @@ -1,10 +1,11 @@ | |||
| 1 | use common::{ | 1 | use common::{ |
| 2 | + constant::LOG_PAGE_LIMIT, | ||
| 2 | error::Error as CommonError, | 3 | error::Error as CommonError, |
| 3 | model::{Report, ReportStatus}, | 4 | model::{Report, ReportStatus}, |
| 4 | }; | 5 | }; |
| 5 | use turso::Connection; | 6 | use turso::Connection; |
| 6 | 7 | ||
| 7 | -use crate::{database::new_id, error::Error, log::user_at}; | 8 | +use crate::{database::new_id, error::Error, user}; |
| 8 | 9 | ||
| 9 | pub async fn is_open( | 10 | pub async fn is_open( |
| 10 | reporter_id: &str, | 11 | reporter_id: &str, |
| @@ -14,8 +15,8 @@ pub async fn is_open( | |||
| 14 | ) -> Result<bool, Error> { | 15 | ) -> Result<bool, Error> { |
| 15 | let mut rows = conn | 16 | let mut rows = conn |
| 16 | .query( | 17 | .query( |
| 17 | - "SELECT EXISTS (SELECT 1 FROM report WHERE reporter_id = ?1 AND target_id = ?2 AND COALESCE(message_id, '') = COALESCE(?3, '') AND status = 'open')", | 18 | + "SELECT EXISTS (SELECT 1 FROM report WHERE reporter_id = ?1 AND target_id = ?2 AND COALESCE(message_id, '') = COALESCE(?3, '') AND status = ?4)", |
| 18 | - (reporter_id, target_id, message_id), | 19 | + (reporter_id, target_id, message_id, ReportStatus::Open.as_str()), |
| 19 | ) | 20 | ) |
| 20 | .await?; | 21 | .await?; |
| 21 | let row = rows | 22 | let row = rows |
| @@ -48,7 +49,7 @@ pub async fn insert( | |||
| 48 | pub async fn list(status: Option<ReportStatus>, conn: &Connection) -> Result<Vec<Report>, Error> { | 49 | pub async fn list(status: Option<ReportStatus>, conn: &Connection) -> Result<Vec<Report>, Error> { |
| 49 | let mut rows = conn | 50 | let mut rows = conn |
| 50 | .query( | 51 | .query( |
| 51 | - "SELECT r.id, rp.id, rp.username, rp.role, rp.status, rp.avatar_id, t.id, t.username, t.role, t.status, t.avatar_id, c.name, r.message_id, r.community_id, r.body, r.reason, r.status, h.id, h.username, h.role, h.status, h.avatar_id FROM report r LEFT JOIN user rp ON rp.id = r.reporter_id LEFT JOIN user t ON t.id = r.target_id LEFT JOIN community c ON c.id = r.community_id LEFT JOIN user h ON h.id = r.handled_by WHERE ?1 IS NULL OR r.status = ?1 ORDER BY r.id DESC LIMIT 500", | 52 | + &format!("SELECT r.id, {}, {}, c.name, r.message_id, r.community_id, r.body, r.reason, r.status, {} FROM report r LEFT JOIN user rp ON rp.id = r.reporter_id LEFT JOIN user t ON t.id = r.target_id LEFT JOIN community c ON c.id = r.community_id LEFT JOIN user h ON h.id = r.handled_by WHERE ?1 IS NULL OR r.status = ?1 ORDER BY r.id DESC LIMIT {LOG_PAGE_LIMIT}", user::user_columns("rp"), user::user_columns("t"), user::user_columns("h")), |
| 52 | (status.map(ReportStatus::as_str),), | 53 | (status.map(ReportStatus::as_str),), |
| 53 | ) | 54 | ) |
| 54 | .await?; | 55 | .await?; |
| @@ -58,15 +59,15 @@ pub async fn list(status: Option<ReportStatus>, conn: &Connection) -> Result<Vec | |||
| 58 | let text: String = row.get(16)?; | 59 | let text: String = row.get(16)?; |
| 59 | reports.push(Report { | 60 | reports.push(Report { |
| 60 | id: row.get(0)?, | 61 | id: row.get(0)?, |
| 61 | - reporter: user_at(&row, 1)?, | 62 | + reporter: user::from_row_at(&row, 1)?, |
| 62 | - target: user_at(&row, 6)?, | 63 | + target: user::from_row_at(&row, 6)?, |
| 63 | community: row.get(11)?, | 64 | community: row.get(11)?, |
| 64 | message_id: row.get(12)?, | 65 | message_id: row.get(12)?, |
| 65 | community_id: row.get(13)?, | 66 | community_id: row.get(13)?, |
| 66 | body: row.get(14)?, | 67 | body: row.get(14)?, |
| 67 | reason: row.get(15)?, | 68 | reason: row.get(15)?, |
| 68 | status: ReportStatus::parse(&text).ok_or(CommonError::Server)?, | 69 | status: ReportStatus::parse(&text).ok_or(CommonError::Server)?, |
| 69 | - handled_by: user_at(&row, 17)?, | 70 | + handled_by: user::from_row_at(&row, 17)?, |
| 70 | }); | 71 | }); |
| 71 | } | 72 | } |
| 72 | 73 | ||
| @@ -81,8 +82,8 @@ pub async fn handle( | |||
| 81 | ) -> Result<Option<String>, Error> { | 82 | ) -> Result<Option<String>, Error> { |
| 82 | let mut rows = conn | 83 | let mut rows = conn |
| 83 | .query( | 84 | .query( |
| 84 | - "UPDATE report SET status = ?2, handled_by = ?3 WHERE id = ?1 AND status = 'open' RETURNING target_id", | 85 | + "UPDATE report SET status = ?2, handled_by = ?3 WHERE id = ?1 AND status = ?4 RETURNING target_id", |
| 85 | - (report_id, status.as_str(), handled_by), | 86 | + (report_id, status.as_str(), handled_by, ReportStatus::Open.as_str()), |
| 86 | ) | 87 | ) |
| 87 | .await?; | 88 | .await?; |
| 88 | let row = rows.next().await?.ok_or(CommonError::NotFound)?; | 89 | let row = rows.next().await?.ok_or(CommonError::NotFound)?; |
crates/server/src/request.rsmodified+28 -28
| @@ -8,7 +8,7 @@ use common::{ | |||
| 8 | error::Error as CommonError, | 8 | error::Error as CommonError, |
| 9 | event::Event, | 9 | event::Event, |
| 10 | model::{ | 10 | model::{ |
| 11 | - AdminAction, CommunityKind, CommunitySummary, LogAction, Member, Rank, ReportStatus, Role, | 11 | + AdminAction, CommunityKind, CommunitySummary, LogAction, Member, Rank, ReportStatus, |
| 12 | ServerBanKind, ServerSettings, User, VoiceRoom, | 12 | ServerBanKind, ServerSettings, User, VoiceRoom, |
| 13 | }, | 13 | }, |
| 14 | permission, | 14 | permission, |
| @@ -138,7 +138,7 @@ async fn dispatch( | |||
| 138 | ), | 138 | ), |
| 139 | Request::UpdateServerSettings { settings } => { | 139 | Request::UpdateServerSettings { settings } => { |
| 140 | let (me, _) = authenticated(state, &conn).await?; | 140 | let (me, _) = authenticated(state, &conn).await?; |
| 141 | - ensure(me.role == Role::Builder)?; | 141 | + ensure(permission::is_builder(me.role))?; |
| 142 | validate_server_settings(&settings)?; | 142 | validate_server_settings(&settings)?; |
| 143 | let previous = server_settings::read(&conn).await?; | 143 | let previous = server_settings::read(&conn).await?; |
| 144 | server_settings::update(&settings, &conn).await?; | 144 | server_settings::update(&settings, &conn).await?; |
| @@ -216,7 +216,7 @@ async fn dispatch( | |||
| 216 | } | 216 | } |
| 217 | Request::CreateUser { username, password } => { | 217 | Request::CreateUser { username, password } => { |
| 218 | let (me, _) = authenticated(state, &conn).await?; | 218 | let (me, _) = authenticated(state, &conn).await?; |
| 219 | - ensure(me.role == Role::Builder)?; | 219 | + ensure(permission::is_builder(me.role))?; |
| 220 | let user = authentication::create_account(&username, &password, &conn).await?; | 220 | let user = authentication::create_account(&username, &password, &conn).await?; |
| 221 | admin_log::record( | 221 | admin_log::record( |
| 222 | Some(&me.id), | 222 | Some(&me.id), |
| @@ -230,7 +230,7 @@ async fn dispatch( | |||
| 230 | } | 230 | } |
| 231 | Request::Users => { | 231 | Request::Users => { |
| 232 | let (me, _) = authenticated(state, &conn).await?; | 232 | let (me, _) = authenticated(state, &conn).await?; |
| 233 | - ensure(me.role == Role::Builder)?; | 233 | + ensure(permission::is_builder(me.role))?; |
| 234 | (Response::Users(user::all(&conn).await?), Vec::new()) | 234 | (Response::Users(user::all(&conn).await?), Vec::new()) |
| 235 | } | 235 | } |
| 236 | Request::AdminLogs { | 236 | Request::AdminLogs { |
| @@ -241,7 +241,7 @@ async fn dispatch( | |||
| 241 | before_ms, | 241 | before_ms, |
| 242 | } => { | 242 | } => { |
| 243 | let (me, _) = authenticated(state, &conn).await?; | 243 | let (me, _) = authenticated(state, &conn).await?; |
| 244 | - ensure(me.role == Role::Builder)?; | 244 | + ensure(permission::is_builder(me.role))?; |
| 245 | ( | 245 | ( |
| 246 | Response::AdminLogs( | 246 | Response::AdminLogs( |
| 247 | admin_log::list( | 247 | admin_log::list( |
| @@ -259,7 +259,7 @@ async fn dispatch( | |||
| 259 | } | 259 | } |
| 260 | Request::DailyStats { days } => { | 260 | Request::DailyStats { days } => { |
| 261 | let (me, _) = authenticated(state, &conn).await?; | 261 | let (me, _) = authenticated(state, &conn).await?; |
| 262 | - ensure(me.role == Role::Builder)?; | 262 | + ensure(permission::is_builder(me.role))?; |
| 263 | ( | 263 | ( |
| 264 | Response::DailyStats(stats::daily(days, &conn).await?), | 264 | Response::DailyStats(stats::daily(days, &conn).await?), |
| 265 | Vec::new(), | 265 | Vec::new(), |
| @@ -267,7 +267,7 @@ async fn dispatch( | |||
| 267 | } | 267 | } |
| 268 | Request::Overview => { | 268 | Request::Overview => { |
| 269 | let (me, _) = authenticated(state, &conn).await?; | 269 | let (me, _) = authenticated(state, &conn).await?; |
| 270 | - ensure(me.role == Role::Builder)?; | 270 | + ensure(permission::is_builder(me.role))?; |
| 271 | ( | 271 | ( |
| 272 | Response::Overview(overview::read(presence, voice, &conn).await?), | 272 | Response::Overview(overview::read(presence, voice, &conn).await?), |
| 273 | Vec::new(), | 273 | Vec::new(), |
| @@ -407,7 +407,7 @@ async fn dispatch( | |||
| 407 | ))?; | 407 | ))?; |
| 408 | let tx = conn.transaction().await?; | 408 | let tx = conn.transaction().await?; |
| 409 | let mut broadcasts = Vec::new(); | 409 | let mut broadcasts = Vec::new(); |
| 410 | - if me.role == Role::Builder && me.id != user_id { | 410 | + if permission::is_builder(me.role) && me.id != user_id { |
| 411 | admin_log::record( | 411 | admin_log::record( |
| 412 | Some(&me.id), | 412 | Some(&me.id), |
| 413 | Some(&user_id), | 413 | Some(&user_id), |
| @@ -514,7 +514,7 @@ async fn dispatch( | |||
| 514 | let owned = | 514 | let owned = |
| 515 | user_community::rank(&me.id, &community_id, &tx).await? == Some(Rank::Owner); | 515 | user_community::rank(&me.id, &community_id, &tx).await? == Some(Rank::Owner); |
| 516 | let deleted = community::delete(&community_id, &me.id, &tx).await?; | 516 | let deleted = community::delete(&community_id, &me.id, &tx).await?; |
| 517 | - if me.role == Role::Builder && !owned { | 517 | + if permission::is_builder(me.role) && !owned { |
| 518 | admin_log::record( | 518 | admin_log::record( |
| 519 | Some(&me.id), | 519 | Some(&me.id), |
| 520 | None, | 520 | None, |
| @@ -765,7 +765,7 @@ async fn dispatch( | |||
| 765 | ) | 765 | ) |
| 766 | .await?; | 766 | .await?; |
| 767 | } | 767 | } |
| 768 | - if me.role == Role::Builder && existing.user_id != me.id { | 768 | + if permission::is_builder(me.role) && existing.user_id != me.id { |
| 769 | let community = community::label(&deleted.community_id, &conn).await?; | 769 | let community = community::label(&deleted.community_id, &conn).await?; |
| 770 | admin_log::record( | 770 | admin_log::record( |
| 771 | Some(&me.id), | 771 | Some(&me.id), |
| @@ -789,7 +789,7 @@ async fn dispatch( | |||
| 789 | actor_rank(&me, &community_id, &conn).await?, | 789 | actor_rank(&me, &community_id, &conn).await?, |
| 790 | ))?; | 790 | ))?; |
| 791 | let visit = before.is_none() | 791 | let visit = before.is_none() |
| 792 | - && me.role == Role::Builder | 792 | + && permission::is_builder(me.role) |
| 793 | && user_community::rank(&me.id, &community_id, &conn) | 793 | && user_community::rank(&me.id, &community_id, &conn) |
| 794 | .await? | 794 | .await? |
| 795 | .is_none() | 795 | .is_none() |
| @@ -993,7 +993,7 @@ async fn dispatch( | |||
| 993 | } | 993 | } |
| 994 | Request::AllCommunities => { | 994 | Request::AllCommunities => { |
| 995 | let (me, _) = authenticated(state, &conn).await?; | 995 | let (me, _) = authenticated(state, &conn).await?; |
| 996 | - ensure(me.role == Role::Builder)?; | 996 | + ensure(permission::is_builder(me.role))?; |
| 997 | let mut summaries = Vec::new(); | 997 | let mut summaries = Vec::new(); |
| 998 | for community in community::groups(&conn).await? { | 998 | for community in community::groups(&conn).await? { |
| 999 | let members = user_community::members(&community.id, &conn).await?; | 999 | let members = user_community::members(&community.id, &conn).await?; |
| @@ -1048,7 +1048,7 @@ async fn dispatch( | |||
| 1048 | &tx, | 1048 | &tx, |
| 1049 | ) | 1049 | ) |
| 1050 | .await?; | 1050 | .await?; |
| 1051 | - if me.role == Role::Builder { | 1051 | + if permission::is_builder(me.role) { |
| 1052 | let community = community::read(&community_id, &me.id, &tx).await?; | 1052 | let community = community::read(&community_id, &me.id, &tx).await?; |
| 1053 | admin_log::record( | 1053 | admin_log::record( |
| 1054 | Some(&me.id), | 1054 | Some(&me.id), |
| @@ -1127,7 +1127,7 @@ async fn dispatch( | |||
| 1127 | } | 1127 | } |
| 1128 | Request::ServerBans => { | 1128 | Request::ServerBans => { |
| 1129 | let (me, _) = authenticated(state, &conn).await?; | 1129 | let (me, _) = authenticated(state, &conn).await?; |
| 1130 | - ensure(me.role == Role::Builder)?; | 1130 | + ensure(permission::is_builder(me.role))?; |
| 1131 | ( | 1131 | ( |
| 1132 | Response::ServerBans(server_ban::list(&conn).await?), | 1132 | Response::ServerBans(server_ban::list(&conn).await?), |
| 1133 | Vec::new(), | 1133 | Vec::new(), |
| @@ -1135,21 +1135,21 @@ async fn dispatch( | |||
| 1135 | } | 1135 | } |
| 1136 | Request::RemoveServerBan { id } => { | 1136 | Request::RemoveServerBan { id } => { |
| 1137 | let (me, _) = authenticated(state, &conn).await?; | 1137 | let (me, _) = authenticated(state, &conn).await?; |
| 1138 | - ensure(me.role == Role::Builder)?; | 1138 | + ensure(permission::is_builder(me.role))?; |
| 1139 | server_ban::remove(&id, &conn).await?; | 1139 | server_ban::remove(&id, &conn).await?; |
| 1140 | admin_log::record(Some(&me.id), None, None, &AdminAction::BanRemoved, &conn).await?; | 1140 | admin_log::record(Some(&me.id), None, None, &AdminAction::BanRemoved, &conn).await?; |
| 1141 | (Response::Done, Vec::new()) | 1141 | (Response::Done, Vec::new()) |
| 1142 | } | 1142 | } |
| 1143 | Request::CreateInviteCode { uses, hours } => { | 1143 | Request::CreateInviteCode { uses, hours } => { |
| 1144 | let (me, _) = authenticated(state, &conn).await?; | 1144 | let (me, _) = authenticated(state, &conn).await?; |
| 1145 | - ensure(me.role == Role::Builder && uses > 0)?; | 1145 | + ensure(permission::is_builder(me.role) && uses > 0)?; |
| 1146 | let code = invite::create(uses, hours, &me, &conn).await?; | 1146 | let code = invite::create(uses, hours, &me, &conn).await?; |
| 1147 | admin_log::record(Some(&me.id), None, None, &AdminAction::InviteCreated, &conn).await?; | 1147 | admin_log::record(Some(&me.id), None, None, &AdminAction::InviteCreated, &conn).await?; |
| 1148 | (Response::InviteCode(code), Vec::new()) | 1148 | (Response::InviteCode(code), Vec::new()) |
| 1149 | } | 1149 | } |
| 1150 | Request::InviteCodes => { | 1150 | Request::InviteCodes => { |
| 1151 | let (me, _) = authenticated(state, &conn).await?; | 1151 | let (me, _) = authenticated(state, &conn).await?; |
| 1152 | - ensure(me.role == Role::Builder)?; | 1152 | + ensure(permission::is_builder(me.role))?; |
| 1153 | ( | 1153 | ( |
| 1154 | Response::InviteCodes(invite::list(&conn).await?), | 1154 | Response::InviteCodes(invite::list(&conn).await?), |
| 1155 | Vec::new(), | 1155 | Vec::new(), |
| @@ -1157,7 +1157,7 @@ async fn dispatch( | |||
| 1157 | } | 1157 | } |
| 1158 | Request::DeleteInviteCode { code } => { | 1158 | Request::DeleteInviteCode { code } => { |
| 1159 | let (me, _) = authenticated(state, &conn).await?; | 1159 | let (me, _) = authenticated(state, &conn).await?; |
| 1160 | - ensure(me.role == Role::Builder)?; | 1160 | + ensure(permission::is_builder(me.role))?; |
| 1161 | invite::delete(&code, &conn).await?; | 1161 | invite::delete(&code, &conn).await?; |
| 1162 | admin_log::record(Some(&me.id), None, None, &AdminAction::InviteDeleted, &conn).await?; | 1162 | admin_log::record(Some(&me.id), None, None, &AdminAction::InviteDeleted, &conn).await?; |
| 1163 | (Response::Done, Vec::new()) | 1163 | (Response::Done, Vec::new()) |
| @@ -1194,7 +1194,7 @@ async fn dispatch( | |||
| 1194 | } | 1194 | } |
| 1195 | Request::Reports { status } => { | 1195 | Request::Reports { status } => { |
| 1196 | let (me, _) = authenticated(state, &conn).await?; | 1196 | let (me, _) = authenticated(state, &conn).await?; |
| 1197 | - ensure(me.role == Role::Builder)?; | 1197 | + ensure(permission::is_builder(me.role))?; |
| 1198 | ( | 1198 | ( |
| 1199 | Response::Reports(report::list(status, &conn).await?), | 1199 | Response::Reports(report::list(status, &conn).await?), |
| 1200 | Vec::new(), | 1200 | Vec::new(), |
| @@ -1202,7 +1202,7 @@ async fn dispatch( | |||
| 1202 | } | 1202 | } |
| 1203 | Request::HandleReport { report_id, status } => { | 1203 | Request::HandleReport { report_id, status } => { |
| 1204 | let (me, _) = authenticated(state, &conn).await?; | 1204 | let (me, _) = authenticated(state, &conn).await?; |
| 1205 | - ensure(me.role == Role::Builder && status != ReportStatus::Open)?; | 1205 | + ensure(permission::is_builder(me.role) && status != ReportStatus::Open)?; |
| 1206 | let target = report::handle(&report_id, status, &me.id, &conn).await?; | 1206 | let target = report::handle(&report_id, status, &me.id, &conn).await?; |
| 1207 | admin_log::record( | 1207 | admin_log::record( |
| 1208 | Some(&me.id), | 1208 | Some(&me.id), |
| @@ -1222,7 +1222,7 @@ async fn dispatch( | |||
| 1222 | before_ms, | 1222 | before_ms, |
| 1223 | } => { | 1223 | } => { |
| 1224 | let (me, _) = authenticated(state, &conn).await?; | 1224 | let (me, _) = authenticated(state, &conn).await?; |
| 1225 | - ensure(me.role == Role::Builder)?; | 1225 | + ensure(permission::is_builder(me.role))?; |
| 1226 | let hits = message::search( | 1226 | let hits = message::search( |
| 1227 | text.as_deref(), | 1227 | text.as_deref(), |
| 1228 | user_id.as_deref(), | 1228 | user_id.as_deref(), |
| @@ -1273,7 +1273,7 @@ async fn dispatch( | |||
| 1273 | community_id, | 1273 | community_id, |
| 1274 | } => { | 1274 | } => { |
| 1275 | let (me, _) = authenticated(state, &conn).await?; | 1275 | let (me, _) = authenticated(state, &conn).await?; |
| 1276 | - ensure(me.role == Role::Builder)?; | 1276 | + ensure(permission::is_builder(me.role))?; |
| 1277 | ( | 1277 | ( |
| 1278 | Response::AttachmentInfos( | 1278 | Response::AttachmentInfos( |
| 1279 | attachment::all(user_id.as_deref(), community_id.as_deref(), &conn).await?, | 1279 | attachment::all(user_id.as_deref(), community_id.as_deref(), &conn).await?, |
| @@ -1283,7 +1283,7 @@ async fn dispatch( | |||
| 1283 | } | 1283 | } |
| 1284 | Request::DeleteAttachment { attachment_id } => { | 1284 | Request::DeleteAttachment { attachment_id } => { |
| 1285 | let (me, _) = authenticated(state, &conn).await?; | 1285 | let (me, _) = authenticated(state, &conn).await?; |
| 1286 | - ensure(me.role == Role::Builder)?; | 1286 | + ensure(permission::is_builder(me.role))?; |
| 1287 | let stored = attachment::read(&attachment_id, &conn).await?; | 1287 | let stored = attachment::read(&attachment_id, &conn).await?; |
| 1288 | let community = community::label(&stored.community_id, &conn).await?; | 1288 | let community = community::label(&stored.community_id, &conn).await?; |
| 1289 | let tx = conn.transaction().await?; | 1289 | let tx = conn.transaction().await?; |
| @@ -1308,13 +1308,13 @@ async fn dispatch( | |||
| 1308 | } | 1308 | } |
| 1309 | Request::StorageUsage => { | 1309 | Request::StorageUsage => { |
| 1310 | let (me, _) = authenticated(state, &conn).await?; | 1310 | let (me, _) = authenticated(state, &conn).await?; |
| 1311 | - ensure(me.role == Role::Builder)?; | 1311 | + ensure(permission::is_builder(me.role))?; |
| 1312 | let (users, communities) = attachment::usage(&conn).await?; | 1312 | let (users, communities) = attachment::usage(&conn).await?; |
| 1313 | (Response::StorageUsage { users, communities }, Vec::new()) | 1313 | (Response::StorageUsage { users, communities }, Vec::new()) |
| 1314 | } | 1314 | } |
| 1315 | Request::VoiceRooms => { | 1315 | Request::VoiceRooms => { |
| 1316 | let (me, _) = authenticated(state, &conn).await?; | 1316 | let (me, _) = authenticated(state, &conn).await?; |
| 1317 | - ensure(me.role == Role::Builder)?; | 1317 | + ensure(permission::is_builder(me.role))?; |
| 1318 | ( | 1318 | ( |
| 1319 | Response::VoiceRooms(voice_rooms(voice, &conn).await?), | 1319 | Response::VoiceRooms(voice_rooms(voice, &conn).await?), |
| 1320 | Vec::new(), | 1320 | Vec::new(), |
| @@ -1376,7 +1376,7 @@ async fn dispatch( | |||
| 1376 | let tx = conn.transaction().await?; | 1376 | let tx = conn.transaction().await?; |
| 1377 | community::set_voice_enabled(&community_id, enabled, &tx).await?; | 1377 | community::set_voice_enabled(&community_id, enabled, &tx).await?; |
| 1378 | let updated = community::read(&community_id, &me.id, &tx).await?; | 1378 | let updated = community::read(&community_id, &me.id, &tx).await?; |
| 1379 | - if me.role == Role::Builder { | 1379 | + if permission::is_builder(me.role) { |
| 1380 | admin_log::record( | 1380 | admin_log::record( |
| 1381 | Some(&me.id), | 1381 | Some(&me.id), |
| 1382 | None, | 1382 | None, |
| @@ -1399,7 +1399,7 @@ async fn dispatch( | |||
| 1399 | } | 1399 | } |
| 1400 | Request::SetGhost { enabled } => { | 1400 | Request::SetGhost { enabled } => { |
| 1401 | let (me, _) = authenticated(state, &conn).await?; | 1401 | let (me, _) = authenticated(state, &conn).await?; |
| 1402 | - ensure(me.role == Role::Builder)?; | 1402 | + ensure(permission::is_builder(me.role))?; |
| 1403 | user::set_ghost(&me.id, enabled, &conn).await?; | 1403 | user::set_ghost(&me.id, enabled, &conn).await?; |
| 1404 | let action = if enabled { | 1404 | let action = if enabled { |
| 1405 | AdminAction::GhostEnabled | 1405 | AdminAction::GhostEnabled |
| @@ -1524,7 +1524,7 @@ fn limit_messages( | |||
| 1524 | rate_limit: &RateLimit, | 1524 | rate_limit: &RateLimit, |
| 1525 | settings: &ServerSettings, | 1525 | settings: &ServerSettings, |
| 1526 | ) -> Result<(), Error> { | 1526 | ) -> Result<(), Error> { |
| 1527 | - if me.role == Role::Builder { | 1527 | + if permission::bypasses_limits(me.role) { |
| 1528 | return Ok(()); | 1528 | return Ok(()); |
| 1529 | } | 1529 | } |
| 1530 | 1530 | ||
crates/server/src/retention.rsmodified+1 -1
| @@ -14,7 +14,7 @@ use crate::{ | |||
| 14 | message, server_settings, | 14 | message, server_settings, |
| 15 | }; | 15 | }; |
| 16 | 16 | ||
| 17 | -const SWEEP_INTERVAL: Duration = Duration::from_secs(3600); | 17 | +const SWEEP_INTERVAL: Duration = Duration::from_millis(time::HOUR_MILLIS); |
| 18 | const BATCH_SIZE: i64 = 500; | 18 | const BATCH_SIZE: i64 = 500; |
| 19 | 19 | ||
| 20 | pub async fn collect(db: DB, directory: PathBuf) { | 20 | pub async fn collect(db: DB, directory: PathBuf) { |
crates/server/src/server_ban.rsmodified+3 -3
| @@ -4,7 +4,7 @@ use common::{ | |||
| 4 | }; | 4 | }; |
| 5 | use turso::Connection; | 5 | use turso::Connection; |
| 6 | 6 | ||
| 7 | -use crate::{database::new_id, error::Error, log::user_at}; | 7 | +use crate::{database::new_id, error::Error, user}; |
| 8 | 8 | ||
| 9 | pub async fn insert( | 9 | pub async fn insert( |
| 10 | kind: ServerBanKind, | 10 | kind: ServerBanKind, |
| @@ -26,7 +26,7 @@ pub async fn insert( | |||
| 26 | pub async fn list(conn: &Connection) -> Result<Vec<ServerBan>, Error> { | 26 | pub async fn list(conn: &Connection) -> Result<Vec<ServerBan>, Error> { |
| 27 | let mut rows = conn | 27 | let mut rows = conn |
| 28 | .query( | 28 | .query( |
| 29 | - "SELECT b.id, b.kind, b.value, b.reason, u.id, u.username, u.role, u.status, u.avatar_id FROM server_ban b LEFT JOIN user u ON u.id = b.created_by ORDER BY b.kind, b.value", | 29 | + &format!("SELECT b.id, b.kind, b.value, b.reason, {} FROM server_ban b LEFT JOIN user u ON u.id = b.created_by ORDER BY b.kind, b.value", user::user_columns("u")), |
| 30 | (), | 30 | (), |
| 31 | ) | 31 | ) |
| 32 | .await?; | 32 | .await?; |
| @@ -39,7 +39,7 @@ pub async fn list(conn: &Connection) -> Result<Vec<ServerBan>, Error> { | |||
| 39 | kind: ServerBanKind::parse(&text).ok_or(CommonError::Server)?, | 39 | kind: ServerBanKind::parse(&text).ok_or(CommonError::Server)?, |
| 40 | value: row.get(2)?, | 40 | value: row.get(2)?, |
| 41 | reason: row.get(3)?, | 41 | reason: row.get(3)?, |
| 42 | - created_by: user_at(&row, 4)?, | 42 | + created_by: user::from_row_at(&row, 4)?, |
| 43 | }); | 43 | }); |
| 44 | } | 44 | } |
| 45 | 45 | ||
crates/server/src/user.rsmodified+33 -23
| @@ -10,26 +10,33 @@ use turso::{Connection, Row}; | |||
| 10 | 10 | ||
| 11 | use crate::{database::new_id, error::Error}; | 11 | use crate::{database::new_id, error::Error}; |
| 12 | 12 | ||
| 13 | -pub fn from_row(row: &Row) -> Result<User, Error> { | 13 | +pub fn user_columns(alias: &str) -> String { |
| 14 | - Ok(User { | 14 | + format!("{alias}.id, {alias}.username, {alias}.role, {alias}.status, {alias}.avatar_id") |
| 15 | - id: row.get(0)?, | ||
| 16 | - username: row.get(1)?, | ||
| 17 | - role: role_of(row)?, | ||
| 18 | - status: row.get(3)?, | ||
| 19 | - avatar_id: row.get(4)?, | ||
| 20 | - }) | ||
| 21 | } | 15 | } |
| 22 | 16 | ||
| 23 | -fn role_of(row: &Row) -> Result<Role, Error> { | 17 | +pub fn from_row_at(row: &Row, start: usize) -> Result<Option<User>, Error> { |
| 24 | - let text: String = row.get(2)?; | 18 | + let Some(id) = row.get::<Option<String>>(start)? else { |
| 19 | + return Ok(None); | ||
| 20 | + }; | ||
| 21 | + let role: String = row.get(start + 2)?; | ||
| 22 | + | ||
| 23 | + Ok(Some(User { | ||
| 24 | + id, | ||
| 25 | + username: row.get(start + 1)?, | ||
| 26 | + role: Role::parse(&role).ok_or(CommonError::Server)?, | ||
| 27 | + status: row.get(start + 3)?, | ||
| 28 | + avatar_id: row.get(start + 4)?, | ||
| 29 | + })) | ||
| 30 | +} | ||
| 25 | 31 | ||
| 26 | - Ok(Role::parse(&text).ok_or(CommonError::Server)?) | 32 | +pub fn from_row(row: &Row) -> Result<User, Error> { |
| 33 | + Ok(from_row_at(row, 0)?.ok_or(CommonError::Server)?) | ||
| 27 | } | 34 | } |
| 28 | 35 | ||
| 29 | pub async fn create(username: &str, password_hash: &str, conn: &Connection) -> Result<User, Error> { | 36 | pub async fn create(username: &str, password_hash: &str, conn: &Connection) -> Result<User, Error> { |
| 30 | let mut rows = conn | 37 | let mut rows = conn |
| 31 | .query( | 38 | .query( |
| 32 | - "INSERT INTO user (id, username, password_hash, role) VALUES (?1, ?2, ?3, CASE WHEN EXISTS (SELECT 1 FROM user) THEN ?4 ELSE ?5 END) RETURNING id, username, role, status, avatar_id", | 39 | + &format!("INSERT INTO user (id, username, password_hash, role) VALUES (?1, ?2, ?3, CASE WHEN EXISTS (SELECT 1 FROM user) THEN ?4 ELSE ?5 END) RETURNING {}", user_columns("user")), |
| 33 | ( | 40 | ( |
| 34 | new_id(), | 41 | new_id(), |
| 35 | username, | 42 | username, |
| @@ -47,7 +54,7 @@ pub async fn create(username: &str, password_hash: &str, conn: &Connection) -> R | |||
| 47 | pub async fn read(user_id: &str, conn: &Connection) -> Result<User, Error> { | 54 | pub async fn read(user_id: &str, conn: &Connection) -> Result<User, Error> { |
| 48 | let mut rows = conn | 55 | let mut rows = conn |
| 49 | .query( | 56 | .query( |
| 50 | - "SELECT id, username, role, status, avatar_id FROM user WHERE id = ?1", | 57 | + &format!("SELECT {} FROM user WHERE id = ?1", user_columns("user")), |
| 51 | (user_id,), | 58 | (user_id,), |
| 52 | ) | 59 | ) |
| 53 | .await?; | 60 | .await?; |
| @@ -70,7 +77,7 @@ pub async fn any(conn: &Connection) -> Result<bool, Error> { | |||
| 70 | pub async fn by_username(username: &str, conn: &Connection) -> Result<Option<User>, Error> { | 77 | pub async fn by_username(username: &str, conn: &Connection) -> Result<Option<User>, Error> { |
| 71 | let mut rows = conn | 78 | let mut rows = conn |
| 72 | .query( | 79 | .query( |
| 73 | - "SELECT id, username, role, status, avatar_id FROM user WHERE username = ?1 COLLATE NOCASE", | 80 | + &format!("SELECT {} FROM user WHERE username = ?1 COLLATE NOCASE", user_columns("user")), |
| 74 | (username,), | 81 | (username,), |
| 75 | ) | 82 | ) |
| 76 | .await?; | 83 | .await?; |
| @@ -84,7 +91,7 @@ pub async fn credentials( | |||
| 84 | ) -> Result<Option<(User, String)>, Error> { | 91 | ) -> Result<Option<(User, String)>, Error> { |
| 85 | let mut rows = conn | 92 | let mut rows = conn |
| 86 | .query( | 93 | .query( |
| 87 | - "SELECT id, username, role, status, avatar_id, password_hash FROM user WHERE username = ?1", | 94 | + &format!("SELECT {}, password_hash FROM user WHERE username = ?1", user_columns("user")), |
| 88 | (username,), | 95 | (username,), |
| 89 | ) | 96 | ) |
| 90 | .await?; | 97 | .await?; |
| @@ -107,7 +114,7 @@ pub async fn update(user_id: &str, username: &str, conn: &Connection) -> Result< | |||
| 107 | 114 | ||
| 108 | let mut rows = conn | 115 | let mut rows = conn |
| 109 | .query( | 116 | .query( |
| 110 | - "UPDATE user SET username = ?2 WHERE id = ?1 RETURNING id, username, role, status, avatar_id", | 117 | + &format!("UPDATE user SET username = ?2 WHERE id = ?1 RETURNING {}", user_columns("user")), |
| 111 | (user_id, username), | 118 | (user_id, username), |
| 112 | ) | 119 | ) |
| 113 | .await?; | 120 | .await?; |
| @@ -121,7 +128,7 @@ pub async fn update_status(user_id: &str, status: &str, conn: &Connection) -> Re | |||
| 121 | 128 | ||
| 122 | let mut rows = conn | 129 | let mut rows = conn |
| 123 | .query( | 130 | .query( |
| 124 | - "UPDATE user SET status = ?2 WHERE id = ?1 RETURNING id, username, role, status, avatar_id", | 131 | + &format!("UPDATE user SET status = ?2 WHERE id = ?1 RETURNING {}", user_columns("user")), |
| 125 | (user_id, status), | 132 | (user_id, status), |
| 126 | ) | 133 | ) |
| 127 | .await?; | 134 | .await?; |
| @@ -179,7 +186,7 @@ async fn set_avatar_id( | |||
| 179 | ) -> Result<User, Error> { | 186 | ) -> Result<User, Error> { |
| 180 | let mut rows = conn | 187 | let mut rows = conn |
| 181 | .query( | 188 | .query( |
| 182 | - "UPDATE user SET avatar_id = ?2 WHERE id = ?1 RETURNING id, username, role, status, avatar_id", | 189 | + &format!("UPDATE user SET avatar_id = ?2 WHERE id = ?1 RETURNING {}", user_columns("user")), |
| 183 | (user_id, avatar_id), | 190 | (user_id, avatar_id), |
| 184 | ) | 191 | ) |
| 185 | .await?; | 192 | .await?; |
| @@ -203,7 +210,7 @@ pub async fn avatar(avatar_id: &str, conn: &Connection) -> Result<Vec<u8>, Error | |||
| 203 | pub async fn set_role(username: &str, role: Role, conn: &Connection) -> Result<User, Error> { | 210 | pub async fn set_role(username: &str, role: Role, conn: &Connection) -> Result<User, Error> { |
| 204 | let mut rows = conn | 211 | let mut rows = conn |
| 205 | .query( | 212 | .query( |
| 206 | - "UPDATE user SET role = ?2 WHERE username = ?1 RETURNING id, username, role, status, avatar_id", | 213 | + &format!("UPDATE user SET role = ?2 WHERE username = ?1 RETURNING {}", user_columns("user")), |
| 207 | (username, role.as_str()), | 214 | (username, role.as_str()), |
| 208 | ) | 215 | ) |
| 209 | .await?; | 216 | .await?; |
| @@ -215,7 +222,7 @@ pub async fn set_role(username: &str, role: Role, conn: &Connection) -> Result<U | |||
| 215 | pub async fn set_role_by_id(user_id: &str, role: Role, conn: &Connection) -> Result<User, Error> { | 222 | pub async fn set_role_by_id(user_id: &str, role: Role, conn: &Connection) -> Result<User, Error> { |
| 216 | let mut rows = conn | 223 | let mut rows = conn |
| 217 | .query( | 224 | .query( |
| 218 | - "UPDATE user SET role = ?2 WHERE id = ?1 RETURNING id, username, role, status, avatar_id", | 225 | + &format!("UPDATE user SET role = ?2 WHERE id = ?1 RETURNING {}", user_columns("user")), |
| 219 | (user_id, role.as_str()), | 226 | (user_id, role.as_str()), |
| 220 | ) | 227 | ) |
| 221 | .await?; | 228 | .await?; |
| @@ -270,7 +277,10 @@ pub async fn last_ip(user_id: &str, conn: &Connection) -> Result<Option<String>, | |||
| 270 | 277 | ||
| 271 | pub async fn builder_ids(conn: &Connection) -> Result<Vec<String>, Error> { | 278 | pub async fn builder_ids(conn: &Connection) -> Result<Vec<String>, Error> { |
| 272 | let mut rows = conn | 279 | let mut rows = conn |
| 273 | - .query("SELECT id FROM user WHERE role = 'builder'", ()) | 280 | + .query( |
| 281 | + "SELECT id FROM user WHERE role = ?1", | ||
| 282 | + (Role::Builder.as_str(),), | ||
| 283 | + ) | ||
| 274 | .await?; | 284 | .await?; |
| 275 | let mut ids = Vec::new(); | 285 | let mut ids = Vec::new(); |
| 276 | while let Some(row) = rows.next().await? { | 286 | while let Some(row) = rows.next().await? { |
| @@ -349,7 +359,7 @@ pub async fn set_password_hash( | |||
| 349 | pub async fn all(conn: &Connection) -> Result<Vec<Account>, Error> { | 359 | pub async fn all(conn: &Connection) -> Result<Vec<Account>, Error> { |
| 350 | let mut rows = conn | 360 | let mut rows = conn |
| 351 | .query( | 361 | .query( |
| 352 | - "SELECT id, username, role, status, avatar_id, suspended, last_seen, suspended_until, ghost FROM user ORDER BY username COLLATE NOCASE", | 362 | + &format!("SELECT {}, suspended, last_seen, suspended_until, ghost FROM user ORDER BY username COLLATE NOCASE", user_columns("user")), |
| 353 | (), | 363 | (), |
| 354 | ) | 364 | ) |
| 355 | .await?; | 365 | .await?; |
| @@ -374,7 +384,7 @@ pub async fn all(conn: &Connection) -> Result<Vec<Account>, Error> { | |||
| 374 | pub async fn delete(user_id: &str, conn: &Connection) -> Result<User, Error> { | 384 | pub async fn delete(user_id: &str, conn: &Connection) -> Result<User, Error> { |
| 375 | let mut rows = conn | 385 | let mut rows = conn |
| 376 | .query( | 386 | .query( |
| 377 | - "DELETE FROM user WHERE id = ?1 RETURNING id, username, role, status, avatar_id", | 387 | + &format!("DELETE FROM user WHERE id = ?1 RETURNING {}", user_columns("user")), |
| 378 | (user_id,), | 388 | (user_id,), |
| 379 | ) | 389 | ) |
| 380 | .await?; | 390 | .await?; |
crates/server/src/user_community.rsmodified+2 -2
| @@ -74,7 +74,7 @@ pub async fn set_rank( | |||
| 74 | pub async fn members(community_id: &str, conn: &Connection) -> Result<Vec<Member>, Error> { | 74 | pub async fn members(community_id: &str, conn: &Connection) -> Result<Vec<Member>, Error> { |
| 75 | let mut rows = conn | 75 | let mut rows = conn |
| 76 | .query( | 76 | .query( |
| 77 | - "SELECT user.id, user.username, user.role, user.status, user.avatar_id, user_community.rank FROM user JOIN user_community ON user_community.user_id = user.id WHERE user_community.community_id = ?1 ORDER BY user.id", | 77 | + &format!("SELECT {}, user_community.rank FROM user JOIN user_community ON user_community.user_id = user.id WHERE user_community.community_id = ?1 ORDER BY user.id", user::user_columns("user")), |
| 78 | (community_id,), | 78 | (community_id,), |
| 79 | ) | 79 | ) |
| 80 | .await?; | 80 | .await?; |
| @@ -151,7 +151,7 @@ pub async fn is_banned( | |||
| 151 | pub async fn bans(community_id: &str, conn: &Connection) -> Result<Vec<Ban>, Error> { | 151 | pub async fn bans(community_id: &str, conn: &Connection) -> Result<Vec<Ban>, Error> { |
| 152 | let mut rows = conn | 152 | let mut rows = conn |
| 153 | .query( | 153 | .query( |
| 154 | - "SELECT user.id, user.username, user.role, user.status, user.avatar_id, ban.reason FROM user JOIN ban ON ban.user_id = user.id WHERE ban.community_id = ?1 ORDER BY ban.rowid DESC", | 154 | + &format!("SELECT {}, ban.reason FROM user JOIN ban ON ban.user_id = user.id WHERE ban.community_id = ?1 ORDER BY ban.rowid DESC", user::user_columns("user")), |
| 155 | (community_id,), | 155 | (community_id,), |
| 156 | ) | 156 | ) |
| 157 | .await?; | 157 | .await?; |