mirror of
https://github.com/stoatchat/stoatchat.git
synced 2026-07-13 21:17:05 +00:00
feat(core/database): axum implementation for User
This commit is contained in:
@@ -16,6 +16,7 @@ mongodb = ["dep:mongodb", "bson"]
|
||||
tasks = ["isahc", "linkify", "url-escape"]
|
||||
async-std-runtime = ["async-std"]
|
||||
rocket-impl = ["rocket", "schemars", "revolt_okapi", "revolt_rocket_okapi"]
|
||||
axum-impl = ["axum"]
|
||||
redis-is-patched = ["revolt-presence/redis-is-patched"]
|
||||
|
||||
# Default Features
|
||||
@@ -76,6 +77,9 @@ async-recursion = "1.0.4"
|
||||
# Async
|
||||
async-std = { version = "1.8.0", features = ["attributes"], optional = true }
|
||||
|
||||
# Axum Impl
|
||||
axum = { version = "0.7.5", optional = true }
|
||||
|
||||
# Rocket Impl
|
||||
schemars = { version = "0.8.8", optional = true }
|
||||
rocket = { version = "0.5.0-rc.2", default-features = false, features = [
|
||||
|
||||
24
crates/core/database/src/models/users/axum.rs
Normal file
24
crates/core/database/src/models/users/axum.rs
Normal file
@@ -0,0 +1,24 @@
|
||||
use axum::{extract::FromRequestParts, http::request::Parts};
|
||||
|
||||
use revolt_result::{create_error, Error, Result};
|
||||
|
||||
use crate::{Database, User};
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl FromRequestParts<Database> for User {
|
||||
type Rejection = Error;
|
||||
|
||||
async fn from_request_parts(parts: &mut Parts, db: &Database) -> Result<User> {
|
||||
if let Some(Ok(bot_token)) = parts.headers.get("x-bot-token").map(|v| v.to_str()) {
|
||||
let bot = db.fetch_bot_by_token(bot_token).await?;
|
||||
db.fetch_user(&bot.id).await
|
||||
} else if let Some(Ok(session_token)) =
|
||||
parts.headers.get("x-session-token").map(|v| v.to_str())
|
||||
{
|
||||
let session = db.fetch_session_by_token(session_token).await?;
|
||||
db.fetch_user(&session.user_id).await
|
||||
} else {
|
||||
Err(create_error!(InvalidCredentials))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
#[cfg(feature = "axum-impl")]
|
||||
mod axum;
|
||||
mod model;
|
||||
mod ops;
|
||||
#[cfg(feature = "rocket-impl")]
|
||||
@@ -5,6 +7,8 @@ mod rocket;
|
||||
#[cfg(feature = "rocket-impl")]
|
||||
mod schema;
|
||||
|
||||
#[cfg(feature = "axum-impl")]
|
||||
pub use self::axum::*;
|
||||
#[cfg(feature = "rocket-impl")]
|
||||
pub use self::rocket::*;
|
||||
#[cfg(feature = "rocket-impl")]
|
||||
|
||||
@@ -218,7 +218,7 @@ impl User {
|
||||
.datetime()
|
||||
.elapsed()
|
||||
.expect("time went backwards")
|
||||
<= Duration::from_secs(86400u64 * config.features.limits.global.new_user_days as u64)
|
||||
<= Duration::from_secs(3600u64 * config.features.limits.global.new_user_hours as u64)
|
||||
{
|
||||
config.features.limits.new_user
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user