2 use actix_web::web::Data;
3 use lemmy_api_common::{
5 person::{AddAdmin, AddAdminResponse},
6 utils::{is_admin, local_user_view_from_jwt},
10 moderator::{ModAdd, ModAddForm},
11 person::{Person, PersonUpdateForm},
15 use lemmy_db_views_actor::structs::PersonView;
16 use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
18 #[async_trait::async_trait(?Send)]
19 impl Perform for AddAdmin {
20 type Response = AddAdminResponse;
22 #[tracing::instrument(skip(context))]
23 async fn perform(&self, context: &Data<LemmyContext>) -> Result<AddAdminResponse, LemmyError> {
24 let data: &AddAdmin = self;
25 let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
27 // Make sure user is an admin
28 is_admin(&local_user_view)?;
30 let added = data.added;
31 let added_person_id = data.person_id;
32 let added_admin = Person::update(
35 &PersonUpdateForm::builder().admin(Some(added)).build(),
38 .with_lemmy_type(LemmyErrorType::CouldntUpdateUser)?;
41 let form = ModAddForm {
42 mod_person_id: local_user_view.person.id,
43 other_person_id: added_admin.id,
44 removed: Some(!data.added),
47 ModAdd::create(&mut context.pool(), &form).await?;
49 let admins = PersonView::admins(&mut context.pool()).await?;
51 Ok(AddAdminResponse { admins })