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},
7 use chrono::NaiveDateTime;
8 use lemmy_apub_lib::{object_id::ObjectId, traits::ApubObject};
9 use lemmy_utils::LemmyError;
10 use lemmy_websocket::LemmyContext;
11 use serde::Deserialize;
14 /// Attempt to parse the query as URL, and fetch an ActivityPub object from it.
16 /// Some working examples for use with the `docker/federation/` setup:
17 /// http://lemmy_alpha:8541/c/main, or !main@lemmy_alpha:8541
18 /// http://lemmy_beta:8551/u/lemmy_alpha, or @lemmy_beta@lemmy_beta:8551
19 /// http://lemmy_gamma:8561/post/3
20 /// http://lemmy_delta:8571/comment/2
21 #[tracing::instrument(skip_all)]
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, context.client(), request_counter)
34 let (kind, identifier) = query.split_at(1);
37 let id = webfinger_resolve::<ApubPerson>(
44 Ok(SearchableObjects::Person(
46 .dereference(context, context.client(), request_counter)
51 let id = webfinger_resolve::<ApubCommunity>(
53 EndpointType::Community,
58 Ok(SearchableObjects::Community(
60 .dereference(context, context.client(), request_counter)
64 _ => Err(LemmyError::from_message("invalid query")),
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 #[tracing::instrument(skip_all)]
109 async fn read_from_apub_id(
111 context: &LemmyContext,
112 ) -> Result<Option<Self>, LemmyError> {
113 let c = ApubCommunity::read_from_apub_id(object_id.clone(), context).await?;
115 return Ok(Some(SearchableObjects::Community(c)));
117 let p = ApubPerson::read_from_apub_id(object_id.clone(), context).await?;
119 return Ok(Some(SearchableObjects::Person(p)));
121 let p = ApubPost::read_from_apub_id(object_id.clone(), context).await?;
123 return Ok(Some(SearchableObjects::Post(p)));
125 let c = ApubComment::read_from_apub_id(object_id, context).await?;
127 return Ok(Some(SearchableObjects::Comment(c)));
132 #[tracing::instrument(skip_all)]
133 async fn delete(self, data: &Self::DataType) -> Result<(), LemmyError> {
135 SearchableObjects::Person(p) => p.delete(data).await,
136 SearchableObjects::Community(c) => c.delete(data).await,
137 SearchableObjects::Post(p) => p.delete(data).await,
138 SearchableObjects::Comment(c) => c.delete(data).await,
142 async fn into_apub(self, _data: &Self::DataType) -> Result<Self::ApubType, LemmyError> {
146 fn to_tombstone(&self) -> Result<Self::TombstoneType, LemmyError> {
150 #[tracing::instrument(skip_all)]
152 apub: &Self::ApubType,
153 expected_domain: &Url,
154 data: &Self::DataType,
155 request_counter: &mut i32,
156 ) -> Result<(), LemmyError> {
158 SearchableApubTypes::Group(a) => {
159 ApubCommunity::verify(a, expected_domain, data, request_counter).await
161 SearchableApubTypes::Person(a) => {
162 ApubPerson::verify(a, expected_domain, data, request_counter).await
164 SearchableApubTypes::Page(a) => {
165 ApubPost::verify(a, expected_domain, data, request_counter).await
167 SearchableApubTypes::Note(a) => {
168 ApubComment::verify(a, expected_domain, data, request_counter).await
173 #[tracing::instrument(skip_all)]
175 apub: Self::ApubType,
176 context: &LemmyContext,
178 ) -> Result<Self, LemmyError> {
179 use SearchableApubTypes as SAT;
180 use SearchableObjects as SO;
182 SAT::Group(g) => SO::Community(ApubCommunity::from_apub(g, context, rc).await?),
183 SAT::Person(p) => SO::Person(ApubPerson::from_apub(p, context, rc).await?),
184 SAT::Page(p) => SO::Post(ApubPost::from_apub(p, context, rc).await?),
185 SAT::Note(n) => SO::Comment(ApubComment::from_apub(n, context, rc).await?),