2 activities::{verify_is_public, verify_person_in_community},
3 check_apub_id_valid_with_strictness,
5 mentions::collect_non_local_mentions,
6 objects::{read_from_string_or_source, verify_is_remote_object},
7 protocol::{objects::note::Note, Source},
10 use activitypub_federation::{
11 core::object_id::ObjectId,
12 deser::values::MediaTypeMarkdownOrHtml,
14 utils::verify_domains_match,
16 use activitystreams_kinds::{object::NoteType, public};
17 use chrono::NaiveDateTime;
18 use lemmy_api_common::utils::blocking;
19 use lemmy_db_schema::{
21 comment::{Comment, CommentForm},
30 utils::{convert_datetime, markdown_to_html, remove_slurs},
32 use lemmy_websocket::LemmyContext;
36 #[derive(Clone, Debug)]
37 pub struct ApubComment(Comment);
39 impl Deref for ApubComment {
40 type Target = Comment;
41 fn deref(&self) -> &Self::Target {
46 impl From<Comment> for ApubComment {
47 fn from(c: Comment) -> Self {
52 #[async_trait::async_trait(?Send)]
53 impl ApubObject for ApubComment {
54 type DataType = LemmyContext;
56 type DbType = Comment;
57 type Error = LemmyError;
59 fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
63 #[tracing::instrument(skip_all)]
64 async fn read_from_apub_id(
66 context: &LemmyContext,
67 ) -> Result<Option<Self>, LemmyError> {
69 blocking(context.pool(), move |conn| {
70 Comment::read_from_apub_id(conn, object_id)
77 #[tracing::instrument(skip_all)]
78 async fn delete(self, context: &LemmyContext) -> Result<(), LemmyError> {
80 blocking(context.pool(), move |conn| {
81 Comment::update_deleted(conn, self.id, true)
88 #[tracing::instrument(skip_all)]
89 async fn into_apub(self, context: &LemmyContext) -> Result<Note, LemmyError> {
90 let creator_id = self.creator_id;
91 let creator = blocking(context.pool(), move |conn| Person::read(conn, creator_id)).await??;
93 let post_id = self.post_id;
94 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
95 let community_id = post.community_id;
96 let community = blocking(context.pool(), move |conn| {
97 Community::read(conn, community_id)
101 let in_reply_to = if let Some(comment_id) = self.parent_comment_id() {
103 blocking(context.pool(), move |conn| Comment::read(conn, comment_id)).await??;
104 ObjectId::<PostOrComment>::new(parent_comment.ap_id)
106 ObjectId::<PostOrComment>::new(post.ap_id)
109 collect_non_local_mentions(&self, ObjectId::new(community.actor_id), context, &mut 0).await?;
112 r#type: NoteType::Note,
113 id: ObjectId::new(self.ap_id.clone()),
114 attributed_to: ObjectId::new(creator.actor_id),
117 content: markdown_to_html(&self.content),
118 media_type: Some(MediaTypeMarkdownOrHtml::Html),
119 source: Some(Source::new(self.content.clone())),
121 published: Some(convert_datetime(self.published)),
122 updated: self.updated.map(convert_datetime),
124 distinguished: Some(self.distinguished),
130 #[tracing::instrument(skip_all)]
133 expected_domain: &Url,
134 context: &LemmyContext,
135 request_counter: &mut i32,
136 ) -> Result<(), LemmyError> {
137 verify_domains_match(note.id.inner(), expected_domain)?;
138 verify_domains_match(note.attributed_to.inner(), note.id.inner())?;
139 verify_is_public(¬e.to, ¬e.cc)?;
140 let (post, _) = note.get_parents(context, request_counter).await?;
141 let community_id = post.community_id;
142 let community = blocking(context.pool(), move |conn| {
143 Community::read(conn, community_id)
146 check_apub_id_valid_with_strictness(note.id.inner(), community.local, context.settings())?;
147 verify_is_remote_object(note.id.inner(), context.settings())?;
148 verify_person_in_community(
156 return Err(LemmyError::from_message("Post is locked"));
161 /// Converts a `Note` to `Comment`.
163 /// If the parent community, post and comment(s) are not known locally, these are also fetched.
164 #[tracing::instrument(skip_all)]
167 context: &LemmyContext,
168 request_counter: &mut i32,
169 ) -> Result<ApubComment, LemmyError> {
172 .dereference(context, local_instance(context), request_counter)
174 let (post, parent_comment) = note.get_parents(context, request_counter).await?;
176 let content = read_from_string_or_source(¬e.content, ¬e.media_type, ¬e.source);
177 let content_slurs_removed = remove_slurs(&content, &context.settings().slur_regex());
179 let form = CommentForm {
180 creator_id: creator.id,
182 content: content_slurs_removed,
184 published: note.published.map(|u| u.naive_local()),
185 updated: note.updated.map(|u| u.naive_local()),
187 ap_id: Some(note.id.into()),
188 distinguished: note.distinguished,
191 let parent_comment_path = parent_comment.map(|t| t.0.path);
192 let comment = blocking(context.pool(), move |conn| {
193 Comment::create(conn, &form, parent_comment_path.as_ref())
201 pub(crate) mod tests {
205 community::{tests::parse_lemmy_community, ApubCommunity},
207 person::{tests::parse_lemmy_person, ApubPerson},
211 protocol::tests::file_to_json_object,
213 use assert_json_diff::assert_json_include;
214 use html2md::parse_html;
215 use lemmy_db_schema::source::site::Site;
216 use serial_test::serial;
218 async fn prepare_comment_test(
220 context: &LemmyContext,
221 ) -> (ApubPerson, ApubCommunity, ApubPost, ApubSite) {
222 let (person, site) = parse_lemmy_person(context).await;
223 let community = parse_lemmy_community(context).await;
224 let post_json = file_to_json_object("assets/lemmy/objects/page.json").unwrap();
225 ApubPost::verify(&post_json, url, context, &mut 0)
228 let post = ApubPost::from_apub(post_json, context, &mut 0)
231 (person, community, post, site)
234 fn cleanup(data: (ApubPerson, ApubCommunity, ApubPost, ApubSite), context: &LemmyContext) {
235 Post::delete(&*context.pool().get().unwrap(), data.2.id).unwrap();
236 Community::delete(&*context.pool().get().unwrap(), data.1.id).unwrap();
237 Person::delete(&*context.pool().get().unwrap(), data.0.id).unwrap();
238 Site::delete(&*context.pool().get().unwrap(), data.3.id).unwrap();
243 pub(crate) async fn test_parse_lemmy_comment() {
244 let context = init_context();
245 let url = Url::parse("https://enterprise.lemmy.ml/comment/38741").unwrap();
246 let data = prepare_comment_test(&url, &context).await;
248 let json: Note = file_to_json_object("assets/lemmy/objects/note.json").unwrap();
249 let mut request_counter = 0;
250 ApubComment::verify(&json, &url, &context, &mut request_counter)
253 let comment = ApubComment::from_apub(json.clone(), &context, &mut request_counter)
257 assert_eq!(comment.ap_id, url.into());
258 assert_eq!(comment.content.len(), 14);
259 assert!(!comment.local);
260 assert_eq!(request_counter, 0);
262 let comment_id = comment.id;
263 let to_apub = comment.into_apub(&context).await.unwrap();
264 assert_json_include!(actual: json, expected: to_apub);
266 Comment::delete(&*context.pool().get().unwrap(), comment_id).unwrap();
267 cleanup(data, &context);
272 async fn test_parse_pleroma_comment() {
273 let context = init_context();
274 let url = Url::parse("https://enterprise.lemmy.ml/comment/38741").unwrap();
275 let data = prepare_comment_test(&url, &context).await;
278 Url::parse("https://queer.hacktivis.me/objects/8d4973f4-53de-49cd-8c27-df160e16a9c2")
280 let person_json = file_to_json_object("assets/pleroma/objects/person.json").unwrap();
281 ApubPerson::verify(&person_json, &pleroma_url, &context, &mut 0)
284 ApubPerson::from_apub(person_json, &context, &mut 0)
287 let json = file_to_json_object("assets/pleroma/objects/note.json").unwrap();
288 let mut request_counter = 0;
289 ApubComment::verify(&json, &pleroma_url, &context, &mut request_counter)
292 let comment = ApubComment::from_apub(json, &context, &mut request_counter)
296 assert_eq!(comment.ap_id, pleroma_url.into());
297 assert_eq!(comment.content.len(), 64);
298 assert!(!comment.local);
299 assert_eq!(request_counter, 0);
301 Comment::delete(&*context.pool().get().unwrap(), comment.id).unwrap();
302 cleanup(data, &context);
307 async fn test_html_to_markdown_sanitize() {
308 let parsed = parse_html("<script></script><b>hello</b>");
309 assert_eq!(parsed, "**hello**");