Support account migration from Mastodon

This commit is contained in:
silverpill 2023-03-17 17:04:56 +00:00
parent b0bf3cf594
commit dcaa2227d2
6 changed files with 66 additions and 69 deletions

View file

@ -12,6 +12,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Created `/api/v1/accounts/aliases/all` API endpoint. - Created `/api/v1/accounts/aliases/all` API endpoint.
- Created API endpoint for adding aliases. - Created API endpoint for adding aliases.
- Populate `alsoKnownAs` property on actor object with declared aliases. - Populate `alsoKnownAs` property on actor object with declared aliases.
- Support account migration from Mastodon.
### Changed ### Changed

View file

@ -3,7 +3,9 @@ use uuid::Uuid;
use mitra_config::Instance; use mitra_config::Instance;
use mitra_models::{ use mitra_models::{
database::{DatabaseClient, DatabaseError},
profiles::types::{DbActor, DbActorProfile}, profiles::types::{DbActor, DbActorProfile},
relationships::queries::{create_follow_request, follow},
users::types::User, users::types::User,
}; };
@ -68,6 +70,40 @@ pub fn prepare_follow(
) )
} }
pub async fn follow_or_create_request(
db_client: &mut impl DatabaseClient,
instance: &Instance,
current_user: &User,
target_profile: &DbActorProfile,
) -> Result<(), DatabaseError> {
if let Some(ref remote_actor) = target_profile.actor_json {
// Create follow request if target is remote
match create_follow_request(
db_client,
&current_user.id,
&target_profile.id,
).await {
Ok(follow_request) => {
prepare_follow(
instance,
current_user,
remote_actor,
&follow_request.id,
).enqueue(db_client).await?;
},
Err(DatabaseError::AlreadyExists(_)) => (), // already following
Err(other_error) => return Err(other_error),
};
} else {
match follow(db_client, &current_user.id, &target_profile.id).await {
Ok(_) => (),
Err(DatabaseError::AlreadyExists(_)) => (), // already following
Err(other_error) => return Err(other_error),
};
};
Ok(())
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use mitra_utils::id::generate_ulid; use mitra_utils::id::generate_ulid;

View file

@ -3,11 +3,10 @@ use serde_json::Value;
use mitra_config::Config; use mitra_config::Config;
use mitra_models::{ use mitra_models::{
database::{DatabaseClient, DatabaseError}, database::DatabaseClient,
notifications::queries::create_move_notification, notifications::queries::create_move_notification,
profiles::helpers::find_verified_aliases, profiles::helpers::find_verified_aliases,
relationships::queries::{ relationships::queries::{
create_follow_request,
get_followers, get_followers,
unfollow, unfollow,
}, },
@ -16,7 +15,7 @@ use mitra_models::{
use crate::activitypub::{ use crate::activitypub::{
builders::{ builders::{
follow::prepare_follow, follow::follow_or_create_request,
undo_follow::prepare_undo_follow, undo_follow::prepare_undo_follow,
}, },
fetcher::helpers::get_or_import_profile_by_actor_id, fetcher::helpers::get_or_import_profile_by_actor_id,
@ -66,14 +65,21 @@ pub async fn handle_move(
).await? ).await?
}; };
let old_actor_id = profile_actor_id(&instance.url(), &old_profile); let old_actor_id = profile_actor_id(&instance.url(), &old_profile);
let new_profile = get_or_import_profile_by_actor_id(
let new_profile = if let Ok(username) = parse_local_actor_id(
&instance.url(),
&activity.target,
) {
let new_user = get_user_by_name(db_client, &username).await?;
new_user.profile
} else {
get_or_import_profile_by_actor_id(
db_client, db_client,
&instance, &instance,
&storage, &storage,
&activity.target, &activity.target,
).await?; ).await?
let new_actor = new_profile.actor_json.as_ref() };
.expect("target should be a remote actor");
// Find aliases by DIDs (verified) // Find aliases by DIDs (verified)
let mut aliases = find_verified_aliases(db_client, &new_profile).await? let mut aliases = find_verified_aliases(db_client, &new_profile).await?
@ -81,7 +87,7 @@ pub async fn handle_move(
.map(|profile| profile_actor_id(&instance.url(), &profile)) .map(|profile| profile_actor_id(&instance.url(), &profile))
.collect::<Vec<_>>(); .collect::<Vec<_>>();
// Add aliases reported by server (actor's alsoKnownAs property) // Add aliases reported by server (actor's alsoKnownAs property)
aliases.extend(new_profile.aliases.into_actor_ids()); aliases.extend(new_profile.aliases.clone().into_actor_ids());
if !aliases.contains(&old_actor_id) { if !aliases.contains(&old_actor_id) {
return Err(ValidationError("target ID is not an alias").into()); return Err(ValidationError("target ID is not an alias").into());
}; };
@ -106,23 +112,17 @@ pub async fn handle_move(
&follow_request_id, &follow_request_id,
).enqueue(db_client).await?; ).enqueue(db_client).await?;
}; };
if follower.id == new_profile.id {
// Don't self-follow
continue;
};
// Follow new profile // Follow new profile
match create_follow_request( follow_or_create_request(
db_client, db_client,
&follower.id,
&new_profile.id,
).await {
Ok(follow_request) => {
prepare_follow(
&instance, &instance,
&follower, &follower,
new_actor, &new_profile,
&follow_request.id, ).await?;
).enqueue(db_client).await?;
},
Err(DatabaseError::AlreadyExists(_)) => (), // already following
Err(other_error) => return Err(other_error.into()),
};
create_move_notification( create_move_notification(
db_client, db_client,
&new_profile.id, &new_profile.id,

View file

@ -1,6 +1,5 @@
use uuid::Uuid; use uuid::Uuid;
use mitra_config::Instance;
use mitra_models::{ use mitra_models::{
database::{DatabaseClient, DatabaseError}, database::{DatabaseClient, DatabaseError},
profiles::helpers::{ profiles::helpers::{
@ -9,52 +8,13 @@ use mitra_models::{
}, },
profiles::types::DbActorProfile, profiles::types::DbActorProfile,
relationships::queries::{ relationships::queries::{
create_follow_request,
follow,
get_relationships, get_relationships,
}, },
relationships::types::RelationshipType, relationships::types::RelationshipType,
users::types::User,
}; };
use crate::activitypub::builders::follow::prepare_follow;
use super::types::{Account, Aliases, RelationshipMap}; use super::types::{Account, Aliases, RelationshipMap};
pub async fn follow_or_create_request(
db_client: &mut impl DatabaseClient,
instance: &Instance,
current_user: &User,
target_profile: &DbActorProfile,
) -> Result<(), DatabaseError> {
if let Some(ref remote_actor) = target_profile.actor_json {
// Create follow request if target is remote
match create_follow_request(
db_client,
&current_user.id,
&target_profile.id,
).await {
Ok(follow_request) => {
prepare_follow(
instance,
current_user,
remote_actor,
&follow_request.id,
).enqueue(db_client).await?;
},
Err(DatabaseError::AlreadyExists(_)) => (), // already following
Err(other_error) => return Err(other_error),
};
} else {
match follow(db_client, &current_user.id, &target_profile.id).await {
Ok(_) => (),
Err(DatabaseError::AlreadyExists(_)) => (), // already following
Err(other_error) => return Err(other_error),
};
};
Ok(())
}
pub async fn get_relationship( pub async fn get_relationship(
db_client: &impl DatabaseClient, db_client: &impl DatabaseClient,
source_id: &Uuid, source_id: &Uuid,

View file

@ -60,6 +60,7 @@ use mitra_utils::{
use crate::activitypub::{ use crate::activitypub::{
builders::{ builders::{
follow::follow_or_create_request,
undo_follow::prepare_undo_follow, undo_follow::prepare_undo_follow,
update_person::{ update_person::{
build_update_person, build_update_person,
@ -101,7 +102,6 @@ use crate::mastodon_api::{
}; };
use crate::validators::profiles::clean_profile_update_data; use crate::validators::profiles::clean_profile_update_data;
use super::helpers::{ use super::helpers::{
follow_or_create_request,
get_aliases, get_aliases,
get_relationship, get_relationship,
}; };

View file

@ -20,6 +20,7 @@ use mitra_models::{
use crate::activitypub::{ use crate::activitypub::{
builders::{ builders::{
follow::follow_or_create_request,
move_person::prepare_move_person, move_person::prepare_move_person,
undo_follow::prepare_undo_follow, undo_follow::prepare_undo_follow,
}, },
@ -27,7 +28,6 @@ use crate::activitypub::{
HandlerError, HandlerError,
}; };
use crate::errors::ValidationError; use crate::errors::ValidationError;
use crate::mastodon_api::accounts::helpers::follow_or_create_request;
use crate::media::MediaStorage; use crate::media::MediaStorage;
use crate::webfinger::types::ActorAddress; use crate::webfinger::types::ActorAddress;