forked from jmug/stoatchat
* feat: create base of push daemon Signed-off-by: IAmTomahawkx <iamtomahawkx@gmail.com> * Add outbound senders * Make web_push send to rabbit instead (temp stuff) * feat: stability and friend requests * make vapid fr stuff not suck * swap naming of queue * move pushd into daemons folder * fix cargo file for move into daemons folder * feat: probably working fcm push notifs * comment out fcm webpush stuff since the config keys dont exist * fix fcm, name queues according to their prod status and configure routing keys * add pushd to docker * mix: Remove old code, add stuff to pushd * fix: lockfile * feat: update rocket to 5.0.1 * fix: fix queues and ack bugs * Move rabbit messsage processing into ack queue * chore: update readme * chore: optimizations for ack database hits * pushd flowchart * misc: update flowchart * exit dependancy hell * add rocket_impl flag to authifier * make the tests file of delta actually compile * fix: don't silence every push message * fix: don't silence all messages * add debug logging for sending data to rabbit from message events * validate mentions at a server membership level * put back that import that was actually important * minor fix to lockfile * update delta authifier * feat: proper permissions for push notifications * add unit test for mention sanitization * remove local file dependancy on authifier * update ports to proper defaults * fixTM the node bindings * Theoretically configure docker releases for pushd Signed-off-by: IAmTomahawkx <iamtomahawkx@gmail.com> * declare exchange in pushd and delta * fix createbuckets script Signed-off-by: IAmTomahawkx <iamtomahawkx@gmail.com> * fix: reference db implementation Signed-off-by: IAmTomahawkx <iamtomahawkx@gmail.com> * fix: remove finally redundant code Signed-off-by: IAmTomahawkx <iamtomahawkx@gmail.com> * fix: changes Signed-off-by: IAmTomahawkx <iamtomahawkx@gmail.com> * fix: other changes Signed-off-by: IAmTomahawkx <iamtomahawkx@gmail.com> * fix: make channel name return result Signed-off-by: IAmTomahawkx <iamtomahawkx@gmail.com> --------- Signed-off-by: IAmTomahawkx <iamtomahawkx@gmail.com>
128 lines
3.6 KiB
Rust
128 lines
3.6 KiB
Rust
use std::num::NonZeroUsize;
|
|
|
|
use revolt_result::{create_error, Result};
|
|
|
|
#[cfg(feature = "rocket-impl")]
|
|
use revolt_result::Error;
|
|
|
|
use async_std::sync::Mutex;
|
|
use once_cell::sync::Lazy;
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
pub struct IdempotencyKey {
|
|
key: String,
|
|
}
|
|
|
|
static TOKEN_CACHE: Lazy<Mutex<lru::LruCache<String, ()>>> =
|
|
Lazy::new(|| Mutex::new(lru::LruCache::new(NonZeroUsize::new(1000).unwrap())));
|
|
|
|
impl IdempotencyKey {
|
|
pub fn unchecked_from_string(key: String) -> Self {
|
|
Self { key }
|
|
}
|
|
|
|
// Backwards compatibility.
|
|
// Issue #109
|
|
pub async fn consume_nonce(&mut self, v: Option<String>) -> Result<()> {
|
|
if let Some(v) = v {
|
|
let mut cache = TOKEN_CACHE.lock().await;
|
|
if cache.get(&v).is_some() {
|
|
return Err(create_error!(DuplicateNonce));
|
|
}
|
|
|
|
cache.put(v.clone(), ());
|
|
self.key = v;
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
pub fn into_key(self) -> String {
|
|
self.key
|
|
}
|
|
}
|
|
|
|
#[cfg(feature = "rocket-impl")]
|
|
use revolt_rocket_okapi::{
|
|
gen::OpenApiGenerator,
|
|
request::{OpenApiFromRequest, RequestHeaderInput},
|
|
revolt_okapi::openapi3::{Parameter, ParameterValue},
|
|
};
|
|
|
|
#[cfg(feature = "rocket-impl")]
|
|
use schemars::schema::{InstanceType, SchemaObject, SingleOrVec};
|
|
|
|
#[cfg(feature = "rocket-impl")]
|
|
impl<'r> OpenApiFromRequest<'r> for IdempotencyKey {
|
|
fn from_request_input(
|
|
_gen: &mut OpenApiGenerator,
|
|
_name: String,
|
|
_required: bool,
|
|
) -> revolt_rocket_okapi::Result<RequestHeaderInput> {
|
|
Ok(RequestHeaderInput::Parameter(Parameter {
|
|
name: "Idempotency-Key".to_string(),
|
|
description: Some("Unique key to prevent duplicate requests".to_string()),
|
|
allow_empty_value: false,
|
|
required: false,
|
|
deprecated: false,
|
|
extensions: schemars::Map::new(),
|
|
location: "header".to_string(),
|
|
value: ParameterValue::Schema {
|
|
allow_reserved: false,
|
|
example: None,
|
|
examples: None,
|
|
explode: None,
|
|
style: None,
|
|
schema: SchemaObject {
|
|
instance_type: Some(SingleOrVec::Single(Box::new(InstanceType::String))),
|
|
..Default::default()
|
|
},
|
|
},
|
|
}))
|
|
}
|
|
}
|
|
|
|
#[cfg(feature = "rocket-impl")]
|
|
use rocket::{
|
|
http::Status,
|
|
request::{FromRequest, Outcome},
|
|
};
|
|
|
|
#[cfg(feature = "rocket-impl")]
|
|
#[async_trait]
|
|
impl<'r> FromRequest<'r> for IdempotencyKey {
|
|
type Error = Error;
|
|
|
|
async fn from_request(request: &'r rocket::Request<'_>) -> Outcome<Self, Self::Error> {
|
|
if let Some(key) = request
|
|
.headers()
|
|
.get("Idempotency-Key")
|
|
.next()
|
|
.map(|k| k.to_string())
|
|
{
|
|
if key.len() > 64 {
|
|
return Outcome::Error((
|
|
Status::BadRequest,
|
|
create_error!(FailedValidation {
|
|
error: "idempotency key too long".to_string(),
|
|
}),
|
|
));
|
|
}
|
|
|
|
let idempotency = IdempotencyKey { key };
|
|
let mut cache = TOKEN_CACHE.lock().await;
|
|
if cache.get(&idempotency.key).is_some() {
|
|
return Outcome::Error((Status::Conflict, create_error!(DuplicateNonce)));
|
|
}
|
|
|
|
cache.put(idempotency.key.clone(), ());
|
|
return Outcome::Success(idempotency);
|
|
}
|
|
|
|
Outcome::Success(IdempotencyKey {
|
|
key: ulid::Ulid::new().to_string(),
|
|
})
|
|
}
|
|
}
|