2020-08-04 13:25:22 +02:00
|
|
|
<?php
|
|
|
|
|
2021-06-08 11:52:11 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-10-02 17:38:16 +02:00
|
|
|
/**
|
2022-02-19 17:06:11 +01:00
|
|
|
* @copyright 2020 Ad Aures
|
2020-10-02 17:38:16 +02:00
|
|
|
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
|
|
|
|
* @link https://castopod.org/
|
|
|
|
*/
|
|
|
|
|
2021-08-23 13:05:16 +02:00
|
|
|
namespace Modules\Auth\Entities;
|
2020-07-31 18:05:10 +02:00
|
|
|
|
2021-08-23 13:05:16 +02:00
|
|
|
use App\Entities\Podcast;
|
2022-08-12 18:02:56 +02:00
|
|
|
use App\Models\NotificationModel;
|
2020-07-31 18:05:10 +02:00
|
|
|
use App\Models\PodcastModel;
|
2022-09-07 11:33:10 +02:00
|
|
|
use App\Models\UserModel;
|
2021-05-12 16:00:25 +02:00
|
|
|
use Myth\Auth\Entities\User as MythAuthUser;
|
2021-05-19 18:35:13 +02:00
|
|
|
use RuntimeException;
|
2020-07-31 18:05:10 +02:00
|
|
|
|
2021-05-12 16:00:25 +02:00
|
|
|
/**
|
|
|
|
* @property int $id
|
|
|
|
* @property string $username
|
|
|
|
* @property string $email
|
|
|
|
* @property string $password
|
|
|
|
* @property bool $active
|
|
|
|
* @property bool $force_pass_reset
|
|
|
|
* @property int|null $podcast_id
|
|
|
|
* @property string|null $podcast_role
|
|
|
|
*
|
|
|
|
* @property Podcast[] $podcasts All podcasts the user is contributing to
|
2022-08-12 18:02:56 +02:00
|
|
|
* @property int[] $actorIdsWithUnreadNotifications Ids of the user's actors that have unread notifications
|
2021-05-12 16:00:25 +02:00
|
|
|
*/
|
|
|
|
class User extends MythAuthUser
|
2020-07-31 18:05:10 +02:00
|
|
|
{
|
2022-09-07 11:33:10 +02:00
|
|
|
public bool $is_owner;
|
|
|
|
|
2020-07-31 18:05:10 +02:00
|
|
|
/**
|
2021-05-17 19:11:23 +02:00
|
|
|
* @var Podcast[]|null
|
2020-07-31 18:05:10 +02:00
|
|
|
*/
|
2021-05-17 19:11:23 +02:00
|
|
|
protected ?array $podcasts = null;
|
2020-07-31 18:05:10 +02:00
|
|
|
|
2022-08-12 18:02:56 +02:00
|
|
|
/**
|
|
|
|
* @var int[]|null
|
|
|
|
*/
|
|
|
|
protected ?array $actorIdsWithUnreadNotifications = null;
|
|
|
|
|
2020-07-31 18:05:10 +02:00
|
|
|
/**
|
2021-05-19 18:35:13 +02:00
|
|
|
* Array of field names and the type of value to cast them as when they are accessed.
|
2021-05-06 16:00:48 +02:00
|
|
|
*
|
|
|
|
* @var array<string, string>
|
2020-07-31 18:05:10 +02:00
|
|
|
*/
|
|
|
|
protected $casts = [
|
2020-12-07 21:13:46 +01:00
|
|
|
'id' => 'integer',
|
2020-07-31 18:05:10 +02:00
|
|
|
'active' => 'boolean',
|
|
|
|
'force_pass_reset' => 'boolean',
|
2020-08-05 18:10:39 +02:00
|
|
|
'podcast_id' => '?integer',
|
2021-05-12 16:00:25 +02:00
|
|
|
'podcast_role' => '?string',
|
2020-07-31 18:05:10 +02:00
|
|
|
];
|
|
|
|
|
2022-09-07 11:33:10 +02:00
|
|
|
public function getIsOwner(): bool
|
|
|
|
{
|
|
|
|
$firstUser = (new UserModel())->first();
|
|
|
|
|
|
|
|
if (! $firstUser instanceof self) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->username === $firstUser->username;
|
|
|
|
}
|
|
|
|
|
2020-07-31 18:05:10 +02:00
|
|
|
/**
|
|
|
|
* Returns the podcasts the user is contributing to
|
|
|
|
*
|
2021-05-06 16:00:48 +02:00
|
|
|
* @return Podcast[]
|
2020-07-31 18:05:10 +02:00
|
|
|
*/
|
2021-05-06 16:00:48 +02:00
|
|
|
public function getPodcasts(): array
|
2020-07-31 18:05:10 +02:00
|
|
|
{
|
2021-05-12 16:00:25 +02:00
|
|
|
if ($this->id === null) {
|
2021-06-08 11:52:11 +02:00
|
|
|
throw new RuntimeException('Users must be created before getting podcasts.');
|
2020-07-31 18:05:10 +02:00
|
|
|
}
|
|
|
|
|
2021-05-12 16:00:25 +02:00
|
|
|
if ($this->podcasts === null) {
|
2020-07-31 18:05:10 +02:00
|
|
|
$this->podcasts = (new PodcastModel())->getUserPodcasts($this->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->podcasts;
|
|
|
|
}
|
2022-08-12 18:02:56 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the ids of the user's actors that have unread notifications
|
|
|
|
*
|
|
|
|
* @return int[]
|
|
|
|
*/
|
|
|
|
public function getActorIdsWithUnreadNotifications(): array
|
|
|
|
{
|
|
|
|
if ($this->getPodcasts() === []) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$unreadNotifications = (new NotificationModel())->whereIn(
|
|
|
|
'target_actor_id',
|
|
|
|
array_column($this->getPodcasts(), 'actor_id')
|
|
|
|
)
|
|
|
|
->where('read_at', null)
|
|
|
|
->findAll();
|
|
|
|
|
|
|
|
return array_column($unreadNotifications, 'target_actor_id');
|
|
|
|
}
|
2020-07-31 18:05:10 +02:00
|
|
|
}
|