Compare commits
No commits in common. "78420eed68ee4fec64e58d7310c6817a47a5a107" and "f9ffc21a3f69dc17637654d918eb6683345835c7" have entirely different histories.
78420eed68
...
f9ffc21a3f
|
|
@ -6,10 +6,9 @@ pub fn serve(config: &crate::config::Config) -> u8 {
|
||||||
tracing::info!("Initializing database and running migrations");
|
tracing::info!("Initializing database and running migrations");
|
||||||
|
|
||||||
let pool = db::initialize_db(config.data_dir.join(crate::DB_FILENAME), true).unwrap();
|
let pool = db::initialize_db(config.data_dir.join(crate::DB_FILENAME), true).unwrap();
|
||||||
let repo = db::SqliteRepository::from(pool);
|
|
||||||
|
|
||||||
let ctx = server::Context {
|
let ctx = server::Context {
|
||||||
store: crate::gpodder::GpodderRepository::new(repo),
|
repo: db::SqliteRepository::from(pool),
|
||||||
};
|
};
|
||||||
let app = server::app(ctx);
|
let app = server::app(ctx);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,18 +11,13 @@ use crate::db::{schema::*, DbPool, DbResult};
|
||||||
pub struct Session {
|
pub struct Session {
|
||||||
pub id: i64,
|
pub id: i64,
|
||||||
pub user_id: i64,
|
pub user_id: i64,
|
||||||
pub last_seen: i64,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Session {
|
impl Session {
|
||||||
pub fn new_for_user(pool: &DbPool, user_id: i64, last_seen: i64) -> DbResult<Self> {
|
pub fn new_for_user(pool: &DbPool, user_id: i64) -> DbResult<Self> {
|
||||||
let id: i64 = rand::thread_rng().gen();
|
let id: i64 = rand::thread_rng().gen();
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self { id, user_id }
|
||||||
id,
|
|
||||||
user_id,
|
|
||||||
last_seen,
|
|
||||||
}
|
|
||||||
.insert_into(sessions::table)
|
.insert_into(sessions::table)
|
||||||
.returning(Self::as_returning())
|
.returning(Self::as_returning())
|
||||||
.get_result(&mut pool.get()?)?)
|
.get_result(&mut pool.get()?)?)
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,3 @@
|
||||||
use chrono::DateTime;
|
|
||||||
use diesel::prelude::*;
|
use diesel::prelude::*;
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
|
|
||||||
|
|
@ -20,16 +19,6 @@ impl From<diesel::result::Error> for gpodder::AuthErr {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<db::User> for gpodder::User {
|
|
||||||
fn from(value: db::User) -> Self {
|
|
||||||
Self {
|
|
||||||
id: value.id,
|
|
||||||
username: value.username,
|
|
||||||
password_hash: value.password_hash,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl gpodder::AuthRepository for SqliteRepository {
|
impl gpodder::AuthRepository for SqliteRepository {
|
||||||
fn validate_credentials(
|
fn validate_credentials(
|
||||||
&self,
|
&self,
|
||||||
|
|
@ -46,7 +35,6 @@ impl gpodder::AuthRepository for SqliteRepository {
|
||||||
Ok(gpodder::User {
|
Ok(gpodder::User {
|
||||||
id: user.id,
|
id: user.id,
|
||||||
username: user.username,
|
username: user.username,
|
||||||
password_hash: user.password_hash,
|
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
Err(gpodder::AuthErr::InvalidPassword)
|
Err(gpodder::AuthErr::InvalidPassword)
|
||||||
|
|
@ -66,7 +54,6 @@ impl gpodder::AuthRepository for SqliteRepository {
|
||||||
Ok(user) => Ok(gpodder::User {
|
Ok(user) => Ok(gpodder::User {
|
||||||
id: user.id,
|
id: user.id,
|
||||||
username: user.username,
|
username: user.username,
|
||||||
password_hash: user.password_hash,
|
|
||||||
}),
|
}),
|
||||||
Err(diesel::result::Error::NotFound) => Err(gpodder::AuthErr::UnknownSession),
|
Err(diesel::result::Error::NotFound) => Err(gpodder::AuthErr::UnknownSession),
|
||||||
Err(err) => Err(gpodder::AuthErr::Other(Box::new(err))),
|
Err(err) => Err(gpodder::AuthErr::Other(Box::new(err))),
|
||||||
|
|
@ -90,7 +77,6 @@ impl gpodder::AuthRepository for SqliteRepository {
|
||||||
let session_id = db::Session {
|
let session_id = db::Session {
|
||||||
id,
|
id,
|
||||||
user_id: user.id,
|
user_id: user.id,
|
||||||
last_seen: chrono::Utc::now().timestamp(),
|
|
||||||
}
|
}
|
||||||
.insert_into(sessions::table)
|
.insert_into(sessions::table)
|
||||||
.returning(sessions::id)
|
.returning(sessions::id)
|
||||||
|
|
@ -101,7 +87,6 @@ impl gpodder::AuthRepository for SqliteRepository {
|
||||||
gpodder::User {
|
gpodder::User {
|
||||||
id: user.id,
|
id: user.id,
|
||||||
username: user.username,
|
username: user.username,
|
||||||
password_hash: user.password_hash,
|
|
||||||
},
|
},
|
||||||
))
|
))
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -136,49 +121,3 @@ impl gpodder::AuthRepository for SqliteRepository {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl gpodder::AuthStore for SqliteRepository {
|
|
||||||
fn get_user(&self, username: &str) -> Result<Option<gpodder::models::User>, AuthErr> {
|
|
||||||
Ok(users::table
|
|
||||||
.select(db::User::as_select())
|
|
||||||
.filter(users::username.eq(username))
|
|
||||||
.first(&mut self.pool.get()?)
|
|
||||||
.optional()?
|
|
||||||
.map(gpodder::User::from))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_session(&self, session_id: i64) -> Result<Option<gpodder::models::Session>, AuthErr> {
|
|
||||||
match sessions::table
|
|
||||||
.inner_join(users::table)
|
|
||||||
.filter(sessions::id.eq(session_id))
|
|
||||||
.select((db::Session::as_select(), db::User::as_select()))
|
|
||||||
.get_result(&mut self.pool.get()?)
|
|
||||||
{
|
|
||||||
Ok((session, user)) => Ok(Some(gpodder::Session {
|
|
||||||
id: session.id,
|
|
||||||
last_seen: DateTime::from_timestamp(session.last_seen, 0).unwrap(),
|
|
||||||
user: user.into(),
|
|
||||||
})),
|
|
||||||
Err(err) => Err(AuthErr::from(err)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn remove_session(&self, session_id: i64) -> Result<(), AuthErr> {
|
|
||||||
Ok(
|
|
||||||
diesel::delete(sessions::table.filter(sessions::id.eq(session_id)))
|
|
||||||
.execute(&mut self.pool.get()?)
|
|
||||||
.map(|_| ())?,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn insert_session(&self, session: &gpodder::Session) -> Result<(), AuthErr> {
|
|
||||||
Ok(db::Session {
|
|
||||||
id: session.id,
|
|
||||||
user_id: session.user.id,
|
|
||||||
last_seen: session.last_seen.timestamp(),
|
|
||||||
}
|
|
||||||
.insert_into(sessions::table)
|
|
||||||
.execute(&mut self.pool.get()?)
|
|
||||||
.map(|_| ())?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
use chrono::{DateTime, Utc};
|
use chrono::DateTime;
|
||||||
use diesel::prelude::*;
|
use diesel::prelude::*;
|
||||||
|
|
||||||
use super::SqliteRepository;
|
use super::SqliteRepository;
|
||||||
|
|
@ -26,7 +26,7 @@ impl From<gpodder::EpisodeAction> for db::NewEpisodeAction {
|
||||||
podcast_url: value.podcast,
|
podcast_url: value.podcast,
|
||||||
episode_url: value.episode,
|
episode_url: value.episode,
|
||||||
time_changed: 0,
|
time_changed: 0,
|
||||||
timestamp: value.timestamp.map(|t| t.timestamp()),
|
timestamp: value.timestamp.map(|t| t.and_utc().timestamp()),
|
||||||
action,
|
action,
|
||||||
started,
|
started,
|
||||||
position,
|
position,
|
||||||
|
|
@ -58,8 +58,7 @@ impl From<(Option<String>, db::EpisodeAction)> for gpodder::EpisodeAction {
|
||||||
// SAFETY the input to the from_timestamp function is always the result of a
|
// SAFETY the input to the from_timestamp function is always the result of a
|
||||||
// previous timestamp() function call, which is guaranteed to be each other's
|
// previous timestamp() function call, which is guaranteed to be each other's
|
||||||
// reverse
|
// reverse
|
||||||
.map(|ts| DateTime::from_timestamp(ts, 0).unwrap()),
|
.map(|ts| DateTime::from_timestamp(ts, 0).unwrap().naive_utc()),
|
||||||
time_changed: DateTime::from_timestamp(db_action.time_changed, 0).unwrap(),
|
|
||||||
device: device_id,
|
device: device_id,
|
||||||
action,
|
action,
|
||||||
}
|
}
|
||||||
|
|
@ -71,9 +70,8 @@ impl gpodder::EpisodeActionRepository for SqliteRepository {
|
||||||
&self,
|
&self,
|
||||||
user: &gpodder::User,
|
user: &gpodder::User,
|
||||||
actions: Vec<gpodder::EpisodeAction>,
|
actions: Vec<gpodder::EpisodeAction>,
|
||||||
time_changed: DateTime<Utc>,
|
) -> Result<i64, gpodder::AuthErr> {
|
||||||
) -> Result<(), gpodder::AuthErr> {
|
let time_changed = chrono::Utc::now().timestamp();
|
||||||
let time_changed = time_changed.timestamp();
|
|
||||||
|
|
||||||
// TODO optimize this query
|
// TODO optimize this query
|
||||||
// 1. The lookup for a device could be replaced with a subquery, although Diesel seems to
|
// 1. The lookup for a device could be replaced with a subquery, although Diesel seems to
|
||||||
|
|
@ -101,18 +99,17 @@ impl gpodder::EpisodeActionRepository for SqliteRepository {
|
||||||
Ok::<_, diesel::result::Error>(())
|
Ok::<_, diesel::result::Error>(())
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
Ok(())
|
Ok(time_changed + 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn episode_actions_for_user(
|
fn episode_actions_for_user(
|
||||||
&self,
|
&self,
|
||||||
user: &gpodder::User,
|
user: &gpodder::User,
|
||||||
since: Option<DateTime<Utc>>,
|
since: Option<i64>,
|
||||||
podcast: Option<String>,
|
podcast: Option<String>,
|
||||||
device: Option<String>,
|
device: Option<String>,
|
||||||
aggregated: bool,
|
aggregated: bool,
|
||||||
) -> Result<Vec<gpodder::EpisodeAction>, gpodder::AuthErr> {
|
) -> Result<(i64, Vec<gpodder::EpisodeAction>), gpodder::AuthErr> {
|
||||||
let since = since.map(|ts| ts.timestamp()).unwrap_or(0);
|
|
||||||
let conn = &mut self.pool.get()?;
|
let conn = &mut self.pool.get()?;
|
||||||
|
|
||||||
let mut query = episode_actions::table
|
let mut query = episode_actions::table
|
||||||
|
|
@ -120,7 +117,7 @@ impl gpodder::EpisodeActionRepository for SqliteRepository {
|
||||||
.filter(
|
.filter(
|
||||||
episode_actions::user_id
|
episode_actions::user_id
|
||||||
.eq(user.id)
|
.eq(user.id)
|
||||||
.and(episode_actions::time_changed.ge(since)),
|
.and(episode_actions::time_changed.ge(since.unwrap_or(0))),
|
||||||
)
|
)
|
||||||
.select((
|
.select((
|
||||||
devices::device_id.nullable(),
|
devices::device_id.nullable(),
|
||||||
|
|
@ -160,11 +157,16 @@ impl gpodder::EpisodeActionRepository for SqliteRepository {
|
||||||
query.get_results(conn)?
|
query.get_results(conn)?
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let max_timestamp = db_actions
|
||||||
|
.iter()
|
||||||
|
.map(|(_, a)| a.time_changed)
|
||||||
|
.max()
|
||||||
|
.unwrap_or(0);
|
||||||
let actions = db_actions
|
let actions = db_actions
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(gpodder::EpisodeAction::from)
|
.map(gpodder::EpisodeAction::from)
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
Ok(actions)
|
Ok((max_timestamp + 1, actions))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
|
|
||||||
use chrono::DateTime;
|
|
||||||
use diesel::prelude::*;
|
use diesel::prelude::*;
|
||||||
|
|
||||||
use super::SqliteRepository;
|
use super::SqliteRepository;
|
||||||
|
|
@ -9,39 +8,24 @@ use crate::{
|
||||||
gpodder,
|
gpodder,
|
||||||
};
|
};
|
||||||
|
|
||||||
impl From<(String, i64)> for gpodder::Subscription {
|
|
||||||
fn from((url, ts): (String, i64)) -> Self {
|
|
||||||
Self {
|
|
||||||
url,
|
|
||||||
time_changed: DateTime::from_timestamp(ts, 0).unwrap(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl gpodder::SubscriptionRepository for SqliteRepository {
|
impl gpodder::SubscriptionRepository for SqliteRepository {
|
||||||
fn subscriptions_for_user(
|
fn subscriptions_for_user(
|
||||||
&self,
|
&self,
|
||||||
user: &gpodder::User,
|
user: &gpodder::User,
|
||||||
) -> Result<Vec<gpodder::Subscription>, gpodder::AuthErr> {
|
) -> Result<Vec<String>, gpodder::AuthErr> {
|
||||||
Ok(device_subscriptions::table
|
Ok(device_subscriptions::table
|
||||||
.inner_join(devices::table)
|
.inner_join(devices::table)
|
||||||
.filter(devices::user_id.eq(user.id))
|
.filter(devices::user_id.eq(user.id))
|
||||||
.select((
|
.select(device_subscriptions::podcast_url)
|
||||||
device_subscriptions::podcast_url,
|
|
||||||
device_subscriptions::time_changed,
|
|
||||||
))
|
|
||||||
.distinct()
|
.distinct()
|
||||||
.get_results::<(String, i64)>(&mut self.pool.get()?)?
|
.get_results(&mut self.pool.get()?)?)
|
||||||
.into_iter()
|
|
||||||
.map(Into::into)
|
|
||||||
.collect())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn subscriptions_for_device(
|
fn subscriptions_for_device(
|
||||||
&self,
|
&self,
|
||||||
user: &gpodder::User,
|
user: &gpodder::User,
|
||||||
device_id: &str,
|
device_id: &str,
|
||||||
) -> Result<Vec<gpodder::Subscription>, gpodder::AuthErr> {
|
) -> Result<Vec<String>, gpodder::AuthErr> {
|
||||||
Ok(device_subscriptions::table
|
Ok(device_subscriptions::table
|
||||||
.inner_join(devices::table)
|
.inner_join(devices::table)
|
||||||
.filter(
|
.filter(
|
||||||
|
|
@ -49,14 +33,8 @@ impl gpodder::SubscriptionRepository for SqliteRepository {
|
||||||
.eq(user.id)
|
.eq(user.id)
|
||||||
.and(devices::device_id.eq(device_id)),
|
.and(devices::device_id.eq(device_id)),
|
||||||
)
|
)
|
||||||
.select((
|
.select(device_subscriptions::podcast_url)
|
||||||
device_subscriptions::podcast_url,
|
.get_results(&mut self.pool.get()?)?)
|
||||||
device_subscriptions::time_changed,
|
|
||||||
))
|
|
||||||
.get_results::<(String, i64)>(&mut self.pool.get()?)?
|
|
||||||
.into_iter()
|
|
||||||
.map(Into::into)
|
|
||||||
.collect())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_subscriptions_for_device(
|
fn set_subscriptions_for_device(
|
||||||
|
|
@ -64,10 +42,9 @@ impl gpodder::SubscriptionRepository for SqliteRepository {
|
||||||
user: &gpodder::User,
|
user: &gpodder::User,
|
||||||
device_id: &str,
|
device_id: &str,
|
||||||
urls: Vec<String>,
|
urls: Vec<String>,
|
||||||
time_changed: chrono::DateTime<chrono::Utc>,
|
) -> Result<i64, gpodder::AuthErr> {
|
||||||
) -> Result<(), gpodder::AuthErr> {
|
|
||||||
// TODO use a better timestamp
|
// TODO use a better timestamp
|
||||||
let timestamp = time_changed.timestamp();
|
let timestamp = chrono::Utc::now().timestamp();
|
||||||
|
|
||||||
self.pool.get()?.transaction(|conn| {
|
self.pool.get()?.transaction(|conn| {
|
||||||
let device = devices::table
|
let device = devices::table
|
||||||
|
|
@ -149,7 +126,7 @@ impl gpodder::SubscriptionRepository for SqliteRepository {
|
||||||
Ok::<_, diesel::result::Error>(())
|
Ok::<_, diesel::result::Error>(())
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
Ok(())
|
Ok(timestamp + 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_subscriptions_for_device(
|
fn update_subscriptions_for_device(
|
||||||
|
|
@ -158,10 +135,9 @@ impl gpodder::SubscriptionRepository for SqliteRepository {
|
||||||
device_id: &str,
|
device_id: &str,
|
||||||
add: Vec<String>,
|
add: Vec<String>,
|
||||||
remove: Vec<String>,
|
remove: Vec<String>,
|
||||||
time_changed: chrono::DateTime<chrono::Utc>,
|
) -> Result<i64, gpodder::AuthErr> {
|
||||||
) -> Result<(), gpodder::AuthErr> {
|
|
||||||
// TODO use a better timestamp
|
// TODO use a better timestamp
|
||||||
let timestamp = time_changed.timestamp();
|
let timestamp = chrono::Utc::now().timestamp_millis();
|
||||||
|
|
||||||
// TODO URLs that are in both the added and removed lists will currently get "re-added",
|
// TODO URLs that are in both the added and removed lists will currently get "re-added",
|
||||||
// meaning their change timestamp will be updated even though they haven't really changed.
|
// meaning their change timestamp will be updated even though they haven't really changed.
|
||||||
|
|
@ -244,18 +220,16 @@ impl gpodder::SubscriptionRepository for SqliteRepository {
|
||||||
Ok::<_, diesel::result::Error>(())
|
Ok::<_, diesel::result::Error>(())
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
Ok(())
|
Ok(timestamp + 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn subscription_updates_for_device(
|
fn subscription_updates_for_device(
|
||||||
&self,
|
&self,
|
||||||
user: &gpodder::User,
|
user: &gpodder::User,
|
||||||
device_id: &str,
|
device_id: &str,
|
||||||
since: chrono::DateTime<chrono::Utc>,
|
since: i64,
|
||||||
) -> Result<(Vec<gpodder::Subscription>, Vec<gpodder::Subscription>), gpodder::AuthErr> {
|
) -> Result<(i64, Vec<String>, Vec<String>), gpodder::AuthErr> {
|
||||||
let since = since.timestamp();
|
let (mut timestamp, mut added, mut removed) = (0, Vec::new(), Vec::new());
|
||||||
|
|
||||||
let (mut added, mut removed) = (Vec::new(), Vec::new());
|
|
||||||
|
|
||||||
let query = device_subscriptions::table
|
let query = device_subscriptions::table
|
||||||
.inner_join(devices::table)
|
.inner_join(devices::table)
|
||||||
|
|
@ -271,18 +245,14 @@ impl gpodder::SubscriptionRepository for SqliteRepository {
|
||||||
let sub = sub?;
|
let sub = sub?;
|
||||||
|
|
||||||
if sub.deleted {
|
if sub.deleted {
|
||||||
removed.push(gpodder::Subscription {
|
removed.push(sub.podcast_url);
|
||||||
url: sub.podcast_url,
|
|
||||||
time_changed: DateTime::from_timestamp(sub.time_changed, 0).unwrap(),
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
added.push(gpodder::Subscription {
|
added.push(sub.podcast_url);
|
||||||
url: sub.podcast_url,
|
|
||||||
time_changed: DateTime::from_timestamp(sub.time_changed, 0).unwrap(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok((added, removed))
|
timestamp = timestamp.max(sub.time_changed);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok((timestamp + 1, added, removed))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -41,7 +41,6 @@ diesel::table! {
|
||||||
sessions (id) {
|
sessions (id) {
|
||||||
id -> BigInt,
|
id -> BigInt,
|
||||||
user_id -> BigInt,
|
user_id -> BigInt,
|
||||||
last_seen -> BigInt,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,6 @@
|
||||||
pub mod models;
|
pub mod models;
|
||||||
mod repository;
|
|
||||||
|
|
||||||
use chrono::{DateTime, Utc};
|
|
||||||
pub use models::*;
|
pub use models::*;
|
||||||
pub use repository::GpodderRepository;
|
|
||||||
|
|
||||||
pub enum AuthErr {
|
pub enum AuthErr {
|
||||||
UnknownSession,
|
UnknownSession,
|
||||||
|
|
@ -12,16 +9,6 @@ pub enum AuthErr {
|
||||||
Other(Box<dyn std::error::Error + Sync + Send>),
|
Other(Box<dyn std::error::Error + Sync + Send>),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait Store:
|
|
||||||
AuthStore + DeviceRepository + SubscriptionRepository + EpisodeActionRepository
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> Store for T where
|
|
||||||
T: AuthStore + DeviceRepository + SubscriptionRepository + EpisodeActionRepository
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
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>;
|
||||||
|
|
@ -40,20 +27,6 @@ pub trait AuthRepository {
|
||||||
fn remove_session(&self, username: &str, session_id: i64) -> Result<(), 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 {
|
pub trait DeviceRepository {
|
||||||
/// Return all devices associated with the user
|
/// Return all devices associated with the user
|
||||||
fn devices_for_user(&self, user: &User) -> Result<Vec<Device>, AuthErr>;
|
fn devices_for_user(&self, user: &User) -> Result<Vec<Device>, AuthErr>;
|
||||||
|
|
@ -74,10 +47,10 @@ pub trait SubscriptionRepository {
|
||||||
&self,
|
&self,
|
||||||
user: &User,
|
user: &User,
|
||||||
device_id: &str,
|
device_id: &str,
|
||||||
) -> Result<Vec<models::Subscription>, AuthErr>;
|
) -> Result<Vec<String>, AuthErr>;
|
||||||
|
|
||||||
/// Return all subscriptions for a given user
|
/// Return all subscriptions for a given user
|
||||||
fn subscriptions_for_user(&self, user: &User) -> Result<Vec<models::Subscription>, AuthErr>;
|
fn subscriptions_for_user(&self, user: &User) -> Result<Vec<String>, AuthErr>;
|
||||||
|
|
||||||
/// Replace the list of subscriptions for a device with the given list
|
/// Replace the list of subscriptions for a device with the given list
|
||||||
fn set_subscriptions_for_device(
|
fn set_subscriptions_for_device(
|
||||||
|
|
@ -85,8 +58,7 @@ pub trait SubscriptionRepository {
|
||||||
user: &User,
|
user: &User,
|
||||||
device_id: &str,
|
device_id: &str,
|
||||||
urls: Vec<String>,
|
urls: Vec<String>,
|
||||||
time_changed: DateTime<Utc>,
|
) -> Result<i64, AuthErr>;
|
||||||
) -> Result<(), AuthErr>;
|
|
||||||
|
|
||||||
/// Update the list of subscriptions for a device by adding and removing the given URLs
|
/// Update the list of subscriptions for a device by adding and removing the given URLs
|
||||||
fn update_subscriptions_for_device(
|
fn update_subscriptions_for_device(
|
||||||
|
|
@ -95,34 +67,29 @@ pub trait SubscriptionRepository {
|
||||||
device_id: &str,
|
device_id: &str,
|
||||||
add: Vec<String>,
|
add: Vec<String>,
|
||||||
remove: Vec<String>,
|
remove: Vec<String>,
|
||||||
time_changed: DateTime<Utc>,
|
) -> Result<i64, AuthErr>;
|
||||||
) -> Result<(), AuthErr>;
|
|
||||||
|
|
||||||
/// Returns the changes in subscriptions since the given timestamp.
|
/// Returns the changes in subscriptions since the given timestamp.
|
||||||
fn subscription_updates_for_device(
|
fn subscription_updates_for_device(
|
||||||
&self,
|
&self,
|
||||||
user: &User,
|
user: &User,
|
||||||
device_id: &str,
|
device_id: &str,
|
||||||
since: DateTime<Utc>,
|
since: i64,
|
||||||
) -> Result<(Vec<Subscription>, Vec<Subscription>), AuthErr>;
|
) -> Result<(i64, Vec<String>, Vec<String>), AuthErr>;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait EpisodeActionRepository {
|
pub trait EpisodeActionRepository {
|
||||||
/// Insert the given episode actions into the datastore.
|
/// Insert the given episode actions into the datastore.
|
||||||
fn add_episode_actions(
|
fn add_episode_actions(&self, user: &User, actions: Vec<EpisodeAction>)
|
||||||
&self,
|
-> Result<i64, AuthErr>;
|
||||||
user: &User,
|
|
||||||
actions: Vec<EpisodeAction>,
|
|
||||||
time_changed: DateTime<Utc>,
|
|
||||||
) -> Result<(), AuthErr>;
|
|
||||||
|
|
||||||
/// Retrieve the list of episode actions for the given user.
|
/// Retrieve the list of episode actions for the given user.
|
||||||
fn episode_actions_for_user(
|
fn episode_actions_for_user(
|
||||||
&self,
|
&self,
|
||||||
user: &User,
|
user: &User,
|
||||||
since: Option<DateTime<Utc>>,
|
since: Option<i64>,
|
||||||
podcast: Option<String>,
|
podcast: Option<String>,
|
||||||
device: Option<String>,
|
device: Option<String>,
|
||||||
aggregated: bool,
|
aggregated: bool,
|
||||||
) -> Result<Vec<EpisodeAction>, AuthErr>;
|
) -> Result<(i64, Vec<EpisodeAction>), AuthErr>;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,14 @@
|
||||||
use chrono::{DateTime, Utc};
|
use chrono::NaiveDateTime;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct User {
|
pub struct User {
|
||||||
pub id: i64,
|
pub id: i64,
|
||||||
pub username: String,
|
pub username: String,
|
||||||
pub password_hash: String,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
#[serde(rename_all = "lowercase")]
|
||||||
pub enum DeviceType {
|
pub enum DeviceType {
|
||||||
Desktop,
|
Desktop,
|
||||||
Laptop,
|
Laptop,
|
||||||
|
|
@ -15,6 +17,7 @@ pub enum DeviceType {
|
||||||
Other,
|
Other,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
pub struct Device {
|
pub struct Device {
|
||||||
pub id: String,
|
pub id: String,
|
||||||
pub caption: String,
|
pub caption: String,
|
||||||
|
|
@ -22,38 +25,35 @@ pub struct Device {
|
||||||
pub subscriptions: i64,
|
pub subscriptions: i64,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
pub struct DevicePatch {
|
pub struct DevicePatch {
|
||||||
pub caption: Option<String>,
|
pub caption: Option<String>,
|
||||||
pub r#type: Option<DeviceType>,
|
pub r#type: Option<DeviceType>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
#[serde(rename_all = "lowercase")]
|
||||||
|
#[serde(tag = "action")]
|
||||||
pub enum EpisodeActionType {
|
pub enum EpisodeActionType {
|
||||||
Download,
|
Download,
|
||||||
Play {
|
Play {
|
||||||
|
#[serde(default)]
|
||||||
started: Option<i32>,
|
started: Option<i32>,
|
||||||
position: i32,
|
position: i32,
|
||||||
|
#[serde(default)]
|
||||||
total: Option<i32>,
|
total: Option<i32>,
|
||||||
},
|
},
|
||||||
Delete,
|
Delete,
|
||||||
New,
|
New,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
pub struct EpisodeAction {
|
pub struct EpisodeAction {
|
||||||
pub podcast: String,
|
pub podcast: String,
|
||||||
pub episode: String,
|
pub episode: String,
|
||||||
pub timestamp: Option<DateTime<Utc>>,
|
pub timestamp: Option<NaiveDateTime>,
|
||||||
pub time_changed: DateTime<Utc>,
|
#[serde(default)]
|
||||||
pub device: Option<String>,
|
pub device: Option<String>,
|
||||||
|
#[serde(flatten)]
|
||||||
pub action: EpisodeActionType,
|
pub action: EpisodeActionType,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Session {
|
|
||||||
pub id: i64,
|
|
||||||
pub last_seen: DateTime<Utc>,
|
|
||||||
pub user: User,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Subscription {
|
|
||||||
pub url: String,
|
|
||||||
pub time_changed: DateTime<Utc>,
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -1,186 +0,0 @@
|
||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
use argon2::{Argon2, PasswordHash, PasswordVerifier};
|
|
||||||
use chrono::{DateTime, TimeDelta, Utc};
|
|
||||||
use rand::Rng;
|
|
||||||
|
|
||||||
use super::{models, AuthErr, Store};
|
|
||||||
|
|
||||||
const MAX_SESSION_AGE: i64 = 60 * 60 * 24 * 7;
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct GpodderRepository {
|
|
||||||
store: Arc<dyn Store + Send + Sync>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl GpodderRepository {
|
|
||||||
pub fn new(store: impl Store + Send + Sync + 'static) -> Self {
|
|
||||||
Self {
|
|
||||||
store: Arc::new(store),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn validate_session(&self, session_id: i64) -> Result<models::Session, AuthErr> {
|
|
||||||
let session = self
|
|
||||||
.store
|
|
||||||
.get_session(session_id)?
|
|
||||||
.ok_or(AuthErr::UnknownSession)?;
|
|
||||||
|
|
||||||
// Expired sessions still in the database are considered removed
|
|
||||||
if Utc::now() - session.last_seen > TimeDelta::new(MAX_SESSION_AGE, 0).unwrap() {
|
|
||||||
Err(AuthErr::UnknownSession)
|
|
||||||
} else {
|
|
||||||
Ok(session)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn validate_credentials(
|
|
||||||
&self,
|
|
||||||
username: &str,
|
|
||||||
password: &str,
|
|
||||||
) -> Result<models::User, AuthErr> {
|
|
||||||
let user = self.store.get_user(username)?.ok_or(AuthErr::UnknownUser)?;
|
|
||||||
|
|
||||||
let password_hash = PasswordHash::new(&user.password_hash).unwrap();
|
|
||||||
|
|
||||||
if Argon2::default()
|
|
||||||
.verify_password(password.as_bytes(), &password_hash)
|
|
||||||
.is_ok()
|
|
||||||
{
|
|
||||||
Ok(user)
|
|
||||||
} else {
|
|
||||||
Err(AuthErr::InvalidPassword)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn create_session(&self, user: &models::User) -> Result<models::Session, AuthErr> {
|
|
||||||
let session = models::Session {
|
|
||||||
id: rand::thread_rng().gen(),
|
|
||||||
last_seen: Utc::now(),
|
|
||||||
user: user.clone(),
|
|
||||||
};
|
|
||||||
|
|
||||||
self.store.insert_session(&session)?;
|
|
||||||
|
|
||||||
Ok(session)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn remove_session(&self, session_id: i64) -> Result<(), AuthErr> {
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn subscriptions_for_device(
|
|
||||||
&self,
|
|
||||||
user: &models::User,
|
|
||||||
device_id: &str,
|
|
||||||
) -> Result<Vec<models::Subscription>, AuthErr> {
|
|
||||||
self.store.subscriptions_for_device(user, device_id)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn subscriptions_for_user(
|
|
||||||
&self,
|
|
||||||
user: &models::User,
|
|
||||||
) -> Result<Vec<models::Subscription>, AuthErr> {
|
|
||||||
self.store.subscriptions_for_user(user)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn set_subscriptions_for_device(
|
|
||||||
&self,
|
|
||||||
user: &models::User,
|
|
||||||
device_id: &str,
|
|
||||||
urls: Vec<String>,
|
|
||||||
) -> Result<DateTime<Utc>, AuthErr> {
|
|
||||||
let time_changed = Utc::now();
|
|
||||||
|
|
||||||
self.store
|
|
||||||
.set_subscriptions_for_device(user, device_id, urls, time_changed)?;
|
|
||||||
|
|
||||||
Ok(time_changed + TimeDelta::seconds(1))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn update_subscriptions_for_device(
|
|
||||||
&self,
|
|
||||||
user: &models::User,
|
|
||||||
device_id: &str,
|
|
||||||
add: Vec<String>,
|
|
||||||
remove: Vec<String>,
|
|
||||||
) -> Result<DateTime<Utc>, AuthErr> {
|
|
||||||
let time_changed = Utc::now();
|
|
||||||
|
|
||||||
self.store
|
|
||||||
.update_subscriptions_for_device(user, device_id, add, remove, time_changed)?;
|
|
||||||
|
|
||||||
Ok(time_changed + TimeDelta::seconds(1))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn subscription_updates_for_device(
|
|
||||||
&self,
|
|
||||||
user: &models::User,
|
|
||||||
device_id: &str,
|
|
||||||
since: DateTime<Utc>,
|
|
||||||
) -> Result<
|
|
||||||
(
|
|
||||||
DateTime<Utc>,
|
|
||||||
Vec<models::Subscription>,
|
|
||||||
Vec<models::Subscription>,
|
|
||||||
),
|
|
||||||
AuthErr,
|
|
||||||
> {
|
|
||||||
let now = chrono::Utc::now();
|
|
||||||
|
|
||||||
let (added, removed) = self
|
|
||||||
.store
|
|
||||||
.subscription_updates_for_device(user, device_id, since)?;
|
|
||||||
|
|
||||||
let max_time_changed = added
|
|
||||||
.iter()
|
|
||||||
.chain(removed.iter())
|
|
||||||
.map(|s| s.time_changed)
|
|
||||||
.max()
|
|
||||||
.unwrap_or(now);
|
|
||||||
Ok((max_time_changed + TimeDelta::seconds(1), added, removed))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn add_episode_actions(
|
|
||||||
&self,
|
|
||||||
user: &models::User,
|
|
||||||
actions: Vec<models::EpisodeAction>,
|
|
||||||
) -> Result<DateTime<Utc>, AuthErr> {
|
|
||||||
let time_changed = Utc::now();
|
|
||||||
|
|
||||||
self.store
|
|
||||||
.add_episode_actions(user, actions, time_changed)?;
|
|
||||||
|
|
||||||
Ok(time_changed + TimeDelta::seconds(1))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn episode_actions_for_user(
|
|
||||||
&self,
|
|
||||||
user: &models::User,
|
|
||||||
since: Option<DateTime<Utc>>,
|
|
||||||
podcast: Option<String>,
|
|
||||||
device: Option<String>,
|
|
||||||
aggregated: bool,
|
|
||||||
) -> Result<(DateTime<Utc>, Vec<models::EpisodeAction>), AuthErr> {
|
|
||||||
let now = chrono::Utc::now();
|
|
||||||
let actions = self
|
|
||||||
.store
|
|
||||||
.episode_actions_for_user(user, since, podcast, device, aggregated)?;
|
|
||||||
let max_time_changed = actions.iter().map(|a| a.time_changed).max().unwrap_or(now);
|
|
||||||
|
|
||||||
Ok((max_time_changed + TimeDelta::seconds(1), actions))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -12,10 +12,13 @@ use axum_extra::{
|
||||||
TypedHeader,
|
TypedHeader,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::server::{
|
use crate::{
|
||||||
|
gpodder::AuthRepository,
|
||||||
|
server::{
|
||||||
error::{AppError, AppResult},
|
error::{AppError, AppResult},
|
||||||
gpodder::SESSION_ID_COOKIE,
|
gpodder::SESSION_ID_COOKIE,
|
||||||
Context,
|
Context,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn router() -> Router<Context> {
|
pub fn router() -> Router<Context> {
|
||||||
|
|
@ -35,17 +38,14 @@ async fn post_login(
|
||||||
return Err(AppError::BadRequest);
|
return Err(AppError::BadRequest);
|
||||||
}
|
}
|
||||||
|
|
||||||
let session = tokio::task::spawn_blocking(move || {
|
let (session_id, _) = tokio::task::spawn_blocking(move || {
|
||||||
let user = ctx
|
ctx.repo.create_session(auth.username(), auth.password())
|
||||||
.store
|
|
||||||
.validate_credentials(auth.username(), auth.password())?;
|
|
||||||
ctx.store.create_session(&user)
|
|
||||||
})
|
})
|
||||||
.await
|
.await
|
||||||
.unwrap()?;
|
.unwrap()?;
|
||||||
|
|
||||||
Ok(jar.add(
|
Ok(jar.add(
|
||||||
Cookie::build((SESSION_ID_COOKIE, session.id.to_string())).expires(Expiration::Session),
|
Cookie::build((SESSION_ID_COOKIE, session_id.to_string())).expires(Expiration::Session),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -60,8 +60,7 @@ async fn post_logout(
|
||||||
.parse()
|
.parse()
|
||||||
.map_err(|_| AppError::BadRequest)?;
|
.map_err(|_| AppError::BadRequest)?;
|
||||||
|
|
||||||
// TODO reintroduce username check
|
tokio::task::spawn_blocking(move || ctx.repo.remove_session(&username, session_id))
|
||||||
tokio::task::spawn_blocking(move || ctx.store.remove_session(session_id))
|
|
||||||
.await
|
.await
|
||||||
.unwrap()?;
|
.unwrap()?;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,13 +6,12 @@ use axum::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
gpodder,
|
gpodder::{self, DeviceRepository},
|
||||||
server::{
|
server::{
|
||||||
error::{AppError, AppResult},
|
error::{AppError, AppResult},
|
||||||
gpodder::{
|
gpodder::{
|
||||||
auth_middleware,
|
auth_middleware,
|
||||||
format::{Format, StringWithFormat},
|
format::{Format, StringWithFormat},
|
||||||
models,
|
|
||||||
},
|
},
|
||||||
Context,
|
Context,
|
||||||
},
|
},
|
||||||
|
|
@ -29,7 +28,7 @@ async fn get_devices(
|
||||||
State(ctx): State<Context>,
|
State(ctx): State<Context>,
|
||||||
Path(username): Path<StringWithFormat>,
|
Path(username): Path<StringWithFormat>,
|
||||||
Extension(user): Extension<gpodder::User>,
|
Extension(user): Extension<gpodder::User>,
|
||||||
) -> AppResult<Json<Vec<models::Device>>> {
|
) -> AppResult<Json<Vec<gpodder::Device>>> {
|
||||||
if username.format != Format::Json {
|
if username.format != Format::Json {
|
||||||
return Err(AppError::NotFound);
|
return Err(AppError::NotFound);
|
||||||
}
|
}
|
||||||
|
|
@ -39,10 +38,10 @@ async fn get_devices(
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(
|
Ok(
|
||||||
tokio::task::spawn_blocking(move || ctx.store.devices_for_user(&user))
|
tokio::task::spawn_blocking(move || ctx.repo.devices_for_user(&user))
|
||||||
.await
|
.await
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.map(|devices| Json(devices.into_iter().map(models::Device::from).collect()))?,
|
.map(Json)?,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -50,13 +49,13 @@ async fn post_device(
|
||||||
State(ctx): State<Context>,
|
State(ctx): State<Context>,
|
||||||
Path((_username, id)): Path<(String, StringWithFormat)>,
|
Path((_username, id)): Path<(String, StringWithFormat)>,
|
||||||
Extension(user): Extension<gpodder::User>,
|
Extension(user): Extension<gpodder::User>,
|
||||||
Json(patch): Json<models::DevicePatch>,
|
Json(patch): Json<gpodder::DevicePatch>,
|
||||||
) -> AppResult<()> {
|
) -> AppResult<()> {
|
||||||
if id.format != Format::Json {
|
if id.format != Format::Json {
|
||||||
return Err(AppError::NotFound);
|
return Err(AppError::NotFound);
|
||||||
}
|
}
|
||||||
|
|
||||||
tokio::task::spawn_blocking(move || ctx.store.update_device_info(&user, &id, patch.into()))
|
tokio::task::spawn_blocking(move || ctx.repo.update_device_info(&user, &id, patch))
|
||||||
.await
|
.await
|
||||||
.unwrap()?;
|
.unwrap()?;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,17 +4,15 @@ use axum::{
|
||||||
routing::post,
|
routing::post,
|
||||||
Extension, Json, Router,
|
Extension, Json, Router,
|
||||||
};
|
};
|
||||||
use chrono::DateTime;
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
gpodder,
|
gpodder::{self, EpisodeActionRepository},
|
||||||
server::{
|
server::{
|
||||||
error::{AppError, AppResult},
|
error::{AppError, AppResult},
|
||||||
gpodder::{
|
gpodder::{
|
||||||
auth_middleware,
|
auth_middleware,
|
||||||
format::{Format, StringWithFormat},
|
format::{Format, StringWithFormat},
|
||||||
models,
|
|
||||||
models::UpdatedUrlsResponse,
|
models::UpdatedUrlsResponse,
|
||||||
},
|
},
|
||||||
Context,
|
Context,
|
||||||
|
|
@ -34,7 +32,7 @@ async fn post_episode_actions(
|
||||||
State(ctx): State<Context>,
|
State(ctx): State<Context>,
|
||||||
Path(username): Path<StringWithFormat>,
|
Path(username): Path<StringWithFormat>,
|
||||||
Extension(user): Extension<gpodder::User>,
|
Extension(user): Extension<gpodder::User>,
|
||||||
Json(actions): Json<Vec<models::EpisodeAction>>,
|
Json(actions): Json<Vec<gpodder::EpisodeAction>>,
|
||||||
) -> AppResult<Json<UpdatedUrlsResponse>> {
|
) -> AppResult<Json<UpdatedUrlsResponse>> {
|
||||||
if username.format != Format::Json {
|
if username.format != Format::Json {
|
||||||
return Err(AppError::NotFound);
|
return Err(AppError::NotFound);
|
||||||
|
|
@ -44,18 +42,17 @@ async fn post_episode_actions(
|
||||||
return Err(AppError::BadRequest);
|
return Err(AppError::BadRequest);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(tokio::task::spawn_blocking(move || {
|
Ok(
|
||||||
ctx.store
|
tokio::task::spawn_blocking(move || ctx.repo.add_episode_actions(&user, actions))
|
||||||
.add_episode_actions(&user, actions.into_iter().map(Into::into).collect())
|
|
||||||
})
|
|
||||||
.await
|
.await
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.map(|time_changed| {
|
.map(|timestamp| {
|
||||||
Json(UpdatedUrlsResponse {
|
Json(UpdatedUrlsResponse {
|
||||||
timestamp: time_changed.timestamp(),
|
timestamp,
|
||||||
update_urls: Vec::new(),
|
update_urls: Vec::new(),
|
||||||
})
|
})
|
||||||
})?)
|
})?,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Default)]
|
#[derive(Deserialize, Default)]
|
||||||
|
|
@ -70,7 +67,7 @@ struct FilterQuery {
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
struct EpisodeActionsResponse {
|
struct EpisodeActionsResponse {
|
||||||
timestamp: i64,
|
timestamp: i64,
|
||||||
actions: Vec<models::EpisodeAction>,
|
actions: Vec<gpodder::EpisodeAction>,
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_episode_actions(
|
async fn get_episode_actions(
|
||||||
|
|
@ -87,15 +84,10 @@ async fn get_episode_actions(
|
||||||
return Err(AppError::BadRequest);
|
return Err(AppError::BadRequest);
|
||||||
}
|
}
|
||||||
|
|
||||||
let since = filter
|
|
||||||
.since
|
|
||||||
.map(|ts| DateTime::from_timestamp(ts, 0))
|
|
||||||
.flatten();
|
|
||||||
|
|
||||||
Ok(tokio::task::spawn_blocking(move || {
|
Ok(tokio::task::spawn_blocking(move || {
|
||||||
ctx.store.episode_actions_for_user(
|
ctx.repo.episode_actions_for_user(
|
||||||
&user,
|
&user,
|
||||||
since,
|
filter.since,
|
||||||
filter.podcast,
|
filter.podcast,
|
||||||
filter.device,
|
filter.device,
|
||||||
filter.aggregated,
|
filter.aggregated,
|
||||||
|
|
@ -103,10 +95,5 @@ async fn get_episode_actions(
|
||||||
})
|
})
|
||||||
.await
|
.await
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.map(|(ts, actions)| {
|
.map(|(timestamp, actions)| Json(EpisodeActionsResponse { timestamp, actions }))?)
|
||||||
Json(EpisodeActionsResponse {
|
|
||||||
timestamp: ts.timestamp(),
|
|
||||||
actions: actions.into_iter().map(Into::into).collect(),
|
|
||||||
})
|
|
||||||
})?)
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ use axum::{
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
gpodder,
|
gpodder::{self, SubscriptionRepository},
|
||||||
server::{
|
server::{
|
||||||
error::{AppError, AppResult},
|
error::{AppError, AppResult},
|
||||||
gpodder::{
|
gpodder::{
|
||||||
|
|
@ -43,14 +43,14 @@ pub async fn post_subscription_changes(
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(tokio::task::spawn_blocking(move || {
|
Ok(tokio::task::spawn_blocking(move || {
|
||||||
ctx.store
|
ctx.repo
|
||||||
.update_subscriptions_for_device(&user, &id, delta.add, delta.remove)
|
.update_subscriptions_for_device(&user, &id, delta.add, delta.remove)
|
||||||
})
|
})
|
||||||
.await
|
.await
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.map(|time_changed| {
|
.map(|timestamp| {
|
||||||
Json(UpdatedUrlsResponse {
|
Json(UpdatedUrlsResponse {
|
||||||
timestamp: time_changed.timestamp(),
|
timestamp,
|
||||||
update_urls: Vec::new(),
|
update_urls: Vec::new(),
|
||||||
})
|
})
|
||||||
})?)
|
})?)
|
||||||
|
|
@ -76,18 +76,17 @@ pub async fn get_subscription_changes(
|
||||||
return Err(AppError::BadRequest);
|
return Err(AppError::BadRequest);
|
||||||
}
|
}
|
||||||
|
|
||||||
let since = chrono::DateTime::from_timestamp(query.since, 0).ok_or(AppError::BadRequest)?;
|
|
||||||
|
|
||||||
Ok(tokio::task::spawn_blocking(move || {
|
Ok(tokio::task::spawn_blocking(move || {
|
||||||
ctx.store.subscription_updates_for_device(&user, &id, since)
|
ctx.repo
|
||||||
|
.subscription_updates_for_device(&user, &id, query.since)
|
||||||
})
|
})
|
||||||
.await
|
.await
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.map(|(next_time_changed, add, remove)| {
|
.map(|(timestamp, add, remove)| {
|
||||||
Json(SubscriptionDeltaResponse {
|
Json(SubscriptionDeltaResponse {
|
||||||
add: add.into_iter().map(|s| s.url).collect(),
|
add,
|
||||||
remove: remove.into_iter().map(|s| s.url).collect(),
|
remove,
|
||||||
timestamp: next_time_changed.timestamp(),
|
timestamp,
|
||||||
})
|
})
|
||||||
})?)
|
})?)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,10 @@ use axum_extra::{
|
||||||
};
|
};
|
||||||
use tower_http::set_header::SetResponseHeaderLayer;
|
use tower_http::set_header::SetResponseHeaderLayer;
|
||||||
|
|
||||||
use crate::{gpodder, server::error::AppError};
|
use crate::{
|
||||||
|
gpodder::{self, AuthRepository},
|
||||||
|
server::error::AppError,
|
||||||
|
};
|
||||||
|
|
||||||
use super::Context;
|
use super::Context;
|
||||||
|
|
||||||
|
|
@ -38,6 +41,8 @@ pub fn router(ctx: Context) -> Router<Context> {
|
||||||
|
|
||||||
/// This middleware accepts
|
/// This middleware accepts
|
||||||
pub async fn auth_middleware(State(ctx): State<Context>, mut req: Request, next: Next) -> Response {
|
pub async fn auth_middleware(State(ctx): State<Context>, mut req: Request, next: Next) -> Response {
|
||||||
|
tracing::debug!("{:?}", req.headers());
|
||||||
|
|
||||||
// SAFETY: this extractor's error type is Infallible
|
// SAFETY: this extractor's error type is Infallible
|
||||||
let mut jar: CookieJar = req.extract_parts().await.unwrap();
|
let mut jar: CookieJar = req.extract_parts().await.unwrap();
|
||||||
let mut auth_user = None;
|
let mut auth_user = None;
|
||||||
|
|
@ -48,12 +53,12 @@ pub async fn auth_middleware(State(ctx): State<Context>, mut req: Request, next:
|
||||||
.and_then(|c| c.value().parse::<i64>().ok())
|
.and_then(|c| c.value().parse::<i64>().ok())
|
||||||
{
|
{
|
||||||
let ctx_clone = ctx.clone();
|
let ctx_clone = ctx.clone();
|
||||||
match tokio::task::spawn_blocking(move || ctx_clone.store.validate_session(session_id))
|
match tokio::task::spawn_blocking(move || ctx_clone.repo.validate_session(session_id))
|
||||||
.await
|
.await
|
||||||
.unwrap()
|
.unwrap()
|
||||||
{
|
{
|
||||||
Ok(session) => {
|
Ok(user) => {
|
||||||
auth_user = Some(session.user);
|
auth_user = Some(user);
|
||||||
}
|
}
|
||||||
Err(gpodder::AuthErr::UnknownSession) => {
|
Err(gpodder::AuthErr::UnknownSession) => {
|
||||||
jar = jar.add(
|
jar = jar.add(
|
||||||
|
|
@ -74,7 +79,7 @@ pub async fn auth_middleware(State(ctx): State<Context>, mut req: Request, next:
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
match tokio::task::spawn_blocking(move || {
|
match tokio::task::spawn_blocking(move || {
|
||||||
ctx.store
|
ctx.repo
|
||||||
.validate_credentials(auth.username(), auth.password())
|
.validate_credentials(auth.username(), auth.password())
|
||||||
})
|
})
|
||||||
.await
|
.await
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,5 @@
|
||||||
use chrono::{DateTime, Utc};
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::gpodder;
|
|
||||||
|
|
||||||
#[derive(Deserialize, Debug)]
|
#[derive(Deserialize, Debug)]
|
||||||
pub struct SubscriptionDelta {
|
pub struct SubscriptionDelta {
|
||||||
pub add: Vec<String>,
|
pub add: Vec<String>,
|
||||||
|
|
@ -21,164 +18,3 @@ pub struct UpdatedUrlsResponse {
|
||||||
pub timestamp: i64,
|
pub timestamp: i64,
|
||||||
pub update_urls: Vec<(String, String)>,
|
pub update_urls: Vec<(String, String)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
|
||||||
#[serde(rename_all = "lowercase")]
|
|
||||||
pub enum DeviceType {
|
|
||||||
Desktop,
|
|
||||||
Laptop,
|
|
||||||
Mobile,
|
|
||||||
Server,
|
|
||||||
Other,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize)]
|
|
||||||
pub struct Device {
|
|
||||||
pub id: String,
|
|
||||||
pub caption: String,
|
|
||||||
pub r#type: DeviceType,
|
|
||||||
pub subscriptions: i64,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
|
||||||
pub struct DevicePatch {
|
|
||||||
pub caption: Option<String>,
|
|
||||||
pub r#type: Option<DeviceType>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
|
||||||
#[serde(rename_all = "lowercase")]
|
|
||||||
#[serde(tag = "action")]
|
|
||||||
pub enum EpisodeActionType {
|
|
||||||
Download,
|
|
||||||
Play {
|
|
||||||
#[serde(default)]
|
|
||||||
started: Option<i32>,
|
|
||||||
position: i32,
|
|
||||||
#[serde(default)]
|
|
||||||
total: Option<i32>,
|
|
||||||
},
|
|
||||||
Delete,
|
|
||||||
New,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
|
||||||
pub struct EpisodeAction {
|
|
||||||
pub podcast: String,
|
|
||||||
pub episode: String,
|
|
||||||
pub timestamp: Option<i64>,
|
|
||||||
#[serde(default)]
|
|
||||||
pub device: Option<String>,
|
|
||||||
#[serde(flatten)]
|
|
||||||
pub action: EpisodeActionType,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<gpodder::DeviceType> for DeviceType {
|
|
||||||
fn from(value: gpodder::DeviceType) -> Self {
|
|
||||||
match value {
|
|
||||||
gpodder::DeviceType::Other => Self::Other,
|
|
||||||
gpodder::DeviceType::Laptop => Self::Laptop,
|
|
||||||
gpodder::DeviceType::Mobile => Self::Mobile,
|
|
||||||
gpodder::DeviceType::Server => Self::Server,
|
|
||||||
gpodder::DeviceType::Desktop => Self::Desktop,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<DeviceType> for gpodder::DeviceType {
|
|
||||||
fn from(value: DeviceType) -> Self {
|
|
||||||
match value {
|
|
||||||
DeviceType::Other => gpodder::DeviceType::Other,
|
|
||||||
DeviceType::Laptop => gpodder::DeviceType::Laptop,
|
|
||||||
DeviceType::Mobile => gpodder::DeviceType::Mobile,
|
|
||||||
DeviceType::Server => gpodder::DeviceType::Server,
|
|
||||||
DeviceType::Desktop => gpodder::DeviceType::Desktop,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<gpodder::Device> for Device {
|
|
||||||
fn from(value: gpodder::Device) -> Self {
|
|
||||||
Self {
|
|
||||||
id: value.id,
|
|
||||||
caption: value.caption,
|
|
||||||
r#type: value.r#type.into(),
|
|
||||||
subscriptions: value.subscriptions,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<DevicePatch> for gpodder::DevicePatch {
|
|
||||||
fn from(value: DevicePatch) -> Self {
|
|
||||||
Self {
|
|
||||||
caption: value.caption,
|
|
||||||
r#type: value.r#type.map(Into::into),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<gpodder::EpisodeActionType> for EpisodeActionType {
|
|
||||||
fn from(value: gpodder::EpisodeActionType) -> Self {
|
|
||||||
match value {
|
|
||||||
gpodder::EpisodeActionType::New => Self::New,
|
|
||||||
gpodder::EpisodeActionType::Delete => Self::Delete,
|
|
||||||
gpodder::EpisodeActionType::Download => Self::Download,
|
|
||||||
gpodder::EpisodeActionType::Play {
|
|
||||||
started,
|
|
||||||
position,
|
|
||||||
total,
|
|
||||||
} => Self::Play {
|
|
||||||
started,
|
|
||||||
position,
|
|
||||||
total,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<EpisodeActionType> for gpodder::EpisodeActionType {
|
|
||||||
fn from(value: EpisodeActionType) -> Self {
|
|
||||||
match value {
|
|
||||||
EpisodeActionType::New => gpodder::EpisodeActionType::New,
|
|
||||||
EpisodeActionType::Delete => gpodder::EpisodeActionType::Delete,
|
|
||||||
EpisodeActionType::Download => gpodder::EpisodeActionType::Download,
|
|
||||||
EpisodeActionType::Play {
|
|
||||||
started,
|
|
||||||
position,
|
|
||||||
total,
|
|
||||||
} => gpodder::EpisodeActionType::Play {
|
|
||||||
started,
|
|
||||||
position,
|
|
||||||
total,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<gpodder::EpisodeAction> for EpisodeAction {
|
|
||||||
fn from(value: gpodder::EpisodeAction) -> Self {
|
|
||||||
Self {
|
|
||||||
podcast: value.podcast,
|
|
||||||
episode: value.episode,
|
|
||||||
timestamp: value.timestamp.map(|ts| ts.timestamp()),
|
|
||||||
device: value.device,
|
|
||||||
action: value.action.into(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<EpisodeAction> for gpodder::EpisodeAction {
|
|
||||||
fn from(value: EpisodeAction) -> Self {
|
|
||||||
Self {
|
|
||||||
podcast: value.podcast,
|
|
||||||
episode: value.episode,
|
|
||||||
// TODO remove this unwrap
|
|
||||||
timestamp: value
|
|
||||||
.timestamp
|
|
||||||
.map(|ts| DateTime::from_timestamp(ts, 0).unwrap()),
|
|
||||||
device: value.device,
|
|
||||||
action: value.action.into(),
|
|
||||||
time_changed: DateTime::<Utc>::MIN_UTC,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ use axum::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
gpodder,
|
gpodder::{self, SubscriptionRepository},
|
||||||
server::{
|
server::{
|
||||||
error::{AppError, AppResult},
|
error::{AppError, AppResult},
|
||||||
gpodder::{auth_middleware, format::StringWithFormat},
|
gpodder::{auth_middleware, format::StringWithFormat},
|
||||||
|
|
@ -34,10 +34,10 @@ pub async fn get_device_subscriptions(
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(
|
Ok(
|
||||||
tokio::task::spawn_blocking(move || ctx.store.subscriptions_for_device(&user, &id))
|
tokio::task::spawn_blocking(move || ctx.repo.subscriptions_for_device(&user, &id))
|
||||||
.await
|
.await
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.map(|subs| Json(subs.into_iter().map(|s| s.url).collect()))?,
|
.map(Json)?,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -51,10 +51,10 @@ pub async fn get_user_subscriptions(
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(
|
Ok(
|
||||||
tokio::task::spawn_blocking(move || ctx.store.subscriptions_for_user(&user))
|
tokio::task::spawn_blocking(move || ctx.repo.subscriptions_for_user(&user))
|
||||||
.await
|
.await
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.map(|subs| Json(subs.into_iter().map(|s| s.url).collect()))?,
|
.map(Json)?,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -68,10 +68,12 @@ pub async fn put_device_subscriptions(
|
||||||
return Err(AppError::BadRequest);
|
return Err(AppError::BadRequest);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(tokio::task::spawn_blocking(move || {
|
Ok(
|
||||||
ctx.store.set_subscriptions_for_device(&user, &id, urls)
|
tokio::task::spawn_blocking(move || {
|
||||||
|
ctx.repo.set_subscriptions_for_device(&user, &id, urls)
|
||||||
})
|
})
|
||||||
.await
|
.await
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.map(|_| ())?)
|
.map(|_| ())?,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,28 +1,17 @@
|
||||||
mod error;
|
mod error;
|
||||||
mod gpodder;
|
mod gpodder;
|
||||||
|
|
||||||
use axum::{extract::Request, middleware::Next, response::Response, Router};
|
use axum::Router;
|
||||||
use tower_http::trace::TraceLayer;
|
use tower_http::trace::TraceLayer;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Context {
|
pub struct Context {
|
||||||
pub store: crate::gpodder::GpodderRepository,
|
pub repo: crate::db::SqliteRepository,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn app(ctx: Context) -> Router {
|
pub fn app(ctx: Context) -> Router {
|
||||||
Router::new()
|
Router::new()
|
||||||
.merge(gpodder::router(ctx.clone()))
|
.merge(gpodder::router(ctx.clone()))
|
||||||
.layer(axum::middleware::from_fn(header_logger))
|
|
||||||
.layer(TraceLayer::new_for_http())
|
.layer(TraceLayer::new_for_http())
|
||||||
.with_state(ctx)
|
.with_state(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn header_logger(request: Request, next: Next) -> Response {
|
|
||||||
tracing::debug!("request headers = {:?}", request.headers());
|
|
||||||
|
|
||||||
let res = next.run(request).await;
|
|
||||||
|
|
||||||
tracing::debug!("response headers = {:?}", res.headers());
|
|
||||||
|
|
||||||
res
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue