Skip to main content

mas_templates/context/
ext.rs

1// Copyright 2025, 2026 Element Creations Ltd.
2// Copyright 2024, 2025 New Vector Ltd.
3// Copyright 2024 The Matrix.org Foundation C.I.C.
4//
5// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
6// Please see LICENSE files in the repository root for full details.
7
8use mas_data_model::SiteConfig;
9
10use super::{SiteBranding, SiteFeatures};
11
12mod private {
13    pub trait Sealed {}
14    impl Sealed for mas_data_model::SiteConfig {}
15}
16
17/// Extension trait for [`SiteConfig`] to construct [`SiteBranding`] and
18/// [`SiteFeatures`] from it.
19pub trait SiteConfigExt: private::Sealed {
20    /// Construct a [`SiteBranding`] from the [`SiteConfig`].
21    fn templates_branding(&self) -> SiteBranding;
22
23    /// Construct a [`SiteFeatures`] from the [`SiteConfig`].
24    fn templates_features(&self) -> SiteFeatures;
25}
26
27impl SiteConfigExt for SiteConfig {
28    fn templates_branding(&self) -> SiteBranding {
29        let mut branding = SiteBranding::new(self.server_name.clone());
30
31        if let Some(policy_uri) = &self.policy_uri {
32            branding = branding.with_policy_uri(policy_uri.as_str());
33        }
34
35        if let Some(tos_uri) = &self.tos_uri {
36            branding = branding.with_tos_uri(tos_uri.as_str());
37        }
38
39        if let Some(imprint) = &self.imprint {
40            branding = branding.with_imprint(imprint.as_str());
41        }
42
43        branding
44    }
45
46    fn templates_features(&self) -> SiteFeatures {
47        SiteFeatures {
48            password_registration: self.password_registration_enabled,
49            password_registration_email_required: self.password_registration_email_required,
50            password_login: self.password_login_enabled,
51            account_recovery: self.account_recovery_allowed,
52            login_with_email_allowed: self.login_with_email_allowed,
53            minimum_password_complexity: self.minimum_password_complexity,
54        }
55    }
56}