forked from Roflin/gamenight
Small rewrite to allow fetching owned game of users
This commit is contained in:
@@ -6,6 +6,9 @@ use gamenight_api_client_rs::apis::{configuration::Configuration, Error};
|
||||
use inquire::InquireError;
|
||||
use dyn_clone::DynClone;
|
||||
pub use clear_screen;
|
||||
use jsonwebtoken::{decode, DecodingKey, Validation};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::domain::config::{Config, ConfigError};
|
||||
|
||||
@@ -33,6 +36,12 @@ pub struct GamenightState {
|
||||
gamenight_configuration: Config,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct Claims {
|
||||
exp: i64,
|
||||
uid: Uuid
|
||||
}
|
||||
|
||||
impl GamenightState {
|
||||
pub fn new() -> Self{
|
||||
Self {
|
||||
@@ -40,6 +49,18 @@ impl GamenightState {
|
||||
gamenight_configuration: Config::new()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_user_id(&self) -> Result<Uuid, FlowError> {
|
||||
let decoding_key = DecodingKey::from_secret(b"");
|
||||
let mut validation = Validation::default();
|
||||
validation.insecure_disable_signature_validation();
|
||||
let claims = decode::<Claims>(
|
||||
self.api_configuration.bearer_access_token.as_ref().unwrap(),
|
||||
&decoding_key,
|
||||
&validation)?.claims;
|
||||
|
||||
Ok(claims.uid)
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for GamenightState {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
|
||||
use gamenight_api_client_rs::{apis::default_api::{game_get, owned_games_get}, models::GameId};
|
||||
use gamenight_api_client_rs::{apis::default_api::{game_get, owned_games_get}, models::{GameId, UserId}};
|
||||
use inquire::Select;
|
||||
use uuid::Uuid;
|
||||
|
||||
@@ -28,10 +28,13 @@ impl<'a> Flow<'a> for ViewGame {
|
||||
|
||||
println!("{}", game);
|
||||
|
||||
let owned_games: Vec<Uuid> = owned_games_get(&state.api_configuration).await?.iter().map(|x| -> Result<Uuid, FlowError> { Ok(Uuid::parse_str(x)?) }).collect::<Result<Vec<Uuid>, FlowError>>()?;
|
||||
let my_uid = state.get_user_id()?;
|
||||
let request = UserId{ user_id: my_uid.to_string() };
|
||||
let owned_games: Vec<Uuid> = owned_games_get(&state.api_configuration, Some(request)).await?
|
||||
.iter().map(|x| -> Result<Uuid, FlowError> {
|
||||
Ok(Uuid::parse_str(x)?)
|
||||
}).collect::<Result<Vec<Uuid>, FlowError>>()?;
|
||||
|
||||
println!("game_id {:?}, owned_games {:?}", game.id, owned_games);
|
||||
|
||||
let own_or_disown: Box<dyn Flow<'a> + Send> =
|
||||
if owned_games.into_iter().any(|x| x == game.id) {
|
||||
Box::new(Disown::new(self.game.id))
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
|
||||
use gamenight_api_client_rs::{apis::default_api::{participants_get, user_get}, models::{GamenightId, UserId}};
|
||||
use inquire::Select;
|
||||
use jsonwebtoken::{decode, DecodingKey, Validation};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::{domain::{gamenight::Gamenight, participants::Participants, user::User}, flows::{exit::Exit, join::Join, leave::Leave}};
|
||||
@@ -22,15 +20,6 @@ impl ViewGamenight {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct Claims {
|
||||
exp: i64,
|
||||
uid: Uuid
|
||||
}
|
||||
|
||||
impl From<jsonwebtoken::errors::Error> for FlowError {
|
||||
fn from(value: jsonwebtoken::errors::Error) -> Self {
|
||||
Self {
|
||||
@@ -54,16 +43,10 @@ impl<'a> Flow<'a> for ViewGamenight {
|
||||
}
|
||||
|
||||
println!("{}\nwho: {}", self.gamenight, Participants(&users));
|
||||
let decoding_key = DecodingKey::from_secret(b"");
|
||||
let mut validation = Validation::default();
|
||||
validation.insecure_disable_signature_validation();
|
||||
let claims = decode::<Claims>(
|
||||
state.api_configuration.bearer_access_token.as_ref().unwrap(),
|
||||
&decoding_key,
|
||||
&validation)?.claims;
|
||||
|
||||
|
||||
let my_uid = state.get_user_id()?;
|
||||
let join_or_leave: Box<dyn Flow<'a> + Send> =
|
||||
if users.iter().map(|x| {x.id}).any(|x| x == claims.uid) {
|
||||
if users.iter().map(|x| {x.id}).any(|x| x == my_uid) {
|
||||
Box::new(Leave::new(self.gamenight.id))
|
||||
}
|
||||
else {
|
||||
|
||||
Reference in New Issue
Block a user