mirror of
https://github.com/LemmyNet/lemmy.git
synced 2025-01-10 12:05:57 +00:00
5d837780f5
* Moving settings to Database. - Moves many settings into the database. Fixes #2285 - Adds a local_site and instance table. Fixes #2365 . Fixes #2368 - Separates SQL update an insert forms, to avoid runtime errors. - Adds TypedBuilder to all the SQL forms, instead of default. * Fix weird clippy issue. * Removing extra lines. * Some fixes from suggestions. * Fixing apub tests. * Using instance creation helper function. * Move forms to their own line. * Trying to fix local_site_data, still broken. * Testing out async * Testing out async 2 * Fixing federation tests. * Trying to fix check features 1. * Starting on adding diesel async. 1/4th done. * Added async to views and schema. * Adding some more async * Compiling now. * Added diesel async. Fixes #2465 * Running clippy --fix * Trying to fix cargo test on drone. * Trying new muslrust. * Trying a custom dns * Trying a custom dns 2 * Trying a custom dns 3 * Trying a custom dns 4 * Trying a custom dns 5 * Trying a custom dns 6 * Trying a custom dns 7 * Addressing PR comments. * Adding check_apub to all verify functions. * Reverting back drone. * Fixing merge * Fix docker images. * Adding missing discussion_languages. * Trying to fix federation tests. * Fix site setup user creation. * Fix clippy * Fix clippy 2 * Test api faster * Try to fix 1 * Try to fix 2 * What are these lines about * Trying to fix 3 * Moving federation test back to top. * Remove logging cat.
211 lines
6.4 KiB
Rust
211 lines
6.4 KiB
Rust
use crate::{
|
|
collections::CommunityContext,
|
|
generate_moderators_url,
|
|
local_instance,
|
|
objects::person::ApubPerson,
|
|
protocol::collections::group_moderators::GroupModerators,
|
|
};
|
|
use activitypub_federation::{
|
|
core::object_id::ObjectId,
|
|
traits::ApubObject,
|
|
utils::verify_domains_match,
|
|
};
|
|
use activitystreams_kinds::collection::OrderedCollectionType;
|
|
use chrono::NaiveDateTime;
|
|
use lemmy_db_schema::{
|
|
source::community::{CommunityModerator, CommunityModeratorForm},
|
|
traits::Joinable,
|
|
};
|
|
use lemmy_db_views_actor::structs::CommunityModeratorView;
|
|
use lemmy_utils::error::LemmyError;
|
|
use url::Url;
|
|
|
|
#[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;
|
|
type Error = LemmyError;
|
|
|
|
fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
|
|
None
|
|
}
|
|
|
|
#[tracing::instrument(skip_all)]
|
|
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 = CommunityModeratorView::for_community(data.1.pool(), cid).await?;
|
|
Ok(Some(ApubCommunityModerators(moderators)))
|
|
} else {
|
|
Ok(None)
|
|
}
|
|
}
|
|
|
|
#[tracing::instrument(skip_all)]
|
|
async fn delete(self, _data: &Self::DataType) -> Result<(), LemmyError> {
|
|
unimplemented!()
|
|
}
|
|
|
|
#[tracing::instrument(skip_all)]
|
|
async fn into_apub(self, data: &Self::DataType) -> Result<Self::ApubType, LemmyError> {
|
|
let ordered_items = self
|
|
.0
|
|
.into_iter()
|
|
.map(|m| ObjectId::<ApubPerson>::new(m.moderator.actor_id))
|
|
.collect();
|
|
Ok(GroupModerators {
|
|
r#type: OrderedCollectionType::OrderedCollection,
|
|
id: generate_moderators_url(&data.0.actor_id)?.into(),
|
|
ordered_items,
|
|
})
|
|
}
|
|
|
|
#[tracing::instrument(skip_all)]
|
|
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(())
|
|
}
|
|
|
|
#[tracing::instrument(skip_all)]
|
|
async fn from_apub(
|
|
apub: Self::ApubType,
|
|
data: &Self::DataType,
|
|
request_counter: &mut i32,
|
|
) -> Result<Self, LemmyError> {
|
|
let community_id = data.0.id;
|
|
let current_moderators =
|
|
CommunityModeratorView::for_community(data.1.pool(), community_id).await?;
|
|
// Remove old mods from database which arent in the moderators collection anymore
|
|
for mod_user in ¤t_moderators {
|
|
let mod_id = ObjectId::new(mod_user.moderator.actor_id.clone());
|
|
if !apub.ordered_items.contains(&mod_id) {
|
|
let community_moderator_form = CommunityModeratorForm {
|
|
community_id: mod_user.community.id,
|
|
person_id: mod_user.moderator.id,
|
|
};
|
|
CommunityModerator::leave(data.1.pool(), &community_moderator_form).await?;
|
|
}
|
|
}
|
|
|
|
// Add new mods to database which have been added to moderators collection
|
|
for mod_id in apub.ordered_items {
|
|
let mod_id = ObjectId::new(mod_id);
|
|
let mod_user: ApubPerson = mod_id
|
|
.dereference(&data.1, local_instance(&data.1).await, request_counter)
|
|
.await?;
|
|
|
|
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,
|
|
};
|
|
CommunityModerator::join(data.1.pool(), &community_moderator_form).await?;
|
|
}
|
|
}
|
|
|
|
// This return value is unused, so just set an empty vec
|
|
Ok(ApubCommunityModerators(Vec::new()))
|
|
}
|
|
|
|
type DbType = ();
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::{
|
|
objects::{
|
|
community::tests::parse_lemmy_community,
|
|
person::tests::parse_lemmy_person,
|
|
tests::init_context,
|
|
},
|
|
protocol::tests::file_to_json_object,
|
|
};
|
|
use lemmy_db_schema::{
|
|
source::{
|
|
community::Community,
|
|
instance::Instance,
|
|
person::{Person, PersonInsertForm},
|
|
site::Site,
|
|
},
|
|
traits::Crud,
|
|
};
|
|
use serial_test::serial;
|
|
|
|
#[actix_rt::test]
|
|
#[serial]
|
|
async fn test_parse_lemmy_community_moderators() {
|
|
let context = init_context().await;
|
|
let (new_mod, site) = parse_lemmy_person(&context).await;
|
|
let community = parse_lemmy_community(&context).await;
|
|
let community_id = community.id;
|
|
|
|
let inserted_instance = Instance::create(context.pool(), "my_domain.tld")
|
|
.await
|
|
.unwrap();
|
|
|
|
let old_mod = PersonInsertForm::builder()
|
|
.name("holly".into())
|
|
.public_key("pubkey".to_string())
|
|
.instance_id(inserted_instance.id)
|
|
.build();
|
|
|
|
let old_mod = Person::create(context.pool(), &old_mod).await.unwrap();
|
|
let community_moderator_form = CommunityModeratorForm {
|
|
community_id: community.id,
|
|
person_id: old_mod.id,
|
|
};
|
|
|
|
CommunityModerator::join(context.pool(), &community_moderator_form)
|
|
.await
|
|
.unwrap();
|
|
|
|
assert_eq!(site.actor_id.to_string(), "https://enterprise.lemmy.ml/");
|
|
|
|
let json: GroupModerators =
|
|
file_to_json_object("assets/lemmy/collections/group_moderators.json").unwrap();
|
|
let url = Url::parse("https://enterprise.lemmy.ml/c/tenforward").unwrap();
|
|
let mut request_counter = 0;
|
|
let community_context = CommunityContext(community, context.to_owned());
|
|
ApubCommunityModerators::verify(&json, &url, &community_context, &mut request_counter)
|
|
.await
|
|
.unwrap();
|
|
ApubCommunityModerators::from_apub(json, &community_context, &mut request_counter)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(request_counter, 0);
|
|
|
|
let current_moderators = CommunityModeratorView::for_community(context.pool(), community_id)
|
|
.await
|
|
.unwrap();
|
|
|
|
assert_eq!(current_moderators.len(), 1);
|
|
assert_eq!(current_moderators[0].moderator.id, new_mod.id);
|
|
|
|
Person::delete(context.pool(), old_mod.id).await.unwrap();
|
|
Person::delete(context.pool(), new_mod.id).await.unwrap();
|
|
Community::delete(context.pool(), community_context.0.id)
|
|
.await
|
|
.unwrap();
|
|
Site::delete(context.pool(), site.id).await.unwrap();
|
|
Instance::delete(context.pool(), inserted_instance.id)
|
|
.await
|
|
.unwrap();
|
|
}
|
|
}
|