diff --git a/CHANGELOG.md b/CHANGELOG.md index c4d6912..d79fb14 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Support calling `/api/v1/accounts/search` with `resolve` parameter. - Created `/api/v1/accounts/aliases/all` API endpoint. +- Created API endpoint for adding aliases. ### Changed diff --git a/docs/openapi.yaml b/docs/openapi.yaml index 79ecb4e..a9c432b 100644 --- a/docs/openapi.yaml +++ b/docs/openapi.yaml @@ -803,6 +803,28 @@ paths: $ref: '#/components/schemas/CredentialAccount' 400: description: Invalid request data. + /api/v1/settings/aliases: + post: + summary: Add alias (not verified). + requestBody: + content: + application/json: + schema: + type: object + properties: + acct: + description: Actor address. + type: string + example: user@example.com + responses: + 200: + description: Successful operation + content: + application/json: + schema: + $ref: '#/components/schemas/Aliases' + 404: + description: Profile not found. /api/v1/settings/export_followers: get: summary: Export followers to CSV file diff --git a/src/mastodon_api/settings/types.rs b/src/mastodon_api/settings/types.rs index 37db750..55b91c2 100644 --- a/src/mastodon_api/settings/types.rs +++ b/src/mastodon_api/settings/types.rs @@ -10,6 +10,11 @@ pub struct ImportFollowsRequest { pub follows_csv: String, } +#[derive(Deserialize)] +pub struct AddAliasRequest { + pub acct: String, +} + #[derive(Deserialize)] pub struct MoveFollowersRequest { pub from_actor_id: String, diff --git a/src/mastodon_api/settings/views.rs b/src/mastodon_api/settings/views.rs index 01ca5e1..577b10a 100644 --- a/src/mastodon_api/settings/views.rs +++ b/src/mastodon_api/settings/views.rs @@ -12,7 +12,12 @@ use mitra_config::Config; use mitra_models::{ database::{get_database_client, DatabaseError, DbPool}, profiles::helpers::find_verified_aliases, - profiles::queries::get_profile_by_remote_actor_id, + profiles::queries::{ + get_profile_by_acct, + get_profile_by_remote_actor_id, + update_profile, + }, + profiles::types::ProfileUpdateData, users::queries::set_user_password, }; use mitra_utils::passwords::hash_password; @@ -21,6 +26,7 @@ use crate::activitypub::identifiers::profile_actor_id; use crate::errors::ValidationError; use crate::http::get_request_base_url; use crate::mastodon_api::{ + accounts::helpers::get_aliases, accounts::types::Account, errors::MastodonError, oauth::auth::get_current_user, @@ -33,6 +39,7 @@ use super::helpers::{ parse_address_list, }; use super::types::{ + AddAliasRequest, ImportFollowsRequest, MoveFollowersRequest, PasswordChangeRequest, @@ -59,6 +66,37 @@ async fn change_password_view( Ok(HttpResponse::Ok().json(account)) } +#[post("/aliases")] +async fn add_alias_view( + auth: BearerAuth, + config: web::Data, + connection_info: ConnectionInfo, + db_pool: web::Data, + request_data: web::Json, +) -> Result { + let db_client = &mut **get_database_client(&db_pool).await?; + let mut current_user = get_current_user(db_client, auth.token()).await?; + let alias = get_profile_by_acct(db_client, &request_data.acct).await?; + let instance = config.instance(); + let alias_id = profile_actor_id(&instance.url(), &alias); + let mut profile_data = ProfileUpdateData::from(¤t_user.profile); + if !profile_data.aliases.contains(&alias_id) { + profile_data.aliases.push(alias_id); + }; + current_user.profile = update_profile( + db_client, + ¤t_user.id, + profile_data, + ).await?; + let aliases = get_aliases( + db_client, + &get_request_base_url(connection_info), + &instance.url(), + ¤t_user.profile, + ).await?; + Ok(HttpResponse::Ok().json(aliases)) +} + #[get("/export_followers")] async fn export_followers_view( auth: BearerAuth, @@ -185,6 +223,7 @@ async fn move_followers( pub fn settings_api_scope() -> Scope { web::scope("/api/v1/settings") .service(change_password_view) + .service(add_alias_view) .service(export_followers_view) .service(export_follows_view) .service(import_follows_view)