From 1eac8962911ebf7366c0ca77a54a705d2124e03c Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Fri, 10 Jul 2020 13:31:40 +0200 Subject: [PATCH] Add the method /notifications/count_all_news --- src/api_data/mod.rs | 3 ++- src/api_data/res_count_all_unreads.rs | 30 +++++++++++++++++++++ src/controllers/notifications_controller.rs | 15 ++++++++++- src/controllers/routes.rs | 2 ++ src/helpers/friends_helper.rs | 9 +++++++ 5 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 src/api_data/res_count_all_unreads.rs diff --git a/src/api_data/mod.rs b/src/api_data/mod.rs index 139587e..ed81ad2 100644 --- a/src/api_data/mod.rs +++ b/src/api_data/mod.rs @@ -38,4 +38,5 @@ pub mod comment_api; pub mod res_create_post; pub mod posts_targets_api; pub mod res_create_comment; -pub mod res_number_unread_notifications; \ No newline at end of file +pub mod res_number_unread_notifications; +pub mod res_count_all_unreads; \ No newline at end of file diff --git a/src/api_data/res_count_all_unreads.rs b/src/api_data/res_count_all_unreads.rs new file mode 100644 index 0000000..ae04985 --- /dev/null +++ b/src/api_data/res_count_all_unreads.rs @@ -0,0 +1,30 @@ +//! # Count unread notifications & conversation +//! +//! Optionally includes friendship requests too +//! +//! @author Pierre Hubert +use serde::Serialize; + +#[derive(Serialize)] +struct CountFriendsRequests { + friends_requests: u64, +} + +#[derive(Serialize)] +pub struct ResCountAllUnread { + notifications: u64, + conversations: u64, + + #[serde(flatten)] + friends: Option, +} + +impl ResCountAllUnread { + pub fn new(notifications: u64, conversations: u64, friends_requests: Option) -> ResCountAllUnread { + ResCountAllUnread { + notifications, + conversations, + friends: friends_requests.map(|n| CountFriendsRequests { friends_requests: n }), + } + } +} \ No newline at end of file diff --git a/src/controllers/notifications_controller.rs b/src/controllers/notifications_controller.rs index a522d13..79fd007 100644 --- a/src/controllers/notifications_controller.rs +++ b/src/controllers/notifications_controller.rs @@ -2,13 +2,26 @@ //! //! @author Pierre Hubert +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::notifications_helper; +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)) } \ No newline at end of file diff --git a/src/controllers/routes.rs b/src/controllers/routes.rs index 9dcccd3..60bb809 100644 --- a/src/controllers/routes.rs +++ b/src/controllers/routes.rs @@ -247,6 +247,8 @@ pub fn get_routes() -> Vec { // Notifications controller Route::post("/notifications/count_unread", Box::new(notifications_controller::count_unread)), + Route::post("/notifications/count_all_news", Box::new(notifications_controller::count_all_news)), + // Movies controller Route::post("/movies/get_list", Box::new(movies_controller::get_list)), diff --git a/src/helpers/friends_helper.rs b/src/helpers/friends_helper.rs index 375e9b9..79fb1d6 100644 --- a/src/helpers/friends_helper.rs +++ b/src/helpers/friends_helper.rs @@ -238,6 +238,15 @@ pub fn get_status(user_id: &UserID, friend_id: &UserID) -> ResultBoxError ResultBoxError { + database::QueryInfo::new(FRIENDS_TABLE) + .cond_user_id("ID_personne", user_id) + .cond_legacy_bool("actif", false) + .exec_count() + .map(|r| r as u64) +} + /// Turn a database entry into a Friend structure fn db_to_friend(row: &database::RowResult) -> ResultBoxError { Ok(Friend {