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;
|
2023-11-14 22:44:39 +03:00
|
|
|
use openvk\Web\Models\Repositories\{Posts as PostsRepo, Comments as CommentsRepo, Photos as PhotosRepo, Videos as VideosRepo};
|
2022-03-29 14:04:11 +03:00
|
|
|
|
|
|
|
final class Likes extends VKAPIRequestHandler
|
|
|
|
{
|
|
|
|
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
|
|
|
|
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);
|
2022-07-21 22:13:09 +03:00
|
|
|
if(is_null($post))
|
|
|
|
$this->fail(100, "One of the parameters specified was missing or invalid: object not found");
|
2022-03-29 14:04:11 +03:00
|
|
|
|
|
|
|
$post->setLike(true, $this->getUser());
|
2022-07-21 22:13:09 +03:00
|
|
|
|
|
|
|
return (object) [
|
2022-03-29 14:04:11 +03:00
|
|
|
"likes" => $post->getLikesCount()
|
|
|
|
];
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-21 00:45:40 +03:00
|
|
|
function delete(string $type, int $owner_id, int $item_id): object
|
2022-03-29 14:04:11 +03:00
|
|
|
{
|
|
|
|
$this->requireUser();
|
2023-02-08 14:20:50 +03:00
|
|
|
$this->willExecuteWriteAction();
|
2022-03-29 14:04:11 +03:00
|
|
|
|
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);
|
2022-07-21 22:13:09 +03:00
|
|
|
if (is_null($post))
|
|
|
|
$this->fail(100, "One of the parameters specified was missing or invalid: object not found");
|
2022-03-29 14:04:11 +03:00
|
|
|
|
|
|
|
$post->setLike(false, $this->getUser());
|
2022-07-21 22:13:09 +03:00
|
|
|
return (object) [
|
2022-03-29 14:04:11 +03:00
|
|
|
"likes" => $post->getLikesCount()
|
|
|
|
];
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function isLiked(int $user_id, string $type, int $owner_id, int $item_id): object
|
|
|
|
{
|
|
|
|
$this->requireUser();
|
|
|
|
|
2022-07-21 22:13:09 +03:00
|
|
|
switch($type) {
|
|
|
|
case "post":
|
2022-03-29 14:04:11 +03:00
|
|
|
$user = (new UsersRepo)->get($user_id);
|
2022-07-21 22:13:09 +03:00
|
|
|
if (is_null($user))
|
2023-05-30 11:41:03 +03:00
|
|
|
$this->fail(100, "One of the parameters specified was missing or invalid: user not found");
|
2022-03-29 14:04:11 +03:00
|
|
|
|
|
|
|
$post = (new PostsRepo)->getPostById($owner_id, $item_id);
|
2022-07-21 22:13:09 +03:00
|
|
|
if (is_null($post))
|
|
|
|
$this->fail(100, "One of the parameters specified was missing or invalid: object not found");
|
2022-03-29 14:04:11 +03:00
|
|
|
|
2022-07-21 22:13:09 +03:00
|
|
|
return (object) [
|
|
|
|
"liked" => (int) $post->hasLikeFrom($user),
|
2022-05-08 13:06:26 +03:00
|
|
|
"copied" => 0 # TODO: handle this
|
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-11-14 22:44:39 +03:00
|
|
|
|
|
|
|
function getList(string $type, int $owner_id, int $item_id, bool $extended = false, int $offset = 0, int $count = 10, bool $skip_own = false)
|
|
|
|
{
|
|
|
|
$this->requireUser();
|
|
|
|
|
|
|
|
$object = NULL;
|
|
|
|
|
|
|
|
switch($type) {
|
|
|
|
case "post":
|
|
|
|
$object = (new PostsRepo)->getPostById($owner_id, $item_id);
|
|
|
|
break;
|
|
|
|
case "comment":
|
|
|
|
$object = (new CommentsRepo)->get($item_id);
|
|
|
|
break;
|
|
|
|
case "photo":
|
|
|
|
$object = (new PhotosRepo)->getByOwnerAndVID($owner_id, $item_id);
|
|
|
|
break;
|
|
|
|
case "video":
|
|
|
|
$object = (new VideosRepo)->getByOwnerAndVID($owner_id, $item_id);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$this->fail(58, "Invalid type");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!$object || $object->isDeleted())
|
|
|
|
$this->fail(56, "Invalid postable");
|
|
|
|
|
|
|
|
$res = (object)[
|
|
|
|
"count" => $object->getLikesCount(),
|
|
|
|
"items" => []
|
|
|
|
];
|
|
|
|
|
|
|
|
$likers = array_slice(iterator_to_array($object->getLikers(1, $offset + $count)), $offset);
|
|
|
|
|
|
|
|
foreach($likers as $liker) {
|
|
|
|
if($skip_own && $liker->getId() == $this->getUser()->getId())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if(!$extended)
|
|
|
|
$res->items[] = $liker->getId();
|
|
|
|
else
|
|
|
|
$res->items[] = $liker->toVkApiStruct();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $res;
|
|
|
|
}
|
2022-03-29 14:04:11 +03:00
|
|
|
}
|