hyperhive/hive-forge/src/verbs/subscription.rs

56 lines
1.8 KiB
Rust

//! `subscription [--watch|--ignore|--unwatch] [repo]` — get or set
//! the current user's watch subscription for a repo.
use anyhow::Result;
use clap::Args as ClapArgs;
use serde_json::json;
use crate::client::Client;
use crate::verbs::print_json;
#[derive(ClapArgs)]
pub struct Args {
/// Subscribe (watch the repo).
#[arg(long, group = "action")]
watch: bool,
/// Mute (mark ignored).
#[arg(long, group = "action")]
ignore: bool,
/// Unsubscribe (clear watch + ignore).
#[arg(long, group = "action")]
unwatch: bool,
}
pub fn run(client: &Client, args: Args) -> Result<()> {
let repo = client.repo();
if args.unwatch {
client.delete(&format!("/repos/{repo}/subscription"), None)?;
println!("unsubscribed");
return Ok(());
}
if args.watch || args.ignore {
let (subscribed, ignored) = if args.ignore {
(false, true)
} else {
(true, false)
};
// Forgejo requires PUT (not POST) for this endpoint.
let resp = client.put_json(
&format!("/repos/{repo}/subscription"),
&json!({ "subscribed": subscribed, "ignored": ignored }),
)?;
return print_json(&json!({
"subscribed": resp.get("subscribed"),
"ignored": resp.get("ignored"),
}));
}
// Forgejo returns 404 when the current user is not watching the repo
// (rather than a response with subscribed=false).
match client.get_json_optional(&format!("/repos/{repo}/subscription"))? {
Some(resp) => print_json(&json!({
"subscribed": resp.get("subscribed"),
"ignored": resp.get("ignored"),
})),
None => print_json(&json!({ "subscribed": false, "ignored": false })),
}
}