use crate::{
- extensions::context::lemmy_context,
- fetcher::objects::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post},
- objects::{
- check_object_domain,
- check_object_for_community_or_site_ban,
- create_tombstone,
- get_object_from_apub,
- get_or_fetch_and_upsert_person,
- get_source_markdown_value,
- set_content_and_source,
- FromApub,
- FromApubToForm,
- ToApub,
- },
- NoteExt,
+ activities::{verify_is_public, verify_person_in_community},
+ check_apub_id_valid_with_strictness,
+ local_instance,
+ mentions::collect_non_local_mentions,
+ objects::{read_from_string_or_source, verify_is_remote_object},
+ protocol::{objects::note::Note, Source},
+ PostOrComment,
};
-use activitystreams::{
- object::{kind::NoteType, ApObject, Note, Tombstone},
- prelude::*,
- public,
+use activitypub_federation::{
+ core::object_id::ObjectId,
+ deser::values::MediaTypeMarkdownOrHtml,
+ traits::ApubObject,
+ utils::verify_domains_match,
};
-use anyhow::{anyhow, Context};
-use lemmy_api_structs::blocking;
-use lemmy_db_queries::{Crud, DbPool};
+use activitystreams_kinds::{object::NoteType, public};
+use chrono::NaiveDateTime;
+use lemmy_api_common::utils::blocking;
use lemmy_db_schema::{
source::{
comment::{Comment, CommentForm},
person::Person,
post::Post,
},
- CommentId,
+ traits::Crud,
};
use lemmy_utils::{
- location_info,
- utils::{convert_datetime, remove_slurs},
- LemmyError,
+ error::LemmyError,
+ utils::{convert_datetime, markdown_to_html, remove_slurs},
};
use lemmy_websocket::LemmyContext;
+use std::ops::Deref;
use url::Url;
-#[async_trait::async_trait(?Send)]
-impl ToApub for Comment {
- type ApubType = NoteExt;
-
- async fn to_apub(&self, pool: &DbPool) -> Result<NoteExt, LemmyError> {
- let mut comment = ApObject::new(Note::new());
+#[derive(Clone, Debug)]
+pub struct ApubComment(Comment);
- let creator_id = self.creator_id;
- let creator = blocking(pool, move |conn| Person::read(conn, creator_id)).await??;
+impl Deref for ApubComment {
+ type Target = Comment;
+ fn deref(&self) -> &Self::Target {
+ &self.0
+ }
+}
- let post_id = self.post_id;
- let post = blocking(pool, move |conn| Post::read(conn, post_id)).await??;
+impl From<Comment> for ApubComment {
+ fn from(c: Comment) -> Self {
+ ApubComment(c)
+ }
+}
- let community_id = post.community_id;
- let community = blocking(pool, move |conn| Community::read(conn, community_id)).await??;
+#[async_trait::async_trait(?Send)]
+impl ApubObject for ApubComment {
+ type DataType = LemmyContext;
+ type ApubType = Note;
+ type DbType = Comment;
+ type Error = LemmyError;
- // Add a vector containing some important info to the "in_reply_to" field
- // [post_ap_id, Option(parent_comment_ap_id)]
- let mut in_reply_to_vec = vec![post.ap_id.into_inner()];
+ fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
+ None
+ }
- if let Some(parent_id) = self.parent_id {
- let parent_comment = blocking(pool, move |conn| Comment::read(conn, parent_id)).await??;
+ #[tracing::instrument(skip_all)]
+ async fn read_from_apub_id(
+ object_id: Url,
+ context: &LemmyContext,
+ ) -> Result<Option<Self>, LemmyError> {
+ Ok(
+ blocking(context.pool(), move |conn| {
+ Comment::read_from_apub_id(conn, object_id)
+ })
+ .await??
+ .map(Into::into),
+ )
+ }
- in_reply_to_vec.push(parent_comment.ap_id.into_inner());
+ #[tracing::instrument(skip_all)]
+ async fn delete(self, context: &LemmyContext) -> Result<(), LemmyError> {
+ if !self.deleted {
+ blocking(context.pool(), move |conn| {
+ Comment::update_deleted(conn, self.id, true)
+ })
+ .await??;
}
+ Ok(())
+ }
- comment
- // Not needed when the Post is embedded in a collection (like for community outbox)
- .set_many_contexts(lemmy_context()?)
- .set_id(self.ap_id.to_owned().into_inner())
- .set_published(convert_datetime(self.published))
- // NOTE: included community id for compatibility with lemmy v0.9.9
- .set_many_tos(vec![community.actor_id.into_inner(), public()])
- .set_many_in_reply_tos(in_reply_to_vec)
- .set_attributed_to(creator.actor_id.into_inner());
+ #[tracing::instrument(skip_all)]
+ async fn into_apub(self, context: &LemmyContext) -> Result<Note, LemmyError> {
+ let creator_id = self.creator_id;
+ let creator = blocking(context.pool(), move |conn| Person::read(conn, creator_id)).await??;
- set_content_and_source(&mut comment, &self.content)?;
+ let post_id = self.post_id;
+ let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
+ let community_id = post.community_id;
+ let community = blocking(context.pool(), move |conn| {
+ Community::read(conn, community_id)
+ })
+ .await??;
- if let Some(u) = self.updated {
- comment.set_updated(convert_datetime(u));
- }
+ let in_reply_to = if let Some(comment_id) = self.parent_comment_id() {
+ let parent_comment =
+ blocking(context.pool(), move |conn| Comment::read(conn, comment_id)).await??;
+ ObjectId::<PostOrComment>::new(parent_comment.ap_id)
+ } else {
+ ObjectId::<PostOrComment>::new(post.ap_id)
+ };
+ let maa =
+ collect_non_local_mentions(&self, ObjectId::new(community.actor_id), context, &mut 0).await?;
- Ok(comment)
- }
+ let note = Note {
+ r#type: NoteType::Note,
+ id: ObjectId::new(self.ap_id.clone()),
+ attributed_to: ObjectId::new(creator.actor_id),
+ to: vec![public()],
+ cc: maa.ccs,
+ content: markdown_to_html(&self.content),
+ media_type: Some(MediaTypeMarkdownOrHtml::Html),
+ source: Some(Source::new(self.content.clone())),
+ in_reply_to,
+ published: Some(convert_datetime(self.published)),
+ updated: self.updated.map(convert_datetime),
+ tag: maa.tags,
+ };
- fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
- create_tombstone(
- self.deleted,
- self.ap_id.to_owned().into(),
- self.updated,
- NoteType::Note,
- )
+ Ok(note)
}
-}
-
-#[async_trait::async_trait(?Send)]
-impl FromApub for Comment {
- type ApubType = NoteExt;
- /// Converts a `Note` to `Comment`.
- ///
- /// If the parent community, post and comment(s) are not known locally, these are also fetched.
- async fn from_apub(
- note: &NoteExt,
+ #[tracing::instrument(skip_all)]
+ async fn verify(
+ note: &Note,
+ expected_domain: &Url,
context: &LemmyContext,
- expected_domain: Url,
request_counter: &mut i32,
- mod_action_allowed: bool,
- ) -> Result<Comment, LemmyError> {
- let comment: Comment = get_object_from_apub(
- note,
+ ) -> Result<(), LemmyError> {
+ verify_domains_match(note.id.inner(), expected_domain)?;
+ verify_domains_match(note.attributed_to.inner(), note.id.inner())?;
+ verify_is_public(¬e.to, ¬e.cc)?;
+ let (post, _) = note.get_parents(context, request_counter).await?;
+ let community_id = post.community_id;
+ let community = blocking(context.pool(), move |conn| {
+ Community::read(conn, community_id)
+ })
+ .await??;
+ check_apub_id_valid_with_strictness(note.id.inner(), community.local, context.settings())?;
+ verify_is_remote_object(note.id.inner(), context.settings())?;
+ verify_person_in_community(
+ ¬e.attributed_to,
+ &community.into(),
context,
- expected_domain,
request_counter,
- mod_action_allowed,
)
.await?;
-
- let post_id = comment.post_id;
- let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
- check_object_for_community_or_site_ban(note, post.community_id, context, request_counter)
- .await?;
if post.locked {
- // This is not very efficient because a comment gets inserted just to be deleted right
- // afterwards, but it seems to be the easiest way to implement it.
- blocking(context.pool(), move |conn| {
- Comment::delete(conn, comment.id)
- })
- .await??;
- Err(anyhow!("Post is locked").into())
- } else {
- Ok(comment)
+ return Err(LemmyError::from_message("Post is locked"));
}
+ Ok(())
}
-}
-#[async_trait::async_trait(?Send)]
-impl FromApubToForm<NoteExt> for CommentForm {
+ /// Converts a `Note` to `Comment`.
+ ///
+ /// If the parent community, post and comment(s) are not known locally, these are also fetched.
+ #[tracing::instrument(skip_all)]
async fn from_apub(
- note: &NoteExt,
+ note: Note,
context: &LemmyContext,
- expected_domain: Url,
request_counter: &mut i32,
- _mod_action_allowed: bool,
- ) -> Result<CommentForm, LemmyError> {
- let creator_actor_id = ¬e
- .attributed_to()
- .context(location_info!())?
- .as_single_xsd_any_uri()
- .context(location_info!())?;
-
- let creator =
- get_or_fetch_and_upsert_person(creator_actor_id, context, request_counter).await?;
-
- let mut in_reply_tos = note
- .in_reply_to()
- .as_ref()
- .context(location_info!())?
- .as_many()
- .context(location_info!())?
- .iter()
- .map(|i| i.as_xsd_any_uri().context(""));
- let post_ap_id = in_reply_tos.next().context(location_info!())??;
-
- // This post, or the parent comment might not yet exist on this server yet, fetch them.
- let post = Box::pin(get_or_fetch_and_insert_post(
- &post_ap_id,
- context,
- request_counter,
- ))
- .await?;
-
- // The 2nd item, if it exists, is the parent comment apub_id
- // For deeply nested comments, FromApub automatically gets called recursively
- let parent_id: Option<CommentId> = match in_reply_tos.next() {
- Some(parent_comment_uri) => {
- let parent_comment_ap_id = &parent_comment_uri?;
- let parent_comment = Box::pin(get_or_fetch_and_insert_comment(
- &parent_comment_ap_id,
- context,
- request_counter,
- ))
- .await?;
-
- Some(parent_comment.id)
- }
- None => None,
- };
+ ) -> Result<ApubComment, LemmyError> {
+ let creator = note
+ .attributed_to
+ .dereference(context, local_instance(context), request_counter)
+ .await?;
+ let (post, parent_comment) = note.get_parents(context, request_counter).await?;
- let content = get_source_markdown_value(note)?.context(location_info!())?;
- let content_slurs_removed = remove_slurs(&content);
+ let content = read_from_string_or_source(¬e.content, ¬e.media_type, ¬e.source);
+ let content_slurs_removed = remove_slurs(&content, &context.settings().slur_regex());
- Ok(CommentForm {
+ let form = CommentForm {
creator_id: creator.id,
post_id: post.id,
- parent_id,
content: content_slurs_removed,
removed: None,
- read: None,
- published: note.published().map(|u| u.to_owned().naive_local()),
- updated: note.updated().map(|u| u.to_owned().naive_local()),
+ published: note.published.map(|u| u.naive_local()),
+ updated: note.updated.map(|u| u.naive_local()),
deleted: None,
- ap_id: Some(check_object_domain(note, expected_domain)?),
+ ap_id: Some(note.id.into()),
local: Some(false),
+ };
+ let parent_comment_path = parent_comment.map(|t| t.0.path);
+ let comment = blocking(context.pool(), move |conn| {
+ Comment::create(conn, &form, parent_comment_path.as_ref())
})
+ .await??;
+ Ok(comment.into())
+ }
+}
+
+#[cfg(test)]
+pub(crate) mod tests {
+ use super::*;
+ use crate::{
+ objects::{
+ community::{tests::parse_lemmy_community, ApubCommunity},
+ instance::ApubSite,
+ person::{tests::parse_lemmy_person, ApubPerson},
+ post::ApubPost,
+ tests::init_context,
+ },
+ protocol::tests::file_to_json_object,
+ };
+ use assert_json_diff::assert_json_include;
+ use html2md::parse_html;
+ use lemmy_db_schema::source::site::Site;
+ use serial_test::serial;
+
+ async fn prepare_comment_test(
+ url: &Url,
+ context: &LemmyContext,
+ ) -> (ApubPerson, ApubCommunity, ApubPost, ApubSite) {
+ let (person, site) = parse_lemmy_person(context).await;
+ let community = parse_lemmy_community(context).await;
+ let post_json = file_to_json_object("assets/lemmy/objects/page.json").unwrap();
+ ApubPost::verify(&post_json, url, context, &mut 0)
+ .await
+ .unwrap();
+ let post = ApubPost::from_apub(post_json, context, &mut 0)
+ .await
+ .unwrap();
+ (person, community, post, site)
+ }
+
+ fn cleanup(data: (ApubPerson, ApubCommunity, ApubPost, ApubSite), context: &LemmyContext) {
+ Post::delete(&*context.pool().get().unwrap(), data.2.id).unwrap();
+ Community::delete(&*context.pool().get().unwrap(), data.1.id).unwrap();
+ Person::delete(&*context.pool().get().unwrap(), data.0.id).unwrap();
+ Site::delete(&*context.pool().get().unwrap(), data.3.id).unwrap();
+ }
+
+ #[actix_rt::test]
+ #[serial]
+ pub(crate) async fn test_parse_lemmy_comment() {
+ let context = init_context();
+ let url = Url::parse("https://enterprise.lemmy.ml/comment/38741").unwrap();
+ let data = prepare_comment_test(&url, &context).await;
+
+ let json: Note = file_to_json_object("assets/lemmy/objects/note.json").unwrap();
+ let mut request_counter = 0;
+ ApubComment::verify(&json, &url, &context, &mut request_counter)
+ .await
+ .unwrap();
+ let comment = ApubComment::from_apub(json.clone(), &context, &mut request_counter)
+ .await
+ .unwrap();
+
+ assert_eq!(comment.ap_id, url.into());
+ assert_eq!(comment.content.len(), 14);
+ assert!(!comment.local);
+ assert_eq!(request_counter, 0);
+
+ let comment_id = comment.id;
+ let to_apub = comment.into_apub(&context).await.unwrap();
+ assert_json_include!(actual: json, expected: to_apub);
+
+ Comment::delete(&*context.pool().get().unwrap(), comment_id).unwrap();
+ cleanup(data, &context);
+ }
+
+ #[actix_rt::test]
+ #[serial]
+ async fn test_parse_pleroma_comment() {
+ let context = init_context();
+ let url = Url::parse("https://enterprise.lemmy.ml/comment/38741").unwrap();
+ let data = prepare_comment_test(&url, &context).await;
+
+ let pleroma_url =
+ Url::parse("https://queer.hacktivis.me/objects/8d4973f4-53de-49cd-8c27-df160e16a9c2")
+ .unwrap();
+ let person_json = file_to_json_object("assets/pleroma/objects/person.json").unwrap();
+ ApubPerson::verify(&person_json, &pleroma_url, &context, &mut 0)
+ .await
+ .unwrap();
+ ApubPerson::from_apub(person_json, &context, &mut 0)
+ .await
+ .unwrap();
+ let json = file_to_json_object("assets/pleroma/objects/note.json").unwrap();
+ let mut request_counter = 0;
+ ApubComment::verify(&json, &pleroma_url, &context, &mut request_counter)
+ .await
+ .unwrap();
+ let comment = ApubComment::from_apub(json, &context, &mut request_counter)
+ .await
+ .unwrap();
+
+ assert_eq!(comment.ap_id, pleroma_url.into());
+ assert_eq!(comment.content.len(), 64);
+ assert!(!comment.local);
+ assert_eq!(request_counter, 0);
+
+ Comment::delete(&*context.pool().get().unwrap(), comment.id).unwrap();
+ cleanup(data, &context);
+ }
+
+ #[actix_rt::test]
+ #[serial]
+ async fn test_html_to_markdown_sanitize() {
+ let parsed = parse_html("<script></script><b>hello</b>");
+ assert_eq!(parsed, "**hello**");
}
}