mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2025-06-21 00:45:18 +00:00
Can get the list of unread notifications
This commit is contained in:
@ -304,6 +304,14 @@ impl<'a> RowResult<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Get an optional unsigned number => Set to None if value is null / empty
|
||||
pub fn get_optional_u64(&self, name: &str) -> ResultBoxError<Option<u64>> {
|
||||
match self.is_null(name)? {
|
||||
true => Ok(None),
|
||||
false => Ok(Some(self.get_u64(name)?))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_u32(&self, name: &str) -> ResultBoxError<u32> {
|
||||
let value = self.row.get_opt(self.find_col(name)?);
|
||||
|
||||
|
@ -4,6 +4,7 @@
|
||||
|
||||
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;
|
||||
|
||||
@ -14,4 +15,31 @@ pub fn count_unread(user_id: &UserID) -> ResultBoxError<u64> {
|
||||
.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)),
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user