From b133827c6118f91b68751d3ea9efd5d43d411396 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 11 Jul 2020 13:57:20 +0200 Subject: [PATCH] Improve code logic --- src/controllers/notifications_controller.rs | 25 ++++++++++----------- src/helpers/notifications_helper.rs | 7 ++++++ 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/controllers/notifications_controller.rs b/src/controllers/notifications_controller.rs index cfe656e..ca18ef4 100644 --- a/src/controllers/notifications_controller.rs +++ b/src/controllers/notifications_controller.rs @@ -8,23 +8,24 @@ use crate::api_data::res_number_unread_notifications::ResNumberUnreadNotificatio use crate::controllers::routes::RequestResult; use crate::data::error::ResultBoxError; use crate::data::http_request_handler::HttpRequestHandler; -use crate::data::notification::PartialNotification; +use crate::data::notification::{Notification, PartialNotification}; use crate::helpers::{conversations_helper, friends_helper, notifications_helper}; impl HttpRequestHandler { /// Get the id of a notification included in the request - pub fn post_notif_id(&mut self, name: &str) -> ResultBoxError { + pub fn post_notif_id(&mut self, name: &str) -> ResultBoxError { let notif_id = self.post_u64(name)?; - let notif = PartialNotification::new() - .set_id(notif_id) - .set_dest_user_id(self.user_id_ref()?); + let notif = self.ok_or_not_found( + notifications_helper::get_single(notif_id), + "Specified notification not found!", + )?; - if !notifications_helper::similar_exists(¬if)? { - self.not_found("Specified notification not found!".to_string())?; + if notif.dest_user_id != self.user_id()? { + self.forbidden("You are not allowed to access this notification!".to_string())?; } - Ok(notif_id) + Ok(notif) } } @@ -55,7 +56,7 @@ pub fn get_list_unread(r: &mut HttpRequestHandler) -> RequestResult { /// Mark a notification as seen pub fn mark_seen(r: &mut HttpRequestHandler) -> RequestResult { - let notif_id = r.post_notif_id("notifID")?; + let notif = r.post_notif_id("notifID")?; let delete_similar = r.post_bool_opt("delete_similar", false); // Check if we are targeting a precise notification or an undetermined number of similar @@ -63,11 +64,9 @@ pub fn mark_seen(r: &mut HttpRequestHandler) -> RequestResult { if !delete_similar { notifications_helper::delete( &PartialNotification::new() - .set_id(notif_id) + .set_id(notif.id) )?; - } - - else { + } else { // TODO : implement me unimplemented!(); } diff --git a/src/helpers/notifications_helper.rs b/src/helpers/notifications_helper.rs index af235e0..4cc42ae 100644 --- a/src/helpers/notifications_helper.rs +++ b/src/helpers/notifications_helper.rs @@ -47,6 +47,13 @@ pub fn get_list_unread(user_id: &UserID) -> ResultBoxError> { .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 {