codeberg_cli/actions/notification/
view.rs

1use crate::render::option::{option_debug_display, option_display};
2use crate::render::ui::fuzzy_select_with_key;
3use crate::types::context::BergContext;
4use crate::{actions::GeneralArgs, render::datetime::render_datetime_and_info};
5use anyhow::Context;
6use forgejo_api::structs::{NotificationThread, NotifyGetListQuery};
7
8use clap::Parser;
9#[derive(Debug, Parser)]
10pub struct ViewNotificationArgs {
11    /// view specific notification if available
12    pub id: Option<i64>,
13
14    /// show everything about the notification if available
15    #[arg(short, long)]
16    pub all: bool,
17}
18
19impl ViewNotificationArgs {
20    pub async fn run(self, general_args: GeneralArgs) -> anyhow::Result<()> {
21        let _ = general_args;
22        let ctx = BergContext::new(self, general_args).await?;
23
24        let thread_id = if let Some(id) = ctx.args.id {
25            id
26        } else {
27            fuzzy_select_with_key(
28                &ctx.client
29                    .notify_get_list(NotifyGetListQuery {
30                        ..Default::default()
31                    })
32                    .await?
33                    .iter()
34                    .filter(|thread| thread.id.is_some())
35                    .collect::<Vec<_>>(),
36                "notification thread",
37                |thread| {
38                    option_display(
39                        &thread
40                            .subject
41                            .as_ref()
42                            .and_then(|subject| subject.title.as_ref()),
43                    )
44                },
45            )
46            .and_then(|thread| thread.id.context("No ID on selected thread"))?
47        };
48
49        let selected_notification_thread = ctx
50            .client
51            .notify_get_thread(thread_id.to_string().as_str())
52            .await?;
53
54        present_notification_thread_details(&ctx, selected_notification_thread);
55
56        Ok(())
57    }
58}
59
60fn present_notification_thread_details(
61    ctx: &BergContext<ViewNotificationArgs>,
62    notification_thread: NotificationThread,
63) {
64    let mut table = ctx.make_table();
65
66    table
67        .add_row(vec![
68            String::from("Title"),
69            option_display(
70                &notification_thread
71                    .subject
72                    .as_ref()
73                    .and_then(|subject| subject.title.as_ref()),
74            ),
75        ])
76        .add_row(vec![
77            String::from("URL"),
78            option_display(
79                &notification_thread
80                    .subject
81                    .as_ref()
82                    .and_then(|subject| subject.html_url.as_ref()),
83            ),
84        ])
85        .add_row(vec![
86            String::from("Type"),
87            option_display(
88                &notification_thread
89                    .subject
90                    .as_ref()
91                    .and_then(|subject| subject.r#type.as_ref()),
92            ),
93        ])
94        .add_row(vec![
95            String::from("State"),
96            option_debug_display(
97                &notification_thread
98                    .subject
99                    .as_ref()
100                    .and_then(|subject| subject.state.as_ref()),
101            ),
102        ])
103        .add_row(vec![
104            "Unread",
105            if notification_thread.unread.is_some_and(|is_true| is_true) {
106                "Yes"
107            } else {
108                "No"
109            },
110        ])
111        .add_row(vec![
112            "Pinned",
113            if notification_thread.pinned.is_some_and(|is_true| is_true) {
114                "Yes"
115            } else {
116                "No"
117            },
118        ])
119        .add_row(vec![
120            String::from("Last updated"),
121            option_display(
122                &notification_thread
123                    .updated_at
124                    .as_ref()
125                    .map(render_datetime_and_info),
126            ),
127        ]);
128
129    println!("{table}", table = table.show());
130}