2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 person::{BlockPerson, BlockPersonResponse},
5 utils::{blocking, get_local_user_view_from_jwt},
8 source::person_block::{PersonBlock, PersonBlockForm},
11 use lemmy_db_views_actor::structs::PersonViewSafe;
12 use lemmy_utils::{error::LemmyError, ConnectionId};
13 use lemmy_websocket::LemmyContext;
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 = blocking(context.pool(), move |conn| {
43 PersonViewSafe::read(conn, target_id)
47 if target_person_view.person.admin {
48 return Err(LemmyError::from_message("cant_block_admin"));
52 let block = move |conn: &'_ _| PersonBlock::block(conn, &person_block_form);
53 blocking(context.pool(), block)
55 .map_err(|e| LemmyError::from_error_message(e, "person_block_already_exists"))?;
57 let unblock = move |conn: &'_ _| PersonBlock::unblock(conn, &person_block_form);
58 blocking(context.pool(), unblock)
60 .map_err(|e| LemmyError::from_error_message(e, "person_block_already_exists"))?;
63 let res = BlockPersonResponse {
64 person_view: target_person_view,