2021-10-27 16:03:07 +00:00
|
|
|
use crate::{
|
|
|
|
collections::CommunityContext,
|
|
|
|
generate_moderators_url,
|
2022-06-02 14:33:41 +00:00
|
|
|
local_instance,
|
2021-10-27 16:03:07 +00:00
|
|
|
objects::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::{
|
|
|
|
core::object_id::ObjectId,
|
|
|
|
traits::ApubObject,
|
|
|
|
utils::verify_domains_match,
|
|
|
|
};
|
2021-11-19 17:47:06 +00:00
|
|
|
use activitystreams_kinds::collection::OrderedCollectionType;
|
|
|
|
use chrono::NaiveDateTime;
|
2022-05-03 17:44:13 +00:00
|
|
|
use lemmy_api_common::utils::blocking;
|
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>);
|
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl ApubObject for ApubCommunityModerators {
|
|
|
|
type DataType = CommunityContext;
|
|
|
|
type ApubType = GroupModerators;
|
2022-06-02 14:33:41 +00:00
|
|
|
type Error = LemmyError;
|
2021-10-27 16:03:07 +00:00
|
|
|
|
|
|
|
fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2021-10-27 16:03:07 +00:00
|
|
|
async fn read_from_apub_id(
|
|
|
|
_object_id: Url,
|
|
|
|
data: &Self::DataType,
|
|
|
|
) -> Result<Option<Self>, LemmyError> {
|
|
|
|
// Only read from database if its a local community, otherwise fetch over http
|
|
|
|
if data.0.local {
|
|
|
|
let cid = data.0.id;
|
|
|
|
let moderators = blocking(data.1.pool(), move |conn| {
|
|
|
|
CommunityModeratorView::for_community(conn, cid)
|
|
|
|
})
|
|
|
|
.await??;
|
2022-03-30 14:58:03 +00:00
|
|
|
Ok(Some(ApubCommunityModerators(moderators)))
|
2021-10-27 16:03:07 +00:00
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2021-10-27 16:03:07 +00:00
|
|
|
async fn delete(self, _data: &Self::DataType) -> Result<(), LemmyError> {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2021-11-06 12:37:55 +00:00
|
|
|
async fn into_apub(self, data: &Self::DataType) -> Result<Self::ApubType, LemmyError> {
|
2021-10-27 16:03:07 +00:00
|
|
|
let ordered_items = self
|
|
|
|
.0
|
2021-11-06 12:37:55 +00:00
|
|
|
.into_iter()
|
|
|
|
.map(|m| ObjectId::<ApubPerson>::new(m.moderator.actor_id))
|
2021-10-27 16:03:07 +00:00
|
|
|
.collect();
|
|
|
|
Ok(GroupModerators {
|
|
|
|
r#type: OrderedCollectionType::OrderedCollection,
|
|
|
|
id: generate_moderators_url(&data.0.actor_id)?.into(),
|
|
|
|
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,
|
|
|
|
_context: &CommunityContext,
|
|
|
|
_request_counter: &mut i32,
|
|
|
|
) -> Result<(), LemmyError> {
|
|
|
|
verify_domains_match(&group_moderators.id, expected_domain)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2021-10-27 16:03:07 +00:00
|
|
|
async fn from_apub(
|
2021-11-06 12:37:55 +00:00
|
|
|
apub: Self::ApubType,
|
2021-10-27 16:03:07 +00:00
|
|
|
data: &Self::DataType,
|
|
|
|
request_counter: &mut i32,
|
|
|
|
) -> Result<Self, LemmyError> {
|
|
|
|
let community_id = data.0.id;
|
|
|
|
let current_moderators = blocking(data.1.pool(), move |conn| {
|
|
|
|
CommunityModeratorView::for_community(conn, community_id)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
// Remove old mods from database which arent in the moderators collection anymore
|
|
|
|
for mod_user in ¤t_moderators {
|
2021-11-05 00:24:10 +00:00
|
|
|
let mod_id = ObjectId::new(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,
|
|
|
|
};
|
|
|
|
blocking(data.1.pool(), move |conn| {
|
|
|
|
CommunityModerator::leave(conn, &community_moderator_form)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
let mod_id = ObjectId::new(mod_id);
|
2021-12-06 22:54:34 +00:00
|
|
|
let mod_user: ApubPerson = mod_id
|
2022-06-02 14:33:41 +00:00
|
|
|
.dereference::<LemmyError>(&data.1, local_instance(&data.1), request_counter)
|
2021-12-06 22:54:34 +00:00
|
|
|
.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 {
|
|
|
|
community_id: data.0.id,
|
|
|
|
person_id: mod_user.id,
|
|
|
|
};
|
|
|
|
blocking(data.1.pool(), move |conn| {
|
|
|
|
CommunityModerator::join(conn, &community_moderator_form)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
2022-03-23 21:27:51 +00:00
|
|
|
|
|
|
|
type DbType = ();
|
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,
|
|
|
|
person::{Person, PersonForm},
|
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-03-03 18:54:33 +00:00
|
|
|
let context = init_context();
|
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;
|
|
|
|
|
|
|
|
let old_mod = PersonForm {
|
|
|
|
name: "holly".into(),
|
|
|
|
..PersonForm::default()
|
|
|
|
};
|
|
|
|
let old_mod = Person::create(&context.pool().get().unwrap(), &old_mod).unwrap();
|
|
|
|
let community_moderator_form = CommunityModeratorForm {
|
|
|
|
community_id: community.id,
|
|
|
|
person_id: old_mod.id,
|
|
|
|
};
|
|
|
|
|
|
|
|
CommunityModerator::join(&context.pool().get().unwrap(), &community_moderator_form).unwrap();
|
|
|
|
|
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();
|
|
|
|
let mut request_counter = 0;
|
2022-05-06 23:53:33 +00:00
|
|
|
let community_context = CommunityContext(community, context);
|
2021-11-06 17:35:14 +00:00
|
|
|
ApubCommunityModerators::verify(&json, &url, &community_context, &mut request_counter)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
ApubCommunityModerators::from_apub(json, &community_context, &mut request_counter)
|
2021-11-01 13:05:20 +00:00
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(request_counter, 0);
|
|
|
|
|
|
|
|
let current_moderators = blocking(community_context.1.pool(), move |conn| {
|
|
|
|
CommunityModeratorView::for_community(conn, community_id)
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(current_moderators.len(), 1);
|
|
|
|
assert_eq!(current_moderators[0].moderator.id, new_mod.id);
|
|
|
|
|
|
|
|
Person::delete(&*community_context.1.pool().get().unwrap(), old_mod.id).unwrap();
|
|
|
|
Person::delete(&*community_context.1.pool().get().unwrap(), new_mod.id).unwrap();
|
|
|
|
Community::delete(
|
|
|
|
&*community_context.1.pool().get().unwrap(),
|
|
|
|
community_context.0.id,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2022-02-07 19:23:12 +00:00
|
|
|
Site::delete(&*community_context.1.pool().get().unwrap(), site.id).unwrap();
|
2021-11-01 13:05:20 +00:00
|
|
|
}
|
|
|
|
}
|