From dbeeb755d1609ca179a8e224c0710ccc71c1f5b7 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Mon, 30 Sep 2024 12:40:21 +0200 Subject: [PATCH] Call from apub handlers, cleanup --- .../fetcher/{post_links.rs => markdown_links.rs} | 16 ++++++++++------ crates/apub/src/fetcher/mod.rs | 2 +- crates/apub/src/objects/comment.rs | 2 ++ crates/apub/src/objects/community.rs | 2 ++ crates/apub/src/objects/instance.rs | 2 ++ crates/apub/src/objects/person.rs | 2 ++ crates/apub/src/objects/post.rs | 4 ++-- crates/apub/src/objects/private_message.rs | 2 ++ 8 files changed, 23 insertions(+), 9 deletions(-) rename crates/apub/src/fetcher/{post_links.rs => markdown_links.rs} (88%) diff --git a/crates/apub/src/fetcher/post_links.rs b/crates/apub/src/fetcher/markdown_links.rs similarity index 88% rename from crates/apub/src/fetcher/post_links.rs rename to crates/apub/src/fetcher/markdown_links.rs index 7486f0030..dd66c1a5f 100644 --- a/crates/apub/src/fetcher/post_links.rs +++ b/crates/apub/src/fetcher/markdown_links.rs @@ -12,19 +12,23 @@ use lemmy_utils::{ }; use url::Url; -pub async fn markdown_rewrite_remote_post_links_opt( +pub async fn markdown_rewrite_remote_links_opt( src: Option, context: &Data, ) -> Option { match src { - Some(t) => Some(markdown_rewrite_remote_post_links(t, context).await), + Some(t) => Some(markdown_rewrite_remote_links(t, context).await), None => None, } } -// TODO: call this logic for comment.text etc -/// TODO: as it uses ObjectId::dereference, it can currently only be used in apub crate -pub async fn markdown_rewrite_remote_post_links( +/// Goes through all remote markdown links and attempts to resolve them as Activitypub objects. +/// If successful, the link is rewritten to a local link, so it can be viewed without leaving the +/// local instance. +/// +/// As it relies on ObjectId::dereference, it can only be used for incoming federated objects, not +/// for the API. +pub async fn markdown_rewrite_remote_links( mut src: String, context: &Data, ) -> String { @@ -142,7 +146,7 @@ mod tests { let context = LemmyContext::init_test_context().await; for &(msg, input, expected) in &tests { - let result = markdown_rewrite_remote_post_links(input.to_string(), &context).await; + let result = markdown_rewrite_remote_links(input.to_string(), &context).await; assert_eq!( result, expected, diff --git a/crates/apub/src/fetcher/mod.rs b/crates/apub/src/fetcher/mod.rs index 9b8a395d6..29202004f 100644 --- a/crates/apub/src/fetcher/mod.rs +++ b/crates/apub/src/fetcher/mod.rs @@ -10,7 +10,7 @@ use lemmy_db_schema::traits::ApubActor; use lemmy_db_views::structs::LocalUserView; use lemmy_utils::error::{LemmyError, LemmyResult}; -pub(crate) mod post_links; +pub(crate) mod markdown_links; pub mod post_or_comment; pub mod search; pub mod site_or_community_or_user; diff --git a/crates/apub/src/objects/comment.rs b/crates/apub/src/objects/comment.rs index ad7d84a6f..67335d20e 100644 --- a/crates/apub/src/objects/comment.rs +++ b/crates/apub/src/objects/comment.rs @@ -1,6 +1,7 @@ use crate::{ activities::{verify_is_public, verify_person_in_community}, check_apub_id_valid_with_strictness, + fetcher::markdown_links::markdown_rewrite_remote_links, mentions::collect_non_local_mentions, objects::{read_from_string_or_source, verify_is_remote_object}, protocol::{ @@ -169,6 +170,7 @@ impl Object for ApubComment { let slur_regex = &local_site_opt_to_slur_regex(&local_site); let url_blocklist = get_url_blocklist(context).await?; let content = process_markdown(&content, slur_regex, &url_blocklist, context).await?; + let content = markdown_rewrite_remote_links(content, context).await; let language_id = LanguageTag::to_language_id_single(note.language, &mut context.pool()).await?; diff --git a/crates/apub/src/objects/community.rs b/crates/apub/src/objects/community.rs index d7e2490a7..ab37f748d 100644 --- a/crates/apub/src/objects/community.rs +++ b/crates/apub/src/objects/community.rs @@ -1,6 +1,7 @@ use crate::{ activities::GetActorType, check_apub_id_valid, + fetcher::markdown_links::markdown_rewrite_remote_links_opt, local_site_data_cached, objects::{instance::fetch_instance_actor_for_object, read_from_string_or_source_opt}, protocol::{ @@ -148,6 +149,7 @@ impl Object for ApubCommunity { let description = read_from_string_or_source_opt(&group.summary, &None, &group.source); let description = process_markdown_opt(&description, slur_regex, &url_blocklist, context).await?; + let description = markdown_rewrite_remote_links_opt(description, context).await; let icon = proxy_image_link_opt_apub(group.icon.map(|i| i.url), context).await?; let banner = proxy_image_link_opt_apub(group.image.map(|i| i.url), context).await?; diff --git a/crates/apub/src/objects/instance.rs b/crates/apub/src/objects/instance.rs index c67a223e0..6ee0a41dc 100644 --- a/crates/apub/src/objects/instance.rs +++ b/crates/apub/src/objects/instance.rs @@ -2,6 +2,7 @@ use super::verify_is_remote_object; use crate::{ activities::GetActorType, check_apub_id_valid_with_strictness, + fetcher::markdown_links::markdown_rewrite_remote_links_opt, local_site_data_cached, objects::read_from_string_or_source_opt, protocol::{ @@ -151,6 +152,7 @@ impl Object for ApubSite { let url_blocklist = get_url_blocklist(context).await?; let sidebar = read_from_string_or_source_opt(&apub.content, &None, &apub.source); let sidebar = process_markdown_opt(&sidebar, slur_regex, &url_blocklist, context).await?; + let sidebar = markdown_rewrite_remote_links_opt(sidebar, context).await; let icon = proxy_image_link_opt_apub(apub.icon.map(|i| i.url), context).await?; let banner = proxy_image_link_opt_apub(apub.image.map(|i| i.url), context).await?; diff --git a/crates/apub/src/objects/person.rs b/crates/apub/src/objects/person.rs index 61ff04622..7325b16fe 100644 --- a/crates/apub/src/objects/person.rs +++ b/crates/apub/src/objects/person.rs @@ -2,6 +2,7 @@ use super::verify_is_remote_object; use crate::{ activities::GetActorType, check_apub_id_valid_with_strictness, + fetcher::markdown_links::markdown_rewrite_remote_links_opt, local_site_data_cached, objects::{instance::fetch_instance_actor_for_object, read_from_string_or_source_opt}, protocol::{ @@ -156,6 +157,7 @@ impl Object for ApubPerson { let url_blocklist = get_url_blocklist(context).await?; let bio = read_from_string_or_source_opt(&person.summary, &None, &person.source); let bio = process_markdown_opt(&bio, slur_regex, &url_blocklist, context).await?; + let bio = markdown_rewrite_remote_links_opt(bio, context).await; let avatar = proxy_image_link_opt_apub(person.icon.map(|i| i.url), context).await?; let banner = proxy_image_link_opt_apub(person.image.map(|i| i.url), context).await?; diff --git a/crates/apub/src/objects/post.rs b/crates/apub/src/objects/post.rs index b6754362d..e1cc57ca3 100644 --- a/crates/apub/src/objects/post.rs +++ b/crates/apub/src/objects/post.rs @@ -1,7 +1,7 @@ use crate::{ activities::{verify_is_public, verify_person_in_community}, check_apub_id_valid_with_strictness, - fetcher::post_links::{markdown_rewrite_remote_post_links_opt, to_local_url}, + fetcher::markdown_links::{markdown_rewrite_remote_links_opt, to_local_url}, local_site_data_cached, objects::{read_from_string_or_source_opt, verify_is_remote_object}, protocol::{ @@ -241,7 +241,7 @@ impl Object for ApubPost { let body = read_from_string_or_source_opt(&page.content, &page.media_type, &page.source); let body = process_markdown_opt(&body, slur_regex, &url_blocklist, context).await?; - let body = markdown_rewrite_remote_post_links_opt(body, context).await; + let body = markdown_rewrite_remote_links_opt(body, context).await; let language_id = LanguageTag::to_language_id_single(page.language, &mut context.pool()).await?; diff --git a/crates/apub/src/objects/private_message.rs b/crates/apub/src/objects/private_message.rs index d3ca340db..3ed5b3572 100644 --- a/crates/apub/src/objects/private_message.rs +++ b/crates/apub/src/objects/private_message.rs @@ -1,6 +1,7 @@ use super::verify_is_remote_object; use crate::{ check_apub_id_valid_with_strictness, + fetcher::markdown_links::markdown_rewrite_remote_links, objects::read_from_string_or_source, protocol::{ objects::chat_message::{ChatMessage, ChatMessageType}, @@ -134,6 +135,7 @@ impl Object for ApubPrivateMessage { let url_blocklist = get_url_blocklist(context).await?; let content = read_from_string_or_source(¬e.content, &None, ¬e.source); let content = process_markdown(&content, slur_regex, &url_blocklist, context).await?; + let content = markdown_rewrite_remote_links(content, context).await; let form = PrivateMessageInsertForm { creator_id: creator.id,