]> Untitled Git - lemmy.git/commitdiff
Fix problem that prevented viewing of pleroma user profiles
authorFelix Ableitner <me@nutomic.com>
Mon, 25 Oct 2021 16:09:21 +0000 (18:09 +0200)
committerFelix Ableitner <me@nutomic.com>
Fri, 29 Oct 2021 10:45:53 +0000 (12:45 +0200)
14 files changed:
Cargo.lock
crates/api/Cargo.toml
crates/api/src/site.rs
crates/api_crud/src/comment/create.rs
crates/api_crud/src/comment/read.rs
crates/api_crud/src/community/create.rs
crates/api_crud/src/community/read.rs
crates/api_crud/src/post/create.rs
crates/api_crud/src/post/read.rs
crates/api_crud/src/private_message/create.rs
crates/api_crud/src/user/create.rs
crates/api_crud/src/user/read.rs
crates/apub/src/lib.rs
src/code_migrations.rs

index 9931aef1364f7a5570cfa3dee224f74f197d5bdd..0b8f6413a9d4fd818e3b147cc3d31823f90d1232 100644 (file)
@@ -1728,6 +1728,7 @@ dependencies = [
  "lazy_static",
  "lemmy_api_common",
  "lemmy_apub",
+ "lemmy_apub_lib",
  "lemmy_db_schema",
  "lemmy_db_views",
  "lemmy_db_views_actor",
index 57a9406fbb6d827281d37f0837304ac243607729..39be5b5ad48de44523a8f13c8995aa75757f8de5 100644 (file)
@@ -14,6 +14,7 @@ doctest = false
 
 [dependencies]
 lemmy_apub = { version = "=0.13.5-rc.7", path = "../apub" }
+lemmy_apub_lib = { version = "=0.13.5-rc.7", path = "../apub_lib" }
 lemmy_utils = { version = "=0.13.5-rc.7", path = "../utils" }
 lemmy_db_schema = { version = "=0.13.5-rc.7", path = "../db_schema" }
 lemmy_db_views = { version = "=0.13.5-rc.7", path = "../db_views" }
index 2797955a5dae79875789d103f027dbeb20236ee7..8d89d5db6a39921775dc63e71243e59a2d4459c0 100644 (file)
@@ -11,10 +11,10 @@ use lemmy_api_common::{
   site::*,
 };
 use lemmy_apub::{
-  build_actor_id_from_shortname,
   fetcher::search::{search_by_apub_id, SearchableObjects},
-  EndpointType,
+  get_actor_id_from_name,
 };
+use lemmy_apub_lib::webfinger::WebfingerType;
 use lemmy_db_schema::{
   from_opt_str_to_opt_enum,
   newtypes::PersonId,
@@ -174,11 +174,13 @@ impl Perform for Search {
     let listing_type: Option<ListingType> = from_opt_str_to_opt_enum(&data.listing_type);
     let search_type: SearchType = from_opt_str_to_opt_enum(&data.type_).unwrap_or(SearchType::All);
     let community_id = data.community_id;
-    let community_actor_id = data
-      .community_name
-      .as_ref()
-      .map(|t| build_actor_id_from_shortname(EndpointType::Community, t, &context.settings()).ok())
-      .unwrap_or(None);
+    let community_actor_id = if let Some(name) = &data.community_name {
+      get_actor_id_from_name(WebfingerType::Group, name, context)
+        .await
+        .ok()
+    } else {
+      None
+    };
     let creator_id = data.creator_id;
     match search_type {
       SearchType::Posts => {
index 690a50820438425b2ff5e1837fdb290b8b1746eb..6c90e94e1a95057d055e6b1e71ba5e256a8a63a7 100644 (file)
@@ -17,7 +17,7 @@ use lemmy_apub::{
     CreateOrUpdateType,
   },
   fetcher::post_or_comment::PostOrComment,
-  generate_apub_endpoint,
+  generate_local_apub_endpoint,
   EndpointType,
 };
 use lemmy_db_schema::{
@@ -109,7 +109,7 @@ impl PerformCrud for CreateComment {
 
     let updated_comment: Comment =
       blocking(context.pool(), move |conn| -> Result<Comment, LemmyError> {
-        let apub_id = generate_apub_endpoint(
+        let apub_id = generate_local_apub_endpoint(
           EndpointType::Comment,
           &inserted_comment_id.to_string(),
           &protocol_and_hostname,
index 490c9670fdacb2f24005faf4a38d33acd7b6cfc6..05f9f90fb25e4f6b790cc2ae367cd20e3c22b0f6 100644 (file)
@@ -1,7 +1,8 @@
 use crate::PerformCrud;
 use actix_web::web::Data;
 use lemmy_api_common::{blocking, comment::*, get_local_user_view_from_jwt_opt};
-use lemmy_apub::{build_actor_id_from_shortname, EndpointType};
+use lemmy_apub::get_actor_id_from_name;
+use lemmy_apub_lib::webfinger::WebfingerType;
 use lemmy_db_schema::{
   from_opt_str_to_opt_enum,
   traits::DeleteableOrRemoveable,
@@ -34,11 +35,13 @@ impl PerformCrud for GetComments {
     let listing_type: Option<ListingType> = from_opt_str_to_opt_enum(&data.type_);
 
     let community_id = data.community_id;
-    let community_actor_id = data
-      .community_name
-      .as_ref()
-      .map(|t| build_actor_id_from_shortname(EndpointType::Community, t, &context.settings()).ok())
-      .unwrap_or(None);
+    let community_actor_id = if let Some(name) = &data.community_name {
+      get_actor_id_from_name(WebfingerType::Group, name, context)
+        .await
+        .ok()
+    } else {
+      None
+    };
     let saved_only = data.saved_only;
     let page = data.page;
     let limit = data.limit;
index 8b7daeb7b7ac834e0828fa889a70db3fd4277bac..523ccf00dd66aa0ff31c7ad043cd38d27ac80b49 100644 (file)
@@ -8,9 +8,9 @@ use lemmy_api_common::{
 };
 use lemmy_apub::{
   fetcher::object_id::ObjectId,
-  generate_apub_endpoint,
   generate_followers_url,
   generate_inbox_url,
+  generate_local_apub_endpoint,
   generate_shared_inbox_url,
   objects::community::ApubCommunity,
   EndpointType,
@@ -67,7 +67,7 @@ impl PerformCrud for CreateCommunity {
     }
 
     // Double check for duplicate community actor_ids
-    let community_actor_id = generate_apub_endpoint(
+    let community_actor_id = generate_local_apub_endpoint(
       EndpointType::Community,
       &data.name,
       &context.settings().get_protocol_and_hostname(),
index 91effae8752b6bff54a7ae6086421bd2afbd14e6..feaf110fb743dd1ab09b819a73377a2feb984546 100644 (file)
@@ -2,11 +2,11 @@ use crate::PerformCrud;
 use actix_web::web::Data;
 use lemmy_api_common::{blocking, community::*, get_local_user_view_from_jwt_opt};
 use lemmy_apub::{
-  build_actor_id_from_shortname,
   fetcher::object_id::ObjectId,
+  get_actor_id_from_name,
   objects::community::ApubCommunity,
-  EndpointType,
 };
+use lemmy_apub_lib::webfinger::WebfingerType;
 use lemmy_db_schema::{
   from_opt_str_to_opt_enum,
   traits::DeleteableOrRemoveable,
@@ -39,7 +39,7 @@ impl PerformCrud for GetCommunity {
       None => {
         let name = data.name.to_owned().unwrap_or_else(|| "main".to_string());
         let community_actor_id =
-          build_actor_id_from_shortname(EndpointType::Community, &name, &context.settings())?;
+          get_actor_id_from_name(WebfingerType::Group, &name, context).await?;
 
         ObjectId::<ApubCommunity>::new(community_actor_id)
           .dereference(context, &mut 0)
index 3e5fb41e1ff5a2ddb5679b862dd80488fb839450..a6641034175d9d8a412159afce2c5a0ff22336a9 100644 (file)
@@ -16,7 +16,7 @@ use lemmy_apub::{
     CreateOrUpdateType,
   },
   fetcher::post_or_comment::PostOrComment,
-  generate_apub_endpoint,
+  generate_local_apub_endpoint,
   EndpointType,
 };
 use lemmy_db_schema::{
@@ -98,7 +98,7 @@ impl PerformCrud for CreatePost {
     let inserted_post_id = inserted_post.id;
     let protocol_and_hostname = context.settings().get_protocol_and_hostname();
     let updated_post = blocking(context.pool(), move |conn| -> Result<Post, LemmyError> {
-      let apub_id = generate_apub_endpoint(
+      let apub_id = generate_local_apub_endpoint(
         EndpointType::Post,
         &inserted_post_id.to_string(),
         &protocol_and_hostname,
index 901d1e9350b2eb2567f0691a46ef8ac63885adf8..d2a46f53e5f5dd5a8554996dfcc50098087f5ee5 100644 (file)
@@ -1,7 +1,8 @@
 use crate::PerformCrud;
 use actix_web::web::Data;
 use lemmy_api_common::{blocking, get_local_user_view_from_jwt_opt, mark_post_as_read, post::*};
-use lemmy_apub::{build_actor_id_from_shortname, EndpointType};
+use lemmy_apub::get_actor_id_from_name;
+use lemmy_apub_lib::webfinger::WebfingerType;
 use lemmy_db_schema::{
   from_opt_str_to_opt_enum,
   traits::DeleteableOrRemoveable,
@@ -136,11 +137,13 @@ impl PerformCrud for GetPosts {
     let page = data.page;
     let limit = data.limit;
     let community_id = data.community_id;
-    let community_actor_id = data
-      .community_name
-      .as_ref()
-      .map(|t| build_actor_id_from_shortname(EndpointType::Community, t, &context.settings()).ok())
-      .unwrap_or(None);
+    let community_actor_id = if let Some(name) = &data.community_name {
+      get_actor_id_from_name(WebfingerType::Group, name, context)
+        .await
+        .ok()
+    } else {
+      None
+    };
     let saved_only = data.saved_only;
 
     let mut posts = blocking(context.pool(), move |conn| {
index d6560be257ca033d7e29245b1e554131acf093dd..c7bca4e0249f401555760144d8a8960608741e44 100644 (file)
@@ -11,7 +11,7 @@ use lemmy_apub::{
     private_message::create_or_update::CreateOrUpdatePrivateMessage,
     CreateOrUpdateType,
   },
-  generate_apub_endpoint,
+  generate_local_apub_endpoint,
   EndpointType,
 };
 use lemmy_db_schema::{
@@ -67,7 +67,7 @@ impl PerformCrud for CreatePrivateMessage {
     let updated_private_message = blocking(
       context.pool(),
       move |conn| -> Result<PrivateMessage, LemmyError> {
-        let apub_id = generate_apub_endpoint(
+        let apub_id = generate_local_apub_endpoint(
           EndpointType::PrivateMessage,
           &inserted_private_message_id.to_string(),
           &protocol_and_hostname,
index ce37cd6a07ac1dd456368954a7710605e8f81156..78b204ff95e8b7786dbb3107600da6ad5b174239 100644 (file)
@@ -2,9 +2,9 @@ use crate::PerformCrud;
 use actix_web::web::Data;
 use lemmy_api_common::{blocking, honeypot_check, password_length_check, person::*};
 use lemmy_apub::{
-  generate_apub_endpoint,
   generate_followers_url,
   generate_inbox_url,
+  generate_local_apub_endpoint,
   generate_shared_inbox_url,
   EndpointType,
 };
@@ -96,7 +96,7 @@ impl PerformCrud for Register {
     if !is_valid_actor_name(&data.username, context.settings().actor_name_max_length) {
       return Err(ApiError::err_plain("invalid_username").into());
     }
-    let actor_id = generate_apub_endpoint(
+    let actor_id = generate_local_apub_endpoint(
       EndpointType::Person,
       &data.username,
       &context.settings().get_protocol_and_hostname(),
@@ -179,7 +179,7 @@ impl PerformCrud for Register {
       Ok(c) => c,
       Err(_e) => {
         let default_community_name = "main";
-        let actor_id = generate_apub_endpoint(
+        let actor_id = generate_local_apub_endpoint(
           EndpointType::Community,
           default_community_name,
           &protocol_and_hostname,
index a5b69ba45653893fc61ba08500bbad68e38759de..b649c8fbb28581c73cec1c76f08aeed1274c8296 100644 (file)
@@ -2,11 +2,11 @@ use crate::PerformCrud;
 use actix_web::web::Data;
 use lemmy_api_common::{blocking, get_local_user_view_from_jwt_opt, person::*};
 use lemmy_apub::{
-  build_actor_id_from_shortname,
   fetcher::object_id::ObjectId,
+  get_actor_id_from_name,
   objects::person::ApubPerson,
-  EndpointType,
 };
+use lemmy_apub_lib::webfinger::WebfingerType;
 use lemmy_db_schema::{from_opt_str_to_opt_enum, SortType};
 use lemmy_db_views::{comment_view::CommentQueryBuilder, post_view::PostQueryBuilder};
 use lemmy_db_views_actor::{
@@ -46,8 +46,7 @@ impl PerformCrud for GetPersonDetails {
           .username
           .to_owned()
           .unwrap_or_else(|| "admin".to_string());
-        let actor_id =
-          build_actor_id_from_shortname(EndpointType::Person, &name, &context.settings())?;
+        let actor_id = get_actor_id_from_name(WebfingerType::Person, &name, context).await?;
 
         let person = ObjectId::<ApubPerson>::new(actor_id)
           .dereference(context, &mut 0)
index 675798b6fe087de7d8288e1ec93965ffe052c72b..a900ad3d13794d12c1192566d1f98c8b7ed1914e 100644 (file)
@@ -12,7 +12,11 @@ extern crate lazy_static;
 use crate::fetcher::post_or_comment::PostOrComment;
 use anyhow::{anyhow, Context};
 use lemmy_api_common::blocking;
-use lemmy_apub_lib::{activity_queue::send_activity, traits::ActorType};
+use lemmy_apub_lib::{
+  activity_queue::send_activity,
+  traits::ActorType,
+  webfinger::{webfinger_resolve_actor, WebfingerType},
+};
 use lemmy_db_schema::{
   newtypes::{CommunityId, DbUrl},
   source::{activity::Activity, person::Person},
@@ -111,7 +115,7 @@ pub enum EndpointType {
 }
 
 /// Generates an apub endpoint for a given domain, IE xyz.tld
-fn generate_apub_endpoint_for_domain(
+pub fn generate_local_apub_endpoint(
   endpoint_type: EndpointType,
   name: &str,
   domain: &str,
@@ -127,15 +131,6 @@ fn generate_apub_endpoint_for_domain(
   Ok(Url::parse(&format!("{}/{}/{}", domain, point, name))?.into())
 }
 
-/// Generates the ActivityPub ID for a given object type and ID.
-pub fn generate_apub_endpoint(
-  endpoint_type: EndpointType,
-  name: &str,
-  protocol_and_hostname: &str,
-) -> Result<DbUrl, ParseError> {
-  generate_apub_endpoint_for_domain(endpoint_type, name, protocol_and_hostname)
-}
-
 pub fn generate_followers_url(actor_id: &DbUrl) -> Result<DbUrl, ParseError> {
   Ok(Url::parse(&format!("{}/followers", actor_id))?.into())
 }
@@ -169,23 +164,31 @@ fn generate_moderators_url(community_id: &DbUrl) -> Result<DbUrl, LemmyError> {
 
 /// Takes in a shortname of the type dessalines@xyz.tld or dessalines (assumed to be local), and outputs the actor id.
 /// Used in the API for communities and users.
-pub fn build_actor_id_from_shortname(
-  endpoint_type: EndpointType,
+pub async fn get_actor_id_from_name(
+  webfinger_type: WebfingerType,
   short_name: &str,
-  settings: &Settings,
-) -> Result<DbUrl, ParseError> {
+  context: &LemmyContext,
+) -> Result<DbUrl, LemmyError> {
   let split = short_name.split('@').collect::<Vec<&str>>();
 
   let name = split[0];
 
   // If there's no @, its local
-  let domain = if split.len() == 1 {
-    settings.get_protocol_and_hostname()
+  if split.len() == 1 {
+    let domain = context.settings().get_protocol_and_hostname();
+    let endpoint_type = match webfinger_type {
+      WebfingerType::Person => EndpointType::Person,
+      WebfingerType::Group => EndpointType::Community,
+    };
+    Ok(generate_local_apub_endpoint(endpoint_type, name, &domain)?)
   } else {
-    format!("{}://{}", settings.get_protocol_string(), split[1])
-  };
-
-  generate_apub_endpoint_for_domain(endpoint_type, name, &domain)
+    let protocol = context.settings().get_protocol_string();
+    Ok(
+      webfinger_resolve_actor(name, split[1], webfinger_type, context.client(), protocol)
+        .await?
+        .into(),
+    )
+  }
 }
 
 /// Store a sent or received activity in the database, for logging purposes. These records are not
index 8910733a515f1604bf1e65fd1487e99d4beec393..2feefbdfb82909b6ef93ea3aa4233253e5be759d 100644 (file)
@@ -4,9 +4,9 @@ use diesel::{
   *,
 };
 use lemmy_apub::{
-  generate_apub_endpoint,
   generate_followers_url,
   generate_inbox_url,
+  generate_local_apub_endpoint,
   generate_shared_inbox_url,
   EndpointType,
 };
@@ -58,7 +58,7 @@ fn user_updates_2020_04_02(
 
     let form = PersonForm {
       name: cperson.name.to_owned(),
-      actor_id: Some(generate_apub_endpoint(
+      actor_id: Some(generate_local_apub_endpoint(
         EndpointType::Person,
         &cperson.name,
         protocol_and_hostname,
@@ -93,7 +93,7 @@ fn community_updates_2020_04_02(
 
   for ccommunity in &incorrect_communities {
     let keypair = generate_actor_keypair()?;
-    let community_actor_id = generate_apub_endpoint(
+    let community_actor_id = generate_local_apub_endpoint(
       EndpointType::Community,
       &ccommunity.name,
       protocol_and_hostname,
@@ -143,7 +143,7 @@ fn post_updates_2020_04_03(
     .load::<Post>(conn)?;
 
   for cpost in &incorrect_posts {
-    let apub_id = generate_apub_endpoint(
+    let apub_id = generate_local_apub_endpoint(
       EndpointType::Post,
       &cpost.id.to_string(),
       protocol_and_hostname,
@@ -171,7 +171,7 @@ fn comment_updates_2020_04_03(
     .load::<Comment>(conn)?;
 
   for ccomment in &incorrect_comments {
-    let apub_id = generate_apub_endpoint(
+    let apub_id = generate_local_apub_endpoint(
       EndpointType::Comment,
       &ccomment.id.to_string(),
       protocol_and_hostname,
@@ -199,7 +199,7 @@ fn private_message_updates_2020_05_05(
     .load::<PrivateMessage>(conn)?;
 
   for cpm in &incorrect_pms {
-    let apub_id = generate_apub_endpoint(
+    let apub_id = generate_local_apub_endpoint(
       EndpointType::PrivateMessage,
       &cpm.id.to_string(),
       protocol_and_hostname,