mas_templates/context/
ext.rs

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