2 activities::verify_person_in_community,
3 extensions::context::lemmy_context,
4 fetcher::objects::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post},
5 objects::{create_tombstone, get_or_fetch_and_upsert_person, FromApub, Source, ToApub},
10 object::{kind::NoteType, Tombstone},
11 primitives::OneOrMany,
14 use anyhow::{anyhow, Context};
15 use chrono::{DateTime, FixedOffset};
16 use lemmy_api_common::blocking;
18 values::{MediaTypeHtml, MediaTypeMarkdown, PublicUrl},
21 use lemmy_db_queries::{ApubObject, Crud, DbPool};
22 use lemmy_db_schema::{
24 comment::{Comment, CommentForm},
33 utils::{convert_datetime, remove_slurs},
36 use lemmy_websocket::LemmyContext;
37 use serde::{Deserialize, Serialize};
40 #[derive(Clone, Debug, Deserialize, Serialize)]
41 #[serde(rename_all = "camelCase")]
43 #[serde(rename = "@context")]
44 context: OneOrMany<AnyBase>,
47 pub(crate) attributed_to: Url,
48 /// Indicates that the object is publicly readable. Unlike [`Post.to`], this one doesn't contain
49 /// the community ID, as it would be incompatible with Pleroma (and we can get the community from
50 /// the post in [`in_reply_to`]).
53 media_type: MediaTypeHtml,
55 in_reply_to: Vec<Url>,
56 published: DateTime<FixedOffset>,
57 updated: Option<DateTime<FixedOffset>>,
65 context: &LemmyContext,
66 request_counter: &mut i32,
67 ) -> Result<(Post, Option<CommentId>), LemmyError> {
68 // This post, or the parent comment might not yet exist on this server yet, fetch them.
69 let post_id = self.in_reply_to.get(0).context(location_info!())?;
70 let post = Box::pin(get_or_fetch_and_insert_post(
77 // The 2nd item, if it exists, is the parent comment apub_id
78 // Nested comments will automatically get fetched recursively
79 let parent_id: Option<CommentId> = match self.in_reply_to.get(1) {
80 Some(parent_comment_uri) => {
81 let parent_comment = Box::pin(get_or_fetch_and_insert_comment(
88 Some(parent_comment.id)
96 pub(crate) async fn verify(
98 context: &LemmyContext,
99 request_counter: &mut i32,
100 ) -> Result<(), LemmyError> {
101 let (post, _parent_comment_id) = self.get_parents(context, request_counter).await?;
102 let community_id = post.community_id;
103 let community = blocking(context.pool(), move |conn| {
104 Community::read(conn, community_id)
109 return Err(anyhow!("Post is locked").into());
111 verify_domains_match(&self.attributed_to, &self.id)?;
112 verify_person_in_community(
114 &community.actor_id(),
123 #[async_trait::async_trait(?Send)]
124 impl ToApub for Comment {
125 type ApubType = Note;
127 async fn to_apub(&self, pool: &DbPool) -> Result<Note, LemmyError> {
128 let creator_id = self.creator_id;
129 let creator = blocking(pool, move |conn| Person::read(conn, creator_id)).await??;
131 let post_id = self.post_id;
132 let post = blocking(pool, move |conn| Post::read(conn, post_id)).await??;
134 // Add a vector containing some important info to the "in_reply_to" field
135 // [post_ap_id, Option(parent_comment_ap_id)]
136 let mut in_reply_to_vec = vec![post.ap_id.into_inner()];
138 if let Some(parent_id) = self.parent_id {
139 let parent_comment = blocking(pool, move |conn| Comment::read(conn, parent_id)).await??;
141 in_reply_to_vec.push(parent_comment.ap_id.into_inner());
145 context: lemmy_context(),
146 r#type: NoteType::Note,
147 id: self.ap_id.to_owned().into_inner(),
148 attributed_to: creator.actor_id.into_inner(),
149 to: PublicUrl::Public,
150 content: self.content.clone(),
151 media_type: MediaTypeHtml::Html,
153 content: self.content.clone(),
154 media_type: MediaTypeMarkdown::Markdown,
156 in_reply_to: in_reply_to_vec,
157 published: convert_datetime(self.published),
158 updated: self.updated.map(convert_datetime),
159 unparsed: Default::default(),
165 fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
168 self.ap_id.to_owned().into(),
175 #[async_trait::async_trait(?Send)]
176 impl FromApub for Comment {
177 type ApubType = Note;
179 /// Converts a `Note` to `Comment`.
181 /// If the parent community, post and comment(s) are not known locally, these are also fetched.
184 context: &LemmyContext,
185 _expected_domain: Url,
186 request_counter: &mut i32,
187 _mod_action_allowed: bool,
188 ) -> Result<Comment, LemmyError> {
190 get_or_fetch_and_upsert_person(¬e.attributed_to, context, request_counter).await?;
191 let (post, parent_comment_id) = note.get_parents(context, request_counter).await?;
193 let content = ¬e.source.content;
194 let content_slurs_removed = remove_slurs(content);
196 let form = CommentForm {
197 creator_id: creator.id,
199 parent_id: parent_comment_id,
200 content: content_slurs_removed,
203 published: Some(note.published.naive_local()),
204 updated: note.updated.map(|u| u.to_owned().naive_local()),
206 ap_id: Some(note.id.clone().into()),
209 Ok(blocking(context.pool(), move |conn| Comment::upsert(conn, &form)).await??)