mas_handlers/graphql/model/viewer/
mod.rs

1// Copyright 2024 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 async_graphql::Union;
8
9use crate::graphql::model::{BrowserSession, OAuth2Session, User};
10
11mod anonymous;
12pub use self::anonymous::Anonymous;
13
14/// Represents the current viewer
15#[derive(Union)]
16pub enum Viewer {
17    User(User),
18    Anonymous(Anonymous),
19}
20
21impl Viewer {
22    pub fn user(user: mas_data_model::User) -> Self {
23        Self::User(User(user))
24    }
25
26    pub fn anonymous() -> Self {
27        Self::Anonymous(Anonymous)
28    }
29}
30
31/// Represents the current viewer's session
32#[derive(Union)]
33pub enum ViewerSession {
34    BrowserSession(Box<BrowserSession>),
35    OAuth2Session(Box<OAuth2Session>),
36    Anonymous(Anonymous),
37}
38
39impl ViewerSession {
40    pub fn browser_session(session: mas_data_model::BrowserSession) -> Self {
41        Self::BrowserSession(Box::new(BrowserSession(session)))
42    }
43
44    pub fn oauth2_session(session: mas_data_model::Session) -> Self {
45        Self::OAuth2Session(Box::new(OAuth2Session(session)))
46    }
47
48    pub fn anonymous() -> Self {
49        Self::Anonymous(Anonymous)
50    }
51}