You've already forked AstralRinth
forked from didirus/AstralRinth
Tests (#719)
* computer switch * some fixes; github action * added pr to master * sqlx database setup * switched intial GHA test db * removed sqlx database setup * unfinished patch route * bug fixes + tests * more tests, more fixes, cargo fmt * merge fixes * more tests, full reorganization * fmt, clippy * sqlx-data * revs * removed comments * delete revs
This commit is contained in:
@@ -1,14 +1,13 @@
|
||||
use super::ids::*;
|
||||
use crate::database::models::DatabaseError;
|
||||
use crate::database::redis::RedisPool;
|
||||
use crate::models::ids::base62_impl::{parse_base62, to_base62};
|
||||
use chrono::{DateTime, Utc};
|
||||
use redis::cmd;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
const SESSIONS_NAMESPACE: &str = "sessions";
|
||||
const SESSIONS_IDS_NAMESPACE: &str = "sessions_ids";
|
||||
const SESSIONS_USERS_NAMESPACE: &str = "sessions_users";
|
||||
const DEFAULT_EXPIRY: i64 = 1800; // 30 minutes
|
||||
|
||||
pub struct SessionBuilder {
|
||||
pub session: String,
|
||||
@@ -83,7 +82,7 @@ impl Session {
|
||||
pub async fn get<'a, E, T: ToString>(
|
||||
id: T,
|
||||
exec: E,
|
||||
redis: &deadpool_redis::Pool,
|
||||
redis: &RedisPool,
|
||||
) -> Result<Option<Session>, DatabaseError>
|
||||
where
|
||||
E: sqlx::Executor<'a, Database = sqlx::Postgres>,
|
||||
@@ -96,7 +95,7 @@ impl Session {
|
||||
pub async fn get_id<'a, 'b, E>(
|
||||
id: SessionId,
|
||||
executor: E,
|
||||
redis: &deadpool_redis::Pool,
|
||||
redis: &RedisPool,
|
||||
) -> Result<Option<Session>, DatabaseError>
|
||||
where
|
||||
E: sqlx::Executor<'a, Database = sqlx::Postgres>,
|
||||
@@ -109,7 +108,7 @@ impl Session {
|
||||
pub async fn get_many_ids<'a, E>(
|
||||
session_ids: &[SessionId],
|
||||
exec: E,
|
||||
redis: &deadpool_redis::Pool,
|
||||
redis: &RedisPool,
|
||||
) -> Result<Vec<Session>, DatabaseError>
|
||||
where
|
||||
E: sqlx::Executor<'a, Database = sqlx::Postgres>,
|
||||
@@ -124,7 +123,7 @@ impl Session {
|
||||
pub async fn get_many<'a, E, T: ToString>(
|
||||
session_strings: &[T],
|
||||
exec: E,
|
||||
redis: &deadpool_redis::Pool,
|
||||
redis: &RedisPool,
|
||||
) -> Result<Vec<Session>, DatabaseError>
|
||||
where
|
||||
E: sqlx::Executor<'a, Database = sqlx::Postgres>,
|
||||
@@ -135,8 +134,6 @@ impl Session {
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
|
||||
let mut redis = redis.get().await?;
|
||||
|
||||
let mut found_sessions = Vec::new();
|
||||
let mut remaining_strings = session_strings
|
||||
.iter()
|
||||
@@ -149,14 +146,11 @@ impl Session {
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
session_ids.append(
|
||||
&mut cmd("MGET")
|
||||
.arg(
|
||||
session_strings
|
||||
.iter()
|
||||
.map(|x| format!("{}:{}", SESSIONS_IDS_NAMESPACE, x.to_string()))
|
||||
.collect::<Vec<_>>(),
|
||||
&mut redis
|
||||
.multi_get::<i64, _>(
|
||||
SESSIONS_IDS_NAMESPACE,
|
||||
session_strings.iter().map(|x| x.to_string()),
|
||||
)
|
||||
.query_async::<_, Vec<Option<i64>>>(&mut redis)
|
||||
.await?
|
||||
.into_iter()
|
||||
.flatten()
|
||||
@@ -164,16 +158,9 @@ impl Session {
|
||||
);
|
||||
|
||||
if !session_ids.is_empty() {
|
||||
let sessions = cmd("MGET")
|
||||
.arg(
|
||||
session_ids
|
||||
.iter()
|
||||
.map(|x| format!("{}:{}", SESSIONS_NAMESPACE, x))
|
||||
.collect::<Vec<_>>(),
|
||||
)
|
||||
.query_async::<_, Vec<Option<String>>>(&mut redis)
|
||||
let sessions = redis
|
||||
.multi_get::<String, _>(SESSIONS_NAMESPACE, session_ids)
|
||||
.await?;
|
||||
|
||||
for session in sessions {
|
||||
if let Some(session) =
|
||||
session.and_then(|x| serde_json::from_str::<Session>(&x).ok())
|
||||
@@ -225,20 +212,21 @@ impl Session {
|
||||
.await?;
|
||||
|
||||
for session in db_sessions {
|
||||
cmd("SET")
|
||||
.arg(format!("{}:{}", SESSIONS_NAMESPACE, session.id.0))
|
||||
.arg(serde_json::to_string(&session)?)
|
||||
.arg("EX")
|
||||
.arg(DEFAULT_EXPIRY)
|
||||
.query_async::<_, ()>(&mut redis)
|
||||
redis
|
||||
.set(
|
||||
SESSIONS_NAMESPACE,
|
||||
session.id.0,
|
||||
serde_json::to_string(&session)?,
|
||||
None,
|
||||
)
|
||||
.await?;
|
||||
|
||||
cmd("SET")
|
||||
.arg(format!("{}:{}", SESSIONS_IDS_NAMESPACE, session.session))
|
||||
.arg(session.id.0)
|
||||
.arg("EX")
|
||||
.arg(DEFAULT_EXPIRY)
|
||||
.query_async::<_, ()>(&mut redis)
|
||||
redis
|
||||
.set(
|
||||
SESSIONS_IDS_NAMESPACE,
|
||||
session.session.clone(),
|
||||
session.id.0,
|
||||
None,
|
||||
)
|
||||
.await?;
|
||||
found_sessions.push(session);
|
||||
}
|
||||
@@ -250,15 +238,13 @@ impl Session {
|
||||
pub async fn get_user_sessions<'a, E>(
|
||||
user_id: UserId,
|
||||
exec: E,
|
||||
redis: &deadpool_redis::Pool,
|
||||
redis: &RedisPool,
|
||||
) -> Result<Vec<SessionId>, DatabaseError>
|
||||
where
|
||||
E: sqlx::Executor<'a, Database = sqlx::Postgres>,
|
||||
{
|
||||
let mut redis = redis.get().await?;
|
||||
let res = cmd("GET")
|
||||
.arg(format!("{}:{}", SESSIONS_USERS_NAMESPACE, user_id.0))
|
||||
.query_async::<_, Option<String>>(&mut redis)
|
||||
let res = redis
|
||||
.get::<String, _>(SESSIONS_USERS_NAMESPACE, user_id.0)
|
||||
.await?
|
||||
.and_then(|x| serde_json::from_str::<Vec<i64>>(&x).ok());
|
||||
|
||||
@@ -281,12 +267,13 @@ impl Session {
|
||||
.try_collect::<Vec<SessionId>>()
|
||||
.await?;
|
||||
|
||||
cmd("SET")
|
||||
.arg(format!("{}:{}", SESSIONS_USERS_NAMESPACE, user_id.0))
|
||||
.arg(serde_json::to_string(&db_sessions)?)
|
||||
.arg("EX")
|
||||
.arg(DEFAULT_EXPIRY)
|
||||
.query_async::<_, ()>(&mut redis)
|
||||
redis
|
||||
.set(
|
||||
SESSIONS_USERS_NAMESPACE,
|
||||
user_id.0,
|
||||
serde_json::to_string(&db_sessions)?,
|
||||
None,
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(db_sessions)
|
||||
@@ -294,29 +281,25 @@ impl Session {
|
||||
|
||||
pub async fn clear_cache(
|
||||
clear_sessions: Vec<(Option<SessionId>, Option<String>, Option<UserId>)>,
|
||||
redis: &deadpool_redis::Pool,
|
||||
redis: &RedisPool,
|
||||
) -> Result<(), DatabaseError> {
|
||||
if clear_sessions.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let mut redis = redis.get().await?;
|
||||
let mut cmd = cmd("DEL");
|
||||
|
||||
for (id, session, user_id) in clear_sessions {
|
||||
if let Some(id) = id {
|
||||
cmd.arg(format!("{}:{}", SESSIONS_NAMESPACE, id.0));
|
||||
}
|
||||
if let Some(session) = session {
|
||||
cmd.arg(format!("{}:{}", SESSIONS_IDS_NAMESPACE, session));
|
||||
}
|
||||
if let Some(user_id) = user_id {
|
||||
cmd.arg(format!("{}:{}", SESSIONS_USERS_NAMESPACE, user_id.0));
|
||||
}
|
||||
}
|
||||
|
||||
cmd.query_async::<_, ()>(&mut redis).await?;
|
||||
|
||||
redis
|
||||
.delete_many(
|
||||
clear_sessions
|
||||
.into_iter()
|
||||
.flat_map(|(id, session, user_id)| {
|
||||
[
|
||||
(SESSIONS_NAMESPACE, id.map(|i| i.0.to_string())),
|
||||
(SESSIONS_IDS_NAMESPACE, session),
|
||||
(SESSIONS_USERS_NAMESPACE, user_id.map(|i| i.0.to_string())),
|
||||
]
|
||||
}),
|
||||
)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user