2 use actix_web::web::Data;
3 use lemmy_api_common::{
5 build_federated_instances,
6 get_local_user_view_from_jwt,
8 site::{GetSiteResponse, LeaveAdmin},
10 use lemmy_db_schema::{
12 moderator::{ModAdd, ModAddForm},
17 use lemmy_db_views::site_view::SiteView;
18 use lemmy_db_views_actor::person_view::PersonViewSafe;
19 use lemmy_utils::{version, ConnectionId, LemmyError};
20 use lemmy_websocket::LemmyContext;
22 #[async_trait::async_trait(?Send)]
23 impl Perform for LeaveAdmin {
24 type Response = GetSiteResponse;
26 #[tracing::instrument(skip(context, _websocket_id))]
29 context: &Data<LemmyContext>,
30 _websocket_id: Option<ConnectionId>,
31 ) -> Result<GetSiteResponse, LemmyError> {
32 let data: &LeaveAdmin = self;
34 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
36 is_admin(&local_user_view)?;
38 // Make sure there isn't just one admin (so if one leaves, there will still be one left)
39 let admins = blocking(context.pool(), PersonViewSafe::admins).await??;
40 if admins.len() == 1 {
41 return Err(LemmyError::from_message("cannot_leave_admin"));
44 let person_id = local_user_view.person.id;
45 blocking(context.pool(), move |conn| {
46 Person::leave_admin(conn, person_id)
51 let form = ModAddForm {
52 mod_person_id: person_id,
53 other_person_id: person_id,
57 blocking(context.pool(), move |conn| ModAdd::create(conn, &form)).await??;
59 // Reread site and admins
60 let site_view = blocking(context.pool(), SiteView::read_local).await??;
61 let admins = blocking(context.pool(), PersonViewSafe::admins).await??;
63 let federated_instances =
64 build_federated_instances(context.pool(), &context.settings()).await?;
67 site_view: Some(site_view),
70 version: version::VERSION.to_string(),