From f786df151f0556a49b329f03d382c9d8e008cc7a Mon Sep 17 00:00:00 2001 From: Nutomic Date: Sat, 18 Nov 2023 03:59:15 +0100 Subject: [PATCH] Include instance blocks in settings export (#4173) --- crates/apub/src/api/user_settings_backup.rs | 19 +++++++++++++++++++ crates/db_schema/src/impls/local_user.rs | 11 +++++++++++ 2 files changed, 30 insertions(+) diff --git a/crates/apub/src/api/user_settings_backup.rs b/crates/apub/src/api/user_settings_backup.rs index abc683687..835a793fa 100644 --- a/crates/apub/src/api/user_settings_backup.rs +++ b/crates/apub/src/api/user_settings_backup.rs @@ -14,6 +14,8 @@ use lemmy_db_schema::{ comment::{CommentSaved, CommentSavedForm}, community::{CommunityFollower, CommunityFollowerForm}, community_block::{CommunityBlock, CommunityBlockForm}, + instance::Instance, + instance_block::{InstanceBlock, InstanceBlockForm}, local_user::{LocalUser, LocalUserUpdateForm}, person::{Person, PersonUpdateForm}, person_block::{PersonBlock, PersonBlockForm}, @@ -58,6 +60,8 @@ pub struct UserSettingsBackup { pub blocked_communities: Vec>, #[serde(default)] pub blocked_users: Vec>, + #[serde(default)] + pub blocked_instances: Vec, } #[tracing::instrument(skip(context))] @@ -78,6 +82,7 @@ pub async fn export_settings( settings: Some(local_user_view.local_user), followed_communities: vec_into(lists.followed_communities), blocked_communities: vec_into(lists.blocked_communities), + blocked_instances: lists.blocked_instances, blocked_users: lists.blocked_users.into_iter().map(Into::into).collect(), saved_posts: lists.saved_posts.into_iter().map(Into::into).collect(), saved_comments: lists.saved_comments.into_iter().map(Into::into).collect(), @@ -130,6 +135,7 @@ pub async fn import_settings( let url_count = data.followed_communities.len() + data.blocked_communities.len() + data.blocked_users.len() + + data.blocked_instances.len() + data.saved_posts.len() + data.saved_comments.len(); if url_count > MAX_API_PARAM_ELEMENTS { @@ -269,6 +275,19 @@ pub async fn import_settings( LemmyResult::Ok(()) })) .await?; + + try_join_all(data.blocked_instances.iter().map(|domain| async { + // dont fetch unknown blocked objects from home server + let instance = Instance::read_or_create(&mut context.pool(), domain.clone()).await?; + let form = InstanceBlockForm { + person_id, + instance_id: instance.id, + }; + InstanceBlock::block(&mut context.pool(), &form).await?; + LemmyResult::Ok(()) + })) + .await?; + Ok(()) }); diff --git a/crates/db_schema/src/impls/local_user.rs b/crates/db_schema/src/impls/local_user.rs index 86960c053..98d1c8494 100644 --- a/crates/db_schema/src/impls/local_user.rs +++ b/crates/db_schema/src/impls/local_user.rs @@ -76,6 +76,8 @@ impl LocalUser { community, community_block, community_follower, + instance, + instance_block, person, person_block, post, @@ -118,6 +120,13 @@ impl LocalUser { .get_results(conn) .await?; + let blocked_instances = instance_block::dsl::instance_block + .filter(instance_block::person_id.eq(person_id_)) + .inner_join(instance::table) + .select(instance::domain) + .get_results(conn) + .await?; + // TODO: use join for parallel queries? Ok(UserBackupLists { @@ -126,6 +135,7 @@ impl LocalUser { saved_comments, blocked_communities, blocked_users, + blocked_instances, }) } } @@ -136,6 +146,7 @@ pub struct UserBackupLists { pub saved_comments: Vec, pub blocked_communities: Vec, pub blocked_users: Vec, + pub blocked_instances: Vec, } #[async_trait]