mas_data_model/compat/
session.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 std::net::IpAddr;
8
9use chrono::{DateTime, Utc};
10use serde::Serialize;
11use ulid::Ulid;
12
13use super::Device;
14use crate::{InvalidTransitionError, UserAgent};
15
16#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]
17pub enum CompatSessionState {
18    #[default]
19    Valid,
20    Finished {
21        finished_at: DateTime<Utc>,
22    },
23}
24
25impl CompatSessionState {
26    /// Returns `true` if the compat session state is [`Valid`].
27    ///
28    /// [`Valid`]: CompatSessionState::Valid
29    #[must_use]
30    pub fn is_valid(&self) -> bool {
31        matches!(self, Self::Valid)
32    }
33
34    /// Returns `true` if the compat session state is [`Finished`].
35    ///
36    /// [`Finished`]: CompatSessionState::Finished
37    #[must_use]
38    pub fn is_finished(&self) -> bool {
39        matches!(self, Self::Finished { .. })
40    }
41
42    /// Transitions the session state to [`Finished`].
43    ///
44    /// # Parameters
45    ///
46    /// * `finished_at` - The time at which the session was finished.
47    ///
48    /// # Errors
49    ///
50    /// Returns an error if the session state is already [`Finished`].
51    ///
52    /// [`Finished`]: CompatSessionState::Finished
53    pub fn finish(self, finished_at: DateTime<Utc>) -> Result<Self, InvalidTransitionError> {
54        match self {
55            Self::Valid => Ok(Self::Finished { finished_at }),
56            Self::Finished { .. } => Err(InvalidTransitionError),
57        }
58    }
59
60    #[must_use]
61    pub fn finished_at(&self) -> Option<DateTime<Utc>> {
62        match self {
63            CompatSessionState::Valid => None,
64            CompatSessionState::Finished { finished_at } => Some(*finished_at),
65        }
66    }
67}
68
69#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
70pub struct CompatSession {
71    pub id: Ulid,
72    pub state: CompatSessionState,
73    pub user_id: Ulid,
74    pub device: Option<Device>,
75    pub human_name: Option<String>,
76    pub user_session_id: Option<Ulid>,
77    pub created_at: DateTime<Utc>,
78    pub is_synapse_admin: bool,
79    pub user_agent: Option<UserAgent>,
80    pub last_active_at: Option<DateTime<Utc>>,
81    pub last_active_ip: Option<IpAddr>,
82}
83
84impl std::ops::Deref for CompatSession {
85    type Target = CompatSessionState;
86
87    fn deref(&self) -> &Self::Target {
88        &self.state
89    }
90}
91
92impl CompatSession {
93    /// Marks the session as finished.
94    ///
95    /// # Parameters
96    ///
97    /// * `finished_at` - The time at which the session was finished.
98    ///
99    /// # Errors
100    ///
101    /// Returns an error if the session is already finished.
102    pub fn finish(mut self, finished_at: DateTime<Utc>) -> Result<Self, InvalidTransitionError> {
103        self.state = self.state.finish(finished_at)?;
104        Ok(self)
105    }
106}