2023-09-05 09:33:46 +00:00
|
|
|
use actix_web::web::{Data, Json};
|
2023-10-19 17:21:05 +00:00
|
|
|
use lemmy_api_common::{context::LemmyContext, person::AddAdmin, utils::is_admin, SuccessResponse};
|
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-09-06 09:37:03 +00:00
|
|
|
use lemmy_db_views::structs::LocalUserView;
|
2023-07-10 14:50:07 +00:00
|
|
|
use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
|
2022-04-13 18:12:25 +00:00
|
|
|
|
2023-09-05 09:33:46 +00:00
|
|
|
#[tracing::instrument(skip(context))]
|
|
|
|
pub async fn add_admin(
|
|
|
|
data: Json<AddAdmin>,
|
|
|
|
context: Data<LemmyContext>,
|
2023-09-21 10:42:28 +00:00
|
|
|
local_user_view: LocalUserView,
|
2023-10-19 17:21:05 +00:00
|
|
|
) -> Result<Json<SuccessResponse>, LemmyError> {
|
2023-09-05 09:33:46 +00:00
|
|
|
// Make sure user is an admin
|
|
|
|
is_admin(&local_user_view)?;
|
|
|
|
|
2023-09-06 09:37:03 +00:00
|
|
|
// Make sure that the person_id added is local
|
|
|
|
let added_local_user = LocalUserView::read_person(&mut context.pool(), data.person_id)
|
|
|
|
.await
|
|
|
|
.with_lemmy_type(LemmyErrorType::ObjectNotLocal)?;
|
|
|
|
|
2023-09-05 09:33:46 +00:00
|
|
|
let added_admin = LocalUser::update(
|
|
|
|
&mut context.pool(),
|
2023-09-06 09:37:03 +00:00
|
|
|
added_local_user.local_user.id,
|
2023-09-05 09:33:46 +00:00
|
|
|
&LocalUserUpdateForm {
|
|
|
|
admin: Some(data.added),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.with_lemmy_type(LemmyErrorType::CouldntUpdateUser)?;
|
|
|
|
|
|
|
|
// Mod tables
|
|
|
|
let form = ModAddForm {
|
|
|
|
mod_person_id: local_user_view.person.id,
|
|
|
|
other_person_id: added_admin.person_id,
|
|
|
|
removed: Some(!data.added),
|
|
|
|
};
|
|
|
|
|
|
|
|
ModAdd::create(&mut context.pool(), &form).await?;
|
|
|
|
|
2023-10-19 17:21:05 +00:00
|
|
|
Ok(Json(SuccessResponse::default()))
|
2022-04-13 18:12:25 +00:00
|
|
|
}
|