]> Untitled Git - lemmy.git/blobdiff - crates/apub/src/protocol/objects/page.rs
Add support for Featured Posts (#2585)
[lemmy.git] / crates / apub / src / protocol / objects / page.rs
index 5ffb4869b5ab8900852c89fc0d25590699e89c72..3aadb20c1a2ba5660e83c03f68c87568a25518e4 100644 (file)
@@ -1,8 +1,9 @@
 use crate::{
+  activities::verify_community_matches,
   fetcher::user_or_community::{PersonOrGroupType, UserOrCommunity},
   local_instance,
   objects::{community::ApubCommunity, person::ApubPerson, post::ApubPost},
-  protocol::{objects::LanguageTag, ImageObject, Source},
+  protocol::{objects::LanguageTag, ImageObject, InCommunity, Source},
 };
 use activitypub_federation::{
   core::object_id::ObjectId,
@@ -13,12 +14,15 @@ use activitypub_federation::{
   },
   traits::{ActivityHandler, ApubObject},
 };
-use activitystreams_kinds::{link::LinkType, object::ImageType};
+use activitystreams_kinds::{
+  link::LinkType,
+  object::{DocumentType, ImageType},
+};
 use chrono::{DateTime, FixedOffset};
 use itertools::Itertools;
+use lemmy_api_common::context::LemmyContext;
 use lemmy_db_schema::newtypes::DbUrl;
 use lemmy_utils::error::LemmyError;
-use lemmy_websocket::LemmyContext;
 use serde::{Deserialize, Serialize};
 use serde_with::skip_serializing_none;
 use url::Url;
@@ -29,6 +33,7 @@ pub enum PageType {
   Article,
   Note,
   Video,
+  Event,
 }
 
 #[skip_serializing_none]
@@ -49,9 +54,6 @@ pub struct Page {
   pub(crate) media_type: Option<MediaTypeMarkdownOrHtml>,
   #[serde(deserialize_with = "deserialize_skip_error", default)]
   pub(crate) source: Option<Source>,
-  /// deprecated, use attachment field
-  #[serde(deserialize_with = "deserialize_skip_error", default)]
-  pub(crate) url: Option<Url>,
   /// most software uses array type for attachment field, so we do the same. nevertheless, we only
   /// use the first item
   #[serde(default)]
@@ -63,6 +65,7 @@ pub struct Page {
   pub(crate) published: Option<DateTime<FixedOffset>>,
   pub(crate) updated: Option<DateTime<FixedOffset>>,
   pub(crate) language: Option<LanguageTag>,
+  pub(crate) audience: Option<ObjectId<ApubCommunity>>,
 }
 
 #[derive(Clone, Debug, Deserialize, Serialize)]
@@ -80,11 +83,33 @@ pub(crate) struct Image {
   pub(crate) url: Url,
 }
 
+#[derive(Clone, Debug, Deserialize, Serialize)]
+#[serde(rename_all = "camelCase")]
+pub(crate) struct Document {
+  #[serde(rename = "type")]
+  pub(crate) kind: DocumentType,
+  pub(crate) url: Url,
+}
+
 #[derive(Clone, Debug, Deserialize, Serialize)]
 #[serde(untagged)]
 pub(crate) enum Attachment {
   Link(Link),
   Image(Image),
+  Document(Document),
+}
+
+impl Attachment {
+  pub(crate) fn url(self) -> Url {
+    match self {
+      // url as sent by Lemmy (new)
+      Attachment::Link(l) => l.href,
+      // image sent by lotide
+      Attachment::Image(i) => i.url,
+      // sent by mobilizon
+      Attachment::Document(d) => d.url,
+    }
+  }
 }
 
 #[derive(Clone, Debug, Deserialize, Serialize)]
@@ -112,18 +137,18 @@ impl Page {
       .dereference_local(context)
       .await;
 
-    let stickied_changed = Page::is_stickied_changed(&old_post, &self.stickied);
+    let featured_changed = Page::is_featured_changed(&old_post, &self.stickied);
     let locked_changed = Page::is_locked_changed(&old_post, &self.comments_enabled);
-    Ok(stickied_changed || locked_changed)
+    Ok(featured_changed || locked_changed)
   }
 
-  pub(crate) fn is_stickied_changed<E>(
+  pub(crate) fn is_featured_changed<E>(
     old_post: &Result<ApubPost, E>,
-    new_stickied: &Option<bool>,
+    new_featured_community: &Option<bool>,
   ) -> bool {
-    if let Some(new_stickied) = new_stickied {
+    if let Some(new_featured_community) = new_featured_community {
       if let Ok(old_post) = old_post {
-        return new_stickied != &old_post.stickied;
+        return new_featured_community != &old_post.featured_community;
       }
     }
 
@@ -143,39 +168,6 @@ impl Page {
     false
   }
 
-  pub(crate) async fn extract_community(
-    &self,
-    context: &LemmyContext,
-    request_counter: &mut i32,
-  ) -> Result<ApubCommunity, LemmyError> {
-    match &self.attributed_to {
-      AttributedTo::Lemmy(_) => {
-        let mut iter = self.to.iter().merge(self.cc.iter());
-        loop {
-          if let Some(cid) = iter.next() {
-            let cid = ObjectId::new(cid.clone());
-            if let Ok(c) = cid
-              .dereference(context, local_instance(context).await, request_counter)
-              .await
-            {
-              break Ok(c);
-            }
-          } else {
-            return Err(LemmyError::from_message("No community found in cc"));
-          }
-        }
-      }
-      AttributedTo::Peertube(p) => {
-        p.iter()
-          .find(|a| a.kind == PersonOrGroupType::Group)
-          .map(|a| ObjectId::<ApubCommunity>::new(a.id.clone().into_inner()))
-          .ok_or_else(|| LemmyError::from_message("page does not specify group"))?
-          .dereference(context, local_instance(context).await, request_counter)
-          .await
-      }
-    }
-  }
-
   pub(crate) fn creator(&self) -> Result<ObjectId<ApubPerson>, LemmyError> {
     match &self.attributed_to {
       AttributedTo::Lemmy(l) => Ok(l.clone()),
@@ -224,3 +216,46 @@ impl ActivityHandler for Page {
     Ok(())
   }
 }
+
+#[async_trait::async_trait(?Send)]
+impl InCommunity for Page {
+  async fn community(
+    &self,
+    context: &LemmyContext,
+    request_counter: &mut i32,
+  ) -> Result<ApubCommunity, LemmyError> {
+    let instance = local_instance(context).await;
+    let community = match &self.attributed_to {
+      AttributedTo::Lemmy(_) => {
+        let mut iter = self.to.iter().merge(self.cc.iter());
+        loop {
+          if let Some(cid) = iter.next() {
+            let cid = ObjectId::new(cid.clone());
+            if let Ok(c) = cid.dereference(context, instance, request_counter).await {
+              break c;
+            }
+          } else {
+            return Err(LemmyError::from_message("No community found in cc"));
+          }
+        }
+      }
+      AttributedTo::Peertube(p) => {
+        p.iter()
+          .find(|a| a.kind == PersonOrGroupType::Group)
+          .map(|a| ObjectId::<ApubCommunity>::new(a.id.clone().into_inner()))
+          .ok_or_else(|| LemmyError::from_message("page does not specify group"))?
+          .dereference(context, instance, request_counter)
+          .await?
+      }
+    };
+    if let Some(audience) = &self.audience {
+      let audience = audience
+        .dereference(context, instance, request_counter)
+        .await?;
+      verify_community_matches(&audience, community.id)?;
+      Ok(audience)
+    } else {
+      Ok(community)
+    }
+  }
+}