Skip to content

Commit c591aac

Browse files
Gargronjesseplusplus
authored andcommitted
Fix localization of cold-start follow recommendations (mastodon#17479)
1 parent 7f614a2 commit c591aac

3 files changed

Lines changed: 19 additions & 14 deletions

File tree

app/models/account_suggestions/global_source.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ def key
66
end
77

88
def get(account, skip_account_ids: [], limit: 40)
9-
account_ids = account_ids_for_locale(account.user_locale) - [account.id] - skip_account_ids
9+
account_ids = account_ids_for_locale(I18n.locale.to_str.split(/[_-]/).first) - [account.id] - skip_account_ids
1010

1111
as_ordered_suggestions(
1212
scope(account).where(id: account_ids),

app/views/admin/follow_recommendations/show.html.haml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
.filter-subset.filter-subset--with-select
1414
%strong= t('admin.follow_recommendations.language')
1515
.input.select.optional
16-
= select_tag :language, options_for_select(I18n.available_locales.map { |key| [human_locale(key), key]}, @language)
16+
= 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)
1717

1818
.filter-subset
1919
%strong= t('admin.follow_recommendations.status')

app/workers/scheduler/follow_recommendations_scheduler.rb

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,33 @@ def perform
1616
AccountSummary.refresh
1717
FollowRecommendation.refresh
1818

19-
fallback_recommendations = FollowRecommendation.order(rank: :desc).limit(SET_SIZE).index_by(&:account_id)
19+
fallback_recommendations = FollowRecommendation.order(rank: :desc).limit(SET_SIZE)
2020

21-
I18n.available_locales.each do |locale|
21+
I18n.available_locales.map { |locale| locale.to_s.split(/[_-]/).first }.uniq.each do |locale|
2222
recommendations = begin
2323
if AccountSummary.safe.filtered.localized(locale).exists? # We can skip the work if no accounts with that language exist
24-
FollowRecommendation.localized(locale).order(rank: :desc).limit(SET_SIZE).index_by(&:account_id)
24+
FollowRecommendation.localized(locale).order(rank: :desc).limit(SET_SIZE).map { |recommendation| [recommendation.account_id, recommendation.rank] }
2525
else
26-
{}
26+
[]
2727
end
2828
end
2929

3030
# Use language-agnostic results if there are not enough language-specific ones
31-
missing = SET_SIZE - recommendations.keys.size
31+
missing = SET_SIZE - recommendations.size
32+
33+
if missing.positive? && fallback_recommendations.size.positive?
34+
max_fallback_rank = fallback_recommendations.first.rank || 0
35+
36+
# Language-specific results should be above language-agnostic ones,
37+
# otherwise language-agnostic ones will always overshadow them
38+
recommendations.map! { |(account_id, rank)| [account_id, rank + max_fallback_rank] }
3239

33-
if missing.positive?
3440
added = 0
3541

36-
# Avoid duplicate results
37-
fallback_recommendations.each_value do |recommendation|
38-
next if recommendations.key?(recommendation.account_id)
42+
fallback_recommendations.each do |recommendation|
43+
next if recommendations.any? { |(account_id, _)| account_id == recommendation.account_id }
3944

40-
recommendations[recommendation.account_id] = recommendation
45+
recommendations << [recommendation.account_id, recommendation.rank]
4146
added += 1
4247

4348
break if added >= missing
@@ -47,8 +52,8 @@ def perform
4752
redis.pipelined do
4853
redis.del(key(locale))
4954

50-
recommendations.each_value do |recommendation|
51-
redis.zadd(key(locale), recommendation.rank, recommendation.account_id)
55+
recommendations.each do |(account_id, rank)|
56+
redis.zadd(key(locale), rank, account_id)
5257
end
5358
end
5459
end

0 commit comments

Comments
 (0)