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),
129 #[tracing::instrument(skip_all)]
132 expected_domain: &Url,
133 context: &LemmyContext,
134 request_counter: &mut i32,
135 ) -> Result<(), LemmyError> {
136 verify_domains_match(note.id.inner(), expected_domain)?;
137 verify_domains_match(note.attributed_to.inner(), note.id.inner())?;
138 verify_is_public(¬e.to, ¬e.cc)?;
139 let (post, _) = note.get_parents(context, request_counter).await?;
140 let community_id = post.community_id;
141 let community = blocking(context.pool(), move |conn| {
142 Community::read(conn, community_id)
145 check_apub_id_valid_with_strictness(note.id.inner(), community.local, context.settings())?;
146 verify_is_remote_object(note.id.inner(), context.settings())?;
147 verify_person_in_community(
155 return Err(LemmyError::from_message("Post is locked"));
160 /// Converts a `Note` to `Comment`.
162 /// If the parent community, post and comment(s) are not known locally, these are also fetched.
163 #[tracing::instrument(skip_all)]
166 context: &LemmyContext,
167 request_counter: &mut i32,
168 ) -> Result<ApubComment, LemmyError> {
171 .dereference(context, local_instance(context), request_counter)
173 let (post, parent_comment) = note.get_parents(context, request_counter).await?;
175 let content = read_from_string_or_source(¬e.content, ¬e.media_type, ¬e.source);
176 let content_slurs_removed = remove_slurs(&content, &context.settings().slur_regex());
178 let form = CommentForm {
179 creator_id: creator.id,
181 content: content_slurs_removed,
183 published: note.published.map(|u| u.naive_local()),
184 updated: note.updated.map(|u| u.naive_local()),
186 ap_id: Some(note.id.into()),
189 let parent_comment_path = parent_comment.map(|t| t.0.path);
190 let comment = blocking(context.pool(), move |conn| {
191 Comment::create(conn, &form, parent_comment_path.as_ref())
199 pub(crate) mod tests {
203 community::{tests::parse_lemmy_community, ApubCommunity},
205 person::{tests::parse_lemmy_person, ApubPerson},
209 protocol::tests::file_to_json_object,
211 use assert_json_diff::assert_json_include;
212 use html2md::parse_html;
213 use lemmy_db_schema::source::site::Site;
214 use serial_test::serial;
216 async fn prepare_comment_test(
218 context: &LemmyContext,
219 ) -> (ApubPerson, ApubCommunity, ApubPost, ApubSite) {
220 let (person, site) = parse_lemmy_person(context).await;
221 let community = parse_lemmy_community(context).await;
222 let post_json = file_to_json_object("assets/lemmy/objects/page.json").unwrap();
223 ApubPost::verify(&post_json, url, context, &mut 0)
226 let post = ApubPost::from_apub(post_json, context, &mut 0)
229 (person, community, post, site)
232 fn cleanup(data: (ApubPerson, ApubCommunity, ApubPost, ApubSite), context: &LemmyContext) {
233 Post::delete(&*context.pool().get().unwrap(), data.2.id).unwrap();
234 Community::delete(&*context.pool().get().unwrap(), data.1.id).unwrap();
235 Person::delete(&*context.pool().get().unwrap(), data.0.id).unwrap();
236 Site::delete(&*context.pool().get().unwrap(), data.3.id).unwrap();
241 pub(crate) async fn test_parse_lemmy_comment() {
242 let context = init_context();
243 let url = Url::parse("https://enterprise.lemmy.ml/comment/38741").unwrap();
244 let data = prepare_comment_test(&url, &context).await;
246 let json: Note = file_to_json_object("assets/lemmy/objects/note.json").unwrap();
247 let mut request_counter = 0;
248 ApubComment::verify(&json, &url, &context, &mut request_counter)
251 let comment = ApubComment::from_apub(json.clone(), &context, &mut request_counter)
255 assert_eq!(comment.ap_id, url.into());
256 assert_eq!(comment.content.len(), 14);
257 assert!(!comment.local);
258 assert_eq!(request_counter, 0);
260 let comment_id = comment.id;
261 let to_apub = comment.into_apub(&context).await.unwrap();
262 assert_json_include!(actual: json, expected: to_apub);
264 Comment::delete(&*context.pool().get().unwrap(), comment_id).unwrap();
265 cleanup(data, &context);
270 async fn test_parse_pleroma_comment() {
271 let context = init_context();
272 let url = Url::parse("https://enterprise.lemmy.ml/comment/38741").unwrap();
273 let data = prepare_comment_test(&url, &context).await;
276 Url::parse("https://queer.hacktivis.me/objects/8d4973f4-53de-49cd-8c27-df160e16a9c2")
278 let person_json = file_to_json_object("assets/pleroma/objects/person.json").unwrap();
279 ApubPerson::verify(&person_json, &pleroma_url, &context, &mut 0)
282 ApubPerson::from_apub(person_json, &context, &mut 0)
285 let json = file_to_json_object("assets/pleroma/objects/note.json").unwrap();
286 let mut request_counter = 0;
287 ApubComment::verify(&json, &pleroma_url, &context, &mut request_counter)
290 let comment = ApubComment::from_apub(json, &context, &mut request_counter)
294 assert_eq!(comment.ap_id, pleroma_url.into());
295 assert_eq!(comment.content.len(), 64);
296 assert!(!comment.local);
297 assert_eq!(request_counter, 0);
299 Comment::delete(&*context.pool().get().unwrap(), comment.id).unwrap();
300 cleanup(data, &context);
305 async fn test_html_to_markdown_sanitize() {
306 let parsed = parse_html("<script></script><b>hello</b>");
307 assert_eq!(parsed, "**hello**");