. // }}} namespace Plugin\TagBasedFiltering; use App\Core\Cache; use App\Core\DB; use App\Core\Event; use function App\Core\I18n\_m; use App\Core\Modules\Plugin; use App\Core\Router; use App\Entity\Actor; use App\Entity\LocalUser; use App\Entity\Note; use Component\Tag\Entity\NoteTag; use Component\Tag\Entity\NoteTagBlock; use Functional as F; use Plugin\TagBasedFiltering\Controller as C; use Symfony\Component\HttpFoundation\Request; class TagBasedFiltering extends Plugin { public const NOTE_TAG_FILTER_ROUTE = 'filter_feeds_edit_blocked_note_tags'; public const ACTOR_TAG_FILTER_ROUTE = 'filter_feeds_edit_blocked_actor_tags'; public static function cacheKeys(int|LocalUser|Actor $actor_id): array { if (!\is_int($actor_id)) { $actor_id = $actor_id->getId(); } return ['note' => "blocked-note-tags-{$actor_id}", 'actor' => "blocked-actor-tags-{$actor_id}"]; } public function onAddRoute(Router $r) { $r->connect(id: self::NOTE_TAG_FILTER_ROUTE, uri_path: '/filter/edit-blocked-note-tags/{note_id<\d+>}', target: [Controller\AddBlocked::class, 'addBlockedNoteTags']); $r->connect(id: self::ACTOR_TAG_FILTER_ROUTE, uri_path: '/filter/edit-blocked-actor-tags/{actor_id<\d+>}', target: [Controller\AddBlocked::class, 'addBlockedActorTags']); return Event::next; } public function onAddExtraNoteActions(Request $request, Note $note, array &$actions) { $actions[] = [ 'title' => _m('Block note tags'), 'classes' => '', 'url' => Router::url(self::NOTE_TAG_FILTER_ROUTE, ['note_id' => $note->getId()]), ]; $actions[] = [ 'title' => _m('Block people tags'), 'classes' => '', 'url' => Router::url(self::ACTOR_TAG_FILTER_ROUTE, ['actor_id' => $note->getActor()->getId()]), ]; } /** * Filter out tags from notes or actors, per the user request */ public function onFilterNoteList(?Actor $actor, array &$notes, Request $request) { if (\is_null($actor)) { return Event::next; } $blocked_note_tags = Cache::get( self::cacheKeys($actor)['note'], fn () => DB::dql('select ntb from note_tag_block ntb where ntb.blocker = :blocker', ['blocker' => $actor->getId()]), ); $notes = F\reject( $notes, fn (Note $n) => ( $n->getActor()->getId() != $actor->getId() && ( F\some( NoteTag::getByNoteId($n->getId()), fn ($nt) => NoteTagBlock::checkBlocksNoteTag($nt, $blocked_note_tags), ) ) ), ); return Event::next; } public function onPopulateSettingsTabs(Request $request, string $section, array &$tabs): bool { if ($section === 'muting') { $tabs[] = [ 'title' => 'Muted note tags', 'desc' => 'Edit your muted note tags', 'id' => 'settings-muting-note-tags', 'controller' => C\EditBlocked::editBlockedNoteTags($request), ]; } return Event::next; } }