[ENTITY][ActorTag][Actor][Activity] Add Actor::getActorCircles

This commit is contained in:
Hugo Sales 2021-12-23 14:36:12 +00:00 committed by Diogo Peralta Cordeiro
parent 95783d6109
commit 764a30695d
Signed by: diogo
GPG Key ID: 18D2D35001FBFAB0
3 changed files with 24 additions and 19 deletions

View File

@ -1,5 +1,7 @@
<?php <?php
declare(strict_types = 1);
// {{{ License // {{{ License
// This file is part of GNU social - https://www.gnu.org/software/social // This file is part of GNU social - https://www.gnu.org/software/social
@ -23,12 +25,8 @@ namespace App\Entity;
use App\Core\DB\DB; use App\Core\DB\DB;
use App\Core\Entity; use App\Core\Entity;
use App\Core\Event;
use App\Core\Log;
use App\Entity\Actor;
use Component\Notification\Entity\Notification; use Component\Notification\Entity\Notification;
use DateTimeInterface; use DateTimeInterface;
use Exception;
use Functional as F; use Functional as F;
/** /**
@ -52,7 +50,7 @@ class Activity extends Entity
private string $object_type; private string $object_type;
private int $object_id; private int $object_id;
private ?string $source; private ?string $source;
private \DateTimeInterface $created; private DateTimeInterface $created;
public function setId(int $id): self public function setId(int $id): self
{ {
@ -144,12 +142,9 @@ class Activity extends Entity
return DB::findOneBy($this->getObjectType(), ['id' => $this->getObjectId()]); return DB::findOneBy($this->getObjectType(), ['id' => $this->getObjectId()]);
} }
/**
* @return array
*/
public function getNotificationTargetIdsFromActorTags(): array public function getNotificationTargetIdsFromActorTags(): array
{ {
[$actor_circles, $actor_tags] = $this->getActor()->getSelfTags(); $actor_circles = $this->getActor()->getActorCircles();
return F\flat_map($actor_circles, fn ($circle) => $circle->getSubscribedActors()); return F\flat_map($actor_circles, fn ($circle) => $circle->getSubscribedActors());
} }
@ -163,31 +158,31 @@ class Activity extends Entity
$target_ids = []; $target_ids = [];
// Actor Circles // Actor Circles
if (array_key_exists('actor_circle', $ids_already_known)) { if (\array_key_exists('actor_circle', $ids_already_known)) {
array_push($target_ids, ...$ids_already_known['actor_circle']); array_push($target_ids, ...$ids_already_known['actor_circle']);
} else { } else {
array_push($target_ids, ...$this->getNotificationTargetIdsFromActorTags()); array_push($target_ids, ...$this->getNotificationTargetIdsFromActorTags());
} }
// Notifications // Notifications
if (array_key_exists('notification_activity', $ids_already_known)) { if (\array_key_exists('notification_activity', $ids_already_known)) {
array_push($target_ids, ...$ids_already_known['notification_activity']); array_push($target_ids, ...$ids_already_known['notification_activity']);
} else { } else {
array_push($target_ids, ...Notification::getNotificationTargetIdsByActivity($this->getId())); array_push($target_ids, ...Notification::getNotificationTargetIdsByActivity($this->getId()));
} }
// Object's targets // Object's targets
if (array_key_exists('object', $ids_already_known)) { if (\array_key_exists('object', $ids_already_known)) {
array_push($target_ids, ...$ids_already_known['object']); array_push($target_ids, ...$ids_already_known['object']);
} else { } else {
if (!is_null($author = $this->getObject()?->getActorId()) && $author !== $sender_id) { if (!\is_null($author = $this->getObject()?->getActorId()) && $author !== $sender_id) {
$target_ids[] = $this->getObject()->getActorId(); $target_ids[] = $this->getObject()->getActorId();
} }
array_push($target_ids, ...$this->getObject()->getNotificationTargetIds($ids_already_known)); array_push($target_ids, ...$this->getObject()->getNotificationTargetIds($ids_already_known));
} }
// Additional actors that should know about this // Additional actors that should know about this
if (array_key_exists('additional', $ids_already_known)) { if (\array_key_exists('additional', $ids_already_known)) {
array_push($target_ids, ...$ids_already_known['additional']); array_push($target_ids, ...$ids_already_known['additional']);
} }

View File

