//! What the server watches, and whether it is allowed to. use crate::db::{self, Db, SubscriptionState}; use crate::error::Result; /// A subscription that now exists. pub struct Subscribe { pub pkgbase: String, pub url: String, } /// Start watching a pkgbase, resolving the repo row first. pub fn subscribe(db: &Db, pkgbase: &str, url: Option) -> Result { let rid = db::repo_id(db)?; let url = url.unwrap_or_else(|| format!("https://aur.archlinux.org/{pkgbase}.git")); db::subscribe(&db.writer, rid, pkgbase, &url)?; Ok(Subscribe { pkgbase: pkgbase.to_string(), url }) } pub enum Pause { /// Paused, or resumed if it already was. Now(SubscriptionState), /// Some other state — removed, say — where pausing means nothing. NotApplicable(SubscriptionState), /// Nothing is subscribed under that name. Unknown, } /// Pause a subscription, or resume it if it is already paused. pub fn toggle_pause(db: &Db, pkgbase: &str) -> Result { let Some(current) = db::get_subscription(db, pkgbase)? else { return Ok(Pause::Unknown); }; let next = match current.state { SubscriptionState::Active => SubscriptionState::Paused, SubscriptionState::Paused => SubscriptionState::Active, other => return Ok(Pause::NotApplicable(other)), }; db::set_subscription_state(&db.writer, pkgbase, next)?; Ok(Pause::Now(next)) } /// Stop watching a pkgbase. The row stays: history outlives subscriptions. pub fn unsubscribe(db: &Db, pkgbase: &str) -> Result<()> { db::set_subscription_state(&db.writer, pkgbase, SubscriptionState::Removed) }