Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use trigger to generate apub URL in insert instead of update, and fix query planner options not being set when TLS is disabled #4797

Merged
merged 29 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
1f30b38
Update create.rs
dullbananas Jun 10, 2024
87e8682
Update utils.rs
dullbananas Jun 10, 2024
dbeffbc
Update utils.sql
dullbananas Jun 10, 2024
a2dfb93
Update triggers.sql
dullbananas Jun 10, 2024
8acf2f9
Update utils.sql
dullbananas Jun 11, 2024
f345277
Update create.rs
dullbananas Jun 11, 2024
00eed9d
Update create.rs
dullbananas Jun 11, 2024
f456122
Update create.rs
dullbananas Jun 11, 2024
5789926
Update create.rs
dullbananas Jun 11, 2024
a958658
Update create.rs
dullbananas Jun 11, 2024
b816b4b
Update create.rs
dullbananas Jun 11, 2024
4b7a965
Update create.rs
dullbananas Jun 11, 2024
b360342
Update create.rs
dullbananas Jun 11, 2024
b4bc5c6
Create up.sql
dullbananas Jun 11, 2024
28fafdd
Update up.sql
dullbananas Jun 13, 2024
5be7daf
Update triggers.sql
dullbananas Jun 13, 2024
2ee880c
Update utils.rs
dullbananas Jun 14, 2024
279d4b0
Merge branch 'main' into ap-id-triggers
dullbananas Jun 14, 2024
35b0613
stuff
dullbananas Jun 14, 2024
83c5181
stuff
dullbananas Jun 16, 2024
a2ee8de
Merge remote-tracking branch 'upstream/main' into ap-id-triggers
dullbananas Jun 20, 2024
028eabb
revert some changed files
dullbananas Jun 21, 2024
3f54bf3
Revert "revert some changed files"
dullbananas Jun 21, 2024
e9b0c1f
revert the correct files
dullbananas Jun 21, 2024
62ba725
partial reverts
dullbananas Jun 21, 2024
923b24b
Merge remote-tracking branch 'upstream/main' into ap-id-triggers
dullbananas Jun 24, 2024
6bc8e76
migration, tests, fix establish_connection
dullbananas Jun 25, 2024
28e04b4
lint
dullbananas Jun 25, 2024
7f435cf
pg_format
dullbananas Jun 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 2 additions & 22 deletions crates/api_crud/src/comment/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,18 @@ 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::{
impls::actor_language::default_post_language,
source::{
actor_language::CommunityLanguage,
comment::{Comment, CommentInsertForm, CommentLike, CommentLikeForm, CommentUpdateForm},
comment::{Comment, CommentInsertForm, CommentLike, CommentLikeForm},
comment_reply::{CommentReply, CommentReplyUpdateForm},
local_site::LocalSite,
person_mention::{PersonMention, PersonMentionUpdateForm},
Expand Down Expand Up @@ -126,25 +124,7 @@ 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);
Expand All @@ -170,7 +150,7 @@ pub async fn create_comment(
.with_lemmy_type(LemmyErrorType::CouldntLikeComment)?;

ActivityChannel::submit_activity(
SendActivityData::CreateComment(updated_comment.clone()),
SendActivityData::CreateComment(inserted_comment.clone()),
&context,
)
.await?;
Expand Down
28 changes: 4 additions & 24 deletions crates/api_crud/src/post/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@ 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::{
Expand All @@ -23,7 +21,7 @@ use lemmy_db_schema::{
actor_language::CommunityLanguage,
community::Community,
local_site::LocalSite,
post::{Post, PostInsertForm, PostLike, PostLikeForm, PostUpdateForm},
post::{Post, PostInsertForm, PostLike, PostLikeForm},
},
traits::{Crud, Likeable},
utils::diesel_url_create,
Expand Down Expand Up @@ -147,26 +145,8 @@ 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(
updated_post.clone(),
inserted_post.clone(),
custom_thumbnail.map(Into::into),
|post| Some(SendActivityData::CreatePost(post)),
Some(local_site),
Expand All @@ -189,11 +169,11 @@ pub async fn create_post(

mark_post_as_read(person_id, post_id, &mut context.pool()).await?;

if let Some(url) = updated_post.url.clone() {
if let Some(url) = inserted_post.url.clone() {
if community.visibility == CommunityVisibility::Public {
spawn_try_task(async move {
let mut webmention =
Webmention::new::<Url>(updated_post.ap_id.clone().into(), url.clone().into())?;
Webmention::new::<Url>(inserted_post.ap_id.clone().into(), url.clone().into())?;
webmention.set_checked(true);
match webmention
.send()
Expand Down
22 changes: 1 addition & 21 deletions crates/api_crud/src/private_message/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,17 @@ 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::{
source::{
local_site::LocalSite,
private_message::{PrivateMessage, PrivateMessageInsertForm, PrivateMessageUpdateForm},
private_message::{PrivateMessage, PrivateMessageInsertForm},
},
traits::Crud,
};
Expand Down Expand Up @@ -58,24 +56,6 @@ 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)?;
Expand Down
40 changes: 40 additions & 0 deletions crates/db_schema/replaceable_schema/triggers.sql
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,10 @@ BEGIN
IF NOT (NEW.path ~ ('*.' || id)::lquery) THEN
NEW.path = NEW.path || id;
END IF;
-- Set local ap_id
IF NEW.local THEN
NEW.ap_id = coalesce(NEW.ap_id, r.local_url ('/comment/' || id));
END IF;
RETURN NEW;
END
$$;
Expand All @@ -573,3 +577,39 @@ CREATE TRIGGER change_values
FOR EACH ROW
EXECUTE FUNCTION r.comment_change_values ();

CREATE FUNCTION r.post_change_values ()
RETURNS TRIGGER
LANGUAGE plpgsql
AS $$
BEGIN
-- Set local ap_id
IF NEW.local THEN
NEW.ap_id = coalesce(NEW.ap_id, r.local_url ('/post/' || NEW.id::text));
END IF;
RETURN NEW;
END
$$;

CREATE TRIGGER change_values
BEFORE INSERT 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 ap_id
IF NEW.local THEN
NEW.ap_id = coalesce(NEW.ap_id, r.local_url ('/private_message/' || NEW.id::text));
END IF;
RETURN NEW;
END
$$;

CREATE TRIGGER change_values
BEFORE INSERT ON private_message
FOR EACH ROW
EXECUTE FUNCTION r.private_message_change_values ();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is the trigger for comment?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For comments, an existing trigger was updated. It's the first change in this file.


7 changes: 7 additions & 0 deletions crates/db_schema/replaceable_schema/utils.sql
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ 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
);

-- 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.
Expand Down
8 changes: 7 additions & 1 deletion crates/db_schema/src/impls/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ mod tests {
use diesel_ltree::Ltree;
use pretty_assertions::assert_eq;
use serial_test::serial;
use url::Url;

#[tokio::test]
#[serial]
Expand Down Expand Up @@ -273,7 +274,12 @@ mod tests {
path: Ltree(format!("0.{}", inserted_comment.id)),
published: inserted_comment.published,
updated: None,
ap_id: inserted_comment.ap_id.clone(),
ap_id: Url::parse(&format!(
"https://lemmy-alpha/comment/{}",
inserted_comment.id
))
.unwrap()
.into(),
distinguished: false,
local: true,
language_id: LanguageId::default(),
Expand Down
5 changes: 4 additions & 1 deletion crates/db_schema/src/impls/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ mod tests {
use pretty_assertions::assert_eq;
use serial_test::serial;
use std::collections::HashSet;
use url::Url;

#[tokio::test]
#[serial]
Expand Down Expand Up @@ -447,7 +448,9 @@ mod tests {
embed_description: None,
embed_video_url: None,
thumbnail_url: None,
ap_id: inserted_post.ap_id.clone(),
ap_id: Url::parse(&format!("https://lemmy-alpha/post/{}", inserted_post.id))
.unwrap()
.into(),
local: true,
language_id: Default::default(),
featured_community: false,
Expand Down
8 changes: 7 additions & 1 deletion crates/db_schema/src/impls/private_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ mod tests {
};
use pretty_assertions::assert_eq;
use serial_test::serial;
use url::Url;

#[tokio::test]
#[serial]
Expand Down Expand Up @@ -138,7 +139,12 @@ mod tests {
read: false,
updated: None,
published: inserted_private_message.published,
ap_id: inserted_private_message.ap_id.clone(),
ap_id: Url::parse(&format!(
"https://lemmy-alpha/private_message/{}",
inserted_private_message.id
))
.unwrap()
.into(),
local: true,
};

Expand Down
Loading