mas_handlers/activity_tracker/
bound.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 mas_data_model::{BrowserSession, CompatSession, Session};
10use mas_storage::Clock;
11
12use crate::activity_tracker::ActivityTracker;
13
14/// An activity tracker with an IP address bound to it.
15#[derive(Clone)]
16pub struct Bound {
17    tracker: ActivityTracker,
18    ip: Option<IpAddr>,
19}
20
21impl Bound {
22    /// Create a new bound activity tracker.
23    #[must_use]
24    pub fn new(tracker: ActivityTracker, ip: Option<IpAddr>) -> Self {
25        Self { tracker, ip }
26    }
27
28    /// Get the IP address bound to this activity tracker.
29    #[must_use]
30    pub fn ip(&self) -> Option<IpAddr> {
31        self.ip
32    }
33
34    /// Record activity in an OAuth 2.0 session.
35    pub async fn record_oauth2_session(&self, clock: &dyn Clock, session: &Session) {
36        self.tracker
37            .record_oauth2_session(clock, session, self.ip)
38            .await;
39    }
40
41    /// Record activity in a compatibility session.
42    pub async fn record_compat_session(&self, clock: &dyn Clock, session: &CompatSession) {
43        self.tracker
44            .record_compat_session(clock, session, self.ip)
45            .await;
46    }
47
48    /// Record activity in a browser session.
49    pub async fn record_browser_session(&self, clock: &dyn Clock, session: &BrowserSession) {
50        self.tracker
51            .record_browser_session(clock, session, self.ip)
52            .await;
53    }
54}