codeberg_cli/actions/notification/
list.rs

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