From 1eb5495802e8447649c054b59843762b15b82b56 Mon Sep 17 00:00:00 2001 From: janost Date: Mon, 30 Nov 2020 22:00:51 +0100 Subject: [PATCH] Show latest active device as last active on admin page --- src/api/admin.rs | 5 +++++ src/db/models/device.rs | 11 +++++++++++ src/db/models/user.rs | 7 +++++++ src/static/templates/admin/users.hbs | 2 ++ 4 files changed, 25 insertions(+) diff --git a/src/api/admin.rs b/src/api/admin.rs index 415311e7..f30855e4 100644 --- a/src/api/admin.rs +++ b/src/api/admin.rs @@ -297,6 +297,11 @@ fn users_overview(_token: AdminToken, conn: DbConn) -> ApiResult> { usr["cipher_count"] = json!(Cipher::count_owned_by_user(&u.uuid, &conn)); usr["attachment_count"] = json!(Attachment::count_by_user(&u.uuid, &conn)); usr["attachment_size"] = json!(get_display_size(Attachment::size_by_user(&u.uuid, &conn) as i32)); + usr["created_at"] = json!(&u.created_at.format("%Y-%m-%d %H:%M:%S").to_string()); + usr["last_active"] = match u.last_active(&conn) { + Some(timestamp) => json!(timestamp.format("%Y-%m-%d %H:%M:%S").to_string()), + None => json!("Never") + }; usr }).collect(); diff --git a/src/db/models/device.rs b/src/db/models/device.rs index 6d6743ff..0172ffb5 100644 --- a/src/db/models/device.rs +++ b/src/db/models/device.rs @@ -178,4 +178,15 @@ impl Device { .from_db() }} } + + pub fn find_latest_active_by_user(user_uuid: &str, conn: &DbConn) -> Option { + db_run! { conn: { + devices::table + .filter(devices::user_uuid.eq(user_uuid)) + .order(devices::updated_at.desc()) + .first::(conn) + .ok() + .from_db() + }} + } } diff --git a/src/db/models/user.rs b/src/db/models/user.rs index 32c3cecc..a9c6c3d2 100644 --- a/src/db/models/user.rs +++ b/src/db/models/user.rs @@ -288,6 +288,13 @@ impl User { users::table.load::(conn).expect("Error loading users").from_db() }} } + + pub fn last_active(&self, conn: &DbConn) -> Option { + match Device::find_latest_active_by_user(&self.uuid, conn) { + Some(device) => Some(device.updated_at), + None => None + } + } } impl Invitation { diff --git a/src/static/templates/admin/users.hbs b/src/static/templates/admin/users.hbs index c99d9283..9f55b1e4 100644 --- a/src/static/templates/admin/users.hbs +++ b/src/static/templates/admin/users.hbs @@ -21,6 +21,8 @@
{{Name}} {{Email}} + Created at: {{created_at}} + Last active: {{last_active}} {{#if TwoFactorEnabled}} 2FA