2023-09-05 09:33:46 +00:00
|
|
|
use actix_web::web::{Data, Json};
|
2022-04-13 18:12:25 +00:00
|
|
|
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-09-06 09:37:03 +00:00
|
|
|
use lemmy_db_views::structs::LocalUserView;
|
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
|
|
|
|
2023-09-05 09:33:46 +00:00
|
|
|
#[tracing::instrument(skip(context))]
|
|
|
|
pub async fn add_admin(
|
|
|
|
data: Json<AddAdmin>,
|
|
|
|
context: Data<LemmyContext>,
|
|
|
|
) -> Result<Json<AddAdminResponse>, LemmyError> {
|
|
|
|
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
|
|
|
|
|
|
|
|
// 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?;
|
|
|
|
|
|
|
|
let admins = PersonView::admins(&mut context.pool()).await?;
|
|
|
|
|
|
|
|
Ok(Json(AddAdminResponse { admins }))
|
2022-04-13 18:12:25 +00:00
|
|
|
}
|