discourse/app/controllers/badges_controller.rb

69 lines
2.1 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
class BadgesController < ApplicationController
skip_before_action :check_xhr, only: [:index, :show]
2014-04-25 13:58:41 -05:00
def index
raise Discourse::NotFound unless SiteSetting.enable_badges
2014-07-03 05:34:26 -05:00
badges = Badge.all
if search = params[:search]
search = search.to_s
badges = badges.where("name ILIKE ?", "%#{search}%")
end
if (params[:only_listable] == "true") || !request.xhr?
# NOTE: this is sorted client side if needed
badges = badges.includes(:badge_grouping)
2018-03-19 01:36:09 -05:00
.includes(:badge_type)
2017-07-27 20:20:09 -05:00
.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-25 13:58:41 -05: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 09:56:11 -05:00
def show
raise Discourse::NotFound unless SiteSetting.enable_badges
2014-04-16 09:56:11 -05:00
params.require(:id)
@badge = Badge.enabled.find(params[:id])
2017-08-08 15:21:42 -05:00
@rss_title = I18n.t('rss_description.badge', display_name: @badge.display_name, site_title: SiteSetting.title)
@rss_link = "#{Discourse.base_url}/badges/#{@badge.id}/#{@badge.slug}"
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 read: true
end
if user_badge
@badge.has_badge = true
end
end
serialized = MultiJson.dump(serialize_data(@badge, BadgeSerializer, root: "badge", include_long_description: true))
2014-04-25 13:58:41 -05:00
respond_to do |format|
2017-08-08 15:21:42 -05:00
format.rss do
@rss_description = @badge.long_description
end
2014-04-25 13:58:41 -05:00
format.html do
store_preloaded "badge", serialized
end
format.json { render json: serialized }
end
2014-04-16 09:56:11 -05:00
end
end