Make User API methods return Account entity

This commit is contained in:
silverpill 2021-10-05 16:24:06 +00:00
parent f6e9c082e2
commit b39733beb7
5 changed files with 24 additions and 40 deletions

View file

@ -9,6 +9,7 @@ use crate::models::profiles::types::{
ExtraField,
ProfileUpdateData,
};
use crate::models::users::types::User;
use crate::utils::files::{FileError, save_validated_b64_file, get_file_url};
#[derive(Serialize)]
@ -41,6 +42,8 @@ pub struct Account {
pub statuses_count: i32,
pub source: Option<Source>,
pub wallet_address: Option<String>,
}
impl Account {
@ -81,8 +84,15 @@ impl Account {
following_count: profile.following_count,
statuses_count: profile.post_count,
source,
wallet_address: None,
}
}
pub fn from_user(user: User, instance_url: &str) -> Self {
let mut account = Self::from_profile(user.profile, instance_url);
account.wallet_address = Some(user.wallet_address);
account
}
}
/// https://docs.joinmastodon.org/methods/accounts/

View file

@ -45,7 +45,7 @@ async fn verify_credentials(
) -> Result<HttpResponse, HttpError> {
let db_client = &**get_database_client(&db_pool).await?;
let user = get_current_user_(db_client, auth.token()).await?;
let account = Account::from_profile(user.profile, &config.instance_url());
let account = Account::from_user(user, &config.instance_url());
Ok(HttpResponse::Ok().json(account))
}
@ -57,12 +57,11 @@ async fn update_credentials(
data: web::Json<AccountUpdateData>,
) -> Result<HttpResponse, HttpError> {
let db_client = &**get_database_client(&db_pool).await?;
let current_user = get_current_user(db_client, session).await?;
let profile = get_profile_by_id(db_client, &current_user.id).await?;
let mut current_user = get_current_user(db_client, session).await?;
let mut profile_data = data.into_inner()
.into_profile_data(
&profile.avatar_file_name,
&profile.banner_file_name,
&current_user.profile.avatar_file_name,
&current_user.profile.banner_file_name,
&config.media_dir(),
)
.map_err(|err| {
@ -77,12 +76,12 @@ async fn update_credentials(
}
})?;
profile_data.clean()?;
let updated_profile = update_profile(
current_user.profile = update_profile(
db_client,
&profile.id,
&current_user.id,
profile_data,
).await?;
let account = Account::from_profile(updated_profile, &config.instance_url());
let account = Account::from_user(current_user, &config.instance_url());
Ok(HttpResponse::Ok().json(account))
}

View file

@ -1,3 +1,2 @@
pub mod auth;
mod types;
pub mod views;

View file

@ -1,24 +0,0 @@
use serde::Serialize;
use uuid::Uuid;
use crate::mastodon_api::accounts::types::Account;
use crate::models::users::types::User;
// TODO: use Account instead
#[derive(Serialize)]
pub struct ApiUser {
pub id: Uuid,
pub profile: Account,
pub wallet_address: String,
}
impl ApiUser {
pub fn from_user(user: User, instance_url: &str) -> Self {
let account = Account::from_profile(user.profile, instance_url);
Self {
id: user.id,
profile: account,
wallet_address: user.wallet_address,
}
}
}

View file

@ -7,6 +7,7 @@ use actix_web::{
use crate::config::Config;
use crate::database::{Pool, get_database_client};
use crate::errors::{HttpError, ValidationError};
use crate::mastodon_api::accounts::types::Account;
use crate::models::users::queries::{
is_valid_invite_code,
create_user,
@ -23,7 +24,6 @@ use crate::utils::crypto::{
serialize_private_key,
};
use super::auth::get_current_user;
use super::types::ApiUser;
// /api/v1/accounts
#[post("/api/v0/create")]
@ -60,8 +60,8 @@ async fn create_user_view(
private_key_pem,
).await?;
session.set("id", user.id)?;
let api_user = ApiUser::from_user(user, &config.instance_url());
Ok(HttpResponse::Created().json(api_user))
let account = Account::from_user(user, &config.instance_url());
Ok(HttpResponse::Created().json(account))
}
#[post("/api/v0/login")]
@ -80,8 +80,8 @@ async fn login_view(
Err(ValidationError("incorrect password"))?;
}
session.set("id", &user.id)?;
let api_user = ApiUser::from_user(user, &config.instance_url());
Ok(HttpResponse::Ok().json(api_user))
let account = Account::from_user(user, &config.instance_url());
Ok(HttpResponse::Ok().json(account))
}
#[get("/api/v0/current-user")]
@ -92,8 +92,8 @@ async fn current_user_view(
) -> Result<HttpResponse, HttpError> {
let db_client = &**get_database_client(&db_pool).await?;
let user = get_current_user(db_client, session).await?;
let api_user = ApiUser::from_user(user, &config.instance_url());
Ok(HttpResponse::Ok().json(api_user))
let account = Account::from_user(user, &config.instance_url());
Ok(HttpResponse::Ok().json(account))
}
#[post("/api/v0/logout")]