use common::{ call::ListAdminLogs, model::{AdminAction, AdminActionKind, AdminLogEntry}, time::now_ms, }; use gpui_kit::{ AnyElement, Context, Div, Entity, IntoElement, ParentElement, Styled, Window, assets::IconName, component::{WindowExt, notification::Notification, select::Select}, div, px, }; use jiff::Timestamp; use super::{ save_directory, ui::{ caption_text, card, end_of_day_ms, fixed, list_row, pill, rows_body, start_of_day_ms, toolbar, }, }; use crate::gui::{ Yumush, community_settings::user_name, entity_action, millis_time, notify_error, style::{Tokens, size, space, text}, text, widget::{field, search_text}, }; use crate::lang::{Label as _, w}; fn builder_log_text(entry: &AdminLogEntry) -> String { let actor = user_name(&entry.actor); let target = user_name(&entry.target); let community = entry.community.as_deref().unwrap_or_default(); let words = &w().builder_log; match &entry.action { AdminAction::RoleChanged { role } => words.role_changed(actor, role.label(), target), AdminAction::UserDeleted => words.user_deleted(actor, target), AdminAction::UserSuspended => words.user_suspended(actor, target), AdminAction::UserUnsuspended => words.user_unsuspended(actor, target), AdminAction::SessionsRevoked => words.sessions_revoked(actor, target), AdminAction::PasswordReset => words.password_reset(actor, target), AdminAction::ProfileCleared => words.profile_cleared(actor, target), AdminAction::UserCreated => words.user_created(actor, target), AdminAction::CommunityDeleted => words.community_deleted(actor, community), AdminAction::OwnershipTransferred => words.ownership_transferred(actor, community, target), AdminAction::SettingsChanged => words.settings_changed(actor), AdminAction::UserRenamed { from } => words.user_renamed(actor, from, target), AdminAction::UserTimedOut { minutes: 0 } => words.user_timeout_cleared(actor, target), AdminAction::UserTimedOut { minutes } => words.user_timed_out(actor, minutes, target), AdminAction::UserBanned { .. } => words.user_banned(actor, target), AdminAction::BanRemoved => words.ban_removed(actor), AdminAction::InviteCreated => words.invite_created(actor), AdminAction::InviteDeleted => words.invite_deleted(actor), AdminAction::ReportHandled { status } => { words.report_handled(actor, status.label(), target) } AdminAction::MessageDeleted => words.message_deleted(actor, community, target), AdminAction::UserPurged { messages } => words.user_purged(actor, messages, target), AdminAction::VoiceKicked => words.voice_kicked(actor, target), AdminAction::VoiceMuted { muted: true } => words.voice_muted(actor, target), AdminAction::VoiceMuted { muted: false } => words.voice_unmuted(actor, target), AdminAction::CommunityVoiceSet { enabled: true } => { words.community_voice_enabled(actor, community) } AdminAction::CommunityVoiceSet { enabled: false } => { words.community_voice_disabled(actor, community) } AdminAction::Community(action) => { crate::gui::community_settings::community_log_text(actor, target, action) } AdminAction::AttachmentDeleted => words.attachment_deleted(actor, community, target), AdminAction::GhostEnabled => words.ghost_enabled(actor), AdminAction::GhostDisabled => words.ghost_disabled(actor), AdminAction::GhostVisit => words.ghost_visit(actor, community), AdminAction::GhostVoice { seconds } => words.ghost_voice(actor, community, seconds), AdminAction::BackupCreated { name } => words.backup_created(actor, name), AdminAction::BackupDeleted { name } => words.backup_deleted(actor, name), AdminAction::BackupDownloaded { name } => words.backup_downloaded(actor, name), AdminAction::ReleasePublished { seq, required: true, } => words.release_published_required(actor, seq), AdminAction::ReleasePublished { seq, required: false, } => words.release_published(actor, seq), AdminAction::ReleaseRequired { seq, required: true, } => words.release_required(actor, seq), AdminAction::ReleaseRequired { seq, required: false, } => words.release_optional(actor, seq), } } fn action_detail(action: &AdminAction) -> Option { match action { AdminAction::RoleChanged { role } => Some(role.as_str().to_string()), AdminAction::UserRenamed { from } => Some(from.clone()), AdminAction::UserTimedOut { minutes } => Some(minutes.to_string()), AdminAction::UserBanned { reason } => reason.clone(), AdminAction::ReportHandled { status } => Some(status.as_str().to_string()), AdminAction::UserPurged { messages } => Some(messages.to_string()), AdminAction::VoiceMuted { muted } => Some(muted.to_string()), AdminAction::CommunityVoiceSet { enabled } => Some(enabled.to_string()), AdminAction::GhostVoice { seconds } => Some(seconds.to_string()), AdminAction::BackupCreated { name } | AdminAction::BackupDeleted { name } | AdminAction::BackupDownloaded { name } => Some(name.clone()), _ => None, } } fn csv_field(text: &str) -> String { let guarded = if text.starts_with(['=', '+', '-', '@', '\t', '\r']) { format!("'{text}") } else { text.to_string() }; format!("\"{}\"", guarded.replace('"', "\"\"")) } fn builder_log_csv(entries: &[AdminLogEntry]) -> String { let mut lines = vec!["time,actor,action,target,community,detail".to_string()]; for entry in entries { let time = Timestamp::from_millisecond(entry.created_at as i64) .ok() .map(|timestamp| timestamp.to_string()) .unwrap_or_default(); let actor = entry .actor .as_ref() .map_or_else(String::new, |user| user.username.clone()); let target = entry .target .as_ref() .map_or_else(String::new, |user| user.username.clone()); let fields = [ time, actor, entry.action.kind().as_str().to_string(), target, entry.community.clone().unwrap_or_default(), action_detail(&entry.action).unwrap_or_default(), ]; lines.push( fields .iter() .map(|field| csv_field(field)) .collect::>() .join(","), ); } lines.join("\n") } fn action_icon(action: &AdminAction) -> IconName { match action { AdminAction::UserDeleted | AdminAction::CommunityDeleted | AdminAction::MessageDeleted | AdminAction::AttachmentDeleted | AdminAction::UserPurged { .. } => IconName::Trash, AdminAction::UserBanned { .. } | AdminAction::BanRemoved | AdminAction::UserSuspended | AdminAction::UserUnsuspended | AdminAction::UserTimedOut { .. } => IconName::Ban, AdminAction::RoleChanged { .. } | AdminAction::OwnershipTransferred | AdminAction::UserRenamed { .. } | AdminAction::ProfileCleared | AdminAction::PasswordReset | AdminAction::SessionsRevoked => IconName::UserCog, AdminAction::UserCreated => IconName::UserPlus, AdminAction::Community(_) => IconName::Users, AdminAction::VoiceKicked | AdminAction::VoiceMuted { .. } => IconName::MicOff, AdminAction::CommunityVoiceSet { .. } => IconName::Mic, AdminAction::InviteCreated | AdminAction::InviteDeleted => IconName::Key, AdminAction::ReportHandled { .. } => IconName::Flag, AdminAction::SettingsChanged => IconName::Settings, AdminAction::GhostEnabled | AdminAction::GhostDisabled | AdminAction::GhostVisit | AdminAction::GhostVoice { .. } => IconName::Eye, AdminAction::BackupCreated { .. } | AdminAction::BackupDeleted { .. } | AdminAction::BackupDownloaded { .. } | AdminAction::ReleasePublished { .. } | AdminAction::ReleaseRequired { .. } => IconName::HardDrive, } } fn builder_log_row(entry: &AdminLogEntry, t: Tokens) -> Div { list_row() .child(crate::gui::parts::small_icon(action_icon(&entry.action)).text_color(t.dim())) .child( div() .flex_1() .min_w_0() .flex() .flex_col() .gap(px(space::XXS)) .child( div() .truncate() .text_size(text(text::LABEL)) .text_color(t.paper()) .child(builder_log_text(entry)), ) .child(caption_text( millis_time(entry.created_at, crate::lang::w().common.stamp), t.dim(), )), ) .children( entry .community .as_deref() .map(|community| pill(community, t.paper())), ) } impl Yumush { pub(super) fn load_builder_logs(&self, window: &mut Window, cx: &mut Context) { let (Some(actor_id), Some(target_id)) = ( self.filter_user(&self.chat.admin.builder_log_actor, cx), self.filter_user(&self.chat.admin.builder_log_target, cx), ) else { window.push_notification(Notification::error(w().builder_log.unknown_user), cx); return; }; let action = self .chat .admin .builder_log_action .read(cx) .selected_value() .copied() .and_then(AdminActionKind::parse); let after_ms = start_of_day_ms(&search_text(&self.chat.admin.builder_log_from, cx)); let before_ms = end_of_day_ms(&search_text(&self.chat.admin.builder_log_to, cx)); self.fetch_builder( window, cx, move |network| async move { network .call(ListAdminLogs { actor_id, target_id, action, after_ms, before_ms, }) .await }, |yumush, entries, cx| { yumush.chat.admin.builder_logs = entries; cx.notify(); }, ); } pub(super) fn export_builder_log(&self, window: &mut Window, cx: &mut Context) { let name = format!( "yumush-admin-log-{}.csv", millis_time(now_ms(), "%Y%m%d-%H%M%S") ); let target = cx.prompt_for_new_path(&save_directory(), Some(&name)); let csv = builder_log_csv(&self.chat.admin.builder_logs); cx.spawn_in(window, async move |this, cx| { let target = match target.await { Ok(Ok(Some(target))) => target, Ok(Err(error)) => { notify_error(&this, cx, error); return; } _ => return, }; if let Err(error) = std::fs::write(target, csv) { notify_error(&this, cx, error); } }) .detach(); } pub(super) fn log_body(&self, entity: &Entity, t: Tokens) -> Vec { let filters = toolbar(vec![ fixed(size::THUMB, field(t, &self.chat.admin.builder_log_actor)), fixed(size::THUMB, field(t, &self.chat.admin.builder_log_target)), fixed( size::THUMB, crate::gui::widget::underline( t, size::CONTROL, Select::new(&self.chat.admin.builder_log_action).appearance(false), ), ), fixed(size::THUMB, field(t, &self.chat.admin.builder_log_from)), fixed(size::THUMB, field(t, &self.chat.admin.builder_log_to)), crate::gui::parts::button( "apply_builder_log", w().builder_log.apply, crate::gui::parts::Press::Plain, t, entity_action(entity, |yumush, window, cx| { yumush.load_builder_logs(window, cx) }), ) .into_any_element(), ]); let rows: Vec
= self .chat .admin .builder_logs .iter() .map(|entry| builder_log_row(entry, t)) .collect(); vec![ card() .child(div().pb(px(space::SM)).child(filters)) .child(rows_body(rows, w().builder_log.no_entries, t)) .into_any_element(), ] } }