3 fetch::fetch_remote_object,
4 get_or_fetch_and_upsert_community,
5 get_or_fetch_and_upsert_person,
9 objects::{comment::Note, community::Group, person::Person as ApubPerson, post::Page, FromApub},
13 use lemmy_api_common::{blocking, site::SearchResponse};
14 use lemmy_db_queries::{
17 community::Community_,
20 private_message::PrivateMessage_,
24 use lemmy_db_schema::source::{
29 private_message::PrivateMessage,
31 use lemmy_db_views::{comment_view::CommentView, post_view::PostView};
32 use lemmy_db_views_actor::{community_view::CommunityView, person_view::PersonViewSafe};
33 use lemmy_utils::{settings::structs::Settings, LemmyError};
34 use lemmy_websocket::LemmyContext;
38 /// The types of ActivityPub objects that can be fetched directly by searching for their ID.
39 #[derive(serde::Deserialize, Debug)]
41 enum SearchAcceptedObjects {
42 Person(Box<ApubPerson>),
48 /// Attempt to parse the query as URL, and fetch an ActivityPub object from it.
50 /// Some working examples for use with the `docker/federation/` setup:
51 /// http://lemmy_alpha:8541/c/main, or !main@lemmy_alpha:8541
52 /// http://lemmy_beta:8551/u/lemmy_alpha, or @lemmy_beta@lemmy_beta:8551
53 /// http://lemmy_gamma:8561/post/3
54 /// http://lemmy_delta:8571/comment/2
55 pub async fn search_by_apub_id(
57 context: &LemmyContext,
58 ) -> Result<SearchResponse, LemmyError> {
59 // Parse the shorthand query url
60 let query_url = if query.contains('@') {
61 debug!("Search for {}", query);
62 let split = query.split('@').collect::<Vec<&str>>();
64 // Person type will look like ['', username, instance]
65 // Community will look like [!community, instance]
66 let (name, instance) = if split.len() == 3 {
67 (format!("/u/{}", split[1]), split[2])
68 } else if split.len() == 2 {
69 if split[0].contains('!') {
70 let split2 = split[0].split('!').collect::<Vec<&str>>();
71 (format!("/c/{}", split2[1]), split[1])
73 return Err(anyhow!("Invalid search query: {}", query).into());
76 return Err(anyhow!("Invalid search query: {}", query).into());
81 Settings::get().get_protocol_string(),
90 let recursion_counter = &mut 0;
92 fetch_remote_object::<SearchAcceptedObjects>(context.client(), &query_url, recursion_counter)
94 if is_deleted(&fetch_response) {
95 delete_object_locally(&query_url, context).await?;
98 // Necessary because we get a stack overflow using FetchError
99 let fet_res = fetch_response.map_err(|e| LemmyError::from(e.inner))?;
100 build_response(fet_res, query_url, recursion_counter, context).await
103 async fn build_response(
104 fetch_response: SearchAcceptedObjects,
106 recursion_counter: &mut i32,
107 context: &LemmyContext,
108 ) -> Result<SearchResponse, LemmyError> {
109 let mut response = SearchResponse {
110 type_: SearchType::All.to_string(),
117 match fetch_response {
118 SearchAcceptedObjects::Person(p) => {
119 let person_id = p.id(&query_url)?;
120 let person = get_or_fetch_and_upsert_person(person_id, context, recursion_counter).await?;
122 response.users = vec![
123 blocking(context.pool(), move |conn| {
124 PersonViewSafe::read(conn, person.id)
129 SearchAcceptedObjects::Group(g) => {
130 let community_uri = g.id(&query_url)?;
132 get_or_fetch_and_upsert_community(community_uri, context, recursion_counter).await?;
134 response.communities = vec![
135 blocking(context.pool(), move |conn| {
136 CommunityView::read(conn, community.id, None)
141 SearchAcceptedObjects::Page(p) => {
142 let p = Post::from_apub(&p, context, &query_url, recursion_counter).await?;
145 vec![blocking(context.pool(), move |conn| PostView::read(conn, p.id, None)).await??];
147 SearchAcceptedObjects::Comment(c) => {
148 let c = Comment::from_apub(&c, context, &query_url, recursion_counter).await?;
150 response.comments = vec![
151 blocking(context.pool(), move |conn| {
152 CommentView::read(conn, c.id, None)
162 async fn delete_object_locally(query_url: &Url, context: &LemmyContext) -> Result<(), LemmyError> {
163 let res = find_object_by_id(context, query_url.to_owned()).await?;
165 Object::Comment(c) => {
166 blocking(context.pool(), move |conn| {
167 Comment::update_deleted(conn, c.id, true)
172 blocking(context.pool(), move |conn| {
173 Post::update_deleted(conn, p.id, true)
177 Object::Person(u) => {
178 // TODO: implement update_deleted() for user, move it to ApubObject trait
179 blocking(context.pool(), move |conn| {
180 Person::delete_account(conn, u.id)
184 Object::Community(c) => {
185 blocking(context.pool(), move |conn| {
186 Community::update_deleted(conn, c.id, true)
190 Object::PrivateMessage(pm) => {
191 blocking(context.pool(), move |conn| {
192 PrivateMessage::update_deleted(conn, pm.id, true)
197 Err(anyhow!("Object was deleted").into())