refactor: migrate devices to store

Jef Roosens 2025-03-15 19:01:38 +01:00
parent 54a723f803
commit 3a5a6759ac
No known key found for this signature in database
GPG Key ID: 21FD3D77D56BAF49
4 changed files with 25 additions and 10 deletions

View File

@ -10,7 +10,7 @@ pub fn serve(config: &crate::config::Config) -> u8 {
let ctx = server::Context { let ctx = server::Context {
repo: repo.clone(), repo: repo.clone(),
store: crate::gpodder::GpodderRepository::new(Box::new(repo)), store: crate::gpodder::GpodderRepository::new(repo),
}; };
let app = server::app(ctx); let app = server::app(ctx);

View File

@ -11,6 +11,10 @@ pub enum AuthErr {
Other(Box<dyn std::error::Error + Sync + Send>), Other(Box<dyn std::error::Error + Sync + Send>),
} }
pub trait Store: AuthStore + DeviceRepository {}
impl<T> Store for T where T: AuthStore + DeviceRepository {}
pub trait AuthRepository { pub trait AuthRepository {
/// Validate the given session ID and return its user. /// Validate the given session ID and return its user.
fn validate_session(&self, session_id: i64) -> Result<models::User, AuthErr>; fn validate_session(&self, session_id: i64) -> Result<models::User, AuthErr>;

View File

@ -3,21 +3,19 @@ use std::sync::Arc;
use argon2::{Argon2, PasswordHash, PasswordVerifier}; use argon2::{Argon2, PasswordHash, PasswordVerifier};
use rand::Rng; use rand::Rng;
use super::{models, AuthErr, AuthStore}; use super::{models, AuthErr, Store};
const MAX_SESSION_AGE: i64 = 60 * 60 * 24 * 7; const MAX_SESSION_AGE: i64 = 60 * 60 * 24 * 7;
type Store = dyn AuthStore + Send + Sync;
#[derive(Clone)] #[derive(Clone)]
pub struct GpodderRepository { pub struct GpodderRepository {
store: Arc<Store>, store: Arc<dyn Store + Send + Sync>,
} }
impl GpodderRepository { impl GpodderRepository {
pub fn new(store: Box<Store>) -> Self { pub fn new(store: impl Store + Send + Sync + 'static) -> Self {
Self { Self {
store: Arc::from(store), store: Arc::new(store),
} }
} }
@ -71,4 +69,17 @@ impl GpodderRepository {
pub fn remove_session(&self, session_id: i64) -> Result<(), AuthErr> { pub fn remove_session(&self, session_id: i64) -> Result<(), AuthErr> {
self.store.remove_session(session_id) self.store.remove_session(session_id)
} }
pub fn devices_for_user(&self, user: &models::User) -> Result<Vec<models::Device>, AuthErr> {
self.store.devices_for_user(user)
}
pub fn update_device_info(
&self,
user: &models::User,
device_id: &str,
patch: models::DevicePatch,
) -> Result<(), AuthErr> {
self.store.update_device_info(user, device_id, patch)
}
} }

View File

@ -6,7 +6,7 @@ use axum::{
}; };
use crate::{ use crate::{
gpodder::{self, DeviceRepository}, gpodder,
server::{ server::{
error::{AppError, AppResult}, error::{AppError, AppResult},
gpodder::{ gpodder::{
@ -38,7 +38,7 @@ async fn get_devices(
} }
Ok( Ok(
tokio::task::spawn_blocking(move || ctx.repo.devices_for_user(&user)) tokio::task::spawn_blocking(move || ctx.store.devices_for_user(&user))
.await .await
.unwrap() .unwrap()
.map(Json)?, .map(Json)?,
@ -55,7 +55,7 @@ async fn post_device(
return Err(AppError::NotFound); return Err(AppError::NotFound);
} }
tokio::task::spawn_blocking(move || ctx.repo.update_device_info(&user, &id, patch)) tokio::task::spawn_blocking(move || ctx.store.update_device_info(&user, &id, patch))
.await .await
.unwrap()?; .unwrap()?;