Create API endpoint for adding aliases

This commit is contained in:
silverpill 2023-04-02 21:23:04 +00:00
parent 13df9e0478
commit 99f6c08e9a
4 changed files with 68 additions and 1 deletions

View file

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

View file

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

View file

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

View file

@ -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<Config>,
connection_info: ConnectionInfo,
db_pool: web::Data<DbPool>,
request_data: web::Json<AddAliasRequest>,
) -> Result<HttpResponse, MastodonError> {
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(&current_user.profile);
if !profile_data.aliases.contains(&alias_id) {
profile_data.aliases.push(alias_id);
};
current_user.profile = update_profile(
db_client,
&current_user.id,
profile_data,
).await?;
let aliases = get_aliases(
db_client,
&get_request_base_url(connection_info),
&instance.url(),
&current_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)