yumush
Clone
https://git.tahinli.com/Tahinli/yumush.git
0 watching·0 forks·Rust·AGPL-3.0·6.8 MiB·27ceb05·Tahinli·8 hours ago·399 commits
| 1 | use common::{ |
| 2 | call::{ |
| 3 | BanUser, ClearProfile, CreateBackup, CreateInviteCode, CreateUser, DeleteAttachment, |
| 4 | DeleteBackup, DeleteInviteCode, DeleteUser, GetDailyStats, GetOverview, GetStorageUsage, |
| 5 | ListAdminLogs, ListAllCommunities, ListAttachments, ListBackups, ListInviteCodes, |
| 6 | ListServerBans, ListUsers, Logout, PurgeUser, RemoveServerBan, RenameUser, ResetPassword, |
| 7 | RestartServer, RevokeSessions, SearchCommunity, SearchMessages, SetCommunityVoice, |
| 8 | SetGhost, StorageUsage, SuspendUser, TimeoutUser, TransferOwnership, UpdateServerSettings, |
| 9 | WatchCommunity, |
| 10 | }, |
| 11 | error::Error, |
| 12 | event::Event, |
| 13 | model::{ |
| 14 | Account, AdminAction, AdminLogEntry, AttachmentInfo, BackupInfo, BanScope, Community, |
| 15 | CommunitySummary, DayStat, InviteCode, LogAction, MessageHit, Overview, Rank, ServerBan, |
| 16 | ServerBanKind, ServerSettings, User, |
| 17 | }, |
| 18 | permission, |
| 19 | time::{self, MINUTE_MILLIS}, |
| 20 | transport::Close, |
| 21 | }; |
| 22 | |
| 23 | use crate::{ |
| 24 | authentication::{self, Revoke}, |
| 25 | backup, |
| 26 | call::user::user_updated, |
| 27 | call::{Cx, Handle, Me}, |
| 28 | db::{ |
| 29 | attachment, avatar, community, log, member, message, registration_code, server_ban, |
| 30 | session, settings, stats, user, |
| 31 | }, |
| 32 | error::{ServerError, ensure}, |
| 33 | publish::{Broadcast, To}, |
| 34 | request::{ |
| 35 | allowed, call_ended, delete_community, ensure_editable, is_outsider, leave_room_voice, |
| 36 | leave_user_voice, managed, message_event, messages_deleted, record_ghost_voice, |
| 37 | shares_ended, voice_joined, |
| 38 | }, |
| 39 | }; |
| 40 | |
| 41 | const CODE_ALPHABET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; |
| 42 | const CODE_LENGTH: usize = 8; |
| 43 | |
| 44 | fn generate_code() -> Result<String, ServerError> { |
| 45 | let bytes = authentication::random_bytes::<CODE_LENGTH>()?; |
| 46 | |
| 47 | Ok(bytes |
| 48 | .iter() |
| 49 | .map(|byte| char::from(CODE_ALPHABET[usize::from(*byte) % CODE_ALPHABET.len()])) |
| 50 | .collect()) |
| 51 | } |
| 52 | |
| 53 | impl Handle for Logout { |
| 54 | async fn handle(self, me: Me, cx: &mut Cx<'_>) -> Result<(), ServerError> { |
| 55 | me.user()?; |
| 56 | let signed = cx.client.me.as_ref().ok_or(Error::Unauthenticated)?; |
| 57 | authentication::logout(signed.session_id, cx.db).await?; |
| 58 | |
| 59 | Ok(()) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | impl Handle for UpdateServerSettings { |
| 64 | async fn handle(self, me: Me, cx: &mut Cx<'_>) -> Result<ServerSettings, ServerError> { |
| 65 | let me = me.user()?; |
| 66 | let settings = self.settings; |
| 67 | settings::update(&settings, cx.db).await?; |
| 68 | log::record_admin( |
| 69 | Some(me.id), |
| 70 | None, |
| 71 | None, |
| 72 | &AdminAction::SettingsChanged, |
| 73 | cx.db, |
| 74 | ) |
| 75 | .await?; |
| 76 | if settings.maintenance { |
| 77 | let builders = user::builder_ids(cx.db).await?; |
| 78 | cx.sh |
| 79 | .services |
| 80 | .hub |
| 81 | .kick_all_except(&builders, Close::Maintenance); |
| 82 | } |
| 83 | cx.emit(To::Everyone, Event::SettingsUpdated(settings.clone())); |
| 84 | |
| 85 | Ok(settings) |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | impl Handle for RenameUser { |
| 90 | async fn handle(self, me: Me, cx: &mut Cx<'_>) -> Result<User, ServerError> { |
| 91 | let me = me.user()?; |
| 92 | let target = managed(&me, self.user_id, cx.db).await?; |
| 93 | let user = user::update(self.user_id, &self.username, cx.db).await?; |
| 94 | log::record_admin( |
| 95 | Some(me.id), |
| 96 | Some(self.user_id), |
| 97 | None, |
| 98 | &AdminAction::UserRenamed { |
| 99 | from: target.username, |
| 100 | }, |
| 101 | cx.db, |
| 102 | ) |
| 103 | .await?; |
| 104 | user_updated(&user, cx); |
| 105 | |
| 106 | Ok(user) |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | impl Handle for CreateUser { |
| 111 | async fn handle(self, me: Me, cx: &mut Cx<'_>) -> Result<User, ServerError> { |
| 112 | let me = me.user()?; |
| 113 | let user = authentication::create_account(&self.username, &self.password, cx.db).await?; |
| 114 | log::record_admin( |
| 115 | Some(me.id), |
| 116 | Some(user.id), |
| 117 | None, |
| 118 | &AdminAction::UserCreated, |
| 119 | cx.db, |
| 120 | ) |
| 121 | .await?; |
| 122 | |
| 123 | Ok(user) |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | impl Handle for ListUsers { |
| 128 | async fn handle(self, _: Me, cx: &mut Cx<'_>) -> Result<Vec<Account>, ServerError> { |
| 129 | user::all_accounts(cx.db).await |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | impl Handle for ListAdminLogs { |
| 134 | async fn handle(self, _: Me, cx: &mut Cx<'_>) -> Result<Vec<AdminLogEntry>, ServerError> { |
| 135 | let mut entries = match self.action { |
| 136 | Some(common::model::AdminActionKind::Community) => Vec::new(), |
| 137 | action => { |
| 138 | log::list_admin( |
| 139 | self.actor_id, |
| 140 | self.target_id, |
| 141 | action, |
| 142 | self.after_ms, |
| 143 | self.before_ms, |
| 144 | cx.db, |
| 145 | ) |
| 146 | .await? |
| 147 | } |
| 148 | }; |
| 149 | if matches!( |
| 150 | self.action, |
| 151 | None | Some(common::model::AdminActionKind::Community) |
| 152 | ) { |
| 153 | entries.extend( |
| 154 | log::list_communities( |
| 155 | self.actor_id, |
| 156 | self.target_id, |
| 157 | self.after_ms, |
| 158 | self.before_ms, |
| 159 | cx.db, |
| 160 | ) |
| 161 | .await?, |
| 162 | ); |
| 163 | } |
| 164 | entries.sort_by_key(|entry| std::cmp::Reverse(entry.created_at)); |
| 165 | entries.truncate(common::constant::LOG_PAGE_LIMIT as usize); |
| 166 | Ok(entries) |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | impl Handle for common::call::ListUserFriends { |
| 171 | async fn handle(self, _: Me, cx: &mut Cx<'_>) -> Result<Vec<common::model::User>, ServerError> { |
| 172 | Ok(crate::db::friend::state(self.user_id, cx.db).await?.friends) |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | impl Handle for GetDailyStats { |
| 177 | async fn handle(self, _: Me, cx: &mut Cx<'_>) -> Result<Vec<DayStat>, ServerError> { |
| 178 | stats::daily(self.days, cx.db).await |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | impl Handle for GetOverview { |
| 183 | async fn handle(self, _: Me, cx: &mut Cx<'_>) -> Result<Overview, ServerError> { |
| 184 | let (users, builders, communities, storage_bytes) = stats::counts(cx.db).await?; |
| 185 | |
| 186 | Ok(Overview { |
| 187 | users, |
| 188 | builders, |
| 189 | communities, |
| 190 | online: cx.sh.services.hub.online() as u64, |
| 191 | voice: cx.sh.services.rooms.count() as u64, |
| 192 | storage_bytes, |
| 193 | }) |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | impl Handle for SuspendUser { |
| 198 | async fn handle(self, me: Me, cx: &mut Cx<'_>) -> Result<(), ServerError> { |
| 199 | let me = me.user()?; |
| 200 | managed(&me, self.user_id, cx.db).await?; |
| 201 | user::set_suspended(self.user_id, self.suspended, cx.db).await?; |
| 202 | let action = if self.suspended { |
| 203 | AdminAction::UserSuspended |
| 204 | } else { |
| 205 | AdminAction::UserUnsuspended |
| 206 | }; |
| 207 | log::record_admin(Some(me.id), Some(self.user_id), None, &action, cx.db).await?; |
| 208 | if self.suspended { |
| 209 | authentication::revoke(&cx.sh.services.hub, Revoke::User(self.user_id), cx.db).await?; |
| 210 | } |
| 211 | |
| 212 | Ok(()) |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | impl Handle for RevokeSessions { |
| 217 | async fn handle(self, me: Me, cx: &mut Cx<'_>) -> Result<(), ServerError> { |
| 218 | let me = me.user()?; |
| 219 | managed(&me, self.user_id, cx.db).await?; |
| 220 | authentication::revoke(&cx.sh.services.hub, Revoke::User(self.user_id), cx.db).await?; |
| 221 | log::record_admin( |
| 222 | Some(me.id), |
| 223 | Some(self.user_id), |
| 224 | None, |
| 225 | &AdminAction::SessionsRevoked, |
| 226 | cx.db, |
| 227 | ) |
| 228 | .await?; |
| 229 | |
| 230 | Ok(()) |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | impl Handle for ResetPassword { |
| 235 | async fn handle(self, me: Me, cx: &mut Cx<'_>) -> Result<(), ServerError> { |
| 236 | let me = me.user()?; |
| 237 | managed(&me, self.user_id, cx.db).await?; |
| 238 | authentication::reset_password(self.user_id, &self.password, cx.db).await?; |
| 239 | authentication::revoke(&cx.sh.services.hub, Revoke::User(self.user_id), cx.db).await?; |
| 240 | log::record_admin( |
| 241 | Some(me.id), |
| 242 | Some(self.user_id), |
| 243 | None, |
| 244 | &AdminAction::PasswordReset, |
| 245 | cx.db, |
| 246 | ) |
| 247 | .await?; |
| 248 | |
| 249 | Ok(()) |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | impl Handle for ClearProfile { |
| 254 | async fn handle(self, me: Me, cx: &mut Cx<'_>) -> Result<User, ServerError> { |
| 255 | let me = me.user()?; |
| 256 | managed(&me, self.user_id, cx.db).await?; |
| 257 | avatar::set_user(self.user_id, None, cx.db).await?; |
| 258 | let user = user::update_status(self.user_id, "", cx.db).await?; |
| 259 | log::record_admin( |
| 260 | Some(me.id), |
| 261 | Some(self.user_id), |
| 262 | None, |
| 263 | &AdminAction::ProfileCleared, |
| 264 | cx.db, |
| 265 | ) |
| 266 | .await?; |
| 267 | user_updated(&user, cx); |
| 268 | |
| 269 | Ok(user) |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | impl Handle for DeleteUser { |
| 274 | async fn handle(self, me: Me, cx: &mut Cx<'_>) -> Result<(), ServerError> { |
| 275 | let me = me.user()?; |
| 276 | let user_id = self.user_id; |
| 277 | let target = user::read(user_id, cx.db).await?; |
| 278 | ensure(permission::can_delete_user( |
| 279 | me.role, |
| 280 | me.id == user_id, |
| 281 | target.role, |
| 282 | ))?; |
| 283 | if permission::is_builder(me.role) && me.id != user_id { |
| 284 | log::record_admin( |
| 285 | Some(me.id), |
| 286 | Some(user_id), |
| 287 | None, |
| 288 | &AdminAction::UserDeleted, |
| 289 | cx.db, |
| 290 | ) |
| 291 | .await?; |
| 292 | } |
| 293 | let departed = leave_user_voice(&cx.sh.services.rooms, user_id, cx.db).await?; |
| 294 | cx.out.extend(departed); |
| 295 | for community_id in member::owned(user_id, cx.db).await? { |
| 296 | delete_community(community_id, cx).await?; |
| 297 | } |
| 298 | for community in community::communities_of(user_id, cx.db).await? { |
| 299 | let member_ids = member::member_ids(community.id, cx.db).await?; |
| 300 | if community.kind.is_direct() { |
| 301 | delete_community(community.id, cx).await?; |
| 302 | } |
| 303 | let event = Event::UserDeleted { user_id }; |
| 304 | cx.out.push(Broadcast::new(To::Users(member_ids), event)); |
| 305 | } |
| 306 | user::delete(user_id, cx.db).await?; |
| 307 | if me.id != user_id { |
| 308 | cx.sh.services.hub.kick_user(user_id, Close::Revoked); |
| 309 | } |
| 310 | |
| 311 | Ok(()) |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | impl Handle for WatchCommunity { |
| 316 | async fn handle(self, me: Me, cx: &mut Cx<'_>) -> Result<(), ServerError> { |
| 317 | let me = me.user()?; |
| 318 | let Some(community_id) = self.community_id else { |
| 319 | cx.client.watch(None); |
| 320 | return Ok(()); |
| 321 | }; |
| 322 | allowed(&me, community_id, cx.db, permission::can_read).await?; |
| 323 | let outsider = is_outsider(me.id, community_id, cx.db).await?; |
| 324 | cx.client.watch(outsider.then_some(community_id)); |
| 325 | let visit = |
| 326 | permission::is_builder(me.role) && outsider && user::is_ghost(me.id, cx.db).await?; |
| 327 | if visit && cx.client.first_visit(community_id) { |
| 328 | let community = community::label(community_id, cx.db).await?; |
| 329 | log::record_admin( |
| 330 | Some(me.id), |
| 331 | None, |
| 332 | Some(&community), |
| 333 | &AdminAction::GhostVisit, |
| 334 | cx.db, |
| 335 | ) |
| 336 | .await?; |
| 337 | } |
| 338 | |
| 339 | Ok(()) |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | impl Handle for ListAllCommunities { |
| 344 | async fn handle(self, _: Me, cx: &mut Cx<'_>) -> Result<Vec<CommunitySummary>, ServerError> { |
| 345 | let mut summaries = Vec::new(); |
| 346 | for community in community::groups(cx.db).await? { |
| 347 | let members = member::members(community.id, cx.db).await?; |
| 348 | let owner = members |
| 349 | .iter() |
| 350 | .find(|member| member.rank == Rank::Owner) |
| 351 | .map(|member| member.user.clone()); |
| 352 | summaries.push(CommunitySummary { |
| 353 | community, |
| 354 | owner, |
| 355 | members: members.len() as u64, |
| 356 | }); |
| 357 | } |
| 358 | |
| 359 | Ok(summaries) |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | impl Handle for TransferOwnership { |
| 364 | async fn handle(self, me: Me, cx: &mut Cx<'_>) -> Result<(), ServerError> { |
| 365 | let me = me.user()?; |
| 366 | let Self { |
| 367 | community_id, |
| 368 | user_id, |
| 369 | } = self; |
| 370 | ensure_editable(community_id, cx.db).await?; |
| 371 | allowed(&me, community_id, cx.db, permission::can_transfer_ownership).await?; |
| 372 | let members = member::members(community_id, cx.db).await?; |
| 373 | let target = members |
| 374 | .iter() |
| 375 | .find(|member| member.user.id == user_id) |
| 376 | .ok_or(Error::NotFound)?; |
| 377 | ensure(target.rank != Rank::Owner)?; |
| 378 | let mut changes = vec![(user_id, Rank::Owner)]; |
| 379 | if let Some(previous) = members.iter().find(|member| member.rank == Rank::Owner) { |
| 380 | member::set_rank(previous.user.id, community_id, Rank::Admin, cx.db).await?; |
| 381 | log::record( |
| 382 | community_id, |
| 383 | Some(me.id), |
| 384 | Some(previous.user.id), |
| 385 | &LogAction::RankChanged { rank: Rank::Admin }, |
| 386 | cx.db, |
| 387 | ) |
| 388 | .await?; |
| 389 | changes.push((previous.user.id, Rank::Admin)); |
| 390 | } |
| 391 | member::set_rank(user_id, community_id, Rank::Owner, cx.db).await?; |
| 392 | log::record( |
| 393 | community_id, |
| 394 | Some(me.id), |
| 395 | Some(user_id), |
| 396 | &LogAction::RankChanged { rank: Rank::Owner }, |
| 397 | cx.db, |
| 398 | ) |
| 399 | .await?; |
| 400 | if permission::is_builder(me.role) { |
| 401 | let community = community::read(community_id, me.id, cx.db).await?; |
| 402 | log::record_admin( |
| 403 | Some(me.id), |
| 404 | Some(user_id), |
| 405 | Some(&community.name), |
| 406 | &AdminAction::OwnershipTransferred, |
| 407 | cx.db, |
| 408 | ) |
| 409 | .await?; |
| 410 | } |
| 411 | for (changed_id, rank) in changes { |
| 412 | cx.emit( |
| 413 | To::Community(community_id), |
| 414 | Event::RankChanged { |
| 415 | community_id, |
| 416 | user_id: changed_id, |
| 417 | rank, |
| 418 | }, |
| 419 | ); |
| 420 | } |
| 421 | |
| 422 | Ok(()) |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | impl Handle for TimeoutUser { |
| 427 | async fn handle(self, me: Me, cx: &mut Cx<'_>) -> Result<(), ServerError> { |
| 428 | let me = me.user()?; |
| 429 | let Self { user_id, minutes } = self; |
| 430 | managed(&me, user_id, cx.db).await?; |
| 431 | let until = (minutes > 0).then(|| time::now_ms() + u64::from(minutes) * MINUTE_MILLIS); |
| 432 | user::set_timeout(user_id, until, cx.db).await?; |
| 433 | if until.is_some() { |
| 434 | authentication::revoke(&cx.sh.services.hub, Revoke::User(user_id), cx.db).await?; |
| 435 | } |
| 436 | log::record_admin( |
| 437 | Some(me.id), |
| 438 | Some(user_id), |
| 439 | None, |
| 440 | &AdminAction::UserTimedOut { minutes }, |
| 441 | cx.db, |
| 442 | ) |
| 443 | .await?; |
| 444 | |
| 445 | Ok(()) |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | impl Handle for BanUser { |
| 450 | async fn handle(self, me: Me, cx: &mut Cx<'_>) -> Result<(), ServerError> { |
| 451 | let me = me.user()?; |
| 452 | let Self { |
| 453 | user_id, |
| 454 | reason, |
| 455 | scope, |
| 456 | } = self; |
| 457 | let target = managed(&me, user_id, cx.db).await?; |
| 458 | user::set_suspended(user_id, true, cx.db).await?; |
| 459 | session::delete_all(user_id, cx.db).await?; |
| 460 | server_ban::insert( |
| 461 | ServerBanKind::Username, |
| 462 | &target.username, |
| 463 | reason.as_deref(), |
| 464 | cx.db, |
| 465 | ) |
| 466 | .await?; |
| 467 | if scope == BanScope::AccountAndIp |
| 468 | && let Some(address) = user::last_ip(user_id, cx.db).await? |
| 469 | { |
| 470 | server_ban::insert(ServerBanKind::Ip, &address, reason.as_deref(), cx.db).await?; |
| 471 | } |
| 472 | log::record_admin( |
| 473 | Some(me.id), |
| 474 | Some(user_id), |
| 475 | None, |
| 476 | &AdminAction::UserBanned { reason }, |
| 477 | cx.db, |
| 478 | ) |
| 479 | .await?; |
| 480 | cx.sh.services.hub.kick_user(user_id, Close::Banned); |
| 481 | |
| 482 | Ok(()) |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | impl Handle for ListServerBans { |
| 487 | async fn handle(self, _: Me, cx: &mut Cx<'_>) -> Result<Vec<ServerBan>, ServerError> { |
| 488 | server_ban::list(cx.db).await |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | impl Handle for RemoveServerBan { |
| 493 | async fn handle(self, me: Me, cx: &mut Cx<'_>) -> Result<(), ServerError> { |
| 494 | let me = me.user()?; |
| 495 | server_ban::remove(self.id, cx.db).await?; |
| 496 | log::record_admin(Some(me.id), None, None, &AdminAction::BanRemoved, cx.db).await?; |
| 497 | |
| 498 | Ok(()) |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | impl Handle for CreateInviteCode { |
| 503 | async fn handle(self, me: Me, cx: &mut Cx<'_>) -> Result<InviteCode, ServerError> { |
| 504 | let me = me.user()?; |
| 505 | ensure(permission::is_builder(me.role) && self.uses > 0)?; |
| 506 | let code = |
| 507 | registration_code::create(&generate_code()?, self.uses, self.hours, cx.db).await?; |
| 508 | log::record_admin(Some(me.id), None, None, &AdminAction::InviteCreated, cx.db).await?; |
| 509 | |
| 510 | Ok(code) |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | impl Handle for ListInviteCodes { |
| 515 | async fn handle(self, _: Me, cx: &mut Cx<'_>) -> Result<Vec<InviteCode>, ServerError> { |
| 516 | registration_code::list(cx.db).await |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | impl Handle for DeleteInviteCode { |
| 521 | async fn handle(self, me: Me, cx: &mut Cx<'_>) -> Result<(), ServerError> { |
| 522 | let me = me.user()?; |
| 523 | registration_code::delete(&self.code, cx.db).await?; |
| 524 | log::record_admin(Some(me.id), None, None, &AdminAction::InviteDeleted, cx.db).await?; |
| 525 | |
| 526 | Ok(()) |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | impl Handle for SearchMessages { |
| 531 | async fn handle(self, _: Me, cx: &mut Cx<'_>) -> Result<Vec<MessageHit>, ServerError> { |
| 532 | message::search( |
| 533 | self.text.as_deref(), |
| 534 | self.user_id, |
| 535 | self.community_id, |
| 536 | self.after_ms, |
| 537 | self.before_ms, |
| 538 | cx.db, |
| 539 | ) |
| 540 | .await |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | impl Handle for SearchCommunity { |
| 545 | async fn handle(self, me: Me, cx: &mut Cx<'_>) -> Result<Vec<MessageHit>, ServerError> { |
| 546 | let me = me.user()?; |
| 547 | allowed(&me, self.community_id, cx.db, permission::can_read).await?; |
| 548 | |
| 549 | message::search( |
| 550 | Some(&self.text), |
| 551 | None, |
| 552 | Some(self.community_id), |
| 553 | None, |
| 554 | None, |
| 555 | cx.db, |
| 556 | ) |
| 557 | .await |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | impl Handle for PurgeUser { |
| 562 | async fn handle(self, me: Me, cx: &mut Cx<'_>) -> Result<(), ServerError> { |
| 563 | let me = me.user()?; |
| 564 | let user_id = self.user_id; |
| 565 | managed(&me, user_id, cx.db).await?; |
| 566 | let deleted = message::delete_where_user(user_id, cx.db).await?; |
| 567 | attachment::delete_uploads(user_id, cx.db).await?; |
| 568 | log::record_admin( |
| 569 | Some(me.id), |
| 570 | Some(user_id), |
| 571 | None, |
| 572 | &AdminAction::UserPurged { |
| 573 | messages: deleted.len() as u64, |
| 574 | }, |
| 575 | cx.db, |
| 576 | ) |
| 577 | .await?; |
| 578 | cx.out.extend(messages_deleted(deleted)); |
| 579 | |
| 580 | Ok(()) |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | impl Handle for ListAttachments { |
| 585 | async fn handle(self, _: Me, cx: &mut Cx<'_>) -> Result<Vec<AttachmentInfo>, ServerError> { |
| 586 | attachment::all(self.user_id, self.community_id, cx.db).await |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | impl Handle for DeleteAttachment { |
| 591 | async fn handle(self, me: Me, cx: &mut Cx<'_>) -> Result<(), ServerError> { |
| 592 | let me = me.user()?; |
| 593 | let stored = attachment::read(self.attachment_id, cx.db).await?; |
| 594 | let community = community::label(stored.community_id, cx.db).await?; |
| 595 | attachment::delete(self.attachment_id, cx.db).await?; |
| 596 | log::record_admin( |
| 597 | Some(me.id), |
| 598 | Some(stored.uploader_id), |
| 599 | Some(&community), |
| 600 | &AdminAction::AttachmentDeleted, |
| 601 | cx.db, |
| 602 | ) |
| 603 | .await?; |
| 604 | if let Some(message_id) = stored.message_id { |
| 605 | let updated = message::read(message_id, cx.db).await?; |
| 606 | cx.out.push(message_event(&updated, Event::MessageUpdated)); |
| 607 | } |
| 608 | |
| 609 | Ok(()) |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | impl Handle for GetStorageUsage { |
| 614 | async fn handle(self, _: Me, cx: &mut Cx<'_>) -> Result<StorageUsage, ServerError> { |
| 615 | let (users, communities) = attachment::usage(cx.db).await?; |
| 616 | |
| 617 | Ok(StorageUsage { users, communities }) |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | impl Handle for CreateBackup { |
| 622 | async fn handle(self, me: Me, cx: &mut Cx<'_>) -> Result<BackupInfo, ServerError> { |
| 623 | let me = me.user()?; |
| 624 | let backup = backup::create(&cx.sh.db, &cx.sh.backups).await?; |
| 625 | let action = AdminAction::BackupCreated { |
| 626 | name: backup.name.clone(), |
| 627 | }; |
| 628 | log::record_admin(Some(me.id), None, None, &action, cx.db).await?; |
| 629 | |
| 630 | Ok(backup) |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | impl Handle for ListBackups { |
| 635 | async fn handle(self, _: Me, cx: &mut Cx<'_>) -> Result<Vec<BackupInfo>, ServerError> { |
| 636 | backup::list(&cx.sh.backups) |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | impl Handle for DeleteBackup { |
| 641 | async fn handle(self, me: Me, cx: &mut Cx<'_>) -> Result<(), ServerError> { |
| 642 | let me = me.user()?; |
| 643 | backup::delete(&self.name, &cx.sh.backups)?; |
| 644 | let action = AdminAction::BackupDeleted { name: self.name }; |
| 645 | log::record_admin(Some(me.id), None, None, &action, cx.db).await?; |
| 646 | |
| 647 | Ok(()) |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | impl Handle for SetCommunityVoice { |
| 652 | async fn handle(self, me: Me, cx: &mut Cx<'_>) -> Result<Community, ServerError> { |
| 653 | let me = me.user()?; |
| 654 | let Self { |
| 655 | community_id, |
| 656 | enabled, |
| 657 | } = self; |
| 658 | allowed(&me, community_id, cx.db, permission::can_manage_voice).await?; |
| 659 | community::set_voice_enabled(community_id, enabled, cx.db).await?; |
| 660 | let updated = community::read(community_id, me.id, cx.db).await?; |
| 661 | if permission::is_builder(me.role) { |
| 662 | log::record_admin( |
| 663 | Some(me.id), |
| 664 | None, |
| 665 | Some(&updated.name), |
| 666 | &AdminAction::CommunityVoiceSet { enabled }, |
| 667 | cx.db, |
| 668 | ) |
| 669 | .await?; |
| 670 | } |
| 671 | if !enabled { |
| 672 | let rooms = &cx.sh.services.rooms; |
| 673 | for user_id in rooms.room_users(community_id) { |
| 674 | if !permission::is_builder(user::read(user_id, cx.db).await?.role) { |
| 675 | cx.out |
| 676 | .extend(leave_room_voice(rooms, community_id, user_id, cx.db).await?); |
| 677 | } |
| 678 | } |
| 679 | } |
| 680 | cx.emit( |
| 681 | To::Community(community_id), |
| 682 | Event::CommunityUpdated(updated.clone()), |
| 683 | ); |
| 684 | |
| 685 | Ok(updated) |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | impl Handle for RestartServer { |
| 690 | async fn handle(self, me: Me, cx: &mut Cx<'_>) -> Result<(), ServerError> { |
| 691 | let me = me.user()?; |
| 692 | tracing::info!("restart requested by {}", me.username); |
| 693 | cx.sh.services.restart_soon(); |
| 694 | Ok(()) |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | impl Handle for SetGhost { |
| 699 | async fn handle(self, me: Me, cx: &mut Cx<'_>) -> Result<(), ServerError> { |
| 700 | let me = me.user()?; |
| 701 | let enabled = self.enabled; |
| 702 | let rooms = &cx.sh.services.rooms; |
| 703 | user::set_ghost(me.id, enabled, cx.db).await?; |
| 704 | let action = if enabled { |
| 705 | AdminAction::GhostEnabled |
| 706 | } else { |
| 707 | AdminAction::GhostDisabled |
| 708 | }; |
| 709 | log::record_admin(Some(me.id), None, None, &action, cx.db).await?; |
| 710 | let mut broadcasts = Vec::new(); |
| 711 | let outsider = match rooms.room_of(me.id) { |
| 712 | Some(community_id) => is_outsider(me.id, community_id, cx.db).await?, |
| 713 | None => false, |
| 714 | }; |
| 715 | if !outsider && let Some(mut departure) = rooms.set_ghost(me.id, enabled) { |
| 716 | let community_id = departure.community_id; |
| 717 | if enabled { |
| 718 | broadcasts.extend(shares_ended(&mut departure)); |
| 719 | broadcasts |
| 720 | .extend(call_ended(community_id, me.id, departure.call_log, cx.db).await?); |
| 721 | let event = Event::VoiceLeft { |
| 722 | community_id, |
| 723 | user_id: me.id, |
| 724 | }; |
| 725 | broadcasts.push(Broadcast::new( |
| 726 | To::CommunityExcept(community_id, me.id), |
| 727 | event, |
| 728 | )); |
| 729 | } else { |
| 730 | let muted = user::voice_muted(me.id, cx.db).await?; |
| 731 | broadcasts.extend(voice_joined(community_id, me.id, muted)); |
| 732 | if let Some(seconds) = departure.ghost_seconds { |
| 733 | record_ghost_voice(me.id, community_id, seconds, cx.db).await?; |
| 734 | } |
| 735 | } |
| 736 | } |
| 737 | cx.out.extend(broadcasts); |
| 738 | |
| 739 | Ok(()) |
| 740 | } |
| 741 | } |