@ -258,7 +258,8 @@ class Actor extends Entity
'id' => "actor-id-{$actor_id}", 'id' => "actor-id-{$actor_id}",
'nickname' => "actor-nickname-id-{$actor_id}", 'nickname' => "actor-nickname-id-{$actor_id}",
'fullname' => "actor-fullname-id-{$actor_id}", 'fullname' => "actor-fullname-id-{$actor_id}",
'tags' => \is_null($other) ? "actor-circles-and-tags-{$actor_id}" : "actor-circles-and-tags-{$actor_id}-by-{$other}", // $other is $context_id 'tags' => \is_null($other) ? "actor-tags-{$actor_id}" : "actor-tags-{$actor_id}-by-{$other}", // $other is $context_id
'circles' => "actor-circles-{$actor_id}",
'subscriber' => "subscriber-{$actor_id}", 'subscriber' => "subscriber-{$actor_id}",
'subscribed' => "subscribed-{$actor_id}", 'subscribed' => "subscribed-{$actor_id}",
'relative-nickname' => "actor-{$actor_id}-relative-nickname-{$other}", // $other is $nickname 'relative-nickname' => "actor-{$actor_id}-relative-nickname-{$other}", // $other is $nickname
@ -362,7 +363,7 @@ class Actor extends Entity
public function getOtherTags(self|int|null $context = null, ?int $offset = null, ?int $limit = null, bool $_test_force_recompute = false): array public function getOtherTags(self|int|null $context = null, ?int $offset = null, ?int $limit = null, bool $_test_force_recompute = false): array
{ {
if (\is_null($context)) { if (\is_null($context)) {
return Cache::get( return Cache::getList(
self::cacheKeys($this->getId())['tags'], self::cacheKeys($this->getId())['tags'],
fn () => DB::dql( fn () => DB::dql(
<<< 'EOQ' <<< 'EOQ'
@ -377,7 +378,7 @@ class Actor extends Entity
); );
} else { } else {
$context_id = \is_int($context) ? $context : $context->getId(); $context_id = \is_int($context) ? $context : $context->getId();
return Cache::get( return Cache::getList(
self::cacheKeys($this->getId(), $context_id)['tags'], self::cacheKeys($this->getId(), $context_id)['tags'],
fn () => DB::dql( fn () => DB::dql(
<<< 'EOQ' <<< 'EOQ'
@ -393,6 +394,14 @@ class Actor extends Entity
} }
} }
public function getActorCircles()
{
return Cache::getList(
self::cacheKeys($this->getId())['circles'],
fn () => DB::findBy('actor_circle', ['tagger' => $this->getId()]),
);
}
private function getSubCount(string $which, string $column): int private function getSubCount(string $which, string $column): int
{ {
return Cache::get( return Cache::get(

View File

@ -167,9 +167,9 @@ class ActorCircle extends Entity
'name' => 'actor_circle', 'name' => 'actor_circle',
'description' => 'a actor can have lists of actors, to separate their feed', 'description' => 'a actor can have lists of actors, to separate their feed',
'fields' => [ 'fields' => [
'id' => ['type' => 'serial', 'not null' => true, 'description' => 'unique identifier'], 'id' => ['type' => 'serial', 'not null' => true, 'description' => 'unique identifier'],
// An actor can be tagged by many actors // An actor can be tagged by many actors
'tagger' => ['type' => 'int', 'foreign key' => true, 'target' => 'Actor.id', 'multiplicity' => 'many to one', 'name' => 'actor_list_tagger_fkey', 'not null' => true, 'description' => 'user making the tag'], 'tagger' => ['type' => 'int', 'foreign key' => true, 'target' => 'Actor.id', 'multiplicity' => 'many to one', 'name' => 'actor_list_tagger_fkey', 'not null' => true, 'description' => 'user making the tag'],
// Many Actor Circles can reference (and probably will) an Actor Tag // Many Actor Circles can reference (and probably will) an Actor Tag
'tag' => ['type' => 'varchar', 'length' => 64, 'foreign key' => true, 'target' => 'ActorTag.tag', 'multiplicity' => 'many to one', 'not null' => true, 'description' => 'actor tag'], // Join with ActorTag // // so, Doctrine doesn't like that the target is not unique, even though the pair is 'tag' => ['type' => 'varchar', 'length' => 64, 'foreign key' => true, 'target' => 'ActorTag.tag', 'multiplicity' => 'many to one', 'not null' => true, 'description' => 'actor tag'], // Join with ActorTag // // so, Doctrine doesn't like that the target is not unique, even though the pair is
'description' => ['type' => 'text', 'description' => 'description of the people tag'], 'description' => ['type' => 'text', 'description' => 'description of the people tag'],
@ -182,6 +182,7 @@ class ActorCircle extends Entity
'actor_list_modified_idx' => ['modified'], 'actor_list_modified_idx' => ['modified'],
'actor_list_tag_idx' => ['tag'], 'actor_list_tag_idx' => ['tag'],
'actor_list_tagger_tag_idx' => ['tagger', 'tag'], 'actor_list_tagger_tag_idx' => ['tagger', 'tag'],
'actor_list_tagger_idx' => ['tagger'],
], ],
]; ];
} }