2 activities::verify_person_in_community,
3 extensions::context::lemmy_context,
5 get_or_fetch_and_insert_comment,
6 get_or_fetch_and_insert_post,
7 get_or_fetch_and_insert_post_or_comment,
9 migrations::CommentInReplyToMigration,
10 objects::{create_tombstone, get_or_fetch_and_upsert_person, FromApub, Source, ToApub},
14 use activitystreams::{
16 object::{kind::NoteType, Tombstone},
17 primitives::OneOrMany,
20 use anyhow::{anyhow, Context};
21 use chrono::{DateTime, FixedOffset};
22 use lemmy_api_common::blocking;
24 values::{MediaTypeHtml, MediaTypeMarkdown, PublicUrl},
27 use lemmy_db_queries::{ApubObject, Crud, DbPool};
28 use lemmy_db_schema::{
30 comment::{Comment, CommentForm},
39 utils::{convert_datetime, remove_slurs},
42 use lemmy_websocket::LemmyContext;
43 use serde::{Deserialize, Serialize};
47 #[derive(Clone, Debug, Deserialize, Serialize)]
48 #[serde(rename_all = "camelCase")]
50 #[serde(rename = "@context")]
51 context: OneOrMany<AnyBase>,
54 pub(crate) attributed_to: Url,
55 /// Indicates that the object is publicly readable. Unlike [`Post.to`], this one doesn't contain
56 /// the community ID, as it would be incompatible with Pleroma (and we can get the community from
57 /// the post in [`in_reply_to`]).
60 media_type: MediaTypeHtml,
62 in_reply_to: CommentInReplyToMigration,
63 published: DateTime<FixedOffset>,
64 updated: Option<DateTime<FixedOffset>>,
72 context: &LemmyContext,
73 request_counter: &mut i32,
74 ) -> Result<(Post, Option<CommentId>), LemmyError> {
75 match &self.in_reply_to {
76 CommentInReplyToMigration::Old(in_reply_to) => {
77 // This post, or the parent comment might not yet exist on this server yet, fetch them.
78 let post_id = in_reply_to.get(0).context(location_info!())?;
79 let post = Box::pin(get_or_fetch_and_insert_post(
86 // The 2nd item, if it exists, is the parent comment apub_id
87 // Nested comments will automatically get fetched recursively
88 let parent_id: Option<CommentId> = match in_reply_to.get(1) {
89 Some(parent_comment_uri) => {
90 let parent_comment = Box::pin(get_or_fetch_and_insert_comment(
97 Some(parent_comment.id)
102 Ok((post, parent_id))
104 CommentInReplyToMigration::New(in_reply_to) => {
105 let parent = Box::pin(
106 get_or_fetch_and_insert_post_or_comment(in_reply_to, context, request_counter).await?,
108 match parent.deref() {
109 PostOrComment::Post(p) => {
110 // Workaround because I cant figure ut how to get the post out of the box (and we dont
111 // want to stackoverflow in a deep comment hierarchy).
113 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
116 PostOrComment::Comment(c) => {
117 let post_id = c.post_id;
118 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
119 Ok((post, Some(c.id)))
126 pub(crate) async fn verify(
128 context: &LemmyContext,
129 request_counter: &mut i32,
130 ) -> Result<(), LemmyError> {
131 let (post, _parent_comment_id) = self.get_parents(context, request_counter).await?;
132 let community_id = post.community_id;
133 let community = blocking(context.pool(), move |conn| {
134 Community::read(conn, community_id)
139 return Err(anyhow!("Post is locked").into());
141 verify_domains_match(&self.attributed_to, &self.id)?;
142 verify_person_in_community(
144 &community.actor_id(),
153 #[async_trait::async_trait(?Send)]
154 impl ToApub for Comment {
155 type ApubType = Note;
157 async fn to_apub(&self, pool: &DbPool) -> Result<Note, LemmyError> {
158 let creator_id = self.creator_id;
159 let creator = blocking(pool, move |conn| Person::read(conn, creator_id)).await??;
161 let post_id = self.post_id;
162 let post = blocking(pool, move |conn| Post::read(conn, post_id)).await??;
164 // Add a vector containing some important info to the "in_reply_to" field
165 // [post_ap_id, Option(parent_comment_ap_id)]
166 let mut in_reply_to_vec = vec![post.ap_id.into_inner()];
168 if let Some(parent_id) = self.parent_id {
169 let parent_comment = blocking(pool, move |conn| Comment::read(conn, parent_id)).await??;
171 in_reply_to_vec.push(parent_comment.ap_id.into_inner());
175 context: lemmy_context(),
176 r#type: NoteType::Note,
177 id: self.ap_id.to_owned().into_inner(),
178 attributed_to: creator.actor_id.into_inner(),
179 to: PublicUrl::Public,
180 content: self.content.clone(),
181 media_type: MediaTypeHtml::Html,
183 content: self.content.clone(),
184 media_type: MediaTypeMarkdown::Markdown,
186 in_reply_to: CommentInReplyToMigration::Old(in_reply_to_vec),
187 published: convert_datetime(self.published),
188 updated: self.updated.map(convert_datetime),
189 unparsed: Default::default(),
195 fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
198 self.ap_id.to_owned().into(),
205 #[async_trait::async_trait(?Send)]
206 impl FromApub for Comment {
207 type ApubType = Note;
209 /// Converts a `Note` to `Comment`.
211 /// If the parent community, post and comment(s) are not known locally, these are also fetched.
214 context: &LemmyContext,
215 _expected_domain: Url,
216 request_counter: &mut i32,
217 _mod_action_allowed: bool,
218 ) -> Result<Comment, LemmyError> {
220 get_or_fetch_and_upsert_person(¬e.attributed_to, context, request_counter).await?;
221 let (post, parent_comment_id) = note.get_parents(context, request_counter).await?;
223 let content = ¬e.source.content;
224 let content_slurs_removed = remove_slurs(content);
226 let form = CommentForm {
227 creator_id: creator.id,
229 parent_id: parent_comment_id,
230 content: content_slurs_removed,
233 published: Some(note.published.naive_local()),
234 updated: note.updated.map(|u| u.to_owned().naive_local()),
236 ap_id: Some(note.id.clone().into()),
239 Ok(blocking(context.pool(), move |conn| Comment::upsert(conn, &form)).await??)