2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 site::{GetSiteResponse, LeaveAdmin},
5 utils::{blocking, build_federated_instances, get_local_user_view_from_jwt, is_admin},
9 moderator::{ModAdd, ModAddForm},
14 use lemmy_db_views::structs::SiteView;
15 use lemmy_db_views_actor::structs::PersonViewSafe;
16 use lemmy_utils::{version, ConnectionId, LemmyError};
17 use lemmy_websocket::LemmyContext;
19 #[async_trait::async_trait(?Send)]
20 impl Perform for LeaveAdmin {
21 type Response = GetSiteResponse;
23 #[tracing::instrument(skip(context, _websocket_id))]
26 context: &Data<LemmyContext>,
27 _websocket_id: Option<ConnectionId>,
28 ) -> Result<GetSiteResponse, LemmyError> {
29 let data: &LeaveAdmin = self;
31 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
33 is_admin(&local_user_view)?;
35 // Make sure there isn't just one admin (so if one leaves, there will still be one left)
36 let admins = blocking(context.pool(), PersonViewSafe::admins).await??;
37 if admins.len() == 1 {
38 return Err(LemmyError::from_message("cannot_leave_admin"));
41 let person_id = local_user_view.person.id;
42 blocking(context.pool(), move |conn| {
43 Person::leave_admin(conn, person_id)
48 let form = ModAddForm {
49 mod_person_id: person_id,
50 other_person_id: person_id,
54 blocking(context.pool(), move |conn| ModAdd::create(conn, &form)).await??;
56 // Reread site and admins
57 let site_view = blocking(context.pool(), SiteView::read_local).await??;
58 let admins = blocking(context.pool(), PersonViewSafe::admins).await??;
60 let federated_instances =
61 build_federated_instances(context.pool(), &context.settings()).await?;
64 site_view: Some(site_view),
67 version: version::VERSION.to_string(),