[ENTITY][Actor] Add is_local, it's common to depend, and this makes it much faster, with a low space cost

This commit is contained in:
Diogo Peralta Cordeiro 2021-11-16 23:24:06 +00:00
parent f1a30ac0e6
commit 89d36a68e5
Signed by: diogo
GPG Key ID: 18D2D35001FBFAB0
6 changed files with 35 additions and 37 deletions

View File

@ -145,9 +145,7 @@ class ActivitypubRsa extends Entity
$apRSA = self::getWithPK(['actor_id' => ($actor_id = $gsactor->getId())]);
if (!$apRSA instanceof self) {
// Nonexistent key pair for this profile
try {
// throws exception if remote
$gsactor->getLocalUser();
if ($gsactor->getIsLocal()) {
self::generateKeys($private_key, $public_key);
@ -155,7 +153,7 @@ class ActivitypubRsa extends Entity
$apRSA->setActorId($actor_id);
$apRSA->setPrivateKey($private_key);
$apRSA->setPublicKey($public_key);
} catch (Exception) {
} else {
// ASSERT: This should never happen, but try to recover!
Log::error("Activitypub_rsa: An impossible thing has happened... Please let the devs know.");
if ($fetch) {

View File

@ -239,6 +239,7 @@ class Explorer
'created' => new DateTime($res['published'] ?? 'now'),
'bio' => isset($res['summary']) ? mb_substr(Security::sanitize($res['summary']), 0, 1000) : null,
'homepage' => $res['url'] ?? $res['id'],
'is_local' => false,
'modified' => new DateTime(),
];

View File

@ -5,6 +5,7 @@ declare(strict_types = 1);
namespace Plugin\ActivityPub\Util\Response;
use App\Entity\Actor;
use App\Util\Exception\ClientException;
use Exception;
use Plugin\ActivityPub\Util\Model\EntityToType\GSActorToType;
@ -17,7 +18,10 @@ abstract class ActorResponse
*/
public static function handle(Actor $gsactor, int $status = 200): TypeResponse
{
$gsactor->getLocalUser(); // This throws exception if not a local user, which is intended
return new TypeResponse(data: GSActorToType::translate($gsactor), status: $status);
if ($gsactor->getIsLocal()) {
return new TypeResponse(data: GSActorToType::translate($gsactor), status: $status);
} else {
throw new ClientException('This is a remote actor, you should request it to its source of authority instead.');
}
}
}

View File

@ -148,7 +148,7 @@ class Security extends Controller
try {
// This already checks if the nickname is being used
$actor = Actor::create(['nickname' => $sanitized_nickname]);
$actor = Actor::create(['nickname' => $sanitized_nickname, 'is_local' => true]);
$user = LocalUser::create([
'nickname' => $sanitized_nickname,
'outgoing_email' => $data['email'],

View File

@ -30,6 +30,7 @@ use App\Core\Router\Router;
use App\Core\UserRoles;
use App\Util\Common;
use App\Util\Exception\NicknameException;
use App\Util\Exception\NotFoundException;
use App\Util\Nickname;
use Component\Avatar\Avatar;
use DateTimeInterface;
@ -56,7 +57,7 @@ class Actor extends Entity
private int $id;
private string $nickname;
private ?string $fullname = null;
private int $roles = 4;
private int $roles = 4;
private ?string $homepage;
private ?string $bio;
private ?string $location;
@ -64,6 +65,7 @@ class Actor extends Entity
private ?float $lon;
private ?int $location_id;
private ?int $location_service;
private bool $is_local;
private DateTimeInterface $created;
private DateTimeInterface $modified;
@ -191,6 +193,17 @@ class Actor extends Entity
return $this->location_service;
}
public function setIsLocal(bool $is_local): self
{
$this->is_local = $is_local;
return $this;
}
public function getIsLocal(): bool
{
return $this->is_local;
}
public function setCreated(DateTimeInterface $created): self
{
$this->created = $created;
@ -218,7 +231,11 @@ class Actor extends Entity
public function getLocalUser()
{
return DB::findOneBy('local_user', ['id' => $this->getId()]);
if ($this->getIsLocal()) {
return DB::findOneBy('local_user', ['id' => $this->getId()]);
} else {
throw new NotFoundException('This is a remote actor.');
}
}
public function getAvatarUrl(string $size = 'full')
@ -353,7 +370,7 @@ class Actor extends Entity
}
/**
* Get the most appropraite language for $this to use when
* Get the most appropriate language for $this to use when
* referring to $context (a reply or a group, for instance)
*
* @return Language[]
@ -395,6 +412,7 @@ class Actor extends Entity
'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'],
'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'],
],

View File

@ -49,9 +49,8 @@ class Note extends Entity
private ?string $content_type = null;
private ?string $content = null;
private ?string $rendered = null;
private ?bool $is_local;
private bool $is_local;
private ?string $source;
private ?int $conversation;
private int $scope = VisibilityScope::PUBLIC;
private string $url;
private string $language;
@ -116,13 +115,13 @@ class Note extends Entity
return $this->rendered;
}
public function setIsLocal(?bool $is_local): self
public function setIsLocal(bool $is_local): self
{
$this->is_local = $is_local;
return $this;
}
public function getIsLocal(): ?bool
public function getIsLocal(): bool
{
return $this->is_local;
}
@ -138,17 +137,6 @@ class Note extends Entity
return $this->source;
}
public function setConversation(?int $conversation): self
{
$this->conversation = $conversation;
return $this;
}
public function getConversation(): ?int
{
return $this->conversation;
}
public function setScope(int $scope): self
{
$this->scope = $scope;
@ -290,15 +278,6 @@ class Note extends Entity
));
}
/**
* @return Actor[]
*/
public function getAttentionProfiles(): array
{
// TODO implement
return [];
}
public static function schemaDef(): array
{
return [
@ -309,9 +288,8 @@ class Note extends Entity
'content' => ['type' => 'text', 'description' => 'note content'],
'content_type' => ['type' => 'varchar', 'not null' => true, 'default' => 'text/plain', 'length' => 129, 'description' => 'A note can be written in a multitude of formats such as text/plain, text/markdown, application/x-latex, and text/html'],
'rendered' => ['type' => 'text', 'description' => 'rendered note content, so we can keep the microtags (if not local)'],
'is_local' => ['type' => 'bool', 'description' => 'was this note generated by a local actor'],
'is_local' => ['type' => 'bool', 'not null' => true, 'description' => 'was this note generated by a local actor'],
'source' => ['type' => 'varchar', 'foreign key' => true, 'length' => 32, 'target' => 'NoteSource.code', 'multiplicity' => 'many to one', 'description' => 'fkey to source of note, like "web", "im", or "clientname"'],
'conversation' => ['type' => 'int', 'foreign key' => true, 'target' => 'Conversation.id', 'multiplicity' => 'one to one', 'description' => 'the local conversation id'],
'scope' => ['type' => 'int', 'not null' => true, 'default' => VisibilityScope::PUBLIC, 'description' => 'bit map for distribution scope; 0 = everywhere; 1 = this server only; 2 = addressees; 4 = groups; 8 = subscribers; 16 = messages; null = default'],
'url' => ['type' => 'text', 'description' => 'Permalink to Note'],
'language' => ['type' => 'int', 'foreign key' => true, 'target' => 'Language.id', 'multiplicity' => 'one to many', 'description' => 'The language for this note'],
@ -323,7 +301,6 @@ class Note extends Entity
'note_created_id_is_local_idx' => ['created', 'is_local'],
'note_actor_created_idx' => ['actor_id', 'created'],
'note_is_local_created_actor_idx' => ['is_local', 'created', 'actor_id'],
'note_conversation_created_idx' => ['conversation', 'created'],
],
'fulltext indexes' => ['notice_fulltext_idx' => ['content']], // TODO make this configurable
];