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 endpoint for adding aliases.
- Populate `alsoKnownAs` property on actor object with declared aliases.
- Support account migration from Mastodon.
### Changed

View file

@ -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,
&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)]
mod tests {
use mitra_utils::id::generate_ulid;

View file

@ -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::<Vec<_>>();
// 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,

View file

@ -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,
&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(
db_client: &impl DatabaseClient,
source_id: &Uuid,

View file

@ -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,
};

View file

@ -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;