2014-04-16 21:12:06 +08:00
|
|
|
class BadgesController < ApplicationController
|
2014-04-26 02:58:41 +08:00
|
|
|
skip_before_filter :check_xhr, only: [:index, :show]
|
|
|
|
|
2014-04-16 21:12:06 +08:00
|
|
|
def index
|
2016-03-29 14:21:32 +08:00
|
|
|
raise Discourse::NotFound unless SiteSetting.enable_badges
|
|
|
|
|
2014-07-03 18:34:26 +08:00
|
|
|
badges = Badge.all
|
2014-07-18 13:46:36 +08:00
|
|
|
|
2016-09-27 08:01:40 +08:00
|
|
|
if search = params[:search]
|
|
|
|
search = search.to_s
|
|
|
|
badges = badges.where("name ILIKE ?", "%#{search}%")
|
|
|
|
end
|
|
|
|
|
2014-07-18 13:46:36 +08:00
|
|
|
if (params[:only_listable] == "true") || !request.xhr?
|
|
|
|
# NOTE: this is sorted client side if needed
|
|
|
|
badges = badges.includes(:badge_grouping)
|
2017-07-28 09:20:09 +08:00
|
|
|
.where(enabled: true, listable: true)
|
2014-07-18 13:46:36 +08:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
badges = badges.to_a
|
|
|
|
|
2014-07-16 15:53:54 +08:00
|
|
|
user_badges = nil
|
|
|
|
if current_user
|
|
|
|
user_badges = Set.new(current_user.user_badges.select('distinct badge_id').pluck(:badge_id))
|
|
|
|
end
|
2015-02-27 14:19:18 +08:00
|
|
|
serialized = MultiJson.dump(serialize_data(badges, BadgeIndexSerializer, root: "badges", user_badges: user_badges, include_long_description: true))
|
2014-04-26 02:58:41 +08:00
|
|
|
respond_to do |format|
|
|
|
|
format.html do
|
|
|
|
store_preloaded "badges", serialized
|
|
|
|
render "default/empty"
|
|
|
|
end
|
|
|
|
format.json { render json: serialized }
|
|
|
|
end
|
2014-04-16 21:12:06 +08:00
|
|
|
end
|
2014-04-16 22:56:11 +08:00
|
|
|
|
|
|
|
def show
|
2016-03-29 14:21:32 +08:00
|
|
|
raise Discourse::NotFound unless SiteSetting.enable_badges
|
|
|
|
|
2014-04-16 22:56:11 +08:00
|
|
|
params.require(:id)
|
2017-04-06 12:02:53 +08:00
|
|
|
@badge = Badge.enabled.find(params[:id])
|
2014-06-14 15:55:06 +08:00
|
|
|
|
|
|
|
if current_user
|
2017-04-06 12:02:53 +08:00
|
|
|
user_badge = UserBadge.find_by(user_id: current_user.id, badge_id: @badge.id)
|
2014-06-14 15:55:06 +08:00
|
|
|
if user_badge && user_badge.notification
|
|
|
|
user_badge.notification.update_attributes read: true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-06 12:02:53 +08:00
|
|
|
serialized = MultiJson.dump(serialize_data(@badge, BadgeSerializer, root: "badge", include_long_description: true))
|
2014-04-26 02:58:41 +08:00
|
|
|
respond_to do |format|
|
|
|
|
format.html do
|
|
|
|
store_preloaded "badge", serialized
|
2017-04-06 12:02:53 +08:00
|
|
|
render :show
|
2014-04-26 02:58:41 +08:00
|
|
|
end
|
|
|
|
format.json { render json: serialized }
|
|
|
|
end
|
2014-04-16 22:56:11 +08:00
|
|
|
end
|
2014-04-16 21:12:06 +08:00
|
|
|
end
|