diff --git a/CHANGELOG.md b/CHANGELOG.md index 66c846a..a41dba8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 endpoint for adding aliases. - Populate `alsoKnownAs` property on actor object with declared aliases. +- Support account migration from Mastodon. ### Changed diff --git a/src/activitypub/builders/follow.rs b/src/activitypub/builders/follow.rs index 4ddf7dc..fb95161 100644 --- a/src/activitypub/builders/follow.rs +++ b/src/activitypub/builders/follow.rs @@ -3,7 +3,9 @@ use uuid::Uuid; use mitra_config::Instance; use mitra_models::{ + database::{DatabaseClient, DatabaseError}, profiles::types::{DbActor, DbActorProfile}, + relationships::queries::{create_follow_request, follow}, 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, + ¤t_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, ¤t_user.id, &target_profile.id).await { + Ok(_) => (), + Err(DatabaseError::AlreadyExists(_)) => (), // already following + Err(other_error) => return Err(other_error), + }; + }; + Ok(()) +} + #[cfg(test)] mod tests { use mitra_utils::id::generate_ulid; diff --git a/src/activitypub/handlers/move.rs b/src/activitypub/handlers/move.rs index aaad1e7..34d0796 100644 --- a/src/activitypub/handlers/move.rs +++ b/src/activitypub/handlers/move.rs @@ -3,11 +3,10 @@ use serde_json::Value; use mitra_config::Config; use mitra_models::{ - database::{DatabaseClient, DatabaseError}, + database::DatabaseClient, notifications::queries::create_move_notification, profiles::helpers::find_verified_aliases, relationships::queries::{ - create_follow_request, get_followers, unfollow, }, @@ -16,7 +15,7 @@ use mitra_models::{ use crate::activitypub::{ builders::{ - follow::prepare_follow, + follow::follow_or_create_request, undo_follow::prepare_undo_follow, }, fetcher::helpers::get_or_import_profile_by_actor_id, @@ -66,14 +65,21 @@ pub async fn handle_move( ).await? }; let old_actor_id = profile_actor_id(&instance.url(), &old_profile); - let new_profile = get_or_import_profile_by_actor_id( - db_client, - &instance, - &storage, + + let new_profile = if let Ok(username) = parse_local_actor_id( + &instance.url(), &activity.target, - ).await?; - let new_actor = new_profile.actor_json.as_ref() - .expect("target should be a remote actor"); + ) { + let new_user = get_user_by_name(db_client, &username).await?; + new_user.profile + } else { + get_or_import_profile_by_actor_id( + db_client, + &instance, + &storage, + &activity.target, + ).await? + }; // Find aliases by DIDs (verified) 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)) .collect::>(); // 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) { return Err(ValidationError("target ID is not an alias").into()); }; @@ -106,23 +112,17 @@ pub async fn handle_move( &follow_request_id, ).enqueue(db_client).await?; }; - // Follow new profile - match create_follow_request( - db_client, - &follower.id, - &new_profile.id, - ).await { - Ok(follow_request) => { - prepare_follow( - &instance, - &follower, - new_actor, - &follow_request.id, - ).enqueue(db_client).await?; - }, - Err(DatabaseError::AlreadyExists(_)) => (), // already following - Err(other_error) => return Err(other_error.into()), + if follower.id == new_profile.id { + // Don't self-follow + continue; }; + // Follow new profile + follow_or_create_request( + db_client, + &instance, + &follower, + &new_profile, + ).await?; create_move_notification( db_client, &new_profile.id, diff --git a/src/mastodon_api/accounts/helpers.rs b/src/mastodon_api/accounts/helpers.rs index 01ea7fa..9461056 100644 --- a/src/mastodon_api/accounts/helpers.rs +++ b/src/mastodon_api/accounts/helpers.rs @@ -1,6 +1,5 @@ use uuid::Uuid; -use mitra_config::Instance; use mitra_models::{ database::{DatabaseClient, DatabaseError}, profiles::helpers::{ @@ -9,52 +8,13 @@ use mitra_models::{ }, profiles::types::DbActorProfile, relationships::queries::{ - create_follow_request, - follow, get_relationships, }, relationships::types::RelationshipType, - users::types::User, }; -use crate::activitypub::builders::follow::prepare_follow; - 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, - ¤t_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, ¤t_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( db_client: &impl DatabaseClient, source_id: &Uuid, diff --git a/src/mastodon_api/accounts/views.rs b/src/mastodon_api/accounts/views.rs index 729c650..f6338d5 100644 --- a/src/mastodon_api/accounts/views.rs +++ b/src/mastodon_api/accounts/views.rs @@ -60,6 +60,7 @@ use mitra_utils::{ use crate::activitypub::{ builders::{ + follow::follow_or_create_request, undo_follow::prepare_undo_follow, update_person::{ build_update_person, @@ -101,7 +102,6 @@ use crate::mastodon_api::{ }; use crate::validators::profiles::clean_profile_update_data; use super::helpers::{ - follow_or_create_request, get_aliases, get_relationship, }; diff --git a/src/mastodon_api/settings/helpers.rs b/src/mastodon_api/settings/helpers.rs index c9a6345..1a02812 100644 --- a/src/mastodon_api/settings/helpers.rs +++ b/src/mastodon_api/settings/helpers.rs @@ -20,6 +20,7 @@ use mitra_models::{ use crate::activitypub::{ builders::{ + follow::follow_or_create_request, move_person::prepare_move_person, undo_follow::prepare_undo_follow, }, @@ -27,7 +28,6 @@ use crate::activitypub::{ HandlerError, }; use crate::errors::ValidationError; -use crate::mastodon_api::accounts::helpers::follow_or_create_request; use crate::media::MediaStorage; use crate::webfinger::types::ActorAddress;