2 fetcher::webfinger::webfinger_resolve_actor,
3 objects::{comment::ApubComment, community::ApubCommunity, person::ApubPerson, post::ApubPost},
4 protocol::objects::{group::Group, note::Note, page::Page, person::Person},
6 use chrono::NaiveDateTime;
7 use lemmy_apub_lib::{object_id::ObjectId, traits::ApubObject};
8 use lemmy_utils::LemmyError;
9 use lemmy_websocket::LemmyContext;
10 use serde::Deserialize;
13 /// Attempt to parse the query as URL, and fetch an ActivityPub object from it.
15 /// Some working examples for use with the `docker/federation/` setup:
16 /// http://lemmy_alpha:8541/c/main, or !main@lemmy_alpha:8541
17 /// http://lemmy_beta:8551/u/lemmy_alpha, or @lemmy_beta@lemmy_beta:8551
18 /// http://lemmy_gamma:8561/post/3
19 /// http://lemmy_delta:8571/comment/2
20 #[tracing::instrument(skip_all)]
21 pub async fn search_by_apub_id(
23 context: &LemmyContext,
24 ) -> Result<SearchableObjects, LemmyError> {
25 let request_counter = &mut 0;
26 match Url::parse(query) {
29 .dereference(context, context.client(), request_counter)
33 let (kind, identifier) = query.split_at(1);
37 webfinger_resolve_actor::<ApubPerson>(identifier, context, request_counter).await?;
38 Ok(SearchableObjects::Person(
40 .dereference(context, context.client(), request_counter)
46 webfinger_resolve_actor::<ApubCommunity>(identifier, context, request_counter).await?;
47 Ok(SearchableObjects::Community(
49 .dereference(context, context.client(), request_counter)
53 _ => Err(LemmyError::from_message("invalid query")),
59 /// The types of ActivityPub objects that can be fetched directly by searching for their ID.
61 pub enum SearchableObjects {
63 Community(ApubCommunity),
68 #[derive(Deserialize)]
70 pub enum SearchableApubTypes {
77 #[async_trait::async_trait(?Send)]
78 impl ApubObject for SearchableObjects {
79 type DataType = LemmyContext;
80 type ApubType = SearchableApubTypes;
81 type TombstoneType = ();
83 fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
85 SearchableObjects::Person(p) => p.last_refreshed_at(),
86 SearchableObjects::Community(c) => c.last_refreshed_at(),
87 SearchableObjects::Post(p) => p.last_refreshed_at(),
88 SearchableObjects::Comment(c) => c.last_refreshed_at(),
92 // TODO: this is inefficient, because if the object is not in local db, it will run 4 db queries
93 // before finally returning an error. it would be nice if we could check all 4 tables in
95 // we could skip this and always return an error, but then it would always fetch objects
96 // over http, and not be able to mark objects as deleted that were deleted by remote server.
97 #[tracing::instrument(skip_all)]
98 async fn read_from_apub_id(
100 context: &LemmyContext,
101 ) -> Result<Option<Self>, LemmyError> {
102 let c = ApubCommunity::read_from_apub_id(object_id.clone(), context).await?;
104 return Ok(Some(SearchableObjects::Community(c)));
106 let p = ApubPerson::read_from_apub_id(object_id.clone(), context).await?;
108 return Ok(Some(SearchableObjects::Person(p)));
110 let p = ApubPost::read_from_apub_id(object_id.clone(), context).await?;
112 return Ok(Some(SearchableObjects::Post(p)));
114 let c = ApubComment::read_from_apub_id(object_id, context).await?;
116 return Ok(Some(SearchableObjects::Comment(c)));
121 #[tracing::instrument(skip_all)]
122 async fn delete(self, data: &Self::DataType) -> Result<(), LemmyError> {
124 SearchableObjects::Person(p) => p.delete(data).await,
125 SearchableObjects::Community(c) => c.delete(data).await,
126 SearchableObjects::Post(p) => p.delete(data).await,
127 SearchableObjects::Comment(c) => c.delete(data).await,
131 async fn into_apub(self, _data: &Self::DataType) -> Result<Self::ApubType, LemmyError> {
135 fn to_tombstone(&self) -> Result<Self::TombstoneType, LemmyError> {
139 #[tracing::instrument(skip_all)]
141 apub: &Self::ApubType,
142 expected_domain: &Url,
143 data: &Self::DataType,
144 request_counter: &mut i32,
145 ) -> Result<(), LemmyError> {
147 SearchableApubTypes::Group(a) => {
148 ApubCommunity::verify(a, expected_domain, data, request_counter).await
150 SearchableApubTypes::Person(a) => {
151 ApubPerson::verify(a, expected_domain, data, request_counter).await
153 SearchableApubTypes::Page(a) => {
154 ApubPost::verify(a, expected_domain, data, request_counter).await
156 SearchableApubTypes::Note(a) => {
157 ApubComment::verify(a, expected_domain, data, request_counter).await
162 #[tracing::instrument(skip_all)]
164 apub: Self::ApubType,
165 context: &LemmyContext,
167 ) -> Result<Self, LemmyError> {
168 use SearchableApubTypes as SAT;
169 use SearchableObjects as SO;
171 SAT::Group(g) => SO::Community(ApubCommunity::from_apub(g, context, rc).await?),
172 SAT::Person(p) => SO::Person(ApubPerson::from_apub(p, context, rc).await?),
173 SAT::Page(p) => SO::Post(ApubPost::from_apub(p, context, rc).await?),
174 SAT::Note(n) => SO::Comment(ApubComment::from_apub(n, context, rc).await?),