2 fetcher::webfinger::webfinger_resolve,
3 objects::{comment::ApubComment, community::ApubCommunity, person::ApubPerson, post::ApubPost},
4 protocol::objects::{group::Group, note::Note, page::Page, person::Person},
8 use chrono::NaiveDateTime;
9 use lemmy_apub_lib::{object_id::ObjectId, traits::ApubObject};
10 use lemmy_utils::LemmyError;
11 use lemmy_websocket::LemmyContext;
12 use serde::Deserialize;
15 /// Attempt to parse the query as URL, and fetch an ActivityPub object from it.
17 /// Some working examples for use with the `docker/federation/` setup:
18 /// http://lemmy_alpha:8541/c/main, or !main@lemmy_alpha:8541
19 /// http://lemmy_beta:8551/u/lemmy_alpha, or @lemmy_beta@lemmy_beta:8551
20 /// http://lemmy_gamma:8561/post/3
21 /// http://lemmy_delta:8571/comment/2
22 pub async fn search_by_apub_id(
24 context: &LemmyContext,
25 ) -> Result<SearchableObjects, LemmyError> {
26 let request_counter = &mut 0;
27 match Url::parse(query) {
30 .dereference(context, request_counter)
34 let (kind, identifier) = query.split_at(1);
37 let id = webfinger_resolve::<ApubPerson>(
44 Ok(SearchableObjects::Person(
46 .dereference(context, request_counter)
51 let id = webfinger_resolve::<ApubCommunity>(
53 EndpointType::Community,
58 Ok(SearchableObjects::Community(
60 .dereference(context, request_counter)
64 _ => Err(anyhow!("invalid query").into()),
70 /// The types of ActivityPub objects that can be fetched directly by searching for their ID.
72 pub enum SearchableObjects {
74 Community(ApubCommunity),
79 #[derive(Deserialize)]
81 pub enum SearchableApubTypes {
88 #[async_trait::async_trait(?Send)]
89 impl ApubObject for SearchableObjects {
90 type DataType = LemmyContext;
91 type ApubType = SearchableApubTypes;
92 type TombstoneType = ();
94 fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
96 SearchableObjects::Person(p) => p.last_refreshed_at(),
97 SearchableObjects::Community(c) => c.last_refreshed_at(),
98 SearchableObjects::Post(p) => p.last_refreshed_at(),
99 SearchableObjects::Comment(c) => c.last_refreshed_at(),
103 // TODO: this is inefficient, because if the object is not in local db, it will run 4 db queries
104 // before finally returning an error. it would be nice if we could check all 4 tables in
106 // we could skip this and always return an error, but then it would always fetch objects
107 // over http, and not be able to mark objects as deleted that were deleted by remote server.
108 async fn read_from_apub_id(
110 context: &LemmyContext,
111 ) -> Result<Option<Self>, LemmyError> {
112 let c = ApubCommunity::read_from_apub_id(object_id.clone(), context).await?;
114 return Ok(Some(SearchableObjects::Community(c)));
116 let p = ApubPerson::read_from_apub_id(object_id.clone(), context).await?;
118 return Ok(Some(SearchableObjects::Person(p)));
120 let p = ApubPost::read_from_apub_id(object_id.clone(), context).await?;
122 return Ok(Some(SearchableObjects::Post(p)));
124 let c = ApubComment::read_from_apub_id(object_id, context).await?;
126 return Ok(Some(SearchableObjects::Comment(c)));
131 async fn delete(self, data: &Self::DataType) -> Result<(), LemmyError> {
133 SearchableObjects::Person(p) => p.delete(data).await,
134 SearchableObjects::Community(c) => c.delete(data).await,
135 SearchableObjects::Post(p) => p.delete(data).await,
136 SearchableObjects::Comment(c) => c.delete(data).await,
140 async fn into_apub(self, _data: &Self::DataType) -> Result<Self::ApubType, LemmyError> {
144 fn to_tombstone(&self) -> Result<Self::TombstoneType, LemmyError> {
149 apub: &Self::ApubType,
150 expected_domain: &Url,
151 data: &Self::DataType,
152 request_counter: &mut i32,
153 ) -> Result<(), LemmyError> {
155 SearchableApubTypes::Group(a) => {
156 ApubCommunity::verify(a, expected_domain, data, request_counter).await
158 SearchableApubTypes::Person(a) => {
159 ApubPerson::verify(a, expected_domain, data, request_counter).await
161 SearchableApubTypes::Page(a) => {
162 ApubPost::verify(a, expected_domain, data, request_counter).await
164 SearchableApubTypes::Note(a) => {
165 ApubComment::verify(a, expected_domain, data, request_counter).await
171 apub: Self::ApubType,
172 context: &LemmyContext,
174 ) -> Result<Self, LemmyError> {
175 use SearchableApubTypes as SAT;
176 use SearchableObjects as SO;
178 SAT::Group(g) => SO::Community(ApubCommunity::from_apub(g, context, rc).await?),
179 SAT::Person(p) => SO::Person(ApubPerson::from_apub(p, context, rc).await?),
180 SAT::Page(p) => SO::Post(ApubPost::from_apub(p, context, rc).await?),
181 SAT::Note(n) => SO::Comment(ApubComment::from_apub(n, context, rc).await?),