Merge two functions into one

This commit is contained in:
Felix Ableitner 2021-11-02 14:02:39 +01:00
parent 1c6f74cad5
commit b396344eae
3 changed files with 31 additions and 59 deletions

View file

@ -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
}
}

View file

@ -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<Url>,
context: &LemmyContext,
) -> Result<Vec<Url>, 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<T: ActorType>(
activity: AnnouncableActivities,
activity_id: &Url,

View file

@ -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<Url>,
context: &LemmyContext,
) -> Result<Vec<Url>, 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<Url> = 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)