From ef7d786e9e7673d192c202b160444c941ec00024 Mon Sep 17 00:00:00 2001 From: Pierre Date: Sat, 3 Mar 2018 14:28:29 +0100 Subject: [PATCH] Sent notifications for friendship requests --- RestControllers/friendsController.php | 23 ++++++++++++++++---- classes/components/notifications.php | 12 +++++++++++ classes/models/Notification.php | 4 ++++ helpers/notifications.php | 31 +++++++++++++++++++++++++-- 4 files changed, 64 insertions(+), 6 deletions(-) diff --git a/RestControllers/friendsController.php b/RestControllers/friendsController.php index d999675..1fae9c0 100644 --- a/RestControllers/friendsController.php +++ b/RestControllers/friendsController.php @@ -66,13 +66,16 @@ class friendsController{ if(!CS::get()->components->friends->send_request(userID, $friendID)) Rest_fatal_error(500, "Couldn't create friendship request !"); + //Create notification + create_friendship_notification(userID, $friendID, Notification::SENT_FRIEND_REQUEST); + //Success return array("success" => "The friendship request has been created !"); } /** - * Remove a previously send frienship request + * Remove a previously sent frienship request * * @url POST /friends/removeRequest */ @@ -90,6 +93,9 @@ class friendsController{ if(!CS::get()->components->friends->remove_request(userID, $friendID)) Rest_fatal_error(500, "An error occured while trying to remove the friendship request !"); + //Delete all related notifications + delete_notifications_friendship_request(userID, $friendID); + //This is a success return array("success" => "The friendship request has been removed!"); } @@ -117,6 +123,11 @@ class friendsController{ if($result != true) Rest_fatal_error(500, "Couldn't respond to friendship request !"); + //Send notification + create_friendship_notification(userID, $friendID, + $acceptRequest ? Notification::ACCEPTED_FRIEND_REQUEST : Notification::REJECTED_FRIEND_REQUEST + ); + //Else it is a success return array("success" => "A response was given to friendship request !"); } @@ -137,11 +148,15 @@ class friendsController{ $friendID = toInt($_POST['friendID']); $result = CS::get()->components->friends->remove(userID, $friendID); - //Check if the operation is a result + //Check if the operation is a success if(!$result) Rest_fatal_error(500, "Couldn't remove user from the friendlist for an unexcepted reason !"); - else - return array("success" => "The friend was removed from the list !"); + + //Delete any related notification + delete_notifications_friendship_request(userID, $friendID); + + //Success + return array("success" => "The friend was removed from the list !"); } /** diff --git a/classes/components/notifications.php b/classes/components/notifications.php index dff9ce1..70cf069 100644 --- a/classes/components/notifications.php +++ b/classes/components/notifications.php @@ -143,6 +143,18 @@ class notificationComponent { } } + + //Handles friendship request notifications + else if($notification->get_on_elem_type() == Notification::FRIENDSHIP_REQUEST){ + + //Complete the notification + $notification->set_from_container_id(0); + $notification->set_from_container_type(""); + + //Push the notification in private way + return $this->push_private($notification); + + } //Unsupported element else { diff --git a/classes/models/Notification.php b/classes/models/Notification.php index 5fcd287..0fa4353 100644 --- a/classes/models/Notification.php +++ b/classes/models/Notification.php @@ -23,11 +23,15 @@ class Notification { const POST_TIMER = "post_timer"; const POST_SURVEY = "post_survey"; const COMMENT = "comment"; + const FRIENDSHIP_REQUEST = "friend"; /** * Event type */ const COMMENT_CREATED = "comment_created"; + const SENT_FRIEND_REQUEST = "sent_friend_request"; + const ACCEPTED_FRIEND_REQUEST = "accepted_request"; + const REJECTED_FRIEND_REQUEST = "rejected_request"; const ELEM_CREATED = "elem_created"; const ELEM_UPDATED = "elem_updated"; diff --git a/helpers/notifications.php b/helpers/notifications.php index 58e9473..86c9bec 100644 --- a/helpers/notifications.php +++ b/helpers/notifications.php @@ -44,15 +44,42 @@ function delete_notifications_friendship_request(int $userOne, int $userTwo) : b //Delete notifications in the two ways $notification->set_dest_user_id($userOne); $notification->set_from_user_id($userTwo); - $notification->set_on_elem_id(0); + $notification->set_on_elem_id($userTwo); if(!components()->notifications->delete($notification)) return false; $notification->set_dest_user_id($userTwo); $notification->set_from_user_id($userOne); - $notification->set_on_elem_id(0); + $notification->set_on_elem_id($userOne); if(!components()->notifications->delete($notification)) return false; return true; +} + +/** + * Create and push a friendship request notification + * + * @param int $fromUser The ID of the user generating the notification + * @param int $destUser The target user for the notification + * @param string $kind The kind of notification to create + * @return bool TRUE in case of success / FALSE else + */ +function create_friendship_notification(int $fromUser, int $destUser, string $kind) : bool { + + //Delete all the previous notifications + if(!delete_notifications_friendship_request($fromUser, $destUser)) + return false; + + //Create the notification + $notif = new Notification(); + $notif->set_time_create(time()); + $notif->set_from_user_id($fromUser); + $notif->set_dest_user_id($destUser); + $notif->set_on_elem_id($fromUser); //Same as from_user_id + $notif->set_on_elem_type(Notification::FRIENDSHIP_REQUEST); + $notif->set_type($kind); + + //Try to push the notification + return components()->notifications->push($notif); } \ No newline at end of file