2 collections::CommunityContext,
3 generate_moderators_url,
4 objects::person::ApubPerson,
5 protocol::collections::group_moderators::GroupModerators,
7 use activitystreams_kinds::collection::OrderedCollectionType;
8 use chrono::NaiveDateTime;
9 use lemmy_api_common::blocking;
10 use lemmy_apub_lib::{object_id::ObjectId, 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 #[tracing::instrument(skip_all)]
33 async fn read_from_apub_id(
35 data: &Self::DataType,
36 ) -> Result<Option<Self>, LemmyError> {
37 // Only read from database if its a local community, otherwise fetch over http
40 let moderators = blocking(data.1.pool(), move |conn| {
41 CommunityModeratorView::for_community(conn, cid)
44 Ok(Some(ApubCommunityModerators { 0: moderators }))
50 #[tracing::instrument(skip_all)]
51 async fn delete(self, _data: &Self::DataType) -> Result<(), LemmyError> {
55 #[tracing::instrument(skip_all)]
56 async fn into_apub(self, data: &Self::DataType) -> Result<Self::ApubType, LemmyError> {
57 let ordered_items = self
60 .map(|m| ObjectId::<ApubPerson>::new(m.moderator.actor_id))
63 r#type: OrderedCollectionType::OrderedCollection,
64 id: generate_moderators_url(&data.0.actor_id)?.into(),
69 fn to_tombstone(&self) -> Result<Self::TombstoneType, LemmyError> {
73 #[tracing::instrument(skip_all)]
75 group_moderators: &GroupModerators,
76 expected_domain: &Url,
77 _context: &CommunityContext,
78 _request_counter: &mut i32,
79 ) -> Result<(), LemmyError> {
80 verify_domains_match(&group_moderators.id, expected_domain)?;
84 #[tracing::instrument(skip_all)]
87 data: &Self::DataType,
88 request_counter: &mut i32,
89 ) -> Result<Self, LemmyError> {
90 let community_id = data.0.id;
91 let current_moderators = blocking(data.1.pool(), move |conn| {
92 CommunityModeratorView::for_community(conn, community_id)
95 // Remove old mods from database which arent in the moderators collection anymore
96 for mod_user in ¤t_moderators {
97 let mod_id = ObjectId::new(mod_user.moderator.actor_id.clone());
98 if !apub.ordered_items.contains(&mod_id) {
99 let community_moderator_form = CommunityModeratorForm {
100 community_id: mod_user.community.id,
101 person_id: mod_user.moderator.id,
103 blocking(data.1.pool(), move |conn| {
104 CommunityModerator::leave(conn, &community_moderator_form)
110 // Add new mods to database which have been added to moderators collection
111 for mod_id in apub.ordered_items {
112 let mod_id = ObjectId::new(mod_id);
113 let mod_user: ApubPerson = mod_id
114 .dereference(&data.1, data.1.client(), request_counter)
117 if !current_moderators
119 .map(|c| c.moderator.actor_id.clone())
120 .any(|x| x == mod_user.actor_id)
122 let community_moderator_form = CommunityModeratorForm {
123 community_id: data.0.id,
124 person_id: mod_user.id,
126 blocking(data.1.pool(), move |conn| {
127 CommunityModerator::join(conn, &community_moderator_form)
133 // This return value is unused, so just set an empty vec
134 Ok(ApubCommunityModerators { 0: vec![] })
143 community::tests::parse_lemmy_community,
144 person::tests::parse_lemmy_person,
147 protocol::tests::file_to_json_object,
149 use lemmy_db_schema::{
151 community::Community,
152 person::{Person, PersonForm},
157 use serial_test::serial;
161 async fn test_parse_lemmy_community_moderators() {
162 let context = init_context();
163 let (new_mod, site) = parse_lemmy_person(&context).await;
164 let community = parse_lemmy_community(&context).await;
165 let community_id = community.id;
167 let old_mod = PersonForm {
168 name: "holly".into(),
169 ..PersonForm::default()
171 let old_mod = Person::create(&context.pool().get().unwrap(), &old_mod).unwrap();
172 let community_moderator_form = CommunityModeratorForm {
173 community_id: community.id,
174 person_id: old_mod.id,
177 CommunityModerator::join(&context.pool().get().unwrap(), &community_moderator_form).unwrap();
179 assert_eq!(site.actor_id.to_string(), "https://enterprise.lemmy.ml/");
181 let json: GroupModerators =
182 file_to_json_object("assets/lemmy/collections/group_moderators.json").unwrap();
183 let url = Url::parse("https://enterprise.lemmy.ml/c/tenforward").unwrap();
184 let mut request_counter = 0;
185 let community_context = CommunityContext {
189 ApubCommunityModerators::verify(&json, &url, &community_context, &mut request_counter)
192 ApubCommunityModerators::from_apub(json, &community_context, &mut request_counter)
195 assert_eq!(request_counter, 0);
197 let current_moderators = blocking(community_context.1.pool(), move |conn| {
198 CommunityModeratorView::for_community(conn, community_id)
204 assert_eq!(current_moderators.len(), 1);
205 assert_eq!(current_moderators[0].moderator.id, new_mod.id);
207 Person::delete(&*community_context.1.pool().get().unwrap(), old_mod.id).unwrap();
208 Person::delete(&*community_context.1.pool().get().unwrap(), new_mod.id).unwrap();
210 &*community_context.1.pool().get().unwrap(),
211 community_context.0.id,
214 Site::delete(&*community_context.1.pool().get().unwrap(), site.id).unwrap();