refactor: moved knowledge of subscription change time to store

This commit is contained in:
Jef Roosens 2025-03-15 20:20:38 +01:00
parent 8a9744c4a9
commit 330877c8c5
Signed by: Jef Roosens
GPG key ID: 21FD3D77D56BAF49
6 changed files with 72 additions and 25 deletions

View file

@ -1,5 +1,6 @@
use std::collections::HashSet;
use chrono::DateTime;
use diesel::prelude::*;
use super::SqliteRepository;
@ -8,24 +9,39 @@ use crate::{
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 {
fn subscriptions_for_user(
&self,
user: &gpodder::User,
) -> Result<Vec<String>, gpodder::AuthErr> {
) -> Result<Vec<gpodder::Subscription>, gpodder::AuthErr> {
Ok(device_subscriptions::table
.inner_join(devices::table)
.filter(devices::user_id.eq(user.id))
.select(device_subscriptions::podcast_url)
.select((
device_subscriptions::podcast_url,
device_subscriptions::time_changed,
))
.distinct()
.get_results(&mut self.pool.get()?)?)
.get_results::<(String, i64)>(&mut self.pool.get()?)?
.into_iter()
.map(Into::into)
.collect())
}
fn subscriptions_for_device(
&self,
user: &gpodder::User,
device_id: &str,
) -> Result<Vec<String>, gpodder::AuthErr> {
) -> Result<Vec<gpodder::Subscription>, gpodder::AuthErr> {
Ok(device_subscriptions::table
.inner_join(devices::table)
.filter(
@ -33,8 +49,14 @@ impl gpodder::SubscriptionRepository for SqliteRepository {
.eq(user.id)
.and(devices::device_id.eq(device_id)),
)
.select(device_subscriptions::podcast_url)
.get_results(&mut self.pool.get()?)?)
.select((
device_subscriptions::podcast_url,
device_subscriptions::time_changed,
))
.get_results::<(String, i64)>(&mut self.pool.get()?)?
.into_iter()
.map(Into::into)
.collect())
}
fn set_subscriptions_for_device(
@ -230,7 +252,7 @@ impl gpodder::SubscriptionRepository for SqliteRepository {
user: &gpodder::User,
device_id: &str,
since: chrono::DateTime<chrono::Utc>,
) -> Result<(chrono::DateTime<chrono::Utc>, Vec<String>, Vec<String>), gpodder::AuthErr> {
) -> Result<(Vec<gpodder::Subscription>, Vec<gpodder::Subscription>), gpodder::AuthErr> {
let since = since.timestamp();
let (mut added, mut removed) = (Vec::new(), Vec::new());
@ -245,20 +267,22 @@ impl gpodder::SubscriptionRepository for SqliteRepository {
)
.select(db::DeviceSubscription::as_select());
let mut max_time: chrono::DateTime<chrono::Utc> = chrono::DateTime::<chrono::Utc>::MIN_UTC;
for sub in query.load_iter(&mut self.pool.get()?)? {
let sub = sub?;
if sub.deleted {
removed.push(sub.podcast_url);
removed.push(gpodder::Subscription {
url: sub.podcast_url,
time_changed: DateTime::from_timestamp(sub.time_changed, 0).unwrap(),
});
} else {
added.push(sub.podcast_url);
added.push(gpodder::Subscription {
url: sub.podcast_url,
time_changed: DateTime::from_timestamp(sub.time_changed, 0).unwrap(),
});
}
max_time = max_time.max(chrono::DateTime::from_timestamp(sub.time_changed, 0).unwrap());
}
Ok((max_time, added, removed))
Ok((added, removed))
}
}