2 collections::CommunityContext,
3 fetcher::object_id::ObjectId,
4 generate_moderators_url,
5 objects::person::ApubPerson,
6 protocol::collections::group_moderators::GroupModerators,
8 use activitystreams::{chrono::NaiveDateTime, collection::kind::OrderedCollectionType};
9 use lemmy_api_common::blocking;
10 use lemmy_apub_lib::{traits::ApubObject, verify::verify_domains_match};
11 use lemmy_db_schema::{
12 source::community::{CommunityModerator, CommunityModeratorForm},
15 use lemmy_db_views_actor::community_moderator_view::CommunityModeratorView;
16 use lemmy_utils::LemmyError;
19 #[derive(Clone, Debug)]
20 pub(crate) struct ApubCommunityModerators(pub(crate) Vec<CommunityModeratorView>);
22 #[async_trait::async_trait(?Send)]
23 impl ApubObject for ApubCommunityModerators {
24 type DataType = CommunityContext;
25 type TombstoneType = ();
26 type ApubType = GroupModerators;
28 fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
32 async fn read_from_apub_id(
34 data: &Self::DataType,
35 ) -> Result<Option<Self>, LemmyError> {
36 // Only read from database if its a local community, otherwise fetch over http
39 let moderators = blocking(data.1.pool(), move |conn| {
40 CommunityModeratorView::for_community(conn, cid)
43 Ok(Some(ApubCommunityModerators { 0: moderators }))
49 async fn delete(self, _data: &Self::DataType) -> Result<(), LemmyError> {
53 async fn to_apub(&self, data: &Self::DataType) -> Result<Self::ApubType, LemmyError> {
54 let ordered_items = self
57 .map(|m| ObjectId::<ApubPerson>::new(m.moderator.actor_id.clone().into_inner()))
60 r#type: OrderedCollectionType::OrderedCollection,
61 id: generate_moderators_url(&data.0.actor_id)?.into(),
66 fn to_tombstone(&self) -> Result<Self::TombstoneType, LemmyError> {
71 apub: &Self::ApubType,
72 data: &Self::DataType,
73 expected_domain: &Url,
74 request_counter: &mut i32,
75 ) -> Result<Self, LemmyError> {
76 verify_domains_match(expected_domain, &apub.id)?;
77 let community_id = data.0.id;
78 let current_moderators = blocking(data.1.pool(), move |conn| {
79 CommunityModeratorView::for_community(conn, community_id)
82 // Remove old mods from database which arent in the moderators collection anymore
83 for mod_user in ¤t_moderators {
84 let mod_id = ObjectId::new(mod_user.moderator.actor_id.clone().into_inner());
85 if !apub.ordered_items.contains(&mod_id) {
86 let community_moderator_form = CommunityModeratorForm {
87 community_id: mod_user.community.id,
88 person_id: mod_user.moderator.id,
90 blocking(data.1.pool(), move |conn| {
91 CommunityModerator::leave(conn, &community_moderator_form)
97 // Add new mods to database which have been added to moderators collection
98 for mod_id in &apub.ordered_items {
99 let mod_id = ObjectId::new(mod_id.clone());
100 let mod_user: ApubPerson = mod_id.dereference(&data.1, request_counter).await?;
102 if !current_moderators
105 .map(|c| c.moderator.actor_id.clone())
106 .any(|x| x == mod_user.actor_id)
108 let community_moderator_form = CommunityModeratorForm {
109 community_id: data.0.id,
110 person_id: mod_user.id,
112 blocking(data.1.pool(), move |conn| {
113 CommunityModerator::join(conn, &community_moderator_form)
119 // This return value is unused, so just set an empty vec
120 Ok(ApubCommunityModerators { 0: vec![] })