You've already forked authentication-service
mirror of
https://github.com/matrix-org/matrix-authentication-service.git
synced 2025-11-20 12:02:22 +03:00
Save the session activity in the database
This commit is contained in:
@@ -12,6 +12,8 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use std::net::IpAddr;
|
||||
|
||||
use async_trait::async_trait;
|
||||
use chrono::{DateTime, Utc};
|
||||
use mas_data_model::{
|
||||
@@ -504,4 +506,51 @@ impl<'c> BrowserSessionRepository for PgBrowserSessionRepository<'c> {
|
||||
let authentication = Authentication::try_from(authentication)?;
|
||||
Ok(Some(authentication))
|
||||
}
|
||||
|
||||
#[tracing::instrument(
|
||||
name = "db.browser_session.record_batch_activity",
|
||||
skip_all,
|
||||
fields(
|
||||
db.statement,
|
||||
),
|
||||
err,
|
||||
)]
|
||||
async fn record_batch_activity(
|
||||
&mut self,
|
||||
activity: Vec<(Ulid, DateTime<Utc>, Option<IpAddr>)>,
|
||||
) -> Result<(), Self::Error> {
|
||||
let mut ids = Vec::with_capacity(activity.len());
|
||||
let mut last_activities = Vec::with_capacity(activity.len());
|
||||
let mut ips = Vec::with_capacity(activity.len());
|
||||
|
||||
for (id, last_activity, ip) in activity {
|
||||
ids.push(Uuid::from(id));
|
||||
last_activities.push(last_activity);
|
||||
ips.push(ip);
|
||||
}
|
||||
|
||||
let res = sqlx::query!(
|
||||
r#"
|
||||
UPDATE user_sessions
|
||||
SET last_active_at = GREATEST(t.last_active_at, user_sessions.last_active_at)
|
||||
, last_active_ip = COALESCE(t.last_active_ip, user_sessions.last_active_ip)
|
||||
FROM (
|
||||
SELECT *
|
||||
FROM UNNEST($1::uuid[], $2::timestamptz[], $3::inet[])
|
||||
AS t(user_session_id, last_active_at, last_active_ip)
|
||||
) AS t
|
||||
WHERE user_sessions.user_session_id = t.user_session_id
|
||||
"#,
|
||||
&ids,
|
||||
&last_activities,
|
||||
&ips as &[Option<IpAddr>],
|
||||
)
|
||||
.traced()
|
||||
.execute(&mut *self.conn)
|
||||
.await?;
|
||||
|
||||
DatabaseError::ensure_affected_rows(&res, ids.len().try_into().unwrap_or(u64::MAX))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user