2 activities::{verify_is_public, verify_person_in_community},
3 check_is_apub_id_valid,
6 note::{Note, SourceCompat},
13 use activitystreams::{object::kind::NoteType, public};
15 use chrono::NaiveDateTime;
16 use html2md::parse_html;
17 use lemmy_api_common::blocking;
21 values::{MediaTypeHtml, MediaTypeMarkdown},
22 verify::verify_domains_match,
24 use lemmy_db_schema::{
26 comment::{Comment, CommentForm},
34 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 TombstoneType = Tombstone;
63 fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
67 async fn read_from_apub_id(
69 context: &LemmyContext,
70 ) -> Result<Option<Self>, LemmyError> {
72 blocking(context.pool(), move |conn| {
73 Comment::read_from_apub_id(conn, object_id)
80 async fn delete(self, context: &LemmyContext) -> Result<(), LemmyError> {
82 blocking(context.pool(), move |conn| {
83 Comment::update_deleted(conn, self.id, true)
90 async fn into_apub(self, context: &LemmyContext) -> Result<Note, LemmyError> {
91 let creator_id = self.creator_id;
92 let creator = blocking(context.pool(), move |conn| Person::read(conn, creator_id)).await??;
94 let post_id = self.post_id;
95 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
97 let in_reply_to = if let Some(comment_id) = self.parent_id {
99 blocking(context.pool(), move |conn| Comment::read(conn, comment_id)).await??;
100 ObjectId::<PostOrComment>::new(parent_comment.ap_id)
102 ObjectId::<PostOrComment>::new(post.ap_id)
106 r#type: NoteType::Note,
107 id: ObjectId::new(self.ap_id.clone()),
108 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),
135 expected_domain: &Url,
136 context: &LemmyContext,
137 request_counter: &mut i32,
138 ) -> Result<(), LemmyError> {
139 verify_domains_match(note.id.inner(), expected_domain)?;
140 verify_domains_match(note.attributed_to.inner(), note.id.inner())?;
141 verify_is_public(¬e.to, ¬e.cc)?;
142 let (post, _) = note.get_parents(context, request_counter).await?;
143 let community_id = post.community_id;
144 let community = blocking(context.pool(), move |conn| {
145 Community::read(conn, community_id)
148 check_is_apub_id_valid(note.id.inner(), community.local, &context.settings())?;
149 verify_person_in_community(
157 return Err(anyhow!("Post is locked").into());
162 /// Converts a `Note` to `Comment`.
164 /// If the parent community, post and comment(s) are not known locally, these are also fetched.
167 context: &LemmyContext,
168 request_counter: &mut i32,
169 ) -> Result<ApubComment, LemmyError> {
172 .dereference(context, request_counter)
174 let (post, parent_comment_id) = note.get_parents(context, request_counter).await?;
176 let content = if let SourceCompat::Lemmy(source) = ¬e.source {
177 source.content.clone()
179 parse_html(¬e.content)
181 let content_slurs_removed = remove_slurs(&content, &context.settings().slur_regex());
183 let form = CommentForm {
184 creator_id: creator.id,
186 parent_id: parent_comment_id,
187 content: content_slurs_removed,
190 published: note.published.map(|u| u.naive_local()),
191 updated: note.updated.map(|u| u.naive_local()),
193 ap_id: Some(note.id.into()),
196 let comment = blocking(context.pool(), move |conn| Comment::upsert(conn, &form)).await??;
202 pub(crate) mod tests {
204 use crate::objects::{
205 community::{tests::parse_lemmy_community, ApubCommunity},
206 person::{tests::parse_lemmy_person, ApubPerson},
208 tests::{file_to_json_object, init_context},
210 use assert_json_diff::assert_json_include;
211 use serial_test::serial;
213 async fn prepare_comment_test(
215 context: &LemmyContext,
216 ) -> (ApubPerson, ApubCommunity, ApubPost) {
217 let person = parse_lemmy_person(context).await;
218 let community = parse_lemmy_community(context).await;
219 let post_json = file_to_json_object("assets/lemmy/objects/page.json");
220 ApubPost::verify(&post_json, url, context, &mut 0)
223 let post = ApubPost::from_apub(post_json, context, &mut 0)
226 (person, community, post)
229 fn cleanup(data: (ApubPerson, ApubCommunity, ApubPost), context: &LemmyContext) {
230 Post::delete(&*context.pool().get().unwrap(), data.2.id).unwrap();
231 Community::delete(&*context.pool().get().unwrap(), data.1.id).unwrap();
232 Person::delete(&*context.pool().get().unwrap(), data.0.id).unwrap();
237 pub(crate) async fn test_parse_lemmy_comment() {
238 let context = init_context();
239 let url = Url::parse("https://enterprise.lemmy.ml/comment/38741").unwrap();
240 let data = prepare_comment_test(&url, &context).await;
242 let json: Note = file_to_json_object("assets/lemmy/objects/note.json");
243 let mut request_counter = 0;
244 ApubComment::verify(&json, &url, &context, &mut request_counter)
247 let comment = ApubComment::from_apub(json.clone(), &context, &mut request_counter)
251 assert_eq!(comment.ap_id, url.into());
252 assert_eq!(comment.content.len(), 14);
253 assert!(!comment.local);
254 assert_eq!(request_counter, 0);
256 let comment_id = comment.id;
257 let to_apub = comment.into_apub(&context).await.unwrap();
258 assert_json_include!(actual: json, expected: to_apub);
260 Comment::delete(&*context.pool().get().unwrap(), comment_id).unwrap();
261 cleanup(data, &context);
266 async fn test_parse_pleroma_comment() {
267 let context = init_context();
268 let url = Url::parse("https://enterprise.lemmy.ml/comment/38741").unwrap();
269 let data = prepare_comment_test(&url, &context).await;
272 Url::parse("https://queer.hacktivis.me/objects/8d4973f4-53de-49cd-8c27-df160e16a9c2")
274 let person_json = file_to_json_object("assets/pleroma/objects/person.json");
275 ApubPerson::verify(&person_json, &pleroma_url, &context, &mut 0)
278 ApubPerson::from_apub(person_json, &context, &mut 0)
281 let json = file_to_json_object("assets/pleroma/objects/note.json");
282 let mut request_counter = 0;
283 ApubComment::verify(&json, &pleroma_url, &context, &mut request_counter)
286 let comment = ApubComment::from_apub(json, &context, &mut request_counter)
290 assert_eq!(comment.ap_id, pleroma_url.into());
291 assert_eq!(comment.content.len(), 64);
292 assert!(!comment.local);
293 assert_eq!(request_counter, 0);
295 Comment::delete(&*context.pool().get().unwrap(), comment.id).unwrap();
296 cleanup(data, &context);
301 async fn test_html_to_markdown_sanitize() {
302 let parsed = parse_html("<script></script><b>hello</b>");
303 assert_eq!(parsed, "**hello**");