1
0
Fork 0
forked from fedi/mastodon

Fix localization of cold-start follow recommendations (#17479)

This commit is contained in:
Eugen Rochko 2022-02-08 01:53:49 +01:00 committed by GitHub
parent 52c1b86964
commit 35850f8195
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 14 deletions

View file

@ -6,7 +6,7 @@ class AccountSuggestions::GlobalSource < AccountSuggestions::Source
end end
def get(account, skip_account_ids: [], limit: 40) def get(account, skip_account_ids: [], limit: 40)
account_ids = account_ids_for_locale(account.user_locale) - [account.id] - skip_account_ids account_ids = account_ids_for_locale(I18n.locale.to_str.split(/[_-]/).first) - [account.id] - skip_account_ids
as_ordered_suggestions( as_ordered_suggestions(
scope(account).where(id: account_ids), scope(account).where(id: account_ids),

View file

@ -13,7 +13,7 @@
.filter-subset.filter-subset--with-select .filter-subset.filter-subset--with-select
%strong= t('admin.follow_recommendations.language') %strong= t('admin.follow_recommendations.language')
.input.select.optional .input.select.optional
= select_tag :language, options_for_select(I18n.available_locales.map { |key| [human_locale(key), key]}, @language) = select_tag :language, options_for_select(I18n.available_locales.map { |key| key.to_s.split(/[_-]/).first.to_sym }.uniq.map { |key| [human_locale(key), key]}, @language)
.filter-subset .filter-subset
%strong= t('admin.follow_recommendations.status') %strong= t('admin.follow_recommendations.status')

View file

@ -16,28 +16,33 @@ class Scheduler::FollowRecommendationsScheduler
AccountSummary.refresh AccountSummary.refresh
FollowRecommendation.refresh FollowRecommendation.refresh
fallback_recommendations = FollowRecommendation.order(rank: :desc).limit(SET_SIZE).index_by(&:account_id) fallback_recommendations = FollowRecommendation.order(rank: :desc).limit(SET_SIZE)
I18n.available_locales.each do |locale| I18n.available_locales.map { |locale| locale.to_s.split(/[_-]/).first }.uniq.each do |locale|
recommendations = begin recommendations = begin
if AccountSummary.safe.filtered.localized(locale).exists? # We can skip the work if no accounts with that language exist if AccountSummary.safe.filtered.localized(locale).exists? # We can skip the work if no accounts with that language exist
FollowRecommendation.localized(locale).order(rank: :desc).limit(SET_SIZE).index_by(&:account_id) FollowRecommendation.localized(locale).order(rank: :desc).limit(SET_SIZE).map { |recommendation| [recommendation.account_id, recommendation.rank] }
else else
{} []
end end
end end
# Use language-agnostic results if there are not enough language-specific ones # Use language-agnostic results if there are not enough language-specific ones
missing = SET_SIZE - recommendations.keys.size missing = SET_SIZE - recommendations.size
if missing.positive? && fallback_recommendations.size.positive?
max_fallback_rank = fallback_recommendations.first.rank || 0
# Language-specific results should be above language-agnostic ones,
# otherwise language-agnostic ones will always overshadow them
recommendations.map! { |(account_id, rank)| [account_id, rank + max_fallback_rank] }
if missing.positive?
added = 0 added = 0
# Avoid duplicate results fallback_recommendations.each do |recommendation|
fallback_recommendations.each_value do |recommendation| next if recommendations.any? { |(account_id, _)| account_id == recommendation.account_id }
next if recommendations.key?(recommendation.account_id)
recommendations[recommendation.account_id] = recommendation recommendations << [recommendation.account_id, recommendation.rank]
added += 1 added += 1
break if added >= missing break if added >= missing
@ -47,8 +52,8 @@ class Scheduler::FollowRecommendationsScheduler
redis.pipelined do redis.pipelined do
redis.del(key(locale)) redis.del(key(locale))
recommendations.each_value do |recommendation| recommendations.each do |(account_id, rank)|
redis.zadd(key(locale), recommendation.rank, recommendation.account_id) redis.zadd(key(locale), rank, account_id)
end end
end end
end end