Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions crates/api/api/src/federation/user_settings_backup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ pub async fn import_settings(
.blocked_instances_communities
.iter()
.map(|domain| async {
let instance = Instance::read_or_create(&mut context.pool(), domain.clone()).await?;
let instance = Instance::read_or_create(&mut context.pool(), domain).await?;
let form = InstanceCommunitiesBlockForm::new(person_id, instance.id);
InstanceActions::block_communities(&mut context.pool(), &form).await?;
LemmyResult::Ok(())
Expand All @@ -240,7 +240,7 @@ pub async fn import_settings(
.await?;

try_join_all(data.blocked_instances_persons.iter().map(|domain| async {
let instance = Instance::read_or_create(&mut context.pool(), domain.clone()).await?;
let instance = Instance::read_or_create(&mut context.pool(), domain).await?;
let form = InstancePersonsBlockForm::new(person_id, instance.id);
InstanceActions::block_persons(&mut context.pool(), &form).await?;
LemmyResult::Ok(())
Expand Down
15 changes: 7 additions & 8 deletions crates/api/api/src/site/admin_allow_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,25 @@ use lemmy_db_schema::{
traits::Crud,
};
use lemmy_db_views_local_user::LocalUserView;
use lemmy_db_views_site::api::{AdminAllowInstanceParams, SuccessResponse};
use lemmy_db_views_site::{api::AdminAllowInstanceParams, FederatedInstanceView};
use lemmy_utils::error::{LemmyErrorType, LemmyResult};

pub async fn admin_allow_instance(
data: Json<AdminAllowInstanceParams>,
local_user_view: LocalUserView,
context: Data<LemmyContext>,
) -> LemmyResult<Json<SuccessResponse>> {
) -> LemmyResult<Json<FederatedInstanceView>> {
is_admin(&local_user_view)?;

let blocklist = Instance::blocklist(&mut context.pool()).await?;
if !blocklist.is_empty() {
Err(LemmyErrorType::CannotCombineFederationBlocklistAndAllowlist)?;
}

let instance_id = Instance::read_or_create(&mut context.pool(), data.instance.clone())
let instance_id = Instance::read_or_create(&mut context.pool(), &data.instance)
.await?
.id;
let form = FederationAllowListForm {
instance_id,
updated_at: None,
};
let form = FederationAllowListForm::new(instance_id);
if data.allow {
FederationAllowList::allow(&mut context.pool(), &form).await?;
} else {
Expand All @@ -46,5 +43,7 @@ pub async fn admin_allow_instance(
};
AdminAllowInstance::create(&mut context.pool(), &mod_log_form).await?;

Ok(Json(SuccessResponse::default()))
Ok(Json(
FederatedInstanceView::read(&mut context.pool(), instance_id).await?,
))
}
16 changes: 7 additions & 9 deletions crates/api/api/src/site/admin_block_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ use lemmy_db_schema::{
traits::Crud,
};
use lemmy_db_views_local_user::LocalUserView;
use lemmy_db_views_site::api::{AdminBlockInstanceParams, SuccessResponse};
use lemmy_db_views_site::{api::AdminBlockInstanceParams, FederatedInstanceView};
use lemmy_utils::error::{LemmyErrorType, LemmyResult};

pub async fn admin_block_instance(
data: Json<AdminBlockInstanceParams>,
local_user_view: LocalUserView,
context: Data<LemmyContext>,
) -> LemmyResult<Json<SuccessResponse>> {
) -> LemmyResult<Json<FederatedInstanceView>> {
is_admin(&local_user_view)?;

let expires_at = check_expire_time(data.expires_at)?;
Expand All @@ -30,15 +30,11 @@ pub async fn admin_block_instance(
Err(LemmyErrorType::CannotCombineFederationBlocklistAndAllowlist)?;
}

let instance_id = Instance::read_or_create(&mut context.pool(), data.instance.clone())
let instance_id = Instance::read_or_create(&mut context.pool(), &data.instance)
.await?
.id;

let form = FederationBlockListForm {
instance_id,
expires_at,
updated_at: None,
};
let form = FederationBlockListForm::new(instance_id, expires_at);

if data.block {
FederationBlockList::block(&mut context.pool(), &form).await?;
Expand All @@ -54,5 +50,7 @@ pub async fn admin_block_instance(
};
AdminBlockInstance::create(&mut context.pool(), &mod_log_form).await?;

Ok(Json(SuccessResponse::default()))
Ok(Json(
FederatedInstanceView::read(&mut context.pool(), instance_id).await?,
))
}
24 changes: 19 additions & 5 deletions crates/api/api/src/site/federated_instances.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
use actix_web::web::{Data, Json};
use lemmy_api_utils::{context::LemmyContext, utils::build_federated_instances};
use lemmy_db_views_site::{api::GetFederatedInstancesResponse, SiteView};
use actix_web::web::{Data, Json, Query};
use lemmy_api_utils::context::LemmyContext;
use lemmy_db_schema::traits::PaginationCursorBuilder;
use lemmy_db_views_site::{
api::{GetFederatedInstances, GetFederatedInstancesResponse},
FederatedInstanceView,
};
use lemmy_utils::error::LemmyResult;

pub async fn get_federated_instances(
data: Query<GetFederatedInstances>,
context: Data<LemmyContext>,
) -> LemmyResult<Json<GetFederatedInstancesResponse>> {
let site_view = SiteView::read_local(&mut context.pool()).await?;
let federated_instances =
build_federated_instances(&site_view.local_site, &mut context.pool()).await?;
FederatedInstanceView::list(&mut context.pool(), data.into_inner()).await?;

let next_page = federated_instances
.last()
.map(PaginationCursorBuilder::to_cursor);
let prev_page = federated_instances
.first()
.map(PaginationCursorBuilder::to_cursor);

// Return the jwt
Ok(Json(GetFederatedInstancesResponse {
federated_instances,
next_page,
prev_page,
}))
}
2 changes: 1 addition & 1 deletion crates/api/api/src/site/mod_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ mod tests {
let context = LemmyContext::init_test_context().await;
let pool = &mut context.pool();

let instance = Instance::read_or_create(pool, "my_domain.tld".to_string()).await?;
let instance = Instance::read_or_create(pool, "my_domain.tld").await?;

// John is the mod
let john = PersonInsertForm::test_form(instance.id, "john the modder");
Expand Down
3 changes: 2 additions & 1 deletion crates/api/api_common/src/federation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ pub use lemmy_db_schema::{
pub use lemmy_db_schema_file::enums::FederationMode;
pub use lemmy_db_views_readable_federation_state::ReadableFederationState;
pub use lemmy_db_views_site::api::{
FederatedInstances,
GetFederatedInstances,
GetFederatedInstancesKind,
GetFederatedInstancesResponse,
InstanceWithFederationState,
ResolveObject,
Expand Down
2 changes: 1 addition & 1 deletion crates/api/api_utils/src/claims.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ mod tests {
let context = LemmyContext::init_test_context().await;
let pool = &mut context.pool();

let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string()).await?;
let inserted_instance = Instance::read_or_create(pool, "my_domain.tld").await?;

let new_person = PersonInsertForm::test_form(inserted_instance.id, "Gerry9812");

Expand Down
2 changes: 1 addition & 1 deletion crates/api/api_utils/src/notify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ mod tests {
}

async fn init_data(pool: &mut DbPool<'_>) -> LemmyResult<Data> {
let instance = Instance::read_or_create(pool, "lemmy-alpha".to_string()).await?;
let instance = Instance::read_or_create(pool, "lemmy-alpha").await?;

let timmy = LocalUserView::create_test_user(pool, "timmy_pcv", "", false).await?;

Expand Down
45 changes: 2 additions & 43 deletions crates/api/api_utils/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use lemmy_db_schema::{
comment::{Comment, CommentActions},
community::{Community, CommunityActions, CommunityUpdateForm},
images::{ImageDetails, RemoteImage},
instance::{Instance, InstanceActions},
instance::InstanceActions,
local_site::LocalSite,
local_site_rate_limit::LocalSiteRateLimit,
local_site_url_blocklist::LocalSiteUrlBlocklist,
Expand All @@ -40,10 +40,7 @@ use lemmy_db_views_community_moderator::CommunityModeratorView;
use lemmy_db_views_community_person_ban::CommunityPersonBanView;
use lemmy_db_views_local_image::LocalImageView;
use lemmy_db_views_local_user::LocalUserView;
use lemmy_db_views_site::{
api::{FederatedInstances, InstanceWithFederationState},
SiteView,
};
use lemmy_db_views_site::SiteView;
use lemmy_utils::{
error::{
LemmyError,
Expand Down Expand Up @@ -366,44 +363,6 @@ pub fn check_private_messages_enabled(local_user_view: &LocalUserView) -> Result
}
}

pub async fn build_federated_instances(
local_site: &LocalSite,
pool: &mut DbPool<'_>,
) -> LemmyResult<Option<FederatedInstances>> {
if local_site.federation_enabled {
let mut linked = Vec::new();
let mut allowed = Vec::new();
let mut blocked = Vec::new();

let all = Instance::read_all_with_fed_state(pool).await?;
for (instance, federation_state, is_blocked, is_allowed) in all {
let i = InstanceWithFederationState {
instance,
federation_state: federation_state.map(std::convert::Into::into),
};
if is_blocked {
// blocked instances will only have an entry here if they had been federated with in the
// past.
blocked.push(i);
} else if is_allowed {
allowed.push(i.clone());
linked.push(i);
} else {
// not explicitly allowed but implicitly linked
linked.push(i);
}
}

Ok(Some(FederatedInstances {
linked,
allowed,
blocked,
}))
} else {
Ok(None)
}
}

/// Checks the password length
pub fn password_length_check(pass: &str) -> LemmyResult<()> {
if !(10..=60).contains(&pass.chars().count()) {
Expand Down
3 changes: 1 addition & 2 deletions crates/apub/apub/src/collections/community_moderators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ mod tests {
let community = parse_lemmy_community(&context).await?;
let community_id = community.id;

let inserted_instance =
Instance::read_or_create(&mut context.pool(), "my_domain.tld".to_string()).await?;
let inserted_instance = Instance::read_or_create(&mut context.pool(), "my_domain.tld").await?;

let old_mod = PersonInsertForm::test_form(inserted_instance.id, "holly");

Expand Down
4 changes: 2 additions & 2 deletions crates/apub/objects/src/objects/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ impl Object for ApubSite {
.inner()
.domain()
.ok_or(UntranslatedError::UrlWithoutDomain)?;
let instance = DbInstance::read_or_create(&mut context.pool(), domain.to_string()).await?;
let instance = DbInstance::read_or_create(&mut context.pool(), domain).await?;

let slur_regex = slur_regex(context).await?;
let url_blocklist = get_url_blocklist(context).await?;
Expand Down Expand Up @@ -210,7 +210,7 @@ pub(crate) async fn fetch_instance_actor_for_object<T: Into<Url> + Clone>(
.domain()
.ok_or(UntranslatedError::UrlWithoutDomain)?;
Ok(
DbInstance::read_or_create(&mut context.pool(), domain.to_string())
DbInstance::read_or_create(&mut context.pool(), domain)
.await?
.id,
)
Expand Down
17 changes: 5 additions & 12 deletions crates/apub/send/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,9 @@ mod test {
};
let pool = &mut context.pool();
let instances = vec![
Instance::read_or_create(pool, "alpha.com".to_string()).await?,
Instance::read_or_create(pool, "beta.com".to_string()).await?,
Instance::read_or_create(pool, "gamma.com".to_string()).await?,
Instance::read_or_create(pool, "alpha.com").await?,
Instance::read_or_create(pool, "beta.com").await?,
Instance::read_or_create(pool, "gamma.com").await?,
];

let send_manager = SendManager::new(opts, federation_config, federation_worker_config);
Expand Down Expand Up @@ -322,11 +322,7 @@ mod test {
let instance_id = data.instances[0].id;
let form = PersonInsertForm::new("tim".to_string(), String::new(), instance_id);
let person = Person::create(&mut data.context.pool(), &form).await?;
let form = FederationBlockListForm {
instance_id,
updated_at: None,
expires_at: None,
};
let form = FederationBlockListForm::new(instance_id, None);
FederationBlockList::block(&mut data.context.pool(), &form).await?;
data.run().await?;
let workers = &data.send_manager.workers;
Expand All @@ -348,10 +344,7 @@ mod test {
let instance_id = data.instances[0].id;
let form = PersonInsertForm::new("tim".to_string(), String::new(), instance_id);
let person = Person::create(&mut data.context.pool(), &form).await?;
let form = FederationAllowListForm {
instance_id: data.instances[0].id,
updated_at: None,
};
let form = FederationAllowListForm::new(data.instances[0].id);
FederationAllowList::allow(&mut data.context.pool(), &form).await?;
data.run().await?;
let workers = &data.send_manager.workers;
Expand Down
4 changes: 2 additions & 2 deletions crates/apub/send/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ mod test {
impl Data {
async fn init() -> LemmyResult<Self> {
let context = LemmyContext::init_test_federation_config().await;
let instance = Instance::read_or_create(&mut context.pool(), "localhost".to_string()).await?;
let instance = Instance::read_or_create(&mut context.pool(), "localhost").await?;

let actor_keypair = generate_actor_keypair()?;
let ap_id: DbUrl = Url::parse("http://local.com/u/alice")?.into();
Expand Down Expand Up @@ -677,7 +677,7 @@ mod test {
data.inbox_receiver.recv().await.unwrap();

let instance =
Instance::read_or_create(&mut data.context.pool(), data.instance.domain.clone()).await?;
Instance::read_or_create(&mut data.context.pool(), &data.instance.domain).await?;

assert!(instance.updated_at.is_some());

Expand Down
6 changes: 3 additions & 3 deletions crates/db_schema/src/impls/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ mod tests {
let pool = &build_db_pool_for_tests();
let pool = &mut pool.into();

let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string()).await?;
let inserted_instance = Instance::read_or_create(pool, "my_domain.tld").await?;

let new_person = PersonInsertForm::test_form(inserted_instance.id, "terry");

Expand Down Expand Up @@ -553,7 +553,7 @@ mod tests {
let pool = &build_db_pool_for_tests();
let pool = &mut pool.into();

let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string()).await?;
let inserted_instance = Instance::read_or_create(pool, "my_domain.tld").await?;

let new_person = PersonInsertForm::test_form(inserted_instance.id, "thommy_comment_agg");

Expand Down Expand Up @@ -649,7 +649,7 @@ mod tests {
let pool = &build_db_pool_for_tests();
let pool = &mut pool.into();

let inserted_instance = Instance::read_or_create(pool, "mydomain.tld".to_string()).await?;
let inserted_instance = Instance::read_or_create(pool, "mydomain.tld").await?;
let new_person = PersonInsertForm::test_form(inserted_instance.id, "john");
let inserted_person = Person::create(pool, &new_person).await?;
let new_community = CommunityInsertForm::new(
Expand Down
4 changes: 2 additions & 2 deletions crates/db_schema/src/impls/community.rs
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,7 @@ mod tests {
let pool = &build_db_pool_for_tests();
let pool = &mut pool.into();

let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string()).await?;
let inserted_instance = Instance::read_or_create(pool, "my_domain.tld").await?;

let bobby_person = PersonInsertForm::test_form(inserted_instance.id, "bobby");
let inserted_bobby = Person::create(pool, &bobby_person).await?;
Expand Down Expand Up @@ -860,7 +860,7 @@ mod tests {
let pool = &build_db_pool_for_tests();
let pool = &mut pool.into();

let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string()).await?;
let inserted_instance = Instance::read_or_create(pool, "my_domain.tld").await?;

let new_person = PersonInsertForm::test_form(inserted_instance.id, "thommy_community_agg");

Expand Down
Loading