Move code to generate apub urls into lemmy_api_common

This commit is contained in:
Felix Ableitner 2022-11-26 21:47:13 +01:00
parent 904d7bec2f
commit 201fa97769
19 changed files with 98 additions and 111 deletions

View file

@ -20,12 +20,16 @@ pub extern crate lemmy_db_views_moderator;
use crate::websocket::chat_server::ChatServer; use crate::websocket::chat_server::ChatServer;
use actix::Addr; 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::{ use lemmy_utils::{
error::LemmyError,
location_info,
rate_limit::RateLimitCell, rate_limit::RateLimitCell,
settings::{structs::Settings, SETTINGS}, settings::{structs::Settings, SETTINGS},
}; };
use reqwest_middleware::ClientWithMiddleware; use reqwest_middleware::ClientWithMiddleware;
use url::{ParseError, Url};
pub struct LemmyContext { pub struct LemmyContext {
pool: DbPool, 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())
}

View file

@ -2,6 +2,7 @@ use crate::PerformCrud;
use actix_web::web::Data; use actix_web::web::Data;
use lemmy_api_common::{ use lemmy_api_common::{
comment::{CommentResponse, CreateComment}, comment::{CommentResponse, CreateComment},
generate_local_apub_endpoint,
utils::{ utils::{
check_community_ban, check_community_ban,
check_community_deleted_or_removed, check_community_deleted_or_removed,
@ -14,13 +15,12 @@ use lemmy_api_common::{
send::{send_comment_ws_message, send_local_notifs}, send::{send_comment_ws_message, send_local_notifs},
UserOperationCrud, UserOperationCrud,
}, },
EndpointType,
LemmyContext, LemmyContext,
}; };
use lemmy_apub::{ use lemmy_apub::{
generate_local_apub_endpoint,
objects::comment::ApubComment, objects::comment::ApubComment,
protocol::activities::{create_or_update::note::CreateOrUpdateNote, CreateOrUpdateType}, protocol::activities::{create_or_update::note::CreateOrUpdateNote, CreateOrUpdateType},
EndpointType,
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
source::{ source::{

View file

@ -3,17 +3,15 @@ use activitypub_federation::core::{object_id::ObjectId, signatures::generate_act
use actix_web::web::Data; use actix_web::web::Data;
use lemmy_api_common::{ use lemmy_api_common::{
community::{CommunityResponse, CreateCommunity}, 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_followers_url,
generate_inbox_url, generate_inbox_url,
generate_local_apub_endpoint, generate_local_apub_endpoint,
generate_shared_inbox_url, generate_shared_inbox_url,
objects::community::ApubCommunity, utils::{get_local_user_view_from_jwt, is_admin, local_site_to_slur_regex},
EndpointType, EndpointType,
LemmyContext,
}; };
use lemmy_apub::objects::community::ApubCommunity;
use lemmy_db_schema::{ use lemmy_db_schema::{
source::community::{ source::community::{
Community, Community,

View file

@ -1,6 +1,7 @@
use crate::PerformCrud; use crate::PerformCrud;
use actix_web::web::Data; use actix_web::web::Data;
use lemmy_api_common::{ use lemmy_api_common::{
generate_local_apub_endpoint,
post::{CreatePost, PostResponse}, post::{CreatePost, PostResponse},
request::fetch_site_data, request::fetch_site_data,
utils::{ utils::{
@ -12,13 +13,12 @@ use lemmy_api_common::{
mark_post_as_read, mark_post_as_read,
}, },
websocket::{send::send_post_ws_message, UserOperationCrud}, websocket::{send::send_post_ws_message, UserOperationCrud},
EndpointType,
LemmyContext, LemmyContext,
}; };
use lemmy_apub::{ use lemmy_apub::{
generate_local_apub_endpoint,
objects::post::ApubPost, objects::post::ApubPost,
protocol::activities::{create_or_update::page::CreateOrUpdatePage, CreateOrUpdateType}, protocol::activities::{create_or_update::page::CreateOrUpdatePage, CreateOrUpdateType},
EndpointType,
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
impls::actor_language::default_post_language, impls::actor_language::default_post_language,

View file

@ -1,6 +1,7 @@
use crate::PerformCrud; use crate::PerformCrud;
use actix_web::web::Data; use actix_web::web::Data;
use lemmy_api_common::{ use lemmy_api_common::{
generate_local_apub_endpoint,
private_message::{CreatePrivateMessage, PrivateMessageResponse}, private_message::{CreatePrivateMessage, PrivateMessageResponse},
utils::{ utils::{
check_person_block, check_person_block,
@ -10,15 +11,12 @@ use lemmy_api_common::{
send_email_to_user, send_email_to_user,
}, },
websocket::{send::send_pm_ws_message, UserOperationCrud}, websocket::{send::send_pm_ws_message, UserOperationCrud},
EndpointType,
LemmyContext, LemmyContext,
}; };
use lemmy_apub::{ use lemmy_apub::protocol::activities::{
generate_local_apub_endpoint,
protocol::activities::{
create_or_update::chat_message::CreateOrUpdateChatMessage, create_or_update::chat_message::CreateOrUpdateChatMessage,
CreateOrUpdateType, CreateOrUpdateType,
},
EndpointType,
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
source::{ source::{

View file

@ -2,6 +2,7 @@ use crate::PerformCrud;
use activitypub_federation::core::signatures::generate_actor_keypair; use activitypub_federation::core::signatures::generate_actor_keypair;
use actix_web::web::Data; use actix_web::web::Data;
use lemmy_api_common::{ use lemmy_api_common::{
generate_site_inbox_url,
site::{CreateSite, SiteResponse}, site::{CreateSite, SiteResponse},
utils::{ utils::{
get_local_user_view_from_jwt, get_local_user_view_from_jwt,
@ -12,7 +13,6 @@ use lemmy_api_common::{
}, },
LemmyContext, LemmyContext,
}; };
use lemmy_apub::generate_site_inbox_url;
use lemmy_db_schema::{ use lemmy_db_schema::{
newtypes::DbUrl, newtypes::DbUrl,
source::{ source::{

View file

@ -2,6 +2,9 @@ use crate::PerformCrud;
use activitypub_federation::core::signatures::generate_actor_keypair; use activitypub_federation::core::signatures::generate_actor_keypair;
use actix_web::web::Data; use actix_web::web::Data;
use lemmy_api_common::{ use lemmy_api_common::{
generate_inbox_url,
generate_local_apub_endpoint,
generate_shared_inbox_url,
person::{LoginResponse, Register}, person::{LoginResponse, Register},
utils::{ utils::{
honeypot_check, honeypot_check,
@ -11,13 +14,8 @@ use lemmy_api_common::{
send_verification_email, send_verification_email,
}, },
websocket::messages::CheckCaptcha, websocket::messages::CheckCaptcha,
LemmyContext,
};
use lemmy_apub::{
generate_inbox_url,
generate_local_apub_endpoint,
generate_shared_inbox_url,
EndpointType, EndpointType,
LemmyContext,
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
aggregates::structs::PersonAggregates, aggregates::structs::PersonAggregates,

View file

@ -8,7 +8,6 @@ use crate::{
verify_person_in_community, verify_person_in_community,
}, },
activity_lists::AnnouncableActivities, activity_lists::AnnouncableActivities,
generate_moderators_url,
local_instance, local_instance,
objects::{community::ApubCommunity, person::ApubPerson}, objects::{community::ApubCommunity, person::ApubPerson},
protocol::{activities::community::add_mod::AddMod, InCommunity}, protocol::{activities::community::add_mod::AddMod, InCommunity},
@ -20,7 +19,7 @@ use activitypub_federation::{
traits::{ActivityHandler, Actor}, traits::{ActivityHandler, Actor},
}; };
use activitystreams_kinds::{activity::AddType, public}; use activitystreams_kinds::{activity::AddType, public};
use lemmy_api_common::LemmyContext; use lemmy_api_common::{generate_moderators_url, LemmyContext};
use lemmy_db_schema::{ use lemmy_db_schema::{
source::{ source::{
community::{CommunityModerator, CommunityModeratorForm}, community::{CommunityModerator, CommunityModeratorForm},

View file

@ -8,7 +8,6 @@ use crate::{
verify_person_in_community, verify_person_in_community,
}, },
activity_lists::AnnouncableActivities, activity_lists::AnnouncableActivities,
generate_moderators_url,
local_instance, local_instance,
objects::{community::ApubCommunity, person::ApubPerson}, objects::{community::ApubCommunity, person::ApubPerson},
protocol::{activities::community::remove_mod::RemoveMod, InCommunity}, protocol::{activities::community::remove_mod::RemoveMod, InCommunity},
@ -20,7 +19,7 @@ use activitypub_federation::{
traits::{ActivityHandler, Actor}, traits::{ActivityHandler, Actor},
}; };
use activitystreams_kinds::{activity::RemoveType, public}; use activitystreams_kinds::{activity::RemoveType, public};
use lemmy_api_common::LemmyContext; use lemmy_api_common::{generate_moderators_url, LemmyContext};
use lemmy_db_schema::{ use lemmy_db_schema::{
source::{ source::{
community::{CommunityModerator, CommunityModeratorForm}, community::{CommunityModerator, CommunityModeratorForm},

View file

@ -1,5 +1,4 @@
use crate::{ use crate::{
generate_moderators_url,
insert_activity, insert_activity,
local_instance, local_instance,
objects::{community::ApubCommunity, person::ApubPerson}, objects::{community::ApubCommunity, person::ApubPerson},
@ -13,7 +12,7 @@ use activitypub_federation::{
}; };
use activitystreams_kinds::public; use activitystreams_kinds::public;
use anyhow::anyhow; use anyhow::anyhow;
use lemmy_api_common::LemmyContext; use lemmy_api_common::{generate_moderators_url, LemmyContext};
use lemmy_db_schema::{ use lemmy_db_schema::{
newtypes::CommunityId, newtypes::CommunityId,
source::{community::Community, local_site::LocalSite}, source::{community::Community, local_site::LocalSite},

View file

@ -1,6 +1,5 @@
use crate::{ use crate::{
collections::CommunityContext, collections::CommunityContext,
generate_moderators_url,
local_instance, local_instance,
objects::person::ApubPerson, objects::person::ApubPerson,
protocol::collections::group_moderators::GroupModerators, protocol::collections::group_moderators::GroupModerators,
@ -12,6 +11,7 @@ use activitypub_federation::{
}; };
use activitystreams_kinds::collection::OrderedCollectionType; use activitystreams_kinds::collection::OrderedCollectionType;
use chrono::NaiveDateTime; use chrono::NaiveDateTime;
use lemmy_api_common::generate_moderators_url;
use lemmy_db_schema::{ use lemmy_db_schema::{
source::community::{CommunityModerator, CommunityModeratorForm}, source::community::{CommunityModerator, CommunityModeratorForm},
traits::Joinable, traits::Joinable,

View file

@ -1,7 +1,6 @@
use crate::{ use crate::{
activity_lists::AnnouncableActivities, activity_lists::AnnouncableActivities,
collections::CommunityContext, collections::CommunityContext,
generate_outbox_url,
objects::post::ApubPost, objects::post::ApubPost,
protocol::{ protocol::{
activities::{ activities::{
@ -20,6 +19,7 @@ use activitypub_federation::{
use activitystreams_kinds::collection::OrderedCollectionType; use activitystreams_kinds::collection::OrderedCollectionType;
use chrono::NaiveDateTime; use chrono::NaiveDateTime;
use futures::future::join_all; use futures::future::join_all;
use lemmy_api_common::generate_outbox_url;
use lemmy_db_schema::{ use lemmy_db_schema::{
source::{person::Person, post::Post}, source::{person::Person, post::Post},
traits::Crud, traits::Crud,

View file

@ -5,7 +5,6 @@ use crate::{
community_outbox::ApubCommunityOutbox, community_outbox::ApubCommunityOutbox,
CommunityContext, CommunityContext,
}, },
generate_outbox_url,
http::{create_apub_response, create_apub_tombstone_response, receive_lemmy_activity}, http::{create_apub_response, create_apub_tombstone_response, receive_lemmy_activity},
local_instance, local_instance,
objects::{community::ApubCommunity, person::ApubPerson}, objects::{community::ApubCommunity, person::ApubPerson},
@ -17,7 +16,7 @@ use activitypub_federation::{
traits::ApubObject, traits::ApubObject,
}; };
use actix_web::{web, HttpRequest, HttpResponse}; 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_db_schema::{source::community::Community, traits::ApubActor};
use lemmy_utils::error::LemmyError; use lemmy_utils::error::LemmyError;
use serde::Deserialize; use serde::Deserialize;

View file

@ -1,14 +1,13 @@
use crate::{ use crate::{
activity_lists::PersonInboxActivitiesWithAnnouncable, activity_lists::PersonInboxActivitiesWithAnnouncable,
fetcher::user_or_community::UserOrCommunity, fetcher::user_or_community::UserOrCommunity,
generate_outbox_url,
http::{create_apub_response, create_apub_tombstone_response, receive_lemmy_activity}, http::{create_apub_response, create_apub_tombstone_response, receive_lemmy_activity},
objects::person::ApubPerson, objects::person::ApubPerson,
protocol::collections::empty_outbox::EmptyOutbox, protocol::collections::empty_outbox::EmptyOutbox,
}; };
use activitypub_federation::{deser::context::WithContext, traits::ApubObject}; use activitypub_federation::{deser::context::WithContext, traits::ApubObject};
use actix_web::{web, HttpRequest, HttpResponse}; 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_db_schema::{source::person::Person, traits::ApubActor};
use lemmy_utils::error::LemmyError; use lemmy_utils::error::LemmyError;
use serde::Deserialize; use serde::Deserialize;

View file

@ -6,18 +6,16 @@ use activitypub_federation::{
LocalInstance, LocalInstance,
UrlVerifier, UrlVerifier,
}; };
use anyhow::Context;
use async_trait::async_trait; use async_trait::async_trait;
use lemmy_api_common::LemmyContext; use lemmy_api_common::LemmyContext;
use lemmy_db_schema::{ use lemmy_db_schema::{
newtypes::DbUrl,
source::{activity::Activity, instance::Instance, local_site::LocalSite}, source::{activity::Activity, instance::Instance, local_site::LocalSite},
utils::DbPool, 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 once_cell::sync::Lazy;
use tokio::sync::OnceCell; use tokio::sync::OnceCell;
use url::{ParseError, Url}; use url::Url;
pub mod activities; pub mod activities;
pub(crate) mod activity_lists; pub(crate) mod activity_lists;
@ -193,68 +191,6 @@ pub(crate) fn check_apub_id_valid_with_strictness(
Ok(()) 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 /// Store a sent or received activity in the database, for logging purposes. These records are not
/// persistent. /// persistent.
#[tracing::instrument(skip(pool))] #[tracing::instrument(skip(pool))]

View file

@ -2,8 +2,6 @@ use crate::{
check_apub_id_valid_with_strictness, check_apub_id_valid_with_strictness,
collections::{community_moderators::ApubCommunityModerators, CommunityContext}, collections::{community_moderators::ApubCommunityModerators, CommunityContext},
fetch_local_site_data, fetch_local_site_data,
generate_moderators_url,
generate_outbox_url,
local_instance, local_instance,
objects::instance::fetch_instance_actor_for_object, objects::instance::fetch_instance_actor_for_object,
protocol::{ protocol::{
@ -20,7 +18,7 @@ use activitypub_federation::{
use activitystreams_kinds::actor::GroupType; use activitystreams_kinds::actor::GroupType;
use chrono::NaiveDateTime; use chrono::NaiveDateTime;
use itertools::Itertools; use itertools::Itertools;
use lemmy_api_common::LemmyContext; use lemmy_api_common::{generate_moderators_url, generate_outbox_url, LemmyContext};
use lemmy_db_schema::{ use lemmy_db_schema::{
source::{ source::{
actor_language::CommunityLanguage, actor_language::CommunityLanguage,

View file

@ -1,7 +1,6 @@
use crate::{ use crate::{
check_apub_id_valid_with_strictness, check_apub_id_valid_with_strictness,
fetch_local_site_data, fetch_local_site_data,
generate_outbox_url,
objects::{instance::fetch_instance_actor_for_object, read_from_string_or_source_opt}, objects::{instance::fetch_instance_actor_for_object, read_from_string_or_source_opt},
protocol::{ protocol::{
objects::{ objects::{
@ -19,7 +18,7 @@ use activitypub_federation::{
utils::verify_domains_match, utils::verify_domains_match,
}; };
use chrono::NaiveDateTime; 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::{ use lemmy_db_schema::{
source::{ source::{
instance::Instance, instance::Instance,

View file

@ -1,6 +1,5 @@
use crate::generate_followers_url;
use activitystreams_kinds::collection::CollectionType; 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_schema::source::community::Community;
use lemmy_db_views_actor::structs::CommunityFollowerView; use lemmy_db_views_actor::structs::CommunityFollowerView;
use lemmy_utils::error::LemmyError; use lemmy_utils::error::LemmyError;

View file

@ -8,13 +8,13 @@ use diesel::{
TextExpressionMethods, TextExpressionMethods,
}; };
use diesel_async::RunQueryDsl; use diesel_async::RunQueryDsl;
use lemmy_api_common::lemmy_db_views::structs::SiteView; use lemmy_api_common::{
use lemmy_apub::{
generate_followers_url, generate_followers_url,
generate_inbox_url, generate_inbox_url,
generate_local_apub_endpoint, generate_local_apub_endpoint,
generate_shared_inbox_url, generate_shared_inbox_url,
generate_site_inbox_url, generate_site_inbox_url,
lemmy_db_views::structs::SiteView,
EndpointType, EndpointType,
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{