]> Untitled Git - lemmy.git/commitdiff
Move resolving of activitypub objects to separate api endpoint (fixes #1584)
authorFelix Ableitner <me@nutomic.com>
Tue, 20 Jul 2021 07:00:20 +0000 (09:00 +0200)
committerFelix Ableitner <me@nutomic.com>
Fri, 20 Aug 2021 10:35:50 +0000 (12:35 +0200)
14 files changed:
Cargo.lock
crates/api/src/lib.rs
crates/api/src/site.rs
crates/api_common/src/lib.rs
crates/api_common/src/site.rs
crates/apub/src/activities/comment/mod.rs
crates/apub/src/fetcher/search.rs
crates/apub_lib/Cargo.toml
crates/apub_lib/src/lib.rs
crates/apub_lib/src/webfinger.rs [new file with mode: 0644]
crates/routes/Cargo.toml
crates/routes/src/webfinger.rs
crates/websocket/src/lib.rs
src/api_routes.rs

index 4cdcc47f66479c0a8c3518bdfde4492cff3d4c8f..624897b389ffc343f19c8964803ee715069cc109 100644 (file)
@@ -1736,10 +1736,13 @@ name = "lemmy_apub_lib"
 version = "0.11.3"
 dependencies = [
  "activitystreams",
+ "anyhow",
  "async-trait",
  "lemmy_apub_lib_derive",
  "lemmy_utils",
  "lemmy_websocket",
+ "log",
+ "reqwest",
  "serde",
  "serde_json",
  "url",
@@ -1836,6 +1839,7 @@ dependencies = [
  "diesel",
  "lazy_static",
  "lemmy_api_common",
+ "lemmy_apub_lib",
  "lemmy_db_queries",
  "lemmy_db_schema",
  "lemmy_db_views",
index bf3a813b36d1c87d90a475d042a010ab3356ca59..83d98bde5db9de5955410d6d7025aa24db5f8025 100644 (file)
@@ -87,6 +87,9 @@ pub async fn match_websocket_operation(
       do_websocket_operation::<SaveSiteConfig>(context, id, op, data).await
     }
     UserOperation::Search => do_websocket_operation::<Search>(context, id, op, data).await,
+    UserOperation::ResolveObject => {
+      do_websocket_operation::<ResolveObject>(context, id, op, data).await
+    }
     UserOperation::TransferCommunity => {
       do_websocket_operation::<TransferCommunity>(context, id, op, data).await
     }
index 221b9870fae159d7bceb15487a79114265b1995b..315cf72da7b81897f0c3941a590bc2a844f6720c 100644 (file)
@@ -50,7 +50,6 @@ use lemmy_utils::{
   LemmyError,
 };
 use lemmy_websocket::LemmyContext;
-use log::debug;
 
 #[async_trait::async_trait(?Send)]
 impl Perform for GetModlog {
@@ -143,11 +142,6 @@ impl Perform for Search {
   ) -> Result<SearchResponse, LemmyError> {
     let data: &Search = self;
 
-    match search_by_apub_id(&data.q, context).await {
-      Ok(r) => return Ok(r),
-      Err(e) => debug!("Failed to resolve search query as activitypub ID: {}", e),
-    }
-
     let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
 
     let show_nsfw = local_user_view.as_ref().map(|t| t.local_user.show_nsfw);
@@ -372,6 +366,20 @@ impl Perform for Search {
   }
 }
 
+#[async_trait::async_trait(?Send)]
+impl Perform for ResolveObject {
+  type Response = ResolveObjectResponse;
+
+  async fn perform(
+    &self,
+    context: &Data<LemmyContext>,
+    _websocket_id: Option<ConnectionId>,
+  ) -> Result<ResolveObjectResponse, LemmyError> {
+    let local_user_view = get_local_user_view_from_jwt_opt(&self.auth, context.pool()).await?;
+    search_by_apub_id(&self.q, local_user_view, context).await
+  }
+}
+
 #[async_trait::async_trait(?Send)]
 impl Perform for TransferSite {
   type Response = GetSiteResponse;
index e7d5181bcdbfc16fdc0e8cedbb449b91ec8b80fc..a79e842eea71763ae3c7d2388ccaa03c63910330 100644 (file)
@@ -46,26 +46,8 @@ use lemmy_utils::{
   LemmyError,
 };
 use log::error;
-use serde::{Deserialize, Serialize};
 use url::Url;
 
-#[derive(Serialize, Deserialize, Debug)]
-pub struct WebFingerLink {
-  pub rel: Option<String>,
-  #[serde(rename(serialize = "type", deserialize = "type"))]
-  pub type_: Option<String>,
-  pub href: Option<Url>,
-  #[serde(skip_serializing_if = "Option::is_none")]
-  pub template: Option<String>,
-}
-
-#[derive(Serialize, Deserialize, Debug)]
-pub struct WebFingerResponse {
-  pub subject: String,
-  pub aliases: Vec<Url>,
-  pub links: Vec<WebFingerLink>,
-}
-
 pub async fn blocking<F, T>(pool: &DbPool, f: F) -> Result<T, LemmyError>
 where
   F: FnOnce(&diesel::PgConnection) -> T + Send + 'static,
index af63854d3965f540fba9de6a5eec3a996e456927..ed62117815ad9213bb84ab4ea55a5f2bddb36b33 100644 (file)
@@ -50,6 +50,20 @@ pub struct SearchResponse {
   pub users: Vec<PersonViewSafe>,
 }
 
+#[derive(Deserialize, Debug)]
+pub struct ResolveObject {
+  pub q: String,
+  pub auth: Option<String>,
+}
+
+#[derive(Serialize, Debug)]
+pub enum ResolveObjectResponse {
+  Comment(CommentView),
+  Post(PostView),
+  Community(CommunityView),
+  Person(PersonViewSafe),
+}
+
 #[derive(Deserialize)]
 pub struct GetModlog {
   pub mod_person_id: Option<PersonId>,
index 228a5f804c518d0acc6e3b69f7d5fc550797f3f2..44b8d245a58cc953e13d0ab9aec1607fb06af9db 100644 (file)
@@ -5,7 +5,8 @@ use activitystreams::{
 };
 use anyhow::anyhow;
 use itertools::Itertools;
-use lemmy_api_common::{blocking, send_local_notifs, WebFingerResponse};
+use lemmy_api_common::{blocking, send_local_notifs};
+use lemmy_apub_lib::webfinger::WebfingerResponse;
 use lemmy_db_queries::{Crud, DbPool};
 use lemmy_db_schema::{
   source::{comment::Comment, community::Community, person::Person, post::Post},
@@ -128,7 +129,7 @@ async fn fetch_webfinger_url(mention: &MentionData, client: &Client) -> Result<U
 
   let response = retry(|| client.get(&fetch_url).send()).await?;
 
-  let res: WebFingerResponse = response
+  let res: WebfingerResponse = response
     .json()
     .await
     .map_err(|e| RecvError(e.to_string()))?;
index 2c9bebb0284a51e95e8d43c6b112b2aa63274b09..6f081c8b412d106e4a2fd9b825e6b9fe33db0d3e 100644 (file)
@@ -1,25 +1,24 @@
 use crate::{
   fetcher::{
+    community::get_or_fetch_and_upsert_community,
     fetch::fetch_remote_object,
-    get_or_fetch_and_upsert_community,
-    get_or_fetch_and_upsert_person,
     is_deleted,
+    person::get_or_fetch_and_upsert_person,
   },
   find_object_by_id,
   objects::{comment::Note, community::Group, person::Person as ApubPerson, post::Page, FromApub},
   Object,
 };
 use anyhow::anyhow;
-use lemmy_api_common::{blocking, site::SearchResponse};
-use lemmy_db_queries::{
-  source::{
-    comment::Comment_,
-    community::Community_,
-    person::Person_,
-    post::Post_,
-    private_message::PrivateMessage_,
-  },
-  SearchType,
+use itertools::Itertools;
+use lemmy_api_common::{blocking, site::ResolveObjectResponse};
+use lemmy_apub_lib::webfinger::{webfinger_resolve_actor, WebfingerType};
+use lemmy_db_queries::source::{
+  comment::Comment_,
+  community::Community_,
+  person::Person_,
+  post::Post_,
+  private_message::PrivateMessage_,
 };
 use lemmy_db_schema::source::{
   comment::Comment,
@@ -28,11 +27,14 @@ use lemmy_db_schema::source::{
   post::Post,
   private_message::PrivateMessage,
 };
-use lemmy_db_views::{comment_view::CommentView, post_view::PostView};
+use lemmy_db_views::{
+  comment_view::CommentView,
+  local_user_view::LocalUserView,
+  post_view::PostView,
+};
 use lemmy_db_views_actor::{community_view::CommunityView, person_view::PersonViewSafe};
-use lemmy_utils::{settings::structs::Settings, LemmyError};
+use lemmy_utils::LemmyError;
 use lemmy_websocket::LemmyContext;
-use log::debug;
 use url::Url;
 
 /// The types of ActivityPub objects that can be fetched directly by searching for their ID.
@@ -54,50 +56,62 @@ enum SearchAcceptedObjects {
 /// http://lemmy_delta:8571/comment/2
 pub async fn search_by_apub_id(
   query: &str,
+  local_user_view: Option<LocalUserView>,
   context: &LemmyContext,
-) -> Result<SearchResponse, LemmyError> {
-  // Parse the shorthand query url
-  let query_url = if query.contains('@') {
-    debug!("Search for {}", query);
-    let split = query.split('@').collect::<Vec<&str>>();
-
-    // Person type will look like ['', username, instance]
-    // Community will look like [!community, instance]
-    let (name, instance) = if split.len() == 3 {
-      (format!("/u/{}", split[1]), split[2])
-    } else if split.len() == 2 {
-      if split[0].contains('!') {
-        let split2 = split[0].split('!').collect::<Vec<&str>>();
-        (format!("/c/{}", split2[1]), split[1])
-      } else {
-        return Err(anyhow!("Invalid search query: {}", query).into());
+) -> Result<ResolveObjectResponse, LemmyError> {
+  let query_url = match Url::parse(query) {
+    Ok(u) => u,
+    Err(_) => {
+      let (kind, name) = query.split_at(1);
+      let kind = match kind {
+        "@" => WebfingerType::Person,
+        "!" => WebfingerType::Group,
+        _ => return Err(anyhow!("invalid query").into()),
+      };
+      // remote actor, use webfinger to resolve url
+      if name.contains('@') {
+        let (name, domain) = name.splitn(2, '@').collect_tuple().expect("invalid query");
+        webfinger_resolve_actor(name, domain, kind, context.client()).await?
       }
-    } else {
-      return Err(anyhow!("Invalid search query: {}", query).into());
-    };
-
-    let url = format!(
-      "{}://{}{}",
-      Settings::get().get_protocol_string(),
-      instance,
-      name
-    );
-    Url::parse(&url)?
-  } else {
-    Url::parse(query)?
+      // local actor, read from database and return
+      else {
+        let name: String = name.into();
+        return match kind {
+          WebfingerType::Group => {
+            let res = blocking(context.pool(), move |conn| {
+              let community = Community::read_from_name(conn, &name)?;
+              CommunityView::read(conn, community.id, local_user_view.map(|l| l.person.id))
+            })
+            .await??;
+            Ok(ResolveObjectResponse::Community(res))
+          }
+          WebfingerType::Person => {
+            let res = blocking(context.pool(), move |conn| {
+              let person = Person::find_by_name(conn, &name)?;
+              PersonViewSafe::read(conn, person.id)
+            })
+            .await??;
+            Ok(ResolveObjectResponse::Person(res))
+          }
+        };
+      }
+    }
   };
 
-  let recursion_counter = &mut 0;
+  let request_counter = &mut 0;
+  // this does a fetch (even for local objects), just to determine its type and fetch it again
+  // below. we need to fix this when rewriting the fetcher.
   let fetch_response =
-    fetch_remote_object::<SearchAcceptedObjects>(context.client(), &query_url, recursion_counter)
+    fetch_remote_object::<SearchAcceptedObjects>(context.client(), &query_url, request_counter)
       .await;
   if is_deleted(&fetch_response) {
     delete_object_locally(&query_url, context).await?;
+    return Err(anyhow!("Object was deleted").into());
   }
 
   // Necessary because we get a stack overflow using FetchError
   let fet_res = fetch_response.map_err(|e| LemmyError::from(e.inner))?;
-  build_response(fet_res, query_url, recursion_counter, context).await
+  build_response(fet_res, query_url, request_counter, context).await
 }
 
 async fn build_response(
@@ -105,58 +119,45 @@ async fn build_response(
   query_url: Url,
   recursion_counter: &mut i32,
   context: &LemmyContext,
-) -> Result<SearchResponse, LemmyError> {
-  let mut response = SearchResponse {
-    type_: SearchType::All.to_string(),
-    comments: vec![],
-    posts: vec![],
-    communities: vec![],
-    users: vec![],
-  };
-
-  match fetch_response {
+) -> Result<ResolveObjectResponse, LemmyError> {
+  use ResolveObjectResponse as ROR;
+  Ok(match fetch_response {
     SearchAcceptedObjects::Person(p) => {
-      let person_id = p.id(&query_url)?;
-      let person = get_or_fetch_and_upsert_person(person_id, context, recursion_counter).await?;
+      let person_uri = p.id(&query_url)?;
 
-      response.users = vec![
+      let person = get_or_fetch_and_upsert_person(person_uri, context, recursion_counter).await?;
+      ROR::Person(
         blocking(context.pool(), move |conn| {
           PersonViewSafe::read(conn, person.id)
         })
         .await??,
-      ];
+      )
     }
     SearchAcceptedObjects::Group(g) => {
       let community_uri = g.id(&query_url)?;
       let community =
         get_or_fetch_and_upsert_community(community_uri, context, recursion_counter).await?;
-
-      response.communities = vec![
+      ROR::Community(
         blocking(context.pool(), move |conn| {
           CommunityView::read(conn, community.id, None)
         })
         .await??,
-      ];
+      )
     }
     SearchAcceptedObjects::Page(p) => {
       let p = Post::from_apub(&p, context, &query_url, recursion_counter).await?;
-
-      response.posts =
-        vec![blocking(context.pool(), move |conn| PostView::read(conn, p.id, None)).await??];
+      ROR::Post(blocking(context.pool(), move |conn| PostView::read(conn, p.id, None)).await??)
     }
     SearchAcceptedObjects::Comment(c) => {
       let c = Comment::from_apub(&c, context, &query_url, recursion_counter).await?;
-
-      response.comments = vec![
+      ROR::Comment(
         blocking(context.pool(), move |conn| {
           CommentView::read(conn, c.id, None)
         })
         .await??,
-      ];
+      )
     }
-  };
-
-  Ok(response)
+  })
 }
 
 async fn delete_object_locally(query_url: &Url, context: &LemmyContext) -> Result<(), LemmyError> {
@@ -194,5 +195,5 @@ async fn delete_object_locally(query_url: &Url, context: &LemmyContext) -> Resul
       .await??;
     }
   }
-  Err(anyhow!("Object was deleted").into())
+  Ok(())
 }
index 2593cd94e2e5c5892e06b6bfe3134a62b5ad8459..7ea84c1f81533fbec6ad7cd91689c8948bbeafdf 100644 (file)
@@ -14,3 +14,6 @@ serde = { version = "1.0.127", features = ["derive"] }
 async-trait = "0.1.51"
 url = { version = "2.2.2", features = ["serde"] }
 serde_json = { version = "1.0.66", features = ["preserve_order"] }
+anyhow = "1.0.41"
+reqwest = { version = "0.11.4", features = ["json"] }
+log = "0.4.14"
index cc88b79c96d7aa528b6b3331464812a611b15552..846666ed2d6fcafa9a11ae71f11ab0780a566085 100644 (file)
@@ -6,6 +6,8 @@ use lemmy_utils::LemmyError;
 use lemmy_websocket::LemmyContext;
 use url::Url;
 
+pub mod webfinger;
+
 pub trait ActivityFields {
   fn id_unchecked(&self) -> &Url;
   fn actor(&self) -> &Url;
diff --git a/crates/apub_lib/src/webfinger.rs b/crates/apub_lib/src/webfinger.rs
new file mode 100644 (file)
index 0000000..ebd49ea
--- /dev/null
@@ -0,0 +1,72 @@
+use anyhow::anyhow;
+use lemmy_utils::{
+  request::{retry, RecvError},
+  settings::structs::Settings,
+  LemmyError,
+};
+use log::debug;
+use reqwest::Client;
+use serde::{Deserialize, Serialize};
+use url::Url;
+
+#[derive(Serialize, Deserialize, Debug)]
+pub struct WebfingerLink {
+  pub rel: Option<String>,
+  #[serde(rename(serialize = "type", deserialize = "type"))]
+  pub type_: Option<String>,
+  pub href: Option<Url>,
+  #[serde(skip_serializing_if = "Option::is_none")]
+  pub template: Option<String>,
+}
+
+#[derive(Serialize, Deserialize, Debug)]
+pub struct WebfingerResponse {
+  pub subject: String,
+  pub aliases: Vec<Url>,
+  pub links: Vec<WebfingerLink>,
+}
+
+pub enum WebfingerType {
+  Person,
+  Group,
+}
+
+/// Turns a person id like `@name@example.com` into an apub ID, like `https://example.com/user/name`,
+/// using webfinger.
+pub async fn webfinger_resolve_actor(
+  name: &str,
+  domain: &str,
+  webfinger_type: WebfingerType,
+  client: &Client,
+) -> Result<Url, LemmyError> {
+  let webfinger_type = match webfinger_type {
+    WebfingerType::Person => "acct",
+    WebfingerType::Group => "group",
+  };
+  let fetch_url = format!(
+    "{}://{}/.well-known/webfinger?resource={}:{}@{}",
+    Settings::get().get_protocol_string(),
+    domain,
+    webfinger_type,
+    name,
+    domain
+  );
+  debug!("Fetching webfinger url: {}", &fetch_url);
+
+  let response = retry(|| client.get(&fetch_url).send()).await?;
+
+  let res: WebfingerResponse = response
+    .json()
+    .await
+    .map_err(|e| RecvError(e.to_string()))?;
+
+  let link = res
+    .links
+    .iter()
+    .find(|l| l.type_.eq(&Some("application/activity+json".to_string())))
+    .ok_or_else(|| anyhow!("No application/activity+json link found."))?;
+  link
+    .href
+    .to_owned()
+    .ok_or_else(|| anyhow!("No href found.").into())
+}
index a29172ac258d610f652134db23def8a032f3e328..e47e7a36e89b4cec3f2eeb21ac92daddf65fc98f 100644 (file)
@@ -16,6 +16,7 @@ lemmy_db_views = { version = "=0.11.3", path = "../db_views" }
 lemmy_db_views_actor = { version = "=0.11.3", path = "../db_views_actor" }
 lemmy_db_schema = { version = "=0.11.3", path = "../db_schema" }
 lemmy_api_common = { version = "=0.11.3", path = "../api_common" }
+lemmy_apub_lib = { version = "=0.11.3", path = "../apub_lib" }
 diesel = "1.4.7"
 actix = "0.12.0"
 actix-web = { version = "4.0.0-beta.8", default-features = false, features = ["rustls"] }
index 5d4be74c4bf6e6c7a991d627bc3b3807c608cf5e..6773218877a22f7c12230a6cc26498bf12a1e31c 100644 (file)
@@ -1,6 +1,7 @@
 use actix_web::{error::ErrorBadRequest, web::Query, *};
 use anyhow::anyhow;
-use lemmy_api_common::{blocking, WebFingerLink, WebFingerResponse};
+use lemmy_api_common::blocking;
+use lemmy_apub_lib::webfinger::{WebfingerLink, WebfingerResponse};
 use lemmy_db_queries::source::{community::Community_, person::Person_};
 use lemmy_db_schema::source::{community::Community, person::Person};
 use lemmy_utils::{
@@ -68,17 +69,17 @@ async fn get_webfinger_response(
     return Err(ErrorBadRequest(LemmyError::from(anyhow!("not_found"))));
   };
 
-  let json = WebFingerResponse {
+  let json = WebfingerResponse {
     subject: info.resource.to_owned(),
     aliases: vec![url.to_owned().into()],
     links: vec![
-      WebFingerLink {
+      WebfingerLink {
         rel: Some("http://webfinger.net/rel/profile-page".to_string()),
         type_: Some("text/html".to_string()),
         href: Some(url.to_owned().into()),
         template: None,
       },
-      WebFingerLink {
+      WebfingerLink {
         rel: Some("self".to_string()),
         type_: Some("application/activity+json".to_string()),
         href: Some(url.into()),
index fbb2bb512d1e4751a8f1cabed712f9410ac64129..d501e9611d7ae2f5871abd004860ff87db5d1fb2 100644 (file)
@@ -110,6 +110,7 @@ pub enum UserOperation {
   AddAdmin,
   BanPerson,
   Search,
+  ResolveObject,
   MarkAllAsRead,
   SaveUserSettings,
   TransferCommunity,
index feaf0e235e618bfd061711170048189ad1caa20e..1d76af002ae509c19c15d4ec7bf86e116412aa23 100644 (file)
@@ -33,6 +33,11 @@ pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimit) {
           .wrap(rate_limit.message())
           .route(web::get().to(route_get::<Search>)),
       )
+      .service(
+        web::resource("/resolve_object")
+          .wrap(rate_limit.message())
+          .route(web::get().to(route_get::<ResolveObject>)),
+      )
       // Community
       .service(
         web::resource("/community")