codeberg_cli/actions/notification/
list.rs

1use crate::render::json::JsonToStdout;
2use crate::types::context::BergContext;
3use crate::types::output::OutputMode;
4use crate::{actions::GeneralArgs, render::option::option_display};
5use forgejo_api::structs::{NotificationThread, NotifyGetListQuery};
6
7use clap::Parser;
8
9#[derive(Debug, Parser)]
10pub struct ListNotificationArgs {
11    /// just list everything
12    #[arg(short, long, default_value_t = false)]
13    pub all: bool,
14
15    /// filter notifications by status
16    #[arg(long, default_values_t = ["Unread", "Pinned"].map(String::from).to_vec())]
17    pub status_types: Vec<String>,
18
19    /// filter notifications by subject type
20    #[arg(long)]
21    pub subject_type: Option<String>,
22
23    /// filter notifications by date
24    #[arg(short, long)]
25    pub dates: bool,
26
27    /// control how many pages of notifications should be shown
28    #[arg(short, long, default_value_t = 1)]
29    pub page: usize,
30
31    /// control how many notifications each page should hold
32    #[arg(short, long, default_value_t = usize::MAX)]
33    pub limit: usize,
34}
35
36impl ListNotificationArgs {
37    pub async fn run(self, general_args: GeneralArgs) -> anyhow::Result<()> {
38        let ctx = BergContext::new(self, general_args).await?;
39
40        let (_, notification_threads_list) = ctx
41            .client
42            .notify_get_list(NotifyGetListQuery {
43                ..Default::default()
44            })
45            .await?;
46
47        match general_args.output_mode {
48            OutputMode::Pretty => {
49                tracing::debug!("{notification_threads_list:?}");
50                present_notification_threads(&ctx, notification_threads_list);
51            }
52            OutputMode::Json => notification_threads_list.print_json()?,
53        }
54
55        Ok(())
56    }
57}
58
59fn present_notification_threads(
60    ctx: &BergContext<ListNotificationArgs>,
61    notification_threads_list: Vec<NotificationThread>,
62) {
63    let header = if notification_threads_list.is_empty() {
64        "Notification Threads (empty)"
65    } else {
66        "Notification Threads"
67    };
68
69    let mut table = ctx.make_table();
70
71    table
72        .set_header(vec![header])
73        .add_rows(
74            notification_threads_list
75                .into_iter()
76                .map(|notification_thread| {
77                    vec![option_display(
78                        &notification_thread
79                            .subject
80                            .as_ref()
81                            .and_then(|subject| subject.title.as_ref()),
82                    )]
83                }),
84        );
85
86    println!("{table}", table = table.show());
87}