openvk/Web/Models/Entities/Message.php

162 lines
4.3 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
2020-06-07 19:04:43 +03:00
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;
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
{
use Traits\TRichText;
use Traits\TAttachmentHost;
2020-06-07 19:04:43 +03:00
protected $tableName = "messages";
2020-06-07 19:04:43 +03:00
/**
* Get origin of the message.
*
2020-06-07 19:04:43 +03:00
* Returns either user or club.
*
2020-06-07 19:04:43 +03:00
* @returns User|Club
*/
public function getSender(): ?RowModel
2020-06-07 19:04:43 +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
}
2020-06-07 19:04:43 +03:00
/**
* Get the destination of the message.
*
2020-06-07 19:04:43 +03:00
* Returns either user or club.
*
2020-06-07 19:04:43 +03:00
* @returns User|Club
*/
public function getRecipient(): ?RowModel
2020-06-07 19:04:43 +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
}
public function getUnreadState(): int
2021-01-27 20:59:11 +03:00
{
trigger_error("TODO: use isUnread", E_USER_DEPRECATED);
2021-01-27 20:59:11 +03:00
return (int) $this->isUnread();
}
2020-06-07 19:04:43 +03:00
/**
* Get date of initial publication.
*
2020-06-07 19:04:43 +03:00
* @returns DateTime
*/
public function getSendTime(): DateTime
2020-06-07 19:04:43 +03:00
{
return new DateTime($this->getRecord()->created);
}
public function getSendTimeHumanized(): string
{
$dateTime = new DateTime($this->getRecord()->created);
if ($dateTime->format("%d.%m.%y") == ovk_strftime_safe("%d.%m.%y", time())) {
return $dateTime->format("%T");
} else {
return $dateTime->format("%d.%m.%y");
}
}
2020-06-07 19:04:43 +03:00
/**
* Get date of last edit, if any edits were made, otherwise null.
*
2020-06-07 19:04:43 +03:00
* @returns DateTime|null
*/
public function getEditTime(): ?DateTime
2020-06-07 19:04:43 +03:00
{
$edited = $this->getRecord()->edited;
if (is_null($edited)) {
return null;
}
2020-06-07 19:04:43 +03:00
return new DateTime($edited);
}
2020-06-07 19:04:43 +03:00
/**
* Is this message an ad?
*
2020-06-07 19:04:43 +03:00
* Messages can never be ads.
*
2020-06-07 19:04:43 +03:00
* @returns false
*/
public function isAd(): bool
2020-06-07 19:04:43 +03:00
{
return false;
}
public function isUnread(): bool
2021-01-27 20:59:11 +03:00
{
return (bool) $this->getRecord()->unread;
}
2020-06-07 19:04:43 +03:00
/**
* Simplify to array
*
2020-06-07 19:04:43 +03:00
* @returns array
*/
public function simplify(): array
2020-06-07 19:04:43 +03:00
{
$author = $this->getSender();
$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 {
$attachments[] = [
"type" => "unknown",
];
# 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(),
"name" => $author->getFirstName() . $unreadmsg,
2020-06-07 19:04:43 +03:00
],
"timing" => [
"sent" => (string) $this->getSendTimeHumanized(),
"edited" => is_null($this->getEditTime()) ? null : (string) $this->getEditTime(),
2020-06-07 19:04:43 +03:00
],
"text" => $this->getText(),
2021-01-27 20:59:11 +03:00
"read" => !$this->isUnread(),
"attachments" => $attachments,
2020-06-07 19:04:43 +03:00
];
}
}