2 use actix_web::web::Data;
3 use lemmy_api_common::{
5 person::{BlockPerson, BlockPersonResponse},
6 utils::get_local_user_view_from_jwt,
9 source::person_block::{PersonBlock, PersonBlockForm},
12 use lemmy_db_views_actor::structs::PersonView;
13 use lemmy_utils::{error::LemmyError, ConnectionId};
15 #[async_trait::async_trait(?Send)]
16 impl Perform for BlockPerson {
17 type Response = BlockPersonResponse;
19 #[tracing::instrument(skip(context, _websocket_id))]
22 context: &Data<LemmyContext>,
23 _websocket_id: Option<ConnectionId>,
24 ) -> Result<BlockPersonResponse, LemmyError> {
25 let data: &BlockPerson = self;
27 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
29 let target_id = data.person_id;
30 let person_id = local_user_view.person.id;
32 // Don't let a person block themselves
33 if target_id == person_id {
34 return Err(LemmyError::from_message("cant_block_yourself"));
37 let person_block_form = PersonBlockForm {
42 let target_person_view = PersonView::read(context.pool(), target_id).await?;
44 if target_person_view.person.admin {
45 return Err(LemmyError::from_message("cant_block_admin"));
49 PersonBlock::block(context.pool(), &person_block_form)
51 .map_err(|e| LemmyError::from_error_message(e, "person_block_already_exists"))?;
53 PersonBlock::unblock(context.pool(), &person_block_form)
55 .map_err(|e| LemmyError::from_error_message(e, "person_block_already_exists"))?;
58 let res = BlockPersonResponse {
59 person_view: target_person_view,