]> Untitled Git - lemmy.git/blob - crates/apub/src/collections/community_moderators.rs
Rewrite community followers and user outbox to use our own structs
[lemmy.git] / crates / apub / src / collections / community_moderators.rs
1 use crate::{
2   collections::CommunityContext,
3   fetcher::object_id::ObjectId,
4   generate_moderators_url,
5   objects::person::ApubPerson,
6 };
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},
12   traits::Joinable,
13 };
14 use lemmy_db_views_actor::community_moderator_view::CommunityModeratorView;
15 use lemmy_utils::LemmyError;
16 use serde::{Deserialize, Serialize};
17 use url::Url;
18
19 #[derive(Clone, Debug, Deserialize, Serialize)]
20 #[serde(rename_all = "camelCase")]
21 pub struct GroupModerators {
22   r#type: OrderedCollectionType,
23   id: Url,
24   ordered_items: Vec<ObjectId<ApubPerson>>,
25 }
26
27 #[derive(Clone, Debug)]
28 pub(crate) struct ApubCommunityModerators(pub(crate) Vec<CommunityModeratorView>);
29
30 #[async_trait::async_trait(?Send)]
31 impl ApubObject for ApubCommunityModerators {
32   type DataType = CommunityContext;
33   type TombstoneType = ();
34   type ApubType = GroupModerators;
35
36   fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
37     None
38   }
39
40   async fn read_from_apub_id(
41     _object_id: Url,
42     data: &Self::DataType,
43   ) -> Result<Option<Self>, LemmyError> {
44     // Only read from database if its a local community, otherwise fetch over http
45     if data.0.local {
46       let cid = data.0.id;
47       let moderators = blocking(data.1.pool(), move |conn| {
48         CommunityModeratorView::for_community(conn, cid)
49       })
50       .await??;
51       Ok(Some(ApubCommunityModerators { 0: moderators }))
52     } else {
53       Ok(None)
54     }
55   }
56
57   async fn delete(self, _data: &Self::DataType) -> Result<(), LemmyError> {
58     unimplemented!()
59   }
60
61   async fn to_apub(&self, data: &Self::DataType) -> Result<Self::ApubType, LemmyError> {
62     let ordered_items = self
63       .0
64       .iter()
65       .map(|m| ObjectId::<ApubPerson>::new(m.moderator.actor_id.clone().into_inner()))
66       .collect();
67     Ok(GroupModerators {
68       r#type: OrderedCollectionType::OrderedCollection,
69       id: generate_moderators_url(&data.0.actor_id)?.into(),
70       ordered_items,
71     })
72   }
73
74   fn to_tombstone(&self) -> Result<Self::TombstoneType, LemmyError> {
75     unimplemented!()
76   }
77
78   async fn from_apub(
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)
88     })
89     .await??;
90     // Remove old mods from database which arent in the moderators collection anymore
91     for mod_user in &current_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,
97         };
98         blocking(data.1.pool(), move |conn| {
99           CommunityModerator::leave(conn, &community_moderator_form)
100         })
101         .await??;
102       }
103     }
104
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?;
109
110       if !current_moderators
111         .clone()
112         .iter()
113         .map(|c| c.moderator.actor_id.clone())
114         .any(|x| x == mod_user.actor_id)
115       {
116         let community_moderator_form = CommunityModeratorForm {
117           community_id: data.0.id,
118           person_id: mod_user.id,
119         };
120         blocking(data.1.pool(), move |conn| {
121           CommunityModerator::join(conn, &community_moderator_form)
122         })
123         .await??;
124       }
125     }
126
127     // This return value is unused, so just set an empty vec
128     Ok(ApubCommunityModerators { 0: vec![] })
129   }
130 }