]> Untitled Git - lemmy.git/commitdiff
Move code to generate apub urls into lemmy_api_common
authorFelix Ableitner <me@nutomic.com>
Sat, 26 Nov 2022 20:47:13 +0000 (21:47 +0100)
committerFelix Ableitner <me@nutomic.com>
Fri, 2 Dec 2022 09:46:49 +0000 (10:46 +0100)
19 files changed:
crates/api_common/src/lib.rs
crates/api_crud/src/comment/create.rs
crates/api_crud/src/community/create.rs
crates/api_crud/src/post/create.rs
crates/api_crud/src/private_message/create.rs
crates/api_crud/src/site/create.rs
crates/api_crud/src/user/create.rs
crates/apub/src/activities/community/add_mod.rs
crates/apub/src/activities/community/remove_mod.rs
crates/apub/src/activities/mod.rs
crates/apub/src/collections/community_moderators.rs
crates/apub/src/collections/community_outbox.rs
crates/apub/src/http/community.rs
crates/apub/src/http/person.rs
crates/apub/src/lib.rs
crates/apub/src/objects/community.rs
crates/apub/src/objects/person.rs
crates/apub/src/protocol/collections/group_followers.rs
src/code_migrations.rs

index bcd603e5f150e3982d12ec18ef30b3cebd7b7dad..990780e151760209d2dc69fb3a2758fe6c033c46 100644 (file)
@@ -20,12 +20,16 @@ pub extern crate lemmy_db_views_moderator;
 
 use crate::websocket::chat_server::ChatServer;
 use actix::Addr;
-use lemmy_db_schema::{source::secret::Secret, utils::DbPool};
+use anyhow::Context;
+use lemmy_db_schema::{newtypes::DbUrl, source::secret::Secret, utils::DbPool};
 use lemmy_utils::{
+  error::LemmyError,
+  location_info,
   rate_limit::RateLimitCell,
   settings::{structs::Settings, SETTINGS},
 };
 use reqwest_middleware::ClientWithMiddleware;
+use url::{ParseError, Url};
 
 pub struct LemmyContext {
   pool: DbPool,
@@ -86,3 +90,65 @@ impl Clone for LemmyContext {
     }
   }
 }
