2020-03-29 20:56:35 +01:00
|
|
|
<?php
|
|
|
|
|
2021-11-28 13:09:04 +00:00
|
|
|
declare(strict_types = 1);
|
2021-10-28 17:34:01 +01:00
|
|
|
|
2020-03-29 20:56:35 +01:00
|
|
|
// {{{ License
|
2020-09-05 03:34:01 +01:00
|
|
|
|
2020-05-20 17:53:53 +01:00
|
|
|
// This file is part of GNU social - https://www.gnu.org/software/social
|
2020-03-29 20:56:35 +01:00
|
|
|
//
|
|
|
|
// GNU social is free software: you can redistribute it and/or modify
|
2020-05-10 21:43:15 +01:00
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
2020-03-29 20:56:35 +01:00
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// GNU social is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
2020-05-10 21:43:15 +01:00
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
2020-03-29 20:56:35 +01:00
|
|
|
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
|
2020-09-05 03:34:01 +01:00
|
|
|
|
2020-03-29 20:56:35 +01:00
|
|
|
// }}}
|
|
|
|
|
|
|
|
namespace App\Entity;
|
|
|
|
|
2020-08-15 08:03:58 +01:00
|
|
|
use App\Core\Cache;
|
2020-07-25 18:53:37 +01:00
|
|
|
use App\Core\DB\DB;
|
2020-08-08 17:10:25 +01:00
|
|
|
use App\Core\Entity;
|
2021-11-27 15:06:46 +00:00
|
|
|
use App\Core\Event;
|
2021-09-14 13:40:50 +01:00
|
|
|
use App\Core\Router\Router;
|
2021-12-21 12:17:51 +00:00
|
|
|
use App\Util\Exception\BugFoundException;
|
2021-09-14 13:40:50 +01:00
|
|
|
use App\Util\Exception\NicknameException;
|
2021-11-16 23:24:06 +00:00
|
|
|
use App\Util\Exception\NotFoundException;
|
2021-12-21 12:12:03 +00:00
|
|
|
use App\Util\Formatting;
|
2021-09-14 13:40:50 +01:00
|
|
|
use App\Util\Nickname;
|
2021-09-18 04:54:35 +01:00
|
|
|
use Component\Avatar\Avatar;
|
2021-12-25 20:27:10 +00:00
|
|
|
use Component\Language\Entity\ActorLanguage;
|
|
|
|
use Component\Language\Entity\Language;
|
2022-01-06 11:26:48 +00:00
|
|
|
use Component\Subscription\Entity\ActorSubscription;
|
2020-07-25 18:53:37 +01:00
|
|
|
use Functional as F;
|
2020-05-10 21:43:15 +01:00
|
|
|
|
2020-03-29 20:56:35 +01:00
|
|
|
/**
|
2020-08-13 00:57:22 +01:00
|
|
|
* Entity for actors
|
2020-03-29 20:56:35 +01:00
|
|
|
*
|
|
|
|
* @category DB
|
|
|
|
* @package GNUsocial
|
|
|
|
*
|
|
|
|
* @author Zach Copley <zach@status.net>
|
|
|
|
* @copyright 2010 StatusNet Inc.
|
|
|
|
* @author Mikael Nordfeldth <mmn@hethane.se>
|
|
|
|
* @copyright 2009-2014 Free Software Foundation, Inc http://www.fsf.org
|
2021-02-19 23:29:43 +00:00
|
|
|
* @author Hugo Sales <hugo@hsal.es>
|
|
|
|
* @copyright 2020-2021 Free Software Foundation, Inc http://www.fsf.org
|
2020-03-29 20:56:35 +01:00
|
|
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
|
|
|
*/
|
2021-09-18 03:22:27 +01:00
|
|
|
class Actor extends Entity
|
2020-03-29 20:56:35 +01:00
|
|
|
{
|
2020-03-30 15:00:13 +01:00
|
|
|
// {{{ Autocode
|
2021-05-05 17:03:03 +01:00
|
|
|
// @codeCoverageIgnoreStart
|
2020-03-30 16:13:51 +01:00
|
|
|
private int $id;
|
|
|
|
private string $nickname;
|
2021-12-26 17:01:02 +00:00
|
|
|
private ?string $fullname = null;
|
2021-12-26 15:12:06 +00:00
|
|
|
private int $roles;
|
2021-12-11 22:19:37 +00:00
|
|
|
private int $type;
|
2021-12-26 17:01:02 +00:00
|
|
|
private ?string $homepage = null;
|
|
|
|
private ?string $bio = null;
|
|
|
|
private ?string $location = null;
|
|
|
|
private ?float $lat = null;
|
|
|
|
private ?float $lon = null;
|
|
|
|
private ?int $location_id = null;
|
|
|
|
private ?int $location_service = null;
|
2021-11-16 23:24:06 +00:00
|
|
|
private bool $is_local;
|
2021-12-26 15:12:06 +00:00
|
|
|
private \DateTimeInterface $created;
|
|
|
|
private \DateTimeInterface $modified;
|
2020-03-30 16:13:51 +01:00
|
|
|
|
|
|
|
public function setId(int $id): self
|
|
|
|
{
|
|
|
|
$this->id = $id;
|
|
|
|
return $this;
|
|
|
|
}
|
2020-08-08 17:10:25 +01:00
|
|
|
|
2020-03-30 16:13:51 +01:00
|
|
|
public function getId(): int
|
|
|
|
{
|
|
|
|
return $this->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setNickname(string $nickname): self
|
|
|
|
{
|
2021-12-26 15:12:06 +00:00
|
|
|
$this->nickname = \mb_substr($nickname, 0, 64);
|
2020-03-30 16:13:51 +01:00
|
|
|
return $this;
|
|
|
|
}
|
2020-08-08 17:10:25 +01:00
|
|
|
|
2020-03-30 16:13:51 +01:00
|
|
|
public function getNickname(): string
|
|
|
|
{
|
|
|
|
return $this->nickname;
|
|
|
|
}
|
|
|
|
|
2021-11-27 04:11:35 +00:00
|
|
|
public function setFullname(?string $fullname): self
|
2020-03-30 16:13:51 +01:00
|
|
|
{
|
|
|
|
$this->fullname = $fullname;
|
|
|
|
return $this;
|
|
|
|
}
|
2020-08-08 17:10:25 +01:00
|
|
|
|
2021-10-29 22:05:10 +01:00
|
|
|
public function getFullname(): ?string
|
2020-03-30 16:13:51 +01:00
|
|
|
{
|
|
|
|
return $this->fullname;
|
|
|
|
}
|
|
|
|
|
2020-07-25 03:03:16 +01:00
|
|
|
public function setRoles(int $roles): self
|
2020-07-22 12:40:53 +01:00
|
|
|
{
|
|
|
|
$this->roles = $roles;
|
|
|
|
return $this;
|
|
|
|
}
|
2020-08-08 17:10:25 +01:00
|
|
|
|
2020-07-25 03:03:16 +01:00
|
|
|
public function getRoles(): int
|
2020-07-22 12:40:53 +01:00
|
|
|
{
|
|
|
|
return $this->roles;
|
|
|
|
}
|
|
|
|
|
2021-12-11 22:19:37 +00:00
|
|
|
public function setType(int $type): self
|
|
|
|
{
|
|
|
|
$this->type = $type;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getType(): int
|
|
|
|
{
|
|
|
|
return $this->type;
|
|
|
|
}
|
|
|
|
|
2020-03-30 16:13:51 +01:00
|
|
|
public function setHomepage(?string $homepage): self
|
|
|
|
{
|
|
|
|
$this->homepage = $homepage;
|
|
|
|
return $this;
|
|
|
|
}
|
2020-08-08 17:10:25 +01:00
|
|
|
|
2020-03-30 16:13:51 +01:00
|
|
|
public function getHomepage(): ?string
|
|
|
|
{
|
|
|
|
return $this->homepage;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setBio(?string $bio): self
|
|
|
|
{
|
|
|
|
$this->bio = $bio;
|
|
|
|
return $this;
|
|
|
|
}
|
2020-08-08 17:10:25 +01:00
|
|
|
|
2020-03-30 16:13:51 +01:00
|
|
|
public function getBio(): ?string
|
|
|
|
{
|
|
|
|
return $this->bio;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setLocation(?string $location): self
|
|
|
|
{
|
|
|
|
$this->location = $location;
|
|
|
|
return $this;
|
|
|
|
}
|
2020-08-08 17:10:25 +01:00
|
|
|
|
2020-03-30 16:13:51 +01:00
|
|
|
public function getLocation(): ?string
|
|
|
|
{
|
|
|
|
return $this->location;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setLat(?float $lat): self
|
|
|
|
{
|
|
|
|
$this->lat = $lat;
|
|
|
|
return $this;
|
|
|
|
}
|
2020-08-08 17:10:25 +01:00
|
|
|
|
2020-03-30 16:13:51 +01:00
|
|
|
public function getLat(): ?float
|
|
|
|
{
|
|
|
|
return $this->lat;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setLon(?float $lon): self
|
|
|
|
{
|
|
|
|
$this->lon = $lon;
|
|
|
|
return $this;
|
|
|
|
}
|
2020-08-08 17:10:25 +01:00
|
|
|
|
2020-03-30 16:13:51 +01:00
|
|
|
public function getLon(): ?float
|
|
|
|
{
|
|
|
|
return $this->lon;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setLocationId(?int $location_id): self
|
|
|
|
{
|
|
|
|
$this->location_id = $location_id;
|
|
|
|
return $this;
|
|
|
|
}
|
2020-08-08 17:10:25 +01:00
|
|
|
|
2020-03-30 16:13:51 +01:00
|
|
|
public function getLocationId(): ?int
|
|
|
|
{
|
|
|
|
return $this->location_id;
|
|
|
|
}
|
|
|
|
|
2020-06-30 19:20:50 +01:00
|
|
|
public function setLocationService(?int $location_service): self
|
2020-03-30 16:13:51 +01:00
|
|
|
{
|
2020-06-30 19:20:50 +01:00
|
|
|
$this->location_service = $location_service;
|
2020-03-30 16:13:51 +01:00
|
|
|
return $this;
|
|
|
|
}
|
2020-08-08 17:10:25 +01:00
|
|
|
|
2020-06-30 19:20:50 +01:00
|
|
|
public function getLocationService(): ?int
|
2020-03-30 16:13:51 +01:00
|
|
|
{
|
2020-06-30 19:20:50 +01:00
|
|
|
return $this->location_service;
|
2020-03-30 16:13:51 +01:00
|
|
|
}
|
|
|
|
|
2021-11-16 23:24:06 +00:00
|
|
|
public function setIsLocal(bool $is_local): self
|
|
|
|
{
|
|
|
|
$this->is_local = $is_local;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getIsLocal(): bool
|
|
|
|
{
|
|
|
|
return $this->is_local;
|
|
|
|
}
|
|
|
|
|
2021-12-26 15:12:06 +00:00
|
|
|
public function setCreated(\DateTimeInterface $created): self
|
2020-03-30 16:13:51 +01:00
|
|
|
{
|
|
|
|
$this->created = $created;
|
|
|
|
return $this;
|
|
|
|
}
|
2020-08-08 17:10:25 +01:00
|
|
|
|
2021-12-26 15:12:06 +00:00
|
|
|
public function getCreated(): \DateTimeInterface
|
2020-03-30 16:13:51 +01:00
|
|
|
{
|
|
|
|
return $this->created;
|
|
|
|
}
|
|
|
|
|
2021-12-26 15:12:06 +00:00
|
|
|
public function setModified(\DateTimeInterface $modified): self
|
2020-03-30 16:13:51 +01:00
|
|
|
{
|
|
|
|
$this->modified = $modified;
|
|
|
|
return $this;
|
|
|
|
}
|
2020-08-08 17:10:25 +01:00
|
|
|
|
2021-12-26 15:12:06 +00:00
|
|
|
public function getModified(): \DateTimeInterface
|
2020-03-30 16:13:51 +01:00
|
|
|
{
|
|
|
|
return $this->modified;
|
|
|
|
}
|
|
|
|
|
2021-05-05 17:03:03 +01:00
|
|
|
// @codeCoverageIgnoreEnd
|
2020-03-30 15:00:13 +01:00
|
|
|
// }}} Autocode
|
2020-03-29 20:56:35 +01:00
|
|
|
|
2022-01-02 20:00:17 +00:00
|
|
|
public const PERSON = 1;
|
|
|
|
public const GROUP = 2;
|
2022-02-10 04:31:06 +00:00
|
|
|
public const BOT = 3;
|
2021-12-11 22:19:37 +00:00
|
|
|
|
2021-12-23 14:04:00 +00:00
|
|
|
public static function cacheKeys(int|self $actor_id, mixed $other = null): array
|
2021-12-09 21:59:49 +00:00
|
|
|
{
|
2021-12-23 14:04:00 +00:00
|
|
|
$actor_id = \is_int($actor_id) ? $actor_id : $actor_id->getId();
|
|
|
|
|
2021-12-09 21:59:49 +00:00
|
|
|
return [
|
2022-01-02 20:00:17 +00:00
|
|
|
'id' => "actor-id-{$actor_id}",
|
|
|
|
'nickname' => "actor-nickname-id-{$actor_id}",
|
|
|
|
'fullname' => "actor-fullname-id-{$actor_id}",
|
2022-01-04 22:20:12 +00:00
|
|
|
'self-tags' => "actor-self-tags-{$actor_id}",
|
2022-01-02 20:00:17 +00:00
|
|
|
'circles' => "actor-circles-{$actor_id}",
|
2022-01-05 17:39:10 +00:00
|
|
|
'subscribers' => "subscribers-{$actor_id}",
|
2022-01-02 20:00:17 +00:00
|
|
|
'subscribed' => "subscribed-{$actor_id}",
|
2021-12-09 21:59:49 +00:00
|
|
|
'relative-nickname' => "actor-{$actor_id}-relative-nickname-{$other}", // $other is $nickname
|
2022-01-02 20:00:17 +00:00
|
|
|
'can-admin' => "actor-{$actor_id}-can-admin-{$other}", // $other is an actor id
|
2021-12-09 21:59:49 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2021-12-21 12:12:03 +00:00
|
|
|
public function getLocalUser(): ?LocalUser
|
2021-09-14 17:15:37 +01:00
|
|
|
{
|
2021-11-16 23:24:06 +00:00
|
|
|
if ($this->getIsLocal()) {
|
|
|
|
return DB::findOneBy('local_user', ['id' => $this->getId()]);
|
|
|
|
} else {
|
|
|
|
throw new NotFoundException('This is a remote actor.');
|
|
|
|
}
|
2021-09-14 17:15:37 +01:00
|
|
|
}
|
|
|
|
|
2021-12-27 04:47:04 +00:00
|
|
|
public function getAvatarUrl(string $size = 'medium')
|
2020-11-29 04:55:23 +00:00
|
|
|
{
|
2021-12-04 11:56:27 +00:00
|
|
|
return Avatar::getUrl($this->getId(), $size);
|
2020-11-29 04:55:23 +00:00
|
|
|
}
|
|
|
|
|
2021-12-27 04:47:04 +00:00
|
|
|
public function getAvatarDimensions(string $size = 'medium')
|
2021-12-02 15:36:08 +00:00
|
|
|
{
|
|
|
|
return Avatar::getDimensions($this->getId(), $size);
|
|
|
|
}
|
|
|
|
|
2021-10-04 17:00:58 +01:00
|
|
|
public static function getById(int $id): ?self
|
2020-07-25 03:03:16 +01:00
|
|
|
{
|
2022-01-02 20:00:17 +00:00
|
|
|
return Cache::get(self::cacheKeys($id)['id'], fn() => DB::find('actor', ['id' => $id]));
|
2020-08-08 17:10:25 +01:00
|
|
|
}
|
2020-07-25 03:03:16 +01:00
|
|
|
|
2021-10-04 17:00:58 +01:00
|
|
|
public static function getNicknameById(int $id): string
|
2020-08-08 17:10:25 +01:00
|
|
|
{
|
2022-01-02 20:00:17 +00:00
|
|
|
return Cache::get(self::cacheKeys($id)['nickname'], fn() => self::getById($id)->getNickname());
|
2020-08-08 17:10:25 +01:00
|
|
|
}
|
|
|
|
|
2021-10-29 22:05:10 +01:00
|
|
|
public static function getFullnameById(int $id): ?string
|
2021-10-26 14:47:28 +01:00
|
|
|
{
|
2022-01-02 20:00:17 +00:00
|
|
|
return Cache::get(self::cacheKeys($id)['fullname'], fn() => self::getById($id)->getFullname());
|
2021-10-26 14:47:28 +01:00
|
|
|
}
|
|
|
|
|
2021-12-05 17:50:15 +00:00
|
|
|
/**
|
|
|
|
* For consistency with Note
|
|
|
|
*/
|
|
|
|
public function getActorId(): int
|
|
|
|
{
|
|
|
|
return $this->getId();
|
|
|
|
}
|
|
|
|
|
2021-11-27 04:11:35 +00:00
|
|
|
/**
|
2022-01-04 22:20:12 +00:00
|
|
|
* @return array ActorTag[] Self Tag Circles of which this actor is a member
|
2021-11-27 04:11:35 +00:00
|
|
|
*/
|
2022-01-04 22:20:12 +00:00
|
|
|
public function getSelfTags(): array
|
2020-08-08 17:10:25 +01:00
|
|
|
{
|
2022-01-04 22:20:12 +00:00
|
|
|
return Cache::getList(
|
|
|
|
self::cacheKeys($this->getId())['self-tags'],
|
|
|
|
fn() => DB::findBy('actor_tag', ['tagger' => $this->getId(), 'tagged' => $this->getId()], order_by: ['modified' => 'DESC']),
|
|
|
|
);
|
2021-11-27 04:11:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-01-04 22:20:12 +00:00
|
|
|
* @return array ActorCircle[]
|
2021-11-27 04:11:35 +00:00
|
|
|
*/
|
2022-01-04 22:20:12 +00:00
|
|
|
public function getCircles(): array
|
2021-12-23 14:36:12 +00:00
|
|
|
{
|
|
|
|
return Cache::getList(
|
|
|
|
self::cacheKeys($this->getId())['circles'],
|
2022-01-02 20:00:17 +00:00
|
|
|
fn() => DB::findBy('actor_circle', ['tagger' => $this->getId()]),
|
2021-12-23 14:36:12 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-12-09 21:59:49 +00:00
|
|
|
private function getSubCount(string $which, string $column): int
|
2020-08-15 08:03:58 +01:00
|
|
|
{
|
2021-10-28 17:34:01 +01:00
|
|
|
return Cache::get(
|
2021-12-09 21:59:49 +00:00
|
|
|
self::cacheKeys($this->getId())[$which],
|
2022-01-06 11:26:48 +00:00
|
|
|
fn() => DB::count(ActorSubscription::class, [$column => $this->getId()]) - ($this->getIsLocal() ? 1 : 0)
|
2021-10-28 17:34:01 +01:00
|
|
|
);
|
2020-08-15 08:03:58 +01:00
|
|
|
}
|
|
|
|
|
2021-12-09 21:59:49 +00:00
|
|
|
public function getSubscribersCount(): int
|
|
|
|
{
|
2022-01-05 17:39:10 +00:00
|
|
|
return $this->getSubCount(which: 'subscribers', column: 'subscribed_id');
|
2021-12-09 21:59:49 +00:00
|
|
|
}
|
|
|
|
|
2022-01-02 20:00:17 +00:00
|
|
|
public function getSubscribedCount(): int
|
2020-08-15 08:03:58 +01:00
|
|
|
{
|
2022-01-02 20:37:15 +00:00
|
|
|
return $this->getSubCount(which: 'subscribed', column: 'subscriber_id');
|
2020-07-25 03:03:16 +01:00
|
|
|
}
|
|
|
|
|
2022-01-02 20:00:17 +00:00
|
|
|
public function getSubscriptions(): array
|
|
|
|
{
|
|
|
|
return DB::dql(<<<EOF
|
|
|
|
SELECT a FROM actor AS a
|
2022-01-18 13:01:31 +00:00
|
|
|
INNER JOIN actor_subscription AS s
|
2022-01-02 20:00:17 +00:00
|
|
|
WITH a.id = s.subscribed_id
|
|
|
|
WHERE s.subscriber_id = :self AND a.id != :self
|
|
|
|
EOF, ['self' => $this->getId()]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getSubscribers(): array
|
|
|
|
{
|
|
|
|
return DB::dql(<<<EOF
|
|
|
|
SELECT a FROM actor AS a
|
2022-01-18 13:01:31 +00:00
|
|
|
INNER JOIN actor_subscription AS s
|
2022-01-02 20:00:17 +00:00
|
|
|
WITH a.id = s.subscriber_id
|
|
|
|
WHERE s.subscribed_id = :self AND a.id != :self
|
|
|
|
EOF, ['self' => $this->getId()]);
|
|
|
|
}
|
|
|
|
|
2022-01-04 22:20:12 +00:00
|
|
|
public function getSubscriptionsUrl(): string
|
|
|
|
{
|
2022-02-10 16:02:51 +00:00
|
|
|
return Router::url('actor_subscriptions_id', ['id' => $this->getId()]);
|
2022-01-04 22:20:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getSubscribersUrl(): string
|
|
|
|
{
|
2022-02-10 16:02:51 +00:00
|
|
|
return Router::url('actor_subscribers_id', ['id' => $this->getId()]);
|
2022-01-04 22:20:12 +00:00
|
|
|
}
|
|
|
|
|
2021-09-14 13:40:50 +01:00
|
|
|
/**
|
|
|
|
* Resolve an ambiguous nickname reference, checking in following order:
|
|
|
|
* - Actors that $sender subscribes to
|
|
|
|
* - Actors that subscribe to $sender
|
|
|
|
* - Any Actor
|
|
|
|
*
|
|
|
|
* @param string $nickname validated nickname of
|
|
|
|
*
|
|
|
|
* @throws NicknameException
|
|
|
|
*/
|
|
|
|
public function findRelativeActor(string $nickname): ?self
|
|
|
|
{
|
|
|
|
// Will throw exception on invalid input.
|
|
|
|
$nickname = Nickname::normalize($nickname, check_already_used: false);
|
2021-11-02 10:42:21 +00:00
|
|
|
return Cache::get(
|
2021-12-09 21:59:49 +00:00
|
|
|
self::cacheKeys($this->getId(), $nickname)['relative-nickname'],
|
2021-11-28 13:09:04 +00:00
|
|
|
fn () => DB::dql(
|
|
|
|
<<<'EOF'
|
2022-01-04 22:20:12 +00:00
|
|
|
SELECT a FROM actor AS a WHERE
|
2022-01-18 13:01:31 +00:00
|
|
|
a.id IN (SELECT sa.subscribed_id FROM actor_subscription sa JOIN actor aa WITH sa.subscribed_id = aa.id WHERE sa.subscriber_id = :actor_id AND aa.nickname = :nickname) OR
|
|
|
|
a.id IN (SELECT sb.subscriber_id FROM actor_subscription sb JOIN actor ab WITH sb.subscriber_id = ab.id WHERE sb.subscribed_id = :actor_id AND ab.nickname = :nickname) OR
|
2021-11-02 11:11:51 +00:00
|
|
|
a.nickname = :nickname
|
|
|
|
EOF,
|
2021-11-28 13:09:04 +00:00
|
|
|
['nickname' => $nickname, 'actor_id' => $this->getId()],
|
|
|
|
['limit' => 1],
|
|
|
|
)[0] ?? null,
|
2021-10-28 17:34:01 +01:00
|
|
|
);
|
2021-09-14 13:40:50 +01:00
|
|
|
}
|
|
|
|
|
2021-12-21 12:24:23 +00:00
|
|
|
/**
|
|
|
|
* Get a URI for this actor, i.e. a unique and stable identifier, using the ID
|
|
|
|
*/
|
2021-11-27 15:06:46 +00:00
|
|
|
public function getUri(int $type = Router::ABSOLUTE_URL): string
|
2021-09-14 13:40:50 +01:00
|
|
|
{
|
2021-11-27 15:06:46 +00:00
|
|
|
$uri = null;
|
|
|
|
if (Event::handle('StartGetActorUri', [$this, $type, &$uri]) === Event::next) {
|
2021-12-21 12:12:03 +00:00
|
|
|
switch ($this->type) {
|
|
|
|
case self::PERSON:
|
|
|
|
case self::BOT:
|
|
|
|
case self::GROUP:
|
2022-01-19 02:22:29 +00:00
|
|
|
$uri = Router::url('actor_view_id', ['id' => $this->getId()], $type);
|
2021-12-21 12:46:42 +00:00
|
|
|
break;
|
2021-12-21 12:17:51 +00:00
|
|
|
default:
|
|
|
|
throw new BugFoundException('Actor type added but `Actor::getUri` was not updated');
|
2021-12-21 12:12:03 +00:00
|
|
|
}
|
2021-11-27 15:06:46 +00:00
|
|
|
Event::handle('EndGetActorUri', [$this, $type, &$uri]);
|
|
|
|
}
|
|
|
|
return $uri;
|
2021-09-14 13:40:50 +01:00
|
|
|
}
|
|
|
|
|
2021-12-21 12:24:23 +00:00
|
|
|
/**
|
|
|
|
* Get a URL for this actor, i.e. a user friendly URL, using the nickname
|
2022-01-27 17:19:50 +00:00
|
|
|
*
|
|
|
|
* @param int $type
|
|
|
|
* @return string
|
|
|
|
* @throws BugFoundException
|
2021-12-21 12:24:23 +00:00
|
|
|
*/
|
2021-11-27 15:06:46 +00:00
|
|
|
public function getUrl(int $type = Router::ABSOLUTE_URL): string
|
2021-09-14 13:40:50 +01:00
|
|
|
{
|
2021-12-27 04:13:09 +00:00
|
|
|
if ($this->getIsLocal()) {
|
|
|
|
$url = null;
|
|
|
|
if (Event::handle('StartGetActorUrl', [$this, $type, &$url]) === Event::next) {
|
2022-01-27 17:19:50 +00:00
|
|
|
$url = match ($this->type) {
|
2022-02-10 16:02:51 +00:00
|
|
|
self::PERSON, self::BOT => Router::url('person_actor_view_nickname', ['nickname' => mb_strtolower($this->getNickname())], $type),
|
2022-01-27 17:19:50 +00:00
|
|
|
self::GROUP => Router::url('group_actor_view_nickname', ['nickname' => $this->getNickname()], $type),
|
|
|
|
default => throw new BugFoundException('Actor type added but `Actor::getUrl` was not updated'),
|
|
|
|
};
|
2021-12-27 04:13:09 +00:00
|
|
|
Event::handle('EndGetActorUrl', [$this, $type, &$url]);
|
2021-11-27 15:06:46 +00:00
|
|
|
}
|
2021-12-27 04:13:09 +00:00
|
|
|
} else {
|
|
|
|
return Router::url('actor_view_id', ['id' => $this->getId()], $type);
|
2021-11-27 15:06:46 +00:00
|
|
|
}
|
|
|
|
return $url;
|
2021-09-14 13:40:50 +01:00
|
|
|
}
|
|
|
|
|
2021-10-18 13:22:02 +01:00
|
|
|
public function getAliases(): array
|
|
|
|
{
|
|
|
|
return array_keys($this->getAliasesWithIDs());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAliasesWithIDs(): array
|
|
|
|
{
|
|
|
|
$aliases = [];
|
|
|
|
|
|
|
|
$aliases[$this->getUri(Router::ABSOLUTE_URL)] = $this->getId();
|
|
|
|
$aliases[$this->getUrl(Router::ABSOLUTE_URL)] = $this->getId();
|
|
|
|
|
|
|
|
return $aliases;
|
|
|
|
}
|
|
|
|
|
2021-11-28 13:09:04 +00:00
|
|
|
public function getTopLanguage(): Language
|
|
|
|
{
|
|
|
|
return ActorLanguage::getActorLanguages($this, context: null)[0];
|
|
|
|
}
|
|
|
|
|
2021-11-01 19:43:23 +00:00
|
|
|
/**
|
2021-11-16 23:24:06 +00:00
|
|
|
* Get the most appropriate language for $this to use when
|
2021-11-01 19:43:23 +00:00
|
|
|
* referring to $context (a reply or a group, for instance)
|
|
|
|
*
|
2021-11-11 12:29:55 +00:00
|
|
|
* @return Language[]
|
2021-11-01 19:43:23 +00:00
|
|
|
*/
|
2021-11-02 10:42:21 +00:00
|
|
|
public function getPreferredLanguageChoices(?self $context = null): array
|
2021-10-28 17:34:01 +01:00
|
|
|
{
|
2021-11-28 13:09:04 +00:00
|
|
|
$langs = ActorLanguage::getActorLanguages($this, context: $context);
|
|
|
|
return array_merge(...F\map($langs, fn ($l) => $l->toChoiceFormat()));
|
|
|
|
}
|
|
|
|
|
2021-11-28 18:58:56 +00:00
|
|
|
public function isVisibleTo(null|LocalUser|self $other): bool
|
2021-11-28 13:09:04 +00:00
|
|
|
{
|
|
|
|
return true; // TODO
|
2021-10-28 17:34:01 +01:00
|
|
|
}
|
|
|
|
|
2021-12-21 19:09:26 +00:00
|
|
|
/**
|
|
|
|
* Check whether $this has permission for performing actions on behalf of $other
|
|
|
|
*/
|
|
|
|
public function canAdmin(self $other): bool
|
|
|
|
{
|
2021-12-28 15:02:03 +00:00
|
|
|
if ($this->getIsLocal()) {
|
|
|
|
switch ($other->getType()) {
|
|
|
|
case self::PERSON:
|
|
|
|
return $this->getId() === $other->getId();
|
|
|
|
case self::GROUP:
|
|
|
|
return Cache::get(
|
|
|
|
self::cacheKeys($this->getId(), $other->getId())['can-admin'],
|
|
|
|
function () use ($other) {
|
|
|
|
try {
|
|
|
|
return DB::findOneBy('group_member', ['group_id' => $other->getId(), 'actor_id' => $this->getId()])->getIsAdmin();
|
|
|
|
} catch (NotFoundException) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$canAdmin = false;
|
|
|
|
Event::handle('FreeNetworkActorCanAdmin', [$this, $other, &$canAdmin]);
|
|
|
|
return $canAdmin;
|
2021-12-21 19:09:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-23 17:06:11 +00:00
|
|
|
/**
|
|
|
|
* @method bool isPerson()
|
|
|
|
* @method bool isGroup()
|
|
|
|
* @method bool isBot()
|
|
|
|
*/
|
|
|
|
public function __call(string $name, array $arguments): mixed
|
|
|
|
{
|
|
|
|
if (Formatting::startsWith($name, 'is')) {
|
|
|
|
$type = Formatting::removePrefix($name, 'is');
|
|
|
|
$const = self::class . '::' . mb_strtoupper($type);
|
|
|
|
if (\defined($const)) {
|
|
|
|
return $this->type === \constant($const);
|
|
|
|
} else {
|
|
|
|
throw new BugFoundException("Actor cannot be a '{$type}', check your spelling");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return parent::__call($name, $arguments);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-29 20:56:35 +01:00
|
|
|
public static function schemaDef(): array
|
|
|
|
{
|
2021-10-28 17:34:01 +01:00
|
|
|
return [
|
2021-11-28 13:09:04 +00:00
|
|
|
'name' => 'actor',
|
2021-09-18 03:22:27 +01:00
|
|
|
'description' => 'local and remote users, groups and bots are actors, for instance',
|
2021-11-28 13:09:04 +00:00
|
|
|
'fields' => [
|
|
|
|
'id' => ['type' => 'serial', 'not null' => true, 'description' => 'unique identifier'],
|
|
|
|
'nickname' => ['type' => 'varchar', 'length' => 64, 'not null' => true, 'description' => 'nickname or username'],
|
2021-12-26 17:01:02 +00:00
|
|
|
'fullname' => ['type' => 'text', 'description' => 'display name', 'default' => null],
|
2021-12-18 04:24:27 +00:00
|
|
|
'roles' => ['type' => 'int', 'not null' => true, 'description' => 'Bitmap of permissions this actor has'],
|
2021-12-11 22:19:37 +00:00
|
|
|
'type' => ['type' => 'int', 'not null' => true, 'description' => 'The type of actor (person, group, bot, etc)'],
|
2021-12-26 17:01:02 +00:00
|
|
|
'homepage' => ['type' => 'text', 'description' => 'identifying URL', 'default' => null],
|
|
|
|
'bio' => ['type' => 'text', 'description' => 'descriptive biography', 'default' => null],
|
|
|
|
'location' => ['type' => 'text', 'description' => 'physical location', 'default' => null],
|
|
|
|
'lat' => ['type' => 'numeric', 'precision' => 10, 'scale' => 7, 'description' => 'latitude', 'default' => null],
|
|
|
|
'lon' => ['type' => 'numeric', 'precision' => 10, 'scale' => 7, 'description' => 'longitude', 'default' => null],
|
|
|
|
'location_id' => ['type' => 'int', 'description' => 'location id if possible', 'default' => null],
|
|
|
|
'location_service' => ['type' => 'int', 'description' => 'service used to obtain location id', 'default' => null],
|
2021-11-28 13:09:04 +00:00
|
|
|
'is_local' => ['type' => 'bool', 'not null' => true, 'description' => 'Does this actor have a LocalUser associated'],
|
|
|
|
'created' => ['type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was created'],
|
|
|
|
'modified' => ['type' => 'timestamp', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'],
|
2020-03-29 20:56:35 +01:00
|
|
|
],
|
|
|
|
'primary key' => ['id'],
|
2021-11-28 13:09:04 +00:00
|
|
|
'indexes' => [
|
2021-09-18 03:22:27 +01:00
|
|
|
'actor_nickname_idx' => ['nickname'],
|
2020-03-29 20:56:35 +01:00
|
|
|
],
|
2020-08-09 13:58:55 +01:00
|
|
|
'fulltext indexes' => [
|
2021-09-18 03:22:27 +01:00
|
|
|
'actor_fulltext_idx' => ['nickname', 'fullname', 'location', 'bio', 'homepage'],
|
2020-08-09 13:58:55 +01:00
|
|
|
],
|
2020-03-29 20:56:35 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|