Added a method to delete notifications.

This commit is contained in:
Pierre 2018-02-20 14:40:42 +01:00
parent 6d1c5c3d2c
commit 6d96091c3f
2 changed files with 96 additions and 0 deletions

View File

@ -50,6 +50,78 @@ class notificationsController {
}
/**
* Mark a notification as seen
*
* @url POST notifications/mark_seen
*/
public function mark_seen(){
user_login_required();
//Get notification ID
$notifID = $this->getPostNotifID("notifID");
//Check the kind of deletion to do
$delete_similar = false;
if(isset($_POST['delete_similar']))
$delete_similar = ($_POST['delete_similar'] === "true");
//Get informations about the notification
$infos_notif = components()->notifications->get_single($notifID);
//Check for error
if(!$infos_notif->has_id())
Rest_fatal_error(500, "An error occured while trying to process the notification !");
//Perform the required kind of deletion required
$notification = new Notification();
if(!$delete_similar){
$notification->set_id($notifID);
}
else {
$notification->set_on_elem_type($infos_notif->get_on_elem_type());
$notification->set_on_elem_id($infos_notif->get_on_elem_id());
$notification->set_dest_user_id($infos_notif->get_dest_user_id());
}
//Delete the notification
if(!components()->notifications->delete($notification))
Rest_fatal_error(500, "Could not delete the notification !");
//Success
return array("success" => "The notification was deleted.");
}
/**
* Get a valid notification ID in a POST request
*
* @param string $name The name of the post field containing the notification ID
* @return int The ID of the notification
*/
private function getPostNotifID(string $name) : int {
user_login_required();
//Check the POST request
if(!isset($_POST[$name]))
Rest_fatal_error(400, "Please specify the ID of the notification in '".$name."' !");
$notifID = (int) $_POST[$name];
//Check if the notification exists and if the user is allowed to use it
$notif = new Notification();
$notif->set_dest_user_id(userID);
$notif->set_id($notifID);
if(!components()->notifications->similar_exists($notif))
Rest_fatal_error(404, "The specified notification was not found !");
return $notifID;
}
/**
* Turn a notification entry into a standard API notification array

View File

@ -54,6 +54,30 @@ class notificationComponent {
return $list;
}
/**
* Get the informations about a single notification
*
* @param int $notifID The target notification ID
* @return Notification The target notification
*/
public function get_single(int $notifID) : Notification {
//Perform database request
$conditions = "WHERE id = ?";
$values = array($notifID);
//Perform the request
$result = CS::get()->db->select(self::NOTIFICATIONS_TABLE, $conditions, $values);
//Check for error
if(count($result) == 0)
return new Notification(); //Return empty notification
//Parse and return notification
return $this->dbToNotification($result[0]);
}
/**
* Push a new notification
*