]> Untitled Git - lemmy.git/blob - crates/apub/src/fetcher/search.rs
70e7c40c1b51085f8f79e7980fb699c5ef152765
[lemmy.git] / crates / apub / src / fetcher / search.rs
1 use crate::{
2   fetcher::{
3     community::get_or_fetch_and_upsert_community,
4     fetch::fetch_remote_object,
5     is_deleted,
6     person::get_or_fetch_and_upsert_person,
7   },
8   find_object_by_id,
9   objects::{comment::Note, community::Group, person::Person as ApubPerson, post::Page, FromApub},
10   Object,
11 };
12 use anyhow::anyhow;
13 use itertools::Itertools;
14 use lemmy_api_common::{blocking, site::ResolveObjectResponse};
15 use lemmy_apub_lib::webfinger::{webfinger_resolve_actor, WebfingerType};
16 use lemmy_db_queries::source::{
17   comment::Comment_,
18   community::Community_,
19   person::Person_,
20   post::Post_,
21   private_message::PrivateMessage_,
22 };
23 use lemmy_db_schema::source::{
24   comment::Comment,
25   community::Community,
26   person::Person,
27   post::Post,
28   private_message::PrivateMessage,
29 };
30 use lemmy_db_views::{
31   comment_view::CommentView,
32   local_user_view::LocalUserView,
33   post_view::PostView,
34 };
35 use lemmy_db_views_actor::{community_view::CommunityView, person_view::PersonViewSafe};
36 use lemmy_utils::LemmyError;
37 use lemmy_websocket::LemmyContext;
38 use url::Url;
39
40 /// The types of ActivityPub objects that can be fetched directly by searching for their ID.
41 #[derive(serde::Deserialize, Debug)]
42 #[serde(untagged)]
43 enum SearchAcceptedObjects {
44   Person(Box<ApubPerson>),
45   Group(Box<Group>),
46   Page(Box<Page>),
47   Comment(Box<Note>),
48 }
49
50 /// Attempt to parse the query as URL, and fetch an ActivityPub object from it.
51 ///
52 /// Some working examples for use with the `docker/federation/` setup:
53 /// http://lemmy_alpha:8541/c/main, or !main@lemmy_alpha:8541
54 /// http://lemmy_beta:8551/u/lemmy_alpha, or @lemmy_beta@lemmy_beta:8551
55 /// http://lemmy_gamma:8561/post/3
56 /// http://lemmy_delta:8571/comment/2
57 pub async fn search_by_apub_id(
58   query: &str,
59   local_user_view: Option<LocalUserView>,
60   context: &LemmyContext,
61 ) -> Result<ResolveObjectResponse, LemmyError> {
62   let query_url = match Url::parse(query) {
63     Ok(u) => u,
64     Err(_) => {
65       let (kind, name) = query.split_at(1);
66       let kind = match kind {
67         "@" => WebfingerType::Person,
68         "!" => WebfingerType::Group,
69         _ => return Err(anyhow!("invalid query").into()),
70       };
71       // remote actor, use webfinger to resolve url
72       if name.contains('@') {
73         let (name, domain) = name.splitn(2, '@').collect_tuple().expect("invalid query");
74         webfinger_resolve_actor(name, domain, kind, context.client()).await?
75       }
76       // local actor, read from database and return
77       else {
78         let name: String = name.into();
79         return match kind {
80           WebfingerType::Group => {
81             let res = blocking(context.pool(), move |conn| {
82               let community = Community::read_from_name(conn, &name)?;
83               CommunityView::read(conn, community.id, local_user_view.map(|l| l.person.id))
84             })
85             .await??;
86             Ok(ResolveObjectResponse {
87               community: Some(res),
88               ..ResolveObjectResponse::default()
89             })
90           }
91           WebfingerType::Person => {
92             let res = blocking(context.pool(), move |conn| {
93               let person = Person::find_by_name(conn, &name)?;
94               PersonViewSafe::read(conn, person.id)
95             })
96             .await??;
97             Ok(ResolveObjectResponse {
98               person: Some(res),
99               ..ResolveObjectResponse::default()
100             })
101           }
102         };
103       }
104     }
105   };
106
107   let request_counter = &mut 0;
108   // this does a fetch (even for local objects), just to determine its type and fetch it again
109   // below. we need to fix this when rewriting the fetcher.
110   let fetch_response =
111     fetch_remote_object::<SearchAcceptedObjects>(context.client(), &query_url, request_counter)
112       .await;
113   if is_deleted(&fetch_response) {
114     delete_object_locally(&query_url, context).await?;
115     return Err(anyhow!("Object was deleted").into());
116   }
117
118   // Necessary because we get a stack overflow using FetchError
119   let fet_res = fetch_response.map_err(|e| LemmyError::from(e.inner))?;
120   build_response(fet_res, query_url, request_counter, context).await
121 }
122
123 async fn build_response(
124   fetch_response: SearchAcceptedObjects,
125   query_url: Url,
126   recursion_counter: &mut i32,
127   context: &LemmyContext,
128 ) -> Result<ResolveObjectResponse, LemmyError> {
129   use ResolveObjectResponse as ROR;
130   Ok(match fetch_response {
131     SearchAcceptedObjects::Person(p) => {
132       let person_uri = p.id(&query_url)?;
133
134       let person = get_or_fetch_and_upsert_person(person_uri, context, recursion_counter).await?;
135       ROR {
136         person: blocking(context.pool(), move |conn| {
137           PersonViewSafe::read(conn, person.id)
138         })
139         .await?
140         .ok(),
141         ..ROR::default()
142       }
143     }
144     SearchAcceptedObjects::Group(g) => {
145       let community_uri = g.id(&query_url)?;
146       let community =
147         get_or_fetch_and_upsert_community(community_uri, context, recursion_counter).await?;
148       ROR {
149         community: blocking(context.pool(), move |conn| {
150           CommunityView::read(conn, community.id, None)
151         })
152         .await?
153         .ok(),
154         ..ROR::default()
155       }
156     }
157     SearchAcceptedObjects::Page(p) => {
158       let p = Post::from_apub(&p, context, &query_url, recursion_counter).await?;
159       ROR {
160         post: blocking(context.pool(), move |conn| PostView::read(conn, p.id, None))
161           .await?
162           .ok(),
163         ..ROR::default()
164       }
165     }
166     SearchAcceptedObjects::Comment(c) => {
167       let c = Comment::from_apub(&c, context, &query_url, recursion_counter).await?;
168       ROR {
169         comment: blocking(context.pool(), move |conn| {
170           CommentView::read(conn, c.id, None)
171         })
172         .await?
173         .ok(),
174         ..ROR::default()
175       }
176     }
177   })
178 }
179
180 async fn delete_object_locally(query_url: &Url, context: &LemmyContext) -> Result<(), LemmyError> {
181   let res = find_object_by_id(context, query_url.to_owned()).await?;
182   match res {
183     Object::Comment(c) => {
184       blocking(context.pool(), move |conn| {
185         Comment::update_deleted(conn, c.id, true)
186       })
187       .await??;
188     }
189     Object::Post(p) => {
190       blocking(context.pool(), move |conn| {
191         Post::update_deleted(conn, p.id, true)
192       })
193       .await??;
194     }
195     Object::Person(u) => {
196       // TODO: implement update_deleted() for user, move it to ApubObject trait
197       blocking(context.pool(), move |conn| {
198         Person::delete_account(conn, u.id)
199       })
200       .await??;
201     }
202     Object::Community(c) => {
203       blocking(context.pool(), move |conn| {
204         Community::update_deleted(conn, c.id, true)
205       })
206       .await??;
207     }
208     Object::PrivateMessage(pm) => {
209       blocking(context.pool(), move |conn| {
210         PrivateMessage::update_deleted(conn, pm.id, true)
211       })
212       .await??;
213     }
214   }
215   Ok(())
216 }