From b396344eaeec167e21077d58dc1d18c22e0b105e Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Tue, 2 Nov 2021 14:02:39 +0100 Subject: [PATCH] Merge two functions into one --- .../apub/src/activities/community/announce.rs | 12 ++--- crates/apub/src/activities/community/mod.rs | 34 ++------------ crates/apub/src/objects/community.rs | 44 ++++++++++--------- 3 files changed, 31 insertions(+), 59 deletions(-) diff --git a/crates/apub/src/activities/community/announce.rs b/crates/apub/src/activities/community/announce.rs index 62e7c56fd..f560b09f6 100644 --- a/crates/apub/src/activities/community/announce.rs +++ b/crates/apub/src/activities/community/announce.rs @@ -1,11 +1,5 @@ use crate::{ - activities::{ - community::list_community_follower_inboxes, - generate_activity_id, - send_lemmy_activity, - verify_activity, - verify_is_public, - }, + activities::{generate_activity_id, send_lemmy_activity, verify_activity, verify_is_public}, activity_lists::AnnouncableActivities, fetcher::object_id::ObjectId, http::is_activity_already_known, @@ -50,7 +44,9 @@ impl AnnounceActivity { )?, unparsed: Default::default(), }; - let inboxes = list_community_follower_inboxes(community, additional_inboxes, context).await?; + let inboxes = community + .get_follower_inboxes(additional_inboxes, context) + .await?; send_lemmy_activity(context, &announce, &announce.id, community, inboxes, false).await } } diff --git a/crates/apub/src/activities/community/mod.rs b/crates/apub/src/activities/community/mod.rs index ebc6e7f92..96e692d58 100644 --- a/crates/apub/src/activities/community/mod.rs +++ b/crates/apub/src/activities/community/mod.rs @@ -1,19 +1,15 @@ -use itertools::Itertools; -use url::Url; - -use lemmy_apub_lib::traits::ActorType; -use lemmy_utils::LemmyError; -use lemmy_websocket::LemmyContext; - use crate::{ activities::send_lemmy_activity, activity_lists::AnnouncableActivities, - check_is_apub_id_valid, fetcher::object_id::ObjectId, insert_activity, objects::community::ApubCommunity, protocol::activities::community::announce::AnnounceActivity, }; +use lemmy_apub_lib::traits::ActorType; +use lemmy_utils::LemmyError; +use lemmy_websocket::LemmyContext; +use url::Url; pub mod add_mod; pub mod announce; @@ -23,28 +19,6 @@ pub mod report; pub mod undo_block_user; pub mod update; -async fn list_community_follower_inboxes( - community: &ApubCommunity, - additional_inboxes: Vec, - context: &LemmyContext, -) -> Result, LemmyError> { - Ok( - vec![ - community - .get_follower_inboxes(context.pool(), &context.settings()) - .await?, - additional_inboxes, - ] - .iter() - .flatten() - .unique() - .filter(|inbox| inbox.host_str() != Some(&context.settings().hostname)) - .filter(|inbox| check_is_apub_id_valid(inbox, false, &context.settings()).is_ok()) - .map(|inbox| inbox.to_owned()) - .collect(), - ) -} - pub(crate) async fn send_to_community( activity: AnnouncableActivities, activity_id: &Url, diff --git a/crates/apub/src/objects/community.rs b/crates/apub/src/objects/community.rs index 89258fe0c..0947ebf37 100644 --- a/crates/apub/src/objects/community.rs +++ b/crates/apub/src/objects/community.rs @@ -1,5 +1,3 @@ -use std::ops::Deref; - use activitystreams::{ actor::{kind::GroupType, Endpoints}, object::kind::ImageType, @@ -7,22 +5,9 @@ use activitystreams::{ use chrono::NaiveDateTime; use itertools::Itertools; use log::debug; +use std::ops::Deref; use url::Url; -use lemmy_api_common::blocking; -use lemmy_apub_lib::{ - traits::{ActorType, ApubObject}, - values::MediaTypeMarkdown, -}; -use lemmy_db_schema::{source::community::Community, DbPool}; -use lemmy_db_views_actor::community_follower_view::CommunityFollowerView; -use lemmy_utils::{ - settings::structs::Settings, - utils::{convert_datetime, markdown_to_html}, - LemmyError, -}; -use lemmy_websocket::LemmyContext; - use crate::{ check_is_apub_id_valid, collections::{community_moderators::ApubCommunityModerators, CommunityContext}, @@ -35,6 +20,18 @@ use crate::{ Source, }, }; +use lemmy_api_common::blocking; +use lemmy_apub_lib::{ + traits::{ActorType, ApubObject}, + values::MediaTypeMarkdown, +}; +use lemmy_db_schema::source::community::Community; +use lemmy_db_views_actor::community_follower_view::CommunityFollowerView; +use lemmy_utils::{ + utils::{convert_datetime, markdown_to_html}, + LemmyError, +}; +use lemmy_websocket::LemmyContext; #[derive(Clone, Debug)] pub struct ApubCommunity(Community); @@ -198,23 +195,28 @@ impl ApubCommunity { /// For a given community, returns the inboxes of all followers. pub(crate) async fn get_follower_inboxes( &self, - pool: &DbPool, - settings: &Settings, + additional_inboxes: Vec, + context: &LemmyContext, ) -> Result, LemmyError> { let id = self.id; - let follows = blocking(pool, move |conn| { + let follows = blocking(context.pool(), move |conn| { CommunityFollowerView::for_community(conn, id) }) .await??; - let inboxes = follows + let follower_inboxes: Vec = follows .into_iter() .filter(|f| !f.follower.local) .map(|f| f.follower.shared_inbox_url.unwrap_or(f.follower.inbox_url)) .map(|i| i.into_inner()) + .collect(); + let inboxes = vec![follower_inboxes, additional_inboxes] + .into_iter() + .flatten() .unique() + .filter(|inbox| inbox.host_str() != Some(&context.settings().hostname)) // Don't send to blocked instances - .filter(|inbox| check_is_apub_id_valid(inbox, false, settings).is_ok()) + .filter(|inbox| check_is_apub_id_valid(inbox, false, &context.settings()).is_ok()) .collect(); Ok(inboxes)