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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
use crate::render::option::option_display;
use crate::render::ui::fuzzy_select_with_key;
use crate::types::context::BergContext;
use crate::{actions::GeneralArgs, render::datetime::render_datetime_and_info};
use anyhow::Context;
use forgejo_api::structs::{NotificationThread, NotifyGetListQuery};

use clap::Parser;
#[derive(Debug, Parser)]
pub struct ViewNotificationArgs {
    /// view specific notification if available
    pub id: Option<u64>,

    /// show everything about the notification if available
    #[arg(short, long)]
    pub all: bool,
}

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

        let thread_id = if let Some(id) = ctx.args.id {
            id
        } else {
            fuzzy_select_with_key(
                &ctx.client
                    .notify_get_list(NotifyGetListQuery {
                        ..Default::default()
                    })
                    .await?
                    .iter()
                    .filter(|thread| thread.id.is_some())
                    .collect::<Vec<_>>(),
                "notification thread",
                |thread| {
                    option_display(
                        &thread
                            .subject
                            .as_ref()
                            .and_then(|subject| subject.title.as_ref()),
                    )
                },
            )
            .and_then(|thread| thread.id.context("No ID on selected thread"))?
        };

        let selected_notification_thread = ctx
            .client
            .notify_get_thread(thread_id.to_string().as_str())
            .await?;

        present_notification_thread_details(&ctx, selected_notification_thread);

        Ok(())
    }
}

fn present_notification_thread_details(
    ctx: &BergContext<ViewNotificationArgs>,
    notification_thread: NotificationThread,
) {
    let mut table = ctx.make_table();

    table
        .add_row(vec![
            String::from("Title"),
            option_display(
                &notification_thread
                    .subject
                    .as_ref()
                    .and_then(|subject| subject.title.as_ref()),
            ),
        ])
        .add_row(vec![
            String::from("URL"),
            option_display(
                &notification_thread
                    .subject
                    .as_ref()
                    .and_then(|subject| subject.html_url.as_ref()),
            ),
        ])
        .add_row(vec![
            String::from("Type"),
            option_display(
                &notification_thread
                    .subject
                    .as_ref()
                    .and_then(|subject| subject.r#type.as_ref()),
            ),
        ])
        .add_row(vec![
            String::from("State"),
            option_display(
                &notification_thread
                    .subject
                    .as_ref()
                    .and_then(|subject| subject.state.as_ref()),
            ),
        ])
        .add_row(vec![
            "Unread",
            if notification_thread.unread.is_some_and(|is_true| is_true) {
                "Yes"
            } else {
                "No"
            },
        ])
        .add_row(vec![
            "Pinned",
            if notification_thread.pinned.is_some_and(|is_true| is_true) {
                "Yes"
            } else {
                "No"
            },
        ])
        .add_row(vec![
            String::from("Last updated"),
            option_display(
                &notification_thread
                    .updated_at
                    .as_ref()
                    .map(render_datetime_and_info),
            ),
        ]);

    println!("{table}");
}