]> Untitled Git - lemmy.git/blobdiff - crates/apub/src/objects/comment.rs
Rewrite activitypub following, person, community, pm (#1692)
[lemmy.git] / crates / apub / src / objects / comment.rs
index d402613a002cd1295862a09ad82e53ca01cfc0eb..1bebb39e99bbd8756ecc7502c9c5c1e08992e2a6 100644 (file)
@@ -41,16 +41,18 @@ use lemmy_utils::{
 };
 use lemmy_websocket::LemmyContext;
 use serde::{Deserialize, Serialize};
+use serde_with::skip_serializing_none;
 use std::ops::Deref;
 use url::Url;
 
+#[skip_serializing_none]
 #[derive(Clone, Debug, Deserialize, Serialize)]
 #[serde(rename_all = "camelCase")]
 pub struct Note {
   #[serde(rename = "@context")]
   context: OneOrMany<AnyBase>,
   r#type: NoteType,
-  pub(crate) id: Url,
+  id: Url,
   pub(crate) attributed_to: Url,
   /// Indicates that the object is publicly readable. Unlike [`Post.to`], this one doesn't contain
   /// the community ID, as it would be incompatible with Pleroma (and we can get the community from
@@ -67,6 +69,14 @@ pub struct Note {
 }
 
 impl Note {
+  pub(crate) fn id_unchecked(&self) -> &Url {
+    &self.id
+  }
+  pub(crate) fn id(&self, expected_domain: &Url) -> Result<&Url, LemmyError> {
+    verify_domains_match(&self.id, expected_domain)?;
+    Ok(&self.id)
+  }
+
   async fn get_parents(
     &self,
     context: &LemmyContext,
@@ -212,10 +222,10 @@ impl FromApub for Comment {
   async fn from_apub(
     note: &Note,
     context: &LemmyContext,
-    _expected_domain: Url,
+    expected_domain: &Url,
     request_counter: &mut i32,
-    _mod_action_allowed: bool,
   ) -> Result<Comment, LemmyError> {
+    let ap_id = Some(note.id(expected_domain)?.clone().into());
     let creator =
       get_or_fetch_and_upsert_person(&note.attributed_to, context, request_counter).await?;
     let (post, parent_comment_id) = note.get_parents(context, request_counter).await?;
@@ -233,7 +243,7 @@ impl FromApub for Comment {
       published: Some(note.published.naive_local()),
       updated: note.updated.map(|u| u.to_owned().naive_local()),
       deleted: None,
-      ap_id: Some(note.id.clone().into()),
+      ap_id,
       local: Some(false),
     };
     Ok(blocking(context.pool(), move |conn| Comment::upsert(conn, &form)).await??)