diff options
Diffstat (limited to 'src/usecase/subscriptions.rs')
| -rw-r--r-- | src/usecase/subscriptions.rs | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/usecase/subscriptions.rs b/src/usecase/subscriptions.rs new file mode 100644 index 0000000..c504cfe --- /dev/null +++ b/src/usecase/subscriptions.rs @@ -0,0 +1,50 @@ +//! 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. +/// +/// The repo row has to be resolved first, which is the step every front end +/// previously open-coded — and the web layer got it wrong, falling back to repo +/// id 1 rather than saying the repository was not initialised. +pub fn subscribe(db: &Db, pkgbase: &str, url: Option<String>) -> Result<Subscribe> { + 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<Pause> { + 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) +} |
