]> 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 c259a6e996bd13f26579c0cfd84336e9c283a088..dba21f99d346d016ce11b8a945f0b83826d8ca61 100644 (file)
@@ -1,9 +1,13 @@
-use crate::local_instance;
-use activitypub_federation::{deser::values::MediaTypeMarkdown, utils::fetch_object_http};
-use activitystreams_kinds::object::ImageType;
+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 lemmy_websocket::LemmyContext;
 use serde::{de::DeserializeOwned, Deserialize, Serialize};
 use std::collections::HashMap;
 use url::Url;
@@ -60,30 +64,35 @@ pub(crate) enum IdOrNestedObject<Kind: Id> {
   NestedObject(Kind),
 }
 
-impl<Kind: Id + DeserializeOwned> IdOrNestedObject<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: &LemmyContext,
-    request_counter: &mut i32,
-  ) -> Result<Kind, LemmyError> {
+  pub(crate) async fn object(self, context: &Data<LemmyContext>) -> Result<Kind, LemmyError> {
     match self {
-      IdOrNestedObject::Id(i) => {
-        Ok(fetch_object_http(&i, local_instance(context).await, request_counter).await?)
-      }
+      // 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 activitypub_federation::deser::context::WithContext;
+  #![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};