2020-06-07 19:04:43 +03:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
namespace openvk\Web\Models\Entities;
|
2021-01-27 20:59:11 +03:00
|
|
|
use Chandler\Database\DatabaseConnection;
|
2020-06-07 19:04:43 +03:00
|
|
|
use openvk\Web\Models\Repositories\Clubs;
|
|
|
|
use openvk\Web\Models\Repositories\Users;
|
2020-06-11 12:55:43 +03:00
|
|
|
use openvk\Web\Models\Entities\Photo;
|
2020-06-07 19:04:43 +03:00
|
|
|
use openvk\Web\Models\RowModel;
|
|
|
|
use openvk\Web\Util\DateTime;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Message entity.
|
|
|
|
*/
|
|
|
|
class Message extends RowModel
|
|
|
|
{
|
|
|
|
protected $tableName = "messages";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get origin of the message.
|
|
|
|
*
|
|
|
|
* Returns either user or club.
|
|
|
|
*
|
|
|
|
* @returns User|Club
|
|
|
|
*/
|
|
|
|
function getSender(): ?RowModel
|
|
|
|
{
|
|
|
|
if($this->getRecord()->sender_type === 'openvk\Web\Models\Entities\User')
|
|
|
|
return (new Users)->get($this->getRecord()->sender_id);
|
|
|
|
else if($this->getRecord()->sender_type === 'openvk\Web\Models\Entities\Club')
|
|
|
|
return (new Clubs)->get($this->getRecord()->sender_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the destination of the message.
|
|
|
|
*
|
|
|
|
* Returns either user or club.
|
|
|
|
*
|
|
|
|
* @returns User|Club
|
|
|
|
*/
|
|
|
|
function getRecipient(): ?RowModel
|
|
|
|
{
|
|
|
|
if($this->getRecord()->recipient_type === 'openvk\Web\Models\Entities\User')
|
|
|
|
return (new Users)->get($this->getRecord()->recipient_id);
|
|
|
|
else if($this->getRecord()->recipient_type === 'openvk\Web\Models\Entities\Club')
|
|
|
|
return (new Clubs)->get($this->getRecord()->recipient_id);
|
|
|
|
}
|
|
|
|
|
2021-01-27 20:59:11 +03:00
|
|
|
function getUnreadState(): int
|
|
|
|
{
|
|
|
|
trigger_error("TODO: use isUnread", E_USER_DEPRECATED);
|
|
|
|
|
|
|
|
return (int) $this->isUnread();
|
|
|
|
}
|
|
|
|
|
2020-06-07 19:04:43 +03:00
|
|
|
/**
|
|
|
|
* Get date of initial publication.
|
|
|
|
*
|
|
|
|
* @returns DateTime
|
|
|
|
*/
|
|
|
|
function getSendTime(): DateTime
|
|
|
|
{
|
|
|
|
return new DateTime($this->getRecord()->created);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get date of last edit, if any edits were made, otherwise null.
|
|
|
|
*
|
|
|
|
* @returns DateTime|null
|
|
|
|
*/
|
|
|
|
function getEditTime(): ?DateTime
|
|
|
|
{
|
|
|
|
$edited = $this->getRecord()->edited;
|
|
|
|
if(is_null($edited)) return NULL;
|
|
|
|
|
|
|
|
return new DateTime($edited);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Is this message an ad?
|
|
|
|
*
|
|
|
|
* Messages can never be ads.
|
|
|
|
*
|
|
|
|
* @returns false
|
|
|
|
*/
|
|
|
|
function isAd(): bool
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-01-27 20:59:11 +03:00
|
|
|
function isUnread(): bool
|
|
|
|
{
|
|
|
|
return (bool) $this->getRecord()->unread;
|
|
|
|
}
|
|
|
|
|
2020-06-07 19:04:43 +03:00
|
|
|
/**
|
|
|
|
* Simplify to array
|
|
|
|
*
|
|
|
|
* @returns array
|
|
|
|
*/
|
|
|
|
function simplify(): array
|
|
|
|
{
|
|
|
|
$author = $this->getSender();
|
|
|
|
|
2020-06-11 12:55:43 +03:00
|
|
|
$attachments = [];
|
|
|
|
foreach($this->getChildren() as $attachment) {
|
|
|
|
if($attachment instanceof Photo) {
|
|
|
|
$attachments[] = [
|
|
|
|
"type" => "photo",
|
|
|
|
"link" => "/photo" . $attachment->getPrettyId(),
|
|
|
|
"photo" => [
|
|
|
|
"url" => $attachment->getURL(),
|
|
|
|
"caption" => $attachment->getDescription(),
|
|
|
|
],
|
|
|
|
];
|
|
|
|
} else {
|
|
|
|
throw new \Exception("Unknown attachment type: " . get_class($attachment));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-07 19:04:43 +03:00
|
|
|
return [
|
|
|
|
"uuid" => $this->getId(),
|
|
|
|
"sender" => [
|
|
|
|
"id" => $author->getId(),
|
|
|
|
"link" => $_SERVER['REQUEST_SCHEME'] . "://" . $_SERVER['HTTP_HOST'] . $author->getURL(),
|
|
|
|
"avatar" => $author->getAvatarUrl(),
|
2021-01-27 20:59:11 +03:00
|
|
|
"name" => $author->getFirstName().$unreadmsg,
|
2020-06-07 19:04:43 +03:00
|
|
|
],
|
|
|
|
"timing" => [
|
|
|
|
"sent" => (string) $this->getSendTime()->format("%e %B %G" . tr("time_at_sp") . "%X"),
|
|
|
|
"edited" => is_null($this->getEditTime()) ? null : (string) $this->getEditTime(),
|
|
|
|
],
|
2020-06-11 12:55:43 +03:00
|
|
|
"text" => $this->getText(),
|
2021-01-27 20:59:11 +03:00
|
|
|
"read" => !$this->isUnread(),
|
2020-06-11 12:55:43 +03:00
|
|
|
"attachments" => $attachments,
|
2020-06-07 19:04:43 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
use Traits\TRichText;
|
2020-06-11 12:55:43 +03:00
|
|
|
use Traits\TAttachmentHost;
|
2020-06-07 19:04:43 +03:00
|
|
|
}
|