2022-11-28 14:29:33 +00:00
|
|
|
use crate::{
|
|
|
|
api::PerformApub,
|
|
|
|
fetcher::search::{search_query_to_object_id, SearchableObjects},
|
|
|
|
};
|
2023-03-21 15:03:05 +00:00
|
|
|
use activitypub_federation::config::Data;
|
2022-04-13 18:12:25 +00:00
|
|
|
use diesel::NotFound;
|
|
|
|
use lemmy_api_common::{
|
2022-11-28 14:29:33 +00:00
|
|
|
context::LemmyContext,
|
2022-04-13 18:12:25 +00:00
|
|
|
site::{ResolveObject, ResolveObjectResponse},
|
2023-05-25 14:50:07 +00:00
|
|
|
utils::{check_private_instance, local_user_view_from_jwt},
|
2022-04-13 18:12:25 +00:00
|
|
|
};
|
2022-10-27 09:24:07 +00:00
|
|
|
use lemmy_db_schema::{newtypes::PersonId, source::local_site::LocalSite, utils::DbPool};
|
2022-05-03 17:44:13 +00:00
|
|
|
use lemmy_db_views::structs::{CommentView, PostView};
|
2023-03-01 17:19:46 +00:00
|
|
|
use lemmy_db_views_actor::structs::{CommunityView, PersonView};
|
2023-06-06 16:27:22 +00:00
|
|
|
use lemmy_utils::error::LemmyError;
|
2022-04-13 18:12:25 +00:00
|
|
|
|
2023-03-21 15:03:05 +00:00
|
|
|
#[async_trait::async_trait]
|
2022-11-28 14:29:33 +00:00
|
|
|
impl PerformApub for ResolveObject {
|
2022-04-13 18:12:25 +00:00
|
|
|
type Response = ResolveObjectResponse;
|
|
|
|
|
2023-06-06 16:27:22 +00:00
|
|
|
#[tracing::instrument(skip(context))]
|
2022-04-13 18:12:25 +00:00
|
|
|
async fn perform(
|
|
|
|
&self,
|
|
|
|
context: &Data<LemmyContext>,
|
|
|
|
) -> Result<ResolveObjectResponse, LemmyError> {
|
2023-05-25 14:50:07 +00:00
|
|
|
let local_user_view = local_user_view_from_jwt(&self.auth, context).await?;
|
2022-11-09 10:05:00 +00:00
|
|
|
let local_site = LocalSite::read(context.pool()).await?;
|
2023-03-21 15:03:05 +00:00
|
|
|
let person_id = local_user_view.person.id;
|
|
|
|
check_private_instance(&Some(local_user_view), &local_site)?;
|
2022-04-13 18:12:25 +00:00
|
|
|
|
2023-03-21 15:03:05 +00:00
|
|
|
let res = search_query_to_object_id(&self.q, context)
|
2022-04-13 18:12:25 +00:00
|
|
|
.await
|
|
|
|
.map_err(|e| e.with_message("couldnt_find_object"))?;
|
2023-03-21 15:03:05 +00:00
|
|
|
convert_response(res, person_id, context.pool())
|
2022-04-13 18:12:25 +00:00
|
|
|
.await
|
|
|
|
.map_err(|e| e.with_message("couldnt_find_object"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn convert_response(
|
|
|
|
object: SearchableObjects,
|
2023-03-21 15:03:05 +00:00
|
|
|
user_id: PersonId,
|
2022-04-13 18:12:25 +00:00
|
|
|
pool: &DbPool,
|
|
|
|
) -> Result<ResolveObjectResponse, LemmyError> {
|
2022-11-19 04:33:54 +00:00
|
|
|
use SearchableObjects::*;
|
2022-04-13 18:12:25 +00:00
|
|
|
let removed_or_deleted;
|
2023-01-20 17:43:23 +00:00
|
|
|
let mut res = ResolveObjectResponse::default();
|
2022-04-13 18:12:25 +00:00
|
|
|
match object {
|
|
|
|
Person(p) => {
|
|
|
|
removed_or_deleted = p.deleted;
|
2023-03-01 17:19:46 +00:00
|
|
|
res.person = Some(PersonView::read(pool, p.id).await?)
|
2022-04-13 18:12:25 +00:00
|
|
|
}
|
|
|
|
Community(c) => {
|
|
|
|
removed_or_deleted = c.deleted || c.removed;
|
2023-03-21 15:03:05 +00:00
|
|
|
res.community = Some(CommunityView::read(pool, c.id, Some(user_id), None).await?)
|
2022-04-13 18:12:25 +00:00
|
|
|
}
|
|
|
|
Post(p) => {
|
|
|
|
removed_or_deleted = p.deleted || p.removed;
|
2023-03-21 15:03:05 +00:00
|
|
|
res.post = Some(PostView::read(pool, p.id, Some(user_id), None).await?)
|
2022-04-13 18:12:25 +00:00
|
|
|
}
|
|
|
|
Comment(c) => {
|
|
|
|
removed_or_deleted = c.deleted || c.removed;
|
2023-03-21 15:03:05 +00:00
|
|
|
res.comment = Some(CommentView::read(pool, c.id, Some(user_id)).await?)
|
2022-04-13 18:12:25 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
// if the object was deleted from database, dont return it
|
|
|
|
if removed_or_deleted {
|
|
|
|
return Err(NotFound {}.into());
|
|
|
|
}
|
|
|
|
Ok(res)
|
|
|
|
}
|