mas_handlers/graphql/
state.rs

1// Copyright 2024, 2025 New Vector Ltd.
2// Copyright 2023, 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;
8use mas_matrix::HomeserverConnection;
9use mas_policy::Policy;
10use mas_router::UrlBuilder;
11use mas_storage::{BoxClock, BoxRepository, BoxRng, RepositoryError};
12
13use crate::{Limiter, graphql::Requester, passwords::PasswordManager};
14
15#[async_trait::async_trait]
16pub trait State {
17    async fn repository(&self) -> Result<BoxRepository, RepositoryError>;
18    async fn policy(&self) -> Result<Policy, mas_policy::InstantiateError>;
19    fn password_manager(&self) -> PasswordManager;
20    fn homeserver_connection(&self) -> &dyn HomeserverConnection;
21    fn clock(&self) -> BoxClock;
22    fn rng(&self) -> BoxRng;
23    fn site_config(&self) -> &SiteConfig;
24    fn url_builder(&self) -> &UrlBuilder;
25    fn limiter(&self) -> &Limiter;
26}
27
28pub type BoxState = Box<dyn State + Send + Sync + 'static>;
29
30pub trait ContextExt {
31    fn state(&self) -> &BoxState;
32
33    fn requester(&self) -> &Requester;
34}
35
36impl ContextExt for async_graphql::Context<'_> {
37    fn state(&self) -> &BoxState {
38        self.data_unchecked()
39    }
40
41    fn requester(&self) -> &Requester {
42        self.data_unchecked()
43    }
44}