Add service api handler for Wall.getPost

This commit is contained in:
Celestora 2021-10-07 09:28:41 +00:00 committed by Celestora
parent d65d747ad3
commit 626b343baa
2 changed files with 70 additions and 0 deletions

58
ServiceAPI/Wall.php Normal file
View file

@ -0,0 +1,58 @@
<?php declare(strict_types=1);
namespace openvk\ServiceAPI;
use openvk\Web\Models\Entities\User;
use openvk\Web\Models\Repositories\Posts;
class Wall implements Handler
{
protected $user;
protected $posts;
function __construct(?User $user)
{
$this->user = $user;
$this->posts = new Posts;
}
function getPost(int $id, callable $resolve, callable $reject): void
{
$post = $this->posts->get($id);
if(!$post || $post->isDeleted())
$reject("No post with id=$id");
$res = (object) [];
$res->id = $post->getId();
$res->wall = $post->getTargetWall();
$res->author = (($owner = $post->getOwner())) instanceof User
? ($owner->getId())
: ($owner->getId() * -1);
if($post->isSigned())
$res->signedOffBy = $post->getOwnerPost();
$res->pinned = $post->isPinned();
$res->sponsored = $post->isAd();
$res->nsfw = $post->isExplicit();
$res->text = $post->getText();
$res->likes = [
"count" => $post->getLikesCount(),
"hasLike" => $post->hasLikeFrom($this->user),
"likedBy" => [],
];
foreach($post->getLikers() as $liker) {
$res->likes["likedBy"][] = [
"id" => $liker->getId(),
"url" => $liker->getURL(),
"name" => $liker->getCanonicalName(),
"avatar" => $liker->getAvatarURL(),
];
}
$res->created = (string) $post->getPublicationTime();
$res->canPin = $post->canBePinnedBy($this->user);
$res->canEdit = $res->canDelete = $post->canBeDeletedBy($this->user);
$resolve((array) $res);
}
}

View file

@ -84,6 +84,18 @@ abstract class Postable extends Attachable
]));
}
// TODO add pagination
function getLikers(): \Traversable
{
$sel = DB::i()->getContext()->table("likes")->where([
"model" => static::class,
"target" => $this->getRecord()->id,
]);
foreach($sel as $like)
yield (new Users)->get($like->origin);
}
function toggleLike(User $user): void
{
$searchData = [