2 use actix_web::web::Data;
3 use lemmy_api_common::{
5 site::{GetSiteResponse, LeaveAdmin},
6 utils::{get_local_user_view_from_jwt, is_admin},
10 actor_language::SiteLanguage,
12 moderator::{ModAdd, ModAddForm},
13 person::{Person, PersonUpdateForm},
18 use lemmy_db_views::structs::SiteView;
19 use lemmy_db_views_actor::structs::PersonView;
20 use lemmy_utils::{error::LemmyError, version, ConnectionId};
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 = PersonView::admins(context.pool()).await?;
40 if admins.len() == 1 {
41 return Err(LemmyError::from_message("cannot_leave_admin"));
44 let person_id = local_user_view.person.id;
48 &PersonUpdateForm::builder().admin(Some(false)).build(),
53 let form = ModAddForm {
54 mod_person_id: person_id,
55 other_person_id: person_id,
59 ModAdd::create(context.pool(), &form).await?;
61 // Reread site and admins
62 let site_view = SiteView::read_local(context.pool()).await?;
63 let admins = PersonView::admins(context.pool()).await?;
65 let all_languages = Language::read_all(context.pool()).await?;
66 let discussion_languages = SiteLanguage::read_local(context.pool()).await?;
67 let taglines_res = Tagline::get_all(context.pool(), site_view.local_site.id).await?;
68 let taglines = taglines_res.is_empty().then_some(taglines_res);
74 version: version::VERSION.to_string(),
76 federated_instances: None,