2021-10-27 16:03:07 +00:00
|
|
|
use crate::{
|
2023-03-21 15:03:05 +00:00
|
|
|
objects::{community::ApubCommunity, person::ApubPerson},
|
2021-10-28 21:17:59 +00:00
|
|
|
protocol::collections::group_moderators::GroupModerators,
|
2021-10-27 16:03:07 +00:00
|
|
|
};
|
2022-06-02 14:33:41 +00:00
|
|
|
use activitypub_federation::{
|
2023-03-21 15:03:05 +00:00
|
|
|
config::Data,
|
|
|
|
fetch::object_id::ObjectId,
|
|
|
|
kinds::collection::OrderedCollectionType,
|
|
|
|
protocol::verification::verify_domains_match,
|
|
|
|
traits::Collection,
|
2022-06-02 14:33:41 +00:00
|
|
|
};
|
2023-03-21 15:03:05 +00:00
|
|
|
use lemmy_api_common::{context::LemmyContext, utils::generate_moderators_url};
|
2021-10-27 16:03:07 +00:00
|
|
|
use lemmy_db_schema::{
|
|
|
|
source::community::{CommunityModerator, CommunityModeratorForm},
|
|
|
|
traits::Joinable,
|
|
|
|
};
|
2022-05-03 17:44:13 +00:00
|
|
|
use lemmy_db_views_actor::structs::CommunityModeratorView;
|
2022-06-02 14:33:41 +00:00
|
|
|
use lemmy_utils::error::LemmyError;
|
2021-10-28 15:25:26 +00:00
|
|
|
use url::Url;
|
2021-10-27 16:03:07 +00:00
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub(crate) struct ApubCommunityModerators(pub(crate) Vec<CommunityModeratorView>);
|
|
|
|
|
2023-03-21 15:03:05 +00:00
|
|
|
#[async_trait::async_trait]
|
|
|
|
impl Collection for ApubCommunityModerators {
|
|
|
|
type Owner = ApubCommunity;
|
|
|
|
type DataType = LemmyContext;
|
|
|
|
type Kind = GroupModerators;
|
2022-06-02 14:33:41 +00:00
|
|
|
type Error = LemmyError;
|
2021-10-27 16:03:07 +00:00
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2023-03-21 15:03:05 +00:00
|
|
|
async fn read_local(
|
|
|
|
owner: &Self::Owner,
|
|
|
|
data: &Data<Self::DataType>,
|
|
|
|
) -> Result<Self::Kind, LemmyError> {
|
|
|
|
let moderators = CommunityModeratorView::for_community(data.pool(), owner.id).await?;
|
|
|
|
let ordered_items = moderators
|
2021-11-06 12:37:55 +00:00
|
|
|
.into_iter()
|
2023-03-21 15:03:05 +00:00
|
|
|
.map(|m| ObjectId::<ApubPerson>::from(m.moderator.actor_id))
|
2021-10-27 16:03:07 +00:00
|
|
|
.collect();
|
|
|
|
Ok(GroupModerators {
|
|
|
|
r#type: OrderedCollectionType::OrderedCollection,
|
2023-03-21 15:03:05 +00:00
|
|
|
id: generate_moderators_url(&owner.actor_id)?.into(),
|
2021-10-27 16:03:07 +00:00
|
|
|
ordered_items,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2021-11-06 17:35:14 +00:00
|
|
|
async fn verify(
|
|
|
|
group_moderators: &GroupModerators,
|
|
|
|
expected_domain: &Url,
|
2023-03-21 15:03:05 +00:00
|
|
|
_data: &Data<Self::DataType>,
|
2021-11-06 17:35:14 +00:00
|
|
|
) -> Result<(), LemmyError> {
|
|
|
|
verify_domains_match(&group_moderators.id, expected_domain)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2023-03-21 15:03:05 +00:00
|
|
|
async fn from_json(
|
|
|
|
apub: Self::Kind,
|
|
|
|
owner: &Self::Owner,
|
|
|
|
data: &Data<Self::DataType>,
|
2021-10-27 16:03:07 +00:00
|
|
|
) -> Result<Self, LemmyError> {
|
2023-03-21 15:03:05 +00:00
|
|
|
let community_id = owner.id;
|
2022-11-09 10:05:00 +00:00
|
|
|
let current_moderators =
|
2023-03-21 15:03:05 +00:00
|
|
|
CommunityModeratorView::for_community(data.pool(), community_id).await?;
|
2021-10-27 16:03:07 +00:00
|
|
|
// Remove old mods from database which arent in the moderators collection anymore
|
|
|
|
for mod_user in ¤t_moderators {
|
2023-03-21 15:03:05 +00:00
|
|
|
let mod_id = ObjectId::from(mod_user.moderator.actor_id.clone());
|
2021-10-27 16:03:07 +00:00
|
|
|
if !apub.ordered_items.contains(&mod_id) {
|
|
|
|
let community_moderator_form = CommunityModeratorForm {
|
|
|
|
community_id: mod_user.community.id,
|
|
|
|
person_id: mod_user.moderator.id,
|
|
|
|
};
|
2023-03-21 15:03:05 +00:00
|
|
|
CommunityModerator::leave(data.pool(), &community_moderator_form).await?;
|
2021-10-27 16:03:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add new mods to database which have been added to moderators collection
|
2021-11-06 12:37:55 +00:00
|
|
|
for mod_id in apub.ordered_items {
|
2023-03-21 15:03:05 +00:00
|
|
|
let mod_user: ApubPerson = mod_id.dereference(data).await?;
|
2021-10-27 16:03:07 +00:00
|
|
|
|
|
|
|
if !current_moderators
|
|
|
|
.iter()
|
|
|
|
.map(|c| c.moderator.actor_id.clone())
|
|
|
|
.any(|x| x == mod_user.actor_id)
|
|
|
|
{
|
|
|
|
let community_moderator_form = CommunityModeratorForm {
|
2023-03-21 15:03:05 +00:00
|
|
|
community_id: owner.id,
|
2021-10-27 16:03:07 +00:00
|
|
|
person_id: mod_user.id,
|
|
|
|
};
|
2023-03-21 15:03:05 +00:00
|
|
|
CommunityModerator::join(data.pool(), &community_moderator_form).await?;
|
2021-10-27 16:03:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This return value is unused, so just set an empty vec
|
2022-03-30 14:58:03 +00:00
|
|
|
Ok(ApubCommunityModerators(Vec::new()))
|
2021-10-27 16:03:07 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-01 13:05:20 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2022-02-17 22:04:01 +00:00
|
|
|
use crate::{
|
|
|
|
objects::{
|
|
|
|
community::tests::parse_lemmy_community,
|
|
|
|
person::tests::parse_lemmy_person,
|
|
|
|
tests::init_context,
|
|
|
|
},
|
|
|
|
protocol::tests::file_to_json_object,
|
2021-11-01 13:05:20 +00:00
|
|
|
};
|
|
|
|
use lemmy_db_schema::{
|
|
|
|
source::{
|
|
|
|
community::Community,
|
2022-10-27 09:24:07 +00:00
|
|
|
instance::Instance,
|
|
|
|
person::{Person, PersonInsertForm},
|
2022-02-07 19:23:12 +00:00
|
|
|
site::Site,
|
2021-11-01 13:05:20 +00:00
|
|
|
},
|
|
|
|
traits::Crud,
|
|
|
|
};
|
|
|
|
use serial_test::serial;
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
#[serial]
|
|
|
|
async fn test_parse_lemmy_community_moderators() {
|
2022-11-09 10:05:00 +00:00
|
|
|
let context = init_context().await;
|
2022-02-07 19:23:12 +00:00
|
|
|
let (new_mod, site) = parse_lemmy_person(&context).await;
|
2021-11-01 13:05:20 +00:00
|
|
|
let community = parse_lemmy_community(&context).await;
|
|
|
|
let community_id = community.id;
|
|
|
|
|
2023-03-01 02:36:57 +00:00
|
|
|
let inserted_instance = Instance::read_or_create(context.pool(), "my_domain.tld".to_string())
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
|
|
|
.unwrap();
|
2022-10-27 09:24:07 +00:00
|
|
|
|
|
|
|
let old_mod = PersonInsertForm::builder()
|
|
|
|
.name("holly".into())
|
|
|
|
.public_key("pubkey".to_string())
|
|
|
|
.instance_id(inserted_instance.id)
|
|
|
|
.build();
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
let old_mod = Person::create(context.pool(), &old_mod).await.unwrap();
|
2021-11-01 13:05:20 +00:00
|
|
|
let community_moderator_form = CommunityModeratorForm {
|
|
|
|
community_id: community.id,
|
|
|
|
person_id: old_mod.id,
|
|
|
|
};
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
CommunityModerator::join(context.pool(), &community_moderator_form)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2021-11-01 13:05:20 +00:00
|
|
|
|
2022-02-07 19:23:12 +00:00
|
|
|
assert_eq!(site.actor_id.to_string(), "https://enterprise.lemmy.ml/");
|
2021-11-01 13:05:20 +00:00
|
|
|
|
|
|
|
let json: GroupModerators =
|
2022-01-17 14:40:47 +00:00
|
|
|
file_to_json_object("assets/lemmy/collections/group_moderators.json").unwrap();
|
2021-11-01 13:05:20 +00:00
|
|
|
let url = Url::parse("https://enterprise.lemmy.ml/c/tenforward").unwrap();
|
2023-03-21 15:03:05 +00:00
|
|
|
ApubCommunityModerators::verify(&json, &url, &context)
|
2021-11-06 17:35:14 +00:00
|
|
|
.await
|
|
|
|
.unwrap();
|
2023-03-21 15:03:05 +00:00
|
|
|
ApubCommunityModerators::from_json(json, &community, &context)
|
2021-11-01 13:05:20 +00:00
|
|
|
.await
|
|
|
|
.unwrap();
|
2023-03-21 15:03:05 +00:00
|
|
|
assert_eq!(context.request_count(), 0);
|
2021-11-01 13:05:20 +00:00
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
let current_moderators = CommunityModeratorView::for_community(context.pool(), community_id)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2021-11-01 13:05:20 +00:00
|
|
|
|
|
|
|
assert_eq!(current_moderators.len(), 1);
|
|
|
|
assert_eq!(current_moderators[0].moderator.id, new_mod.id);
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
Person::delete(context.pool(), old_mod.id).await.unwrap();
|
|
|
|
Person::delete(context.pool(), new_mod.id).await.unwrap();
|
2023-03-21 15:03:05 +00:00
|
|
|
Community::delete(context.pool(), community.id)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
Site::delete(context.pool(), site.id).await.unwrap();
|
|
|
|
Instance::delete(context.pool(), inserted_instance.id)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2021-11-01 13:05:20 +00:00
|
|
|
}
|
|
|
|
}
|