]> Untitled Git - lemmy.git/blobdiff - crates/apub/src/objects/instance.rs
Diesel 2.0.0 upgrade (#2452)
[lemmy.git] / crates / apub / src / objects / instance.rs
index cdd76c5a959300a54a6f00c451f2258a69cad15c..dbf2f9f3a05fdd362139c58c42052c49d627d2ed 100644 (file)
@@ -1,24 +1,29 @@
 use crate::{
-  check_is_apub_id_valid,
-  objects::get_summary_from_string_or_source,
-  protocol::{objects::instance::Instance, ImageObject, Source, Unparsed},
+  check_apub_id_valid_with_strictness,
+  local_instance,
+  objects::read_from_string_or_source_opt,
+  protocol::{
+    objects::instance::{Instance, InstanceType},
+    ImageObject,
+    Source,
+  },
+  ActorType,
 };
-use activitystreams_kinds::actor::ServiceType;
-use chrono::NaiveDateTime;
-use lemmy_api_common::blocking;
-use lemmy_apub_lib::{
-  object_id::ObjectId,
-  traits::{ActorType, ApubObject},
-  values::MediaTypeHtml,
-  verify::verify_domains_match,
+use activitypub_federation::{
+  core::object_id::ObjectId,
+  deser::values::MediaTypeHtml,
+  traits::{Actor, ApubObject},
+  utils::verify_domains_match,
 };
+use chrono::NaiveDateTime;
+use lemmy_api_common::utils::blocking;
 use lemmy_db_schema::{
-  naive_now,
   source::site::{Site, SiteForm},
+  utils::{naive_now, DbPool},
 };
 use lemmy_utils::{
+  error::LemmyError,
   utils::{check_slurs, check_slurs_opt, convert_datetime, markdown_to_html},
-  LemmyError,
 };
 use lemmy_websocket::LemmyContext;
 use std::ops::Deref;
@@ -37,7 +42,7 @@ impl Deref for ApubSite {
 
 impl From<Site> for ApubSite {
   fn from(s: Site) -> Self {
-    ApubSite { 0: s }
+    ApubSite(s)
   }
 }
 
@@ -45,7 +50,8 @@ impl From<Site> for ApubSite {
 impl ApubObject for ApubSite {
   type DataType = LemmyContext;
   type ApubType = Instance;
-  type TombstoneType = ();
+  type DbType = Site;
+  type Error = LemmyError;
 
   fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
     Some(self.last_refreshed_at)
@@ -72,7 +78,7 @@ impl ApubObject for ApubSite {
   #[tracing::instrument(skip_all)]
   async fn into_apub(self, _data: &Self::DataType) -> Result<Self::ApubType, LemmyError> {
     let instance = Instance {
-      kind: ServiceType::Service,
+      kind: InstanceType::Service,
       id: ObjectId::new(self.actor_id()),
       name: self.name.clone(),
       content: self.sidebar.as_ref().map(|d| markdown_to_html(d)),
@@ -83,18 +89,13 @@ impl ApubObject for ApubSite {
       image: self.banner.clone().map(ImageObject::new),
       inbox: self.inbox_url.clone().into(),
       outbox: Url::parse(&format!("{}/site_outbox", self.actor_id))?,
-      public_key: self.get_public_key()?,
+      public_key: self.get_public_key(),
       published: convert_datetime(self.published),
       updated: self.updated.map(convert_datetime),
-      unparsed: Unparsed::default(),
     };
     Ok(instance)
   }
 
-  fn to_tombstone(&self) -> Result<Self::TombstoneType, LemmyError> {
-    unimplemented!()
-  }
-
   #[tracing::instrument(skip_all)]
   async fn verify(
     apub: &Self::ApubType,
@@ -102,7 +103,7 @@ impl ApubObject for ApubSite {
     data: &Self::DataType,
     _request_counter: &mut i32,
   ) -> Result<(), LemmyError> {
-    check_is_apub_id_valid(apub.id.inner(), true, &data.settings())?;
+    check_apub_id_valid_with_strictness(apub.id.inner(), true, data.settings())?;
     verify_domains_match(expected_domain, apub.id.inner())?;
 
     let slur_regex = &data.settings().slur_regex();
@@ -119,8 +120,9 @@ impl ApubObject for ApubSite {
   ) -> Result<Self, LemmyError> {
     let site_form = SiteForm {
       name: apub.name.clone(),
-      sidebar: Some(get_summary_from_string_or_source(
+      sidebar: Some(read_from_string_or_source_opt(
         &apub.content,
+        &None,
         &apub.source,
       )),
       updated: apub.updated.map(|u| u.clone().naive_local()),
@@ -142,19 +144,18 @@ impl ActorType for ApubSite {
   fn actor_id(&self) -> Url {
     self.actor_id.to_owned().into()
   }
-  fn public_key(&self) -> String {
-    self.public_key.to_owned()
-  }
   fn private_key(&self) -> Option<String> {
     self.private_key.to_owned()
   }
+}
 
-  fn inbox_url(&self) -> Url {
-    self.inbox_url.clone().into()
+impl Actor for ApubSite {
+  fn public_key(&self) -> &str {
+    &self.public_key
   }
 
-  fn shared_inbox_url(&self) -> Option<Url> {
-    None
+  fn inbox(&self) -> Url {
+    self.inbox_url.clone().into()
   }
 }
 
@@ -176,18 +177,27 @@ pub(in crate::objects) async fn fetch_instance_actor_for_object(
   // try to fetch the instance actor (to make things like instance rules available)
   let instance_id = instance_actor_id_from_url(object_id);
   let site = ObjectId::<ApubSite>::new(instance_id.clone())
-    .dereference(context, context.client(), request_counter)
+    .dereference(context, local_instance(context), request_counter)
     .await;
   if let Err(e) = site {
     debug!("Failed to dereference site for {}: {}", instance_id, e);
   }
 }
 
+pub(crate) async fn remote_instance_inboxes(pool: &DbPool) -> Result<Vec<Url>, LemmyError> {
+  Ok(
+    blocking(pool, Site::read_remote_sites)
+      .await??
+      .into_iter()
+      .map(|s| ApubSite::from(s).shared_inbox_or_inbox())
+      .collect(),
+  )
+}
+
 #[cfg(test)]
 pub(crate) mod tests {
   use super::*;
-  use crate::objects::tests::{file_to_json_object, init_context};
-  use lemmy_apub_lib::activity_queue::create_activity_queue;
+  use crate::{objects::tests::init_context, protocol::tests::file_to_json_object};
   use lemmy_db_schema::traits::Crud;
   use serial_test::serial;
 
@@ -208,14 +218,13 @@ pub(crate) mod tests {
   #[actix_rt::test]
   #[serial]
   async fn test_parse_lemmy_instance() {
-    let client = reqwest::Client::new().into();
-    let manager = create_activity_queue(client);
-    let context = init_context(manager.queue_handle().clone());
+    let context = init_context();
+    let conn = &mut context.pool().get().unwrap();
     let site = parse_lemmy_instance(&context).await;
 
     assert_eq!(site.name, "Enterprise");
     assert_eq!(site.description.as_ref().unwrap().len(), 15);
 
-    Site::delete(&*context.pool().get().unwrap(), site.id).unwrap();
+    Site::delete(conn, site.id).unwrap();
   }
 }