refactor: migrate subscriptions API to store

This commit is contained in:
Jef Roosens 2025-03-15 19:19:18 +01:00
parent 6bb3e8a27f
commit dd14a2152f
Signed by: Jef Roosens
GPG key ID: 21FD3D77D56BAF49
5 changed files with 101 additions and 36 deletions

View file

@ -42,9 +42,10 @@ impl gpodder::SubscriptionRepository for SqliteRepository {
user: &gpodder::User,
device_id: &str,
urls: Vec<String>,
) -> Result<i64, gpodder::AuthErr> {
time_changed: chrono::DateTime<chrono::Utc>,
) -> Result<(), gpodder::AuthErr> {
// TODO use a better timestamp
let timestamp = chrono::Utc::now().timestamp();
let timestamp = time_changed.timestamp();
self.pool.get()?.transaction(|conn| {
let device = devices::table
@ -126,7 +127,7 @@ impl gpodder::SubscriptionRepository for SqliteRepository {
Ok::<_, diesel::result::Error>(())
})?;
Ok(timestamp + 1)
Ok(())
}
fn update_subscriptions_for_device(
@ -135,9 +136,10 @@ impl gpodder::SubscriptionRepository for SqliteRepository {
device_id: &str,
add: Vec<String>,
remove: Vec<String>,
) -> Result<i64, gpodder::AuthErr> {
time_changed: chrono::DateTime<chrono::Utc>,
) -> Result<(), gpodder::AuthErr> {
// TODO use a better timestamp
let timestamp = chrono::Utc::now().timestamp_millis();
let timestamp = time_changed.timestamp();
// 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.
@ -220,16 +222,18 @@ impl gpodder::SubscriptionRepository for SqliteRepository {
Ok::<_, diesel::result::Error>(())
})?;
Ok(timestamp + 1)
Ok(())
}
fn subscription_updates_for_device(
&self,
user: &gpodder::User,
device_id: &str,
since: i64,
) -> Result<(i64, Vec<String>, Vec<String>), gpodder::AuthErr> {
let (mut timestamp, mut added, mut removed) = (0, Vec::new(), Vec::new());
since: chrono::DateTime<chrono::Utc>,
) -> Result<(chrono::DateTime<chrono::Utc>, Vec<String>, Vec<String>), gpodder::AuthErr> {
let since = since.timestamp();
let (mut added, mut removed) = (Vec::new(), Vec::new());
let query = device_subscriptions::table
.inner_join(devices::table)
@ -241,6 +245,8 @@ 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?;
@ -250,9 +256,9 @@ impl gpodder::SubscriptionRepository for SqliteRepository {
added.push(sub.podcast_url);
}
timestamp = timestamp.max(sub.time_changed);
max_time = max_time.max(chrono::DateTime::from_timestamp(sub.time_changed, 0).unwrap());
}
Ok((timestamp + 1, added, removed))
Ok((max_time, added, removed))
}
}