From c6b759c56db9d9a9670adb09d21fa7086a5a364b Mon Sep 17 00:00:00 2001 From: Quentin Gliech Date: Thu, 18 Jul 2024 10:12:50 +0200 Subject: [PATCH] storage: add a filter by last active time on browser sessions --- crates/storage-pg/src/user/session.rs | 8 ++++++- crates/storage/src/user/session.rs | 34 ++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/crates/storage-pg/src/user/session.rs b/crates/storage-pg/src/user/session.rs index 10efbcd7..95ae1587 100644 --- a/crates/storage-pg/src/user/session.rs +++ b/crates/storage-pg/src/user/session.rs @@ -1,4 +1,4 @@ -// Copyright 2022, 2023 The Matrix.org Foundation C.I.C. +// Copyright 2022-2024 The Matrix.org Foundation C.I.C. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -147,6 +147,12 @@ impl crate::filter::Filter for BrowserSessionFilter<'_> { Expr::col((UserSessions::Table, UserSessions::FinishedAt)).is_not_null() } })) + .add_option(self.last_active_after().map(|last_active_after| { + Expr::col((UserSessions::Table, UserSessions::LastActiveAt)).gt(last_active_after) + })) + .add_option(self.last_active_before().map(|last_active_before| { + Expr::col((UserSessions::Table, UserSessions::LastActiveAt)).lt(last_active_before) + })) } } diff --git a/crates/storage/src/user/session.rs b/crates/storage/src/user/session.rs index a10c65b6..953aa786 100644 --- a/crates/storage/src/user/session.rs +++ b/crates/storage/src/user/session.rs @@ -1,4 +1,4 @@ -// Copyright 2022, 2023 The Matrix.org Foundation C.I.C. +// Copyright 2022-2024 The Matrix.org Foundation C.I.C. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -45,6 +45,8 @@ impl BrowserSessionState { pub struct BrowserSessionFilter<'a> { user: Option<&'a User>, state: Option, + last_active_before: Option>, + last_active_after: Option>, } impl<'a> BrowserSessionFilter<'a> { @@ -67,6 +69,36 @@ impl<'a> BrowserSessionFilter<'a> { self.user } + /// Only return sessions with a last active time before the given time + #[must_use] + pub fn with_last_active_before(mut self, last_active_before: DateTime) -> Self { + self.last_active_before = Some(last_active_before); + self + } + + /// Only return sessions with a last active time after the given time + #[must_use] + pub fn with_last_active_after(mut self, last_active_after: DateTime) -> Self { + self.last_active_after = Some(last_active_after); + self + } + + /// Get the last active before filter + /// + /// Returns [`None`] if no client filter was set + #[must_use] + pub fn last_active_before(&self) -> Option> { + self.last_active_before + } + + /// Get the last active after filter + /// + /// Returns [`None`] if no client filter was set + #[must_use] + pub fn last_active_after(&self) -> Option> { + self.last_active_after + } + /// Only return active browser sessions #[must_use] pub fn active_only(mut self) -> Self {