Include instance blocks in settings export (#4173)

This commit is contained in:
Nutomic 2023-11-18 03:59:15 +01:00 committed by GitHub
parent 25450ea090
commit f786df151f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 0 deletions

View File

@ -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<ObjectId<ApubCommunity>>,
#[serde(default)]
pub blocked_users: Vec<ObjectId<ApubPerson>>,
#[serde(default)]
pub blocked_instances: Vec<String>,
}
#[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(())
});

View File

@ -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<DbUrl>,
pub blocked_communities: Vec<DbUrl>,
pub blocked_users: Vec<DbUrl>,
pub blocked_instances: Vec<String>,
}
#[async_trait]