mas_handlers/graphql/query/
viewer.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Copyright 2024 New Vector Ltd.
// Copyright 2023, 2024 The Matrix.org Foundation C.I.C.
//
// SPDX-License-Identifier: AGPL-3.0-only
// Please see LICENSE in the repository root for full details.

use async_graphql::{Context, Object};

use crate::graphql::{
    model::{Viewer, ViewerSession},
    state::ContextExt,
};

#[derive(Default)]
pub struct ViewerQuery;

#[Object]
impl ViewerQuery {
    /// Get the viewer
    async fn viewer(&self, ctx: &Context<'_>) -> Viewer {
        let requester = ctx.requester();

        if let Some(user) = requester.user() {
            return Viewer::user(user.clone());
        }

        Viewer::anonymous()
    }

    /// Get the viewer's session
    async fn viewer_session(&self, ctx: &Context<'_>) -> ViewerSession {
        let requester = ctx.requester();

        if let Some(session) = requester.browser_session() {
            return ViewerSession::browser_session(session.clone());
        }

        if let Some(session) = requester.oauth2_session() {
            return ViewerSession::oauth2_session(session.clone());
        }

        ViewerSession::anonymous()
    }
}