Added update and reload actions for user settings
This commit is contained in:
69
src/users.rs
69
src/users.rs
@@ -5,6 +5,7 @@ use axum::{
|
||||
extract::{Query, State},
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
use sqlx::{PgPool, query, query_as, query_scalar, types::Uuid};
|
||||
use tower_sessions::Session;
|
||||
|
||||
@@ -88,10 +89,7 @@ impl User {
|
||||
return Ok(user);
|
||||
}
|
||||
|
||||
let response = reqwest::get(format!("https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=ABBFE30D7BDC1FD582DB1A2CEBD563B4&steamids={steamid}"))
|
||||
.await?.bytes().await?;
|
||||
let json: serde_json::Value = serde_json::from_slice(&response)?;
|
||||
let raw_user = &json["response"]["players"][0];
|
||||
let raw_user = Self::fetch_steam(&steamid).await?;
|
||||
let mut user = User::new(
|
||||
raw_user["steamid"].to_string().trim_matches('"'),
|
||||
raw_user["personaname"].to_string().trim_matches('"'),
|
||||
@@ -100,6 +98,20 @@ impl User {
|
||||
user.create(pool).await?;
|
||||
Ok(user)
|
||||
}
|
||||
|
||||
async fn fetch_steam(steamid: &String) -> Result<Value, Box<dyn Error>> {
|
||||
let response = reqwest::get(format!("https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=ABBFE30D7BDC1FD582DB1A2CEBD563B4&steamids={steamid}"))
|
||||
.await?.bytes().await?;
|
||||
let mut json: serde_json::Value = serde_json::from_slice(&response)?;
|
||||
Ok(json["response"]["players"][0].take())
|
||||
}
|
||||
|
||||
async fn update_from_steam(&mut self, pool: &PgPool) -> Result<(), Box<dyn Error>> {
|
||||
let raw_user = Self::fetch_steam(&self.steamid).await?;
|
||||
self.name = raw_user["personaname"].to_string().trim_matches('"').into();
|
||||
self.avatar = raw_user["avatarfull"].to_string().trim_matches('"').into();
|
||||
self.update(pool).await
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
@@ -158,7 +170,9 @@ pub async fn login(
|
||||
session: Session,
|
||||
) -> Result<(), AppError> {
|
||||
if !info.is_valid().await? {
|
||||
return Err(AppError::OpenIdError("Some query parameters are erronous or lacking.".into()));
|
||||
return Err(AppError::OpenIdError(
|
||||
"Some query parameters are erronous or lacking.".into(),
|
||||
));
|
||||
}
|
||||
let steamid = info
|
||||
.identity
|
||||
@@ -174,10 +188,7 @@ pub async fn logout(session: Session) -> Result<(), AppError> {
|
||||
}
|
||||
|
||||
#[axum::debug_handler]
|
||||
pub async fn me(
|
||||
State(pool): State<PgPool>,
|
||||
session: Session,
|
||||
) -> Result<Json<User>, AppError> {
|
||||
pub async fn me(State(pool): State<PgPool>, session: Session) -> Result<Json<User>, AppError> {
|
||||
if let Some(uuid) = session.get("uuid").await? {
|
||||
let user = User::fetch_id(&pool, uuid).await?;
|
||||
Ok(Json(user))
|
||||
@@ -185,3 +196,43 @@ pub async fn me(
|
||||
Err(AppError::Unauthorised)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct Settings {
|
||||
username: String,
|
||||
avatar: String,
|
||||
}
|
||||
|
||||
#[axum::debug_handler]
|
||||
pub async fn update(
|
||||
State(pool): State<PgPool>,
|
||||
session: Session,
|
||||
data: Json<Settings>,
|
||||
) -> Result<(), AppError> {
|
||||
if let Some(uuid) = session.get("uuid").await? {
|
||||
let mut user = User::fetch_id(&pool, uuid).await?;
|
||||
|
||||
if !data.username.is_empty() {
|
||||
user.name = data.username.clone();
|
||||
}
|
||||
if !data.avatar.is_empty() {
|
||||
user.avatar = data.avatar.clone();
|
||||
}
|
||||
|
||||
user.update(&pool).await?;
|
||||
|
||||
Ok(())
|
||||
} else {
|
||||
Err(AppError::Unauthorised)
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn reload(State(pool): State<PgPool>, session: Session) -> Result<(), AppError> {
|
||||
if let Some(uuid) = session.get("uuid").await? {
|
||||
let mut user = User::fetch_id(&pool, uuid).await?;
|
||||
user.update_from_steam(&pool).await?;
|
||||
Ok(())
|
||||
} else {
|
||||
Err(AppError::Unauthorised)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user