use crate::lang::{Label as _, w}; use common::model::Id; use std::future::Future; use common::{ call::{BanUser, DeleteMessage, HandleReport, ListReports, TimeoutUser}, model::{BanScope, Hue, Report, ReportStatus}, }; use gpui_kit::{ AnyElement, Context, Div, Entity, FontWeight, Hsla, IntoElement, ParentElement, Styled, Window, div, px, }; use super::ui::{caption_text, confirmed, list_card, pill}; use crate::{ gui::{ Yumush, avatar, badge, clickable, community_settings::user_name, entity_action, glyph, millis_time, notify_error, parts::{self, Press}, profile::user_link, settings_page::{segment, segment_track}, style::{Tokens, radius, size, space, text}, text as text_fn, }, net::{Fail, NetworkHandle}, }; const REPORT_FILTERS: [Option; 4] = [ Some(ReportStatus::Open), Some(ReportStatus::Resolved), Some(ReportStatus::Dismissed), None, ]; #[derive(Clone, Copy, PartialEq, Eq)] pub(super) enum Punish { Timeout(Id), Ban(Id), } fn status_color(status: ReportStatus, t: Tokens) -> Hsla { match status { ReportStatus::Open => t.paper(), ReportStatus::Resolved => t.hue(Hue::Mint), ReportStatus::Dismissed => t.dim(), } } impl Yumush { pub(super) fn load_open_reports(&self, window: &mut Window, cx: &mut Context) { self.fetch_builder( window, cx, |network| async move { network .call(ListReports { status: Some(ReportStatus::Open), }) .await .map(|reports| reports.len()) }, |yumush, count, cx| { yumush.chat.admin.open_reports = count; cx.notify(); }, ); } pub(super) fn load_reports(&self, window: &mut Window, cx: &mut Context) { let status = self.chat.admin.report_filter; self.fetch_builder( window, cx, move |network| async move { network.call(ListReports { status }).await }, |yumush, reports, cx| { yumush.chat.admin.builder_reports = reports; cx.notify(); }, ); } fn refresh_reports(&self, window: &mut Window, cx: &mut Context) { self.load_reports(window, cx); self.load_open_reports(window, cx); } fn select_report_filter( &mut self, status: Option, window: &mut Window, cx: &mut Context, ) { self.chat.admin.report_filter = status; self.load_reports(window, cx); cx.notify(); } fn report_action( &mut self, window: &mut Window, cx: &mut Context, action: impl FnOnce(NetworkHandle) -> Fut + 'static, ) where Fut: Future> + 'static, { let network = self.network.clone(); cx.spawn_in(window, async move |this, cx| match action(network).await { Ok(()) => { let _ = this.update_in(cx, |yumush, window, cx| yumush.refresh_reports(window, cx)); } Err(error) => notify_error(&this, cx, error), }) .detach(); } fn handle_report( &mut self, report_id: Id, status: ReportStatus, window: &mut Window, cx: &mut Context, ) { self.report_action(window, cx, move |network| async move { network.call(HandleReport { report_id, status }).await }); } fn delete_report_message( &mut self, message_id: Id, window: &mut Window, cx: &mut Context, ) { self.report_action(window, cx, move |network| async move { network.call(DeleteMessage { message_id }).await }); } fn punish_report( &mut self, punish: Punish, reason: &str, window: &mut Window, cx: &mut Context, ) { if !confirmed(&mut self.desk.punish, punish) { cx.notify(); return; } match punish { Punish::Timeout(user_id) => self.report_action(window, cx, move |network| async move { network .call(TimeoutUser { user_id, minutes: 60, }) .await }), Punish::Ban(user_id) => { let reason = reason.to_string(); self.report_action(window, cx, move |network| async move { network .call(BanUser { user_id, reason: Some(reason), scope: BanScope::Account, }) .await }); } } } fn report_filters(&self, entity: &Entity, t: Tokens) -> Div { div() .flex() .child(segment_track(t).children(REPORT_FILTERS.map(|status| { let label = status.map_or(w().builder_reports.all, ReportStatus::label); segment( clickable( label, t, entity_action(entity, move |yumush, window, cx| { yumush.select_report_filter(status, window, cx) }), ), self.chat.admin.report_filter == status, t, ) .px(px(space::MD)) .child(label) }))) } pub(super) fn reports_body(&self, entity: &Entity, t: Tokens) -> Vec { let rows: Vec
= self .chat .admin .builder_reports .iter() .map(|report| self.report_row(report, entity, t)) .collect(); vec![ self.report_filters(entity, t).into_any_element(), list_card(None, rows, w().builder_reports.no_reports, t).into_any_element(), ] } fn report_row(&self, report: &Report, entity: &Entity, t: Tokens) -> Div { let target_avatar = match &report.target { Some(user) => avatar( t, user, px(size::AVATAR), text_fn(text::CAPTION), &self.chat.session.avatars, ) .into_any_element(), None => badge( t, report.id, w().builder_reports.someone, px(size::AVATAR), text_fn(text::CAPTION), ) .into_any_element(), }; let head = div() .flex_1() .min_w_0() .flex() .items_center() .gap(px(space::SM)) .child(user_link( format!("report_target_{}", report.id), report.target.as_ref(), entity, div() .flex() .items_center() .gap(px(space::SM)) .child(target_avatar) .child( div() .font_weight(FontWeight::MEDIUM) .truncate() .child(user_name(&report.target).to_string()), ), )) .child(pill(report.status.label(), status_color(report.status, t))) .child(user_link( format!("report_reporter_{}", report.id), report.reporter.as_ref(), entity, div() .text_size(text_fn(text::CAPTION)) .text_color(t.dim()) .truncate() .child(w().builder_reports.reported_by(user_name(&report.reporter))), )); let meta = caption_text( format!( "{} ยท {}", report .community .as_deref() .unwrap_or(w().builder_reports.deleted_community), millis_time(report.created_at, crate::lang::w().common.stamp) ), t.dim(), ) .flex_shrink_0(); let actions = self.report_actions(report, entity, t); let handled = (report.status != ReportStatus::Open).then(|| { caption_text( w().builder_reports .handled_by(user_name(&report.handled_by)), t.dim(), ) }); div() .px(px(space::LG)) .py(px(space::MD)) .flex() .flex_col() .gap(px(space::SM)) .child( div() .flex() .items_center() .gap(px(space::SM)) .child(head) .child(meta), ) .child( div() .text_size(text_fn(text::LABEL)) .text_color(t.paper()) .child(glyph::wrapped( report.reason.clone().into(), text_fn(text::LABEL), )), ) .children(report.body.as_ref().map(|body| { div() .px(px(space::SM)) .py(px(space::SM)) .rounded(px(radius::MD)) .bg(t.ground()) .text_size(text_fn(text::CAPTION)) .text_color(t.dim()) .line_clamp(2) .child(glyph::wrapped(body.clone().into(), text_fn(text::CAPTION))) })) .children(handled) .children((!actions.is_empty()).then(|| { div() .flex() .flex_wrap() .items_center() .gap(px(space::XS)) .children(actions) })) } fn report_actions(&self, report: &Report, entity: &Entity, t: Tokens) -> Vec { if report.status != ReportStatus::Open { return Vec::new(); } let report_id = report.id; let words = &w().builder_reports; let mut buttons = vec![ parts::button( format!("resolve_report_{report_id}"), words.resolve, Press::Plain, t, entity_action(entity, move |yumush, window, cx| { yumush.handle_report(report_id, ReportStatus::Resolved, window, cx) }), ) .into_any_element(), parts::button( format!("dismiss_report_{report_id}"), words.dismiss, Press::Plain, t, entity_action(entity, move |yumush, window, cx| { yumush.handle_report(report_id, ReportStatus::Dismissed, window, cx) }), ) .into_any_element(), ]; if let (Some(message_id), Some(_)) = (report.message_id, report.community_id) { buttons.push( parts::button( format!("delete_report_message_{report_id}"), words.delete_message, Press::Plain, t, entity_action(entity, move |yumush, window, cx| { yumush.delete_report_message(message_id, window, cx) }), ) .into_any_element(), ); } if let Some(target) = &report.target { let user_id = target.id; let reason = report.reason.clone(); let armed = self.desk.punish; let timeout = Punish::Timeout(user_id); let ban = Punish::Ban(user_id); let timeout_reason = reason.clone(); buttons.push( parts::button( format!("timeout_report_{report_id}_{}", armed == Some(timeout)), if armed == Some(timeout) { w().builder_ui.confirm } else { words.timeout }, Press::Plain, t, entity_action(entity, move |yumush, window, cx| { yumush.punish_report(timeout, &timeout_reason, window, cx) }), ) .into_any_element(), ); buttons.push( parts::button( format!("ban_report_{report_id}_{}", armed == Some(ban)), if armed == Some(ban) { w().builder_ui.confirm } else { words.ban }, Press::Danger, t, entity_action(entity, move |yumush, window, cx| { yumush.punish_report(ban, &reason, window, cx) }), ) .into_any_element(), ); } buttons } }