discourse/app/controllers/badges_controller.rb

55 lines
1.6 KiB
Ruby
Raw Normal View History

class BadgesController < ApplicationController
2014-04-26 02:58:41 +08:00
skip_before_filter :check_xhr, only: [:index, :show]
def index
2016-04-01 05:33:25 +08:00
raise Discourse::NotFound unless SiteSetting.enable_badges
2014-07-03 18:34:26 +08:00
badges = Badge.all
if (params[:only_listable] == "true") || !request.xhr?
# NOTE: this is sorted client side if needed
badges = badges.includes(:badge_grouping)
.where(enabled: true, listable: true)
end
badges = badges.to_a
user_badges = nil
if current_user
user_badges = Set.new(current_user.user_badges.select('distinct badge_id').pluck(:badge_id))
end
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
end
2014-04-16 22:56:11 +08:00
def show
2016-04-01 05:33:25 +08:00
raise Discourse::NotFound unless SiteSetting.enable_badges
2014-04-16 22:56:11 +08:00
params.require(:id)
badge = Badge.enabled.find(params[:id])
if current_user
user_badge = UserBadge.find_by(user_id: current_user.id, badge_id: badge.id)
if user_badge && user_badge.notification
user_badge.notification.update_attributes read: true
end
end
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
render "default/empty"
end
format.json { render json: serialized }
end
2014-04-16 22:56:11 +08:00
end
end