yumush
Clone
https://git.tahinli.com/Tahinli/yumush.git
0 watching·0 forks·Rust·AGPL-3.0·7.2 MiB·4b67e80·Tahinli·1 hour ago·400 commits
| 1 | use crate::lang::{Label as _, w}; |
| 2 | use common::model::Id; |
| 3 | use std::future::Future; |
| 4 | |
| 5 | use common::{ |
| 6 | call::{BanUser, DeleteMessage, HandleReport, ListReports, TimeoutUser}, |
| 7 | model::{BanScope, Report, ReportStatus}, |
| 8 | }; |
| 9 | use gpui_kit::{ |
| 10 | AnyElement, Context, Div, Entity, FontWeight, Hsla, IntoElement, ParentElement, Styled, Window, |
| 11 | div, px, |
| 12 | }; |
| 13 | |
| 14 | use super::ui::{list_card, pill}; |
| 15 | use crate::{ |
| 16 | gui::{ |
| 17 | Palette, Yumush, avatar, badge, clickable, |
| 18 | community_settings::user_name, |
| 19 | entity_action, millis_time, notify_error, |
| 20 | parts::{self, Press}, |
| 21 | profile::user_link, |
| 22 | settings_page::{segment, segment_track}, |
| 23 | style::{radius, size, space, text}, |
| 24 | text as text_fn, |
| 25 | }, |
| 26 | net::{Fail, NetworkHandle}, |
| 27 | }; |
| 28 | |
| 29 | const REPORT_FILTERS: [Option<ReportStatus>; 4] = [ |
| 30 | Some(ReportStatus::Open), |
| 31 | Some(ReportStatus::Resolved), |
| 32 | Some(ReportStatus::Dismissed), |
| 33 | None, |
| 34 | ]; |
| 35 | |
| 36 | fn status_color(status: ReportStatus, palette: Palette) -> Hsla { |
| 37 | match status { |
| 38 | ReportStatus::Open => palette.accent, |
| 39 | ReportStatus::Resolved => palette.ok, |
| 40 | ReportStatus::Dismissed => palette.muted, |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | impl Yumush { |
| 45 | pub(super) fn load_open_reports(&self, window: &mut Window, cx: &mut Context<Self>) { |
| 46 | self.fetch_builder( |
| 47 | window, |
| 48 | cx, |
| 49 | |network| async move { |
| 50 | network |
| 51 | .call(ListReports { |
| 52 | status: Some(ReportStatus::Open), |
| 53 | }) |
| 54 | .await |
| 55 | .map(|reports| reports.len()) |
| 56 | }, |
| 57 | |yumush, count, cx| { |
| 58 | yumush.chat.open_reports = count; |
| 59 | cx.notify(); |
| 60 | }, |
| 61 | ); |
| 62 | } |
| 63 | |
| 64 | pub(super) fn load_reports(&self, window: &mut Window, cx: &mut Context<Self>) { |
| 65 | let status = self.chat.report_filter; |
| 66 | self.fetch_builder( |
| 67 | window, |
| 68 | cx, |
| 69 | move |network| async move { network.call(ListReports { status }).await }, |
| 70 | |yumush, reports, cx| { |
| 71 | yumush.chat.builder_reports = reports; |
| 72 | cx.notify(); |
| 73 | }, |
| 74 | ); |
| 75 | } |
| 76 | |
| 77 | pub(super) fn refresh_reports(&self, window: &mut Window, cx: &mut Context<Self>) { |
| 78 | self.load_reports(window, cx); |
| 79 | self.load_open_reports(window, cx); |
| 80 | } |
| 81 | |
| 82 | pub(super) fn select_report_filter( |
| 83 | &mut self, |
| 84 | status: Option<ReportStatus>, |
| 85 | window: &mut Window, |
| 86 | cx: &mut Context<Self>, |
| 87 | ) { |
| 88 | self.chat.report_filter = status; |
| 89 | self.load_reports(window, cx); |
| 90 | cx.notify(); |
| 91 | } |
| 92 | |
| 93 | pub(super) fn report_action<Fut>( |
| 94 | &mut self, |
| 95 | window: &mut Window, |
| 96 | cx: &mut Context<Self>, |
| 97 | action: impl FnOnce(NetworkHandle) -> Fut + 'static, |
| 98 | ) where |
| 99 | Fut: Future<Output = Result<(), Fail>> + 'static, |
| 100 | { |
| 101 | let network = self.network.clone(); |
| 102 | |
| 103 | cx.spawn_in(window, async move |this, cx| match action(network).await { |
| 104 | Ok(()) => { |
| 105 | let _ = this.update_in(cx, |yumush, window, cx| yumush.refresh_reports(window, cx)); |
| 106 | } |
| 107 | Err(error) => notify_error(&this, cx, error), |
| 108 | }) |
| 109 | .detach(); |
| 110 | } |
| 111 | |
| 112 | pub(super) fn handle_report( |
| 113 | &mut self, |
| 114 | report_id: Id, |
| 115 | status: ReportStatus, |
| 116 | window: &mut Window, |
| 117 | cx: &mut Context<Self>, |
| 118 | ) { |
| 119 | self.report_action(window, cx, move |network| async move { |
| 120 | network.call(HandleReport { report_id, status }).await |
| 121 | }); |
| 122 | } |
| 123 | |
| 124 | pub(super) fn delete_report_message( |
| 125 | &mut self, |
| 126 | message_id: Id, |
| 127 | window: &mut Window, |
| 128 | cx: &mut Context<Self>, |
| 129 | ) { |
| 130 | self.report_action(window, cx, move |network| async move { |
| 131 | network.call(DeleteMessage { message_id }).await |
| 132 | }); |
| 133 | } |
| 134 | |
| 135 | pub(super) fn timeout_report_target( |
| 136 | &mut self, |
| 137 | user_id: Id, |
| 138 | window: &mut Window, |
| 139 | cx: &mut Context<Self>, |
| 140 | ) { |
| 141 | self.report_action(window, cx, move |network| async move { |
| 142 | network |
| 143 | .call(TimeoutUser { |
| 144 | user_id, |
| 145 | minutes: 60, |
| 146 | }) |
| 147 | .await |
| 148 | }); |
| 149 | } |
| 150 | |
| 151 | pub(super) fn ban_report_target( |
| 152 | &mut self, |
| 153 | user_id: Id, |
| 154 | reason: &str, |
| 155 | window: &mut Window, |
| 156 | cx: &mut Context<Self>, |
| 157 | ) { |
| 158 | let reason = reason.to_string(); |
| 159 | self.report_action(window, cx, move |network| async move { |
| 160 | network |
| 161 | .call(BanUser { |
| 162 | user_id, |
| 163 | reason: Some(reason), |
| 164 | scope: BanScope::Account, |
| 165 | }) |
| 166 | .await |
| 167 | }); |
| 168 | } |
| 169 | |
| 170 | pub(super) fn report_filters(&self, entity: &Entity<Self>, palette: Palette) -> Div { |
| 171 | div().flex().child( |
| 172 | segment_track(palette).children(REPORT_FILTERS.map(|status| { |
| 173 | let label = status.map_or(w().builder_reports.all, ReportStatus::label); |
| 174 | segment( |
| 175 | clickable( |
| 176 | label, |
| 177 | palette, |
| 178 | entity_action(entity, move |yumush, window, cx| { |
| 179 | yumush.select_report_filter(status, window, cx) |
| 180 | }), |
| 181 | ), |
| 182 | self.chat.report_filter == status, |
| 183 | palette, |
| 184 | ) |
| 185 | .px(px(space::MD)) |
| 186 | .child(label) |
| 187 | })), |
| 188 | ) |
| 189 | } |
| 190 | |
| 191 | pub(super) fn reports_body(&self, entity: &Entity<Self>, palette: Palette) -> Vec<AnyElement> { |
| 192 | let rows: Vec<Div> = self |
| 193 | .chat |
| 194 | .builder_reports |
| 195 | .iter() |
| 196 | .map(|report| self.report_row(report, entity, palette)) |
| 197 | .collect(); |
| 198 | |
| 199 | vec![ |
| 200 | self.report_filters(entity, palette).into_any_element(), |
| 201 | list_card(None, rows, w().builder_reports.no_reports, palette).into_any_element(), |
| 202 | ] |
| 203 | } |
| 204 | |
| 205 | pub(super) fn report_row( |
| 206 | &self, |
| 207 | report: &Report, |
| 208 | entity: &Entity<Self>, |
| 209 | palette: Palette, |
| 210 | ) -> Div { |
| 211 | let target_avatar = match &report.target { |
| 212 | Some(user) => avatar( |
| 213 | palette, |
| 214 | user, |
| 215 | px(size::AVATAR), |
| 216 | text_fn(text::CAPTION), |
| 217 | &self.chat.avatars, |
| 218 | ) |
| 219 | .into_any_element(), |
| 220 | None => badge( |
| 221 | palette, |
| 222 | report.id, |
| 223 | w().builder_reports.someone, |
| 224 | px(size::AVATAR), |
| 225 | text_fn(text::CAPTION), |
| 226 | ) |
| 227 | .into_any_element(), |
| 228 | }; |
| 229 | let head = div() |
| 230 | .flex_1() |
| 231 | .min_w_0() |
| 232 | .flex() |
| 233 | .items_center() |
| 234 | .gap(px(space::SM)) |
| 235 | .child(user_link( |
| 236 | format!("report_target_{}", report.id), |
| 237 | report.target.as_ref(), |
| 238 | entity, |
| 239 | div() |
| 240 | .flex() |
| 241 | .items_center() |
| 242 | .gap(px(space::SM)) |
| 243 | .child(target_avatar) |
| 244 | .child( |
| 245 | div() |
| 246 | .font_weight(FontWeight::MEDIUM) |
| 247 | .truncate() |
| 248 | .child(user_name(&report.target).to_string()), |
| 249 | ), |
| 250 | )) |
| 251 | .child(pill( |
| 252 | report.status.label(), |
| 253 | status_color(report.status, palette), |
| 254 | palette, |
| 255 | )) |
| 256 | .child(user_link( |
| 257 | format!("report_reporter_{}", report.id), |
| 258 | report.reporter.as_ref(), |
| 259 | entity, |
| 260 | div() |
| 261 | .text_size(text_fn(text::CAPTION)) |
| 262 | .text_color(palette.muted) |
| 263 | .truncate() |
| 264 | .child(w().builder_reports.reported_by(user_name(&report.reporter))), |
| 265 | )); |
| 266 | let meta = div() |
| 267 | .flex_shrink_0() |
| 268 | .text_size(text_fn(text::CAPTION)) |
| 269 | .text_color(palette.muted) |
| 270 | .child(format!( |
| 271 | "{} · {}", |
| 272 | report |
| 273 | .community |
| 274 | .as_deref() |
| 275 | .unwrap_or(w().builder_reports.deleted_community), |
| 276 | millis_time(report.created_at, crate::lang::w().common.stamp) |
| 277 | )); |
| 278 | let actions = self.report_actions(report, entity, palette); |
| 279 | let handled = (report.status != ReportStatus::Open).then(|| { |
| 280 | div() |
| 281 | .text_size(text_fn(text::CAPTION)) |
| 282 | .text_color(palette.muted) |
| 283 | .child( |
| 284 | w().builder_reports |
| 285 | .handled_by(user_name(&report.handled_by)), |
| 286 | ) |
| 287 | }); |
| 288 | |
| 289 | div() |
| 290 | .px(px(space::LG)) |
| 291 | .py(px(space::MD)) |
| 292 | .flex() |
| 293 | .flex_col() |
| 294 | .gap(px(space::SM)) |
| 295 | .child( |
| 296 | div() |
| 297 | .flex() |
| 298 | .items_center() |
| 299 | .gap(px(space::SM)) |
| 300 | .child(head) |
| 301 | .child(meta), |
| 302 | ) |
| 303 | .child( |
| 304 | div() |
| 305 | .text_size(text_fn(text::LABEL)) |
| 306 | .text_color(palette.body) |
| 307 | .child(super::super::glyph::wrapped( |
| 308 | report.reason.clone().into(), |
| 309 | super::super::text(super::super::style::text::LABEL), |
| 310 | )), |
| 311 | ) |
| 312 | .children(report.body.as_ref().map(|body| { |
| 313 | div() |
| 314 | .px(px(space::SM)) |
| 315 | .py(px(space::SM)) |
| 316 | .rounded(px(radius::MD)) |
| 317 | .bg(palette.window) |
| 318 | .text_size(text_fn(text::CAPTION)) |
| 319 | .text_color(palette.muted) |
| 320 | .line_clamp(2) |
| 321 | .child(super::super::glyph::wrapped( |
| 322 | body.clone().into(), |
| 323 | super::super::text(super::super::style::text::CAPTION), |
| 324 | )) |
| 325 | })) |
| 326 | .children(handled) |
| 327 | .children((!actions.is_empty()).then(|| { |
| 328 | div() |
| 329 | .flex() |
| 330 | .flex_wrap() |
| 331 | .items_center() |
| 332 | .gap(px(space::XS)) |
| 333 | .children(actions) |
| 334 | })) |
| 335 | } |
| 336 | |
| 337 | pub(super) fn report_actions( |
| 338 | &self, |
| 339 | report: &Report, |
| 340 | entity: &Entity<Self>, |
| 341 | palette: Palette, |
| 342 | ) -> Vec<AnyElement> { |
| 343 | if report.status != ReportStatus::Open { |
| 344 | return Vec::new(); |
| 345 | } |
| 346 | let resolve_id = report.id; |
| 347 | let dismiss_id = report.id; |
| 348 | let mut buttons = vec![ |
| 349 | parts::button( |
| 350 | format!("resolve_report_{}", report.id), |
| 351 | w().builder_reports.resolve, |
| 352 | Press::Plain, |
| 353 | palette.tokens, |
| 354 | entity_action(entity, move |yumush, window, cx| { |
| 355 | yumush.handle_report(resolve_id, ReportStatus::Resolved, window, cx) |
| 356 | }), |
| 357 | ) |
| 358 | .into_any_element(), |
| 359 | parts::button( |
| 360 | format!("dismiss_report_{}", report.id), |
| 361 | w().builder_reports.dismiss, |
| 362 | Press::Plain, |
| 363 | palette.tokens, |
| 364 | entity_action(entity, move |yumush, window, cx| { |
| 365 | yumush.handle_report(dismiss_id, ReportStatus::Dismissed, window, cx) |
| 366 | }), |
| 367 | ) |
| 368 | .into_any_element(), |
| 369 | ]; |
| 370 | if let (Some(message_id), Some(_)) = (report.message_id, report.community_id) { |
| 371 | buttons.push( |
| 372 | parts::button( |
| 373 | format!("delete_report_message_{}", report.id), |
| 374 | w().builder_reports.delete_message, |
| 375 | Press::Plain, |
| 376 | palette.tokens, |
| 377 | entity_action(entity, move |yumush, window, cx| { |
| 378 | yumush.delete_report_message(message_id, window, cx) |
| 379 | }), |
| 380 | ) |
| 381 | .into_any_element(), |
| 382 | ); |
| 383 | } |
| 384 | if let Some(target) = &report.target { |
| 385 | let timeout_id = target.id; |
| 386 | let ban_id = target.id; |
| 387 | let ban_reason = report.reason.clone(); |
| 388 | buttons.push( |
| 389 | parts::button( |
| 390 | format!("timeout_report_{}", report.id), |
| 391 | w().builder_reports.timeout, |
| 392 | Press::Plain, |
| 393 | palette.tokens, |
| 394 | entity_action(entity, move |yumush, window, cx| { |
| 395 | yumush.timeout_report_target(timeout_id, window, cx) |
| 396 | }), |
| 397 | ) |
| 398 | .into_any_element(), |
| 399 | ); |
| 400 | buttons.push( |
| 401 | parts::button( |
| 402 | format!("ban_report_{}", report.id), |
| 403 | w().builder_reports.ban, |
| 404 | Press::Danger, |
| 405 | palette.tokens, |
| 406 | entity_action(entity, move |yumush, window, cx| { |
| 407 | yumush.ban_report_target(ban_id, &ban_reason, window, cx) |
| 408 | }), |
| 409 | ) |
| 410 | .into_any_element(), |
| 411 | ); |
| 412 | } |
| 413 | |
| 414 | buttons |
| 415 | } |
| 416 | } |