1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2025-01-08 11:42:35 +00:00
comunicapiv3/src/controllers/notifications_controller.rs

35 lines
1.5 KiB
Rust
Raw Normal View History

//! # Notifications controller
//!
//! @author Pierre Hubert
use crate::api_data::notification_api::NotificationAPI;
use crate::api_data::res_count_all_unreads::ResCountAllUnread;
use crate::api_data::res_number_unread_notifications::ResNumberUnreadNotifications;
use crate::controllers::routes::RequestResult;
use crate::data::http_request_handler::HttpRequestHandler;
use crate::helpers::{conversations_helper, friends_helper, notifications_helper};
/// 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))
}
/// 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))
}
/// 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))
}