2025-01-31 18:20:13 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-06-07 19:04:43 +03:00
|
|
|
namespace openvk\Web\Models\Entities;
|
2025-01-31 18:20:13 +03:00
|
|
|
|
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
|
|
|
|
{
|
2025-01-31 18:20:13 +03:00
|
|
|
use Traits\TRichText;
|
|
|
|
use Traits\TAttachmentHost;
|
2020-06-07 19:04:43 +03:00
|
|
|
protected $tableName = "messages";
|
2025-01-31 18:20:13 +03:00
|
|
|
|
2020-06-07 19:04:43 +03:00
|
|
|
/**
|
|
|
|
* Get origin of the message.
|
2025-01-31 18:20:13 +03:00
|
|
|
*
|
2020-06-07 19:04:43 +03:00
|
|
|
* Returns either user or club.
|
2025-01-31 18:20:13 +03:00
|
|
|
*
|
2020-06-07 19:04:43 +03:00
|
|
|
* @returns User|Club
|
|
|
|
*/
|
2025-01-31 18:20:13 +03:00
|
|
|
public function getSender(): ?RowModel
|
2020-06-07 19:04:43 +03:00
|
|
|
{
|
2025-01-31 18:20:13 +03:00
|
|
|
if ($this->getRecord()->sender_type === 'openvk\Web\Models\Entities\User') {
|
|
|
|
return (new Users())->get($this->getRecord()->sender_id);
|
|
|
|
} elseif ($this->getRecord()->sender_type === 'openvk\Web\Models\Entities\Club') {
|
|
|
|
return (new Clubs())->get($this->getRecord()->sender_id);
|
|
|
|
}
|
2020-06-07 19:04:43 +03:00
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
2020-06-07 19:04:43 +03:00
|
|
|
/**
|
|
|
|
* Get the destination of the message.
|
2025-01-31 18:20:13 +03:00
|
|
|
*
|
2020-06-07 19:04:43 +03:00
|
|
|
* Returns either user or club.
|
2025-01-31 18:20:13 +03:00
|
|
|
*
|
2020-06-07 19:04:43 +03:00
|
|
|
* @returns User|Club
|
|
|
|
*/
|
2025-01-31 18:20:13 +03:00
|
|
|
public function getRecipient(): ?RowModel
|
2020-06-07 19:04:43 +03:00
|
|
|
{
|
2025-01-31 18:20:13 +03:00
|
|
|
if ($this->getRecord()->recipient_type === 'openvk\Web\Models\Entities\User') {
|
|
|
|
return (new Users())->get($this->getRecord()->recipient_id);
|
|
|
|
} elseif ($this->getRecord()->recipient_type === 'openvk\Web\Models\Entities\Club') {
|
|
|
|
return (new Clubs())->get($this->getRecord()->recipient_id);
|
|
|
|
}
|
2020-06-07 19:04:43 +03:00
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function getUnreadState(): int
|
2021-01-27 20:59:11 +03:00
|
|
|
{
|
|
|
|
trigger_error("TODO: use isUnread", E_USER_DEPRECATED);
|
2025-01-31 18:20:13 +03:00
|
|
|
|
2021-01-27 20:59:11 +03:00
|
|
|
return (int) $this->isUnread();
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
2020-06-07 19:04:43 +03:00
|
|
|
/**
|
|
|
|
* Get date of initial publication.
|
2025-01-31 18:20:13 +03:00
|
|
|
*
|
2020-06-07 19:04:43 +03:00
|
|
|
* @returns DateTime
|
|
|
|
*/
|
2025-01-31 18:20:13 +03:00
|
|
|
public function getSendTime(): DateTime
|
2020-06-07 19:04:43 +03:00
|
|
|
{
|
|
|
|
return new DateTime($this->getRecord()->created);
|
|
|
|
}
|
2022-05-31 17:39:52 +03:00
|
|
|
|
2025-01-31 18:20:13 +03:00
|
|
|
public function getSendTimeHumanized(): string
|
2022-05-31 17:39:52 +03:00
|
|
|
{
|
|
|
|
$dateTime = new DateTime($this->getRecord()->created);
|
|
|
|
|
2025-01-31 18:20:13 +03:00
|
|
|
if ($dateTime->format("%d.%m.%y") == ovk_strftime_safe("%d.%m.%y", time())) {
|
2024-01-02 23:22:10 +03:00
|
|
|
return $dateTime->format("%T");
|
2022-05-31 17:39:52 +03:00
|
|
|
} else {
|
|
|
|
return $dateTime->format("%d.%m.%y");
|
|
|
|
}
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
2020-06-07 19:04:43 +03:00
|
|
|
/**
|
|
|
|
* Get date of last edit, if any edits were made, otherwise null.
|
2025-01-31 18:20:13 +03:00
|
|
|
*
|
2020-06-07 19:04:43 +03:00
|
|
|
* @returns DateTime|null
|
|
|
|
*/
|
2025-01-31 18:20:13 +03:00
|
|
|
public function getEditTime(): ?DateTime
|
2020-06-07 19:04:43 +03:00
|
|
|
{
|
|
|
|
$edited = $this->getRecord()->edited;
|
2025-01-31 18:20:13 +03:00
|
|
|
if (is_null($edited)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-06-07 19:04:43 +03:00
|
|
|
return new DateTime($edited);
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
2020-06-07 19:04:43 +03:00
|
|
|
/**
|
|
|
|
* Is this message an ad?
|
2025-01-31 18:20:13 +03:00
|
|
|
*
|
2020-06-07 19:04:43 +03:00
|
|
|
* Messages can never be ads.
|
2025-01-31 18:20:13 +03:00
|
|
|
*
|
2020-06-07 19:04:43 +03:00
|
|
|
* @returns false
|
|
|
|
*/
|
2025-01-31 18:20:13 +03:00
|
|
|
public function isAd(): bool
|
2020-06-07 19:04:43 +03:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function isUnread(): bool
|
2021-01-27 20:59:11 +03:00
|
|
|
{
|
|
|
|
return (bool) $this->getRecord()->unread;
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
2020-06-07 19:04:43 +03:00
|
|
|
/**
|
|
|
|
* Simplify to array
|
2025-01-31 18:20:13 +03:00
|
|
|
*
|
2020-06-07 19:04:43 +03:00
|
|
|
* @returns array
|
|
|
|
*/
|
2025-01-31 18:20:13 +03:00
|
|
|
public function simplify(): array
|
2020-06-07 19:04:43 +03:00
|
|
|
{
|
|
|
|
$author = $this->getSender();
|
2025-01-31 18:20:13 +03:00
|
|
|
|
2020-06-11 12:55:43 +03:00
|
|
|
$attachments = [];
|
2025-01-31 18:20:13 +03:00
|
|
|
foreach ($this->getChildren() as $attachment) {
|
|
|
|
if ($attachment instanceof Photo) {
|
2020-06-11 12:55:43 +03:00
|
|
|
$attachments[] = [
|
|
|
|
"type" => "photo",
|
|
|
|
"link" => "/photo" . $attachment->getPrettyId(),
|
|
|
|
"photo" => [
|
|
|
|
"url" => $attachment->getURL(),
|
|
|
|
"caption" => $attachment->getDescription(),
|
|
|
|
],
|
|
|
|
];
|
|
|
|
} else {
|
2023-11-14 22:44:39 +03:00
|
|
|
$attachments[] = [
|
2025-01-31 18:20:13 +03:00
|
|
|
"type" => "unknown",
|
2023-11-14 22:44:39 +03:00
|
|
|
];
|
2025-01-31 18:20:13 +03:00
|
|
|
|
2023-11-14 22:44:39 +03:00
|
|
|
# throw new \Exception("Unknown attachment type: " . get_class($attachment));
|
2020-06-11 12:55:43 +03:00
|
|
|
}
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
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(),
|
2025-01-31 18:20:13 +03:00
|
|
|
"name" => $author->getFirstName() . $unreadmsg,
|
2020-06-07 19:04:43 +03:00
|
|
|
],
|
|
|
|
"timing" => [
|
2022-05-31 17:39:52 +03:00
|
|
|
"sent" => (string) $this->getSendTimeHumanized(),
|
2025-01-31 18:20:13 +03:00
|
|
|
"edited" => is_null($this->getEditTime()) ? null : (string) $this->getEditTime(),
|
2020-06-07 19:04:43 +03:00
|
|
|
],
|
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
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|