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_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_id) = 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 parent_id: parent_comment_id,
182 content: content_slurs_removed,
185 published: note.published.map(|u| u.naive_local()),
186 updated: note.updated.map(|u| u.naive_local()),
188 ap_id: Some(note.id.into()),
191 let comment = blocking(context.pool(), move |conn| Comment::upsert(conn, &form)).await??;
197 pub(crate) mod tests {
201 community::{tests::parse_lemmy_community, ApubCommunity},
203 person::{tests::parse_lemmy_person, ApubPerson},
207 protocol::tests::file_to_json_object,
209 use assert_json_diff::assert_json_include;
210 use html2md::parse_html;
211 use lemmy_db_schema::source::site::Site;
212 use serial_test::serial;
214 async fn prepare_comment_test(
216 context: &LemmyContext,
217 ) -> (ApubPerson, ApubCommunity, ApubPost, ApubSite) {
218 let (person, site) = parse_lemmy_person(context).await;
219 let community = parse_lemmy_community(context).await;
220 let post_json = file_to_json_object("assets/lemmy/objects/page.json").unwrap();
221 ApubPost::verify(&post_json, url, context, &mut 0)
224 let post = ApubPost::from_apub(post_json, context, &mut 0)
227 (person, community, post, site)
230 fn cleanup(data: (ApubPerson, ApubCommunity, ApubPost, ApubSite), context: &LemmyContext) {
231 Post::delete(&*context.pool().get().unwrap(), data.2.id).unwrap();
232 Community::delete(&*context.pool().get().unwrap(), data.1.id).unwrap();
233 Person::delete(&*context.pool().get().unwrap(), data.0.id).unwrap();
234 Site::delete(&*context.pool().get().unwrap(), data.3.id).unwrap();
239 pub(crate) async fn test_parse_lemmy_comment() {
240 let context = init_context();
241 let url = Url::parse("https://enterprise.lemmy.ml/comment/38741").unwrap();
242 let data = prepare_comment_test(&url, &context).await;
244 let json: Note = file_to_json_object("assets/lemmy/objects/note.json").unwrap();
245 let mut request_counter = 0;
246 ApubComment::verify(&json, &url, &context, &mut request_counter)
249 let comment = ApubComment::from_apub(json.clone(), &context, &mut request_counter)
253 assert_eq!(comment.ap_id, url.into());
254 assert_eq!(comment.content.len(), 14);
255 assert!(!comment.local);
256 assert_eq!(request_counter, 0);
258 let comment_id = comment.id;
259 let to_apub = comment.into_apub(&context).await.unwrap();
260 assert_json_include!(actual: json, expected: to_apub);
262 Comment::delete(&*context.pool().get().unwrap(), comment_id).unwrap();
263 cleanup(data, &context);
268 async fn test_parse_pleroma_comment() {
269 let context = init_context();
270 let url = Url::parse("https://enterprise.lemmy.ml/comment/38741").unwrap();
271 let data = prepare_comment_test(&url, &context).await;
274 Url::parse("https://queer.hacktivis.me/objects/8d4973f4-53de-49cd-8c27-df160e16a9c2")
276 let person_json = file_to_json_object("assets/pleroma/objects/person.json").unwrap();
277 ApubPerson::verify(&person_json, &pleroma_url, &context, &mut 0)
280 ApubPerson::from_apub(person_json, &context, &mut 0)
283 let json = file_to_json_object("assets/pleroma/objects/note.json").unwrap();
284 let mut request_counter = 0;
285 ApubComment::verify(&json, &pleroma_url, &context, &mut request_counter)
288 let comment = ApubComment::from_apub(json, &context, &mut request_counter)
292 assert_eq!(comment.ap_id, pleroma_url.into());
293 assert_eq!(comment.content.len(), 64);
294 assert!(!comment.local);
295 assert_eq!(request_counter, 0);
297 Comment::delete(&*context.pool().get().unwrap(), comment.id).unwrap();
298 cleanup(data, &context);
303 async fn test_html_to_markdown_sanitize() {
304 let parsed = parse_html("<script></script><b>hello</b>");
305 assert_eq!(parsed, "**hello**");