2 collections::CommunityContext,
3 generate_moderators_url,
5 objects::person::ApubPerson,
6 protocol::collections::group_moderators::GroupModerators,
8 use activitypub_federation::{
9 core::object_id::ObjectId,
11 utils::verify_domains_match,
13 use activitystreams_kinds::collection::OrderedCollectionType;
14 use chrono::NaiveDateTime;
15 use lemmy_api_common::utils::blocking;
16 use lemmy_db_schema::{
17 source::community::{CommunityModerator, CommunityModeratorForm},
20 use lemmy_db_views_actor::structs::CommunityModeratorView;
21 use lemmy_utils::error::LemmyError;
24 #[derive(Clone, Debug)]
25 pub(crate) struct ApubCommunityModerators(pub(crate) Vec<CommunityModeratorView>);
27 #[async_trait::async_trait(?Send)]
28 impl ApubObject for ApubCommunityModerators {
29 type DataType = CommunityContext;
30 type ApubType = GroupModerators;
31 type Error = LemmyError;
33 fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
37 #[tracing::instrument(skip_all)]
38 async fn read_from_apub_id(
40 data: &Self::DataType,
41 ) -> Result<Option<Self>, LemmyError> {
42 // Only read from database if its a local community, otherwise fetch over http
45 let moderators = blocking(data.1.pool(), move |conn| {
46 CommunityModeratorView::for_community(conn, cid)
49 Ok(Some(ApubCommunityModerators(moderators)))
55 #[tracing::instrument(skip_all)]
56 async fn delete(self, _data: &Self::DataType) -> Result<(), LemmyError> {
60 #[tracing::instrument(skip_all)]
61 async fn into_apub(self, data: &Self::DataType) -> Result<Self::ApubType, LemmyError> {
62 let ordered_items = self
65 .map(|m| ObjectId::<ApubPerson>::new(m.moderator.actor_id))
68 r#type: OrderedCollectionType::OrderedCollection,
69 id: generate_moderators_url(&data.0.actor_id)?.into(),
74 #[tracing::instrument(skip_all)]
76 group_moderators: &GroupModerators,
77 expected_domain: &Url,
78 _context: &CommunityContext,
79 _request_counter: &mut i32,
80 ) -> Result<(), LemmyError> {
81 verify_domains_match(&group_moderators.id, expected_domain)?;
85 #[tracing::instrument(skip_all)]
88 data: &Self::DataType,
89 request_counter: &mut i32,
90 ) -> Result<Self, LemmyError> {
91 let community_id = data.0.id;
92 let current_moderators = blocking(data.1.pool(), move |conn| {
93 CommunityModeratorView::for_community(conn, community_id)
96 // Remove old mods from database which arent in the moderators collection anymore
97 for mod_user in ¤t_moderators {
98 let mod_id = ObjectId::new(mod_user.moderator.actor_id.clone());
99 if !apub.ordered_items.contains(&mod_id) {
100 let community_moderator_form = CommunityModeratorForm {
101 community_id: mod_user.community.id,
102 person_id: mod_user.moderator.id,
104 blocking(data.1.pool(), move |conn| {
105 CommunityModerator::leave(conn, &community_moderator_form)
111 // Add new mods to database which have been added to moderators collection
112 for mod_id in apub.ordered_items {
113 let mod_id = ObjectId::new(mod_id);
114 let mod_user: ApubPerson = mod_id
115 .dereference(&data.1, local_instance(&data.1), request_counter)
118 if !current_moderators
120 .map(|c| c.moderator.actor_id.clone())
121 .any(|x| x == mod_user.actor_id)
123 let community_moderator_form = CommunityModeratorForm {
124 community_id: data.0.id,
125 person_id: mod_user.id,
127 blocking(data.1.pool(), move |conn| {
128 CommunityModerator::join(conn, &community_moderator_form)
134 // This return value is unused, so just set an empty vec
135 Ok(ApubCommunityModerators(Vec::new()))
146 community::tests::parse_lemmy_community,
147 person::tests::parse_lemmy_person,
150 protocol::tests::file_to_json_object,
152 use lemmy_db_schema::{
154 community::Community,
155 person::{Person, PersonForm},
160 use serial_test::serial;
164 async fn test_parse_lemmy_community_moderators() {
165 let context = init_context();
166 let (new_mod, site) = parse_lemmy_person(&context).await;
167 let community = parse_lemmy_community(&context).await;
168 let community_id = community.id;
170 let old_mod = PersonForm {
171 name: "holly".into(),
172 public_key: Some("pubkey".to_string()),
173 ..PersonForm::default()
175 let old_mod = Person::create(&context.pool().get().unwrap(), &old_mod).unwrap();
176 let community_moderator_form = CommunityModeratorForm {
177 community_id: community.id,
178 person_id: old_mod.id,
181 CommunityModerator::join(&context.pool().get().unwrap(), &community_moderator_form).unwrap();
183 assert_eq!(site.actor_id.to_string(), "https://enterprise.lemmy.ml/");
185 let json: GroupModerators =
186 file_to_json_object("assets/lemmy/collections/group_moderators.json").unwrap();
187 let url = Url::parse("https://enterprise.lemmy.ml/c/tenforward").unwrap();
188 let mut request_counter = 0;
189 let community_context = CommunityContext(community, context);
190 ApubCommunityModerators::verify(&json, &url, &community_context, &mut request_counter)
193 ApubCommunityModerators::from_apub(json, &community_context, &mut request_counter)
196 assert_eq!(request_counter, 0);
198 let current_moderators = blocking(community_context.1.pool(), move |conn| {
199 CommunityModeratorView::for_community(conn, community_id)
205 assert_eq!(current_moderators.len(), 1);
206 assert_eq!(current_moderators[0].moderator.id, new_mod.id);
208 Person::delete(&*community_context.1.pool().get().unwrap(), old_mod.id).unwrap();
209 Person::delete(&*community_context.1.pool().get().unwrap(), new_mod.id).unwrap();
211 &*community_context.1.pool().get().unwrap(),
212 community_context.0.id,
215 Site::delete(&*community_context.1.pool().get().unwrap(), site.id).unwrap();