Skip to content

Commit

Permalink
Add notification module
Browse files Browse the repository at this point in the history
  • Loading branch information
JieningYu committed Jan 8, 2024
1 parent 65914fd commit 6dfcb3f
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use time::Duration;
pub mod config;

pub mod account;
pub mod notification;
pub mod post;

pub mod resource;
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::sync::Arc;
use dmds::{IoHandle, World};
use lettre::AsyncSmtpTransport;
use sms4_backend::{account::Account, config::Config, resource, Error};
use tokio::sync::{Mutex, RwLock};
use tokio::sync::Mutex;

macro_rules! ipc {
($c:literal) => {
Expand Down
102 changes: 102 additions & 0 deletions src/notification.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
use std::{
hash::{Hash, Hasher},
time::SystemTime,
};

use serde::{Deserialize, Serialize};
use time::OffsetDateTime;

/// Notification sent by admins, and displayed
/// by the screens.
///
/// # dmds Dimensions
///
/// ```txt
/// 0 -> id
/// 1 -> start date day of the year
/// ```
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Notification {
/// Id of the notification.
#[serde(skip)]
id: u64,

/// Title of the notification.
///
/// # Examples
///
/// ```txt
/// 教务通知
/// ```
///
/// ```txt
/// 教务公告
/// ```
pub title: String,
/// Body of the notification.
pub body: String,

/// Start time of the notification.
time: OffsetDateTime,
/// Sender's account id of the notification.
sender: u64,
}

impl Notification {
/// Creates a new notification.
///
/// The **id** of the notification is generated
/// from the body, time and current time.
pub fn new(title: String, body: String, time: OffsetDateTime, sender: u64) -> Self {
let mut hasher = siphasher::sip::SipHasher24::new();
body.hash(&mut hasher);
time.hash(&mut hasher);
SystemTime::now().hash(&mut hasher);

Self {
id: hasher.finish(),
title,
body,
time,
sender,
}
}

/// Returns the id of the notification.
#[inline]
pub fn id(&self) -> u64 {
self.id
}
}

impl dmds::Data for Notification {
const DIMS: usize = 2;
const VERSION: u32 = 1;

#[inline]
fn dim(&self, dim: usize) -> u64 {
match dim {
0 => self.id,
1 => self.time.date().ordinal() as u64,
_ => unreachable!(),
}
}

fn decode<B: bytes::Buf>(version: u32, dims: &[u64], buf: B) -> std::io::Result<Self> {
match version {
1 => bincode::deserialize_from(buf.reader())
.map(|mut n: Self| {
n.id = dims[0];
n
})
.map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, err)),
_ => unreachable!("unsupported data version {version}"),
}
}

#[inline]
fn encode<B: bytes::BufMut>(&self, buf: B) -> std::io::Result<()> {
bincode::serialize_into(buf.writer(), self)
.map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, err))
}
}
2 changes: 1 addition & 1 deletion src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{path::PathBuf, sync::Arc};
use axum::Router;
use dmds::{mem_io_handle::MemStorage, world};
use sms4_backend::config::Config;
use tokio::sync::{Mutex, RwLock};
use tokio::sync::Mutex;

use crate::Global;

Expand Down

0 comments on commit 6dfcb3f

Please sign in to comment.