2 collections::CommunityContext,
3 generate_moderators_url,
4 objects::person::ApubPerson,
5 protocol::collections::group_moderators::GroupModerators,
7 use activitystreams::{chrono::NaiveDateTime, collection::kind::OrderedCollectionType};
8 use lemmy_api_common::blocking;
9 use lemmy_apub_lib::{object_id::ObjectId, 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;
18 #[derive(Clone, Debug)]
19 pub(crate) struct ApubCommunityModerators(pub(crate) Vec<CommunityModeratorView>);
21 #[async_trait::async_trait(?Send)]
22 impl ApubObject for ApubCommunityModerators {
23 type DataType = CommunityContext;
24 type TombstoneType = ();
25 type ApubType = GroupModerators;
27 fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
31 async fn read_from_apub_id(
33 data: &Self::DataType,
34 ) -> Result<Option<Self>, LemmyError> {
35 // Only read from database if its a local community, otherwise fetch over http
38 let moderators = blocking(data.1.pool(), move |conn| {
39 CommunityModeratorView::for_community(conn, cid)
42 Ok(Some(ApubCommunityModerators { 0: moderators }))
48 async fn delete(self, _data: &Self::DataType) -> Result<(), LemmyError> {
52 async fn into_apub(self, data: &Self::DataType) -> Result<Self::ApubType, LemmyError> {
53 let ordered_items = self
56 .map(|m| ObjectId::<ApubPerson>::new(m.moderator.actor_id))
59 r#type: OrderedCollectionType::OrderedCollection,
60 id: generate_moderators_url(&data.0.actor_id)?.into(),
65 fn to_tombstone(&self) -> Result<Self::TombstoneType, LemmyError> {
71 data: &Self::DataType,
72 expected_domain: &Url,
73 request_counter: &mut i32,
74 ) -> Result<Self, LemmyError> {
75 verify_domains_match(expected_domain, &apub.id)?;
76 let community_id = data.0.id;
77 let current_moderators = blocking(data.1.pool(), move |conn| {
78 CommunityModeratorView::for_community(conn, community_id)
81 // Remove old mods from database which arent in the moderators collection anymore
82 for mod_user in ¤t_moderators {
83 let mod_id = ObjectId::new(mod_user.moderator.actor_id.clone());
84 if !apub.ordered_items.contains(&mod_id) {
85 let community_moderator_form = CommunityModeratorForm {
86 community_id: mod_user.community.id,
87 person_id: mod_user.moderator.id,
89 blocking(data.1.pool(), move |conn| {
90 CommunityModerator::leave(conn, &community_moderator_form)
96 // Add new mods to database which have been added to moderators collection
97 for mod_id in apub.ordered_items {
98 let mod_id = ObjectId::new(mod_id);
99 let mod_user: ApubPerson = mod_id.dereference(&data.1, request_counter).await?;
101 if !current_moderators
103 .map(|c| c.moderator.actor_id.clone())
104 .any(|x| x == mod_user.actor_id)
106 let community_moderator_form = CommunityModeratorForm {
107 community_id: data.0.id,
108 person_id: mod_user.id,
110 blocking(data.1.pool(), move |conn| {
111 CommunityModerator::join(conn, &community_moderator_form)
117 // This return value is unused, so just set an empty vec
118 Ok(ApubCommunityModerators { 0: vec![] })
125 use crate::objects::{
126 community::tests::parse_lemmy_community,
127 person::tests::parse_lemmy_person,
128 tests::{file_to_json_object, init_context},
130 use lemmy_db_schema::{
132 community::Community,
133 person::{Person, PersonForm},
137 use serial_test::serial;
141 async fn test_parse_lemmy_community_moderators() {
142 let context = init_context();
143 let community = parse_lemmy_community(&context).await;
144 let community_id = community.id;
146 let old_mod = PersonForm {
147 name: "holly".into(),
148 ..PersonForm::default()
150 let old_mod = Person::create(&context.pool().get().unwrap(), &old_mod).unwrap();
151 let community_moderator_form = CommunityModeratorForm {
152 community_id: community.id,
153 person_id: old_mod.id,
156 CommunityModerator::join(&context.pool().get().unwrap(), &community_moderator_form).unwrap();
158 let new_mod = parse_lemmy_person(&context).await;
160 let json: GroupModerators =
161 file_to_json_object("assets/lemmy/collections/group_moderators.json");
162 let url = Url::parse("https://enterprise.lemmy.ml/c/tenforward").unwrap();
163 let mut request_counter = 0;
164 let community_context = CommunityContext {
168 ApubCommunityModerators::from_apub(json, &community_context, &url, &mut request_counter)
171 assert_eq!(request_counter, 0);
173 let current_moderators = blocking(community_context.1.pool(), move |conn| {
174 CommunityModeratorView::for_community(conn, community_id)
180 assert_eq!(current_moderators.len(), 1);
181 assert_eq!(current_moderators[0].moderator.id, new_mod.id);
183 Person::delete(&*community_context.1.pool().get().unwrap(), old_mod.id).unwrap();
184 Person::delete(&*community_context.1.pool().get().unwrap(), new_mod.id).unwrap();
186 &*community_context.1.pool().get().unwrap(),
187 community_context.0.id,