This commit is contained in:
Felix Ableitner 2024-03-06 15:33:16 +01:00
parent c2249c009a
commit 9da28935e0
2 changed files with 27 additions and 35 deletions

View file

@ -1,7 +1,6 @@
use crate::fetcher::search::{ use crate::fetcher::{
search_query_to_object_id, search::{search_query_to_object_id, search_query_to_object_id_local, SearchableObjects},
search_query_to_object_id_local, user_or_community::UserOrCommunity,
SearchableObjects,
}; };
use activitypub_federation::config::Data; use activitypub_federation::config::Data;
use actix_web::web::{Json, Query}; use actix_web::web::{Json, Query};
@ -15,7 +14,6 @@ use lemmy_db_schema::{newtypes::PersonId, source::local_site::LocalSite, utils::
use lemmy_db_views::structs::{CommentView, LocalUserView, PostView}; use lemmy_db_views::structs::{CommentView, LocalUserView, PostView};
use lemmy_db_views_actor::structs::{CommunityView, PersonView}; use lemmy_db_views_actor::structs::{CommunityView, PersonView};
use lemmy_utils::error::{LemmyError, LemmyErrorExt2, LemmyErrorType}; use lemmy_utils::error::{LemmyError, LemmyErrorExt2, LemmyErrorType};
use crate::fetcher::user_or_community::UserOrCommunity;
#[tracing::instrument(skip(context))] #[tracing::instrument(skip(context))]
pub async fn resolve_object( pub async fn resolve_object(
@ -61,18 +59,16 @@ async fn convert_response(
removed_or_deleted = c.deleted || c.removed; removed_or_deleted = c.deleted || c.removed;
res.comment = Some(CommentView::read(pool, c.id, user_id).await?) res.comment = Some(CommentView::read(pool, c.id, user_id).await?)
} }
PersonOrCommunity(p) => { PersonOrCommunity(p) => match p {
match p { UserOrCommunity::User(u) => {
UserOrCommunity::User(u) => { removed_or_deleted = u.deleted;
removed_or_deleted = u.deleted; res.person = Some(PersonView::read(pool, u.id).await?)
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?)
}
} }
} UserOrCommunity::Community(c) => {
removed_or_deleted = c.deleted || c.removed;
res.community = Some(CommunityView::read(pool, c.id, user_id, false).await?)
}
},
}; };
// if the object was deleted from database, dont return it // if the object was deleted from database, dont return it
if removed_or_deleted { if removed_or_deleted {

View file

@ -1,4 +1,5 @@
use crate::{ use crate::{
fetcher::user_or_community::{PersonOrGroup, UserOrCommunity},
objects::{comment::ApubComment, community::ApubCommunity, person::ApubPerson, post::ApubPost}, objects::{comment::ApubComment, community::ApubCommunity, person::ApubPerson, post::ApubPost},
protocol::objects::{note::Note, page::Page}, protocol::objects::{note::Note, page::Page},
}; };
@ -7,15 +8,12 @@ use activitypub_federation::{
fetch::{object_id::ObjectId, webfinger::webfinger_resolve_actor}, fetch::{object_id::ObjectId, webfinger::webfinger_resolve_actor},
traits::Object, traits::Object,
}; };
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use lemmy_api_common::context::LemmyContext; use lemmy_api_common::context::LemmyContext;
use lemmy_utils::error::{LemmyError}; use lemmy_utils::error::LemmyError;
use serde::Deserialize; use serde::Deserialize;
use url::Url; use url::Url;
use crate::fetcher::user_or_community::{PersonOrGroup, UserOrCommunity};
/// Converts search query to object id. The query can either be an URL, which will be treated as /// Converts search query to object id. The query can either be an URL, which will be treated as
/// ObjectId directly, or a webfinger identifier (@user@example.com or !community@example.com) /// ObjectId directly, or a webfinger identifier (@user@example.com or !community@example.com)
/// which gets resolved to an URL. /// which gets resolved to an URL.
@ -34,7 +32,9 @@ pub(crate) async fn search_query_to_object_id(
if query.starts_with("!") || query.starts_with("@") { if query.starts_with("!") || query.starts_with("@") {
query.remove(0); query.remove(0);
} }
SearchableObjects::PersonOrCommunity(webfinger_resolve_actor::<LemmyContext, UserOrCommunity>(&query, context).await?) SearchableObjects::PersonOrCommunity(
webfinger_resolve_actor::<LemmyContext, UserOrCommunity>(&query, context).await?,
)
} }
}) })
} }
@ -56,7 +56,7 @@ pub(crate) async fn search_query_to_object_id_local(
pub(crate) enum SearchableObjects { pub(crate) enum SearchableObjects {
Post(ApubPost), Post(ApubPost),
Comment(ApubComment), Comment(ApubComment),
PersonOrCommunity(UserOrCommunity) PersonOrCommunity(UserOrCommunity),
} }
#[derive(Deserialize)] #[derive(Deserialize)]
@ -64,7 +64,7 @@ pub(crate) enum SearchableObjects {
pub(crate) enum SearchableKinds { pub(crate) enum SearchableKinds {
Page(Page), Page(Page),
Note(Note), Note(Note),
PersonOrGroup(PersonOrGroup) PersonOrGroup(PersonOrGroup),
} }
#[async_trait::async_trait] #[async_trait::async_trait]
@ -111,12 +111,10 @@ impl Object for SearchableObjects {
match self { match self {
SearchableObjects::Post(p) => p.delete(data).await, SearchableObjects::Post(p) => p.delete(data).await,
SearchableObjects::Comment(c) => c.delete(data).await, SearchableObjects::Comment(c) => c.delete(data).await,
SearchableObjects::PersonOrCommunity(pc) => { SearchableObjects::PersonOrCommunity(pc) => match pc {
match pc { UserOrCommunity::User(p) => p.delete(data).await,
UserOrCommunity::User(p) => p.delete(data).await, UserOrCommunity::Community(c) => c.delete(data).await,
UserOrCommunity::Community(c) => c.delete(data).await, },
}
}
} }
} }
@ -133,12 +131,10 @@ impl Object for SearchableObjects {
match apub { match apub {
SearchableKinds::Page(a) => ApubPost::verify(a, expected_domain, data).await, SearchableKinds::Page(a) => ApubPost::verify(a, expected_domain, data).await,
SearchableKinds::Note(a) => ApubComment::verify(a, expected_domain, data).await, SearchableKinds::Note(a) => ApubComment::verify(a, expected_domain, data).await,
SearchableKinds::PersonOrGroup(pg) => { SearchableKinds::PersonOrGroup(pg) => match pg {
match pg { PersonOrGroup::Person(a) => ApubPerson::verify(a, expected_domain, data).await,
PersonOrGroup::Person(a) => ApubPerson::verify(a, expected_domain, data).await, PersonOrGroup::Group(a) => ApubCommunity::verify(a, expected_domain, data).await,
PersonOrGroup::Group(a) => ApubCommunity::verify(a, expected_domain, data).await, },
}
}
} }
} }