//! # Notifications helper //! //! @author Pierre Hubert use std::collections::HashMap; use crate::constants::database_tables_names::NOTIFICATIONS_TABLE; use crate::data::error::ResultBoxError; use crate::data::notification::{NotifElemType, NotifEventType, NotifEventVisibility, Notification, PartialNotification}; use crate::data::user::UserID; use crate::helpers::database; /// Delete notifications pub fn delete(notification: &PartialNotification) -> ResultBoxError { let conditions = notif_to_db(notification, false); // Delete the notifications database::DeleteQuery::new(NOTIFICATIONS_TABLE) .add_conditions(conditions) .exec() } /// Delete all the notifications of a given user pub fn delete_all_user(user_id: &UserID) -> ResultBoxError { delete(&PartialNotification::new().set_dest_user_id(user_id)) } /// Check out whether a similar notification exists for given specifications pub fn similar_exists(n: &PartialNotification) -> ResultBoxError { database::QueryInfo::new(NOTIFICATIONS_TABLE) .add_conditions(¬if_to_db(n, false)) .exec_count() .map(|f| f > 0) } /// Count the number of unread notifications pub fn count_unread(user_id: &UserID) -> ResultBoxError { 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> { 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) } /// Get information about a single notification pub fn get_single(notif_id: u64) -> ResultBoxError { database::QueryInfo::new(NOTIFICATIONS_TABLE) .cond_u64("id", notif_id) .query_row(db_to_notif) } /// Turn a database row into a notification object fn db_to_notif(row: &database::RowResult) -> ResultBoxError { 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)), }) } /// Turn a notification into a database entry fn notif_to_db(n: &PartialNotification, complete_information: bool) -> HashMap { let mut map = HashMap::new(); if let Some(id) = n.id { map.insert("id".to_string(), mysql::Value::UInt(id)); } if let Some(seen) = n.seen { map.insert("seen".to_string(), mysql::Value::Int(match seen { true => 1, false => 0, })); } if let Some(from_user_id) = &n.from_user_id { map.insert("from_user_id".to_string(), mysql::Value::UInt(from_user_id.id())); } if let Some(dest_user_id) = &n.dest_user_id { map.insert("dest_user_id".to_string(), mysql::Value::UInt(dest_user_id.id())); } if let Some(kind) = &n.kind { map.insert("type".to_string(), mysql::Value::from(kind.to_db())); } if let Some(on_elem_id) = n.on_elem_id { map.insert("on_elem_id".to_string(), mysql::Value::from(on_elem_id)); } if let Some(on_elem_type) = &n.on_elem_type { map.insert("on_elem_type".to_string(), mysql::Value::from(on_elem_type.to_db())); } if complete_information { if let Some(from_container_id) = n.container_id { map.insert("from_container_id".to_string(), mysql::Value::from(from_container_id)); } if let Some(from_container_type) = &n.container_type { map.insert("from_container_type".to_string(), mysql::Value::from(from_container_type.to_db())); } if let Some(time_create) = n.time_create { map.insert("time_create".to_string(), mysql::Value::from(time_create)); } if let Some(visibility) = &n.visibility { map.insert("visibility".to_string(), mysql::Value::from(visibility.to_db())); } } map }