2020-07-10 10:55:50 +00:00
|
|
|
//! # Notifications helper
|
|
|
|
//!
|
|
|
|
//! @author Pierre Hubert
|
|
|
|
|
|
|
|
use crate::constants::database_tables_names::NOTIFICATIONS_TABLE;
|
|
|
|
use crate::data::error::ResultBoxError;
|
2020-07-11 06:14:30 +00:00
|
|
|
use crate::data::notification::{NotifElemType, NotifEventType, NotifEventVisibility, Notification};
|
2020-07-10 10:55:50 +00:00
|
|
|
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)
|
2020-07-11 06:14:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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)),
|
|
|
|
})
|
2020-07-10 10:55:50 +00:00
|
|
|
}
|