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;
|
2024-04-10 14:14:11 +00:00
|
|
|
use lemmy_utils::error::{LemmyError, LemmyResult};
|
2021-10-28 15:25:26 +00:00
|
|
|
use url::Url;
|
2021-10-27 16:03:07 +00:00
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
2024-01-24 09:34:09 +00:00
|
|
|
pub(crate) struct ApubCommunityModerators(());
|
2021-10-27 16:03:07 +00:00
|
|
|
|
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)]
|
2024-04-10 14:14:11 +00:00
|
|
|
async fn read_local(owner: &Self::Owner, data: &Data<Self::DataType>) -> LemmyResult<Self::Kind> {
|
2023-07-11 13:09:59 +00:00
|
|
|
let moderators = CommunityModeratorView::for_community(&mut data.pool(), owner.id).await?;
|
2023-03-21 15:03:05 +00:00
|
|
|
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>,
|
2024-04-10 14:14:11 +00:00
|
|
|
) -> LemmyResult<()> {
|
2021-11-06 17:35:14 +00:00
|
|
|
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>,
|
2024-04-10 14:14:11 +00:00
|
|
|
) -> LemmyResult<Self> {
|
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-07-11 13:09:59 +00:00
|
|
|
CommunityModeratorView::for_community(&mut 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-07-11 13:09:59 +00:00
|
|
|
CommunityModerator::leave(&mut 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-07-20 15:34:07 +00:00
|
|
|
// Ignore errors as mod accounts might be deleted or instances unavailable.
|
|
|
|
let mod_user: Option<ApubPerson> = mod_id.dereference(data).await.ok();
|
|
|
|
if let Some(mod_user) = mod_user {
|
|
|
|
if !current_moderators
|
|
|
|
.iter()
|
|
|
|
.map(|c| c.moderator.actor_id.clone())
|
|
|
|
.any(|x| x == mod_user.actor_id)
|
|
|
|
{
|
|
|
|
let community_moderator_form = CommunityModeratorForm {
|
|
|
|
community_id: owner.id,
|
|
|
|
person_id: mod_user.id,
|
|
|
|
};
|
|
|
|
CommunityModerator::join(&mut 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
|
2024-01-24 09:34:09 +00:00
|
|
|
Ok(ApubCommunityModerators(()))
|
2021-10-27 16:03:07 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-01 13:05:20 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
2024-09-24 17:29:02 +00:00
|
|
|
#[expect(clippy::indexing_slicing)]
|
2021-11-01 13:05:20 +00:00
|
|
|
mod tests {
|
2023-07-17 15:04:14 +00:00
|
|
|
|
2021-11-01 13:05:20 +00:00
|
|
|
use super::*;
|
2022-02-17 22:04:01 +00:00
|
|
|
use crate::{
|
2024-01-25 14:22:11 +00:00
|
|
|
objects::{community::tests::parse_lemmy_community, person::tests::parse_lemmy_person},
|
2022-02-17 22:04:01 +00:00
|
|
|
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,
|
|
|
|
};
|
2024-01-04 09:47:18 +00:00
|
|
|
use pretty_assertions::assert_eq;
|
2021-11-01 13:05:20 +00:00
|
|
|
use serial_test::serial;
|
|
|
|
|
2023-06-26 08:24:11 +00:00
|
|
|
#[tokio::test]
|
2021-11-01 13:05:20 +00:00
|
|
|
#[serial]
|
2023-11-17 03:51:33 +00:00
|
|
|
async fn test_parse_lemmy_community_moderators() -> LemmyResult<()> {
|
2024-01-25 14:22:11 +00:00
|
|
|
let context = LemmyContext::init_test_context().await;
|
2023-11-17 03:51:33 +00:00
|
|
|
let (new_mod, site) = parse_lemmy_person(&context).await?;
|
|
|
|
let community = parse_lemmy_community(&context).await?;
|
2021-11-01 13:05:20 +00:00
|
|
|
let community_id = community.id;
|
|
|
|
|
2023-07-11 13:09:59 +00:00
|
|
|
let inserted_instance =
|
2023-11-17 03:51:33 +00:00
|
|
|
Instance::read_or_create(&mut context.pool(), "my_domain.tld".to_string()).await?;
|
2022-10-27 09:24:07 +00:00
|
|
|
|
2024-06-06 12:29:18 +00:00
|
|
|
let old_mod = PersonInsertForm::test_form(inserted_instance.id, "holly");
|
2022-10-27 09:24:07 +00:00
|
|
|
|
2023-11-17 03:51:33 +00:00
|
|
|
let old_mod = Person::create(&mut context.pool(), &old_mod).await?;
|
2021-11-01 13:05:20 +00:00
|
|
|
let community_moderator_form = CommunityModeratorForm {
|
|
|
|
community_id: community.id,
|
|
|
|
person_id: old_mod.id,
|
|
|
|
};
|
|
|
|
|
2023-11-17 03:51:33 +00:00
|
|
|
CommunityModerator::join(&mut context.pool(), &community_moderator_form).await?;
|
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 =
|
2023-11-17 03:51:33 +00:00
|
|
|
file_to_json_object("assets/lemmy/collections/group_moderators.json")?;
|
|
|
|
let url = Url::parse("https://enterprise.lemmy.ml/c/tenforward")?;
|
|
|
|
ApubCommunityModerators::verify(&json, &url, &context).await?;
|
|
|
|
ApubCommunityModerators::from_json(json, &community, &context).await?;
|
2023-03-21 15:03:05 +00:00
|
|
|
assert_eq!(context.request_count(), 0);
|
2021-11-01 13:05:20 +00:00
|
|
|
|
2023-07-11 13:09:59 +00:00
|
|
|
let current_moderators =
|
2023-11-17 03:51:33 +00:00
|
|
|
CommunityModeratorView::for_community(&mut context.pool(), community_id).await?;
|
2021-11-01 13:05:20 +00:00
|
|
|
|
|
|
|
assert_eq!(current_moderators.len(), 1);
|
|
|
|
assert_eq!(current_moderators[0].moderator.id, new_mod.id);
|
|
|
|
|
2023-11-17 03:51:33 +00:00
|
|
|
Person::delete(&mut context.pool(), old_mod.id).await?;
|
|
|
|
Person::delete(&mut context.pool(), new_mod.id).await?;
|
|
|
|
Community::delete(&mut context.pool(), community.id).await?;
|
|
|
|
Site::delete(&mut context.pool(), site.id).await?;
|
|
|
|
Instance::delete(&mut context.pool(), inserted_instance.id).await?;
|
|
|
|
Ok(())
|
2021-11-01 13:05:20 +00:00
|
|
|
}
|
|
|
|
}
|