From aeb94687b5858de635c6d5a918af1af4da5d8823 Mon Sep 17 00:00:00 2001 From: Natty Date: Mon, 29 Apr 2024 18:27:10 +0200 Subject: [PATCH] Local users are not supposed to be fetched by URI --- src/service/local_user_cache.rs | 54 ++++++--------------------------- 1 file changed, 10 insertions(+), 44 deletions(-) diff --git a/src/service/local_user_cache.rs b/src/service/local_user_cache.rs index b435e05..10cf41d 100644 --- a/src/service/local_user_cache.rs +++ b/src/service/local_user_cache.rs @@ -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, id_to_user: HashMap>, token_to_user: HashMap>, - uri_to_user: HashMap>, } 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> { - 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>, 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,