2 collections::CommunityContext,
3 fetcher::object_id::ObjectId,
4 generate_moderators_url,
5 objects::person::ApubPerson,
7 use activitystreams::{chrono::NaiveDateTime, collection::kind::OrderedCollectionType};
8 use lemmy_api_common::blocking;
9 use lemmy_apub_lib::{traits::ApubObject, verify::verify_domains_match};
10 use lemmy_db_schema::{
11 source::community::{CommunityModerator, CommunityModeratorForm},
14 use lemmy_db_views_actor::community_moderator_view::CommunityModeratorView;
15 use lemmy_utils::LemmyError;
16 use serde::{Deserialize, Serialize};
19 #[derive(Clone, Debug, Deserialize, Serialize)]
20 #[serde(rename_all = "camelCase")]
21 pub struct GroupModerators {
22 r#type: OrderedCollectionType,
24 ordered_items: Vec<ObjectId<ApubPerson>>,
27 #[derive(Clone, Debug)]
28 pub(crate) struct ApubCommunityModerators(pub(crate) Vec<CommunityModeratorView>);
30 #[async_trait::async_trait(?Send)]
31 impl ApubObject for ApubCommunityModerators {
32 type DataType = CommunityContext;
33 type TombstoneType = ();
34 type ApubType = GroupModerators;
36 fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
40 async fn read_from_apub_id(
42 data: &Self::DataType,
43 ) -> Result<Option<Self>, LemmyError> {
44 // Only read from database if its a local community, otherwise fetch over http
47 let moderators = blocking(data.1.pool(), move |conn| {
48 CommunityModeratorView::for_community(conn, cid)
51 Ok(Some(ApubCommunityModerators { 0: moderators }))
57 async fn delete(self, _data: &Self::DataType) -> Result<(), LemmyError> {
61 async fn to_apub(&self, data: &Self::DataType) -> Result<Self::ApubType, LemmyError> {
62 let ordered_items = self
65 .map(|m| ObjectId::<ApubPerson>::new(m.moderator.actor_id.clone().into_inner()))
68 r#type: OrderedCollectionType::OrderedCollection,
69 id: generate_moderators_url(&data.0.actor_id)?.into(),
74 fn to_tombstone(&self) -> Result<Self::TombstoneType, LemmyError> {
79 apub: &Self::ApubType,
80 data: &Self::DataType,
81 expected_domain: &Url,
82 request_counter: &mut i32,
83 ) -> Result<Self, LemmyError> {
84 verify_domains_match(expected_domain, &apub.id)?;
85 let community_id = data.0.id;
86 let current_moderators = blocking(data.1.pool(), move |conn| {
87 CommunityModeratorView::for_community(conn, community_id)
90 // Remove old mods from database which arent in the moderators collection anymore
91 for mod_user in ¤t_moderators {
92 let mod_id = ObjectId::new(mod_user.moderator.actor_id.clone().into_inner());
93 if !apub.ordered_items.contains(&mod_id) {
94 let community_moderator_form = CommunityModeratorForm {
95 community_id: mod_user.community.id,
96 person_id: mod_user.moderator.id,
98 blocking(data.1.pool(), move |conn| {
99 CommunityModerator::leave(conn, &community_moderator_form)
105 // Add new mods to database which have been added to moderators collection
106 for mod_id in &apub.ordered_items {
107 let mod_id = ObjectId::new(mod_id.clone());
108 let mod_user: ApubPerson = mod_id.dereference(&data.1, request_counter).await?;
110 if !current_moderators
113 .map(|c| c.moderator.actor_id.clone())
114 .any(|x| x == mod_user.actor_id)
116 let community_moderator_form = CommunityModeratorForm {
117 community_id: data.0.id,
118 person_id: mod_user.id,
120 blocking(data.1.pool(), move |conn| {
121 CommunityModerator::join(conn, &community_moderator_form)
127 // This return value is unused, so just set an empty vec
128 Ok(ApubCommunityModerators { 0: vec![] })