+
+pub enum EndpointType {
+  Community,
+  Person,
+  Post,
+  Comment,
+  PrivateMessage,
+}
+
+/// Generates an apub endpoint for a given domain, IE xyz.tld
+pub fn generate_local_apub_endpoint(
+  endpoint_type: EndpointType,
+  name: &str,
+  domain: &str,
+) -> Result<DbUrl, ParseError> {
+  let point = match endpoint_type {
+    EndpointType::Community => "c",
+    EndpointType::Person => "u",
+    EndpointType::Post => "post",
+    EndpointType::Comment => "comment",
+    EndpointType::PrivateMessage => "private_message",
+  };
+
+  Ok(Url::parse(&format!("{}/{}/{}", domain, point, name))?.into())
+}
+
+pub fn generate_followers_url(actor_id: &DbUrl) -> Result<DbUrl, ParseError> {
+  Ok(Url::parse(&format!("{}/followers", actor_id))?.into())
+}
+
+pub fn generate_inbox_url(actor_id: &DbUrl) -> Result<DbUrl, ParseError> {
+  Ok(Url::parse(&format!("{}/inbox", actor_id))?.into())
+}
+
+pub fn generate_site_inbox_url(actor_id: &DbUrl) -> Result<DbUrl, ParseError> {
+  let mut actor_id: Url = actor_id.clone().into();
+  actor_id.set_path("site_inbox");
+  Ok(actor_id.into())
+}
+
+pub fn generate_shared_inbox_url(actor_id: &DbUrl) -> Result<DbUrl, LemmyError> {
+  let actor_id: Url = actor_id.clone().into();
+  let url = format!(
+    "{}://{}{}/inbox",
+    &actor_id.scheme(),
+    &actor_id.host_str().context(location_info!())?,
+    if let Some(port) = actor_id.port() {
+      format!(":{}", port)
+    } else {
+      String::new()
+    },
+  );
+  Ok(Url::parse(&url)?.into())
+}
+
+pub fn generate_outbox_url(actor_id: &DbUrl) -> Result<DbUrl, ParseError> {
+  Ok(Url::parse(&format!("{}/outbox", actor_id))?.into())
+}
+
+pub fn generate_moderators_url(community_id: &DbUrl) -> Result<DbUrl, LemmyError> {
+  Ok(Url::parse(&format!("{}/moderators", community_id))?.into())
+}
index a3be10a63c175e3bd60bdd6ed669b1c0dc2dc982..5d9603ac320e5859239d79c06b58ed5fd5c6f5cb 100644 (file)
@@ -2,6 +2,7 @@ use crate::PerformCrud;
 use actix_web::web::Data;
 use lemmy_api_common::{
   comment::{CommentResponse, CreateComment},
+  generate_local_apub_endpoint,
   utils::{
     check_community_ban,
     check_community_deleted_or_removed,
@@ -14,13 +15,12 @@ use lemmy_api_common::{
     send::{send_comment_ws_message, send_local_notifs},
     UserOperationCrud,
   },
+  EndpointType,
   LemmyContext,
 };
 use lemmy_apub::{
-  generate_local_apub_endpoint,
   objects::comment::ApubComment,
   protocol::activities::{create_or_update::note::CreateOrUpdateNote, CreateOrUpdateType},
-  EndpointType,
 };
 use lemmy_db_schema::{
   source::{
index caa1a363d4625c6315ac95cd18af66b681a47f4c..11a30766831cfa98162b2fef460114f53a283aeb 100644 (file)
@@ -3,17 +3,15 @@ use activitypub_federation::core::{object_id::ObjectId, signatures::generate_act
 use actix_web::web::Data;
 use lemmy_api_common::{
   community::{CommunityResponse, CreateCommunity},
-  utils::{get_local_user_view_from_jwt, is_admin, local_site_to_slur_regex},
-  LemmyContext,
-};
-use lemmy_apub::{
   generate_followers_url,
   generate_inbox_url,
   generate_local_apub_endpoint,
   generate_shared_inbox_url,
-  objects::community::ApubCommunity,
+  utils::{get_local_user_view_from_jwt, is_admin, local_site_to_slur_regex},
   EndpointType,
+  LemmyContext,
 };
+use lemmy_apub::objects::community::ApubCommunity;
 use lemmy_db_schema::{
   source::community::{
     Community,
index 2353def6d266fec806495c26687dac824f7d6c82..46d4a7f644befeea10225eada2420b994ff186b8 100644 (file)
@@ -1,6 +1,7 @@
 use crate::PerformCrud;
 use actix_web::web::Data;
 use lemmy_api_common::{
+  generate_local_apub_endpoint,
   post::{CreatePost, PostResponse},
   request::fetch_site_data,
   utils::{
@@ -12,13 +13,12 @@ use lemmy_api_common::{
     mark_post_as_read,
   },
   websocket::{send::send_post_ws_message, UserOperationCrud},
+  EndpointType,
   LemmyContext,
 };
 use lemmy_apub::{
-  generate_local_apub_endpoint,
   objects::post::ApubPost,
   protocol::activities::{create_or_update::page::CreateOrUpdatePage, CreateOrUpdateType},
-  EndpointType,
 };
 use lemmy_db_schema::{
   impls::actor_language::default_post_language,
index 6f099f79367e2aaa709e744e88239bffad6b849e..03d994c08b79e578d0c5a5223d8b66ea78fc7886 100644 (file)
@@ -1,6 +1,7 @@
 use crate::PerformCrud;
 use actix_web::web::Data;
 use lemmy_api_common::{
+  generate_local_apub_endpoint,
   private_message::{CreatePrivateMessage, PrivateMessageResponse},
   utils::{
     check_person_block,
@@ -10,15 +11,12 @@ use lemmy_api_common::{
     send_email_to_user,
   },
   websocket::{send::send_pm_ws_message, UserOperationCrud},
+  EndpointType,
   LemmyContext,
 };
-use lemmy_apub::{
-  generate_local_apub_endpoint,
-  protocol::activities::{
-    create_or_update::chat_message::CreateOrUpdateChatMessage,
-    CreateOrUpdateType,
-  },
-  EndpointType,
+use lemmy_apub::protocol::activities::{
+  create_or_update::chat_message::CreateOrUpdateChatMessage,
+  CreateOrUpdateType,
 };
 use lemmy_db_schema::{
   source::{
index 22631d5146e5de419797436cd2857f62503124e9..34ac404a946e6aee9d94a664eaf1ce9302bf6f7a 100644 (file)
@@ -2,6 +2,7 @@ use crate::PerformCrud;
 use activitypub_federation::core::signatures::generate_actor_keypair;
 use actix_web::web::Data;
 use lemmy_api_common::{
+  generate_site_inbox_url,
   site::{CreateSite, SiteResponse},
   utils::{
     get_local_user_view_from_jwt,
@@ -12,7 +13,6 @@ use lemmy_api_common::{
   },
   LemmyContext,
 };
-use lemmy_apub::generate_site_inbox_url;
 use lemmy_db_schema::{
   newtypes::DbUrl,
   source::{
index 5686348f7710697a716f2acd23a5369b5fb31d40..0521ffe0ced7274627e1b454f2d351a8cfc3010b 100644 (file)
@@ -2,6 +2,9 @@ use crate::PerformCrud;
 use activitypub_federation::core::signatures::generate_actor_keypair;
 use actix_web::web::Data;
 use lemmy_api_common::{
+  generate_inbox_url,
+  generate_local_apub_endpoint,
+  generate_shared_inbox_url,
   person::{LoginResponse, Register},
   utils::{
     honeypot_check,
@@ -11,13 +14,8 @@ use lemmy_api_common::{
     send_verification_email,
   },
   websocket::messages::CheckCaptcha,
-  LemmyContext,
-};
-use lemmy_apub::{
-  generate_inbox_url,
-  generate_local_apub_endpoint,
-  generate_shared_inbox_url,
   EndpointType,
+  LemmyContext,
 };
 use lemmy_db_schema::{
   aggregates::structs::PersonAggregates,
index 2bdf2be5e6276ea086b1945b2188a3ab1812ddd6..3718ea680edd28a0d3edb43ec1f9f9c2cea71847 100644 (file)
@@ -8,7 +8,6 @@ use crate::{
     verify_person_in_community,
   },
   activity_lists::AnnouncableActivities,
-  generate_moderators_url,
   local_instance,
   objects::{community::ApubCommunity, person::ApubPerson},
   protocol::{activities::community::add_mod::AddMod, InCommunity},
@@ -20,7 +19,7 @@ use activitypub_federation::{
   traits::{ActivityHandler, Actor},
 };
 use activitystreams_kinds::{activity::AddType, public};
-use lemmy_api_common::LemmyContext;
+use lemmy_api_common::{generate_moderators_url, LemmyContext};
 use lemmy_db_schema::{
   source::{
     community::{CommunityModerator, CommunityModeratorForm},
index ca8d5b042810cee770cf33624d03d24de80ab430..855fe7b4ed22899c01fdface6e0b01302f174577 100644 (file)
@@ -8,7 +8,6 @@ use crate::{
     verify_person_in_community,
   },
   activity_lists::AnnouncableActivities,
-  generate_moderators_url,
   local_instance,
   objects::{community::ApubCommunity, person::ApubPerson},
   protocol::{activities::community::remove_mod::RemoveMod, InCommunity},
@@ -20,7 +19,7 @@ use activitypub_federation::{
   traits::{ActivityHandler, Actor},
 };
 use activitystreams_kinds::{activity::RemoveType, public};
-use lemmy_api_common::LemmyContext;
+use lemmy_api_common::{generate_moderators_url, LemmyContext};
 use lemmy_db_schema::{
   source::{
     community::{CommunityModerator, CommunityModeratorForm},
index 1528a273c5ebaff28a13100bb23633b3c7f28593..68a3b535c10bd16ddb5c9b6c7e1d5a2e4c4fcab2 100644 (file)
@@ -1,5 +1,4 @@
 use crate::{
-  generate_moderators_url,
   insert_activity,
   local_instance,
   objects::{community::ApubCommunity, person::ApubPerson},
@@ -13,7 +12,7 @@ use activitypub_federation::{
 };
 use activitystreams_kinds::public;
 use anyhow::anyhow;
-use lemmy_api_common::LemmyContext;
+use lemmy_api_common::{generate_moderators_url, LemmyContext};
 use lemmy_db_schema::{
   newtypes::CommunityId,
   source::{community::Community, local_site::LocalSite},
index b7575370880e0df254fe257cc35385908e6abc8d..aa38f8bee2ae63a6307deee0c5c36b024c975e6e 100644 (file)
@@ -1,6 +1,5 @@
 use crate::{
   collections::CommunityContext,
-  generate_moderators_url,
   local_instance,
   objects::person::ApubPerson,
   protocol::collections::group_moderators::GroupModerators,
@@ -12,6 +11,7 @@ use activitypub_federation::{
 };
 use activitystreams_kinds::collection::OrderedCollectionType;
 use chrono::NaiveDateTime;
+use lemmy_api_common::generate_moderators_url;
 use lemmy_db_schema::{
   source::community::{CommunityModerator, CommunityModeratorForm},
   traits::Joinable,
index c7742de725451300365b082cde4ab2a6b11a7a17..44685430e7bdf2046f218edeb2129a602b25860d 100644 (file)
@@ -1,7 +1,6 @@
 use crate::{
   activity_lists::AnnouncableActivities,
   collections::CommunityContext,
-  generate_outbox_url,
   objects::post::ApubPost,
   protocol::{
     activities::{
@@ -20,6 +19,7 @@ use activitypub_federation::{
 use activitystreams_kinds::collection::OrderedCollectionType;
 use chrono::NaiveDateTime;
 use futures::future::join_all;
+use lemmy_api_common::generate_outbox_url;
 use lemmy_db_schema::{
   source::{person::Person, post::Post},
   traits::Crud,
index 5b1a5b92647f85038ece56448538edbfc43cb8e5..487ad5512722245ae0e7ac7636cfcffd41d7e59c 100644 (file)
@@ -5,7 +5,6 @@ use crate::{
     community_outbox::ApubCommunityOutbox,
     CommunityContext,
   },
-  generate_outbox_url,
   http::{create_apub_response, create_apub_tombstone_response, receive_lemmy_activity},
   local_instance,
   objects::{community::ApubCommunity, person::ApubPerson},
@@ -17,7 +16,7 @@ use activitypub_federation::{
   traits::ApubObject,
 };
 use actix_web::{web, HttpRequest, HttpResponse};
-use lemmy_api_common::LemmyContext;
+use lemmy_api_common::{generate_outbox_url, LemmyContext};
 use lemmy_db_schema::{source::community::Community, traits::ApubActor};
 use lemmy_utils::error::LemmyError;
 use serde::Deserialize;
index 19c57b20c4e345d21e4c7574bc5650b3b4f18b9b..0e838fe137a7ddb7d78478c73ad8ae2288f3c560 100644 (file)
@@ -1,14 +1,13 @@
 use crate::{
   activity_lists::PersonInboxActivitiesWithAnnouncable,
   fetcher::user_or_community::UserOrCommunity,
-  generate_outbox_url,
   http::{create_apub_response, create_apub_tombstone_response, receive_lemmy_activity},
   objects::person::ApubPerson,
   protocol::collections::empty_outbox::EmptyOutbox,
 };
 use activitypub_federation::{deser::context::WithContext, traits::ApubObject};
 use actix_web::{web, HttpRequest, HttpResponse};
-use lemmy_api_common::LemmyContext;
+use lemmy_api_common::{generate_outbox_url, LemmyContext};
 use lemmy_db_schema::{source::person::Person, traits::ApubActor};
 use lemmy_utils::error::LemmyError;
 use serde::Deserialize;
index 48fd58f1d35c31ea04a457e94803ab5536b11910..92ae014b1fa63da7c6f765d5fb155ab729d36c79 100644 (file)
@@ -6,18 +6,16 @@ use activitypub_federation::{
   LocalInstance,
   UrlVerifier,
 };
-use anyhow::Context;
 use async_trait::async_trait;
 use lemmy_api_common::LemmyContext;
 use lemmy_db_schema::{
-  newtypes::DbUrl,
   source::{activity::Activity, instance::Instance, local_site::LocalSite},
   utils::DbPool,
 };
-use lemmy_utils::{error::LemmyError, location_info, settings::structs::Settings};
+use lemmy_utils::{error::LemmyError, settings::structs::Settings};
 use once_cell::sync::Lazy;
 use tokio::sync::OnceCell;
-use url::{ParseError, Url};
+use url::Url;
 
 pub mod activities;
 pub(crate) mod activity_lists;
@@ -193,68 +191,6 @@ pub(crate) fn check_apub_id_valid_with_strictness(
   Ok(())
 }
 
-pub enum EndpointType {
-  Community,
-  Person,
-  Post,
-  Comment,
-  PrivateMessage,
-}
-
-/// Generates an apub endpoint for a given domain, IE xyz.tld
-pub fn generate_local_apub_endpoint(
-  endpoint_type: EndpointType,
-  name: &str,
-  domain: &str,
-) -> Result<DbUrl, ParseError> {
-  let point = match endpoint_type {
-    EndpointType::Community => "c",
-    EndpointType::Person => "u",
-    EndpointType::Post => "post",
-    EndpointType::Comment => "comment",
-    EndpointType::PrivateMessage => "private_message",
-  };
-
-  Ok(Url::parse(&format!("{}/{}/{}", domain, point, name))?.into())
-}
-
-pub fn generate_followers_url(actor_id: &DbUrl) -> Result<DbUrl, ParseError> {
-  Ok(Url::parse(&format!("{}/followers", actor_id))?.into())
-}
-
-pub fn generate_inbox_url(actor_id: &DbUrl) -> Result<DbUrl, ParseError> {
-  Ok(Url::parse(&format!("{}/inbox", actor_id))?.into())
-}
-
-pub fn generate_site_inbox_url(actor_id: &DbUrl) -> Result<DbUrl, ParseError> {
-  let mut actor_id: Url = actor_id.clone().into();
-  actor_id.set_path("site_inbox");
-  Ok(actor_id.into())
-}
-
-pub fn generate_shared_inbox_url(actor_id: &DbUrl) -> Result<DbUrl, LemmyError> {
-  let actor_id: Url = actor_id.clone().into();
-  let url = format!(
-    "{}://{}{}/inbox",
-    &actor_id.scheme(),
-    &actor_id.host_str().context(location_info!())?,
-    if let Some(port) = actor_id.port() {
-      format!(":{}", port)
-    } else {
-      String::new()
-    },
-  );
-  Ok(Url::parse(&url)?.into())
-}
-
-pub fn generate_outbox_url(actor_id: &DbUrl) -> Result<DbUrl, ParseError> {
-  Ok(Url::parse(&format!("{}/outbox", actor_id))?.into())
-}
-
-fn generate_moderators_url(community_id: &DbUrl) -> Result<DbUrl, LemmyError> {
-  Ok(Url::parse(&format!("{}/moderators", community_id))?.into())
-}
-
 /// Store a sent or received activity in the database, for logging purposes. These records are not
 /// persistent.
 #[tracing::instrument(skip(pool))]
index a454813bb0c93f38aeae18e8c7649686f02fed5b..d8c1596ae2f664c68e0366d49e7bb44adadf8c88 100644 (file)
@@ -2,8 +2,6 @@ use crate::{
   check_apub_id_valid_with_strictness,
   collections::{community_moderators::ApubCommunityModerators, CommunityContext},
   fetch_local_site_data,
-  generate_moderators_url,
-  generate_outbox_url,
   local_instance,
   objects::instance::fetch_instance_actor_for_object,
   protocol::{
@@ -20,7 +18,7 @@ use activitypub_federation::{
 use activitystreams_kinds::actor::GroupType;
 use chrono::NaiveDateTime;
 use itertools::Itertools;
-use lemmy_api_common::LemmyContext;
+use lemmy_api_common::{generate_moderators_url, generate_outbox_url, LemmyContext};
 use lemmy_db_schema::{
   source::{
     actor_language::CommunityLanguage,
index ad7fc1c9cc4d801674e9b3658c7fc550e2a846a1..236f3edf345ed2c0c5340459c8a5c4dbb159c321 100644 (file)
@@ -1,7 +1,6 @@
 use crate::{
   check_apub_id_valid_with_strictness,
   fetch_local_site_data,
-  generate_outbox_url,
   objects::{instance::fetch_instance_actor_for_object, read_from_string_or_source_opt},
   protocol::{
     objects::{
@@ -19,7 +18,7 @@ use activitypub_federation::{
   utils::verify_domains_match,
 };
 use chrono::NaiveDateTime;
-use lemmy_api_common::{utils::local_site_opt_to_slur_regex, LemmyContext};
+use lemmy_api_common::{generate_outbox_url, utils::local_site_opt_to_slur_regex, LemmyContext};
 use lemmy_db_schema::{
   source::{
     instance::Instance,
index 0a095bb708de56d5e16428e48dac0c6d9730d5a4..23ee169d4a6b966b2118c9e6161b5071f626137c 100644 (file)
@@ -1,6 +1,5 @@
-use crate::generate_followers_url;
 use activitystreams_kinds::collection::CollectionType;
-use lemmy_api_common::LemmyContext;
+use lemmy_api_common::{generate_followers_url, LemmyContext};
 use lemmy_db_schema::source::community::Community;
 use lemmy_db_views_actor::structs::CommunityFollowerView;
 use lemmy_utils::error::LemmyError;
index bcbebb503fadc630363f0140e0e50fe665603357..0c9fecf6927b75949e0cc0e3c51e710c0148eca8 100644 (file)
@@ -8,13 +8,13 @@ use diesel::{
   TextExpressionMethods,
 };
 use diesel_async::RunQueryDsl;
-use lemmy_api_common::lemmy_db_views::structs::SiteView;
-use lemmy_apub::{
+use lemmy_api_common::{
   generate_followers_url,
   generate_inbox_url,
   generate_local_apub_endpoint,
   generate_shared_inbox_url,
   generate_site_inbox_url,
+  lemmy_db_views::structs::SiteView,
   EndpointType,
 };
 use lemmy_db_schema::{