Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change source attribute of Suggestion entity in /api/v2/suggestions back to a string #29108

Merged
merged 2 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading