]> Untitled Git - lemmy.git/blobdiff - crates/apub/src/protocol/mod.rs
Cache & Optimize Woodpecker CI (#3450)
[lemmy.git] / crates / apub / src / protocol / mod.rs
index a7a233964c1023258a4330cea31a18c03d4ce3a7..dba21f99d346d016ce11b8a945f0b83826d8ca61 100644 (file)
@@ -1,10 +1,16 @@
-use activitystreams_kinds::object::ImageType;
-use serde::{Deserialize, Serialize};
-use url::Url;
-
-use lemmy_apub_lib::values::MediaTypeMarkdown;
+use crate::objects::community::ApubCommunity;
+use activitypub_federation::{
+  config::Data,
+  fetch::fetch_object_http,
+  kinds::object::ImageType,
+  protocol::values::MediaTypeMarkdown,
+};
+use lemmy_api_common::context::LemmyContext;
 use lemmy_db_schema::newtypes::DbUrl;
+use lemmy_utils::error::LemmyError;
+use serde::{de::DeserializeOwned, Deserialize, Serialize};
 use std::collections::HashMap;
+use url::Url;
 
 pub mod activities;
 pub(crate) mod collections;
@@ -17,6 +23,15 @@ pub struct Source {
   pub(crate) media_type: MediaTypeMarkdown,
 }
 
+impl Source {
+  pub(crate) fn new(content: String) -> Self {
+    Source {
+      content,
+      media_type: MediaTypeMarkdown::Markdown,
+    }
+  }
+}
+
 #[derive(Clone, Debug, Deserialize, Serialize)]
 #[serde(rename_all = "camelCase")]
 pub struct ImageObject {
@@ -34,29 +49,77 @@ impl ImageObject {
   }
 }
 
-#[derive(Clone, Debug, Default, serde::Deserialize, serde::Serialize)]
+#[derive(Clone, Debug, Default, Deserialize, Serialize)]
 #[serde(transparent)]
 pub struct Unparsed(HashMap<String, serde_json::Value>);
 
+pub(crate) trait Id {
+  fn object_id(&self) -> &Url;
+}
+
+#[derive(Clone, Debug, Deserialize, Serialize)]
+#[serde(untagged)]
+pub(crate) enum IdOrNestedObject<Kind: Id> {
+  Id(Url),
+  NestedObject(Kind),
+}
+
+impl<Kind: Id + DeserializeOwned + Send> IdOrNestedObject<Kind> {
+  pub(crate) fn id(&self) -> &Url {
+    match self {
+      IdOrNestedObject::Id(i) => i,
+      IdOrNestedObject::NestedObject(n) => n.object_id(),
+    }
+  }
+  pub(crate) async fn object(self, context: &Data<LemmyContext>) -> Result<Kind, LemmyError> {
+    match self {
+      // TODO: move IdOrNestedObject struct to library and make fetch_object_http private
+      IdOrNestedObject::Id(i) => Ok(fetch_object_http(&i, context).await?),
+      IdOrNestedObject::NestedObject(o) => Ok(o),
+    }
+  }
+}
+
+#[async_trait::async_trait]
+pub trait InCommunity {
+  // TODO: after we use audience field and remove backwards compat, it should be possible to change
+  //       this to simply `fn community(&self)  -> Result<ObjectId<ApubCommunity>, LemmyError>`
+  async fn community(&self, context: &Data<LemmyContext>) -> Result<ApubCommunity, LemmyError>;
+}
+
 #[cfg(test)]
 pub(crate) mod tests {
-  use crate::objects::tests::file_to_json_object;
+  #![allow(clippy::unwrap_used)]
+  #![allow(clippy::indexing_slicing)]
+
+  use activitypub_federation::protocol::context::WithContext;
   use assert_json_diff::assert_json_include;
+  use lemmy_utils::error::LemmyError;
   use serde::{de::DeserializeOwned, Serialize};
-  use std::collections::HashMap;
+  use std::{collections::HashMap, fs::File, io::BufReader};
+
+  pub(crate) fn file_to_json_object<T: DeserializeOwned>(path: &str) -> Result<T, LemmyError> {
+    let file = File::open(path)?;
+    let reader = BufReader::new(file);
+    Ok(serde_json::from_reader(reader)?)
+  }
+
+  pub(crate) fn test_json<T: DeserializeOwned>(path: &str) -> Result<WithContext<T>, LemmyError> {
+    file_to_json_object::<WithContext<T>>(path)
+  }
 
   /// Check that json deserialize -> serialize -> deserialize gives identical file as initial one.
   /// Ensures that there are no breaking changes in sent data.
   pub(crate) fn test_parse_lemmy_item<T: Serialize + DeserializeOwned + std::fmt::Debug>(
     path: &str,
-  ) -> T {
+  ) -> Result<T, LemmyError> {
     // parse file as T
-    let parsed = file_to_json_object::<T>(path).unwrap();
+    let parsed = file_to_json_object::<T>(path)?;
 
     // parse file into hashmap, which ensures that every field is included
-    let raw = file_to_json_object::<HashMap<String, serde_json::Value>>(path).unwrap();
+    let raw = file_to_json_object::<HashMap<String, serde_json::Value>>(path)?;
     // assert that all fields are identical, otherwise print diff
     assert_json_include!(actual: &parsed, expected: raw);
-    parsed
+    Ok(parsed)
   }
 }