2 use actix_web::web::Data;
3 use lemmy_api_common::{
5 get_local_user_view_from_jwt,
6 person::{BlockPerson, BlockPersonResponse},
9 source::person_block::{PersonBlock, PersonBlockForm},
12 use lemmy_db_views_actor::person_view::PersonViewSafe;
13 use lemmy_utils::{ConnectionId, LemmyError};
14 use lemmy_websocket::LemmyContext;
16 #[async_trait::async_trait(?Send)]
17 impl Perform for BlockPerson {
18 type Response = BlockPersonResponse;
20 #[tracing::instrument(skip(context, _websocket_id))]
23 context: &Data<LemmyContext>,
24 _websocket_id: Option<ConnectionId>,
25 ) -> Result<BlockPersonResponse, LemmyError> {
26 let data: &BlockPerson = self;
28 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
30 let target_id = data.person_id;
31 let person_id = local_user_view.person.id;
33 // Don't let a person block themselves
34 if target_id == person_id {
35 return Err(LemmyError::from_message("cant_block_yourself"));
38 let person_block_form = PersonBlockForm {
44 let block = move |conn: &'_ _| PersonBlock::block(conn, &person_block_form);
45 blocking(context.pool(), block)
47 .map_err(|e| LemmyError::from_error_message(e, "person_block_already_exists"))?;
49 let unblock = move |conn: &'_ _| PersonBlock::unblock(conn, &person_block_form);
50 blocking(context.pool(), unblock)
52 .map_err(|e| LemmyError::from_error_message(e, "person_block_already_exists"))?;
55 let person_view = blocking(context.pool(), move |conn| {
56 PersonViewSafe::read(conn, target_id)
60 let res = BlockPersonResponse {