. // }}} namespace Plugin\RelatedTags; use App\Core\Cache; use App\Core\DB\DB; use App\Core\Event; use App\Core\Modules\Plugin; use App\Entity\ActorTag; use App\Entity\NoteTag; use Symfony\Component\HttpFoundation\Request; class RelatedTags extends Plugin { /** * Add a pinnned block containing tags related to the current page, be it note tags or actor tags */ public function onAddPinnedFeedContent(Request $request, array &$pinned) { $tags = $request->attributes->get('tags'); $tags = !\is_null($tags) ? explode('-', $tags) : [$request->attributes->get('tag')]; switch ($request->attributes->get('_route')) { case 'single_note_tag': // fall-through case 'multi_note_tag': $related = Cache::getList( 'related-note-tags-' . implode('-', $tags), fn () => DB::sql( <<<'EOQ' select distinct on (nt.canonical) canonical, nt.tag, nt.note_id, nt.created from note_tag nt where nt.note_id in (select n.id from note n join note_tag nt on n.id = nt.note_id where nt.canonical in (:tags)) and not nt.canonical in (:tags) limit 5 EOQ, ['tags' => $tags], entities: ['nt' => NoteTag::class], ), ); $pinned[] = ['template' => 'related_tags/note_tags.html.twig', 'vars' => $related]; break; case 'single_actor_tag': // fall-through case 'multi_actor_tag': $related = Cache::getList( 'related-actor-tags-' . implode('-', $tags), fn () => DB::sql( <<<'EOQ' select distinct on (at.canonical) canonical, at.tagger, at.tagged, at.tag, at.modified from actor_tag at where at.tagged in (select at.tagged from actor_tag at where at.canonical in (:tags)) and not at.canonical in (:tags) limit 5 EOQ, ['tags' => $tags], entities: ['at' => ActorTag::class], ), ); $pinned[] = ['template' => 'related_tags/actor_tags.html.twig', 'vars' => $related]; break; } return Event::next; } }