From ae0f78d8c8deab86d15e2a65b0938651c6e2e08b Mon Sep 17 00:00:00 2001 From: Lennart Date: Fri, 19 Jan 2024 03:00:44 +0200 Subject: [PATCH] fix(website): some minor fixes (#198) This fixes some minor bugs on the website. --- common/src/http/router/mod.rs | 18 +- package.json | 6 +- platform/api/src/api/v1/gql/mod.rs | 2 +- platform/api/src/api/v1/gql/models/color.rs | 57 ++++-- platform/api/src/api/v1/gql/mutations/user.rs | 4 +- platform/api/src/api/v1/gql/playground.html | 6 + platform/website/codegen.cjs | 2 +- .../src/components/chat/messages.svelte | 16 +- platform/website/src/components/player.svelte | 99 ++++++--- .../components/settings/profile/color.svelte | 4 +- .../website/src/components/side-nav.svelte | 8 +- .../src/components/user/default-avatar.svelte | 10 +- platform/website/src/lib/auth.ts | 8 +- platform/website/src/lib/colors.ts | 63 ++++++ platform/website/src/lib/gql.ts | 11 +- platform/website/src/lib/search.ts | 8 +- .../src/routes/(app)/[username]/+layout.ts | 8 +- .../(app)/settings/profile/+page.svelte | 37 ++-- pnpm-lock.yaml | 193 +++++++++--------- schema.graphql | 28 ++- video/player/src/player/runner/ext/drive.rs | 2 +- video/player/src/player/runner/mod.rs | 5 +- 22 files changed, 400 insertions(+), 195 deletions(-) create mode 100644 platform/website/src/lib/colors.ts diff --git a/common/src/http/router/mod.rs b/common/src/http/router/mod.rs index 664b6c61..8972dbd4 100644 --- a/common/src/http/router/mod.rs +++ b/common/src/http/router/mod.rs @@ -48,7 +48,23 @@ impl Router { } Err(err) => { if let Some(error_handler) = error_handler { - return Ok(error_handler((hyper::Request::from_parts(parts, ()), err)).await); + // run post middlewares for error handler response + // TODO: prevent code duplication + let req = hyper::Request::from_parts(parts.clone(), ()); + let mut res = error_handler((req.clone(), err)).await; + for idx in info.post_middleware.iter().copied() { + let (parts, body) = res.into_parts(); + res = match self.post_middlewares[idx].0(( + hyper::Response::from_parts(parts.clone(), body), + req.clone(), + )) + .await + { + Ok(res) => res, + Err(err) => error_handler((req.clone(), err)).await, + }; + } + return Ok(res); } else { return Err(RouterError::Unhandled(err)); } diff --git a/package.json b/package.json index ac03405f..b8aac4c7 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,9 @@ { "private": true, "devDependencies": { - "@commitlint/cli": "^18.4.3", - "@commitlint/config-conventional": "^18.4.3", - "commitlint": "^18.4.3", + "@commitlint/cli": "^18.4.4", + "@commitlint/config-conventional": "^18.4.4", + "commitlint": "^18.4.4", "husky": "^8.0.3", "prettier": "^3.1.1" }, diff --git a/platform/api/src/api/v1/gql/mod.rs b/platform/api/src/api/v1/gql/mod.rs index 2dff2994..ae5f6e01 100644 --- a/platform/api/src/api/v1/gql/mod.rs +++ b/platform/api/src/api/v1/gql/mod.rs @@ -35,7 +35,7 @@ pub fn schema() -> MySchema { .enable_subscription_in_federation() .extension(extensions::Analyzer) .extension(extensions::Tracing) - .limit_complexity(100) // We don't want to allow too complex queries to be executed + .limit_complexity(200) // We don't want to allow too complex queries to be executed .finish() } diff --git a/platform/api/src/api/v1/gql/models/color.rs b/platform/api/src/api/v1/gql/models/color.rs index 30e0771d..ad2193bb 100644 --- a/platform/api/src/api/v1/gql/models/color.rs +++ b/platform/api/src/api/v1/gql/models/color.rs @@ -4,10 +4,10 @@ use async_graphql::{ComplexObject, InputValueError, InputValueResult, Scalar, Sc /// A hex rgb color code. #[derive(Copy, Clone, Debug)] -pub struct Color(i32); +pub struct RgbColor(i32); #[Scalar] -impl ScalarType for Color { +impl ScalarType for RgbColor { fn parse(value: Value) -> InputValueResult { match value { Value::String(s) => { @@ -35,8 +35,8 @@ impl ScalarType for Color { } } -impl Color { - fn rgb(&self) -> (u8, u8, u8) { +impl RgbColor { + fn split(&self) -> (u8, u8, u8) { let r = (self.0 >> 16) & 0xFF; let g = (self.0 >> 8) & 0xFF; let b = self.0 & 0xFF; @@ -44,7 +44,7 @@ impl Color { } } -impl Deref for Color { +impl Deref for RgbColor { type Target = i32; fn deref(&self) -> &Self::Target { @@ -52,34 +52,45 @@ impl Deref for Color { } } -impl From for Color { +impl From for RgbColor { fn from(value: i32) -> Self { Self(value) } } -impl ToString for Color { +impl ToString for RgbColor { fn to_string(&self) -> String { format!("#{:06x}", self.0) } } +/// A HSL color. +#[derive(Copy, Clone, Debug, SimpleObject)] +pub struct HslColor { + /// Hue in degrees, value between 0.0 and 360.0. + pub h: f64, + /// Saturation, value between 0.0 and 1.0. + pub s: f64, + /// Lightness, value between 0.0 and 1.0. + pub l: f64, +} + #[derive(SimpleObject, Clone)] #[graphql(complex)] pub struct DisplayColor { - pub color: Color, + pub rgb: RgbColor, } -impl From for DisplayColor { - fn from(color: Color) -> Self { - Self { color } +impl From for DisplayColor { + fn from(color: RgbColor) -> Self { + Self { rgb: color } } } impl From for DisplayColor { fn from(color: i32) -> Self { Self { - color: Color::from(color), + rgb: RgbColor::from(color), } } } @@ -87,8 +98,8 @@ impl From for DisplayColor { #[ComplexObject] impl DisplayColor { // https://www.rapidtables.com/convert/color/rgb-to-hsl.html - async fn hue(&self) -> f64 { - let (r, g, b) = self.color.rgb(); + async fn hsl(&self) -> HslColor { + let (r, g, b) = self.rgb.split(); let r = r as f64 / 255.0; let g = g as f64 / 255.0; let b = b as f64 / 255.0; @@ -97,7 +108,7 @@ impl DisplayColor { let c_min = r.min(g).min(b); let delta = c_max - c_min; - let h = if delta == 0.0 { + let mut h = if delta == 0.0 { 0.0 } else if c_max == r { 60.0 * (((g - b) / delta) % 6.0) @@ -107,11 +118,23 @@ impl DisplayColor { 60.0 * ((r - g) / delta + 4.0) }; - if h < 0.0 { h + 360.0 } else { h } + if h < 0.0 { + h += 360.0; + } + + let l = (c_max + c_min) / 2.0; + + let s = if delta == 0.0 { + 0.0 + } else { + delta / (1.0 - (2.0 * l - 1.0).abs()) + }; + + HslColor { h, s, l } } async fn is_gray(&self) -> bool { - let (r, g, b) = self.color.rgb(); + let (r, g, b) = self.rgb.split(); // Color is gray when r == g == b r == g && g == b } diff --git a/platform/api/src/api/v1/gql/mutations/user.rs b/platform/api/src/api/v1/gql/mutations/user.rs index 9d544d59..2cc0501a 100644 --- a/platform/api/src/api/v1/gql/mutations/user.rs +++ b/platform/api/src/api/v1/gql/mutations/user.rs @@ -8,7 +8,7 @@ use crate::api::auth::AuthError; use crate::api::v1::gql::error::ext::*; use crate::api::v1::gql::error::{GqlError, Result}; use crate::api::v1::gql::ext::ContextExt; -use crate::api::v1::gql::models::color::Color; +use crate::api::v1::gql::models::color::RgbColor; use crate::api::v1::gql::models::two_fa::{TwoFaRequest, TwoFaResponse}; use crate::api::v1::gql::models::ulid::GqlUlid; use crate::api::v1::gql::models::user::User; @@ -142,7 +142,7 @@ impl UserMutation { async fn display_color<'ctx>( &self, ctx: &Context<'_>, - #[graphql(desc = "New display color.")] color: Color, + #[graphql(desc = "New display color.")] color: RgbColor, ) -> Result> { let global = ctx.get_global::(); let request_context = ctx.get_req_context(); diff --git a/platform/api/src/api/v1/gql/playground.html b/platform/api/src/api/v1/gql/playground.html index f649cd3e..e92850fb 100644 --- a/platform/api/src/api/v1/gql/playground.html +++ b/platform/api/src/api/v1/gql/playground.html @@ -49,10 +49,16 @@ type="application/javascript" > -