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_user,
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::source::{
27 comment::{Comment, CommentForm},
33 utils::{convert_datetime, remove_slurs},
36 use lemmy_websocket::LemmyContext;
39 #[async_trait::async_trait(?Send)]
40 impl ToApub for Comment {
41 type ApubType = NoteExt;
43 async fn to_apub(&self, pool: &DbPool) -> Result<NoteExt, LemmyError> {
44 let mut comment = ApObject::new(Note::new());
46 let creator_id = self.creator_id;
47 let creator = blocking(pool, move |conn| User_::read(conn, creator_id)).await??;
49 let post_id = self.post_id;
50 let post = blocking(pool, move |conn| Post::read(conn, post_id)).await??;
52 // Add a vector containing some important info to the "in_reply_to" field
53 // [post_ap_id, Option(parent_comment_ap_id)]
54 let mut in_reply_to_vec = vec![post.ap_id.into_inner()];
56 if let Some(parent_id) = self.parent_id {
57 let parent_comment = blocking(pool, move |conn| Comment::read(conn, parent_id)).await??;
59 in_reply_to_vec.push(parent_comment.ap_id.into_inner());
63 // Not needed when the Post is embedded in a collection (like for community outbox)
64 .set_many_contexts(lemmy_context()?)
65 .set_id(self.ap_id.to_owned().into_inner())
66 .set_published(convert_datetime(self.published))
68 .set_many_in_reply_tos(in_reply_to_vec)
69 .set_attributed_to(creator.actor_id.into_inner());
71 set_content_and_source(&mut comment, &self.content)?;
73 if let Some(u) = self.updated {
74 comment.set_updated(convert_datetime(u));
80 fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
83 self.ap_id.to_owned().into(),
90 #[async_trait::async_trait(?Send)]
91 impl FromApub for Comment {
92 type ApubType = NoteExt;
94 /// Converts a `Note` to `Comment`.
96 /// If the parent community, post and comment(s) are not known locally, these are also fetched.
99 context: &LemmyContext,
100 expected_domain: Url,
101 request_counter: &mut i32,
103 ) -> Result<Comment, LemmyError> {
104 let comment: Comment = get_object_from_apub(
113 let post_id = comment.post_id;
114 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
115 check_object_for_community_or_site_ban(note, post.community_id, context, request_counter)
118 // This is not very efficient because a comment gets inserted just to be deleted right
119 // afterwards, but it seems to be the easiest way to implement it.
120 blocking(context.pool(), move |conn| {
121 Comment::delete(conn, comment.id)
124 Err(anyhow!("Post is locked").into())
131 #[async_trait::async_trait(?Send)]
132 impl FromApubToForm<NoteExt> for CommentForm {
135 context: &LemmyContext,
136 expected_domain: Url,
137 request_counter: &mut i32,
138 _is_mod_action: bool,
139 ) -> Result<CommentForm, LemmyError> {
140 let creator_actor_id = ¬e
142 .context(location_info!())?
143 .as_single_xsd_any_uri()
144 .context(location_info!())?;
146 let creator = get_or_fetch_and_upsert_user(creator_actor_id, context, request_counter).await?;
148 let mut in_reply_tos = note
151 .context(location_info!())?
153 .context(location_info!())?
155 .map(|i| i.as_xsd_any_uri().context(""));
156 let post_ap_id = in_reply_tos.next().context(location_info!())??;
158 // This post, or the parent comment might not yet exist on this server yet, fetch them.
159 let post = get_or_fetch_and_insert_post(&post_ap_id, context, request_counter).await?;
161 // The 2nd item, if it exists, is the parent comment apub_id
162 // For deeply nested comments, FromApub automatically gets called recursively
163 let parent_id: Option<i32> = match in_reply_tos.next() {
164 Some(parent_comment_uri) => {
165 let parent_comment_ap_id = &parent_comment_uri?;
167 get_or_fetch_and_insert_comment(&parent_comment_ap_id, context, request_counter).await?;
169 Some(parent_comment.id)
174 let content = get_source_markdown_value(note)?.context(location_info!())?;
175 let content_slurs_removed = remove_slurs(&content);
178 creator_id: creator.id,
181 content: content_slurs_removed,
184 published: note.published().map(|u| u.to_owned().naive_local()),
185 updated: note.updated().map(|u| u.to_owned().naive_local()),
187 ap_id: Some(check_object_domain(note, expected_domain)?),