From 028eabb4bdcf9eda65e0f315ca1c98f8765f9d7e Mon Sep 17 00:00:00 2001 From: Dull Bananas Date: Fri, 21 Jun 2024 03:17:41 +0000 Subject: [PATCH] revert some changed files --- crates/api_crud/src/comment/create.rs | 22 ++++- crates/api_crud/src/post/create.rs | 26 +++++- crates/api_crud/src/private_message/create.rs | 20 +++++ crates/api_crud/src/site/create.rs | 5 ++ crates/api_crud/src/user/create.rs | 12 +++ .../db_schema/replaceable_schema/triggers.sql | 86 +------------------ crates/db_schema/replaceable_schema/utils.sql | 12 --- crates/db_schema/src/utils.rs | 24 ++---- 8 files changed, 90 insertions(+), 117 deletions(-) diff --git a/crates/api_crud/src/comment/create.rs b/crates/api_crud/src/comment/create.rs index 36ab8eb63..636f83392 100644 --- a/crates/api_crud/src/comment/create.rs +++ b/crates/api_crud/src/comment/create.rs @@ -8,11 +8,13 @@ use lemmy_api_common::{ utils::{ check_community_user_action, check_post_deleted_or_removed, + generate_local_apub_endpoint, get_url_blocklist, is_mod_or_admin, local_site_to_slur_regex, process_markdown, update_read_comments, + EndpointType, }, }; use lemmy_db_schema::{ @@ -124,7 +126,25 @@ pub async fn create_comment( .await .with_lemmy_type(LemmyErrorType::CouldntCreateComment)?; + // Necessary to update the ap_id let inserted_comment_id = inserted_comment.id; + let protocol_and_hostname = context.settings().get_protocol_and_hostname(); + + let apub_id = generate_local_apub_endpoint( + EndpointType::Comment, + &inserted_comment_id.to_string(), + &protocol_and_hostname, + )?; + let updated_comment = Comment::update( + &mut context.pool(), + inserted_comment_id, + &CommentUpdateForm { + ap_id: Some(apub_id), + ..Default::default() + }, + ) + .await + .with_lemmy_type(LemmyErrorType::CouldntCreateComment)?; // Scan the comment for user mentions, add those rows let mentions = scrape_text_for_mentions(&content); @@ -150,7 +170,7 @@ pub async fn create_comment( .with_lemmy_type(LemmyErrorType::CouldntLikeComment)?; ActivityChannel::submit_activity( - SendActivityData::CreateComment(inserted_comment.clone()), + SendActivityData::CreateComment(updated_comment.clone()), &context, ) .await?; diff --git a/crates/api_crud/src/post/create.rs b/crates/api_crud/src/post/create.rs index fadfb8aa1..0b0fad5dc 100644 --- a/crates/api_crud/src/post/create.rs +++ b/crates/api_crud/src/post/create.rs @@ -8,11 +8,13 @@ use lemmy_api_common::{ send_activity::SendActivityData, utils::{ check_community_user_action, + generate_local_apub_endpoint, get_url_blocklist, honeypot_check, local_site_to_slur_regex, mark_post_as_read, process_markdown_opt, + EndpointType, }, }; use lemmy_db_schema::{ @@ -145,8 +147,26 @@ pub async fn create_post( .await .with_lemmy_type(LemmyErrorType::CouldntCreatePost)?; + let inserted_post_id = inserted_post.id; + let protocol_and_hostname = context.settings().get_protocol_and_hostname(); + let apub_id = generate_local_apub_endpoint( + EndpointType::Post, + &inserted_post_id.to_string(), + &protocol_and_hostname, + )?; + let updated_post = Post::update( + &mut context.pool(), + inserted_post_id, + &PostUpdateForm { + ap_id: Some(apub_id), + ..Default::default() + }, + ) + .await + .with_lemmy_type(LemmyErrorType::CouldntCreatePost)?; + generate_post_link_metadata( - inserted_post.clone(), + updated_post.clone(), custom_thumbnail.map(Into::into), |post| Some(SendActivityData::CreatePost(post)), Some(local_site), @@ -169,11 +189,11 @@ pub async fn create_post( mark_post_as_read(person_id, post_id, &mut context.pool()).await?; - if let Some(url) = inserted_post.url.clone() { + if let Some(url) = updated_post.url.clone() { if community.visibility == CommunityVisibility::Public { spawn_try_task(async move { let mut webmention = - Webmention::new::(inserted_post.ap_id.clone().into(), url.clone().into())?; + Webmention::new::(updated_post.ap_id.clone().into(), url.clone().into())?; webmention.set_checked(true); match webmention .send() diff --git a/crates/api_crud/src/private_message/create.rs b/crates/api_crud/src/private_message/create.rs index 03afe91da..0381d196c 100644 --- a/crates/api_crud/src/private_message/create.rs +++ b/crates/api_crud/src/private_message/create.rs @@ -6,11 +6,13 @@ use lemmy_api_common::{ send_activity::{ActivityChannel, SendActivityData}, utils::{ check_person_block, + generate_local_apub_endpoint, get_interface_language, get_url_blocklist, local_site_to_slur_regex, process_markdown, send_email_to_user, + EndpointType, }, }; use lemmy_db_schema::{ @@ -56,6 +58,24 @@ pub async fn create_private_message( .await .with_lemmy_type(LemmyErrorType::CouldntCreatePrivateMessage)?; + let inserted_private_message_id = inserted_private_message.id; + let protocol_and_hostname = context.settings().get_protocol_and_hostname(); + let apub_id = generate_local_apub_endpoint( + EndpointType::PrivateMessage, + &inserted_private_message_id.to_string(), + &protocol_and_hostname, + )?; + PrivateMessage::update( + &mut context.pool(), + inserted_private_message.id, + &PrivateMessageUpdateForm { + ap_id: Some(apub_id), + ..Default::default() + }, + ) + .await + .with_lemmy_type(LemmyErrorType::CouldntCreatePrivateMessage)?; + let view = PrivateMessageView::read(&mut context.pool(), inserted_private_message.id) .await? .ok_or(LemmyErrorType::CouldntFindPrivateMessage)?; diff --git a/crates/api_crud/src/site/create.rs b/crates/api_crud/src/site/create.rs index 457f4ed95..6b1909966 100644 --- a/crates/api_crud/src/site/create.rs +++ b/crates/api_crud/src/site/create.rs @@ -5,6 +5,7 @@ use lemmy_api_common::{ context::LemmyContext, site::{CreateSite, SiteResponse}, utils::{ + generate_shared_inbox_url, get_url_blocklist, is_admin, local_site_rate_limit_to_rate_limit_config, @@ -53,6 +54,8 @@ pub async fn create_site( validate_create_payload(&local_site, &data)?; + let actor_id: DbUrl = Url::parse(&context.settings().get_protocol_and_hostname())?.into(); + let inbox_url = Some(generate_shared_inbox_url(context.settings())?); let keypair = generate_actor_keypair()?; let slur_regex = local_site_to_slur_regex(&local_site); @@ -71,7 +74,9 @@ pub async fn create_site( description: diesel_string_update(data.description.as_deref()), icon: Some(icon), banner: Some(banner), + actor_id: Some(actor_id), last_refreshed_at: Some(naive_now()), + inbox_url, private_key: Some(Some(keypair.private_key)), public_key: Some(keypair.public_key), content_warning: diesel_string_update(data.content_warning.as_deref()), diff --git a/crates/api_crud/src/user/create.rs b/crates/api_crud/src/user/create.rs index 88beacf9b..c84bd0a50 100644 --- a/crates/api_crud/src/user/create.rs +++ b/crates/api_crud/src/user/create.rs @@ -5,11 +5,15 @@ use lemmy_api_common::{ context::LemmyContext, person::{LoginResponse, Register}, utils::{ + generate_inbox_url, + generate_local_apub_endpoint, + generate_shared_inbox_url, honeypot_check, local_site_to_slur_regex, password_length_check, send_new_applicant_email_to_admins, send_verification_email, + EndpointType, }, }; use lemmy_db_schema::{ @@ -93,6 +97,11 @@ pub async fn register( let actor_keypair = generate_actor_keypair()?; is_valid_actor_name(&data.username, local_site.actor_name_max_length as usize)?; + let actor_id = generate_local_apub_endpoint( + EndpointType::Person, + &data.username, + &context.settings().get_protocol_and_hostname(), + )?; if let Some(email) = &data.email { if LocalUser::is_email_taken(&mut context.pool(), email).await? { @@ -104,6 +113,9 @@ pub async fn register( // Register the new person let person_form = PersonInsertForm { + actor_id: Some(actor_id.clone()), + inbox_url: Some(generate_inbox_url(&actor_id)?), + shared_inbox_url: Some(generate_shared_inbox_url(context.settings())?), private_key: Some(actor_keypair.private_key), ..PersonInsertForm::new( data.username.clone(), diff --git a/crates/db_schema/replaceable_schema/triggers.sql b/crates/db_schema/replaceable_schema/triggers.sql index b99e30265..87866e89c 100644 --- a/crates/db_schema/replaceable_schema/triggers.sql +++ b/crates/db_schema/replaceable_schema/triggers.sql @@ -552,8 +552,7 @@ CREATE TRIGGER delete_follow FOR EACH ROW EXECUTE FUNCTION r.delete_follow_before_person (); --- Triggers that change values before insert or update (they can't be for insert only, because regenerating --- the values using update must be possible) +-- Triggers that change values before insert or update CREATE FUNCTION r.comment_change_values () RETURNS TRIGGER LANGUAGE plpgsql @@ -565,10 +564,6 @@ BEGIN IF NOT (NEW.path ~ ('*.' || id)::lquery) THEN NEW.path = NEW.path || id; END IF; - -- Set local apub URLs - IF NEW.local THEN - NEW.ap_id = coalesce(r.null_if_needs_replacement (NEW.ap_id), r.local_url('/comment/' || id)); - END IF; RETURN NEW; END $$; @@ -578,82 +573,3 @@ CREATE TRIGGER change_values FOR EACH ROW EXECUTE FUNCTION r.comment_change_values (); -CREATE FUNCTION r.community_change_values () - RETURNS TRIGGER - LANGUAGE plpgsql - AS $$ -BEGIN - -- Set local apub URLs - IF NEW.local THEN - NEW.actor_id = coalesce(r.null_if_needs_replacement (NEW.actor_id), r.local_url('/c/' || NEW.id::text)); - NEW.followers_url = coalesce(NEW.followers_url, NEW.actor_id || '/followers'); - NEW.inbox_url = coalesce(NEW.inbox_url, NEW.actor_id || '/inbox'); - NEW.moderators_url = coalesce(NEW.moderators_url, NEW.actor_id || '/moderators'); - NEW.featured_url = coalesce(NEW.featured_url, NEW.actor_id || '/featured'); - NEW.shared_inbox_url = coalesce(NEW.shared_inbox_url, r.local_url('/inbox'); - END IF; - RETURN NEW; -END -$$; - -CREATE TRIGGER change_values - BEFORE INSERT OR UPDATE ON community - FOR EACH ROW - EXECUTE FUNCTION r.community_change_values (); - -CREATE FUNCTION r.person_change_values () - RETURNS TRIGGER - LANGUAGE plpgsql - AS $$ -BEGIN - -- Set local apub URLs - IF NEW.local THEN - NEW.actor_id = coalesce(r.null_if_needs_replacement (NEW.actor_id), r.local_url('/u/' || NEW.id::text)); - NEW.inbox_url = coalesce(NEW.inbox_url, NEW.actor_id || '/inbox'); - NEW.shared_inbox_url = coalesce(NEW.shared_inbox_url, r.local_url('/inbox')); - END IF; - RETURN NEW; -END -$$; - -CREATE TRIGGER change_values - BEFORE INSERT OR UPDATE ON person - FOR EACH ROW - EXECUTE FUNCTION r.person_change_values (); - -CREATE FUNCTION r.post_change_values () - RETURNS TRIGGER - LANGUAGE plpgsql - AS $$ -BEGIN - -- Set local apub URLs - IF NEW.local THEN - NEW.ap_id = coalesce(r.null_if_needs_replacement (NEW.ap_id), r.local_url('/post/' || NEW.id::text)); - END IF; - RETURN NEW; -END -$$; - -CREATE TRIGGER change_values - BEFORE INSERT OR UPDATE ON post - FOR EACH ROW - EXECUTE FUNCTION r.post_change_values (); - -CREATE FUNCTION r.private_message_change_values () - RETURNS TRIGGER - LANGUAGE plpgsql - AS $$ -BEGIN - -- Set local apub URLs - IF NEW.local THEN - NEW.ap_id = coalesce(r.null_if_needs_replacement (NEW.ap_id), r.local_url('/private_message/' || NEW.id::text)); - END IF; - RETURN NEW; -END -$$; - -CREATE TRIGGER change_values - BEFORE INSERT OR UPDATE ON private_message - FOR EACH ROW - EXECUTE FUNCTION r.private_message_change_values (); - diff --git a/crates/db_schema/replaceable_schema/utils.sql b/crates/db_schema/replaceable_schema/utils.sql index 28dabcaf9..f236c5387 100644 --- a/crates/db_schema/replaceable_schema/utils.sql +++ b/crates/db_schema/replaceable_schema/utils.sql @@ -57,18 +57,6 @@ BEGIN END; $$; -CREATE FUNCTION r.local_url (url_path text) - RETURNS text - LANGUAGE sql - STABLE PARALLEL SAFE - RETURN ( - current_setting ('lemmy.protocol_and_hostname') || url_path); - -CREATE FUNCTION r.null_if_needs_replacement (t text) - RETURNS text - LANGUAGE sql - IMMUTABLE PARALLEL SAFE RETURN CASE WHEN t LIKE 'http://changeme%' THEN NULL ELSE t END CASE; - -- This function creates statement-level triggers for all operation types. It's designed this way -- because of these limitations: -- * A trigger that uses transition tables can only handle 1 operation type. diff --git a/crates/db_schema/src/utils.rs b/crates/db_schema/src/utils.rs index 48084799f..6761adeee 100644 --- a/crates/db_schema/src/utils.rs +++ b/crates/db_schema/src/utils.rs @@ -30,7 +30,7 @@ use diesel_async::{ AsyncDieselConnectionManager, ManagerConfig, }, - RunQueryDsl, + SimpleAsyncConnection, }; use futures_util::{future::BoxFuture, Future, FutureExt}; use i_love_jesus::CursorKey; @@ -352,18 +352,12 @@ fn establish_connection(config: &str) -> BoxFuture = Lazy::new(|| { }); pub mod functions { - use diesel::sql_types::{BigInt, Bool, Text, Timestamptz}; + use diesel::sql_types::{BigInt, Text, Timestamptz}; sql_function! { #[sql_name = "r.hot_rank"] @@ -514,8 +508,6 @@ pub mod functions { // really this function is variadic, this just adds the two-argument version sql_function!(fn coalesce(x: diesel::sql_types::Nullable, y: T) -> T); - - sql_function!(fn set_config(setting_name: Text, new_value: Text, is_local: Bool) -> Text); } pub const DELETED_REPLACEMENT_TEXT: &str = "*Permanently Deleted*";