2 collections::CommunityContext,
3 context::lemmy_context,
4 fetcher::object_id::ObjectId,
5 generate_moderators_url,
6 objects::person::ApubPerson,
10 chrono::NaiveDateTime,
11 collection::kind::OrderedCollectionType,
12 primitives::OneOrMany,
15 use lemmy_api_common::blocking;
16 use lemmy_apub_lib::{traits::ApubObject, verify::verify_domains_match};
17 use lemmy_db_schema::{
18 source::community::{CommunityModerator, CommunityModeratorForm},
21 use lemmy_db_views_actor::community_moderator_view::CommunityModeratorView;
22 use lemmy_utils::LemmyError;
23 use serde::{Deserialize, Serialize};
24 use serde_with::skip_serializing_none;
26 #[skip_serializing_none]
27 #[derive(Clone, Debug, Deserialize, Serialize)]
28 #[serde(rename_all = "camelCase")]
29 pub struct GroupModerators {
30 #[serde(rename = "@context")]
31 context: OneOrMany<AnyBase>,
32 r#type: OrderedCollectionType,
34 ordered_items: Vec<ObjectId<ApubPerson>>,
37 #[derive(Clone, Debug)]
38 pub(crate) struct ApubCommunityModerators(pub(crate) Vec<CommunityModeratorView>);
40 #[async_trait::async_trait(?Send)]
41 impl ApubObject for ApubCommunityModerators {
42 type DataType = CommunityContext;
43 type TombstoneType = ();
44 type ApubType = GroupModerators;
46 fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
50 async fn read_from_apub_id(
52 data: &Self::DataType,
53 ) -> Result<Option<Self>, LemmyError> {
54 // Only read from database if its a local community, otherwise fetch over http
57 let moderators = blocking(data.1.pool(), move |conn| {
58 CommunityModeratorView::for_community(conn, cid)
61 Ok(Some(ApubCommunityModerators { 0: moderators }))
67 async fn delete(self, _data: &Self::DataType) -> Result<(), LemmyError> {
71 async fn to_apub(&self, data: &Self::DataType) -> Result<Self::ApubType, LemmyError> {
72 let ordered_items = self
75 .map(|m| ObjectId::<ApubPerson>::new(m.moderator.actor_id.clone().into_inner()))
78 context: lemmy_context(),
79 r#type: OrderedCollectionType::OrderedCollection,
80 id: generate_moderators_url(&data.0.actor_id)?.into(),
85 fn to_tombstone(&self) -> Result<Self::TombstoneType, LemmyError> {
90 apub: &Self::ApubType,
91 data: &Self::DataType,
92 expected_domain: &Url,
93 request_counter: &mut i32,
94 ) -> Result<Self, LemmyError> {
95 verify_domains_match(expected_domain, &apub.id)?;
96 let community_id = data.0.id;
97 let current_moderators = blocking(data.1.pool(), move |conn| {
98 CommunityModeratorView::for_community(conn, community_id)
101 // Remove old mods from database which arent in the moderators collection anymore
102 for mod_user in ¤t_moderators {
103 let mod_id = ObjectId::new(mod_user.moderator.actor_id.clone().into_inner());
104 if !apub.ordered_items.contains(&mod_id) {
105 let community_moderator_form = CommunityModeratorForm {
106 community_id: mod_user.community.id,
107 person_id: mod_user.moderator.id,
109 blocking(data.1.pool(), move |conn| {
110 CommunityModerator::leave(conn, &community_moderator_form)
116 // Add new mods to database which have been added to moderators collection
117 for mod_id in &apub.ordered_items {
118 let mod_id = ObjectId::new(mod_id.clone());
119 let mod_user: ApubPerson = mod_id.dereference(&data.1, request_counter).await?;
121 if !current_moderators
124 .map(|c| c.moderator.actor_id.clone())
125 .any(|x| x == mod_user.actor_id)
127 let community_moderator_form = CommunityModeratorForm {
128 community_id: data.0.id,
129 person_id: mod_user.id,
131 blocking(data.1.pool(), move |conn| {
132 CommunityModerator::join(conn, &community_moderator_form)
138 // This return value is unused, so just set an empty vec
139 Ok(ApubCommunityModerators { 0: vec![] })