feat: implement change timestamp for subscriptions set

episode-actions
Jef Roosens 2025-02-24 22:04:47 +01:00
parent 2f0fe08f4c
commit 6d439783b5
No known key found for this signature in database
GPG Key ID: 21FD3D77D56BAF49
7 changed files with 243 additions and 7 deletions

151
Cargo.lock generated
View File

@ -17,6 +17,21 @@ version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627"
[[package]]
name = "android-tzdata"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0"
[[package]]
name = "android_system_properties"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311"
dependencies = [
"libc",
]
[[package]]
name = "anstream"
version = "0.6.18"
@ -213,6 +228,12 @@ dependencies = [
"generic-array",
]
[[package]]
name = "bumpalo"
version = "3.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1628fb46dfa0b37568d12e5edd512553eccf6a22a78e8bde00bb4aed84d5bdbf"
[[package]]
name = "byteorder"
version = "1.5.0"
@ -240,6 +261,20 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "chrono"
version = "0.4.39"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e36cc9d416881d2e24f9a963be5fb1cd90966419ac844274161d10488b3e825"
dependencies = [
"android-tzdata",
"iana-time-zone",
"js-sys",
"num-traits",
"wasm-bindgen",
"windows-targets",
]
[[package]]
name = "clap"
version = "4.5.30"
@ -297,6 +332,12 @@ dependencies = [
"version_check",
]
[[package]]
name = "core-foundation-sys"
version = "0.8.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b"
[[package]]
name = "cpufeatures"
version = "0.2.17"
@ -634,6 +675,29 @@ dependencies = [
"tower-service",
]
[[package]]
name = "iana-time-zone"
version = "0.1.61"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "235e081f3925a06703c2d0117ea8b91f042756fd6e7a6e5d901e8ca1a996b220"
dependencies = [
"android_system_properties",
"core-foundation-sys",
"iana-time-zone-haiku",
"js-sys",
"wasm-bindgen",
"windows-core",
]
[[package]]
name = "iana-time-zone-haiku"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f"
dependencies = [
"cc",
]
[[package]]
name = "ident_case"
version = "1.0.1"
@ -662,6 +726,16 @@ version = "1.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674"
[[package]]
name = "js-sys"
version = "0.3.77"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1cfaf33c695fc6e08064efbc1f72ec937429614f25eef83af942d0e227c3a28f"
dependencies = [
"once_cell",
"wasm-bindgen",
]
[[package]]
name = "lazy_static"
version = "1.5.0"
@ -776,6 +850,15 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9"
[[package]]
name = "num-traits"
version = "0.2.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
dependencies = [
"autocfg",
]
[[package]]
name = "object"
version = "0.36.7"
@ -798,6 +881,7 @@ dependencies = [
"argon2",
"axum",
"axum-extra",
"chrono",
"clap",
"diesel",
"diesel_migrations",
@ -1381,6 +1465,64 @@ version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "wasm-bindgen"
version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1edc8929d7499fc4e8f0be2262a241556cfc54a0bea223790e71446f2aab1ef5"
dependencies = [
"cfg-if",
"once_cell",
"rustversion",
"wasm-bindgen-macro",
]
[[package]]
name = "wasm-bindgen-backend"
version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2f0a0651a5c2bc21487bde11ee802ccaf4c51935d0d3d42a6101f98161700bc6"
dependencies = [
"bumpalo",
"log",
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fe63fc6d09ed3792bd0897b314f53de8e16568c2b3f7982f468c0bf9bd0b407"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
]
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ae87ea40c9f689fc23f209965b6fb8a99ad69aeeb0231408be24920604395de"
dependencies = [
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a05d73b933a847d6cccdda8f838a22ff101ad9bf93e33684f39c1f5f0eece3d"
dependencies = [
"unicode-ident",
]
[[package]]
name = "winapi"
version = "0.3.9"
@ -1403,6 +1545,15 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "windows-core"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-sys"
version = "0.52.0"

View File

@ -7,6 +7,7 @@ edition = "2021"
argon2 = "0.5.3"
axum = { version = "0.8.1" }
axum-extra = { version = "0.10", features = ["cookie", "typed-header"] }
chrono = "0.4.39"
clap = { version = "4.5.30", features = ["derive", "env"] }
diesel = { version = "2.2.7", features = ["r2d2", "sqlite", "returning_clauses_for_sqlite_3_35"] }
diesel_migrations = { version = "2.2.0", features = ["sqlite"] }

View File

@ -0,0 +1,6 @@
-- This file should undo anything in `up.sql`
alter table subscriptions
drop column time_changed;
alter table subscriptions
drop column deleted;

View File

@ -0,0 +1,6 @@
-- Your SQL goes here
alter table subscriptions
add column time_changed bigint not null default 0;
alter table subscriptions
add column deleted boolean not null default false;

View File

@ -1,3 +1,5 @@
use std::collections::HashSet;
use diesel::prelude::*;
use serde::{Deserialize, Serialize};
@ -10,6 +12,8 @@ pub struct Subscription {
pub id: i64,
pub device_id: i64,
pub url: String,
pub time_changed: i64,
pub deleted: bool,
}
#[derive(Deserialize, Insertable)]
@ -18,6 +22,8 @@ pub struct Subscription {
pub struct NewSubscription {
pub device_id: i64,
pub url: String,
pub time_changed: i64,
pub deleted: bool,
}
impl Subscription {
@ -37,15 +43,76 @@ impl Subscription {
.get_results(&mut pool.get()?)?)
}
pub fn update_for_device(pool: &DbPool, device_id: i64, urls: Vec<String>) -> DbResult<()> {
pub fn set_for_device(
pool: &DbPool,
device_id: i64,
urls: Vec<String>,
timestamp: i64,
) -> DbResult<()> {
pool.get()?.transaction(|conn| {
diesel::delete(subscriptions::table.filter(subscriptions::device_id.eq(device_id)))
.execute(conn)?;
// https://github.com/diesel-rs/diesel/discussions/2826
// SQLite doesn't support default on conflict set values, so we can't handle this using
// on conflict. Therefore, we instead calculate which URLs should be inserted and which
// updated, so we avoid conflicts.
let urls: HashSet<String> = urls.into_iter().collect();
let urls_in_db: HashSet<String> = subscriptions::table
.select(subscriptions::url)
.filter(subscriptions::device_id.eq(device_id))
.get_results(&mut pool.get()?)?
.into_iter()
.collect();
// URLs originally in the database that are no longer in the list
let urls_to_delete = urls_in_db.difference(&urls);
// URLs not in the database that are in the new list
let urls_to_insert = urls.difference(&urls_in_db);
// URLs that are in both the database and the new list. For these, those marked as
// "deleted" in the database are updated so they're no longer deleted, with their
// timestamp updated.
let urls_to_update = urls.intersection(&urls_in_db);
// Mark the URLs to delete as properly deleted
diesel::update(
subscriptions::table.filter(
subscriptions::device_id
.eq(device_id)
.and(subscriptions::url.eq_any(urls_to_delete)),
),
)
.set((
subscriptions::deleted.eq(true),
subscriptions::time_changed.eq(timestamp),
))
.execute(conn)?;
// Update the existing deleted URLs that are reinserted as no longer deleted
diesel::update(
subscriptions::table.filter(
subscriptions::device_id
.eq(device_id)
.and(subscriptions::url.eq_any(urls_to_update))
.and(subscriptions::deleted.eq(true)),
),
)
.set((
subscriptions::deleted.eq(false),
subscriptions::time_changed.eq(timestamp),
))
.execute(conn)?;
// Insert the new values into the database
diesel::insert_into(subscriptions::table)
.values(
urls.into_iter()
.map(|url| NewSubscription { device_id, url })
urls_to_insert
.into_iter()
.map(|url| NewSubscription {
device_id,
url: url.to_string(),
deleted: false,
time_changed: timestamp,
})
.collect::<Vec<_>>(),
)
.execute(conn)?;

View File

@ -23,6 +23,8 @@ diesel::table! {
id -> BigInt,
device_id -> BigInt,
url -> Text,
time_changed -> BigInt,
deleted -> Bool,
}
}

View File

@ -85,8 +85,11 @@ pub async fn put_device_subscriptions(
.insert(&ctx.pool)?
};
Ok::<_, AppError>(db::Subscription::update_for_device(
&ctx.pool, device.id, urls,
Ok::<_, AppError>(db::Subscription::set_for_device(
&ctx.pool,
device.id,
urls,
chrono::Utc::now().timestamp(),
)?)
})
.await