2021-01-12 16:12:41 +00:00
|
|
|
use crate::{
|
2021-07-17 16:20:44 +00:00
|
|
|
activities::community::announce::AnnounceActivity,
|
2021-09-25 15:44:52 +00:00
|
|
|
fetcher::{fetch::fetch_remote_object, object_id::ObjectId},
|
|
|
|
objects::community::Group,
|
2021-01-12 16:12:41 +00:00
|
|
|
};
|
2021-08-12 12:48:09 +00:00
|
|
|
use activitystreams::collection::{CollectionExt, OrderedCollection};
|
2021-01-12 16:12:41 +00:00
|
|
|
use anyhow::Context;
|
2021-03-25 19:19:40 +00:00
|
|
|
use lemmy_api_common::blocking;
|
2021-10-06 20:20:05 +00:00
|
|
|
use lemmy_apub_lib::{data::Data, traits::ActivityHandler};
|
2021-10-16 13:33:38 +00:00
|
|
|
use lemmy_db_schema::{
|
|
|
|
source::{
|
|
|
|
community::{Community, CommunityModerator, CommunityModeratorForm},
|
|
|
|
person::Person,
|
|
|
|
},
|
|
|
|
traits::Joinable,
|
2021-09-25 15:44:52 +00:00
|
|
|
};
|
2021-03-22 12:52:00 +00:00
|
|
|
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 url::Url;
|
|
|
|
|
2021-09-25 15:44:52 +00:00
|
|
|
pub(crate) async fn update_community_mods(
|
2021-08-12 12:48:09 +00:00
|
|
|
group: &Group,
|
2021-03-22 12:52:00 +00:00
|
|
|
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| {
|
2021-07-05 16:07:26 +00:00
|
|
|
CommunityModeratorView::for_community(conn, community_id)
|
2021-03-22 12:52:00 +00:00
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
// Remove old mods from database which arent in the moderators collection anymore
|
|
|
|
for mod_user in ¤t_moderators {
|
2021-07-05 16:07:26 +00:00
|
|
|
if !new_moderators.contains(&mod_user.moderator.actor_id.clone().into()) {
|
2021-03-22 12:52:00 +00:00
|
|
|
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
|
2021-09-25 15:44:52 +00:00
|
|
|
for mod_id in new_moderators {
|
|
|
|
let mod_id = ObjectId::new(mod_id);
|
|
|
|
let mod_user: Person = mod_id.dereference(context, request_counter).await?;
|
2021-07-05 16:07:26 +00:00
|
|
|
|
|
|
|
if !current_moderators
|
2021-03-22 12:52:00 +00:00
|
|
|
.clone()
|
|
|
|
.iter()
|
|
|
|
.map(|c| c.moderator.actor_id.clone())
|
2021-07-05 16:07:26 +00:00
|
|
|
.any(|x| x == mod_user.actor_id)
|
|
|
|
{
|
2021-03-22 12:52:00 +00:00
|
|
|
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-09-25 15:44:52 +00:00
|
|
|
pub(crate) async fn fetch_community_outbox(
|
2021-01-27 14:02:28 +00:00
|
|
|
context: &LemmyContext,
|
2021-02-04 16:34:58 +00:00
|
|
|
outbox: &Url,
|
2021-01-27 14:02:28 +00:00
|
|
|
recursion_counter: &mut i32,
|
|
|
|
) -> Result<(), LemmyError> {
|
2021-09-22 15:57:09 +00:00
|
|
|
let outbox = fetch_remote_object::<OrderedCollection>(
|
|
|
|
context.client(),
|
|
|
|
&context.settings(),
|
|
|
|
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-07-17 16:20:44 +00:00
|
|
|
for announce in outbox_activities {
|
|
|
|
// TODO: instead of converting like this, we should create a struct CommunityOutbox with
|
|
|
|
// AnnounceActivity as inner type, but that gives me stackoverflow
|
|
|
|
let ser = serde_json::to_string(&announce)?;
|
|
|
|
let announce: AnnounceActivity = serde_json::from_str(&ser)?;
|
2021-10-06 20:20:05 +00:00
|
|
|
announce
|
|
|
|
.receive(&Data::new(context.clone()), 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
|
|
|
|
2021-09-25 15:44:52 +00:00
|
|
|
async fn fetch_community_mods(
|
2021-03-08 13:40:28 +00:00
|
|
|
context: &LemmyContext,
|
2021-08-12 12:48:09 +00:00
|
|
|
group: &Group,
|
2021-03-08 13:40:28 +00:00
|
|
|
recursion_counter: &mut i32,
|
|
|
|
) -> Result<Vec<Url>, LemmyError> {
|
2021-08-12 12:48:09 +00:00
|
|
|
if let Some(mods_url) = &group.moderators {
|
2021-09-22 15:57:09 +00:00
|
|
|
let mods = fetch_remote_object::<OrderedCollection>(
|
|
|
|
context.client(),
|
|
|
|
&context.settings(),
|
|
|
|
mods_url,
|
|
|
|
recursion_counter,
|
|
|
|
)
|
|
|
|
.await?;
|
2021-03-08 13:40:28 +00:00
|
|
|
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![])
|
|
|
|
}
|
|
|
|
}
|