docbrown/app/controllers/api/v0/users_controller.rb
rhymes 2d6df70dea Return an empty result set instead of 404 in case users are not available. (#5238)
A common response in collection endpoint with REST APIs is to return an empty result set when the requested params end up in an empty result. The reason for this is that the resource itself exists (the collection always exists), it just is empty.

See https://github.com/thepracticaldev/dev.to/pull/5192
2019-12-24 13:08:10 -05:00

44 lines
1.3 KiB
Ruby

module Api
module V0
class UsersController < ApiController
before_action :authenticate!, only: %i[me]
before_action -> { doorkeeper_authorize! :public }, only: :me, if: -> { doorkeeper_token }
def index
if !user_signed_in? || less_than_one_day_old?(current_user)
usernames = %w[ben jess peter maestromac andy liana]
@users = User.where(username: usernames)
return
end
if params[:state] == "follow_suggestions"
@users = Suggester::Users::Recent.new(current_user).suggest
elsif params[:state] == "sidebar_suggestions"
given_tag = params[:tag]
@users = Suggester::Users::Sidebar.new(current_user, given_tag).suggest.sample(3)
else
@users = User.none
end
end
def show
@user = if params[:id] == "by_username"
User.find_by(username: params[:url])
else
User.find(params[:id])
end
end
def me; end
private
def less_than_one_day_old?(user)
range = 1.day.ago.beginning_of_day..Time.current
user_identity_age = user.github_created_at || user.twitter_created_at || 8.days.ago
# last one is a fallback in case both are nil
range.cover? user_identity_age
end
end
end
end