From: Felix Ableitner Date: Sat, 26 Nov 2022 20:47:13 +0000 (+0100) Subject: Move code to generate apub urls into lemmy_api_common X-Git-Url: http://these/git/ui/assets/static/%7Bthis.state.url%7D?a=commitdiff_plain;h=201fa97769882736b560f1c6e3564c70e5ce3f1f;p=lemmy.git Move code to generate apub urls into lemmy_api_common --- diff --git a/crates/api_common/src/lib.rs b/crates/api_common/src/lib.rs index bcd603e5..990780e1 100644 --- a/crates/api_common/src/lib.rs +++ b/crates/api_common/src/lib.rs @@ -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 { + 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 { + Ok(Url::parse(&format!("{}/followers", actor_id))?.into()) +} + +pub fn generate_inbox_url(actor_id: &DbUrl) -> Result { + Ok(Url::parse(&format!("{}/inbox", actor_id))?.into()) +} + +pub fn generate_site_inbox_url(actor_id: &DbUrl) -> Result { + 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 { + 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 { + Ok(Url::parse(&format!("{}/outbox", actor_id))?.into()) +} + +pub fn generate_moderators_url(community_id: &DbUrl) -> Result { + Ok(Url::parse(&format!("{}/moderators", community_id))?.into()) +} diff --git a/crates/api_crud/src/comment/create.rs b/crates/api_crud/src/comment/create.rs index a3be10a6..5d9603ac 100644 --- a/crates/api_crud/src/comment/create.rs +++ b/crates/api_crud/src/comment/create.rs @@ -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::{ diff --git a/crates/api_crud/src/community/create.rs b/crates/api_crud/src/community/create.rs index caa1a363..11a30766 100644 --- a/crates/api_crud/src/community/create.rs +++ b/crates/api_crud/src/community/create.rs @@ -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, diff --git a/crates/api_crud/src/post/create.rs b/crates/api_crud/src/post/create.rs index 2353def6..46d4a7f6 100644 --- a/crates/api_crud/src/post/create.rs +++ b/crates/api_crud/src/post/create.rs @@ -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, diff --git a/crates/api_crud/src/private_message/create.rs b/crates/api_crud/src/private_message/create.rs index 6f099f79..03d994c0 100644 --- a/crates/api_crud/src/private_message/create.rs +++ b/crates/api_crud/src/private_message/create.rs @@ -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::{ diff --git a/crates/api_crud/src/site/create.rs b/crates/api_crud/src/site/create.rs index 22631d51..34ac404a 100644 --- a/crates/api_crud/src/site/create.rs +++ b/crates/api_crud/src/site/create.rs @@ -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::{ diff --git a/crates/api_crud/src/user/create.rs b/crates/api_crud/src/user/create.rs index 5686348f..0521ffe0 100644 --- a/crates/api_crud/src/user/create.rs +++ b/crates/api_crud/src/user/create.rs @@ -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, diff --git a/crates/apub/src/activities/community/add_mod.rs b/crates/apub/src/activities/community/add_mod.rs index 2bdf2be5..3718ea68 100644 --- a/crates/apub/src/activities/community/add_mod.rs +++ b/crates/apub/src/activities/community/add_mod.rs @@ -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}, diff --git a/crates/apub/src/activities/community/remove_mod.rs b/crates/apub/src/activities/community/remove_mod.rs index ca8d5b04..855fe7b4 100644 --- a/crates/apub/src/activities/community/remove_mod.rs +++ b/crates/apub/src/activities/community/remove_mod.rs @@ -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}, diff --git a/crates/apub/src/activities/mod.rs b/crates/apub/src/activities/mod.rs index 1528a273..68a3b535 100644 --- a/crates/apub/src/activities/mod.rs +++ b/crates/apub/src/activities/mod.rs @@ -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}, diff --git a/crates/apub/src/collections/community_moderators.rs b/crates/apub/src/collections/community_moderators.rs index b7575370..aa38f8be 100644 --- a/crates/apub/src/collections/community_moderators.rs +++ b/crates/apub/src/collections/community_moderators.rs @@ -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, diff --git a/crates/apub/src/collections/community_outbox.rs b/crates/apub/src/collections/community_outbox.rs index c7742de7..44685430 100644 --- a/crates/apub/src/collections/community_outbox.rs +++ b/crates/apub/src/collections/community_outbox.rs @@ -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, diff --git a/crates/apub/src/http/community.rs b/crates/apub/src/http/community.rs index 5b1a5b92..487ad551 100644 --- a/crates/apub/src/http/community.rs +++ b/crates/apub/src/http/community.rs @@ -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; diff --git a/crates/apub/src/http/person.rs b/crates/apub/src/http/person.rs index 19c57b20..0e838fe1 100644 --- a/crates/apub/src/http/person.rs +++ b/crates/apub/src/http/person.rs @@ -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; diff --git a/crates/apub/src/lib.rs b/crates/apub/src/lib.rs index 48fd58f1..92ae014b 100644 --- a/crates/apub/src/lib.rs +++ b/crates/apub/src/lib.rs @@ -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 { - 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 { - Ok(Url::parse(&format!("{}/followers", actor_id))?.into()) -} - -pub fn generate_inbox_url(actor_id: &DbUrl) -> Result { - Ok(Url::parse(&format!("{}/inbox", actor_id))?.into()) -} - -pub fn generate_site_inbox_url(actor_id: &DbUrl) -> Result { - 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 { - 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 { - Ok(Url::parse(&format!("{}/outbox", actor_id))?.into()) -} - -fn generate_moderators_url(community_id: &DbUrl) -> Result { - 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))] diff --git a/crates/apub/src/objects/community.rs b/crates/apub/src/objects/community.rs index a454813b..d8c1596a 100644 --- a/crates/apub/src/objects/community.rs +++ b/crates/apub/src/objects/community.rs @@ -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, diff --git a/crates/apub/src/objects/person.rs b/crates/apub/src/objects/person.rs index ad7fc1c9..236f3edf 100644 --- a/crates/apub/src/objects/person.rs +++ b/crates/apub/src/objects/person.rs @@ -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, diff --git a/crates/apub/src/protocol/collections/group_followers.rs b/crates/apub/src/protocol/collections/group_followers.rs index 0a095bb7..23ee169d 100644 --- a/crates/apub/src/protocol/collections/group_followers.rs +++ b/crates/apub/src/protocol/collections/group_followers.rs @@ -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; diff --git a/src/code_migrations.rs b/src/code_migrations.rs index bcbebb50..0c9fecf6 100644 --- a/src/code_migrations.rs +++ b/src/code_migrations.rs @@ -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::{