From aa2e2229bc695586715fee0f32314999e65e18d1 Mon Sep 17 00:00:00 2001 From: Quentin Gliech Date: Fri, 29 Mar 2024 13:19:17 +0100 Subject: [PATCH] Finish moving the site config --- crates/cli/src/app_state.rs | 3 ++- crates/cli/src/util.rs | 5 +++-- crates/handlers/src/compat/login.rs | 9 ++++----- crates/handlers/src/compat/refresh.rs | 4 ++-- crates/handlers/src/lib.rs | 3 +-- crates/handlers/src/oauth2/token.rs | 4 ++-- crates/handlers/src/test_utils.rs | 6 +++--- crates/templates/src/context.rs | 3 ++- .../src/context/ext.rs} | 20 ++++++++++++++----- crates/templates/src/lib.rs | 4 ++-- 10 files changed, 36 insertions(+), 25 deletions(-) rename crates/{handlers/src/site_config.rs => templates/src/context/ext.rs} (74%) diff --git a/crates/cli/src/app_state.rs b/crates/cli/src/app_state.rs index 1480319c..1db8491a 100644 --- a/crates/cli/src/app_state.rs +++ b/crates/cli/src/app_state.rs @@ -19,9 +19,10 @@ use axum::{ extract::{FromRef, FromRequestParts}, }; use ipnetwork::IpNetwork; +use mas_data_model::SiteConfig; use mas_handlers::{ passwords::PasswordManager, ActivityTracker, BoundActivityTracker, CookieManager, ErrorWrapper, - HttpClientFactory, MetadataCache, SiteConfig, + HttpClientFactory, MetadataCache, }; use mas_i18n::Translator; use mas_keystore::{Encrypter, Keystore}; diff --git a/crates/cli/src/util.rs b/crates/cli/src/util.rs index 0fb27e7e..ae45bdcd 100644 --- a/crates/cli/src/util.rs +++ b/crates/cli/src/util.rs @@ -19,11 +19,12 @@ use mas_config::{ BrandingConfig, DatabaseConfig, EmailConfig, EmailSmtpMode, EmailTransportKind, ExperimentalConfig, MatrixConfig, PasswordsConfig, PolicyConfig, TemplatesConfig, }; +use mas_data_model::SiteConfig; use mas_email::{MailTransport, Mailer}; -use mas_handlers::{passwords::PasswordManager, ActivityTracker, SiteConfig, SiteConfigExt}; +use mas_handlers::{passwords::PasswordManager, ActivityTracker}; use mas_policy::PolicyFactory; use mas_router::UrlBuilder; -use mas_templates::{TemplateLoadingError, Templates}; +use mas_templates::{SiteConfigExt, TemplateLoadingError, Templates}; use sqlx::{ postgres::{PgConnectOptions, PgPoolOptions}, ConnectOptions, PgConnection, PgPool, diff --git a/crates/handlers/src/compat/login.rs b/crates/handlers/src/compat/login.rs index 2bfdf9d4..d3111739 100644 --- a/crates/handlers/src/compat/login.rs +++ b/crates/handlers/src/compat/login.rs @@ -16,7 +16,9 @@ use axum::{extract::State, response::IntoResponse, Json, TypedHeader}; use chrono::Duration; use hyper::StatusCode; use mas_axum_utils::sentry::SentryEventID; -use mas_data_model::{CompatSession, CompatSsoLoginState, Device, TokenType, User, UserAgent}; +use mas_data_model::{ + CompatSession, CompatSsoLoginState, Device, SiteConfig, TokenType, User, UserAgent, +}; use mas_matrix::BoxHomeserverConnection; use mas_storage::{ compat::{ @@ -34,10 +36,7 @@ use thiserror::Error; use zeroize::Zeroizing; use super::MatrixError; -use crate::{ - impl_from_error_for_route, passwords::PasswordManager, site_config::SiteConfig, - BoundActivityTracker, -}; +use crate::{impl_from_error_for_route, passwords::PasswordManager, BoundActivityTracker}; #[derive(Debug, Serialize)] #[serde(tag = "type")] diff --git a/crates/handlers/src/compat/refresh.rs b/crates/handlers/src/compat/refresh.rs index 7e982c9f..71bf2672 100644 --- a/crates/handlers/src/compat/refresh.rs +++ b/crates/handlers/src/compat/refresh.rs @@ -16,7 +16,7 @@ use axum::{extract::State, response::IntoResponse, Json}; use chrono::Duration; use hyper::StatusCode; use mas_axum_utils::sentry::SentryEventID; -use mas_data_model::{TokenFormatError, TokenType}; +use mas_data_model::{SiteConfig, TokenFormatError, TokenType}; use mas_storage::{ compat::{CompatAccessTokenRepository, CompatRefreshTokenRepository, CompatSessionRepository}, BoxClock, BoxRepository, BoxRng, Clock, @@ -26,7 +26,7 @@ use serde_with::{serde_as, DurationMilliSeconds}; use thiserror::Error; use super::MatrixError; -use crate::{impl_from_error_for_route, site_config::SiteConfig, BoundActivityTracker}; +use crate::{impl_from_error_for_route, BoundActivityTracker}; #[derive(Debug, Deserialize)] pub struct RequestBody { diff --git a/crates/handlers/src/lib.rs b/crates/handlers/src/lib.rs index 05bc1f41..3a0cfd65 100644 --- a/crates/handlers/src/lib.rs +++ b/crates/handlers/src/lib.rs @@ -41,6 +41,7 @@ use hyper::{ StatusCode, Version, }; use mas_axum_utils::{cookies::CookieJar, FancyError}; +use mas_data_model::SiteConfig; use mas_http::CorsLayerExt; use mas_keystore::{Encrypter, Keystore}; use mas_matrix::BoxHomeserverConnection; @@ -63,7 +64,6 @@ mod views; mod activity_tracker; mod preferred_language; -mod site_config; #[cfg(test)] mod test_utils; @@ -91,7 +91,6 @@ pub use self::{ activity_tracker::{ActivityTracker, Bound as BoundActivityTracker}, graphql::schema as graphql_schema, preferred_language::PreferredLanguage, - site_config::{SiteConfig, SiteConfigExt}, upstream_oauth2::cache::MetadataCache, }; diff --git a/crates/handlers/src/oauth2/token.rs b/crates/handlers/src/oauth2/token.rs index 4093d44e..6cce660b 100644 --- a/crates/handlers/src/oauth2/token.rs +++ b/crates/handlers/src/oauth2/token.rs @@ -22,7 +22,7 @@ use mas_axum_utils::{ sentry::SentryEventID, }; use mas_data_model::{ - AuthorizationGrantStage, Client, Device, DeviceCodeGrantState, TokenType, UserAgent, + AuthorizationGrantStage, Client, Device, DeviceCodeGrantState, SiteConfig, TokenType, UserAgent, }; use mas_keystore::{Encrypter, Keystore}; use mas_oidc_client::types::scope::ScopeToken; @@ -54,7 +54,7 @@ use ulid::Ulid; use url::Url; use super::{generate_id_token, generate_token_pair}; -use crate::{impl_from_error_for_route, site_config::SiteConfig, BoundActivityTracker}; +use crate::{impl_from_error_for_route, BoundActivityTracker}; #[serde_as] #[skip_serializing_none] diff --git a/crates/handlers/src/test_utils.rs b/crates/handlers/src/test_utils.rs index d0a81c5a..3aaf02b2 100644 --- a/crates/handlers/src/test_utils.rs +++ b/crates/handlers/src/test_utils.rs @@ -37,6 +37,7 @@ use mas_axum_utils::{ http_client_factory::HttpClientFactory, ErrorWrapper, }; +use mas_data_model::SiteConfig; use mas_i18n::Translator; use mas_keystore::{Encrypter, JsonWebKey, JsonWebKeySet, Keystore, PrivateKey}; use mas_matrix::{BoxHomeserverConnection, HomeserverConnection, MockHomeserverConnection}; @@ -44,7 +45,7 @@ use mas_policy::{InstantiateError, Policy, PolicyFactory}; use mas_router::{SimpleRoute, UrlBuilder}; use mas_storage::{clock::MockClock, BoxClock, BoxRepository, BoxRng, Repository}; use mas_storage_pg::{DatabaseError, PgRepository}; -use mas_templates::Templates; +use mas_templates::{SiteConfigExt, Templates}; use rand::SeedableRng; use rand_chacha::ChaChaRng; use serde::{de::DeserializeOwned, Serialize}; @@ -54,9 +55,8 @@ use url::Url; use crate::{ passwords::{Hasher, PasswordManager}, - site_config::SiteConfig, upstream_oauth2::cache::MetadataCache, - ActivityTracker, BoundActivityTracker, SiteConfigExt, + ActivityTracker, BoundActivityTracker, }; // This might fail if it's not the first time it's being called, which is fine, diff --git a/crates/templates/src/context.rs b/crates/templates/src/context.rs index de1f7cc0..c9069e26 100644 --- a/crates/templates/src/context.rs +++ b/crates/templates/src/context.rs @@ -15,6 +15,7 @@ //! Contexts used in templates mod branding; +mod ext; mod features; use std::{ @@ -40,7 +41,7 @@ use serde::{ser::SerializeStruct, Deserialize, Serialize}; use ulid::Ulid; use url::Url; -pub use self::{branding::SiteBranding, features::SiteFeatures}; +pub use self::{branding::SiteBranding, ext::SiteConfigExt, features::SiteFeatures}; use crate::{FieldError, FormField, FormState}; /// Helper trait to construct context wrappers diff --git a/crates/handlers/src/site_config.rs b/crates/templates/src/context/ext.rs similarity index 74% rename from crates/handlers/src/site_config.rs rename to crates/templates/src/context/ext.rs index 46769433..f92f226b 100644 --- a/crates/handlers/src/site_config.rs +++ b/crates/templates/src/context/ext.rs @@ -1,4 +1,4 @@ -// Copyright 2023, 2024 The Matrix.org Foundation C.I.C. +// Copyright 2024 The Matrix.org Foundation C.I.C. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -12,12 +12,22 @@ // See the License for the specific language governing permissions and // limitations under the License. -pub use mas_data_model::SiteConfig; -use mas_templates::{SiteBranding, SiteFeatures}; +use mas_data_model::SiteConfig; -#[allow(clippy::module_name_repetitions)] -pub trait SiteConfigExt { +use super::{SiteBranding, SiteFeatures}; + +mod private { + pub trait Sealed {} + impl Sealed for mas_data_model::SiteConfig {} +} + +/// Extension trait for [`SiteConfig`] to construct [`SiteBranding`] and +/// [`SiteFeatures`] from it. +pub trait SiteConfigExt: private::Sealed { + /// Construct a [`SiteBranding`] from the [`SiteConfig`]. fn templates_branding(&self) -> SiteBranding; + + /// Construct a [`SiteFeatures`] from the [`SiteConfig`]. fn templates_features(&self) -> SiteFeatures; } diff --git a/crates/templates/src/lib.rs b/crates/templates/src/lib.rs index d5dc67d8..10b271ad 100644 --- a/crates/templates/src/lib.rs +++ b/crates/templates/src/lib.rs @@ -47,8 +47,8 @@ pub use self::{ EmailVerificationPageContext, EmptyContext, ErrorContext, FormPostContext, IndexContext, LoginContext, LoginFormField, NotFoundContext, PolicyViolationContext, PostAuthContext, PostAuthContextInner, ReauthContext, ReauthFormField, RegisterContext, RegisterFormField, - SiteBranding, SiteFeatures, TemplateContext, UpstreamExistingLinkContext, UpstreamRegister, - UpstreamRegisterFormField, UpstreamSuggestLink, WithCsrf, WithLanguage, + SiteBranding, SiteConfigExt, SiteFeatures, TemplateContext, UpstreamExistingLinkContext, + UpstreamRegister, UpstreamRegisterFormField, UpstreamSuggestLink, WithCsrf, WithLanguage, WithOptionalSession, WithSession, }, forms::{FieldError, FormError, FormField, FormState, ToFormState},