1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-12-28 14:38:52 +00:00

Improve code logic

This commit is contained in:
Pierre HUBERT 2020-07-11 13:57:20 +02:00
parent cf549f973c
commit b133827c61
2 changed files with 19 additions and 13 deletions

View File

@ -8,23 +8,24 @@ use crate::api_data::res_number_unread_notifications::ResNumberUnreadNotificatio
use crate::controllers::routes::RequestResult; use crate::controllers::routes::RequestResult;
use crate::data::error::ResultBoxError; use crate::data::error::ResultBoxError;
use crate::data::http_request_handler::HttpRequestHandler; 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}; use crate::helpers::{conversations_helper, friends_helper, notifications_helper};
impl HttpRequestHandler { impl HttpRequestHandler {
/// Get the id of a notification included in the request /// Get the id of a notification included in the request
pub fn post_notif_id(&mut self, name: &str) -> ResultBoxError<u64> { pub fn post_notif_id(&mut self, name: &str) -> ResultBoxError<Notification> {
let notif_id = self.post_u64(name)?; let notif_id = self.post_u64(name)?;
let notif = PartialNotification::new() let notif = self.ok_or_not_found(
.set_id(notif_id) notifications_helper::get_single(notif_id),
.set_dest_user_id(self.user_id_ref()?); "Specified notification not found!",
)?;
if !notifications_helper::similar_exists(&notif)? { if notif.dest_user_id != self.user_id()? {
self.not_found("Specified notification not found!".to_string())?; 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 /// Mark a notification as seen
pub fn mark_seen(r: &mut HttpRequestHandler) -> RequestResult { 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); let delete_similar = r.post_bool_opt("delete_similar", false);
// Check if we are targeting a precise notification or an undetermined number of similar // 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 { if !delete_similar {
notifications_helper::delete( notifications_helper::delete(
&PartialNotification::new() &PartialNotification::new()
.set_id(notif_id) .set_id(notif.id)
)?; )?;
} } else {
else {
// TODO : implement me // TODO : implement me
unimplemented!(); unimplemented!();
} }

View File

@ -47,6 +47,13 @@ pub fn get_list_unread(user_id: &UserID) -> ResultBoxError<Vec<Notification>> {
.exec(db_to_notif) .exec(db_to_notif)
} }
/// Get information about a single notification
pub fn get_single(notif_id: u64) -> ResultBoxError<Notification> {
database::QueryInfo::new(NOTIFICATIONS_TABLE)
.cond_u64("id", notif_id)
.query_row(db_to_notif)
}
/// Turn a database row into a notification object /// Turn a database row into a notification object
fn db_to_notif(row: &database::RowResult) -> ResultBoxError<Notification> { fn db_to_notif(row: &database::RowResult) -> ResultBoxError<Notification> {
Ok(Notification { Ok(Notification {