2022-03-29 14:04:11 +03:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
namespace openvk\VKAPI\Handlers;
|
|
|
|
use openvk\Web\Models\Repositories\Users as UsersRepo;
|
|
|
|
use openvk\Web\Models\Repositories\Posts as PostsRepo;
|
2023-08-05 18:29:37 +03:00
|
|
|
use openvk\Web\Models\Repositories\Comments as CommentsRepo;
|
|
|
|
use openvk\Web\Models\Repositories\Videos as VideosRepo;
|
|
|
|
use openvk\Web\Models\Repositories\Photos as PhotosRepo;
|
|
|
|
use openvk\Web\Models\Repositories\Notes as NotesRepo;
|
2022-03-29 14:04:11 +03:00
|
|
|
|
|
|
|
final class Likes extends VKAPIRequestHandler
|
|
|
|
{
|
2023-08-05 18:29:37 +03:00
|
|
|
function add(string $type, int $owner_id, int $item_id): object
|
|
|
|
{
|
|
|
|
$this->requireUser();
|
2023-02-08 14:20:50 +03:00
|
|
|
$this->willExecuteWriteAction();
|
2022-03-29 14:04:11 +03:00
|
|
|
|
2023-08-05 18:29:37 +03:00
|
|
|
$postable = NULL;
|
2022-07-21 22:13:09 +03:00
|
|
|
switch($type) {
|
|
|
|
case "post":
|
2022-03-29 14:04:11 +03:00
|
|
|
$post = (new PostsRepo)->getPostById($owner_id, $item_id);
|
2023-08-05 18:29:37 +03:00
|
|
|
$postable = $post;
|
|
|
|
break;
|
|
|
|
case "comment":
|
|
|
|
$comment = (new CommentsRepo)->get($item_id);
|
|
|
|
$postable = $comment;
|
|
|
|
break;
|
|
|
|
case "video":
|
|
|
|
$video = (new VideosRepo)->getByOwnerAndVID($owner_id, $item_id);
|
|
|
|
$postable = $video;
|
|
|
|
break;
|
|
|
|
case "photo":
|
|
|
|
$photo = (new PhotosRepo)->getByOwnerAndVID($owner_id, $item_id);
|
|
|
|
$postable = $photo;
|
|
|
|
break;
|
|
|
|
case "note":
|
|
|
|
$note = (new NotesRepo)->getNoteById($owner_id, $item_id);
|
|
|
|
$postable = $note;
|
|
|
|
break;
|
2022-03-29 14:04:11 +03:00
|
|
|
default:
|
2022-07-21 22:13:09 +03:00
|
|
|
$this->fail(100, "One of the parameters specified was missing or invalid: incorrect type");
|
2022-03-29 14:04:11 +03:00
|
|
|
}
|
|
|
|
|
2023-08-05 18:29:37 +03:00
|
|
|
if(is_null($postable) || $postable->isDeleted())
|
|
|
|
$this->fail(100, "One of the parameters specified was missing or invalid: object not found");
|
|
|
|
|
|
|
|
if(!$postable->canBeViewedBy($this->getUser() ?? NULL)) {
|
|
|
|
$this->fail(2, "Access to postable denied");
|
|
|
|
}
|
|
|
|
|
|
|
|
$postable->setLike(true, $this->getUser());
|
|
|
|
|
|
|
|
return (object) [
|
|
|
|
"likes" => $postable->getLikesCount()
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
function delete(string $type, int $owner_id, int $item_id): object
|
|
|
|
{
|
|
|
|
$this->requireUser();
|
2023-02-08 14:20:50 +03:00
|
|
|
$this->willExecuteWriteAction();
|
2022-03-29 14:04:11 +03:00
|
|
|
|
2023-08-05 18:29:37 +03:00
|
|
|
$postable = NULL;
|
2022-07-21 22:13:09 +03:00
|
|
|
switch($type) {
|
|
|
|
case "post":
|
2022-03-29 14:04:11 +03:00
|
|
|
$post = (new PostsRepo)->getPostById($owner_id, $item_id);
|
2023-08-05 18:29:37 +03:00
|
|
|
$postable = $post;
|
|
|
|
break;
|
|
|
|
case "comment":
|
|
|
|
$comment = (new CommentsRepo)->get($item_id);
|
|
|
|
$postable = $comment;
|
|
|
|
break;
|
|
|
|
case "video":
|
|
|
|
$video = (new VideosRepo)->getByOwnerAndVID($owner_id, $item_id);
|
|
|
|
$postable = $video;
|
|
|
|
break;
|
|
|
|
case "photo":
|
|
|
|
$photo = (new PhotosRepo)->getByOwnerAndVID($owner_id, $item_id);
|
|
|
|
$postable = $photo;
|
|
|
|
break;
|
|
|
|
case "note":
|
|
|
|
$note = (new NotesRepo)->getNoteById($owner_id, $item_id);
|
|
|
|
$postable = $note;
|
|
|
|
break;
|
2022-03-29 14:04:11 +03:00
|
|
|
default:
|
2022-07-21 22:13:09 +03:00
|
|
|
$this->fail(100, "One of the parameters specified was missing or invalid: incorrect type");
|
2022-03-29 14:04:11 +03:00
|
|
|
}
|
2023-08-05 18:29:37 +03:00
|
|
|
|
|
|
|
if(is_null($postable) || $postable->isDeleted())
|
|
|
|
$this->fail(100, "One of the parameters specified was missing or invalid: object not found");
|
|
|
|
|
|
|
|
if(!$postable->canBeViewedBy($this->getUser() ?? NULL)) {
|
|
|
|
$this->fail(2, "Access to postable denied");
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!is_null($postable)) {
|
|
|
|
$postable->setLike(false, $this->getUser());
|
|
|
|
|
|
|
|
return (object) [
|
|
|
|
"likes" => $postable->getLikesCount()
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-29 14:04:11 +03:00
|
|
|
function isLiked(int $user_id, string $type, int $owner_id, int $item_id): object
|
2023-08-05 18:29:37 +03:00
|
|
|
{
|
|
|
|
$this->requireUser();
|
|
|
|
|
|
|
|
$user = (new UsersRepo)->get($user_id);
|
|
|
|
|
|
|
|
if(is_null($user) || $user->isDeleted())
|
|
|
|
$this->fail(100, "One of the parameters specified was missing or invalid: user not found");
|
|
|
|
|
|
|
|
if(!$user->canBeViewedBy($this->getUser())) {
|
|
|
|
$this->fail(1984, "Access denied: you can't see this user");
|
|
|
|
}
|
2022-03-29 14:04:11 +03:00
|
|
|
|
2023-08-05 18:29:37 +03:00
|
|
|
$postable = NULL;
|
2022-07-21 22:13:09 +03:00
|
|
|
switch($type) {
|
|
|
|
case "post":
|
2022-03-29 14:04:11 +03:00
|
|
|
$post = (new PostsRepo)->getPostById($owner_id, $item_id);
|
2023-08-05 18:29:37 +03:00
|
|
|
$postable = $post;
|
|
|
|
break;
|
|
|
|
case "comment":
|
|
|
|
$comment = (new CommentsRepo)->get($item_id);
|
|
|
|
$postable = $comment;
|
|
|
|
break;
|
|
|
|
case "video":
|
|
|
|
$video = (new VideosRepo)->getByOwnerAndVID($owner_id, $item_id);
|
|
|
|
$postable = $video;
|
|
|
|
break;
|
|
|
|
case "photo":
|
|
|
|
$photo = (new PhotosRepo)->getByOwnerAndVID($owner_id, $item_id);
|
|
|
|
$postable = $photo;
|
|
|
|
break;
|
|
|
|
case "note":
|
|
|
|
$note = (new NotesRepo)->getNoteById($owner_id, $item_id);
|
|
|
|
$postable = $note;
|
|
|
|
break;
|
2022-03-29 14:04:11 +03:00
|
|
|
default:
|
2022-07-21 22:13:09 +03:00
|
|
|
$this->fail(100, "One of the parameters specified was missing or invalid: incorrect type");
|
2022-03-29 14:04:11 +03:00
|
|
|
}
|
2023-08-05 18:29:37 +03:00
|
|
|
|
|
|
|
if(is_null($postable) || $postable->isDeleted())
|
|
|
|
$this->fail(100, "One of the parameters specified was missing or invalid: object not found");
|
|
|
|
|
|
|
|
if(!$postable->canBeViewedBy($this->getUser())) {
|
|
|
|
$this->fail(665, "Access to postable denied");
|
|
|
|
}
|
|
|
|
|
|
|
|
return (object) [
|
|
|
|
"liked" => (int) $postable->hasLikeFrom($user),
|
|
|
|
"copied" => 0
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|