2 use actix_web::web::Data;
3 use lemmy_api_common::{
5 person::{AddAdmin, AddAdminResponse},
6 utils::{is_admin, local_user_view_from_jwt},
7 websocket::UserOperation,
11 moderator::{ModAdd, ModAddForm},
12 person::{Person, PersonUpdateForm},
16 use lemmy_db_views_actor::structs::PersonView;
17 use lemmy_utils::{error::LemmyError, ConnectionId};
19 #[async_trait::async_trait(?Send)]
20 impl Perform for AddAdmin {
21 type Response = AddAdminResponse;
23 #[tracing::instrument(skip(context, websocket_id))]
26 context: &Data<LemmyContext>,
27 websocket_id: Option<ConnectionId>,
28 ) -> Result<AddAdminResponse, LemmyError> {
29 let data: &AddAdmin = self;
30 let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
32 // Make sure user is an admin
33 is_admin(&local_user_view)?;
35 let added = data.added;
36 let added_person_id = data.person_id;
37 let added_admin = Person::update(
40 &PersonUpdateForm::builder().admin(Some(added)).build(),
43 .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_user"))?;
46 let form = ModAddForm {
47 mod_person_id: local_user_view.person.id,
48 other_person_id: added_admin.id,
49 removed: Some(!data.added),
52 ModAdd::create(context.pool(), &form).await?;
54 let admins = PersonView::admins(context.pool()).await?;
56 let res = AddAdminResponse { admins };
58 context.send_all_ws_message(&UserOperation::AddAdmin, &res, websocket_id)?;