2 use actix_web::web::Data;
4 use lemmy_api_common::{
5 site::{ResolveObject, ResolveObjectResponse},
6 utils::{blocking, check_private_instance, get_local_user_view_from_jwt_opt},
8 use lemmy_apub::fetcher::search::{search_by_apub_id, SearchableObjects};
9 use lemmy_db_schema::{newtypes::PersonId, utils::DbPool};
10 use lemmy_db_views::structs::{CommentView, PostView};
11 use lemmy_db_views_actor::structs::{CommunityView, PersonViewSafe};
12 use lemmy_utils::{ConnectionId, LemmyError};
13 use lemmy_websocket::LemmyContext;
15 #[async_trait::async_trait(?Send)]
16 impl Perform for ResolveObject {
17 type Response = ResolveObjectResponse;
19 #[tracing::instrument(skip(context, _websocket_id))]
22 context: &Data<LemmyContext>,
23 _websocket_id: Option<ConnectionId>,
24 ) -> Result<ResolveObjectResponse, LemmyError> {
26 get_local_user_view_from_jwt_opt(self.auth.as_ref(), context.pool(), context.secret())
28 check_private_instance(&local_user_view, context.pool()).await?;
30 let res = search_by_apub_id(&self.q, context)
32 .map_err(|e| e.with_message("couldnt_find_object"))?;
33 convert_response(res, local_user_view.map(|l| l.person.id), context.pool())
35 .map_err(|e| e.with_message("couldnt_find_object"))
39 async fn convert_response(
40 object: SearchableObjects,
41 user_id: Option<PersonId>,
43 ) -> Result<ResolveObjectResponse, LemmyError> {
44 let removed_or_deleted;
45 let mut res = ResolveObjectResponse {
51 use SearchableObjects::*;
54 removed_or_deleted = p.deleted;
55 res.person = Some(blocking(pool, move |conn| PersonViewSafe::read(conn, p.id)).await??)
58 removed_or_deleted = c.deleted || c.removed;
60 Some(blocking(pool, move |conn| CommunityView::read(conn, c.id, user_id)).await??)
63 removed_or_deleted = p.deleted || p.removed;
64 res.post = Some(blocking(pool, move |conn| PostView::read(conn, p.id, user_id)).await??)
67 removed_or_deleted = c.deleted || c.removed;
68 res.comment = Some(blocking(pool, move |conn| CommentView::read(conn, c.id, user_id)).await??)
71 // if the object was deleted from database, dont return it
72 if removed_or_deleted {
73 return Err(NotFound {}.into());