2 activities::{verify_is_public, verify_person_in_community},
3 check_apub_id_valid_with_strictness,
6 mentions::collect_non_local_mentions,
7 objects::{read_from_string_or_source, verify_is_remote_object},
9 objects::{note::Note, LanguageTag},
14 use activitypub_federation::{
15 core::object_id::ObjectId,
16 deser::values::MediaTypeMarkdownOrHtml,
18 utils::verify_domains_match,
20 use activitystreams_kinds::{object::NoteType, public};
21 use chrono::NaiveDateTime;
22 use lemmy_api_common::utils::{blocking, local_site_opt_to_slur_regex};
23 use lemmy_db_schema::{
25 comment::{Comment, CommentInsertForm, CommentUpdateForm},
27 local_site::LocalSite,
35 utils::{convert_datetime, markdown_to_html, remove_slurs},
37 use lemmy_websocket::LemmyContext;
41 #[derive(Clone, Debug)]
42 pub struct ApubComment(Comment);
44 impl Deref for ApubComment {
45 type Target = Comment;
46 fn deref(&self) -> &Self::Target {
51 impl From<Comment> for ApubComment {
52 fn from(c: Comment) -> Self {
57 #[async_trait::async_trait(?Send)]
58 impl ApubObject for ApubComment {
59 type DataType = LemmyContext;
61 type DbType = Comment;
62 type Error = LemmyError;
64 fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
68 #[tracing::instrument(skip_all)]
69 async fn read_from_apub_id(
71 context: &LemmyContext,
72 ) -> Result<Option<Self>, LemmyError> {
74 blocking(context.pool(), move |conn| {
75 Comment::read_from_apub_id(conn, object_id)
82 #[tracing::instrument(skip_all)]
83 async fn delete(self, context: &LemmyContext) -> Result<(), LemmyError> {
85 blocking(context.pool(), move |conn| {
86 let form = CommentUpdateForm::builder().deleted(Some(true)).build();
87 Comment::update(conn, self.id, &form)
94 #[tracing::instrument(skip_all)]
95 async fn into_apub(self, context: &LemmyContext) -> Result<Note, LemmyError> {
96 let creator_id = self.creator_id;
97 let creator = blocking(context.pool(), move |conn| Person::read(conn, creator_id)).await??;
99 let post_id = self.post_id;
100 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
101 let community_id = post.community_id;
102 let community = blocking(context.pool(), move |conn| {
103 Community::read(conn, community_id)
107 let in_reply_to = if let Some(comment_id) = self.parent_comment_id() {
109 blocking(context.pool(), move |conn| Comment::read(conn, comment_id)).await??;
110 ObjectId::<PostOrComment>::new(parent_comment.ap_id)
112 ObjectId::<PostOrComment>::new(post.ap_id)
114 let language = LanguageTag::new_single(self.language_id, context.pool()).await?;
116 collect_non_local_mentions(&self, ObjectId::new(community.actor_id), context, &mut 0).await?;
119 r#type: NoteType::Note,
120 id: ObjectId::new(self.ap_id.clone()),
121 attributed_to: ObjectId::new(creator.actor_id),
124 content: markdown_to_html(&self.content),
125 media_type: Some(MediaTypeMarkdownOrHtml::Html),
126 source: Some(Source::new(self.content.clone())),
128 published: Some(convert_datetime(self.published)),
129 updated: self.updated.map(convert_datetime),
131 distinguished: Some(self.distinguished),
138 #[tracing::instrument(skip_all)]
141 expected_domain: &Url,
142 context: &LemmyContext,
143 request_counter: &mut i32,
144 ) -> Result<(), LemmyError> {
145 verify_domains_match(note.id.inner(), expected_domain)?;
146 verify_domains_match(note.attributed_to.inner(), note.id.inner())?;
147 verify_is_public(¬e.to, ¬e.cc)?;
148 let (post, _) = note.get_parents(context, request_counter).await?;
149 let community_id = post.community_id;
150 let community = blocking(context.pool(), move |conn| {
151 Community::read(conn, community_id)
154 let local_site_data = blocking(context.pool(), fetch_local_site_data).await??;
156 check_apub_id_valid_with_strictness(
162 verify_is_remote_object(note.id.inner(), context.settings())?;
163 verify_person_in_community(
171 return Err(LemmyError::from_message("Post is locked"));
176 /// Converts a `Note` to `Comment`.
178 /// If the parent community, post and comment(s) are not known locally, these are also fetched.
179 #[tracing::instrument(skip_all)]
182 context: &LemmyContext,
183 request_counter: &mut i32,
184 ) -> Result<ApubComment, LemmyError> {
187 .dereference(context, local_instance(context), request_counter)
189 let (post, parent_comment) = note.get_parents(context, request_counter).await?;
191 let content = read_from_string_or_source(¬e.content, ¬e.media_type, ¬e.source);
193 let local_site = blocking(context.pool(), LocalSite::read).await?.ok();
194 let slur_regex = &local_site_opt_to_slur_regex(&local_site);
195 let content_slurs_removed = remove_slurs(&content, slur_regex);
196 let language_id = LanguageTag::to_language_id_single(note.language, context.pool()).await?;
198 let form = CommentInsertForm {
199 creator_id: creator.id,
201 content: content_slurs_removed,
203 published: note.published.map(|u| u.naive_local()),
204 updated: note.updated.map(|u| u.naive_local()),
205 deleted: Some(false),
206 ap_id: Some(note.id.into()),
207 distinguished: note.distinguished,
211 let parent_comment_path = parent_comment.map(|t| t.0.path);
212 let comment = blocking(context.pool(), move |conn| {
213 Comment::create(conn, &form, parent_comment_path.as_ref())
221 pub(crate) mod tests {
225 community::{tests::parse_lemmy_community, ApubCommunity},
227 person::{tests::parse_lemmy_person, ApubPerson},
231 protocol::tests::file_to_json_object,
233 use assert_json_diff::assert_json_include;
234 use html2md::parse_html;
235 use lemmy_db_schema::source::site::Site;
236 use serial_test::serial;
238 async fn prepare_comment_test(
240 context: &LemmyContext,
241 ) -> (ApubPerson, ApubCommunity, ApubPost, ApubSite) {
242 let (person, site) = parse_lemmy_person(context).await;
243 let community = parse_lemmy_community(context).await;
244 let post_json = file_to_json_object("assets/lemmy/objects/page.json").unwrap();
245 ApubPost::verify(&post_json, url, context, &mut 0)
248 let post = ApubPost::from_apub(post_json, context, &mut 0)
251 (person, community, post, site)
254 fn cleanup(data: (ApubPerson, ApubCommunity, ApubPost, ApubSite), context: &LemmyContext) {
255 let conn = &mut context.pool().get().unwrap();
256 Post::delete(conn, data.2.id).unwrap();
257 Community::delete(conn, data.1.id).unwrap();
258 Person::delete(conn, data.0.id).unwrap();
259 Site::delete(conn, data.3.id).unwrap();
260 LocalSite::delete(conn).unwrap();
265 pub(crate) async fn test_parse_lemmy_comment() {
266 let context = init_context();
267 let conn = &mut context.pool().get().unwrap();
268 let url = Url::parse("https://enterprise.lemmy.ml/comment/38741").unwrap();
269 let data = prepare_comment_test(&url, &context).await;
271 let json: Note = file_to_json_object("assets/lemmy/objects/note.json").unwrap();
272 let mut request_counter = 0;
273 ApubComment::verify(&json, &url, &context, &mut request_counter)
276 let comment = ApubComment::from_apub(json.clone(), &context, &mut request_counter)
280 assert_eq!(comment.ap_id, url.into());
281 assert_eq!(comment.content.len(), 14);
282 assert!(!comment.local);
283 assert_eq!(request_counter, 0);
285 let comment_id = comment.id;
286 let to_apub = comment.into_apub(&context).await.unwrap();
287 assert_json_include!(actual: json, expected: to_apub);
289 Comment::delete(conn, comment_id).unwrap();
290 cleanup(data, &context);
295 async fn test_parse_pleroma_comment() {
296 let context = init_context();
297 let conn = &mut context.pool().get().unwrap();
298 let url = Url::parse("https://enterprise.lemmy.ml/comment/38741").unwrap();
299 let data = prepare_comment_test(&url, &context).await;
302 Url::parse("https://queer.hacktivis.me/objects/8d4973f4-53de-49cd-8c27-df160e16a9c2")
304 let person_json = file_to_json_object("assets/pleroma/objects/person.json").unwrap();
305 ApubPerson::verify(&person_json, &pleroma_url, &context, &mut 0)
308 ApubPerson::from_apub(person_json, &context, &mut 0)
311 let json = file_to_json_object("assets/pleroma/objects/note.json").unwrap();
312 let mut request_counter = 0;
313 ApubComment::verify(&json, &pleroma_url, &context, &mut request_counter)
316 let comment = ApubComment::from_apub(json, &context, &mut request_counter)
320 assert_eq!(comment.ap_id, pleroma_url.into());
321 assert_eq!(comment.content.len(), 64);
322 assert!(!comment.local);
323 assert_eq!(request_counter, 0);
325 Comment::delete(conn, comment.id).unwrap();
326 cleanup(data, &context);
331 async fn test_html_to_markdown_sanitize() {
332 let parsed = parse_html("<script></script><b>hello</b>");
333 assert_eq!(parsed, "**hello**");