118 lines
3.7 KiB
Rust
118 lines
3.7 KiB
Rust
pub mod models;
|
|
mod repository;
|
|
|
|
pub use models::*;
|
|
pub use repository::GpodderRepository;
|
|
|
|
pub enum AuthErr {
|
|
UnknownSession,
|
|
UnknownUser,
|
|
InvalidPassword,
|
|
Other(Box<dyn std::error::Error + Sync + Send>),
|
|
}
|
|
|
|
pub trait Store: AuthStore + DeviceRepository + SubscriptionRepository {}
|
|
|
|
impl<T> Store for T where T: AuthStore + DeviceRepository + SubscriptionRepository {}
|
|
|
|
pub trait AuthRepository {
|
|
/// Validate the given session ID and return its user.
|
|
fn validate_session(&self, session_id: i64) -> Result<models::User, AuthErr>;
|
|
|
|
/// Validate the credentials, returning the user if the credentials are correct.
|
|
fn validate_credentials(&self, username: &str, password: &str)
|
|
-> Result<models::User, AuthErr>;
|
|
|
|
/// Create a new session for the given user.
|
|
fn create_session(
|
|
&self,
|
|
username: &str,
|
|
password: &str,
|
|
) -> Result<(i64, models::User), AuthErr>;
|
|
|
|
fn remove_session(&self, username: &str, session_id: i64) -> Result<(), AuthErr>;
|
|
}
|
|
|
|
pub trait AuthStore {
|
|
/// Retrieve the session with the given session ID
|
|
fn get_session(&self, session_id: i64) -> Result<Option<models::Session>, AuthErr>;
|
|
|
|
/// Retrieve the user with the given username
|
|
fn get_user(&self, username: &str) -> Result<Option<models::User>, AuthErr>;
|
|
|
|
/// Create a new session for a user with the given session ID
|
|
fn insert_session(&self, session: &Session) -> Result<(), AuthErr>;
|
|
|
|
/// Remove the session with the given session ID
|
|
fn remove_session(&self, session_id: i64) -> Result<(), AuthErr>;
|
|
}
|
|
|
|
pub trait DeviceRepository {
|
|
/// Return all devices associated with the user
|
|
fn devices_for_user(&self, user: &User) -> Result<Vec<Device>, AuthErr>;
|
|
|
|
/// Update the information for the given device. If the device doesn't exist yet, it should be
|
|
/// created.
|
|
fn update_device_info(
|
|
&self,
|
|
user: &User,
|
|
device_id: &str,
|
|
patch: DevicePatch,
|
|
) -> Result<(), AuthErr>;
|
|
}
|
|
|
|
pub trait SubscriptionRepository {
|
|
/// Return the subscriptions for the given device
|
|
fn subscriptions_for_device(
|
|
&self,
|
|
user: &User,
|
|
device_id: &str,
|
|
) -> Result<Vec<String>, AuthErr>;
|
|
|
|
/// Return all subscriptions for a given user
|
|
fn subscriptions_for_user(&self, user: &User) -> Result<Vec<String>, AuthErr>;
|
|
|
|
/// Replace the list of subscriptions for a device with the given list
|
|
fn set_subscriptions_for_device(
|
|
&self,
|
|
user: &User,
|
|
device_id: &str,
|
|
urls: Vec<String>,
|
|
time_changed: chrono::DateTime<chrono::Utc>,
|
|
) -> Result<(), AuthErr>;
|
|
|
|
/// Update the list of subscriptions for a device by adding and removing the given URLs
|
|
fn update_subscriptions_for_device(
|
|
&self,
|
|
user: &User,
|
|
device_id: &str,
|
|
add: Vec<String>,
|
|
remove: Vec<String>,
|
|
time_changed: chrono::DateTime<chrono::Utc>,
|
|
) -> Result<(), AuthErr>;
|
|
|
|
/// Returns the changes in subscriptions since the given timestamp.
|
|
fn subscription_updates_for_device(
|
|
&self,
|
|
user: &User,
|
|
device_id: &str,
|
|
since: chrono::DateTime<chrono::Utc>,
|
|
) -> Result<(chrono::DateTime<chrono::Utc>, Vec<String>, Vec<String>), AuthErr>;
|
|
}
|
|
|
|
pub trait EpisodeActionRepository {
|
|
/// Insert the given episode actions into the datastore.
|
|
fn add_episode_actions(&self, user: &User, actions: Vec<EpisodeAction>)
|
|
-> Result<i64, AuthErr>;
|
|
|
|
/// Retrieve the list of episode actions for the given user.
|
|
fn episode_actions_for_user(
|
|
&self,
|
|
user: &User,
|
|
since: Option<i64>,
|
|
podcast: Option<String>,
|
|
device: Option<String>,
|
|
aggregated: bool,
|
|
) -> Result<(i64, Vec<EpisodeAction>), AuthErr>;
|
|
}
|