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
|
|
|
/**
|
2022-02-19 16:06:11 +00:00
|
|
|
* @copyright 2021 Ad Aures
|
2021-04-02 17:20:02 +00:00
|
|
|
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
|
|
|
|
* @link https://castopod.org/
|
|
|
|
*/
|
|
|
|
|
2021-08-23 11:05:16 +00:00
|
|
|
namespace Modules\Fediverse\Entities;
|
2021-04-02 17:20:02 +00:00
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
use CodeIgniter\Entity\Entity;
|
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 int $id
|
|
|
|
* @property string $uri
|
|
|
|
* @property string $username
|
|
|
|
* @property string $domain
|
|
|
|
* @property string $display_name
|
|
|
|
* @property string|null $summary
|
|
|
|
* @property string|null $private_key
|
|
|
|
* @property string|null $public_key
|
2022-02-02 17:36:03 +00:00
|
|
|
* @property string $public_key_id
|
2021-05-12 14:00:25 +00:00
|
|
|
* @property string|null $avatar_image_url
|
|
|
|
* @property string|null $avatar_image_mimetype
|
|
|
|
* @property string|null $cover_image_url
|
|
|
|
* @property string|null $cover_image_mimetype
|
|
|
|
* @property string $inbox_url
|
|
|
|
* @property string|null $outbox_url
|
|
|
|
* @property string|null $followers_url
|
|
|
|
* @property int $followers_count
|
2021-08-13 11:07:29 +00:00
|
|
|
* @property int $posts_count
|
2021-05-12 14:00:25 +00:00
|
|
|
* @property bool $is_blocked
|
|
|
|
*
|
|
|
|
* @property Actor[] $followers
|
|
|
|
* @property bool $is_local
|
|
|
|
*/
|
2021-04-02 17:20:02 +00:00
|
|
|
class Actor extends Entity
|
|
|
|
{
|
2021-05-18 17:16:36 +00:00
|
|
|
protected string $public_key_id;
|
2021-04-02 17:20:02 +00:00
|
|
|
|
|
|
|
/**
|
2022-07-03 16:42:20 +00:00
|
|
|
* @var \Modules\Fediverse\Entities\Actor[]|null
|
2021-04-02 17:20:02 +00:00
|
|
|
*/
|
2021-05-18 17:16:36 +00:00
|
|
|
protected ?array $followers = null;
|
2021-05-19 16:35:13 +00:00
|
|
|
|
2021-05-18 17:16:36 +00:00
|
|
|
protected bool $is_local = false;
|
2021-04-02 17:20:02 +00:00
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
/**
|
|
|
|
* @var array<string, string>
|
|
|
|
*/
|
2021-04-02 17:20:02 +00:00
|
|
|
protected $casts = [
|
2023-06-12 14:47:38 +00:00
|
|
|
'id' => 'integer',
|
|
|
|
'uri' => 'string',
|
|
|
|
'username' => 'string',
|
|
|
|
'domain' => 'string',
|
|
|
|
'display_name' => 'string',
|
|
|
|
'summary' => '?string',
|
|
|
|
'private_key' => '?string',
|
|
|
|
'public_key' => '?string',
|
|
|
|
'avatar_image_url' => '?string',
|
2021-04-14 13:37:11 +00:00
|
|
|
'avatar_image_mimetype' => '?string',
|
2023-06-12 14:47:38 +00:00
|
|
|
'cover_image_url' => '?string',
|
|
|
|
'cover_image_mimetype' => '?string',
|
|
|
|
'inbox_url' => 'string',
|
|
|
|
'outbox_url' => '?string',
|
|
|
|
'followers_url' => '?string',
|
|
|
|
'followers_count' => 'integer',
|
|
|
|
'posts_count' => 'integer',
|
|
|
|
'is_blocked' => 'boolean',
|
2021-04-02 17:20:02 +00:00
|
|
|
];
|
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
public function getPublicKeyId(): string
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
|
|
|
return $this->uri . '#main-key';
|
|
|
|
}
|
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
public function getIsLocal(): bool
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
2021-05-19 16:35:13 +00:00
|
|
|
if (! $this->is_local) {
|
2021-04-02 17:20:02 +00:00
|
|
|
$uri = current_url(true);
|
|
|
|
|
|
|
|
$this->is_local =
|
|
|
|
$this->domain ===
|
|
|
|
$uri->getHost() .
|
|
|
|
($uri->getPort() ? ':' . $uri->getPort() : '');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->is_local;
|
|
|
|
}
|
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
/**
|
2021-05-12 14:00:25 +00:00
|
|
|
* @return Actor[]
|
2021-05-06 14:00:48 +00:00
|
|
|
*/
|
|
|
|
public function getFollowers(): 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('Actor must be created before getting followers.');
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
|
2021-05-18 17:16:36 +00:00
|
|
|
if ($this->followers === null) {
|
2023-08-29 12:58:20 +00:00
|
|
|
$this->followers = model('ActorModel', false)
|
2021-06-08 09:52:11 +00:00
|
|
|
->getFollowers($this->id);
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->followers;
|
|
|
|
}
|
2021-04-14 13:37:11 +00:00
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
public function getAvatarImageUrl(): string
|
2021-04-14 13:37:11 +00:00
|
|
|
{
|
2021-05-18 17:16:36 +00:00
|
|
|
if ($this->attributes['avatar_image_url'] === null) {
|
2024-02-20 10:01:16 +00:00
|
|
|
return base_url(config('Fediverse')->defaultAvatarImagePath);
|
2021-04-14 13:37:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->attributes['avatar_image_url'];
|
|
|
|
}
|
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
public function getAvatarImageMimetype(): string
|
2021-04-14 13:37:11 +00:00
|
|
|
{
|
2021-05-18 17:16:36 +00:00
|
|
|
if ($this->attributes['avatar_image_mimetype'] === null) {
|
2024-02-20 10:01:16 +00:00
|
|
|
return config('Fediverse')->defaultAvatarImageMimetype;
|
2021-04-14 13:37:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->attributes['avatar_image_mimetype'];
|
|
|
|
}
|
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
public function getCoverImageUrl(): string
|
2021-04-14 13:37:11 +00:00
|
|
|
{
|
2021-05-18 17:16:36 +00:00
|
|
|
if ($this->attributes['cover_image_url'] === null) {
|
2024-02-20 10:01:16 +00:00
|
|
|
return base_url(config('Fediverse')->defaultCoverImagePath);
|
2021-04-14 13:37:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->attributes['cover_image_url'];
|
|
|
|
}
|
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
public function getCoverImageMimetype(): string
|
2021-04-14 13:37:11 +00:00
|
|
|
{
|
2021-05-18 17:16:36 +00:00
|
|
|
if ($this->attributes['cover_image_mimetype'] === null) {
|
2024-02-20 10:01:16 +00:00
|
|
|
return config('Fediverse')->defaultCoverImageMimetype;
|
2021-04-14 13:37:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->attributes['cover_image_mimetype'];
|
|
|
|
}
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|