2020-06-07 19:04:43 +03:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
namespace openvk\Web\Models\Entities;
|
2021-09-20 15:19:15 +03:00
|
|
|
use Chandler\Database\DatabaseConnection as DB;
|
2022-10-08 23:47:21 +03:00
|
|
|
use openvk\Web\Models\Repositories\{Clubs, Users};
|
2020-06-07 19:04:43 +03:00
|
|
|
use openvk\Web\Models\RowModel;
|
2021-11-13 23:28:17 +03:00
|
|
|
use openvk\Web\Models\Entities\Notifications\LikeNotification;
|
2020-06-07 19:04:43 +03:00
|
|
|
|
|
|
|
class Post extends Postable
|
|
|
|
{
|
|
|
|
protected $tableName = "posts";
|
|
|
|
protected $upperNodeReferenceColumnName = "wall";
|
2021-11-13 23:28:17 +03:00
|
|
|
|
|
|
|
private function setLikeRecursively(bool $liked, User $user, int $depth): void
|
|
|
|
{
|
|
|
|
$searchData = [
|
|
|
|
"origin" => $user->getId(),
|
|
|
|
"model" => static::class,
|
|
|
|
"target" => $this->getRecord()->id,
|
|
|
|
];
|
|
|
|
|
|
|
|
if((sizeof(DB::i()->getContext()->table("likes")->where($searchData)) > 0) !== $liked) {
|
2021-11-29 14:23:17 +03:00
|
|
|
if($this->getOwner(false)->getId() !== $user->getId() && !($this->getOwner() instanceof Club) && !$this instanceof Comment)
|
2021-11-13 23:28:17 +03:00
|
|
|
(new LikeNotification($this->getOwner(false), $this, $user))->emit();
|
|
|
|
|
|
|
|
parent::setLike($liked, $user);
|
|
|
|
}
|
|
|
|
|
|
|
|
if($depth < ovkGetQuirk("wall.repost-liking-recursion-limit"))
|
|
|
|
foreach($this->getChildren() as $attachment)
|
|
|
|
if($attachment instanceof Post)
|
|
|
|
$attachment->setLikeRecursively($liked, $user, $depth + 1);
|
|
|
|
}
|
2020-06-07 19:04:43 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* May return fake owner (group), if flags are [1, (*)]
|
|
|
|
*
|
|
|
|
* @param bool $honourFlags - check flags
|
|
|
|
*/
|
2021-11-15 22:45:48 +03:00
|
|
|
function getOwner(bool $honourFlags = true, bool $real = false): RowModel
|
2020-06-07 19:04:43 +03:00
|
|
|
{
|
2021-11-24 22:24:44 +03:00
|
|
|
if($honourFlags && $this->isPostedOnBehalfOfGroup()) {
|
2020-06-07 19:04:43 +03:00
|
|
|
if($this->getRecord()->wall < 0)
|
|
|
|
return (new Clubs)->get(abs($this->getRecord()->wall));
|
|
|
|
}
|
|
|
|
|
2021-11-15 22:45:48 +03:00
|
|
|
return parent::getOwner($real);
|
2020-06-07 19:04:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function getPrettyId(): string
|
|
|
|
{
|
|
|
|
return $this->getRecord()->wall . "_" . $this->getVirtualId();
|
|
|
|
}
|
|
|
|
|
|
|
|
function getTargetWall(): int
|
|
|
|
{
|
|
|
|
return $this->getRecord()->wall;
|
|
|
|
}
|
2022-10-08 23:47:21 +03:00
|
|
|
|
|
|
|
function getWallOwner()
|
|
|
|
{
|
|
|
|
$w = $this->getRecord()->wall;
|
|
|
|
if($w < 0)
|
|
|
|
return (new Clubs)->get(abs($w));
|
|
|
|
|
|
|
|
return (new Users)->get($w);
|
|
|
|
}
|
2020-06-07 19:04:43 +03:00
|
|
|
|
|
|
|
function getRepostCount(): int
|
|
|
|
{
|
|
|
|
return sizeof(
|
|
|
|
$this->getRecord()
|
|
|
|
->related("attachments.attachable_id")
|
|
|
|
->where("attachable_type", get_class($this))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-09-20 15:19:15 +03:00
|
|
|
function isPinned(): bool
|
|
|
|
{
|
|
|
|
return (bool) $this->getRecord()->pinned;
|
|
|
|
}
|
|
|
|
|
2020-06-07 19:04:43 +03:00
|
|
|
function isAd(): bool
|
|
|
|
{
|
|
|
|
return (bool) $this->getRecord()->ad;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isPostedOnBehalfOfGroup(): bool
|
|
|
|
{
|
|
|
|
return ($this->getRecord()->flags & 0b10000000) > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isSigned(): bool
|
|
|
|
{
|
|
|
|
return ($this->getRecord()->flags & 0b01000000) > 0;
|
|
|
|
}
|
2022-08-05 23:00:52 +03:00
|
|
|
|
|
|
|
function isDeactivationMessage(): bool
|
|
|
|
{
|
|
|
|
return ($this->getRecord()->flags & 0b00100000) > 0;
|
|
|
|
}
|
2020-06-07 19:04:43 +03:00
|
|
|
|
|
|
|
function isExplicit(): bool
|
|
|
|
{
|
2021-01-07 19:19:36 +03:00
|
|
|
return (bool) $this->getRecord()->nsfw;
|
2020-06-07 19:04:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function isDeleted(): bool
|
|
|
|
{
|
|
|
|
return (bool) $this->getRecord()->deleted;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getOwnerPost(): int
|
|
|
|
{
|
2021-11-17 19:27:11 +03:00
|
|
|
return $this->getOwner(false)->getId();
|
2020-06-07 19:04:43 +03:00
|
|
|
}
|
|
|
|
|
2021-09-20 15:19:15 +03:00
|
|
|
function pin(): void
|
|
|
|
{
|
|
|
|
DB::i()
|
|
|
|
->getContext()
|
|
|
|
->table("posts")
|
|
|
|
->where([
|
|
|
|
"wall" => $this->getTargetWall(),
|
|
|
|
"pinned" => true,
|
|
|
|
])
|
|
|
|
->update(["pinned" => false]);
|
|
|
|
|
|
|
|
$this->stateChanges("pinned", true);
|
|
|
|
$this->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
function unpin(): void
|
|
|
|
{
|
|
|
|
$this->stateChanges("pinned", false);
|
|
|
|
$this->save();
|
|
|
|
}
|
|
|
|
|
2021-09-20 16:46:55 +03:00
|
|
|
function canBePinnedBy(User $user): bool
|
2020-06-18 14:22:32 +03:00
|
|
|
{
|
|
|
|
if($this->getTargetWall() < 0)
|
2021-09-20 16:46:55 +03:00
|
|
|
return (new Clubs)->get(abs($this->getTargetWall()))->canBeModifiedBy($user);
|
2020-06-18 14:22:32 +03:00
|
|
|
|
2021-09-20 16:46:55 +03:00
|
|
|
return $this->getTargetWall() === $user->getId();
|
|
|
|
}
|
|
|
|
|
|
|
|
function canBeDeletedBy(User $user): bool
|
|
|
|
{
|
|
|
|
return $this->getOwnerPost() === $user->getId() || $this->canBePinnedBy($user);
|
2020-06-18 14:22:32 +03:00
|
|
|
}
|
|
|
|
|
2020-06-07 19:04:43 +03:00
|
|
|
function setContent(string $content): void
|
|
|
|
{
|
|
|
|
if(ctype_space($content))
|
|
|
|
throw new \LengthException("Content length must be at least 1 character (not counting whitespaces).");
|
2020-08-20 15:58:40 +03:00
|
|
|
else if(iconv_strlen($content) > OPENVK_ROOT_CONF["openvk"]["preferences"]["wall"]["postSizes"]["maxSize"])
|
|
|
|
throw new \LengthException("Content is too large.");
|
2020-06-07 19:04:43 +03:00
|
|
|
|
|
|
|
$this->stateChanges("content", $content);
|
|
|
|
}
|
2021-11-13 23:28:17 +03:00
|
|
|
|
|
|
|
function toggleLike(User $user): bool
|
|
|
|
{
|
|
|
|
$liked = parent::toggleLike($user);
|
|
|
|
|
2021-11-29 14:23:17 +03:00
|
|
|
if($this->getOwner(false)->getId() !== $user->getId() && !($this->getOwner() instanceof Club) && !$this instanceof Comment)
|
2021-11-13 23:28:17 +03:00
|
|
|
(new LikeNotification($this->getOwner(false), $this, $user))->emit();
|
|
|
|
|
|
|
|
foreach($this->getChildren() as $attachment)
|
|
|
|
if($attachment instanceof Post)
|
|
|
|
$attachment->setLikeRecursively($liked, $user, 2);
|
|
|
|
|
|
|
|
return $liked;
|
|
|
|
}
|
|
|
|
|
|
|
|
function setLike(bool $liked, User $user): void
|
|
|
|
{
|
|
|
|
$this->setLikeRecursively($liked, $user, 1);
|
|
|
|
}
|
2020-06-07 19:04:43 +03:00
|
|
|
|
|
|
|
function deletePost(): void
|
|
|
|
{
|
|
|
|
$this->setDeleted(1);
|
|
|
|
$this->unwire();
|
|
|
|
$this->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
use Traits\TRichText;
|
|
|
|
}
|