| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | use crate::model::UserId; |
| 9 | use crate::store::{self, Result, Store, backend}; |
| 10 | |
| 11 | |
| 12 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 13 | pub struct ProfileStats { |
| 14 | |
| 15 | pub sign_ins: u64, |
| 16 | |
| 17 | |
| 18 | pub active_sessions: u64, |
| 19 | |
| 20 | |
| 21 | pub connected_apps: u64, |
| 22 | } |
| 23 | |
| 24 | |
| 25 | |
| 26 | pub async fn profile_stats(store: &Store, user: &UserId) -> Result<ProfileStats> { |
| 27 | let conn = store.conn.lock().await; |
| 28 | let id = user.to_string(); |
| 29 | let now = store::stamp(store::now())?; |
| 30 | let mut rows = conn |
| 31 | .query( |
| 32 | "SELECT \ |
| 33 | (SELECT COUNT(*) FROM sessions WHERE user_id = ?1), \ |
| 34 | (SELECT COUNT(*) FROM sessions \ |
| 35 | WHERE user_id = ?1 AND revoked_at IS NULL AND expires_at > ?2), \ |
| 36 | (SELECT COUNT(DISTINCT client_id) FROM app_sessions \ |
| 37 | WHERE user_id = ?1 AND revoked_at IS NULL AND expires_at > ?2)", |
| 38 | turso::params![id, now], |
| 39 | ) |
| 40 | .await |
| 41 | .map_err(backend)?; |
| 42 | let row = rows |
| 43 | .next() |
| 44 | .await |
| 45 | .map_err(backend)? |
| 46 | .ok_or_else(|| backend("counts query returned no row"))?; |
| 47 | Ok(ProfileStats { |
| 48 | sign_ins: store::int(&row, 0)? as u64, |
| 49 | active_sessions: store::int(&row, 1)? as u64, |
| 50 | connected_apps: store::int(&row, 2)? as u64, |
| 51 | }) |
| 52 | } |
| 53 | |
| 54 | #[cfg(test)] |
| 55 | mod tests { |
| 56 | use super::*; |
| 57 | use crate::accounts::{create_invite, create_user_from_invite}; |
| 58 | use std::path::Path; |
| 59 | |
| 60 | async fn fixture() -> (Store, UserId) { |
| 61 | let store = Store::open(Path::new(":memory:")).await.unwrap(); |
| 62 | let invite = create_invite(&store, "ann@example.com", None, false) |
| 63 | .await |
| 64 | .unwrap(); |
| 65 | let user = create_user_from_invite(&store, invite.expose(), "Ann", "tDLr9!mZQ2xv") |
| 66 | .await |
| 67 | .unwrap(); |
| 68 | (store, user.id) |
| 69 | } |
| 70 | |
| 71 | |
| 72 | |
| 73 | fn stamp_in(hours: i64) -> String { |
| 74 | store::stamp(store::now() + time::Duration::hours(hours)).unwrap() |
| 75 | } |
| 76 | |
| 77 | #[tokio::test] |
| 78 | async fn counts_reflect_the_tables() { |
| 79 | let (store, user_id) = fixture().await; |
| 80 | let id = user_id.to_string(); |
| 81 | { |
| 82 | let conn = store.conn.lock().await; |
| 83 | |
| 84 | for (hash, expires, revoked) in [ |
| 85 | ("h-expired", stamp_in(-1), None), |
| 86 | ("h-revoked", stamp_in(24), Some(stamp_in(-1))), |
| 87 | ("h-live", stamp_in(24), None), |
| 88 | ] { |
| 89 | conn.execute( |
| 90 | "INSERT INTO sessions (token_hash, user_id, created_at, expires_at, revoked_at) \ |
| 91 | VALUES (?1, ?2, ?3, ?4, ?5)", |
| 92 | turso::params![hash, id.clone(), stamp_in(-24), expires, revoked], |
| 93 | ) |
| 94 | .await |
| 95 | .unwrap(); |
| 96 | } |
| 97 | |
| 98 | |
| 99 | conn.execute( |
| 100 | "INSERT INTO oidc_clients (client_id, name, secret_hash, redirect_uris, created_at) \ |
| 101 | VALUES ('client-a', 'A', 'x', '[]', ?1)", |
| 102 | turso::params![stamp_in(-24)], |
| 103 | ) |
| 104 | .await |
| 105 | .unwrap(); |
| 106 | conn.execute( |
| 107 | "INSERT INTO oidc_clients (client_id, name, secret_hash, redirect_uris, created_at) \ |
| 108 | VALUES ('client-b', 'B', 'x', '[]', ?1)", |
| 109 | turso::params![stamp_in(-24)], |
| 110 | ) |
| 111 | .await |
| 112 | .unwrap(); |
| 113 | for (hash, client, expires, revoked) in [ |
| 114 | ("a1", "client-a", stamp_in(24), None), |
| 115 | ("a2", "client-a", stamp_in(24), None), |
| 116 | ("b1", "client-b", stamp_in(24), Some(stamp_in(-1))), |
| 117 | ] { |
| 118 | conn.execute( |
| 119 | "INSERT INTO app_sessions \ |
| 120 | (token_hash, user_id, client_id, session_hash, created_at, expires_at, revoked_at) \ |
| 121 | VALUES (?1, ?2, ?3, 'h-live', ?4, ?5, ?6)", |
| 122 | turso::params![hash, id.clone(), client, stamp_in(-24), expires, revoked], |
| 123 | ) |
| 124 | .await |
| 125 | .unwrap(); |
| 126 | } |
| 127 | } |
| 128 | let stats = profile_stats(&store, &user_id).await.unwrap(); |
| 129 | assert_eq!( |
| 130 | stats, |
| 131 | ProfileStats { |
| 132 | sign_ins: 3, |
| 133 | active_sessions: 1, |
| 134 | connected_apps: 1, |
| 135 | } |
| 136 | ); |
| 137 | } |
| 138 | |
| 139 | #[tokio::test] |
| 140 | async fn a_fresh_account_reads_zero() { |
| 141 | let (store, user_id) = fixture().await; |
| 142 | assert_eq!( |
| 143 | profile_stats(&store, &user_id).await.unwrap(), |
| 144 | ProfileStats { |
| 145 | sign_ins: 0, |
| 146 | active_sessions: 0, |
| 147 | connected_apps: 0, |
| 148 | } |
| 149 | ); |
| 150 | } |
| 151 | } |