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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use crate::types::context::BergContext;
use crate::{actions::GeneralArgs, render::option::option_display};
use forgejo_api::structs::{NotificationThread, NotifyGetListQuery};

use clap::Parser;

#[derive(Debug, Parser)]
pub struct ListNotificationArgs {
    /// just list everything
    #[arg(short, long, default_value_t = false)]
    pub all: bool,

    /// filter notifications by status
    #[arg(long, default_values_t = ["Unread", "Pinned"].map(String::from).to_vec())]
    pub status_types: Vec<String>,

    /// filter notifications by subject type
    #[arg(long)]
    pub subject_type: Option<String>,

    /// filter notifications by date
    #[arg(short, long)]
    pub dates: bool,

    /// control how many pages of notifications should be shown
    #[arg(short, long, default_value_t = 1)]
    pub page: usize,

    /// control how many notifications each page should hold
    #[arg(short, long, default_value_t = usize::MAX)]
    pub limit: usize,
}

impl ListNotificationArgs {
    pub async fn run(self, general_args: GeneralArgs) -> anyhow::Result<()> {
        let _ = general_args;
        let ctx = BergContext::new(self).await?;

        let notification_threads_list = ctx
            .client
            .notify_get_list(NotifyGetListQuery {
                ..Default::default()
            })
            .await?;

        tracing::debug!("{notification_threads_list:?}");

        present_notification_threads(&ctx, notification_threads_list);

        Ok(())
    }
}

fn present_notification_threads(
    ctx: &BergContext<ListNotificationArgs>,
    notification_threads_list: Vec<NotificationThread>,
) {
    let header = if notification_threads_list.is_empty() {
        "Notification Threads (empty)"
    } else {
        "Notification Threads"
    };

    let mut table = ctx.make_table();

    table
        .set_header(vec![header])
        .add_rows(
            notification_threads_list
                .into_iter()
                .map(|notification_thread| {
                    vec![option_display(
                        &notification_thread
                            .subject
                            .as_ref()
                            .and_then(|subject| subject.title.as_ref()),
                    )]
                }),
        );

    println!("{table}");
}