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,17 +1,16 @@
|
||||
use super::ids::{ProjectId, UserId};
|
||||
use super::CollectionId;
|
||||
use crate::database::models::DatabaseError;
|
||||
use crate::database::redis::RedisPool;
|
||||
use crate::models::ids::base62_impl::{parse_base62, to_base62};
|
||||
use crate::models::users::{Badges, RecipientType, RecipientWallet};
|
||||
use chrono::{DateTime, Utc};
|
||||
use redis::cmd;
|
||||
use rust_decimal::Decimal;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
const USERS_NAMESPACE: &str = "users";
|
||||
const USER_USERNAMES_NAMESPACE: &str = "users_usernames";
|
||||
// const USERS_PROJECTS_NAMESPACE: &str = "users_projects";
|
||||
const DEFAULT_EXPIRY: i64 = 1800; // 30 minutes
|
||||
|
||||
#[derive(Deserialize, Serialize, Clone, Debug)]
|
||||
pub struct User {
|
||||
@@ -87,7 +86,7 @@ impl User {
|
||||
pub async fn get<'a, 'b, E>(
|
||||
string: &str,
|
||||
executor: E,
|
||||
redis: &deadpool_redis::Pool,
|
||||
redis: &RedisPool,
|
||||
) -> Result<Option<User>, DatabaseError>
|
||||
where
|
||||
E: sqlx::Executor<'a, Database = sqlx::Postgres>,
|
||||
@@ -100,7 +99,7 @@ impl User {
|
||||
pub async fn get_id<'a, 'b, E>(
|
||||
id: UserId,
|
||||
executor: E,
|
||||
redis: &deadpool_redis::Pool,
|
||||
redis: &RedisPool,
|
||||
) -> Result<Option<User>, DatabaseError>
|
||||
where
|
||||
E: sqlx::Executor<'a, Database = sqlx::Postgres>,
|
||||
@@ -113,7 +112,7 @@ impl User {
|
||||
pub async fn get_many_ids<'a, E>(
|
||||
user_ids: &[UserId],
|
||||
exec: E,
|
||||
redis: &deadpool_redis::Pool,
|
||||
redis: &RedisPool,
|
||||
) -> Result<Vec<User>, DatabaseError>
|
||||
where
|
||||
E: sqlx::Executor<'a, Database = sqlx::Postgres>,
|
||||
@@ -128,7 +127,7 @@ impl User {
|
||||
pub async fn get_many<'a, E, T: ToString>(
|
||||
users_strings: &[T],
|
||||
exec: E,
|
||||
redis: &deadpool_redis::Pool,
|
||||
redis: &RedisPool,
|
||||
) -> Result<Vec<User>, DatabaseError>
|
||||
where
|
||||
E: sqlx::Executor<'a, Database = sqlx::Postgres>,
|
||||
@@ -139,8 +138,6 @@ impl User {
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
|
||||
let mut redis = redis.get().await?;
|
||||
|
||||
let mut found_users = Vec::new();
|
||||
let mut remaining_strings = users_strings
|
||||
.iter()
|
||||
@@ -153,20 +150,11 @@ impl User {
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
user_ids.append(
|
||||
&mut cmd("MGET")
|
||||
.arg(
|
||||
users_strings
|
||||
.iter()
|
||||
.map(|x| {
|
||||
format!(
|
||||
"{}:{}",
|
||||
USER_USERNAMES_NAMESPACE,
|
||||
x.to_string().to_lowercase()
|
||||
)
|
||||
})
|
||||
.collect::<Vec<_>>(),
|
||||
&mut redis
|
||||
.multi_get::<i64, _>(
|
||||
USER_USERNAMES_NAMESPACE,
|
||||
users_strings.iter().map(|x| x.to_string().to_lowercase()),
|
||||
)
|
||||
.query_async::<_, Vec<Option<i64>>>(&mut redis)
|
||||
.await?
|
||||
.into_iter()
|
||||
.flatten()
|
||||
@@ -174,16 +162,9 @@ impl User {
|
||||
);
|
||||
|
||||
if !user_ids.is_empty() {
|
||||
let users = cmd("MGET")
|
||||
.arg(
|
||||
user_ids
|
||||
.iter()
|
||||
.map(|x| format!("{}:{}", USERS_NAMESPACE, x))
|
||||
.collect::<Vec<_>>(),
|
||||
)
|
||||
.query_async::<_, Vec<Option<String>>>(&mut redis)
|
||||
let users = redis
|
||||
.multi_get::<String, _>(USERS_NAMESPACE, user_ids)
|
||||
.await?;
|
||||
|
||||
for user in users {
|
||||
if let Some(user) = user.and_then(|x| serde_json::from_str::<User>(&x).ok()) {
|
||||
remaining_strings.retain(|x| {
|
||||
@@ -252,24 +233,21 @@ impl User {
|
||||
.await?;
|
||||
|
||||
for user in db_users {
|
||||
cmd("SET")
|
||||
.arg(format!("{}:{}", USERS_NAMESPACE, user.id.0))
|
||||
.arg(serde_json::to_string(&user)?)
|
||||
.arg("EX")
|
||||
.arg(DEFAULT_EXPIRY)
|
||||
.query_async::<_, ()>(&mut redis)
|
||||
redis
|
||||
.set(
|
||||
USERS_NAMESPACE,
|
||||
user.id.0,
|
||||
serde_json::to_string(&user)?,
|
||||
None,
|
||||
)
|
||||
.await?;
|
||||
|
||||
cmd("SET")
|
||||
.arg(format!(
|
||||
"{}:{}",
|
||||
redis
|
||||
.set(
|
||||
USER_USERNAMES_NAMESPACE,
|
||||
user.username.to_lowercase()
|
||||
))
|
||||
.arg(user.id.0)
|
||||
.arg("EX")
|
||||
.arg(DEFAULT_EXPIRY)
|
||||
.query_async::<_, ()>(&mut redis)
|
||||
user.username.to_lowercase(),
|
||||
user.id.0,
|
||||
None,
|
||||
)
|
||||
.await?;
|
||||
found_users.push(user);
|
||||
}
|
||||
@@ -371,24 +349,19 @@ impl User {
|
||||
|
||||
pub async fn clear_caches(
|
||||
user_ids: &[(UserId, Option<String>)],
|
||||
redis: &deadpool_redis::Pool,
|
||||
redis: &RedisPool,
|
||||
) -> Result<(), DatabaseError> {
|
||||
let mut redis = redis.get().await?;
|
||||
let mut cmd = cmd("DEL");
|
||||
|
||||
for (id, username) in user_ids {
|
||||
cmd.arg(format!("{}:{}", USERS_NAMESPACE, id.0));
|
||||
if let Some(username) = username {
|
||||
cmd.arg(format!(
|
||||
"{}:{}",
|
||||
USER_USERNAMES_NAMESPACE,
|
||||
username.to_lowercase()
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
cmd.query_async::<_, ()>(&mut redis).await?;
|
||||
|
||||
redis
|
||||
.delete_many(user_ids.into_iter().flat_map(|(id, username)| {
|
||||
[
|
||||
(USERS_NAMESPACE, Some(id.0.to_string())),
|
||||
(
|
||||
USER_USERNAMES_NAMESPACE,
|
||||
username.clone().map(|i| i.to_lowercase()),
|
||||
),
|
||||
]
|
||||
}))
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -396,7 +369,7 @@ impl User {
|
||||
id: UserId,
|
||||
full: bool,
|
||||
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
|
||||
redis: &deadpool_redis::Pool,
|
||||
redis: &RedisPool,
|
||||
) -> Result<Option<()>, DatabaseError> {
|
||||
let user = Self::get_id(id, &mut *transaction, redis).await?;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user