lemmy/crates/apub/src/api/resolve_object.rs

65 lines
2.2 KiB
Rust
Raw Normal View History

use crate::fetcher::search::{search_query_to_object_id, SearchableObjects};
use activitypub_federation::config::Data;
use actix_web::web::{Json, Query};
2022-04-13 18:12:25 +00:00
use diesel::NotFound;
use lemmy_api_common::{
context::LemmyContext,
2022-04-13 18:12:25 +00:00
site::{ResolveObject, ResolveObjectResponse},
utils::{check_private_instance, local_user_view_from_jwt},
2022-04-13 18:12:25 +00:00
};
use lemmy_db_schema::{newtypes::PersonId, source::local_site::LocalSite, utils::DbPool};
use lemmy_db_views::structs::{CommentView, PostView};
use lemmy_db_views_actor::structs::{CommunityView, PersonView};
use lemmy_utils::error::LemmyError;
2022-04-13 18:12:25 +00:00
#[tracing::instrument(skip(context))]
pub async fn resolve_object(
data: Query<ResolveObject>,
context: Data<LemmyContext>,
) -> Result<Json<ResolveObjectResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
let local_site = LocalSite::read(context.pool()).await?;
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
let res = search_query_to_object_id(&data.q, &context)
.await
.map_err(|e| e.with_message("couldnt_find_object"))?;
convert_response(res, person_id, context.pool())
.await
.map_err(|e| e.with_message("couldnt_find_object"))
2022-04-13 18:12:25 +00:00
}
async fn convert_response(
object: SearchableObjects,
user_id: PersonId,
2022-04-13 18:12:25 +00:00
pool: &DbPool,
) -> Result<Json<ResolveObjectResponse>, LemmyError> {
use SearchableObjects::*;
2022-04-13 18:12:25 +00:00
let removed_or_deleted;
let mut res = ResolveObjectResponse::default();
2022-04-13 18:12:25 +00:00
match object {
Person(p) => {
removed_or_deleted = p.deleted;
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;
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;
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;
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(Json(res))
2022-04-13 18:12:25 +00:00
}