mas_handlers/graphql/model/
mod.rs

1// Copyright 2024, 2025 New Vector Ltd.
2// Copyright 2022-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::{Enum, Interface, Object, SimpleObject};
8use chrono::{DateTime, Utc};
9
10mod browser_sessions;
11mod compat_sessions;
12mod cursor;
13mod matrix;
14mod node;
15mod oauth;
16mod site_config;
17mod upstream_oauth;
18mod users;
19mod viewer;
20
21pub use self::{
22    browser_sessions::{Authentication, BrowserSession},
23    compat_sessions::{CompatSession, CompatSsoLogin},
24    cursor::{Cursor, NodeCursor},
25    node::{Node, NodeType},
26    oauth::{OAuth2Client, OAuth2Session},
27    site_config::{SITE_CONFIG_ID, SiteConfig},
28    upstream_oauth::{UpstreamOAuth2Link, UpstreamOAuth2Provider},
29    users::{AppSession, User, UserEmail, UserEmailAuthentication, UserRecoveryTicket},
30    viewer::{Anonymous, Viewer, ViewerSession},
31};
32
33/// An object with a creation date.
34#[derive(Interface)]
35#[graphql(field(
36    name = "created_at",
37    desc = "When the object was created.",
38    ty = "DateTime<Utc>"
39))]
40pub enum CreationEvent {
41    Authentication(Box<Authentication>),
42    CompatSession(Box<CompatSession>),
43    BrowserSession(Box<BrowserSession>),
44    UserEmail(Box<UserEmail>),
45    UserEmailAuthentication(Box<UserEmailAuthentication>),
46    UserRecoveryTicket(Box<UserRecoveryTicket>),
47    UpstreamOAuth2Provider(Box<UpstreamOAuth2Provider>),
48    UpstreamOAuth2Link(Box<UpstreamOAuth2Link>),
49    OAuth2Session(Box<OAuth2Session>),
50}
51
52pub struct PreloadedTotalCount(pub Option<usize>);
53
54#[Object]
55impl PreloadedTotalCount {
56    /// Identifies the total count of items in the connection.
57    async fn total_count(&self) -> Result<usize, async_graphql::Error> {
58        self.0
59            .ok_or_else(|| async_graphql::Error::new("total count not preloaded"))
60    }
61}
62
63/// The state of a session
64#[derive(Enum, Copy, Clone, Eq, PartialEq)]
65pub enum SessionState {
66    /// The session is active.
67    Active,
68
69    /// The session is no longer active.
70    Finished,
71}
72
73/// The type of a user agent
74#[derive(Enum, Copy, Clone, Eq, PartialEq)]
75pub enum DeviceType {
76    /// A personal computer, laptop or desktop
77    Pc,
78
79    /// A mobile phone. Can also sometimes be a tablet.
80    Mobile,
81
82    /// A tablet
83    Tablet,
84
85    /// Unknown device type
86    Unknown,
87}
88
89impl From<mas_data_model::DeviceType> for DeviceType {
90    fn from(device_type: mas_data_model::DeviceType) -> Self {
91        match device_type {
92            mas_data_model::DeviceType::Pc => Self::Pc,
93            mas_data_model::DeviceType::Mobile => Self::Mobile,
94            mas_data_model::DeviceType::Tablet => Self::Tablet,
95            mas_data_model::DeviceType::Unknown => Self::Unknown,
96        }
97    }
98}
99
100/// A parsed user agent string
101#[derive(SimpleObject)]
102pub struct UserAgent {
103    /// The user agent string
104    pub raw: String,
105
106    /// The name of the browser
107    pub name: Option<String>,
108
109    /// The version of the browser
110    pub version: Option<String>,
111
112    /// The operating system name
113    pub os: Option<String>,
114
115    /// The operating system version
116    pub os_version: Option<String>,
117
118    /// The device model
119    pub model: Option<String>,
120
121    /// The device type
122    pub device_type: DeviceType,
123}
124
125impl From<mas_data_model::UserAgent> for UserAgent {
126    fn from(ua: mas_data_model::UserAgent) -> Self {
127        Self {
128            raw: ua.raw,
129            name: ua.name,
130            version: ua.version,
131            os: ua.os,
132            os_version: ua.os_version,
133            model: ua.model,
134            device_type: ua.device_type.into(),
135        }
136    }
137}