2 use actix_web::web::Data;
3 use lemmy_api_common::{
5 site::{GetSiteResponse, LeaveAdmin},
6 utils::{is_admin, local_user_view_from_jwt},
10 actor_language::SiteLanguage,
12 moderator::{ModAdd, ModAddForm},
13 person::{Person, PersonUpdateForm},
18 use lemmy_db_views::structs::{CustomEmojiView, SiteView};
19 use lemmy_db_views_actor::structs::PersonView;
20 use lemmy_utils::{error::LemmyError, version};
22 #[async_trait::async_trait(?Send)]
23 impl Perform for LeaveAdmin {
24 type Response = GetSiteResponse;
26 #[tracing::instrument(skip(context))]
27 async fn perform(&self, context: &Data<LemmyContext>) -> Result<GetSiteResponse, LemmyError> {
28 let data: &LeaveAdmin = self;
29 let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
31 is_admin(&local_user_view)?;
33 // Make sure there isn't just one admin (so if one leaves, there will still be one left)
34 let admins = PersonView::admins(context.pool()).await?;
35 if admins.len() == 1 {
36 return Err(LemmyError::from_message("cannot_leave_admin"));
39 let person_id = local_user_view.person.id;
43 &PersonUpdateForm::builder().admin(Some(false)).build(),
48 let form = ModAddForm {
49 mod_person_id: person_id,
50 other_person_id: person_id,
54 ModAdd::create(context.pool(), &form).await?;
56 // Reread site and admins
57 let site_view = SiteView::read_local(context.pool()).await?;
58 let admins = PersonView::admins(context.pool()).await?;
60 let all_languages = Language::read_all(context.pool()).await?;
61 let discussion_languages = SiteLanguage::read_local_raw(context.pool()).await?;
62 let taglines = Tagline::get_all(context.pool(), site_view.local_site.id).await?;
63 let custom_emojis = CustomEmojiView::get_all(context.pool(), site_view.local_site.id).await?;
68 version: version::VERSION.to_string(),