2 fetcher::{deletable_apub_object::DeletableApubObject, object_id::ObjectId},
3 objects::{comment::Note, community::Group, person::Person as ApubPerson, post::Page, FromApub},
5 use activitystreams::chrono::NaiveDateTime;
7 use diesel::{result::Error, PgConnection};
8 use itertools::Itertools;
9 use lemmy_api_common::blocking;
10 use lemmy_apub_lib::webfinger::{webfinger_resolve_actor, WebfingerType};
11 use lemmy_db_queries::{
12 source::{community::Community_, person::Person_},
16 use lemmy_db_schema::{
17 source::{comment::Comment, community::Community, person::Person, post::Post},
20 use lemmy_utils::LemmyError;
21 use lemmy_websocket::LemmyContext;
22 use serde::Deserialize;
25 /// Attempt to parse the query as URL, and fetch an ActivityPub object from it.
27 /// Some working examples for use with the `docker/federation/` setup:
28 /// http://lemmy_alpha:8541/c/main, or !main@lemmy_alpha:8541
29 /// http://lemmy_beta:8551/u/lemmy_alpha, or @lemmy_beta@lemmy_beta:8551
30 /// http://lemmy_gamma:8561/post/3
31 /// http://lemmy_delta:8571/comment/2
32 pub async fn search_by_apub_id(
34 context: &LemmyContext,
35 ) -> Result<SearchableObjects, LemmyError> {
36 let query_url = match Url::parse(query) {
39 let (kind, name) = query.split_at(1);
40 let kind = match kind {
41 "@" => WebfingerType::Person,
42 "!" => WebfingerType::Group,
43 _ => return Err(anyhow!("invalid query").into()),
45 // remote actor, use webfinger to resolve url
46 if name.contains('@') {
47 let (name, domain) = name.splitn(2, '@').collect_tuple().expect("invalid query");
48 webfinger_resolve_actor(name, domain, kind, context.client()).await?
50 // local actor, read from database and return
52 return find_local_actor_by_name(name, kind, context.pool()).await;
57 let request_counter = &mut 0;
58 ObjectId::new(query_url)
59 .dereference(context, request_counter)
63 async fn find_local_actor_by_name(
67 ) -> Result<SearchableObjects, LemmyError> {
68 let name: String = name.into();
70 WebfingerType::Group => SearchableObjects::Community(
71 blocking(pool, move |conn| Community::read_from_name(conn, &name)).await??,
73 WebfingerType::Person => SearchableObjects::Person(
74 blocking(pool, move |conn| Person::find_by_name(conn, &name)).await??,
79 /// The types of ActivityPub objects that can be fetched directly by searching for their ID.
81 pub enum SearchableObjects {
88 #[derive(Deserialize)]
90 pub enum SearchableApubTypes {
97 impl ApubObject for SearchableObjects {
98 fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
100 SearchableObjects::Person(p) => p.last_refreshed_at(),
101 SearchableObjects::Community(c) => c.last_refreshed_at(),
102 SearchableObjects::Post(p) => p.last_refreshed_at(),
103 SearchableObjects::Comment(c) => c.last_refreshed_at(),
107 // TODO: this is inefficient, because if the object is not in local db, it will run 4 db queries
108 // before finally returning an error. it would be nice if we could check all 4 tables in
110 // we could skip this and always return an error, but then it would not be able to mark
111 // objects as deleted that were deleted by remote server.
112 fn read_from_apub_id(conn: &PgConnection, object_id: &DbUrl) -> Result<Self, Error> {
113 let c = Community::read_from_apub_id(conn, object_id);
115 return Ok(SearchableObjects::Community(c));
117 let p = Person::read_from_apub_id(conn, object_id);
119 return Ok(SearchableObjects::Person(p));
121 let p = Post::read_from_apub_id(conn, object_id);
123 return Ok(SearchableObjects::Post(p));
125 let c = Comment::read_from_apub_id(conn, object_id);
126 Ok(SearchableObjects::Comment(c?))
130 #[async_trait::async_trait(?Send)]
131 impl FromApub for SearchableObjects {
132 type ApubType = SearchableApubTypes;
135 apub: &Self::ApubType,
136 context: &LemmyContext,
139 ) -> Result<Self, LemmyError> {
140 use SearchableApubTypes as SAT;
141 use SearchableObjects as SO;
143 SAT::Group(g) => SO::Community(Community::from_apub(g, context, ed, rc).await?),
144 SAT::Person(p) => SO::Person(Person::from_apub(p, context, ed, rc).await?),
145 SAT::Page(p) => SO::Post(Post::from_apub(p, context, ed, rc).await?),
146 SAT::Note(n) => SO::Comment(Comment::from_apub(n, context, ed, rc).await?),
151 #[async_trait::async_trait(?Send)]
152 impl DeletableApubObject for SearchableObjects {
153 async fn delete(self, context: &LemmyContext) -> Result<(), LemmyError> {
155 SearchableObjects::Person(p) => p.delete(context).await,
156 SearchableObjects::Community(c) => c.delete(context).await,
157 SearchableObjects::Post(p) => p.delete(context).await,
158 SearchableObjects::Comment(c) => c.delete(context).await,