2 extensions::context::lemmy_context,
3 fetcher::objects::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post},
6 check_object_for_community_or_site_ban,
9 get_or_fetch_and_upsert_person,
10 get_source_markdown_value,
11 set_content_and_source,
18 use activitystreams::{
19 object::{kind::NoteType, ApObject, Note, Tombstone},
23 use anyhow::{anyhow, Context};
24 use lemmy_api_structs::blocking;
25 use lemmy_db_queries::{Crud, DbPool};
26 use lemmy_db_schema::{
28 comment::{Comment, CommentForm},
36 utils::{convert_datetime, remove_slurs},
39 use lemmy_websocket::LemmyContext;
42 #[async_trait::async_trait(?Send)]
43 impl ToApub for Comment {
44 type ApubType = NoteExt;
46 async fn to_apub(&self, pool: &DbPool) -> Result<NoteExt, LemmyError> {
47 let mut comment = ApObject::new(Note::new());
49 let creator_id = self.creator_id;
50 let creator = blocking(pool, move |conn| Person::read(conn, creator_id)).await??;
52 let post_id = self.post_id;
53 let post = blocking(pool, move |conn| Post::read(conn, post_id)).await??;
55 // Add a vector containing some important info to the "in_reply_to" field
56 // [post_ap_id, Option(parent_comment_ap_id)]
57 let mut in_reply_to_vec = vec![post.ap_id.into_inner()];
59 if let Some(parent_id) = self.parent_id {
60 let parent_comment = blocking(pool, move |conn| Comment::read(conn, parent_id)).await??;
62 in_reply_to_vec.push(parent_comment.ap_id.into_inner());
66 // Not needed when the Post is embedded in a collection (like for community outbox)
67 .set_many_contexts(lemmy_context()?)
68 .set_id(self.ap_id.to_owned().into_inner())
69 .set_published(convert_datetime(self.published))
71 .set_many_in_reply_tos(in_reply_to_vec)
72 .set_attributed_to(creator.actor_id.into_inner());
74 set_content_and_source(&mut comment, &self.content)?;
76 if let Some(u) = self.updated {
77 comment.set_updated(convert_datetime(u));
83 fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
86 self.ap_id.to_owned().into(),
93 #[async_trait::async_trait(?Send)]
94 impl FromApub for Comment {
95 type ApubType = NoteExt;
97 /// Converts a `Note` to `Comment`.
99 /// If the parent community, post and comment(s) are not known locally, these are also fetched.
102 context: &LemmyContext,
103 expected_domain: Url,
104 request_counter: &mut i32,
105 mod_action_allowed: bool,
106 ) -> Result<Comment, LemmyError> {
107 let comment: Comment = get_object_from_apub(
116 let post_id = comment.post_id;
117 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
118 check_object_for_community_or_site_ban(note, post.community_id, context, request_counter)
121 // This is not very efficient because a comment gets inserted just to be deleted right
122 // afterwards, but it seems to be the easiest way to implement it.
123 blocking(context.pool(), move |conn| {
124 Comment::delete(conn, comment.id)
127 Err(anyhow!("Post is locked").into())
134 #[async_trait::async_trait(?Send)]
135 impl FromApubToForm<NoteExt> for CommentForm {
138 context: &LemmyContext,
139 expected_domain: Url,
140 request_counter: &mut i32,
141 _mod_action_allowed: bool,
142 ) -> Result<CommentForm, LemmyError> {
143 let creator_actor_id = ¬e
145 .context(location_info!())?
146 .as_single_xsd_any_uri()
147 .context(location_info!())?;
150 get_or_fetch_and_upsert_person(creator_actor_id, context, request_counter).await?;
152 let mut in_reply_tos = note
155 .context(location_info!())?
157 .context(location_info!())?
159 .map(|i| i.as_xsd_any_uri().context(""));
160 let post_ap_id = in_reply_tos.next().context(location_info!())??;
162 // This post, or the parent comment might not yet exist on this server yet, fetch them.
163 let post = get_or_fetch_and_insert_post(&post_ap_id, context, request_counter).await?;
165 // The 2nd item, if it exists, is the parent comment apub_id
166 // For deeply nested comments, FromApub automatically gets called recursively
167 let parent_id: Option<CommentId> = match in_reply_tos.next() {
168 Some(parent_comment_uri) => {
169 let parent_comment_ap_id = &parent_comment_uri?;
171 get_or_fetch_and_insert_comment(&parent_comment_ap_id, context, request_counter).await?;
173 Some(parent_comment.id)
178 let content = get_source_markdown_value(note)?.context(location_info!())?;
179 let content_slurs_removed = remove_slurs(&content);
182 creator_id: creator.id,
185 content: content_slurs_removed,
188 published: note.published().map(|u| u.to_owned().naive_local()),
189 updated: note.updated().map(|u| u.to_owned().naive_local()),
191 ap_id: Some(check_object_domain(note, expected_domain)?),