2021-01-12 16:12:41 +00:00
|
|
|
use crate::{
|
2021-03-22 12:52:00 +00:00
|
|
|
fetcher::{
|
|
|
|
fetch::fetch_remote_object,
|
|
|
|
is_deleted,
|
|
|
|
person::get_or_fetch_and_upsert_person,
|
|
|
|
should_refetch_actor,
|
|
|
|
},
|
2021-03-10 22:33:55 +00:00
|
|
|
inbox::person_inbox::receive_announce,
|
2021-01-12 16:12:41 +00:00
|
|
|
objects::FromApub,
|
|
|
|
GroupExt,
|
|
|
|
};
|
|
|
|
use activitystreams::{
|
2021-02-04 16:34:58 +00:00
|
|
|
actor::ApActorExt,
|
2021-01-12 16:12:41 +00:00
|
|
|
collection::{CollectionExt, OrderedCollection},
|
|
|
|
};
|
|
|
|
use anyhow::Context;
|
|
|
|
use diesel::result::Error::NotFound;
|
2021-03-25 19:19:40 +00:00
|
|
|
use lemmy_api_common::blocking;
|
2021-03-22 12:52:00 +00:00
|
|
|
use lemmy_db_queries::{source::community::Community_, ApubObject, Joinable};
|
|
|
|
use lemmy_db_schema::{
|
|
|
|
source::community::{Community, CommunityModerator, CommunityModeratorForm},
|
|
|
|
DbUrl,
|
|
|
|
};
|
|
|
|
use lemmy_db_views_actor::community_moderator_view::CommunityModeratorView;
|
2021-01-12 16:12:41 +00:00
|
|
|
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| {
|
2021-01-25 13:33:34 +00:00
|
|
|
Community::read_from_apub_id(conn, &apub_id_owned.into())
|
2021-01-12 16:12:41 +00:00
|
|
|
})
|
|
|
|
.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>,
|
2021-03-22 12:52:00 +00:00
|
|
|
request_counter: &mut i32,
|
2021-01-12 16:12:41 +00:00
|
|
|
) -> Result<Community, LemmyError> {
|
2021-03-22 12:52:00 +00:00
|
|
|
let group = fetch_remote_object::<GroupExt>(context.client(), apub_id, request_counter).await;
|
2021-01-12 16:12:41 +00:00
|
|
|
|
|
|
|
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?;
|
2021-03-22 12:52:00 +00:00
|
|
|
let community =
|
|
|
|
Community::from_apub(&group, context, apub_id.to_owned(), request_counter, false).await?;
|
|
|
|
|
|
|
|
update_community_mods(&group, &community, context, request_counter).await?;
|
2021-01-12 16:12:41 +00:00
|
|
|
|
2021-01-27 14:02:28 +00:00
|
|
|
// only fetch outbox for new communities, otherwise this can create an infinite loop
|
|
|
|
if old_community.is_none() {
|
2021-02-04 16:34:58 +00:00
|
|
|
let outbox = group.inner.outbox()?.context(location_info!())?;
|
2021-03-22 12:52:00 +00:00
|
|
|
fetch_community_outbox(context, outbox, &community, request_counter).await?
|
2021-01-27 14:02:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(community)
|
|
|
|
}
|
|
|
|
|
2021-03-22 12:52:00 +00:00
|
|
|
async fn update_community_mods(
|
|
|
|
group: &GroupExt,
|
|
|
|
community: &Community,
|
|
|
|
context: &LemmyContext,
|
|
|
|
request_counter: &mut i32,
|
|
|
|
) -> Result<(), LemmyError> {
|
|
|
|
let new_moderators = fetch_community_mods(context, group, request_counter).await?;
|
|
|
|
let community_id = community.id;
|
|
|
|
let current_moderators = blocking(context.pool(), move |conn| {
|
|
|
|
CommunityModeratorView::for_community(&conn, community_id)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
// Remove old mods from database which arent in the moderators collection anymore
|
|
|
|
for mod_user in ¤t_moderators {
|
|
|
|
if !new_moderators.contains(&&mod_user.moderator.actor_id.clone().into()) {
|
|
|
|
let community_moderator_form = CommunityModeratorForm {
|
|
|
|
community_id: mod_user.community.id,
|
|
|
|
person_id: mod_user.moderator.id,
|
|
|
|
};
|
|
|
|
blocking(context.pool(), move |conn| {
|
|
|
|
CommunityModerator::leave(conn, &community_moderator_form)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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?;
|
|
|
|
let current_mod_uris: Vec<DbUrl> = current_moderators
|
|
|
|
.clone()
|
|
|
|
.iter()
|
|
|
|
.map(|c| c.moderator.actor_id.clone())
|
|
|
|
.collect();
|
|
|
|
if !current_mod_uris.contains(&mod_user.actor_id) {
|
|
|
|
let community_moderator_form = CommunityModeratorForm {
|
|
|
|
community_id: community.id,
|
|
|
|
person_id: mod_user.id,
|
|
|
|
};
|
|
|
|
blocking(context.pool(), move |conn| {
|
|
|
|
CommunityModerator::join(conn, &community_moderator_form)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-01-27 14:02:28 +00:00
|
|
|
async fn fetch_community_outbox(
|
|
|
|
context: &LemmyContext,
|
2021-02-04 16:34:58 +00:00
|
|
|
outbox: &Url,
|
2021-01-27 14:02:28 +00:00
|
|
|
community: &Community,
|
|
|
|
recursion_counter: &mut i32,
|
|
|
|
) -> Result<(), LemmyError> {
|
2021-02-04 16:34:58 +00:00
|
|
|
let outbox =
|
|
|
|
fetch_remote_object::<OrderedCollection>(context.client(), outbox, recursion_counter).await?;
|
2021-01-27 14:02:28 +00:00
|
|
|
let outbox_activities = outbox.items().context(location_info!())?.clone();
|
|
|
|
let mut outbox_activities = outbox_activities.many().context(location_info!())?;
|
|
|
|
if outbox_activities.len() > 20 {
|
|
|
|
outbox_activities = outbox_activities[0..20].to_vec();
|
2021-01-12 16:12:41 +00:00
|
|
|
}
|
|
|
|
|
2021-01-27 14:02:28 +00:00
|
|
|
for activity in outbox_activities {
|
|
|
|
receive_announce(context, activity, community, recursion_counter).await?;
|
2021-01-12 16:12:41 +00:00
|
|
|
}
|
|
|
|
|
2021-01-27 14:02:28 +00:00
|
|
|
Ok(())
|
2021-01-12 16:12:41 +00:00
|
|
|
}
|
2021-03-08 13:40:28 +00:00
|
|
|
|
|
|
|
pub(crate) async fn fetch_community_mods(
|
|
|
|
context: &LemmyContext,
|
|
|
|
group: &GroupExt,
|
|
|
|
recursion_counter: &mut i32,
|
|
|
|
) -> Result<Vec<Url>, LemmyError> {
|
|
|
|
if let Some(mods_url) = &group.ext_one.moderators {
|
|
|
|
let mods =
|
|
|
|
fetch_remote_object::<OrderedCollection>(context.client(), mods_url, recursion_counter)
|
|
|
|
.await?;
|
|
|
|
let mods = mods
|
|
|
|
.items()
|
|
|
|
.map(|i| i.as_many())
|
|
|
|
.flatten()
|
|
|
|
.context(location_info!())?
|
|
|
|
.iter()
|
|
|
|
.filter_map(|i| i.as_xsd_any_uri())
|
|
|
|
.map(|u| u.to_owned())
|
|
|
|
.collect();
|
|
|
|
Ok(mods)
|
|
|
|
} else {
|
|
|
|
Ok(vec![])
|
|
|
|
}
|
|
|
|
}
|