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::{ConnectionId, LemmyError};
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 {
43 let block = move |conn: &'_ _| PersonBlock::block(conn, &person_block_form);
44 blocking(context.pool(), block)
46 .map_err(|e| LemmyError::from_error_message(e, "person_block_already_exists"))?;
48 let unblock = move |conn: &'_ _| PersonBlock::unblock(conn, &person_block_form);
49 blocking(context.pool(), unblock)
51 .map_err(|e| LemmyError::from_error_message(e, "person_block_already_exists"))?;
54 let person_view = blocking(context.pool(), move |conn| {
55 PersonViewSafe::read(conn, target_id)
59 let res = BlockPersonResponse {