From b39733beb7f69f700d844505b0ada3947cc24973 Mon Sep 17 00:00:00 2001 From: silverpill Date: Tue, 5 Oct 2021 16:24:06 +0000 Subject: [PATCH] Make User API methods return Account entity --- src/mastodon_api/accounts/types.rs | 10 ++++++++++ src/mastodon_api/accounts/views.rs | 15 +++++++-------- src/mastodon_api/users/mod.rs | 1 - src/mastodon_api/users/types.rs | 24 ------------------------ src/mastodon_api/users/views.rs | 14 +++++++------- 5 files changed, 24 insertions(+), 40 deletions(-) delete mode 100644 src/mastodon_api/users/types.rs diff --git a/src/mastodon_api/accounts/types.rs b/src/mastodon_api/accounts/types.rs index b6d14af..4deac49 100644 --- a/src/mastodon_api/accounts/types.rs +++ b/src/mastodon_api/accounts/types.rs @@ -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, + + pub wallet_address: Option, } 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/ diff --git a/src/mastodon_api/accounts/views.rs b/src/mastodon_api/accounts/views.rs index 529b54a..69f5888 100644 --- a/src/mastodon_api/accounts/views.rs +++ b/src/mastodon_api/accounts/views.rs @@ -45,7 +45,7 @@ async fn verify_credentials( ) -> Result { 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, ) -> Result { 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, ¤t_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, + ¤t_user.profile.avatar_file_name, + ¤t_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, + ¤t_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)) } diff --git a/src/mastodon_api/users/mod.rs b/src/mastodon_api/users/mod.rs index 70b7922..35a5d0b 100644 --- a/src/mastodon_api/users/mod.rs +++ b/src/mastodon_api/users/mod.rs @@ -1,3 +1,2 @@ pub mod auth; -mod types; pub mod views; diff --git a/src/mastodon_api/users/types.rs b/src/mastodon_api/users/types.rs deleted file mode 100644 index df5732d..0000000 --- a/src/mastodon_api/users/types.rs +++ /dev/null @@ -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, - } - } -} diff --git a/src/mastodon_api/users/views.rs b/src/mastodon_api/users/views.rs index abba684..ea067ff 100644 --- a/src/mastodon_api/users/views.rs +++ b/src/mastodon_api/users/views.rs @@ -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 { 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")]