1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-12-03 02:36:27 +00:00
comunicapiv3/src/helpers/notifications_helper.rs

45 lines
1.8 KiB
Rust
Raw Normal View History

//! # Notifications helper
//!
//! @author Pierre Hubert
use crate::constants::database_tables_names::NOTIFICATIONS_TABLE;
use crate::data::error::ResultBoxError;
use crate::data::notification::{NotifElemType, NotifEventType, NotifEventVisibility, Notification};
use crate::data::user::UserID;
use crate::helpers::database;
/// Count the number of unread notifications
pub fn count_unread(user_id: &UserID) -> ResultBoxError<u64> {
database::QueryInfo::new(NOTIFICATIONS_TABLE)
.cond_user_id("dest_user_id", user_id)
.cond_legacy_bool("seen", false)
.exec_count()
.map(|c| c as u64)
}
/// Get the list of notifications of the user
pub fn get_list_unread(user_id: &UserID) -> ResultBoxError<Vec<Notification>> {
database::QueryInfo::new(NOTIFICATIONS_TABLE)
.cond_user_id("dest_user_id", user_id)
.cond_legacy_bool("seen", false)
.set_order("id DESC")
.exec(db_to_notif)
}
/// Turn a database row into a notification object
fn db_to_notif(row: &database::RowResult) -> ResultBoxError<Notification> {
Ok(Notification {
id: row.get_u64("id")?,
time_create: row.get_u64("time_create")?,
seen: row.get_legacy_bool("seen")?,
from_user_id: row.get_user_id("from_user_id")?,
dest_user_id: row.get_user_id("dest_user_id")?,
on_elem_id: row.get_u64("on_elem_id")?,
on_elem_type: NotifElemType::from_db(&row.get_str("on_elem_type")?),
kind: NotifEventType::from_db(&row.get_str("type")?),
visibility: NotifEventVisibility::from_db(&row.get_str("visibility")?),
container_id: row.get_optional_u64("from_container_id")?,
container_type: row.get_optional_str("from_container_type")?
.map(|s| NotifElemType::from_db(&s)),
})
}