2021-04-02 17:20:02 +00:00
|
|
|
<?php
|
|
|
|
|
2021-06-08 09:52:11 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2021-04-02 17:20:02 +00:00
|
|
|
/**
|
|
|
|
* @copyright 2021 Podlibre
|
|
|
|
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
|
|
|
|
* @link https://castopod.org/
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace ActivityPub\Entities;
|
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
use CodeIgniter\I18n\Time;
|
2021-04-02 17:20:02 +00:00
|
|
|
use Michalsn\Uuid\UuidEntity;
|
2021-05-19 16:35:13 +00:00
|
|
|
use RuntimeException;
|
2021-04-02 17:20:02 +00:00
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
/**
|
|
|
|
* @property string $id
|
|
|
|
* @property string $uri
|
|
|
|
* @property int $actor_id
|
2021-06-08 09:52:11 +00:00
|
|
|
* @property Actor|null $actor
|
2021-05-12 14:00:25 +00:00
|
|
|
* @property string|null $in_reply_to_id
|
|
|
|
* @property bool $is_reply
|
|
|
|
* @property Note|null $reply_to_note
|
|
|
|
* @property string|null $reblog_of_id
|
|
|
|
* @property bool $is_reblog
|
|
|
|
* @property Note|null $reblog_of_note
|
|
|
|
* @property string $message
|
|
|
|
* @property string $message_html
|
|
|
|
* @property int $favourites_count
|
|
|
|
* @property int $reblogs_count
|
|
|
|
* @property int $replies_count
|
|
|
|
* @property Time $published_at
|
|
|
|
* @property Time $created_at
|
|
|
|
*
|
|
|
|
* @property bool $has_preview_card
|
|
|
|
* @property PreviewCard|null $preview_card
|
|
|
|
*
|
|
|
|
* @property bool $has_replies
|
|
|
|
* @property Note[] $replies
|
|
|
|
* @property Note[] $reblogs
|
|
|
|
*/
|
2021-04-02 17:20:02 +00:00
|
|
|
class Note extends UuidEntity
|
|
|
|
{
|
2021-05-18 17:16:36 +00:00
|
|
|
protected ?Actor $actor = null;
|
2021-05-19 16:35:13 +00:00
|
|
|
|
2021-05-18 17:16:36 +00:00
|
|
|
protected bool $is_reply = false;
|
2021-05-19 16:35:13 +00:00
|
|
|
|
2021-05-18 17:16:36 +00:00
|
|
|
protected ?Note $reply_to_note = null;
|
2021-05-19 16:35:13 +00:00
|
|
|
|
2021-05-18 17:16:36 +00:00
|
|
|
protected bool $is_reblog = false;
|
2021-05-19 16:35:13 +00:00
|
|
|
|
2021-05-18 17:16:36 +00:00
|
|
|
protected ?Note $reblog_of_note = null;
|
2021-05-19 16:35:13 +00:00
|
|
|
|
2021-05-18 17:16:36 +00:00
|
|
|
protected ?PreviewCard $preview_card = null;
|
2021-05-19 16:35:13 +00:00
|
|
|
|
2021-05-18 17:16:36 +00:00
|
|
|
protected bool $has_preview_card = false;
|
2021-04-02 17:20:02 +00:00
|
|
|
|
|
|
|
/**
|
2021-05-18 17:16:36 +00:00
|
|
|
* @var Note[]|null
|
2021-04-02 17:20:02 +00:00
|
|
|
*/
|
2021-05-18 17:16:36 +00:00
|
|
|
protected ?array $replies = null;
|
2021-04-02 17:20:02 +00:00
|
|
|
|
2021-05-18 17:16:36 +00:00
|
|
|
protected bool $has_replies = false;
|
2021-04-22 17:20:28 +00:00
|
|
|
|
2021-04-02 17:20:02 +00:00
|
|
|
/**
|
2021-05-18 17:16:36 +00:00
|
|
|
* @var Note[]|null
|
2021-04-02 17:20:02 +00:00
|
|
|
*/
|
2021-05-18 17:16:36 +00:00
|
|
|
protected ?array $reblogs = null;
|
2021-04-02 17:20:02 +00:00
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
protected $uuids = ['id', 'in_reply_to_id', 'reblog_of_id'];
|
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
2021-04-02 17:20:02 +00:00
|
|
|
protected $dates = ['published_at', 'created_at'];
|
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
/**
|
|
|
|
* @var array<string, string>
|
|
|
|
*/
|
2021-04-02 17:20:02 +00:00
|
|
|
protected $casts = [
|
|
|
|
'id' => 'string',
|
|
|
|
'uri' => 'string',
|
|
|
|
'actor_id' => 'integer',
|
|
|
|
'in_reply_to_id' => '?string',
|
|
|
|
'reblog_of_id' => '?string',
|
|
|
|
'message' => 'string',
|
|
|
|
'message_html' => 'string',
|
|
|
|
'favourites_count' => 'integer',
|
|
|
|
'reblogs_count' => 'integer',
|
|
|
|
'replies_count' => 'integer',
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the note's actor
|
|
|
|
*/
|
2021-06-08 09:52:11 +00:00
|
|
|
public function getActor(): ?Actor
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
2021-05-18 17:16:36 +00:00
|
|
|
if ($this->actor_id === null) {
|
2021-06-08 09:52:11 +00:00
|
|
|
throw new RuntimeException('Note must have an actor_id before getting actor.');
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
|
2021-05-18 17:16:36 +00:00
|
|
|
if ($this->actor === null) {
|
2021-05-19 16:35:13 +00:00
|
|
|
$this->actor = model('ActorModel')
|
|
|
|
->getActorById($this->actor_id);
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->actor;
|
|
|
|
}
|
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
public function getPreviewCard(): ?PreviewCard
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
2021-05-18 17:16:36 +00:00
|
|
|
if ($this->id === null) {
|
2021-06-08 09:52:11 +00:00
|
|
|
throw new RuntimeException('Note must be created before getting preview_card.');
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
|
2021-05-18 17:16:36 +00:00
|
|
|
if ($this->preview_card === null) {
|
2021-05-19 16:35:13 +00:00
|
|
|
$this->preview_card = model('PreviewCardModel')
|
2021-06-08 09:52:11 +00:00
|
|
|
->getNotePreviewCard($this->id);
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->preview_card;
|
|
|
|
}
|
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
public function getHasPreviewCard(): bool
|
2021-04-22 17:20:28 +00:00
|
|
|
{
|
2021-05-18 17:16:36 +00:00
|
|
|
return $this->getPreviewCard() !== null;
|
2021-04-22 17:20:28 +00:00
|
|
|
}
|
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
public function getIsReply(): bool
|
2021-04-22 17:20:28 +00:00
|
|
|
{
|
2021-06-08 09:52:11 +00:00
|
|
|
return $this->in_reply_to_id && ($this->getReplyToNote() !== null);
|
2021-04-22 17:20:28 +00:00
|
|
|
}
|
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
/**
|
|
|
|
* @return Note[]
|
|
|
|
*/
|
|
|
|
public function getReplies(): array
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
2021-05-18 17:16:36 +00:00
|
|
|
if ($this->id === null) {
|
2021-06-08 09:52:11 +00:00
|
|
|
throw new RuntimeException('Note must be created before getting replies.');
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
|
2021-05-18 17:16:36 +00:00
|
|
|
if ($this->replies === null) {
|
2021-05-19 16:35:13 +00:00
|
|
|
$this->replies = (array) model('NoteModel')
|
2021-06-08 09:52:11 +00:00
|
|
|
->getNoteReplies($this->id);
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->replies;
|
|
|
|
}
|
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
public function getHasReplies(): bool
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
2021-05-18 17:16:36 +00:00
|
|
|
return $this->getReplies() !== null;
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 09:52:11 +00:00
|
|
|
public function getReplyToNote(): ?self
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
2021-05-18 17:16:36 +00:00
|
|
|
if ($this->in_reply_to_id === null) {
|
2021-05-06 14:00:48 +00:00
|
|
|
throw new RuntimeException('Note is not a reply.');
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
|
2021-05-18 17:16:36 +00:00
|
|
|
if ($this->reply_to_note === null) {
|
2021-05-19 16:35:13 +00:00
|
|
|
$this->reply_to_note = model('NoteModel')
|
2021-06-08 09:52:11 +00:00
|
|
|
->getNoteById($this->in_reply_to_id);
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->reply_to_note;
|
|
|
|
}
|
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
/**
|
|
|
|
* @return Note[]
|
|
|
|
*/
|
|
|
|
public function getReblogs(): array
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
2021-05-18 17:16:36 +00:00
|
|
|
if ($this->id === null) {
|
2021-06-08 09:52:11 +00:00
|
|
|
throw new RuntimeException('Note must be created before getting reblogs.');
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
|
2021-05-18 17:16:36 +00:00
|
|
|
if ($this->reblogs === null) {
|
2021-05-19 16:35:13 +00:00
|
|
|
$this->reblogs = (array) model('NoteModel')
|
2021-06-08 09:52:11 +00:00
|
|
|
->getNoteReblogs($this->id);
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->reblogs;
|
|
|
|
}
|
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
public function getIsReblog(): bool
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
2021-05-19 16:35:13 +00:00
|
|
|
return $this->reblog_of_id !== null;
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 09:52:11 +00:00
|
|
|
public function getReblogOfNote(): ?self
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
2021-05-18 17:16:36 +00:00
|
|
|
if ($this->reblog_of_id === null) {
|
2021-05-06 14:00:48 +00:00
|
|
|
throw new RuntimeException('Note is not a reblog.');
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
|
2021-05-18 17:16:36 +00:00
|
|
|
if ($this->reblog_of_note === null) {
|
2021-05-19 16:35:13 +00:00
|
|
|
$this->reblog_of_note = model('NoteModel')
|
2021-06-08 09:52:11 +00:00
|
|
|
->getNoteById($this->reblog_of_id);
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->reblog_of_note;
|
|
|
|
}
|
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
public function setMessage(string $message): static
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
|
|
|
helper('activitypub');
|
|
|
|
|
|
|
|
$messageWithoutTags = strip_tags($message);
|
|
|
|
|
|
|
|
$this->attributes['message'] = $messageWithoutTags;
|
2021-06-08 09:52:11 +00:00
|
|
|
$this->attributes['message_html'] = str_replace("\n", '<br />', linkify($messageWithoutTags));
|
2021-04-02 17:20:02 +00:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}
|