2021-10-07 12:28:41 +03:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
namespace openvk\ServiceAPI;
|
2022-08-20 21:07:54 +03:00
|
|
|
use openvk\Web\Models\Entities\Post;
|
2021-10-07 12:28:41 +03:00
|
|
|
use openvk\Web\Models\Entities\User;
|
2023-07-05 14:54:58 +03:00
|
|
|
use openvk\Web\Models\Repositories\{Posts, Notes};
|
2021-10-07 12:28:41 +03:00
|
|
|
|
|
|
|
class Wall implements Handler
|
|
|
|
{
|
|
|
|
protected $user;
|
|
|
|
protected $posts;
|
2023-07-05 14:54:58 +03:00
|
|
|
protected $notes;
|
2021-10-07 12:28:41 +03:00
|
|
|
|
|
|
|
function __construct(?User $user)
|
|
|
|
{
|
|
|
|
$this->user = $user;
|
|
|
|
$this->posts = new Posts;
|
2023-07-05 14:54:58 +03:00
|
|
|
$this->notes = new Notes;
|
2021-10-07 12:28:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2022-08-20 21:07:54 +03:00
|
|
|
|
|
|
|
function newStatus(string $text, callable $resolve, callable $reject): void
|
|
|
|
{
|
|
|
|
$post = new Post;
|
|
|
|
$post->setOwner($this->user->getId());
|
|
|
|
$post->setWall($this->user->getId());
|
|
|
|
$post->setCreated(time());
|
|
|
|
$post->setContent($text);
|
|
|
|
$post->setAnonymous(false);
|
|
|
|
$post->setFlags(0);
|
|
|
|
$post->setNsfw(false);
|
|
|
|
$post->save();
|
|
|
|
|
|
|
|
$resolve($post->getId());
|
|
|
|
}
|
2023-07-05 14:54:58 +03:00
|
|
|
|
|
|
|
function getMyNotes(callable $resolve, callable $reject)
|
|
|
|
{
|
2023-07-06 11:47:53 +03:00
|
|
|
$count = $this->notes->getUserNotesCount($this->user);
|
|
|
|
$myNotes = $this->notes->getUserNotes($this->user, 1, $count);
|
2023-07-05 14:54:58 +03:00
|
|
|
|
|
|
|
$arr = [
|
2023-07-06 11:47:53 +03:00
|
|
|
"count" => $count,
|
2023-07-05 14:54:58 +03:00
|
|
|
"closed" => $this->user->getPrivacySetting("notes.read"),
|
|
|
|
"items" => [],
|
|
|
|
];
|
|
|
|
|
|
|
|
foreach($myNotes as $note) {
|
|
|
|
$arr["items"][] = [
|
|
|
|
"id" => $note->getId(),
|
|
|
|
"name" => ovk_proc_strtr($note->getName(), 30),
|
|
|
|
#"preview" => $note->getPreview()
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
$resolve($arr);
|
|
|
|
}
|
2021-10-07 12:28:41 +03:00
|
|
|
}
|