2024-03-15 12:42:09 +00:00
|
|
|
use crate::fetcher::{
|
|
|
|
search::{search_query_to_object_id, search_query_to_object_id_local, SearchableObjects},
|
|
|
|
user_or_community::UserOrCommunity,
|
2023-07-26 16:17:42 +00:00
|
|
|
};
|
2023-03-21 15:03:05 +00:00
|
|
|
use activitypub_federation::config::Data;
|
2023-07-03 09:01:41 +00:00
|
|
|
use actix_web::web::{Json, Query};
|
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-09-21 10:42:28 +00:00
|
|
|
utils::check_private_instance,
|
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};
|
2023-08-29 14:47:57 +00:00
|
|
|
use lemmy_db_views::structs::{CommentView, LocalUserView, PostView};
|
2023-03-01 17:19:46 +00:00
|
|
|
use lemmy_db_views_actor::structs::{CommunityView, PersonView};
|
2023-07-10 14:50:07 +00:00
|
|
|
use lemmy_utils::error::{LemmyError, LemmyErrorExt2, LemmyErrorType};
|
2022-04-13 18:12:25 +00:00
|
|
|
|
2023-07-03 09:01:41 +00:00
|
|
|
#[tracing::instrument(skip(context))]
|
|
|
|
pub async fn resolve_object(
|
|
|
|
data: Query<ResolveObject>,
|
|
|
|
context: Data<LemmyContext>,
|
2023-09-21 10:42:28 +00:00
|
|
|
local_user_view: Option<LocalUserView>,
|
2023-07-03 09:01:41 +00:00
|
|
|
) -> Result<Json<ResolveObjectResponse>, LemmyError> {
|
2023-07-11 13:09:59 +00:00
|
|
|
let local_site = LocalSite::read(&mut context.pool()).await?;
|
2023-07-26 16:17:42 +00:00
|
|
|
check_private_instance(&local_user_view, &local_site)?;
|
|
|
|
let person_id = local_user_view.map(|v| v.person.id);
|
|
|
|
// If we get a valid personId back we can safely assume that the user is authenticated,
|
|
|
|
// if there's no personId then the JWT was missing or invalid.
|
|
|
|
let is_authenticated = person_id.is_some();
|
|
|
|
|
|
|
|
let res = if is_authenticated {
|
|
|
|
// user is fully authenticated; allow remote lookups as well.
|
2024-03-15 12:42:09 +00:00
|
|
|
search_query_to_object_id(data.q.clone(), &context).await
|
2023-07-26 16:17:42 +00:00
|
|
|
} else {
|
|
|
|
// user isn't authenticated only allow a local search.
|
|
|
|
search_query_to_object_id_local(&data.q, &context).await
|
|
|
|
}
|
|
|
|
.with_lemmy_type(LemmyErrorType::CouldntFindObject)?;
|
2022-04-13 18:12:25 +00:00
|
|
|
|
2023-07-11 13:09:59 +00:00
|
|
|
convert_response(res, person_id, &mut context.pool())
|
2023-07-03 09:01:41 +00:00
|
|
|
.await
|
2023-07-10 14:50:07 +00:00
|
|
|
.with_lemmy_type(LemmyErrorType::CouldntFindObject)
|
2022-04-13 18:12:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn convert_response(
|
|
|
|
object: SearchableObjects,
|
2023-07-26 16:17:42 +00:00
|
|
|
user_id: Option<PersonId>,
|
2023-07-11 13:09:59 +00:00
|
|
|
pool: &mut DbPool<'_>,
|
2023-07-03 09:01:41 +00:00
|
|
|
) -> Result<Json<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 {
|
|
|
|
Post(p) => {
|
|
|
|
removed_or_deleted = p.deleted || p.removed;
|
2023-08-11 09:13:14 +00:00
|
|
|
res.post = Some(PostView::read(pool, p.id, user_id, false).await?)
|
2022-04-13 18:12:25 +00:00
|
|
|
}
|
|
|
|
Comment(c) => {
|
|
|
|
removed_or_deleted = c.deleted || c.removed;
|
2023-07-26 16:17:42 +00:00
|
|
|
res.comment = Some(CommentView::read(pool, c.id, user_id).await?)
|
2022-04-13 18:12:25 +00:00
|
|
|
}
|
2024-03-15 12:42:09 +00:00
|
|
|
PersonOrCommunity(p) => match *p {
|
|
|
|
UserOrCommunity::User(u) => {
|
|
|
|
removed_or_deleted = u.deleted;
|
|
|
|
res.person = Some(PersonView::read(pool, u.id).await?)
|
|
|
|
}
|
|
|
|
UserOrCommunity::Community(c) => {
|
|
|
|
removed_or_deleted = c.deleted || c.removed;
|
|
|
|
res.community = Some(CommunityView::read(pool, c.id, user_id, false).await?)
|
|
|
|
}
|
|
|
|
},
|
2022-04-13 18:12:25 +00:00
|
|
|
};
|
|
|
|
// if the object was deleted from database, dont return it
|
|
|
|
if removed_or_deleted {
|
2023-08-31 13:01:08 +00:00
|
|
|
Err(NotFound {}.into())
|
|
|
|
} else {
|
|
|
|
Ok(Json(res))
|
2022-04-13 18:12:25 +00:00
|
|
|
}
|
|
|
|
}
|