2022-04-13 18:12:25 +00:00
|
|
|
use crate::Perform;
|
|
|
|
use actix_web::web::Data;
|
|
|
|
use lemmy_api_common::{
|
2022-11-28 14:29:33 +00:00
|
|
|
context::LemmyContext,
|
2022-04-13 18:12:25 +00:00
|
|
|
person::{AddAdmin, AddAdminResponse},
|
2023-05-25 14:50:07 +00:00
|
|
|
utils::{is_admin, local_user_view_from_jwt},
|
2022-04-13 18:12:25 +00:00
|
|
|
};
|
|
|
|
use lemmy_db_schema::{
|
|
|
|
source::{
|
2023-08-24 09:40:08 +00:00
|
|
|
local_user::{LocalUser, LocalUserUpdateForm},
|
2022-04-13 18:12:25 +00:00
|
|
|
moderator::{ModAdd, ModAddForm},
|
|
|
|
},
|
|
|
|
traits::Crud,
|
|
|
|
};
|
2023-03-01 17:19:46 +00:00
|
|
|
use lemmy_db_views_actor::structs::PersonView;
|
2023-07-10 14:50:07 +00:00
|
|
|
use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
|
2022-04-13 18:12:25 +00:00
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl Perform for AddAdmin {
|
|
|
|
type Response = AddAdminResponse;
|
|
|
|
|
2023-06-06 16:27:22 +00:00
|
|
|
#[tracing::instrument(skip(context))]
|
|
|
|
async fn perform(&self, context: &Data<LemmyContext>) -> Result<AddAdminResponse, LemmyError> {
|
2022-04-13 18:12:25 +00:00
|
|
|
let data: &AddAdmin = self;
|
2023-05-25 14:50:07 +00:00
|
|
|
let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
|
2022-04-13 18:12:25 +00:00
|
|
|
|
|
|
|
// Make sure user is an admin
|
|
|
|
is_admin(&local_user_view)?;
|
|
|
|
|
2023-08-24 09:40:08 +00:00
|
|
|
let added_admin = LocalUser::update(
|
2023-07-11 13:09:59 +00:00
|
|
|
&mut context.pool(),
|
2023-08-24 09:40:08 +00:00
|
|
|
data.local_user_id,
|
|
|
|
&LocalUserUpdateForm {
|
|
|
|
admin: Some(data.added),
|
2023-08-08 09:41:41 +00:00
|
|
|
..Default::default()
|
|
|
|
},
|
2022-11-09 10:05:00 +00:00
|
|
|
)
|
|
|
|
.await
|
2023-07-10 14:50:07 +00:00
|
|
|
.with_lemmy_type(LemmyErrorType::CouldntUpdateUser)?;
|
2022-04-13 18:12:25 +00:00
|
|
|
|
|
|
|
// Mod tables
|
|
|
|
let form = ModAddForm {
|
|
|
|
mod_person_id: local_user_view.person.id,
|
2023-08-24 09:40:08 +00:00
|
|
|
other_person_id: added_admin.person_id,
|
2022-04-13 18:12:25 +00:00
|
|
|
removed: Some(!data.added),
|
|
|
|
};
|
|
|
|
|
2023-07-11 13:09:59 +00:00
|
|
|
ModAdd::create(&mut context.pool(), &form).await?;
|
2022-04-13 18:12:25 +00:00
|
|
|
|
2023-07-11 13:09:59 +00:00
|
|
|
let admins = PersonView::admins(&mut context.pool()).await?;
|
2022-04-13 18:12:25 +00:00
|
|
|
|
2023-06-06 16:27:22 +00:00
|
|
|
Ok(AddAdminResponse { admins })
|
2022-04-13 18:12:25 +00:00
|
|
|
}
|
|
|
|
}
|