FIX: Do not raise error if most liked user is deleted

This commit is contained in:
Vinoth Kannan 2018-12-25 01:04:55 +05:30
parent 2988d281fc
commit a16bb32865
2 changed files with 23 additions and 3 deletions

View File

@ -192,10 +192,12 @@ protected
lookup = AvatarLookup.new(user_ids)
user_ids.map do |user_id|
lookup_hash = lookup[user_id]
UserWithCount.new(
lookup[user_id].attributes.merge(count: user_hash[user_id])
)
end.sort_by { |u| -u[:count] }
lookup_hash.attributes.merge(count: user_hash[user_id])
) if lookup_hash.present?
end.compact.sort_by { |u| -u[:count] }
end
end

View File

@ -38,4 +38,22 @@ describe UserSummary do
expect(summary.top_categories.length).to eq(0)
end
it "is robust enough to handle bad data" do
UserActionCreator.enable
liked_post = create_post
user = Fabricate(:user)
PostAction.act(user, liked_post, PostActionType.types[:like])
users = UserSummary.new(user, Guardian.new).most_liked_users
expect(users.map(&:id)).to eq([liked_post.user_id])
# really we should not be corrupting stuff like this
# but in production dbs this can happens sometimes I guess
liked_post.user.delete
users = UserSummary.new(user, Guardian.new).most_liked_users
expect(users).to eq([])
end
end