[ENTITY][Actor][ActorTag] Make Actor->getSelfTags and Actor->getOtherTags return [ActorCircle[], ActorTag[]], rather than ActorCrircle alone

This commit is contained in:
Hugo Sales 2021-11-29 15:31:02 +00:00 committed by Diogo Peralta Cordeiro
parent 3477ad5efc
commit 98568b6f53
Signed by: diogo
GPG Key ID: 18D2D35001FBFAB0
3 changed files with 35 additions and 43 deletions

View File

@ -29,6 +29,7 @@ use App\Entity\Actor;
use Component\Notification\Entity\Notification; use Component\Notification\Entity\Notification;
use DateTimeInterface; use DateTimeInterface;
use Exception; use Exception;
use Functional as F;
/** /**
* Entity for all activities we know about * Entity for all activities we know about
@ -160,13 +161,8 @@ class Activity extends Entity
*/ */
public function getNotificationTargetIdsFromActorTags(): array public function getNotificationTargetIdsFromActorTags(): array
{ {
$actors = []; [$actor_circles, $actor_tags] = $this->getActor()->getSelfTags();
$self_tags = $this->getActor()->getSelfTags(); return F\flat_map($actor_circles, fn ($circle) => $circle->getSubscribedActors());
foreach ($self_tags as $circle) {
// Get subscriptions
array_push($actors, ...$circle->getSubscribedActors());
}
return $actors;
} }
/** /**

View File

@ -266,9 +266,9 @@ class Actor extends Entity
} }
/** /**
* Tags attributed to self * Tags attributed to self, shortcut function for increased legibility
* *
* @return [ActorCircle] * @return [ActorCircle[], ActorTag[]]
*/ */
public function getSelfTags(bool $_test_force_recompute = false): array public function getSelfTags(bool $_test_force_recompute = false): array
{ {
@ -285,62 +285,58 @@ class Actor extends Entity
* @param null|int $offset Offset from latest * @param null|int $offset Offset from latest
* @param null|int $limit Max number to get * @param null|int $limit Max number to get
* *
* @return [ActorCircle] resulting lists * @return [ActorCircle[], ActorTag[]] resulting lists
*/ */
public function getOtherTags(self|int|null $scoped = null, ?int $offset = null, ?int $limit = null, bool $_test_force_recompute = false): array public function getOtherTags(self|int|null $scoped = null, ?int $offset = null, ?int $limit = null, bool $_test_force_recompute = false): array
{ {
if (\is_null($scoped)) { if (\is_null($scoped)) {
return Cache::get( return Cache::get(
"othertags-{$this->getId()}", "othertags-{$this->getId()}",
fn () => DB::dql( fn () => (($t = DB::dql(
<<< 'EOQ' <<< 'EOQ'
SELECT circle SELECT circle, tag
FROM App\Entity\ActorTag tag FROM actor_tag tag
JOIN App\Entity\ActorCircle circle JOIN actor_circle circle
WITH WITH tag.tagger = circle.tagger
tag.tagger = circle.tagger AND tag.tag = circle.tag
AND tag.tag = circle.tag
WHERE tag.tagged = :id WHERE tag.tagged = :id
ORDER BY tag.modified DESC, tag.tagged DESC ORDER BY tag.modified DESC, tag.tagged DESC
EOQ, EOQ,
['id' => $this->getId()], ['id' => $this->getId()],
['offset' => $offset, options: ['offset' => $offset, 'limit' => $limit],
'limit' => $limit, ], )) === [] ? [[],[]] : $t),
),
); );
} else { } else {
$scoped_id = \is_int($scoped) ? $scoped : $scoped->getId(); $scoped_id = \is_int($scoped) ? $scoped : $scoped->getId();
return Cache::get( return Cache::get(
"othertags-{$this->getId()}-by-{$scoped_id}", "othertags-{$this->getId()}-by-{$scoped_id}",
fn () => DB::dql( fn () => (($t = DB::dql(
<<< 'EOQ' <<< 'EOQ'
SELECT circle SELECT circle, tag
FROM App\Entity\ActorTag tag FROM actor_tag tag
JOIN App\Entity\ActorCircle circle JOIN actor_circle circle
WITH WITH
tag.tagger = circle.tagger tag.tagger = circle.tagger
AND tag.tag = circle.tag AND tag.tag = circle.tag
WHERE WHERE
tag.tagged = :id tag.tagged = :id
AND ( circle.private != true AND (circle.private != true
OR ( circle.tagger = :scoped OR (circle.tagger = :scoped
AND circle.private = true AND circle.private = true
) )
) )
ORDER BY tag.modified DESC, tag.tagged DESC ORDER BY tag.modified DESC, tag.tagged DESC
EOQ, EOQ,
['id' => $this->getId(), ['id' => $this->getId(), 'scoped' => $scoped_id],
'scoped' => $scoped_id, ], options: ['offset' => $offset, 'limit' => $limit],
['offset' => $offset, )) === [] ? [[],[]] : $t),
'limit' => $limit, ],
),
); );
} }
} }
/** /**
* @param array $tags array of strings to become self tags * @param array $tags array of strings to become self tags
* @param null|array $existing array of existing self tags (actor_circle[]) * @param null|array $existing array of existing self tags (ActorTag[])
* *
* @throws \App\Util\Exception\DuplicateFoundException * @throws \App\Util\Exception\DuplicateFoundException
* @throws NotFoundException * @throws NotFoundException
@ -350,21 +346,21 @@ class Actor extends Entity
public function setSelfTags(array $tags, ?array $existing = null): self public function setSelfTags(array $tags, ?array $existing = null): self
{ {
if (\is_null($existing)) { if (\is_null($existing)) {
$existing = $this->getSelfTags(); [$_, $existing] = $this->getSelfTags();
} }
$existing_actor_circles = F\map($existing, fn ($actor_circle) => $actor_circle->getTag()); $existing_actor_tags = F\map($existing, fn ($actor_tag) => $actor_tag->getTag());
$tags_to_add = array_diff($tags, $existing_actor_circles); $tags_to_add = array_diff($tags, $existing_actor_tags);
$tags_to_remove = array_diff($existing_actor_circles, $tags); $tags_to_remove = array_diff($existing_actor_tags, $tags);
$actor_circles_to_remove = F\filter($existing, fn ($actor_circle) => \in_array($actor_circle->getTag(), $tags_to_remove)); $actor_tags_to_remove = F\filter($existing, fn ($actor_tag) => \in_array($actor_tag->getTag(), $tags_to_remove));
foreach ($tags_to_add as $tag) { foreach ($tags_to_add as $tag) {
$canonical_tag = TagComponent::canonicalTag($tag, $this->getTopLanguage()->getLocale()); $canonical_tag = TagComponent::canonicalTag($tag, $this->getTopLanguage()->getLocale());
DB::persist(ActorCircle::create(['tagger' => $this->getId(), 'tag' => $canonical_tag, 'private' => false])); DB::persist(ActorCircle::create(['tagger' => $this->getId(), 'tag' => $canonical_tag, 'private' => false]));
DB::persist(ActorTag::create(['tagger' => $this->id, 'tagged' => $this->id, 'tag' => $tag, 'canonical' => $canonical_tag])); DB::persist(ActorTag::create(['tagger' => $this->id, 'tagged' => $this->id, 'tag' => $tag, 'canonical' => $canonical_tag]));
} }
foreach ($actor_circles_to_remove as $actor_circle) { foreach ($actor_tags_to_remove as $actor_tag) {
$canonical_tag = TagComponent::canonicalTag($actor_circle->getTag(), $this->getTopLanguage()->getLocale()); $canonical_tag = TagComponent::canonicalTag($actor_tag->getTag(), $this->getTopLanguage()->getLocale());
DB::removeBy('actor_tag', ['tagger' => $this->getId(), 'tagged' => $this->getId(), 'canonical' => $canonical_tag]); DB::removeBy('actor_tag', ['tagger' => $this->getId(), 'tagged' => $this->getId(), 'canonical' => $canonical_tag]);
DB::removeBy('actor_circle', ['id' => $actor_circle->getId()]); DB::removeBy('actor_circle', ['tagger' => $this->getId(), 'tag' => $canonical_tag]);
} }
Cache::delete("selftags-{$this->getId()}"); Cache::delete("selftags-{$this->getId()}");
Cache::delete("othertags-{$this->getId()}-by-{$this->getId()}"); Cache::delete("othertags-{$this->getId()}-by-{$this->getId()}");

View File

@ -1,6 +1,6 @@
{% set actor_nickname = actor.getNickname() %} {% set actor_nickname = actor.getNickname() %}
{% set actor_avatar = actor.getAvatarUrl() %} {% set actor_avatar = actor.getAvatarUrl() %}
{% set actor_tags = actor.getSelfTags() %} {% set actor_tags = actor.getSelfTags()[1] %} {# Take only the actor_tags, not the circles #}
{% set actor_bio = actor.getBio() %} {% set actor_bio = actor.getBio() %}
{% set actor_uri = actor.getUri() %} {% set actor_uri = actor.getUri() %}
@ -28,8 +28,8 @@
<nav class="profile-info-tags"> <nav class="profile-info-tags">
{% if actor_tags %} {% if actor_tags %}
{% for tag in actor_tags %} {# Actually a list of actor_circle #} {% for tag in actor_tags %}
{% include 'cards/tag/actor_tag.html.twig' with { 'tag': tag.getActorTag(), 'actor': actor } %} {% include 'cards/tag/actor_tag.html.twig' with { 'tag': tag, 'actor': actor } %}
{% endfor %} {% endfor %}
{% else %} {% else %}
{{ '(No tags)' | trans }} {{ '(No tags)' | trans }}