3 use activitystreams::{object::kind::NoteType, public};
5 use chrono::NaiveDateTime;
6 use html2md::parse_html;
9 use lemmy_api_common::blocking;
12 values::{MediaTypeHtml, MediaTypeMarkdown},
14 use lemmy_db_schema::{
16 comment::{Comment, CommentForm},
24 utils::{convert_datetime, remove_slurs},
27 use lemmy_websocket::LemmyContext;
30 activities::verify_person_in_community,
31 check_is_apub_id_valid,
32 fetcher::object_id::ObjectId,
35 note::{Note, SourceCompat},
42 use lemmy_utils::utils::markdown_to_html;
44 #[derive(Clone, Debug)]
45 pub struct ApubComment(Comment);
47 impl Deref for ApubComment {
48 type Target = Comment;
49 fn deref(&self) -> &Self::Target {
54 impl From<Comment> for ApubComment {
55 fn from(c: Comment) -> Self {
60 #[async_trait::async_trait(?Send)]
61 impl ApubObject for ApubComment {
62 type DataType = LemmyContext;
64 type TombstoneType = Tombstone;
66 fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
70 async fn read_from_apub_id(
72 context: &LemmyContext,
73 ) -> Result<Option<Self>, LemmyError> {
75 blocking(context.pool(), move |conn| {
76 Comment::read_from_apub_id(conn, object_id)
83 async fn delete(self, context: &LemmyContext) -> Result<(), LemmyError> {
84 blocking(context.pool(), move |conn| {
85 Comment::update_deleted(conn, self.id, true)
91 async fn to_apub(&self, context: &LemmyContext) -> Result<Note, LemmyError> {
92 let creator_id = self.creator_id;
93 let creator = blocking(context.pool(), move |conn| Person::read(conn, creator_id)).await??;
95 let post_id = self.post_id;
96 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
98 let in_reply_to = if let Some(comment_id) = self.parent_id {
100 blocking(context.pool(), move |conn| Comment::read(conn, comment_id)).await??;
101 ObjectId::<PostOrComment>::new(parent_comment.ap_id.into_inner())
103 ObjectId::<PostOrComment>::new(post.ap_id.into_inner())
107 r#type: NoteType::Note,
108 id: self.ap_id.to_owned().into_inner(),
109 attributed_to: ObjectId::new(creator.actor_id),
111 content: markdown_to_html(&self.content),
112 media_type: Some(MediaTypeHtml::Html),
113 source: SourceCompat::Lemmy(Source {
114 content: self.content.clone(),
115 media_type: MediaTypeMarkdown::Markdown,
118 published: Some(convert_datetime(self.published)),
119 updated: self.updated.map(convert_datetime),
120 unparsed: Default::default(),
126 fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
129 self.updated.unwrap_or(self.published),
133 /// Converts a `Note` to `Comment`.
135 /// If the parent community, post and comment(s) are not known locally, these are also fetched.
138 context: &LemmyContext,
139 expected_domain: &Url,
140 request_counter: &mut i32,
141 ) -> Result<ApubComment, LemmyError> {
142 let ap_id = Some(note.id(expected_domain)?.clone().into());
145 .dereference(context, request_counter)
147 let (post, parent_comment_id) = note.get_parents(context, request_counter).await?;
148 let community_id = post.community_id;
149 let community = blocking(context.pool(), move |conn| {
150 Community::read(conn, community_id)
153 check_is_apub_id_valid(¬e.id, community.local, &context.settings())?;
154 verify_person_in_community(
162 return Err(anyhow!("Post is locked").into());
165 let content = if let SourceCompat::Lemmy(source) = ¬e.source {
166 source.content.clone()
168 parse_html(¬e.content)
170 let content_slurs_removed = remove_slurs(&content, &context.settings().slur_regex());
172 let form = CommentForm {
173 creator_id: creator.id,
175 parent_id: parent_comment_id,
176 content: content_slurs_removed,
179 published: note.published.map(|u| u.to_owned().naive_local()),
180 updated: note.updated.map(|u| u.to_owned().naive_local()),
185 let comment = blocking(context.pool(), move |conn| Comment::upsert(conn, &form)).await??;
191 pub(crate) mod tests {
193 use crate::objects::{
194 community::{tests::parse_lemmy_community, ApubCommunity},
195 person::{tests::parse_lemmy_person, ApubPerson},
197 tests::{file_to_json_object, init_context},
199 use assert_json_diff::assert_json_include;
200 use serial_test::serial;
202 async fn prepare_comment_test(
204 context: &LemmyContext,
205 ) -> (ApubPerson, ApubCommunity, ApubPost) {
206 let person = parse_lemmy_person(context).await;
207 let community = parse_lemmy_community(context).await;
208 let post_json = file_to_json_object("assets/lemmy/objects/page.json");
209 let post = ApubPost::from_apub(&post_json, context, url, &mut 0)
212 (person, community, post)
215 fn cleanup(data: (ApubPerson, ApubCommunity, ApubPost), context: &LemmyContext) {
216 Post::delete(&*context.pool().get().unwrap(), data.2.id).unwrap();
217 Community::delete(&*context.pool().get().unwrap(), data.1.id).unwrap();
218 Person::delete(&*context.pool().get().unwrap(), data.0.id).unwrap();
223 pub(crate) async fn test_parse_lemmy_comment() {
224 let context = init_context();
225 let url = Url::parse("https://enterprise.lemmy.ml/comment/38741").unwrap();
226 let data = prepare_comment_test(&url, &context).await;
228 let json = file_to_json_object("assets/lemmy/objects/note.json");
229 let mut request_counter = 0;
230 let comment = ApubComment::from_apub(&json, &context, &url, &mut request_counter)
234 assert_eq!(comment.ap_id.clone().into_inner(), url);
235 assert_eq!(comment.content.len(), 14);
236 assert!(!comment.local);
237 assert_eq!(request_counter, 0);
239 let to_apub = comment.to_apub(&context).await.unwrap();
240 assert_json_include!(actual: json, expected: to_apub);
242 Comment::delete(&*context.pool().get().unwrap(), comment.id).unwrap();
243 cleanup(data, &context);
248 async fn test_parse_pleroma_comment() {
249 let context = init_context();
250 let url = Url::parse("https://enterprise.lemmy.ml/comment/38741").unwrap();
251 let data = prepare_comment_test(&url, &context).await;
254 Url::parse("https://queer.hacktivis.me/objects/8d4973f4-53de-49cd-8c27-df160e16a9c2")
256 let person_json = file_to_json_object("assets/pleroma/objects/person.json");
257 ApubPerson::from_apub(&person_json, &context, &pleroma_url, &mut 0)
260 let json = file_to_json_object("assets/pleroma/objects/note.json");
261 let mut request_counter = 0;
262 let comment = ApubComment::from_apub(&json, &context, &pleroma_url, &mut request_counter)
266 assert_eq!(comment.ap_id.clone().into_inner(), pleroma_url);
267 assert_eq!(comment.content.len(), 64);
268 assert!(!comment.local);
269 assert_eq!(request_counter, 0);
271 Comment::delete(&*context.pool().get().unwrap(), comment.id).unwrap();
272 cleanup(data, &context);
277 async fn test_html_to_markdown_sanitize() {
278 let parsed = parse_html("<script></script><b>hello</b>");
279 assert_eq!(parsed, "**hello**");