mirror of
https://github.com/pierre42100/ComunicAPI
synced 2024-11-23 22:09:29 +00:00
Work progress on notifications.
This commit is contained in:
parent
b9cb4e50fc
commit
1194bb4b71
@ -45,6 +45,18 @@ class commentsController {
|
||||
if($commentID < 1)
|
||||
Rest_fatal_error(500, "An error occured while trying to create comment !");
|
||||
|
||||
|
||||
//Create a notification
|
||||
$notification = new Notification();
|
||||
$notification->set_time(time());
|
||||
$notification->set_from_user_id(userID);
|
||||
$notification->set_on_elem_id($postID);
|
||||
$notification->set_on_elem_type(Notification::POST);
|
||||
$notification->set_type(Notification::COMMENT_CREATED);
|
||||
|
||||
//Push notification
|
||||
components()->notifications->push($notification);
|
||||
|
||||
//Success
|
||||
return array(
|
||||
"success" => "The comment was created!",
|
||||
|
194
classes/components/notifications.php
Normal file
194
classes/components/notifications.php
Normal file
@ -0,0 +1,194 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* User notification component
|
||||
*
|
||||
* @author Pierre HUBERT
|
||||
*/
|
||||
|
||||
class notificationComponent {
|
||||
|
||||
/**
|
||||
* Notifications table name
|
||||
*/
|
||||
const NOTIFICATIONS_TABLE = "comunic_notifications";
|
||||
|
||||
/**
|
||||
* Push a new notification
|
||||
*
|
||||
* @param Notification $notification The notification to push
|
||||
* @return bool TRUE if the notification was pushed / FALSE else
|
||||
*/
|
||||
public function push(Notification $notification) : bool {
|
||||
|
||||
//Determine the visibility level of the notification
|
||||
if($notification->get_on_elem_type() == Notification::POST){
|
||||
|
||||
//Fetch post informations
|
||||
$infos_post = components()->posts->get_single($notification->get_on_elem_id());
|
||||
|
||||
//Update post informations
|
||||
$notification->set_from_container_type(Notification::USER_PAGE);
|
||||
$notification->set_from_container_id($infos_post['user_page_id']);
|
||||
|
||||
//Check if the notification is private or not
|
||||
if($infos_post['visibility_level'] == Posts::VISIBILITY_USER){
|
||||
|
||||
//Push the notification only to the user, and only if it is not him
|
||||
if($notification->get_from_user_id() == $infos_post['userID'])
|
||||
return false; //Nothing to be done
|
||||
|
||||
//Set the target user
|
||||
$notification->set_dest_user_id($infos_post['user_page_id']);
|
||||
|
||||
//Push the notification
|
||||
return $notification->push_private($notification);
|
||||
}
|
||||
else {
|
||||
|
||||
//Get the list of friends of the user
|
||||
$friendslist = components()->friends->getList($infos_post['user_page_id']);
|
||||
|
||||
//Generate the list of target users
|
||||
$target_users = array();
|
||||
foreach($friendslist as $friend){
|
||||
|
||||
//Check if the friend is following his friend
|
||||
if(!components()->friends->is_following($friend->getFriendID(), $notification->get_from_user_id())){
|
||||
continue;
|
||||
}
|
||||
|
||||
//Check if target user and page owner are friends
|
||||
if(!components()->friends->are_friend($friend->getFriendID(), $notification->from_container_id()))
|
||||
continue;
|
||||
|
||||
//Add the user to the list
|
||||
$target_users[] = $friend->getFriendID();
|
||||
|
||||
}
|
||||
|
||||
//The notification can be publicy published
|
||||
return $this->push_public($notification, $target_users);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//Unsupported element
|
||||
else {
|
||||
throw new Exception("The kind of notification ".$notification->get_on_elem_type()." is not currently supported !");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Push a notification to several users
|
||||
*
|
||||
* @param Notification $notification The notification to push
|
||||
* @param array $usersID The list of target users
|
||||
* @return bool FALSE for a failure
|
||||
*/
|
||||
private function push_public(Notification $notification, array $usersID) : bool {
|
||||
|
||||
//Process the list of users
|
||||
foreach($usersID as $current_user){
|
||||
|
||||
//Set the current user id for the notification
|
||||
$notification->set_dest_user_id($current_user);
|
||||
|
||||
//Check if a similar notification already exists or not
|
||||
if($this->similar_exists($notification))
|
||||
continue; //A similar notification already exists
|
||||
|
||||
//Create the notification
|
||||
$this->create($notification);
|
||||
}
|
||||
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Push a private notification
|
||||
*
|
||||
* Warning ! The target user for the notification must be already set !!!
|
||||
*
|
||||
* @param Notification $notification The notification to push
|
||||
* @return bool TRUE if the notification was created / FALSE else
|
||||
*/
|
||||
private function push_private(Notification $notification) : bool {
|
||||
|
||||
//Check if a similar notification already exists or not
|
||||
if($this->similar_exists($notification))
|
||||
return false; //A similar notification already exists
|
||||
|
||||
//Create the notification
|
||||
return $this->create($notification);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check if a notification similar to the given one exists or not
|
||||
*
|
||||
* @param Notification $notification The notification to compare
|
||||
* @return bool TRUE if a similar notification exists / FALSE else
|
||||
*/
|
||||
public function similar_exists(Notification $notification) : bool {
|
||||
|
||||
//Prepare the request
|
||||
$tableName = self::NOTIFICATIONS_TABLE;
|
||||
$conditions = array(
|
||||
|
||||
"seen" => $notification->is_seen() ? 1 : 0,
|
||||
"dest_user_id" => $notification->get_dest_user_id(),
|
||||
"on_elem_id" => $notification->get_on_elem_id(),
|
||||
"on_elem_type" => $notification->get_on_elem_type(),
|
||||
"type" => $notification->get_type()
|
||||
|
||||
);
|
||||
|
||||
//Make conditions
|
||||
$process_conditions = CS::get()->db->splitConditionsArray($conditions);
|
||||
$conditions = "WHERE ".$process_conditions[0];
|
||||
$values = $process_conditions[1];
|
||||
|
||||
//Return the result
|
||||
return CS::get()->db->count($tableName, $conditions, $values) > 0;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a notification
|
||||
*
|
||||
* @param Notification $notification The notification to create
|
||||
* @return bool TRUE for a success / FALSE else
|
||||
*/
|
||||
private function create(Notification $notification) : bool {
|
||||
|
||||
//Generate notification datas
|
||||
$values = $this->noticationToDB($notification);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a notification object into database array
|
||||
*
|
||||
* @param Notification $notification The notification to convert
|
||||
* @return array The converted array
|
||||
*/
|
||||
private function noticationToDB(Notification $notification) : array {
|
||||
|
||||
$data = array();
|
||||
|
||||
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//Register component
|
||||
Components::register("notifications", new notificationComponent());
|
@ -10,8 +10,11 @@ class Notification {
|
||||
/**
|
||||
* Elements type
|
||||
*/
|
||||
const USER_PAGE = "user_page";
|
||||
const CONVERSATION = "conversation";
|
||||
const POST_TEXT = "post";
|
||||
const CONVERSATION_MESSAGE = "conversation_message";
|
||||
const POST = "post";
|
||||
const POST_TEXT = "post_text";
|
||||
const POST_IMAGE = "post_img";
|
||||
const POST_YOUTUBE = "post_youtube";
|
||||
const POST_MOVIE = "post_movie";
|
||||
@ -24,9 +27,17 @@ class Notification {
|
||||
/**
|
||||
* Event type
|
||||
*/
|
||||
const COMMENT_CREATED = "comments";
|
||||
const ELEM_CREATED = "elem_created";
|
||||
const ELEM_UPDATED = "elem_updated";
|
||||
|
||||
/**
|
||||
* Event visibility
|
||||
*/
|
||||
const EVENT_PRIVATE = "event_private";
|
||||
const EVENT_PUBLIC = "event_public";
|
||||
|
||||
|
||||
//Private fields
|
||||
private $id;
|
||||
private $time;
|
||||
@ -36,9 +47,23 @@ class Notification {
|
||||
private $on_elem_id;
|
||||
private $on_elem_type;
|
||||
private $type;
|
||||
private $event_visibility;
|
||||
private $from_container_id;
|
||||
private $from_container_type;
|
||||
|
||||
/**
|
||||
* Default constructor of the notification
|
||||
*/
|
||||
public function __construct(){
|
||||
|
||||
//Notification not seen by default
|
||||
$this->seen = false;
|
||||
|
||||
//By default, the notification does not have any contener
|
||||
$this->from_container_id = 0;
|
||||
$this->from_container_type = "";
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Set notification id
|
||||
@ -185,6 +210,24 @@ class Notification {
|
||||
return $this->type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set notification event visibility
|
||||
*
|
||||
* @param string $visibility The visibility of the notification
|
||||
*/
|
||||
public function set_event_visibility(string $event_visibility){
|
||||
$this->event_visibility = $event_visibility;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get notification event visibility
|
||||
*
|
||||
* @return string The visibility of the notification
|
||||
*/
|
||||
public function get_event_visibility() : string {
|
||||
return $this->event_visibility;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set notification target container element type (if any)
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user