summaryrefslogtreecommitdiffstats
path: root/src/usecase/subscriptions.rs
blob: a37e65034c4b3c8170c4a52da413a8c51451be81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//! 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<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)
}