summaryrefslogtreecommitdiffstats
path: root/src/db/subscriptions.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/db/subscriptions.rs')
-rw-r--r--src/db/subscriptions.rs122
1 files changed, 122 insertions, 0 deletions
diff --git a/src/db/subscriptions.rs b/src/db/subscriptions.rs
new file mode 100644
index 0000000..947a205
--- /dev/null
+++ b/src/db/subscriptions.rs
@@ -0,0 +1,122 @@
+use rusqlite::{params, OptionalExtension, Row};
+
+use crate::db::*;
+use crate::error::Result;
+use crate::time;
+
+// ------------------------------------------------------------- subscriptions --
+
+#[derive(Debug, Clone)]
+pub struct Subscription {
+ pub id: i64,
+ pub pkgbase: String,
+ pub aur_url: String,
+ pub maintainer: Option<String>,
+ pub state: SubscriptionState,
+ pub next_check_at: Option<String>,
+ pub last_checked_at: Option<String>,
+ pub last_error: Option<String>,
+ pub poll_interval_secs: Option<i64>,
+}
+
+fn row_to_sub(r: &Row<'_>) -> rusqlite::Result<Subscription> {
+ Ok(Subscription {
+ id: r.get(0)?,
+ pkgbase: r.get(1)?,
+ aur_url: r.get(2)?,
+ maintainer: r.get(3)?,
+ state: crate::db::state_col::<SubscriptionState>(4, &r.get::<_, String>(4)?)?,
+ next_check_at: r.get(5)?,
+ last_checked_at: r.get(6)?,
+ last_error: r.get(7)?,
+ poll_interval_secs: r.get(8)?,
+ })
+}
+
+const SUB_COLS: &str = "id, pkgbase, aur_url, maintainer, state, next_check_at, last_checked_at, \
+ last_error, poll_interval_secs";
+
+pub fn list_subscriptions(db: &Db) -> Result<Vec<Subscription>> {
+ db.read(|c| {
+ let mut stmt = c.prepare(&format!("SELECT {SUB_COLS} FROM subscription ORDER BY pkgbase"))?;
+ let rows = stmt.query_map([], row_to_sub)?;
+ let mut out = Vec::new();
+ for r in rows {
+ out.push(r?);
+ }
+ Ok(out)
+ })
+}
+
+pub fn get_subscription(db: &Db, pkgbase: &str) -> Result<Option<Subscription>> {
+ db.read(|c| {
+ c.query_row(
+ &format!("SELECT {SUB_COLS} FROM subscription WHERE pkgbase = ?1"),
+ params![pkgbase],
+ row_to_sub,
+ )
+ .optional()
+ .map_err(Into::into)
+ })
+}
+
+pub fn subscribe(writer: &Writer, repo_id: i64, pkgbase: &str, aur_url: &str) -> Result<i64> {
+ let (p, u) = (pkgbase.to_string(), aur_url.to_string());
+ let at = time::now();
+ writer.tx(move |tx| {
+ tx.execute(
+ "INSERT INTO pkgbase (name, aur_url, created_at) VALUES (?1,?2,?3)
+ ON CONFLICT(name) DO UPDATE SET aur_url = excluded.aur_url",
+ params![p, u, at],
+ )?;
+ tx.execute(
+ "INSERT INTO subscription (repo_id, pkgbase, aur_url, state, next_check_at, created_at)
+ VALUES (?1,?2,?3,'active',?4,?5)
+ ON CONFLICT(repo_id, pkgbase) DO UPDATE SET state='active', aur_url=excluded.aur_url",
+ params![repo_id, p, u, at.clone(), at],
+ )?;
+ Ok(1)
+ })
+}
+
+pub fn set_subscription_state(writer: &Writer, pkgbase: &str, state: SubscriptionState) -> Result<()> {
+ let (p, s) = (pkgbase.to_string(), state.to_string());
+ writer.tx(move |tx| {
+ tx.execute(
+ "UPDATE subscription SET state=?1 WHERE pkgbase=?2",
+ params![s, p],
+ )?;
+ Ok(())
+ })
+}
+
+pub fn note_check(writer: &Writer, pkgbase: &str, next_check_at: &str, err: Option<&str>) -> Result<()> {
+ let (p, n) = (pkgbase.to_string(), next_check_at.to_string());
+ let e = err.map(|s| s.to_string());
+ let at = time::now();
+ writer.tx(move |tx| {
+ tx.execute(
+ "UPDATE subscription SET last_checked_at=?1, next_check_at=?2, last_error=?3
+ WHERE pkgbase=?4",
+ params![at, n, e, p],
+ )?;
+ Ok(())
+ })
+}
+
+pub fn due_subscriptions(db: &Db, now: &str) -> Result<Vec<Subscription>> {
+ db.read(|c| {
+ let mut stmt = c.prepare(&format!(
+ "SELECT {SUB_COLS} FROM subscription
+ WHERE state IN ('active','orphaned') AND (next_check_at IS NULL OR next_check_at <= ?1)
+ ORDER BY next_check_at IS NULL DESC, next_check_at LIMIT 200"
+ ))?;
+ let rows = stmt.query_map(params![now], row_to_sub)?;
+ let mut out = Vec::new();
+ for r in rows {
+ out.push(r?);
+ }
+ Ok(out)
+ })
+}
+