Skip to content

Commit

Permalink
Change source attribute of Suggestion entity in `/api/v2/suggesti…
Browse files Browse the repository at this point in the history
…ons` back to a string (mastodon#29108)
  • Loading branch information
ClearlyClaire authored and skerit committed Feb 27, 2024
1 parent ca21d38 commit 08f5f0f
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Source.propTypes = {
id: PropTypes.oneOf(['friends_of_friends', 'similar_to_recently_followed', 'featured', 'most_followed', 'most_interactions']),
};

const Card = ({ id, source }) => {
const Card = ({ id, sources }) => {
const intl = useIntl();
const account = useSelector(state => state.getIn(['accounts', id]));
const relationship = useSelector(state => state.getIn(['relationships', id]));
Expand Down Expand Up @@ -89,7 +89,7 @@ const Card = ({ id, source }) => {

<div className='inline-follow-suggestions__body__scrollable__card__text-stack'>
<Link to={`/@${account.get('acct')}`}><DisplayName account={account} /></Link>
{firstVerifiedField ? <VerifiedBadge link={firstVerifiedField.get('value')} /> : <Source id={source.get(0)} />}
{firstVerifiedField ? <VerifiedBadge link={firstVerifiedField.get('value')} /> : <Source id={sources.get(0)} />}
</div>

<Button text={intl.formatMessage(following ? messages.unfollow : messages.follow)} onClick={handleFollow} />
Expand All @@ -99,7 +99,7 @@ const Card = ({ id, source }) => {

Card.propTypes = {
id: PropTypes.string.isRequired,
source: ImmutablePropTypes.list,
sources: ImmutablePropTypes.list,
};

const DISMISSIBLE_ID = 'home/follow-suggestions';
Expand Down Expand Up @@ -175,7 +175,7 @@ export const InlineFollowSuggestions = ({ hidden }) => {
<Card
key={suggestion.get('account')}
id={suggestion.get('account')}
source={suggestion.get('source')}
sources={suggestion.get('sources')}
/>
))}
</div>
Expand Down
4 changes: 2 additions & 2 deletions app/models/account_suggestions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ def get(limit, offset = 0)
account_ids = account_ids_with_sources[offset, limit]
accounts_map = Account.where(id: account_ids.map(&:first)).includes(:account_stat, :user).index_by(&:id)

account_ids.filter_map do |(account_id, source)|
account_ids.filter_map do |(account_id, sources)|
next unless accounts_map.key?(account_id)

AccountSuggestions::Suggestion.new(
account: accounts_map[account_id],
source: source
sources: sources
)
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/account_suggestions/suggestion.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

class AccountSuggestions::Suggestion < ActiveModelSerializers::Model
attributes :account, :source
attributes :account, :sources

delegate :id, to: :account, prefix: true
end
15 changes: 14 additions & 1 deletion app/serializers/rest/suggestion_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
# frozen_string_literal: true

class REST::SuggestionSerializer < ActiveModel::Serializer
attributes :source
attributes :source, :sources

has_one :account, serializer: REST::AccountSerializer

LEGACY_SOURCE_TYPE_MAP = {
featured: 'staff',
most_followed: 'global',
most_interactions: 'global',
# NOTE: Those are not completely accurate, but those are personalized interactions
similar_to_recently_followed: 'past_interactions',
friends_of_friends: 'past_interactions',
}.freeze

def source
LEGACY_SOURCE_TYPE_MAP[object.sources.first]
end
end
20 changes: 19 additions & 1 deletion spec/requests/api/v2/suggestions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,28 @@
let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }

describe 'GET /api/v2/suggestions' do
it 'returns http success' do
let(:bob) { Fabricate(:account) }
let(:jeff) { Fabricate(:account) }
let(:params) { {} }

before do
Setting.bootstrap_timeline_accounts = [bob, jeff].map(&:acct).join(',')
end

it 'returns the expected suggestions' do
get '/api/v2/suggestions', headers: headers

expect(response).to have_http_status(200)

expect(body_as_json).to match_array(
[bob, jeff].map do |account|
hash_including({
source: 'staff',
sources: ['featured'],
account: hash_including({ id: account.id.to_s }),
})
end
)
end
end
end
2 changes: 1 addition & 1 deletion spec/serializers/rest/suggestion_serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
let(:record) do
AccountSuggestions::Suggestion.new(
account: account,
source: 'SuggestionSource'
sources: ['SuggestionSource']
)
end
let(:account) { Fabricate(:account) }
Expand Down

0 comments on commit 08f5f0f

Please sign in to comment.