Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Timeline] clean event data with user context
- Loading branch information
Showing
14 changed files
with
199 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace App\Event; | ||
|
||
class EventCleaner | ||
{ | ||
public function cleanEventData(array $eventData): array | ||
{ | ||
foreach ($eventData as $key => $value) { | ||
if (!\in_array($key, ['name', 'title', 'objectID', 'uuid', 'slug', 'time_zone', 'begin_at', 'finish_at', 'status', 'visibility', 'image', 'image_url', 'link', 'category'])) { | ||
$eventData[$key] = null; | ||
} | ||
} | ||
|
||
return $eventData; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace App\JeMengage\Timeline; | ||
|
||
use App\Algolia\SearchService; | ||
use App\Entity\Adherent; | ||
use App\Entity\Algolia\AlgoliaJeMengageTimelineFeed; | ||
use App\JeMengage\Timeline\FeedProcessor\FeedProcessorInterface; | ||
|
||
class DataProvider | ||
{ | ||
public function __construct( | ||
private readonly SearchService $search, | ||
private readonly iterable $processors, | ||
) { | ||
} | ||
|
||
public function findItems(Adherent $user, int $page, array $filters, array $tagFilters): array | ||
{ | ||
$timelineFeeds = $this->search->rawSearch(AlgoliaJeMengageTimelineFeed::class, '', [ | ||
'page' => $page, | ||
'attributesToHighlight' => [], | ||
'filters' => implode(' OR ', $filters), | ||
'tagFilters' => $tagFilters, | ||
]); | ||
|
||
$timelineFeeds['hits'] = $this->processItems($user, $timelineFeeds['hits']); | ||
|
||
return $timelineFeeds; | ||
} | ||
|
||
private function processItems(Adherent $user, array $items): array | ||
{ | ||
$context = []; | ||
foreach ($items as &$item) { | ||
$item = $this->getProcessor($item)->process($item, $context); | ||
} | ||
|
||
return $items; | ||
} | ||
|
||
private function getProcessor(array $item): FeedProcessorInterface | ||
{ | ||
/** @var FeedProcessorInterface $processor */ | ||
foreach ($this->processors as $processor) { | ||
if ($processor->supports($item)) { | ||
return $processor; | ||
} | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/JeMengage/Timeline/FeedProcessor/AbstractFeedProcessor.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace App\JeMengage\Timeline\FeedProcessor; | ||
|
||
abstract class AbstractFeedProcessor implements FeedProcessorInterface | ||
{ | ||
public static function getDefaultPriority(): int | ||
{ | ||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace App\JeMengage\Timeline\FeedProcessor; | ||
|
||
use App\Adherent\Tag\TagEnum; | ||
use App\Entity\Adherent; | ||
use App\Event\EventCleaner; | ||
use App\Event\EventVisibilityEnum; | ||
use App\JeMengage\Timeline\TimelineFeedTypeEnum; | ||
use Symfony\Component\Security\Core\Security; | ||
|
||
class EventProcessor extends AbstractFeedProcessor | ||
{ | ||
private const CONTEXT_CLEANER_ENABLED_KEY = 'event:cleaner_enabled'; | ||
|
||
public function __construct( | ||
private readonly EventCleaner $eventCleaner, | ||
private readonly Security $security, | ||
) { | ||
} | ||
|
||
public function process(array $item, array &$context): array | ||
{ | ||
return $this->cleanEventDataIfNeed($item, $context); | ||
} | ||
|
||
public function supports(array $item): bool | ||
{ | ||
return ($item['type'] ?? null) === TimelineFeedTypeEnum::EVENT; | ||
} | ||
|
||
private function cleanEventDataIfNeed(array $item, array &$context): array | ||
{ | ||
$needClean = $context[self::CONTEXT_CLEANER_ENABLED_KEY] ?? null; | ||
|
||
if (null === $needClean) { | ||
$user = $this->security->getUser(); | ||
$needClean = $context[self::CONTEXT_CLEANER_ENABLED_KEY] = | ||
EventVisibilityEnum::isForAdherent($visibility = $item['visibility'] ?? EventVisibilityEnum::ADHERENT_DUES->value) | ||
&& ( | ||
!$user instanceof Adherent | ||
|| (EventVisibilityEnum::ADHERENT->value === $visibility && !$user->hasTag(TagEnum::ADHERENT)) | ||
|| (EventVisibilityEnum::ADHERENT_DUES->value === $visibility && !$user->hasTag(TagEnum::getAdherentYearTag())) | ||
); | ||
} | ||
|
||
if ($needClean) { | ||
$item = $this->eventCleaner->cleanEventData($item); | ||
$item['object_state'] = 'partial'; | ||
} else { | ||
$item['object_state'] = 'full'; | ||
} | ||
|
||
return $item; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/JeMengage/Timeline/FeedProcessor/FeedProcessorInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace App\JeMengage\Timeline\FeedProcessor; | ||
|
||
interface FeedProcessorInterface | ||
{ | ||
public function process(array $item, array &$context): array; | ||
|
||
public function supports(array $item): bool; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace App\JeMengage\Timeline\FeedProcessor; | ||
|
||
class NullProcessor extends AbstractFeedProcessor | ||
{ | ||
public static function getDefaultPriority(): int | ||
{ | ||
return -100; | ||
} | ||
|
||
public function process(array $item, array &$context): array | ||
{ | ||
return $item; | ||
} | ||
|
||
public function supports(array $item): bool | ||
{ | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters