openvk/ServiceAPI/Wall.php

164 lines
5 KiB
PHP
Raw Normal View History

<?php declare(strict_types=1);
namespace openvk\ServiceAPI;
2022-08-20 21:07:54 +03:00
use openvk\Web\Models\Entities\Post;
use openvk\Web\Models\Entities\User;
use openvk\Web\Models\Entities\Notifications\PostAcceptedNotification;
use openvk\Web\Models\Repositories\{Posts, Notes};
class Wall implements Handler
{
protected $user;
protected $posts;
protected $notes;
function __construct(?User $user)
{
$this->user = $user;
$this->posts = new Posts;
$this->notes = new Notes;
}
function getPost(int $id, callable $resolve, callable $reject): void
{
$post = $this->posts->get($id);
if(!$post || $post->isDeleted())
$reject(53, "No post with id=$id");
if($post->getSuggestionType() != 0)
$reject(25, "Can't get suggested post");
$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());
}
function getMyNotes(callable $resolve, callable $reject)
{
$count = $this->notes->getUserNotesCount($this->user);
$myNotes = $this->notes->getUserNotes($this->user, 1, $count);
$arr = [
"count" => $count,
"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);
}
2023-07-30 20:03:27 +03:00
function declinePost(int $id, callable $resolve, callable $reject)
{
$post = $this->posts->get($id);
if(!$post || $post->isDeleted())
$reject(11, "No post with id=$id");
if($post->getSuggestionType() == 0)
$reject(19, "Post is not suggested");
if($post->getSuggestionType() == 2)
$reject(10, "Post is already declined");
if(!$post->canBePinnedBy($this->user))
2023-07-31 10:00:55 +03:00
$reject(22, "Access to post denied");
2023-07-30 20:03:27 +03:00
$post->setSuggested(2);
2023-08-02 12:59:29 +03:00
$post->setDeleted(1);
2023-07-30 20:03:27 +03:00
$post->save();
$resolve($this->posts->getSuggestedPostsCount($post->getWallOwner()->getId()));
}
function acceptPost(int $id, bool $sign, string $content, callable $resolve, callable $reject)
{
$post = $this->posts->get($id);
if(!$post || $post->isDeleted())
$reject(11, "No post with id=$id");
if($post->getSuggestionType() == 0)
$reject(19, "Post is not suggested");
if($post->getSuggestionType() == 2)
$reject(10, "Post is declined");
if(!$post->canBePinnedBy($this->user))
2023-07-31 10:00:55 +03:00
$reject(22, "Access to post denied");
2023-07-30 20:03:27 +03:00
$author = $post->getOwner();
$flags = 0;
$flags |= 0b10000000;
if($sign) {
$flags |= 0b01000000;
}
$post->setSuggested(0);
$post->setCreated(time());
$post->setFlags($flags);
2023-07-31 10:00:55 +03:00
if(mb_strlen($content) > 0) {
$post->setContent($content);
}
2023-07-30 20:03:27 +03:00
$post->save();
if($author->getId() != $this->user->getId())
(new PostAcceptedNotification($author, $post, $post->getWallOwner()))->emit();
2023-07-30 20:03:27 +03:00
$resolve(["id" => $post->getPrettyId(), "new_count" => $this->posts->getSuggestedPostsCount($post->getWallOwner()->getId())]);
}
}