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, 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;
33 let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
35 is_admin(&local_user_view)?;
37 // Make sure there isn't just one admin (so if one leaves, there will still be one left)
38 let admins = PersonView::admins(context.pool()).await?;
39 if admins.len() == 1 {
40 return Err(LemmyError::from_message("cannot_leave_admin"));
43 let person_id = local_user_view.person.id;
47 &PersonUpdateForm::builder().admin(Some(false)).build(),
52 let form = ModAddForm {
53 mod_person_id: person_id,
54 other_person_id: person_id,
58 ModAdd::create(context.pool(), &form).await?;
60 // Reread site and admins
61 let site_view = SiteView::read_local(context.pool()).await?;
62 let admins = PersonView::admins(context.pool()).await?;
64 let all_languages = Language::read_all(context.pool()).await?;
65 let discussion_languages = SiteLanguage::read_local_raw(context.pool()).await?;
66 let taglines = Tagline::get_all(context.pool(), site_view.local_site.id).await?;
67 let custom_emojis = CustomEmojiView::get_all(context.pool(), site_view.local_site.id).await?;
73 version: version::VERSION.to_string(),