2 use actix_web::web::Data;
4 use lemmy_api_common::{
5 site::{ResolveObject, ResolveObjectResponse},
6 utils::{check_private_instance, get_local_user_view_from_jwt_opt},
8 use lemmy_apub::fetcher::search::{search_query_to_object_id, SearchableObjects};
9 use lemmy_db_schema::{newtypes::PersonId, source::local_site::LocalSite, utils::DbPool};
10 use lemmy_db_views::structs::{CommentView, PostView};
11 use lemmy_db_views_actor::structs::{CommunityView, PersonViewSafe};
12 use lemmy_utils::{error::LemmyError, ConnectionId};
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 let local_site = LocalSite::read(context.pool()).await?;
29 check_private_instance(&local_user_view, &local_site)?;
31 let res = search_query_to_object_id(&self.q, local_user_view.is_none(), context)
33 .map_err(|e| e.with_message("couldnt_find_object"))?;
34 convert_response(res, local_user_view.map(|l| l.person.id), context.pool())
36 .map_err(|e| e.with_message("couldnt_find_object"))
40 async fn convert_response(
41 object: SearchableObjects,
42 user_id: Option<PersonId>,
44 ) -> Result<ResolveObjectResponse, LemmyError> {
45 use SearchableObjects::*;
46 let removed_or_deleted;
47 let mut res = ResolveObjectResponse {
55 removed_or_deleted = p.deleted;
56 res.person = Some(PersonViewSafe::read(pool, p.id).await?)
59 removed_or_deleted = c.deleted || c.removed;
60 res.community = Some(CommunityView::read(pool, c.id, user_id).await?)
63 removed_or_deleted = p.deleted || p.removed;
64 res.post = Some(PostView::read(pool, p.id, user_id).await?)
67 removed_or_deleted = c.deleted || c.removed;
68 res.comment = Some(CommentView::read(pool, 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());