Sent notifications for friendship requests

This commit is contained in:
Pierre 2018-03-03 14:28:29 +01:00
parent 9213ad239c
commit ef7d786e9e
4 changed files with 64 additions and 6 deletions

View File

@ -66,13 +66,16 @@ class friendsController{
if(!CS::get()->components->friends->send_request(userID, $friendID)) if(!CS::get()->components->friends->send_request(userID, $friendID))
Rest_fatal_error(500, "Couldn't create friendship request !"); Rest_fatal_error(500, "Couldn't create friendship request !");
//Create notification
create_friendship_notification(userID, $friendID, Notification::SENT_FRIEND_REQUEST);
//Success //Success
return array("success" => "The friendship request has been created !"); 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 * @url POST /friends/removeRequest
*/ */
@ -90,6 +93,9 @@ class friendsController{
if(!CS::get()->components->friends->remove_request(userID, $friendID)) if(!CS::get()->components->friends->remove_request(userID, $friendID))
Rest_fatal_error(500, "An error occured while trying to remove the friendship request !"); 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 //This is a success
return array("success" => "The friendship request has been removed!"); return array("success" => "The friendship request has been removed!");
} }
@ -117,6 +123,11 @@ class friendsController{
if($result != true) if($result != true)
Rest_fatal_error(500, "Couldn't respond to friendship request !"); 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 //Else it is a success
return array("success" => "A response was given to friendship request !"); return array("success" => "A response was given to friendship request !");
} }
@ -137,11 +148,15 @@ class friendsController{
$friendID = toInt($_POST['friendID']); $friendID = toInt($_POST['friendID']);
$result = CS::get()->components->friends->remove(userID, $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) if(!$result)
Rest_fatal_error(500, "Couldn't remove user from the friendlist for an unexcepted reason !"); 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 !");
} }
/** /**

View File

@ -144,6 +144,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 //Unsupported element
else { else {
throw new Exception("The kind of notification ".$notification->get_on_elem_type()." is not currently supported !"); throw new Exception("The kind of notification ".$notification->get_on_elem_type()." is not currently supported !");

View File

@ -23,11 +23,15 @@ class Notification {
const POST_TIMER = "post_timer"; const POST_TIMER = "post_timer";
const POST_SURVEY = "post_survey"; const POST_SURVEY = "post_survey";
const COMMENT = "comment"; const COMMENT = "comment";
const FRIENDSHIP_REQUEST = "friend";
/** /**
* Event type * Event type
*/ */
const COMMENT_CREATED = "comment_created"; 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_CREATED = "elem_created";
const ELEM_UPDATED = "elem_updated"; const ELEM_UPDATED = "elem_updated";

View File

@ -44,15 +44,42 @@ function delete_notifications_friendship_request(int $userOne, int $userTwo) : b
//Delete notifications in the two ways //Delete notifications in the two ways
$notification->set_dest_user_id($userOne); $notification->set_dest_user_id($userOne);
$notification->set_from_user_id($userTwo); $notification->set_from_user_id($userTwo);
$notification->set_on_elem_id(0); $notification->set_on_elem_id($userTwo);
if(!components()->notifications->delete($notification)) if(!components()->notifications->delete($notification))
return false; return false;
$notification->set_dest_user_id($userTwo); $notification->set_dest_user_id($userTwo);
$notification->set_from_user_id($userOne); $notification->set_from_user_id($userOne);
$notification->set_on_elem_id(0); $notification->set_on_elem_id($userOne);
if(!components()->notifications->delete($notification)) if(!components()->notifications->delete($notification))
return false; return false;
return true; 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);
}