[Fix] User deletion & migration
This commit is contained in:
parent
b65b22dfbc
commit
d3bcf56b59
14 changed files with 456 additions and 148 deletions
29
src/state.rs
29
src/state.rs
|
|
@ -1,18 +1,47 @@
|
|||
use crate::sql::user_online_tracker::PresenceTracker;
|
||||
use std::sync::Arc;
|
||||
use dashmap::DashMap;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
pub enum AccountChallengeOperation { Attach, Delete }
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct AccountChallenge {
|
||||
pub operation: AccountChallengeOperation,
|
||||
pub user_id: i64,
|
||||
pub requester_iota_id: i64,
|
||||
pub nonce: u64,
|
||||
pub created_at: Instant,
|
||||
}
|
||||
|
||||
pub struct OmegaState {
|
||||
pub presence: Arc<PresenceTracker>,
|
||||
challenges: DashMap<(AccountChallengeOperation, i64, i64), AccountChallenge>,
|
||||
}
|
||||
|
||||
impl Default for OmegaState {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
presence: Arc::new(PresenceTracker::default()),
|
||||
challenges: DashMap::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl OmegaState {
|
||||
pub fn issue_challenge(&self, operation: AccountChallengeOperation, user_id: i64, requester_iota_id: i64) -> u64 {
|
||||
let nonce = rand::random::<u64>();
|
||||
self.challenges.insert((operation, user_id, requester_iota_id), AccountChallenge { operation, user_id, requester_iota_id, nonce, created_at: Instant::now() });
|
||||
nonce
|
||||
}
|
||||
|
||||
pub fn consume_challenge(&self, operation: AccountChallengeOperation, user_id: i64, requester_iota_id: i64, nonce: u64) -> bool {
|
||||
self.challenges.remove(&(operation, user_id, requester_iota_id)).is_some_and(|(_, value)|
|
||||
value.nonce == nonce && value.created_at.elapsed() <= Duration::from_secs(120))
|
||||
}
|
||||
}
|
||||
|
||||
impl OmegaState {
|
||||
pub fn new() -> Arc<Self> {
|
||||
Arc::new(Self::default())
|
||||
|
|
|
|||
Loading…
Reference in a new issue