ComunicAPI/RestControllers/LikesController.php

102 lines
2.1 KiB
PHP
Raw Permalink Normal View History

2017-12-29 08:49:44 +00:00
<?php
/**
* API likes controller
*
* @author Pierre HUBERT
*/
2018-05-13 15:50:54 +00:00
class LikesController {
2017-12-29 08:49:44 +00:00
/**
* Update a specific component like
*
* @url POST /likes/update
*/
public function update_like(){
user_login_required(); //Need login
//Get component kind
if(!isset($_POST["type"]))
Rest_fatal_error(401, "Please specify like type !");
$type = $_POST["type"];
//Check new status for the like
if(!isset($_POST['like']))
Rest_fatal_error(400, "Please specify the new like status with the request !");
$like = $_POST['like'] == "true";
//Find the right component type for checks
switch($type){
//In case of user
case "user":
//Extract informations
$id = getPostUserId("id");
$componentType = Likes::LIKE_USER;
//Check if user can access page
if(!CS::get()->components->user->userAllowed(userID, $id))
Rest_fatal_error(401, "You can not access this user information !");
break;
2018-01-04 16:42:40 +00:00
//In case of post
case "post":
//Extract informations
2018-01-15 18:14:34 +00:00
$id = getPostPostIDWithAccess("id");
2018-01-04 16:42:40 +00:00
$componentType = Likes::LIKE_POST;
//Delete any notification targeting this user about the post
delete_user_notifications_over_post(userID, $id);
2018-01-04 16:42:40 +00:00
break;
//In case of comments
case "comment":
//Extract informations
$id = getPostCommentIDWithAccess("id");
$componentType = Likes::LIKE_COMMENT;
break;
2017-12-29 08:49:44 +00:00
//Default case : error
default:
2018-01-04 16:42:40 +00:00
Rest_fatal_error(404, "Specifed component type currently not supported !");
2017-12-29 08:49:44 +00:00
}
//Update like status
CS::get()->components->likes->update(userID, $like, $id, $componentType);
//Success
return array("success" => "Like status updated.");
}
2018-05-13 15:50:54 +00:00
/**
* Turn a UserLike object into valid API entry
*
* @param UserLike $object The object to convert
* @return array Valid API database entry
*/
public static function UserLikeToAPI(UserLike $object) : array {
$data = array();
$data["id"] = $object->get_id();
$data["userID"] = $object->get_userID();
$data["time_sent"] = $object->get_time_sent();
$data["elem_type"] = $object->get_elem_type();
$data["elem_id"] = $object->get_elem_id();
return $data;
}
2017-12-29 08:49:44 +00:00
}