mcaptcha/pages/panel/
notifications.rs1use actix_identity::Identity;
7use actix_web::{HttpResponse, Responder};
8use sailfish::TemplateOnce;
9use sqlx::types::time::OffsetDateTime;
10
11use crate::date::Date;
12use crate::errors::PageResult;
13use crate::AppData;
14
15#[derive(TemplateOnce)]
16#[template(path = "panel/notifications/index.html")]
17pub struct IndexPage {
18 n: Vec<Notification>,
20}
21
22impl IndexPage {
23 fn new(n: Vec<Notification>) -> Self {
24 IndexPage { n }
25 }
26}
27
28pub struct Notification {
29 pub name: String,
30 pub heading: String,
31 pub message: String,
32 pub received: OffsetDateTime,
33 pub id: i32,
34}
35
36impl From<db_core::Notification> for Notification {
37 fn from(n: db_core::Notification) -> Self {
38 Notification {
39 name: n.name.unwrap(),
40 heading: n.heading.unwrap(),
41 received: OffsetDateTime::from_unix_timestamp(n.received.unwrap()).unwrap(),
42 id: n.id.unwrap(),
43 message: n.message.unwrap(),
44 }
45 }
46}
47
48impl Notification {
49 pub fn print_date(&self) -> String {
50 Date::format(&self.received)
51 }
52}
53
54const PAGE: &str = "Notifications";
55
56#[my_codegen::get(
57 path = "crate::PAGES.panel.notifications",
58 wrap = "crate::pages::get_middleware()"
59)]
60pub async fn notifications(data: AppData, id: Identity) -> PageResult<impl Responder> {
61 let receiver = id.identity().unwrap();
62 let mut notifications = data.db.get_all_unread_notifications(&receiver).await?;
66 let notifications = notifications.drain(0..).map(|x| x.into()).collect();
67
68 let body = IndexPage::new(notifications).render_once().unwrap();
69 Ok(HttpResponse::Ok()
70 .content_type("text/html; charset=utf-8")
71 .body(body))
72}
73
74#[cfg(test)]
75mod tests {
76 use super::*;
77 use crate::date::*;
78
79 #[test]
80 fn print_date_test() {
81 let mut n = Notification {
82 received: OffsetDateTime::now_utc(),
83 name: String::default(),
84 heading: String::default(),
85 message: String::default(),
86 id: 1,
87 };
88
89 let timestamp = n.received.unix_timestamp();
90 println!("timestamp: {}", timestamp);
91
92 assert!(n.print_date().contains("seconds ago"));
94 n.received = OffsetDateTime::from_unix_timestamp(timestamp - 5).unwrap();
95 assert!(n.print_date().contains("seconds ago"));
96
97 n.received =
99 OffsetDateTime::from_unix_timestamp(timestamp - MINUTE * 2).unwrap();
100 assert!(n.print_date().contains("minutes ago"));
101 n.received =
102 OffsetDateTime::from_unix_timestamp(timestamp - MINUTE * 56).unwrap();
103 assert!(n.print_date().contains("minutes ago"));
104
105 n.received = OffsetDateTime::from_unix_timestamp(timestamp - HOUR).unwrap();
107 assert!(n.print_date().contains("hours ago"));
108 n.received = OffsetDateTime::from_unix_timestamp(timestamp - HOUR * 23).unwrap();
109 assert!(n.print_date().contains("hours ago"));
110
111 n.received = OffsetDateTime::from_unix_timestamp(timestamp - 2 * WEEK).unwrap();
113 assert!(n.print_date().contains("days ago"));
114
115 n.received = OffsetDateTime::from_unix_timestamp(timestamp - 6 * WEEK).unwrap();
117 let date = format!(
118 "{}{}{}",
119 n.received.year(),
120 n.received.month(),
121 n.received.date()
122 );
123 assert!(n.print_date().contains(&date))
124 }
125}