forge
Explore
Log in

Don't you dare go Hollow

yumush

Clone
★ 0

0 watching0 forksRustAGPL-3.07.2 MiB4b67e80Tahinli1 hour ago400 commits

log.rs333 lines, 13.0 KiBHistoryRawDownload
1use common::{
2 call::ListAdminLogs,
3 model::{AdminAction, AdminActionKind, AdminLogEntry},
4 time::now_ms,
5};
6use gpui_kit::{
7 AnyElement, Context, Div, Entity, IntoElement, ParentElement, Styled, Window,
8 assets::IconName,
9 component::{WindowExt, notification::Notification, select::Select},
10 div, px,
11};
12use jiff::Timestamp;
13
14use super::{
15 save_directory,
16 ui::{card, end_of_day_ms, fixed, list_row, pill, start_of_day_ms, toolbar},
17};
18use crate::gui::{
19 Palette, Yumush,
20 community_settings::user_name,
21 entity_action, millis_time, notify_error,
22 style::{size, space, text},
23 text,
24 widget::{field, search_text},
25};
26use crate::lang::{Label as _, w};
27
28fn builder_log_text(entry: &AdminLogEntry) -> String {
29 let actor = user_name(&entry.actor);
30 let target = user_name(&entry.target);
31 let community = entry.community.as_deref().unwrap_or_default();
32 let words = &w().builder_log;
33
34 match &entry.action {
35 AdminAction::RoleChanged { role } => words.role_changed(actor, role.label(), target),
36 AdminAction::UserDeleted => words.user_deleted(actor, target),
37 AdminAction::UserSuspended => words.user_suspended(actor, target),
38 AdminAction::UserUnsuspended => words.user_unsuspended(actor, target),
39 AdminAction::SessionsRevoked => words.sessions_revoked(actor, target),
40 AdminAction::PasswordReset => words.password_reset(actor, target),
41 AdminAction::ProfileCleared => words.profile_cleared(actor, target),
42 AdminAction::UserCreated => words.user_created(actor, target),
43 AdminAction::CommunityDeleted => words.community_deleted(actor, community),
44 AdminAction::OwnershipTransferred => words.ownership_transferred(actor, community, target),
45 AdminAction::SettingsChanged => words.settings_changed(actor),
46 AdminAction::UserRenamed { from } => words.user_renamed(actor, from, target),
47 AdminAction::UserTimedOut { minutes: 0 } => words.user_timeout_cleared(actor, target),
48 AdminAction::UserTimedOut { minutes } => words.user_timed_out(actor, minutes, target),
49 AdminAction::UserBanned { .. } => words.user_banned(actor, target),
50 AdminAction::BanRemoved => words.ban_removed(actor),
51 AdminAction::InviteCreated => words.invite_created(actor),
52 AdminAction::InviteDeleted => words.invite_deleted(actor),
53 AdminAction::ReportHandled { status } => {
54 words.report_handled(actor, status.label(), target)
55 }
56 AdminAction::MessageDeleted => words.message_deleted(actor, community, target),
57 AdminAction::UserPurged { messages } => words.user_purged(actor, messages, target),
58 AdminAction::VoiceKicked => words.voice_kicked(actor, target),
59 AdminAction::VoiceMuted { muted: true } => words.voice_muted(actor, target),
60 AdminAction::VoiceMuted { muted: false } => words.voice_unmuted(actor, target),
61 AdminAction::CommunityVoiceSet { enabled: true } => {
62 words.community_voice_enabled(actor, community)
63 }
64 AdminAction::CommunityVoiceSet { enabled: false } => {
65 words.community_voice_disabled(actor, community)
66 }
67 AdminAction::Community(action) => {
68 crate::gui::community_settings::community_log_text(actor, target, action)
69 }
70 AdminAction::AttachmentDeleted => words.attachment_deleted(actor, community, target),
71 AdminAction::GhostEnabled => words.ghost_enabled(actor),
72 AdminAction::GhostDisabled => words.ghost_disabled(actor),
73 AdminAction::GhostVisit => words.ghost_visit(actor, community),
74 AdminAction::GhostVoice { seconds } => words.ghost_voice(actor, community, seconds),
75 AdminAction::BackupCreated { name } => words.backup_created(actor, name),
76 AdminAction::BackupDeleted { name } => words.backup_deleted(actor, name),
77 AdminAction::BackupDownloaded { name } => words.backup_downloaded(actor, name),
78 AdminAction::ReleasePublished {
79 seq,
80 required: true,
81 } => words.release_published_required(actor, seq),
82 AdminAction::ReleasePublished {
83 seq,
84 required: false,
85 } => words.release_published(actor, seq),
86 AdminAction::ReleaseRequired {
87 seq,
88 required: true,
89 } => words.release_required(actor, seq),
90 AdminAction::ReleaseRequired {
91 seq,
92 required: false,
93 } => words.release_optional(actor, seq),
94 }
95}
96
97fn action_detail(action: &AdminAction) -> Option<String> {
98 match action {
99 AdminAction::RoleChanged { role } => Some(role.as_str().to_string()),
100 AdminAction::UserRenamed { from } => Some(from.clone()),
101 AdminAction::UserTimedOut { minutes } => Some(minutes.to_string()),
102 AdminAction::UserBanned { reason } => reason.clone(),
103 AdminAction::ReportHandled { status } => Some(status.as_str().to_string()),
104 AdminAction::UserPurged { messages } => Some(messages.to_string()),
105 AdminAction::VoiceMuted { muted } => Some(muted.to_string()),
106 AdminAction::CommunityVoiceSet { enabled } => Some(enabled.to_string()),
107 AdminAction::GhostVoice { seconds } => Some(seconds.to_string()),
108 AdminAction::BackupCreated { name }
109 | AdminAction::BackupDeleted { name }
110 | AdminAction::BackupDownloaded { name } => Some(name.clone()),
111 _ => None,
112 }
113}
114
115fn csv_field(text: &str) -> String {
116 format!("\"{}\"", text.replace('"', "\"\""))
117}
118
119fn builder_log_csv(entries: &[AdminLogEntry]) -> String {
120 let mut lines = vec!["time,actor,action,target,community,detail".to_string()];
121 for entry in entries {
122 let time = Timestamp::from_millisecond(entry.created_at as i64)
123 .ok()
124 .map(|timestamp| timestamp.to_string())
125 .unwrap_or_default();
126 let actor = entry
127 .actor
128 .as_ref()
129 .map_or_else(String::new, |user| user.username.clone());
130 let target = entry
131 .target
132 .as_ref()
133 .map_or_else(String::new, |user| user.username.clone());
134 let fields = [
135 time,
136 actor,
137 entry.action.kind().as_str().to_string(),
138 target,
139 entry.community.clone().unwrap_or_default(),
140 action_detail(&entry.action).unwrap_or_default(),
141 ];
142 lines.push(
143 fields
144 .iter()
145 .map(|field| csv_field(field))
146 .collect::<Vec<_>>()
147 .join(","),
148 );
149 }
150
151 lines.join("\n")
152}
153
154fn action_icon(action: &AdminAction) -> IconName {
155 match action {
156 AdminAction::UserDeleted
157 | AdminAction::CommunityDeleted
158 | AdminAction::MessageDeleted
159 | AdminAction::AttachmentDeleted
160 | AdminAction::UserPurged { .. } => IconName::Trash,
161 AdminAction::UserBanned { .. }
162 | AdminAction::BanRemoved
163 | AdminAction::UserSuspended
164 | AdminAction::UserUnsuspended
165 | AdminAction::UserTimedOut { .. } => IconName::Ban,
166 AdminAction::RoleChanged { .. }
167 | AdminAction::OwnershipTransferred
168 | AdminAction::UserRenamed { .. }
169 | AdminAction::ProfileCleared
170 | AdminAction::PasswordReset
171 | AdminAction::SessionsRevoked => IconName::UserCog,
172 AdminAction::UserCreated => IconName::UserPlus,
173 AdminAction::Community(_) => IconName::Users,
174 AdminAction::VoiceKicked | AdminAction::VoiceMuted { .. } => IconName::MicOff,
175 AdminAction::CommunityVoiceSet { .. } => IconName::Mic,
176 AdminAction::InviteCreated | AdminAction::InviteDeleted => IconName::Key,
177 AdminAction::ReportHandled { .. } => IconName::Flag,
178 AdminAction::SettingsChanged => IconName::Settings,
179 AdminAction::GhostEnabled
180 | AdminAction::GhostDisabled
181 | AdminAction::GhostVisit
182 | AdminAction::GhostVoice { .. } => IconName::Eye,
183 AdminAction::BackupCreated { .. }
184 | AdminAction::BackupDeleted { .. }
185 | AdminAction::BackupDownloaded { .. }
186 | AdminAction::ReleasePublished { .. }
187 | AdminAction::ReleaseRequired { .. } => IconName::HardDrive,
188 }
189}
190
191fn builder_log_row(entry: &AdminLogEntry, palette: Palette) -> Div {
192 list_row()
193 .child(crate::gui::parts::small_icon(action_icon(&entry.action)).text_color(palette.muted))
194 .child(
195 div()
196 .flex_1()
197 .min_w_0()
198 .flex()
199 .flex_col()
200 .gap(px(space::XXS))
201 .child(
202 div()
203 .truncate()
204 .text_size(text(text::LABEL))
205 .text_color(palette.text)
206 .child(builder_log_text(entry)),
207 )
208 .child(
209 div()
210 .text_size(text(text::CAPTION))
211 .text_color(palette.muted)
212 .child(millis_time(entry.created_at, crate::lang::w().common.stamp)),
213 ),
214 )
215 .children(
216 entry
217 .community
218 .as_deref()
219 .map(|community| pill(community, palette.accent, palette)),
220 )
221}
222
223impl Yumush {
224 pub(super) fn load_builder_logs(&self, window: &mut Window, cx: &mut Context<Self>) {
225 if !self.is_builder() {
226 return;
227 }
228 let (Some(actor_id), Some(target_id)) = (
229 self.filter_user(&self.chat.builder_log_actor, cx),
230 self.filter_user(&self.chat.builder_log_target, cx),
231 ) else {
232 window.push_notification(Notification::error(w().builder_log.unknown_user), cx);
233 return;
234 };
235 let action = self
236 .chat
237 .builder_log_action
238 .read(cx)
239 .selected_value()
240 .copied()
241 .filter(|name| *name != "All")
242 .and_then(AdminActionKind::parse);
243 let after_ms = start_of_day_ms(&search_text(&self.chat.builder_log_from, cx));
244 let before_ms = end_of_day_ms(&search_text(&self.chat.builder_log_to, cx));
245 self.fetch_builder(
246 window,
247 cx,
248 move |network| async move {
249 network
250 .call(ListAdminLogs {
251 actor_id,
252 target_id,
253 action,
254 after_ms,
255 before_ms,
256 })
257 .await
258 },
259 |yumush, entries, cx| {
260 yumush.chat.builder_logs = entries;
261 cx.notify();
262 },
263 );
264 }
265
266 pub(super) fn export_builder_log(&self, window: &mut Window, cx: &mut Context<Self>) {
267 let name = format!(
268 "yumush-admin-log-{}.csv",
269 millis_time(now_ms(), "%Y%m%d-%H%M%S")
270 );
271 let target = cx.prompt_for_new_path(&save_directory(), Some(&name));
272 let csv = builder_log_csv(&self.chat.builder_logs);
273
274 cx.spawn_in(window, async move |this, cx| {
275 let target = match target.await {
276 Ok(Ok(Some(target))) => target,
277 Ok(Err(error)) => {
278 notify_error(&this, cx, error);
279 return;
280 }
281 _ => return,
282 };
283 if let Err(error) = std::fs::write(target, csv) {
284 notify_error(&this, cx, error);
285 }
286 })
287 .detach();
288 }
289
290 pub(super) fn log_body(&self, entity: &Entity<Self>, palette: Palette) -> Vec<AnyElement> {
291 let filters = toolbar(vec![
292 fixed(size::THUMB, field(palette, &self.chat.builder_log_actor)),
293 fixed(size::THUMB, field(palette, &self.chat.builder_log_target)),
294 fixed(
295 size::THUMB,
296 crate::gui::widget::underline(
297 palette,
298 Select::new(&self.chat.builder_log_action).appearance(false),
299 ),
300 ),
301 fixed(size::THUMB, field(palette, &self.chat.builder_log_from)),
302 fixed(size::THUMB, field(palette, &self.chat.builder_log_to)),
303 crate::gui::parts::button(
304 "apply_builder_log",
305 w().builder_log.apply,
306 crate::gui::parts::Press::Plain,
307 palette.tokens,
308 entity_action(entity, |yumush, window, cx| {
309 yumush.load_builder_logs(window, cx)
310 }),
311 )
312 .into_any_element(),
313 ]);
314 let rows: Vec<Div> = self
315 .chat
316 .builder_logs
317 .iter()
318 .map(|entry| builder_log_row(entry, palette))
319 .collect();
320 let body = if rows.is_empty() {
321 crate::gui::parts::empty(w().builder_log.no_entries, palette.tokens)
322 } else {
323 div().flex().flex_col().children(rows)
324 };
325
326 vec![
327 card(palette)
328 .child(div().pb(px(space::SM)).child(filters))
329 .child(body)
330 .into_any_element(),
331 ]
332 }
333}