From a1c78696f78222c820fe3d4a49d1a9f70797d373 Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Thu, 11 Nov 2021 12:29:55 +0000 Subject: [PATCH] [CORE][Actor][Posting] Fixup Actor::getPreferredLanguageChoices following changes in how the data is cached --- src/Entity/Actor.php | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/Entity/Actor.php b/src/Entity/Actor.php index b1b87a8900..7ab4ca8bbb 100644 --- a/src/Entity/Actor.php +++ b/src/Entity/Actor.php @@ -356,20 +356,26 @@ class Actor extends Entity * Get the most appropraite language for $this to use when * referring to $context (a reply or a group, for instance) * - * @return string[] the Languages as string (save space in cache) + * @return Language[] */ public function getPreferredLanguageChoices(?self $context = null): array { - $id = $context?->getId() ?? $this->getId(); - return Cache::getHashMap( - 'actor-' . $this->getId() . '-langs' . (!\is_null($context) ? '-' . $context->getId() : ''), - fn () => array_merge( // TODO replace with F\transform - ...F\map(DB::dql( + $id = $context?->getId() ?? $this->getId(); + $key = 'actor-' . $this->getId() . '-langs' . (!\is_null($context) ? '-' . $context->getId() : ''); + $langs = Cache::getHashMap( + $key, + fn () => F\reindex( + DB::dql( 'select l from actor_language al join language l with al.language_id = l.id where al.actor_id = :id order by al.order ASC', ['id' => $id], - ), fn ($l) => $l->toChoiceFormat()), - ) ?: Cache::getHashMapKey('languages', Common::config('site', 'language'))->toChoiceFormat(), - ); + ), + fn (Language $l) => $l->getLocale(), + ), + ) ?: [ + Common::config('site', 'language') => (Cache::getHashMapKey('languages', Common::config('site', 'language')) + ?: DB::findOneBy('language', ['locale' => Common::config('site', 'language')])), + ]; + return array_merge(...F\map(array_values($langs), fn ($l) => $l->toChoiceFormat())); } public static function schemaDef(): array