2 use actix_web::web::Data;
4 use lemmy_api_common::{
6 check_private_instance,
7 get_local_user_view_from_jwt_opt,
8 site::{ResolveObject, ResolveObjectResponse},
10 use lemmy_apub::fetcher::search::{search_by_apub_id, SearchableObjects};
11 use lemmy_db_schema::{newtypes::PersonId, DbPool};
12 use lemmy_db_views::{comment_view::CommentView, post_view::PostView};
13 use lemmy_db_views_actor::{community_view::CommunityView, person_view::PersonViewSafe};
14 use lemmy_utils::{ConnectionId, LemmyError};
15 use lemmy_websocket::LemmyContext;
17 #[async_trait::async_trait(?Send)]
18 impl Perform for ResolveObject {
19 type Response = ResolveObjectResponse;
21 #[tracing::instrument(skip(context, _websocket_id))]
24 context: &Data<LemmyContext>,
25 _websocket_id: Option<ConnectionId>,
26 ) -> Result<ResolveObjectResponse, LemmyError> {
28 get_local_user_view_from_jwt_opt(self.auth.as_ref(), context.pool(), context.secret())
30 check_private_instance(&local_user_view, context.pool()).await?;
32 let res = search_by_apub_id(&self.q, context)
34 .map_err(|e| e.with_message("couldnt_find_object"))?;
35 convert_response(res, local_user_view.map(|l| l.person.id), context.pool())
37 .map_err(|e| e.with_message("couldnt_find_object"))
41 async fn convert_response(
42 object: SearchableObjects,
43 user_id: Option<PersonId>,
45 ) -> Result<ResolveObjectResponse, LemmyError> {
46 let removed_or_deleted;
47 let mut res = ResolveObjectResponse {
53 use SearchableObjects::*;
56 removed_or_deleted = p.deleted;
57 res.person = Some(blocking(pool, move |conn| PersonViewSafe::read(conn, p.id)).await??)
60 removed_or_deleted = c.deleted || c.removed;
62 Some(blocking(pool, move |conn| CommunityView::read(conn, c.id, user_id)).await??)
65 removed_or_deleted = p.deleted || p.removed;
66 res.post = Some(blocking(pool, move |conn| PostView::read(conn, p.id, user_id)).await??)
69 removed_or_deleted = c.deleted || c.removed;
70 res.comment = Some(blocking(pool, move |conn| CommentView::read(conn, c.id, user_id)).await??)
73 // if the object was deleted from database, dont return it
74 if removed_or_deleted {
75 return Err(NotFound {}.into());