mirror of
https://github.com/LemmyNet/lemmy.git
synced 2024-11-17 01:44:00 +00:00
Append attachments to comments (#5143)
* Append attachments to comments. * fmt * Proxy images + use newlines for separation * Use md for plain links * Use proxy_image_link directly
This commit is contained in:
parent
8f88dda28f
commit
e8875dec99
8 changed files with 130 additions and 6 deletions
|
@ -1110,7 +1110,7 @@ async fn proxy_image_link_internal(
|
||||||
|
|
||||||
/// Rewrite a link to go through `/api/v3/image_proxy` endpoint. This is only for remote urls and
|
/// Rewrite a link to go through `/api/v3/image_proxy` endpoint. This is only for remote urls and
|
||||||
/// if image_proxy setting is enabled.
|
/// if image_proxy setting is enabled.
|
||||||
pub(crate) async fn proxy_image_link(link: Url, context: &LemmyContext) -> LemmyResult<DbUrl> {
|
pub async fn proxy_image_link(link: Url, context: &LemmyContext) -> LemmyResult<DbUrl> {
|
||||||
proxy_image_link_internal(
|
proxy_image_link_internal(
|
||||||
link,
|
link,
|
||||||
context.settings().pictrs_config()?.image_mode(),
|
context.settings().pictrs_config()?.image_mode(),
|
||||||
|
|
79
crates/apub/assets/mastodon/objects/note_2.json
Normal file
79
crates/apub/assets/mastodon/objects/note_2.json
Normal file
|
@ -0,0 +1,79 @@
|
||||||
|
{
|
||||||
|
"@context": [
|
||||||
|
"https://www.w3.org/ns/activitystreams",
|
||||||
|
{
|
||||||
|
"ostatus": "http://ostatus.org#",
|
||||||
|
"atomUri": "ostatus:atomUri",
|
||||||
|
"inReplyToAtomUri": "ostatus:inReplyToAtomUri",
|
||||||
|
"conversation": "ostatus:conversation",
|
||||||
|
"sensitive": "as:sensitive",
|
||||||
|
"toot": "http://joinmastodon.org/ns#",
|
||||||
|
"votersCount": "toot:votersCount",
|
||||||
|
"blurhash": "toot:blurhash",
|
||||||
|
"focalPoint": {
|
||||||
|
"@container": "@list",
|
||||||
|
"@id": "toot:focalPoint"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"id": "https://floss.social/users/kde/statuses/113306831140126616",
|
||||||
|
"type": "Note",
|
||||||
|
"summary": null,
|
||||||
|
"inReplyTo": "https://floss.social/users/kde/statuses/113306824627995724",
|
||||||
|
"published": "2024-10-14T16:57:15Z",
|
||||||
|
"url": "https://floss.social/@kde/113306831140126616",
|
||||||
|
"attributedTo": "https://floss.social/users/kde",
|
||||||
|
"to": ["https://www.w3.org/ns/activitystreams#Public"],
|
||||||
|
"cc": [
|
||||||
|
"https://floss.social/users/kde/followers",
|
||||||
|
"https://lemmy.kde.social/c/kde",
|
||||||
|
"https://lemmy.kde.social/c/kde/followers"
|
||||||
|
],
|
||||||
|
"sensitive": false,
|
||||||
|
"atomUri": "https://floss.social/users/kde/statuses/113306831140126616",
|
||||||
|
"inReplyToAtomUri": "https://floss.social/users/kde/statuses/113306824627995724",
|
||||||
|
"conversation": "tag:floss.social,2024-10-14:objectId=71424279:objectType=Conversation",
|
||||||
|
"content": "<p><span class=\"h-card\" translate=\"no\"><a href=\"https://lemmy.kde.social/c/kde\" class=\"u-url mention\">@<span>kde@lemmy.kde.social</span></a></span> </p><p>We also need funding 💶 to keep the gears turning! Please support us with a donation:</p><p><a href=\"https://kde.org/donate/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" translate=\"no\"><span class=\"invisible\">https://</span><span class=\"\">kde.org/donate/</span><span class=\"invisible\"></span></a></p><p>[3/3]</p>",
|
||||||
|
"contentMap": {
|
||||||
|
"en": "<p><span class=\"h-card\" translate=\"no\"><a href=\"https://lemmy.kde.social/c/kde\" class=\"u-url mention\">@<span>kde@lemmy.kde.social</span></a></span> </p><p>We also need funding 💶 to keep the gears turning! Please support us with a donation:</p><p><a href=\"https://kde.org/donate/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" translate=\"no\"><span class=\"invisible\">https://</span><span class=\"\">kde.org/donate/</span><span class=\"invisible\"></span></a></p><p>[3/3]</p>"
|
||||||
|
},
|
||||||
|
"attachment": [
|
||||||
|
{
|
||||||
|
"type": "Document",
|
||||||
|
"mediaType": "image/jpeg",
|
||||||
|
"url": "https://cdn.masto.host/floss/media_attachments/files/113/306/826/682/985/891/original/c8d906a2f2ab2334.jpg",
|
||||||
|
"name": "The KDE dragons Katie and Konqi stand on either side of a pot filling up with gold coins. Donate!",
|
||||||
|
"blurhash": "USQv:h-W-qI-^,W;RPs=^-R%NZxbo#sDobSc",
|
||||||
|
"focalPoint": [0.0, 0.0],
|
||||||
|
"width": 1500,
|
||||||
|
"height": 1095
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"tag": [
|
||||||
|
{
|
||||||
|
"type": "Mention",
|
||||||
|
"href": "https://lemmy.kde.social/c/kde",
|
||||||
|
"name": "@kde@lemmy.kde.social"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"replies": {
|
||||||
|
"id": "https://floss.social/users/kde/statuses/113306831140126616/replies",
|
||||||
|
"type": "Collection",
|
||||||
|
"first": {
|
||||||
|
"type": "CollectionPage",
|
||||||
|
"next": "https://floss.social/users/kde/statuses/113306831140126616/replies?only_other_accounts=true&page=true",
|
||||||
|
"partOf": "https://floss.social/users/kde/statuses/113306831140126616/replies",
|
||||||
|
"items": []
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"likes": {
|
||||||
|
"id": "https://floss.social/users/kde/statuses/113306831140126616/likes",
|
||||||
|
"type": "Collection",
|
||||||
|
"totalItems": 39
|
||||||
|
},
|
||||||
|
"shares": {
|
||||||
|
"id": "https://floss.social/users/kde/statuses/113306831140126616/shares",
|
||||||
|
"type": "Collection",
|
||||||
|
"totalItems": 24
|
||||||
|
}
|
||||||
|
}
|
|
@ -3,7 +3,7 @@ use crate::{
|
||||||
check_apub_id_valid_with_strictness,
|
check_apub_id_valid_with_strictness,
|
||||||
fetcher::markdown_links::markdown_rewrite_remote_links,
|
fetcher::markdown_links::markdown_rewrite_remote_links,
|
||||||
mentions::collect_non_local_mentions,
|
mentions::collect_non_local_mentions,
|
||||||
objects::{read_from_string_or_source, verify_is_remote_object},
|
objects::{append_attachments_to_comment, read_from_string_or_source, verify_is_remote_object},
|
||||||
protocol::{
|
protocol::{
|
||||||
objects::{note::Note, LanguageTag},
|
objects::{note::Note, LanguageTag},
|
||||||
InCommunity,
|
InCommunity,
|
||||||
|
@ -124,6 +124,7 @@ impl Object for ApubComment {
|
||||||
distinguished: Some(self.distinguished),
|
distinguished: Some(self.distinguished),
|
||||||
language,
|
language,
|
||||||
audience: Some(community.actor_id.into()),
|
audience: Some(community.actor_id.into()),
|
||||||
|
attachment: vec![],
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(note)
|
Ok(note)
|
||||||
|
@ -181,6 +182,7 @@ impl Object for ApubComment {
|
||||||
let local_site = LocalSite::read(&mut context.pool()).await.ok();
|
let local_site = LocalSite::read(&mut context.pool()).await.ok();
|
||||||
let slur_regex = &local_site_opt_to_slur_regex(&local_site);
|
let slur_regex = &local_site_opt_to_slur_regex(&local_site);
|
||||||
let url_blocklist = get_url_blocklist(context).await?;
|
let url_blocklist = get_url_blocklist(context).await?;
|
||||||
|
let content = append_attachments_to_comment(content, ¬e.attachment, context).await?;
|
||||||
let content = process_markdown(&content, slur_regex, &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 content = markdown_rewrite_remote_links(content, context).await;
|
||||||
let language_id = Some(
|
let language_id = Some(
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::protocol::Source;
|
use crate::protocol::{objects::page::Attachment, Source};
|
||||||
use activitypub_federation::{
|
use activitypub_federation::{
|
||||||
config::Data,
|
config::Data,
|
||||||
fetch::object_id::ObjectId,
|
fetch::object_id::ObjectId,
|
||||||
|
@ -46,6 +46,23 @@ pub(crate) fn read_from_string_or_source_opt(
|
||||||
.map(|content| read_from_string_or_source(content, media_type, source))
|
.map(|content| read_from_string_or_source(content, media_type, source))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn append_attachments_to_comment(
|
||||||
|
content: String,
|
||||||
|
attachments: &[Attachment],
|
||||||
|
context: &Data<LemmyContext>,
|
||||||
|
) -> LemmyResult<String> {
|
||||||
|
let mut content = content;
|
||||||
|
// Don't modify comments with no attachments
|
||||||
|
if !attachments.is_empty() {
|
||||||
|
content += "\n";
|
||||||
|
for attachment in attachments {
|
||||||
|
content = content + "\n" + &attachment.as_markdown(context).await?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(content)
|
||||||
|
}
|
||||||
|
|
||||||
/// When for example a Post is made in a remote community, the community will send it back,
|
/// When for example a Post is made in a remote community, the community will send it back,
|
||||||
/// wrapped in Announce. If we simply receive this like any other federated object, overwrite the
|
/// wrapped in Announce. If we simply receive this like any other federated object, overwrite the
|
||||||
/// existing, local Post. In particular, it will set the field local = false, so that the object
|
/// existing, local Post. In particular, it will set the field local = false, so that the object
|
||||||
|
|
|
@ -145,7 +145,8 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_parse_objects_mastodon() -> LemmyResult<()> {
|
fn test_parse_objects_mastodon() -> LemmyResult<()> {
|
||||||
test_json::<Person>("assets/mastodon/objects/person.json")?;
|
test_json::<Person>("assets/mastodon/objects/person.json")?;
|
||||||
test_json::<Note>("assets/mastodon/objects/note.json")?;
|
test_json::<Note>("assets/mastodon/objects/note_1.json")?;
|
||||||
|
test_json::<Note>("assets/mastodon/objects/note_2.json")?;
|
||||||
test_json::<Page>("assets/mastodon/objects/page.json")?;
|
test_json::<Page>("assets/mastodon/objects/page.json")?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,11 @@ use crate::{
|
||||||
fetcher::post_or_comment::PostOrComment,
|
fetcher::post_or_comment::PostOrComment,
|
||||||
mentions::MentionOrValue,
|
mentions::MentionOrValue,
|
||||||
objects::{comment::ApubComment, community::ApubCommunity, person::ApubPerson, post::ApubPost},
|
objects::{comment::ApubComment, community::ApubCommunity, person::ApubPerson, post::ApubPost},
|
||||||
protocol::{objects::LanguageTag, InCommunity, Source},
|
protocol::{
|
||||||
|
objects::{page::Attachment, LanguageTag},
|
||||||
|
InCommunity,
|
||||||
|
Source,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
use activitypub_federation::{
|
use activitypub_federation::{
|
||||||
config::Data,
|
config::Data,
|
||||||
|
@ -50,6 +54,8 @@ pub struct Note {
|
||||||
pub(crate) distinguished: Option<bool>,
|
pub(crate) distinguished: Option<bool>,
|
||||||
pub(crate) language: Option<LanguageTag>,
|
pub(crate) language: Option<LanguageTag>,
|
||||||
pub(crate) audience: Option<ObjectId<ApubCommunity>>,
|
pub(crate) audience: Option<ObjectId<ApubCommunity>>,
|
||||||
|
#[serde(default)]
|
||||||
|
pub(crate) attachment: Vec<Attachment>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Note {
|
impl Note {
|
||||||
|
|
|
@ -19,7 +19,7 @@ use activitypub_federation::{
|
||||||
};
|
};
|
||||||
use chrono::{DateTime, Utc};
|
use chrono::{DateTime, Utc};
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use lemmy_api_common::context::LemmyContext;
|
use lemmy_api_common::{context::LemmyContext, utils::proxy_image_link};
|
||||||
use lemmy_utils::error::{FederationError, LemmyError, LemmyErrorType, LemmyResult};
|
use lemmy_utils::error::{FederationError, LemmyError, LemmyErrorType, LemmyResult};
|
||||||
use serde::{de::Error, Deserialize, Deserializer, Serialize};
|
use serde::{de::Error, Deserialize, Deserializer, Serialize};
|
||||||
use serde_with::skip_serializing_none;
|
use serde_with::skip_serializing_none;
|
||||||
|
@ -93,6 +93,7 @@ pub(crate) struct Document {
|
||||||
#[serde(rename = "type")]
|
#[serde(rename = "type")]
|
||||||
kind: DocumentType,
|
kind: DocumentType,
|
||||||
url: Url,
|
url: Url,
|
||||||
|
media_type: Option<String>,
|
||||||
/// Used for alt_text
|
/// Used for alt_text
|
||||||
name: Option<String>,
|
name: Option<String>,
|
||||||
}
|
}
|
||||||
|
@ -124,6 +125,24 @@ impl Attachment {
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn as_markdown(&self, context: &Data<LemmyContext>) -> LemmyResult<String> {
|
||||||
|
let (url, name, media_type) = match self {
|
||||||
|
Attachment::Image(i) => (i.url.clone(), i.name.clone(), Some(String::from("image"))),
|
||||||
|
Attachment::Document(d) => (d.url.clone(), d.name.clone(), d.media_type.clone()),
|
||||||
|
Attachment::Link(l) => (l.href.clone(), None, l.media_type.clone()),
|
||||||
|
};
|
||||||
|
|
||||||
|
let is_image =
|
||||||
|
media_type.is_some_and(|media| media.starts_with("video") || media.starts_with("image"));
|
||||||
|
|
||||||
|
if is_image {
|
||||||
|
let url = proxy_image_link(url, context).await?;
|
||||||
|
Ok(format!("![{}]({url})", name.unwrap_or_default()))
|
||||||
|
} else {
|
||||||
|
Ok(format!("[{url}]({url})"))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||||
|
|
Loading…
Reference in a new issue