2014-03-05 07:52:20 -05:00
|
|
|
class UserBadgesController < ApplicationController
|
2017-11-21 12:22:24 -05:00
|
|
|
before_action :ensure_badges_enabled
|
|
|
|
|
2014-03-05 07:52:20 -05:00
|
|
|
def index
|
2016-01-18 01:59:07 -05:00
|
|
|
params.permit [:granted_before, :offset, :username]
|
2014-08-25 13:38:20 -04:00
|
|
|
|
|
|
|
badge = fetch_badge_from_params
|
2015-09-24 20:20:47 -04:00
|
|
|
user_badges = badge.user_badges.order('granted_at DESC, id DESC').limit(96)
|
2014-08-25 13:38:20 -04:00
|
|
|
user_badges = user_badges.includes(:user, :granted_by, badge: :badge_type, post: :topic)
|
2014-04-22 05:40:47 -04:00
|
|
|
|
2016-01-18 01:59:07 -05:00
|
|
|
grant_count = nil
|
|
|
|
|
|
|
|
if params[:username]
|
|
|
|
user_id = User.where(username_lower: params[:username].downcase).pluck(:id).first
|
|
|
|
user_badges = user_badges.where(user_id: user_id) if user_id
|
2016-03-04 18:57:32 -05:00
|
|
|
grant_count = badge.user_badges.where(user_id: user_id).count
|
2016-01-18 01:59:07 -05:00
|
|
|
end
|
|
|
|
|
2014-07-18 01:46:36 -04:00
|
|
|
if offset = params[:offset]
|
|
|
|
user_badges = user_badges.offset(offset.to_i)
|
2014-04-16 10:56:11 -04:00
|
|
|
end
|
2014-04-22 05:40:47 -04:00
|
|
|
|
2016-01-18 01:59:07 -05:00
|
|
|
user_badges = UserBadges.new(user_badges: user_badges,
|
|
|
|
username: params[:username],
|
|
|
|
grant_count: grant_count)
|
|
|
|
|
|
|
|
render_serialized(user_badges, UserBadgesSerializer, root: :user_badge_info, include_long_description: true)
|
2014-08-25 13:38:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def username
|
|
|
|
params.permit [:grouped]
|
|
|
|
|
2017-12-07 01:23:27 -05:00
|
|
|
user = fetch_user_from_params(include_inactive: current_user.try(:staff?) || (current_user && SiteSetting.show_inactive_accounts))
|
2015-09-24 20:20:47 -04:00
|
|
|
user_badges = user.user_badges
|
2014-04-22 05:40:47 -04:00
|
|
|
|
2014-06-09 21:23:18 -04:00
|
|
|
if params[:grouped]
|
2014-07-14 03:40:01 -04:00
|
|
|
user_badges = user_badges.group(:badge_id)
|
2017-07-27 21:20:09 -04:00
|
|
|
.select(UserBadge.attribute_names.map { |x| "MAX(#{x}) AS #{x}" }, 'COUNT(*) AS "count"')
|
2014-05-21 03:22:42 -04:00
|
|
|
end
|
|
|
|
|
2015-09-24 20:20:47 -04:00
|
|
|
user_badges = user_badges.includes(badge: [:badge_grouping, :badge_type])
|
2017-07-27 21:20:09 -04:00
|
|
|
.includes(post: :topic)
|
|
|
|
.includes(:granted_by)
|
2015-09-24 20:20:47 -04:00
|
|
|
|
2016-01-18 01:59:07 -05:00
|
|
|
render_serialized(user_badges, DetailedUserBadgeSerializer, root: :user_badges)
|
2014-03-05 07:52:20 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
params.require(:username)
|
|
|
|
user = fetch_user_from_params
|
|
|
|
|
|
|
|
unless can_assign_badge_to_user?(user)
|
|
|
|
render json: failed_json, status: 403
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
badge = fetch_badge_from_params
|
2015-02-24 20:52:43 -05:00
|
|
|
post_id = nil
|
2014-03-05 07:52:20 -05:00
|
|
|
|
2015-02-24 20:52:43 -05:00
|
|
|
if params[:reason].present?
|
2018-03-28 04:20:08 -04:00
|
|
|
path = begin
|
|
|
|
URI.parse(params[:reason]).path
|
|
|
|
rescue URI::InvalidURIError
|
|
|
|
end
|
|
|
|
|
2015-02-24 20:52:43 -05:00
|
|
|
route = Rails.application.routes.recognize_path(path) if path
|
|
|
|
if route
|
|
|
|
topic_id = route[:topic_id].to_i
|
|
|
|
post_number = route[:post_number] || 1
|
|
|
|
|
|
|
|
post_id = Post.find_by(topic_id: topic_id, post_number: post_number).try(:id) if topic_id > 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
user_badge = BadgeGranter.grant(badge, user, granted_by: current_user, post_id: post_id)
|
|
|
|
|
|
|
|
render_serialized(user_badge, DetailedUserBadgeSerializer, root: "user_badge")
|
2014-03-05 07:52:20 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
params.require(:id)
|
|
|
|
user_badge = UserBadge.find(params[:id])
|
|
|
|
|
|
|
|
unless can_assign_badge_to_user?(user_badge.user)
|
|
|
|
render json: failed_json, status: 403
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2014-03-19 15:30:12 -04:00
|
|
|
BadgeGranter.revoke(user_badge, revoked_by: current_user)
|
2014-03-05 07:52:20 -05:00
|
|
|
render json: success_json
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
# Get the badge from either the badge name or id specified in the params.
|
|
|
|
def fetch_badge_from_params
|
|
|
|
badge = nil
|
|
|
|
|
|
|
|
params.permit(:badge_name)
|
|
|
|
if params[:badge_name].nil?
|
|
|
|
params.require(:badge_id)
|
|
|
|
badge = Badge.find_by(id: params[:badge_id], enabled: true)
|
|
|
|
else
|
|
|
|
badge = Badge.find_by(name: params[:badge_name], enabled: true)
|
2014-03-05 07:52:20 -05:00
|
|
|
end
|
2018-06-07 01:28:18 -04:00
|
|
|
raise Discourse::NotFound if badge.blank?
|
2014-03-05 07:52:20 -05:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
badge
|
|
|
|
end
|
2017-11-21 12:22:24 -05:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
def can_assign_badge_to_user?(user)
|
|
|
|
master_api_call = current_user.nil? && is_api?
|
|
|
|
master_api_call || guardian.can_grant_badges?(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_badges_enabled
|
|
|
|
raise Discourse::NotFound unless SiteSetting.enable_badges?
|
|
|
|
end
|
2014-03-05 07:52:20 -05:00
|
|
|
end
|