Add find_aliases() helper function
This commit is contained in:
parent
2714378f22
commit
212db48d75
4 changed files with 38 additions and 33 deletions
|
@ -17,7 +17,7 @@ use crate::database::DatabaseError;
|
|||
use crate::errors::ValidationError;
|
||||
use crate::models::{
|
||||
notifications::queries::create_move_notification,
|
||||
profiles::queries::search_profiles_by_did_only,
|
||||
profiles::helpers::find_aliases,
|
||||
relationships::queries::{
|
||||
create_follow_request,
|
||||
get_followers,
|
||||
|
@ -71,24 +71,14 @@ pub async fn handle_move(
|
|||
&media_dir,
|
||||
&activity.target,
|
||||
).await?;
|
||||
let new_actor = new_profile.actor_json
|
||||
let new_actor = new_profile.actor_json.as_ref()
|
||||
.expect("target should be a remote actor");
|
||||
|
||||
// Find aliases by DIDs
|
||||
let mut aliases = vec![];
|
||||
for identity_proof in new_profile.identity_proofs.inner() {
|
||||
let profiles = search_profiles_by_did_only(
|
||||
db_client,
|
||||
&identity_proof.issuer,
|
||||
).await?;
|
||||
for profile in profiles {
|
||||
if profile.id == new_profile.id {
|
||||
continue;
|
||||
};
|
||||
let actor_id = profile.actor_id(&instance.url());
|
||||
aliases.push(actor_id);
|
||||
};
|
||||
};
|
||||
let mut aliases = find_aliases(db_client, &new_profile).await?
|
||||
.into_iter()
|
||||
.map(|profile| profile.actor_id(&instance.url()))
|
||||
.collect::<Vec<_>>();
|
||||
// Read aliases from alsoKnownAs property
|
||||
if let Some(ref value) = new_actor.also_known_as {
|
||||
let also_known_as = parse_array(value)
|
||||
|
@ -130,7 +120,7 @@ pub async fn handle_move(
|
|||
activities.push(prepare_follow(
|
||||
&instance,
|
||||
&follower,
|
||||
&new_actor,
|
||||
new_actor,
|
||||
&follow_request.id,
|
||||
));
|
||||
},
|
||||
|
|
|
@ -51,12 +51,12 @@ use crate::mastodon_api::search::helpers::search_profiles_only;
|
|||
use crate::mastodon_api::statuses::helpers::build_status_list;
|
||||
use crate::mastodon_api::statuses::types::Status;
|
||||
use crate::models::posts::queries::get_posts_by_author;
|
||||
use crate::models::profiles::helpers::find_aliases;
|
||||
use crate::models::profiles::queries::{
|
||||
get_profile_by_acct,
|
||||
get_profile_by_id,
|
||||
get_profile_by_remote_actor_id,
|
||||
search_profiles_by_did,
|
||||
search_profiles_by_did_only,
|
||||
update_profile,
|
||||
};
|
||||
use crate::models::profiles::types::{
|
||||
|
@ -286,21 +286,10 @@ async fn move_followers(
|
|||
};
|
||||
if maybe_from_profile.is_some() {
|
||||
// Find known aliases of the current user
|
||||
let mut aliases = vec![];
|
||||
for identity_proof in current_user.profile.identity_proofs.inner() {
|
||||
let profiles = search_profiles_by_did_only(
|
||||
db_client,
|
||||
&identity_proof.issuer,
|
||||
).await?;
|
||||
for profile in profiles {
|
||||
if profile.id == current_user.id {
|
||||
continue;
|
||||
};
|
||||
let actor_id = profile.actor_id(&config.instance_url());
|
||||
aliases.push(actor_id);
|
||||
};
|
||||
};
|
||||
if !aliases.contains(&request_data.from_actor_id) {
|
||||
let mut aliases = find_aliases(db_client, ¤t_user.profile).await?
|
||||
.into_iter()
|
||||
.map(|profile| profile.actor_id(&config.instance_url()));
|
||||
if !aliases.any(|actor_id| actor_id == request_data.from_actor_id) {
|
||||
return Err(ValidationError("old profile is not an alias").into());
|
||||
};
|
||||
};
|
||||
|
|
25
src/models/profiles/helpers.rs
Normal file
25
src/models/profiles/helpers.rs
Normal file
|
@ -0,0 +1,25 @@
|
|||
use tokio_postgres::GenericClient;
|
||||
|
||||
use crate::database::DatabaseError;
|
||||
use super::queries::search_profiles_by_did_only;
|
||||
use super::types::DbActorProfile;
|
||||
|
||||
pub async fn find_aliases(
|
||||
db_client: &impl GenericClient,
|
||||
profile: &DbActorProfile,
|
||||
) -> Result<Vec<DbActorProfile>, DatabaseError> {
|
||||
let mut results = vec![];
|
||||
for identity_proof in profile.identity_proofs.inner() {
|
||||
let aliases = search_profiles_by_did_only(
|
||||
db_client,
|
||||
&identity_proof.issuer,
|
||||
).await?;
|
||||
for alias in aliases {
|
||||
if alias.id == profile.id {
|
||||
continue;
|
||||
};
|
||||
results.push(alias);
|
||||
};
|
||||
};
|
||||
Ok(results)
|
||||
}
|
|
@ -1,3 +1,4 @@
|
|||
pub mod helpers;
|
||||
pub mod queries;
|
||||
pub mod types;
|
||||
pub mod validators;
|
||||
|
|
Loading…
Reference in a new issue