]> Untitled Git - lemmy.git/blobdiff - crates/apub/src/fetcher/community.rs
Rewrite fetcher (#1792)
[lemmy.git] / crates / apub / src / fetcher / community.rs
index 83aa7b51e291425dba29a27785e72fd1e1240936..99dd3f85e87074cead5988d6231ff1907248ab11 100644 (file)
@@ -1,92 +1,23 @@
 use crate::{
   activities::community::announce::AnnounceActivity,
-  fetcher::{
-    fetch::fetch_remote_object,
-    is_deleted,
-    person::get_or_fetch_and_upsert_person,
-    should_refetch_actor,
-  },
-  objects::{community::Group, FromApub},
+  fetcher::{fetch::fetch_remote_object, object_id::ObjectId},
+  objects::community::Group,
 };
 use activitystreams::collection::{CollectionExt, OrderedCollection};
 use anyhow::Context;
-use diesel::result::Error::NotFound;
 use lemmy_api_common::blocking;
 use lemmy_apub_lib::ActivityHandler;
-use lemmy_db_queries::{source::community::Community_, ApubObject, Joinable};
-use lemmy_db_schema::source::community::{Community, CommunityModerator, CommunityModeratorForm};
+use lemmy_db_queries::Joinable;
+use lemmy_db_schema::source::{
+  community::{Community, CommunityModerator, CommunityModeratorForm},
+  person::Person,
+};
 use lemmy_db_views_actor::community_moderator_view::CommunityModeratorView;
 use lemmy_utils::{location_info, LemmyError};
 use lemmy_websocket::LemmyContext;
-use log::debug;
 use url::Url;
 
-/// Get a community from its apub ID.
-///
-/// If it exists locally and `!should_refetch_actor()`, it is returned directly from the database.
-/// Otherwise it is fetched from the remote instance, stored and returned.
-pub(crate) async fn get_or_fetch_and_upsert_community(
-  apub_id: &Url,
-  context: &LemmyContext,
-  recursion_counter: &mut i32,
-) -> Result<Community, LemmyError> {
-  let apub_id_owned = apub_id.to_owned();
-  let community = blocking(context.pool(), move |conn| {
-    Community::read_from_apub_id(conn, &apub_id_owned.into())
-  })
-  .await?;
-
-  match community {
-    Ok(c) if !c.local && should_refetch_actor(c.last_refreshed_at) => {
-      debug!("Fetching and updating from remote community: {}", apub_id);
-      fetch_remote_community(apub_id, context, Some(c), recursion_counter).await
-    }
-    Ok(c) => Ok(c),
-    Err(NotFound {}) => {
-      debug!("Fetching and creating remote community: {}", apub_id);
-      fetch_remote_community(apub_id, context, None, recursion_counter).await
-    }
-    Err(e) => Err(e.into()),
-  }
-}
-
-/// Request a community by apub ID from a remote instance, including moderators. If `old_community`,
-/// is set, this is an update for a community which is already known locally. If not, we don't know
-/// the community yet and also pull the outbox, to get some initial posts.
-async fn fetch_remote_community(
-  apub_id: &Url,
-  context: &LemmyContext,
-  old_community: Option<Community>,
-  request_counter: &mut i32,
-) -> Result<Community, LemmyError> {
-  let group = fetch_remote_object::<Group>(context.client(), apub_id, request_counter).await;
-
-  if let Some(c) = old_community.to_owned() {
-    if is_deleted(&group) {
-      blocking(context.pool(), move |conn| {
-        Community::update_deleted(conn, c.id, true)
-      })
-      .await??;
-    } else if group.is_err() {
-      // If fetching failed, return the existing data.
-      return Ok(c);
-    }
-  }
-
-  let group = group?;
-  let community = Community::from_apub(&group, context, apub_id, request_counter).await?;
-
-  update_community_mods(&group, &community, context, request_counter).await?;
-
-  // only fetch outbox for new communities, otherwise this can create an infinite loop
-  if old_community.is_none() {
-    fetch_community_outbox(context, &group.outbox, request_counter).await?
-  }
-
-  Ok(community)
-}
-
-async fn update_community_mods(
+pub(crate) async fn update_community_mods(
   group: &Group,
   community: &Community,
   context: &LemmyContext,
@@ -113,8 +44,9 @@ async fn update_community_mods(
   }
 
   // Add new mods to database which have been added to moderators collection
-  for mod_uri in new_moderators {
-    let mod_user = get_or_fetch_and_upsert_person(&mod_uri, context, request_counter).await?;
+  for mod_id in new_moderators {
+    let mod_id = ObjectId::new(mod_id);
+    let mod_user: Person = mod_id.dereference(context, request_counter).await?;
 
     if !current_moderators
       .clone()
@@ -136,7 +68,7 @@ async fn update_community_mods(
   Ok(())
 }
 
-async fn fetch_community_outbox(
+pub(crate) async fn fetch_community_outbox(
   context: &LemmyContext,
   outbox: &Url,
   recursion_counter: &mut i32,
@@ -160,7 +92,7 @@ async fn fetch_community_outbox(
   Ok(())
 }
 
-pub(crate) async fn fetch_community_mods(
+async fn fetch_community_mods(
   context: &LemmyContext,
   group: &Group,
   recursion_counter: &mut i32,