use chrono::{DateTime, Utc}; use juniper::{FieldError, FieldResult, graphql_object}; use serde::{Deserialize, Serialize}; use crate::{ database::DataContext, schema::{Chat, Float, GameUser, Long, Template}, }; #[derive(sqlx::FromRow, Deserialize, Serialize)] pub struct User { id: i32, username: String, country_code: String, country_name: String, cover_url: String, avatar_url: String, pp: f32, global_rank: Option, country_rank: Option, total_score: Option, ranked_score: Option, hit_accuracy: Option, play_count: Option, level: Option, level_progress: Option, last_refreshed: DateTime, } #[graphql_object(description = "Represents an osu! user (and a bingo user)", Context = DataContext)] impl User { pub fn id(&self) -> i32 { self.id } /// Matches the user's osu! username pub fn username(&self) -> &str { &self.username } /// The country they are from (US, GE, CA) pub fn country_code(&self) -> &str { &self.country_code } /// The country they are from (United States, Germany, Canada) pub fn country_name(&self) -> &str { &self.country_name } /// The url for their profile banner pub fn cover_url(&self) -> &str { &self.cover_url } /// The url for their profile picture pub fn avatar_url(&self) -> &str { &self.avatar_url } pub fn pp(&self) -> Float { self.pp.into() } pub fn global_rank(&self) -> Option { self.global_rank } pub fn country_rank(&self) -> Option { self.country_rank } pub fn total_score(&self) -> Option { self.total_score.map(|x| x.into()) } pub fn ranked_score(&self) -> Option { self.ranked_score.map(|x| x.into()) } pub fn hit_accuracy(&self) -> Option { self.hit_accuracy.map(|x| x.into()) } pub fn play_count(&self) -> Option { self.play_count } pub fn level(&self) -> Option { self.level } pub fn level_progress(&self) -> Option { self.level_progress } pub fn last_refreshed(&self) -> &DateTime { &self.last_refreshed } pub async fn in_games(&self, context: &DataContext) -> FieldResult> { context .acquire() .await .get_user_gameusers(self.id) .await .map_err(FieldError::from) } pub async fn chats(&self, context: &DataContext) -> FieldResult> { context .acquire() .await .get_user_chats(self.id) .await .map_err(FieldError::from) } pub async fn templates(&self, context: &DataContext) -> FieldResult> { context .acquire() .await .get_user_templates(self.id) .await .map_err(FieldError::from) } }