2020-07-10 10:55:50 +00:00
|
|
|
//! # Notifications controller
|
|
|
|
//!
|
|
|
|
//! @author Pierre Hubert
|
|
|
|
|
2020-07-11 06:14:30 +00:00
|
|
|
use crate::api_data::notification_api::NotificationAPI;
|
2020-07-10 11:31:40 +00:00
|
|
|
use crate::api_data::res_count_all_unreads::ResCountAllUnread;
|
2020-07-10 10:55:50 +00:00
|
|
|
use crate::api_data::res_number_unread_notifications::ResNumberUnreadNotifications;
|
|
|
|
use crate::controllers::routes::RequestResult;
|
2020-07-11 11:29:12 +00:00
|
|
|
use crate::data::error::ResultBoxError;
|
2020-07-10 10:55:50 +00:00
|
|
|
use crate::data::http_request_handler::HttpRequestHandler;
|
2020-07-11 11:29:12 +00:00
|
|
|
use crate::data::notification::PartialNotification;
|
2020-07-10 11:31:40 +00:00
|
|
|
use crate::helpers::{conversations_helper, friends_helper, notifications_helper};
|
2020-07-10 10:55:50 +00:00
|
|
|
|
2020-07-11 11:29:12 +00:00
|
|
|
impl HttpRequestHandler {
|
|
|
|
/// Get the id of a notification included in the request
|
|
|
|
pub fn post_notif_id(&mut self, name: &str) -> ResultBoxError<u64> {
|
|
|
|
let notif_id = self.post_u64(name)?;
|
|
|
|
|
|
|
|
let notif = PartialNotification::new()
|
|
|
|
.set_id(notif_id)
|
|
|
|
.set_dest_user_id(self.user_id_ref()?);
|
|
|
|
|
|
|
|
if !notifications_helper::similar_exists(¬if)? {
|
|
|
|
self.not_found("Specified notification not found!".to_string())?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(notif_id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-10 10:55:50 +00:00
|
|
|
/// Count the number of unread notifications
|
|
|
|
pub fn count_unread(r: &mut HttpRequestHandler) -> RequestResult {
|
|
|
|
let number = notifications_helper::count_unread(r.user_id_ref()?)?;
|
|
|
|
r.set_response(ResNumberUnreadNotifications::new(number))
|
2020-07-10 11:31:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Count the number of unread notifications
|
|
|
|
pub fn count_all_news(r: &mut HttpRequestHandler) -> RequestResult {
|
|
|
|
let notifications = notifications_helper::count_unread(r.user_id_ref()?)?;
|
|
|
|
let conversations = conversations_helper::count_unread_for_user(r.user_id_ref()?)?;
|
|
|
|
let friends_requests = match r.post_bool_opt("friends_request", false) {
|
|
|
|
true => Some(friends_helper::count_requests(r.user_id_ref()?)?),
|
|
|
|
false => None
|
|
|
|
};
|
|
|
|
|
|
|
|
r.set_response(ResCountAllUnread::new(notifications, conversations as u64, friends_requests))
|
2020-07-11 06:14:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the list of unread notifications
|
|
|
|
pub fn get_list_unread(r: &mut HttpRequestHandler) -> RequestResult {
|
|
|
|
let list = notifications_helper::get_list_unread(r.user_id_ref()?)?;
|
|
|
|
|
|
|
|
r.set_response(NotificationAPI::for_list(&list))
|
2020-07-11 11:29:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Mark a notification as seen
|
|
|
|
pub fn mark_seen(r: &mut HttpRequestHandler) -> RequestResult {
|
|
|
|
let notif_id = r.post_notif_id("notifID")?;
|
|
|
|
let delete_similar = r.post_bool_opt("delete_similar", false);
|
|
|
|
|
2020-07-11 11:47:07 +00:00
|
|
|
// Check if we are targeting a precise notification or an undetermined number of similar
|
|
|
|
// notifications
|
|
|
|
if !delete_similar {
|
|
|
|
notifications_helper::delete(
|
|
|
|
&PartialNotification::new()
|
|
|
|
.set_id(notif_id)
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
// TODO : implement me
|
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
|
|
|
|
r.success("Notification deleted")
|
2020-07-10 10:55:50 +00:00
|
|
|
}
|