[CORE][Actor][Posting] Fixup Actor::getPreferredLanguageChoices following changes in how the data is cached

This commit is contained in:
Hugo Sales 2021-11-11 12:29:55 +00:00
parent 316723075b
commit a1c78696f7
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
1 changed files with 15 additions and 9 deletions

View File

@ -356,20 +356,26 @@ class Actor extends Entity
* Get the most appropraite language for $this to use when * Get the most appropraite language for $this to use when
* referring to $context (a reply or a group, for instance) * 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 public function getPreferredLanguageChoices(?self $context = null): array
{ {
$id = $context?->getId() ?? $this->getId(); $id = $context?->getId() ?? $this->getId();
return Cache::getHashMap( $key = 'actor-' . $this->getId() . '-langs' . (!\is_null($context) ? '-' . $context->getId() : '');
'actor-' . $this->getId() . '-langs' . (!\is_null($context) ? '-' . $context->getId() : ''), $langs = Cache::getHashMap(
fn () => array_merge( // TODO replace with F\transform $key,
...F\map(DB::dql( 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', '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], ['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 public static function schemaDef(): array