2020-06-07 19:04:43 +03:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
namespace openvk\Web\Models\Entities;
|
|
|
|
use openvk\Web\Util\DateTime;
|
|
|
|
use openvk\Web\Models\RowModel;
|
|
|
|
use openvk\Web\Models\Entities\User;
|
|
|
|
use openvk\Web\Models\Repositories\Users;
|
|
|
|
use openvk\Web\Models\Repositories\Clubs;
|
|
|
|
use openvk\Web\Models\Repositories\Comments;
|
|
|
|
use Chandler\Database\DatabaseConnection as DB;
|
|
|
|
use Nette\InvalidStateException as ISE;
|
|
|
|
use Nette\Database\Table\Selection;
|
|
|
|
|
|
|
|
abstract class Postable extends Attachable
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Column name, that references to an object, that
|
|
|
|
* is hieararchically higher than this Postable.
|
|
|
|
*
|
|
|
|
* For example: Images belong to User, but Posts belong to Wall.
|
|
|
|
* Formally users still own posts, but walls also own posts and they are
|
|
|
|
* their direct parent.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $upperNodeReferenceColumnName = "owner";
|
|
|
|
|
|
|
|
private function getTable(): Selection
|
|
|
|
{
|
|
|
|
return DB::i()->getContext()->table($this->tableName);
|
|
|
|
}
|
|
|
|
|
2021-11-15 22:45:48 +03:00
|
|
|
function getOwner(bool $real = false): RowModel
|
2020-06-07 19:04:43 +03:00
|
|
|
{
|
|
|
|
$oid = (int) $this->getRecord()->owner;
|
2021-11-15 22:45:48 +03:00
|
|
|
if(!$real && $this->isAnonymous())
|
2023-11-16 19:44:12 +03:00
|
|
|
$oid = (int) OPENVK_ROOT_CONF["openvk"]["preferences"]["wall"]["anonymousPosting"]["account"];
|
2023-08-11 16:50:19 +03:00
|
|
|
|
|
|
|
$oid = abs($oid);
|
2020-06-07 19:04:43 +03:00
|
|
|
if($oid > 0)
|
|
|
|
return (new Users)->get($oid);
|
|
|
|
else
|
|
|
|
return (new Clubs)->get($oid * -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getVirtualId(): int
|
|
|
|
{
|
|
|
|
return $this->getRecord()->virtual_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getPrettyId(): string
|
|
|
|
{
|
|
|
|
return $this->getRecord()->owner . "_" . $this->getVirtualId();
|
|
|
|
}
|
|
|
|
|
|
|
|
function getPublicationTime(): DateTime
|
|
|
|
{
|
|
|
|
return new DateTime($this->getRecord()->created);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getEditTime(): ?DateTime
|
|
|
|
{
|
|
|
|
$edited = $this->getRecord()->edited;
|
|
|
|
if(is_null($edited)) return NULL;
|
|
|
|
|
|
|
|
return new DateTime($edited);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getComments(int $page, ?int $perPage = NULL): \Traversable
|
|
|
|
{
|
|
|
|
return (new Comments)->getCommentsByTarget($this, $page, $perPage);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getCommentsCount(): int
|
|
|
|
{
|
|
|
|
return (new Comments)->getCommentsCountByTarget($this);
|
|
|
|
}
|
2021-11-28 14:39:42 +03:00
|
|
|
|
|
|
|
function getLastComments(int $count): \Traversable
|
2020-08-01 17:23:08 +03:00
|
|
|
{
|
2021-11-28 14:39:42 +03:00
|
|
|
return (new Comments)->getLastCommentsByTarget($this, $count);
|
2020-08-01 17:23:08 +03:00
|
|
|
}
|
2020-06-07 19:04:43 +03:00
|
|
|
|
|
|
|
function getLikesCount(): int
|
|
|
|
{
|
|
|
|
return sizeof(DB::i()->getContext()->table("likes")->where([
|
|
|
|
"model" => static::class,
|
|
|
|
"target" => $this->getRecord()->id,
|
2023-08-04 15:10:23 +03:00
|
|
|
])->group("origin"));
|
2020-06-07 19:04:43 +03:00
|
|
|
}
|
|
|
|
|
2023-11-14 22:44:39 +03:00
|
|
|
function getLikers(int $page = 1, ?int $perPage = NULL): \Traversable
|
2021-10-07 12:28:41 +03:00
|
|
|
{
|
2023-11-14 22:44:39 +03:00
|
|
|
$perPage ??= OPENVK_DEFAULT_PER_PAGE;
|
|
|
|
|
2021-10-07 12:28:41 +03:00
|
|
|
$sel = DB::i()->getContext()->table("likes")->where([
|
|
|
|
"model" => static::class,
|
|
|
|
"target" => $this->getRecord()->id,
|
2023-11-14 22:44:39 +03:00
|
|
|
])->page($page, $perPage);
|
2021-10-07 12:28:41 +03:00
|
|
|
|
|
|
|
foreach($sel as $like)
|
|
|
|
yield (new Users)->get($like->origin);
|
|
|
|
}
|
|
|
|
|
2021-11-15 22:45:48 +03:00
|
|
|
function isAnonymous(): bool
|
|
|
|
{
|
|
|
|
return (bool) $this->getRecord()->anonymous;
|
|
|
|
}
|
|
|
|
|
2021-11-13 23:28:17 +03:00
|
|
|
function toggleLike(User $user): bool
|
2020-06-07 19:04:43 +03:00
|
|
|
{
|
|
|
|
$searchData = [
|
|
|
|
"origin" => $user->getId(),
|
|
|
|
"model" => static::class,
|
|
|
|
"target" => $this->getRecord()->id,
|
|
|
|
];
|
2021-11-13 23:28:17 +03:00
|
|
|
|
|
|
|
if(sizeof(DB::i()->getContext()->table("likes")->where($searchData)) > 0) {
|
2020-06-07 19:04:43 +03:00
|
|
|
DB::i()->getContext()->table("likes")->where($searchData)->delete();
|
2021-11-13 23:28:17 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
DB::i()->getContext()->table("likes")->insert($searchData);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function setLike(bool $liked, User $user): void
|
|
|
|
{
|
|
|
|
$searchData = [
|
|
|
|
"origin" => $user->getId(),
|
|
|
|
"model" => static::class,
|
|
|
|
"target" => $this->getRecord()->id,
|
|
|
|
];
|
|
|
|
|
2023-11-15 11:41:18 +03:00
|
|
|
if($liked) {
|
|
|
|
if(!$this->hasLikeFrom($user)) {
|
|
|
|
DB::i()->getContext()->table("likes")->insert($searchData);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if($this->hasLikeFrom($user)) {
|
|
|
|
DB::i()->getContext()->table("likes")->where($searchData)->delete();
|
|
|
|
}
|
|
|
|
}
|
2020-06-07 19:04:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function hasLikeFrom(User $user): bool
|
|
|
|
{
|
|
|
|
$searchData = [
|
|
|
|
"origin" => $user->getId(),
|
|
|
|
"model" => static::class,
|
|
|
|
"target" => $this->getRecord()->id,
|
|
|
|
];
|
|
|
|
|
|
|
|
return sizeof(DB::i()->getContext()->table("likes")->where($searchData)) > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
function setVirtual_Id(int $id): void
|
|
|
|
{
|
|
|
|
throw new ISE("Setting virtual id manually is forbidden");
|
|
|
|
}
|
|
|
|
|
2023-10-03 19:40:13 +03:00
|
|
|
function save(?bool $log = false): void
|
2020-06-07 19:04:43 +03:00
|
|
|
{
|
|
|
|
$vref = $this->upperNodeReferenceColumnName;
|
|
|
|
|
|
|
|
$vid = $this->getRecord()->{$vref} ?? $this->changes[$vref];
|
|
|
|
if(!$vid)
|
|
|
|
throw new ISE("Can't presist post due to inability to calculate it's $vref post count. Have you set it?");
|
|
|
|
|
|
|
|
$pCount = sizeof($this->getTable()->where($vref, $vid));
|
|
|
|
if(is_null($this->getRecord())) {
|
|
|
|
# lol allow ppl to taint created value
|
|
|
|
if(!isset($this->changes["created"]))
|
|
|
|
$this->stateChanges("created", time());
|
|
|
|
|
|
|
|
$this->stateChanges("virtual_id", $pCount + 1);
|
2023-09-16 19:14:23 +03:00
|
|
|
} /*else {
|
2020-06-07 19:04:43 +03:00
|
|
|
$this->stateChanges("edited", time());
|
2023-09-16 19:14:23 +03:00
|
|
|
}*/
|
2020-06-07 19:04:43 +03:00
|
|
|
|
2023-10-03 19:40:13 +03:00
|
|
|
parent::save($log);
|
2020-06-07 19:04:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
use Traits\TAttachmentHost;
|
|
|
|
use Traits\TOwnable;
|
|
|
|
}
|