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 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())
}

View 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::{

View 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,

View 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,

View 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::{
use lemmy_apub::protocol::activities::{
create_or_update::chat_message::CreateOrUpdateChatMessage,
CreateOrUpdateType,
},
EndpointType,
};
use lemmy_db_schema::{
source::{

View 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::{

View 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,

View 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},

View 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},

View 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},

View 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,

View 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,

View 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;

View 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;

View 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))]

View 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,

View 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,

View 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;

View 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::{