Local users are not supposed to be fetched by URI

This commit is contained in:
Natty 2024-04-29 18:27:10 +02:00
parent 553dbb9b7b
commit aeb94687b5
Signed by: natty
GPG Key ID: BF6CB659ADEE60EC
1 changed files with 10 additions and 44 deletions

View File

@ -1,17 +1,20 @@
use crate::web::ApiError;
use cached::{Cached, TimedCache};
use magnetar_common::config::MagnetarConfig;
use magnetar_model::{
ck, CalckeyCache, CalckeyCacheError, CalckeyDbError, CalckeyModel, CalckeySub,
InternalStreamMessage, SubMessage,
};
use std::collections::HashMap;
use std::sync::Arc;
use cached::{Cached, TimedCache};
use strum::EnumVariantNames;
use thiserror::Error;
use tokio::sync::Mutex;
use tracing::error;
use magnetar_common::config::MagnetarConfig;
use magnetar_model::{
CalckeyCache, CalckeyCacheError, CalckeyDbError, CalckeyModel, CalckeySub, ck,
InternalStreamMessage, SubMessage,
};
use crate::web::ApiError;
#[derive(Debug, Error, EnumVariantNames)]
pub enum UserCacheError {
#[error("Database error: {0}")]
@ -37,7 +40,6 @@ struct LocalUserCache {
lifetime: TimedCache<String, ()>,
id_to_user: HashMap<String, Arc<ck::user::Model>>,
token_to_user: HashMap<String, Arc<ck::user::Model>>,
uri_to_user: HashMap<String, Arc<ck::user::Model>>,
}
impl LocalUserCache {
@ -50,9 +52,6 @@ impl LocalUserCache {
if let Some(token) = user.token.clone() {
self.token_to_user.remove(&token);
}
if let Some(uri) = user.uri.clone() {
self.uri_to_user.remove(&uri);
}
}
}
@ -66,10 +65,6 @@ impl LocalUserCache {
if let Some(token) = user.token.clone() {
self.token_to_user.insert(token, user.clone());
}
if let Some(uri) = user.uri.clone() {
self.uri_to_user.insert(uri, user);
}
}
/// Low-priority refresh. Only refreshes the cache if the user is not there.
@ -105,19 +100,6 @@ impl LocalUserCache {
None
}
fn get_by_uri(&mut self, uri: &str) -> Option<Arc<ck::user::Model>> {
if let Some(user) = self.uri_to_user.get(uri).cloned() {
if self.lifetime.cache_get(&user.id).is_none() {
self.purge(&user);
return None;
}
return Some(user);
}
None
}
}
pub struct LocalUserCacheService {
@ -137,7 +119,6 @@ impl LocalUserCacheService {
lifetime: TimedCache::with_lifespan(60 * 5),
id_to_user: HashMap::new(),
token_to_user: HashMap::new(),
uri_to_user: HashMap::new(),
}));
let cache_clone = cache.clone();
@ -213,21 +194,6 @@ impl LocalUserCacheService {
.await
}
pub async fn get_by_uri(
&self,
uri: &str,
) -> Result<Option<Arc<ck::user::Model>>, UserCacheError> {
let result = self.cache.lock().await.get_by_uri(uri);
if let Some(user) = result {
return Ok(Some(user));
}
let user = self.db.get_user_by_uri(uri).await?;
self.map_cache_user(user).await
}
pub async fn get_by_id(
&self,
id: &str,