1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-12-27 22:18:51 +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::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<u64> {
pub fn post_notif_id(&mut self, name: &str) -> ResultBoxError<Notification> {
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(&notif)? {
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!();
}

View File

@ -47,6 +47,13 @@ pub fn get_list_unread(user_id: &UserID) -> ResultBoxError<Vec<Notification>> {
.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
fn db_to_notif(row: &database::RowResult) -> ResultBoxError<Notification> {
Ok(Notification {