2020-03-29 20:56:35 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
// {{{ 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;
|
2020-11-29 04:55:23 +00:00
|
|
|
use App\Core\Event;
|
2020-07-22 12:40:53 +01:00
|
|
|
use App\Core\UserRoles;
|
2020-05-10 21:43:15 +01:00
|
|
|
use DateTimeInterface;
|
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
|
|
|
|
*/
|
2020-08-13 00:57:22 +01:00
|
|
|
class GSActor 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-05-05 13:19:10 +01:00
|
|
|
private string $normalized_nickname;
|
2020-03-30 16:13:51 +01:00
|
|
|
private ?string $fullname;
|
2020-07-25 03:03:16 +01:00
|
|
|
private int $roles = 4;
|
2020-03-30 16:13:51 +01:00
|
|
|
private ?string $homepage;
|
|
|
|
private ?string $bio;
|
|
|
|
private ?string $location;
|
|
|
|
private ?float $lat;
|
|
|
|
private ?float $lon;
|
|
|
|
private ?int $location_id;
|
2020-06-30 19:20:50 +01:00
|
|
|
private ?int $location_service;
|
2021-04-27 22:24:48 +01: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
|
|
|
|
{
|
|
|
|
$this->nickname = $nickname;
|
|
|
|
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-05-05 13:19:10 +01:00
|
|
|
public function setNormalizedNickname(string $normalized_nickname): self
|
|
|
|
{
|
|
|
|
$this->normalized_nickname = $normalized_nickname;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getNormalizedNickname(): string
|
|
|
|
{
|
|
|
|
return $this->normalized_nickname;
|
|
|
|
}
|
|
|
|
|
2020-03-30 16:13:51 +01:00
|
|
|
public function setFullname(?string $fullname): self
|
|
|
|
{
|
|
|
|
$this->fullname = $fullname;
|
|
|
|
return $this;
|
|
|
|
}
|
2020-08-08 17:10:25 +01:00
|
|
|
|
2020-03-30 16:13:51 +01:00
|
|
|
public function getFullname(): ?string
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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-05-05 13:19:10 +01: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-05-05 13:19:10 +01:00
|
|
|
public function getCreated(): DateTimeInterface
|
2020-03-30 16:13:51 +01:00
|
|
|
{
|
|
|
|
return $this->created;
|
|
|
|
}
|
|
|
|
|
2021-05-05 13:19:10 +01: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-05-05 13:19:10 +01: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
|
|
|
|
2020-11-29 04:55:23 +00:00
|
|
|
public function getAvatarUrl()
|
|
|
|
{
|
|
|
|
$url = null;
|
2021-04-29 17:42:06 +01:00
|
|
|
Event::handle('GetAvatarUrl', [$this->getId(), &$url]);
|
2020-11-29 04:55:23 +00:00
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
|
2020-08-09 13:44:47 +01:00
|
|
|
public static function getFromId(int $id): ?self
|
2020-07-25 03:03:16 +01:00
|
|
|
{
|
2020-08-19 16:32:45 +01:00
|
|
|
return Cache::get('gsactor-id-' . $id, function () use ($id) {
|
|
|
|
return DB::find('gsactor', ['id' => $id]);
|
|
|
|
});
|
2020-08-08 17:10:25 +01:00
|
|
|
}
|
2020-07-25 03:03:16 +01:00
|
|
|
|
2020-08-09 13:44:47 +01:00
|
|
|
public static function getFromNickname(string $nickname): ?self
|
2020-08-08 17:10:25 +01:00
|
|
|
{
|
2020-08-19 16:32:45 +01:00
|
|
|
return Cache::get('gsactor-nick-' . $nickname, function () use ($nickname) {
|
|
|
|
return DB::findOneBy('gsactor', ['nickname' => $nickname]);
|
|
|
|
});
|
2020-08-08 17:10:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function getNicknameFromId(int $id): string
|
|
|
|
{
|
2020-08-19 16:32:45 +01:00
|
|
|
return Cache::get('gsactor-nick-id-' . $id, function () use ($id) {
|
|
|
|
return self::getFromId($id)->getNickname();
|
|
|
|
});
|
2020-08-08 17:10:25 +01:00
|
|
|
}
|
|
|
|
|
2021-08-04 21:06:58 +01:00
|
|
|
public function getSelfTags(bool $_test_force_recompute = false): array
|
2020-08-08 17:10:25 +01:00
|
|
|
{
|
2020-08-15 08:03:58 +01:00
|
|
|
return Cache::get('selftags-' . $this->id,
|
|
|
|
function () {
|
|
|
|
return DB::findBy('gsactor_tag', ['tagger' => $this->id, 'tagged' => $this->id]);
|
2021-08-04 21:06:58 +01:00
|
|
|
}, beta: $_test_force_recompute ? INF : 1.0);
|
2020-08-08 17:10:25 +01:00
|
|
|
}
|
|
|
|
|
2020-08-15 07:08:52 +01:00
|
|
|
public function setSelfTags(array $tags, array $existing): void
|
2020-08-08 17:10:25 +01:00
|
|
|
{
|
2020-08-15 07:08:52 +01:00
|
|
|
$tag_existing = F\map($existing, function ($pt) { return $pt->getTag(); });
|
2020-08-08 17:10:25 +01:00
|
|
|
$tag_to_add = array_diff($tags, $tag_existing);
|
|
|
|
$tag_to_remove = array_diff($tag_existing, $tags);
|
2020-08-15 07:08:52 +01:00
|
|
|
$pt_to_remove = F\filter($existing, function ($pt) use ($tag_to_remove) { return in_array($pt->getTag(), $tag_to_remove); });
|
2020-08-08 17:10:25 +01:00
|
|
|
foreach ($tag_to_add as $tag) {
|
2020-08-15 07:08:52 +01:00
|
|
|
$pt = GSActorTag::create(['tagger' => $this->id, 'tagged' => $this->id, 'tag' => $tag]);
|
2020-08-08 17:10:25 +01:00
|
|
|
DB::persist($pt);
|
|
|
|
}
|
|
|
|
foreach ($pt_to_remove as $pt) {
|
|
|
|
DB::remove($pt);
|
|
|
|
}
|
2020-08-15 08:03:58 +01:00
|
|
|
Cache::delete('selftags-' . $this->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getFollowersCount()
|
|
|
|
{
|
|
|
|
return Cache::get('followers-' . $this->id,
|
|
|
|
function () {
|
|
|
|
return DB::dql('select count(f) from App\Entity\Follow f where f.followed = :followed',
|
2020-09-05 03:34:01 +01:00
|
|
|
['followed' => $this->id])[0][1] - 1; // Remove self follow
|
2020-08-15 08:03:58 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getFollowedCount()
|
|
|
|
{
|
|
|
|
return Cache::get('followed-' . $this->id,
|
|
|
|
function () {
|
|
|
|
return DB::dql('select count(f) from App\Entity\Follow f where f.follower = :follower',
|
2020-09-05 03:34:01 +01:00
|
|
|
['follower' => $this->id])[0][1] - 1; // Remove self follow
|
2020-08-15 08:03:58 +01:00
|
|
|
});
|
2020-07-25 03:03:16 +01:00
|
|
|
}
|
|
|
|
|
2020-03-29 20:56:35 +01:00
|
|
|
public static function schemaDef(): array
|
|
|
|
{
|
|
|
|
$def = [
|
2020-08-13 00:57:22 +01:00
|
|
|
'name' => 'gsactor',
|
|
|
|
'description' => 'local and remote users, groups and bots are gsactors, for instance',
|
2020-03-29 20:56:35 +01:00
|
|
|
'fields' => [
|
2021-07-28 22:20:32 +01:00
|
|
|
'id' => ['type' => 'serial', 'not null' => true, 'description' => 'unique identifier'],
|
|
|
|
'nickname' => ['type' => 'varchar', 'length' => 64, 'not null' => true, 'description' => 'nickname or username'],
|
|
|
|
'fullname' => ['type' => 'text', 'description' => 'display name'],
|
|
|
|
'roles' => ['type' => 'int', 'not null' => true, 'default' => UserRoles::USER, 'description' => 'Bitmap of permissions this gsactor has'],
|
|
|
|
'homepage' => ['type' => 'text', 'description' => 'identifying URL'],
|
|
|
|
'bio' => ['type' => 'text', 'description' => 'descriptive biography'],
|
|
|
|
'location' => ['type' => 'text', 'description' => 'physical location'],
|
|
|
|
'lat' => ['type' => 'numeric', 'precision' => 10, 'scale' => 7, 'description' => 'latitude'],
|
|
|
|
'lon' => ['type' => 'numeric', 'precision' => 10, 'scale' => 7, 'description' => 'longitude'],
|
|
|
|
'location_id' => ['type' => 'int', 'description' => 'location id if possible'],
|
|
|
|
'location_service' => ['type' => 'int', 'description' => 'service used to obtain location id'],
|
|
|
|
'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-04-11 12:03:32 +01:00
|
|
|
'indexes' => [
|
2021-07-28 22:20:32 +01:00
|
|
|
'gsactor_nickname_idx' => ['nickname'],
|
2020-03-29 20:56:35 +01:00
|
|
|
],
|
2020-08-09 13:58:55 +01:00
|
|
|
'fulltext indexes' => [
|
2020-08-13 00:57:22 +01:00
|
|
|
'gsactor_fulltext_idx' => ['nickname', 'fullname', 'location', 'bio', 'homepage'],
|
2020-08-09 13:58:55 +01:00
|
|
|
],
|
2020-03-29 20:56:35 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
return $def;
|
|
|
|
}
|
|
|
|
}
|