2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-04-16 08:12:06 -05:00
|
|
|
class BadgesController < ApplicationController
|
2017-08-30 23:06:56 -05:00
|
|
|
skip_before_action :check_xhr, only: %i[index show]
|
2020-05-11 00:05:42 -05:00
|
|
|
after_action :add_noindex_header
|
2014-04-25 13:58:41 -05:00
|
|
|
|
2014-04-16 08:12:06 -05:00
|
|
|
def index
|
2016-03-29 01:21:32 -05:00
|
|
|
raise Discourse::NotFound unless SiteSetting.enable_badges
|
|
|
|
|
2014-07-03 05:34:26 -05:00
|
|
|
badges = Badge.all
|
2014-07-18 00:46:36 -05:00
|
|
|
|
2016-09-26 19:01:40 -05:00
|
|
|
if search = params[:search]
|
|
|
|
search = search.to_s
|
|
|
|
badges = badges.where("name ILIKE ?", "%#{search}%")
|
|
|
|
end
|
|
|
|
|
2014-07-18 00:46:36 -05:00
|
|
|
if (params[:only_listable] == "true") || !request.xhr?
|
|
|
|
# NOTE: this is sorted client side if needed
|
|
|
|
badges =
|
|
|
|
badges
|
|
|
|
.includes(:badge_grouping)
|
2021-03-17 00:55:23 -05:00
|
|
|
.includes(:badge_type, :image_upload)
|
2014-07-18 00:46:36 -05:00
|
|
|
.where(enabled: true, listable: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
badges = badges.to_a
|
|
|
|
|
2014-07-16 02:53:54 -05: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 00:19:18 -06:00
|
|
|
serialized =
|
|
|
|
MultiJson.dump(
|
|
|
|
serialize_data(
|
|
|
|
badges,
|
|
|
|
BadgeIndexSerializer,
|
|
|
|
root: "badges",
|
|
|
|
user_badges: user_badges,
|
|
|
|
include_long_description: true,
|
2023-01-09 06:20:10 -06:00
|
|
|
),
|
2015-02-27 00:19:18 -06:00
|
|
|
)
|
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
|
2014-04-16 08:12:06 -05:00
|
|
|
end
|
2014-04-16 09:56:11 -05:00
|
|
|
|
|
|
|
def show
|
2016-03-29 01:21:32 -05:00
|
|
|
raise Discourse::NotFound unless SiteSetting.enable_badges
|
|
|
|
|
2014-04-16 09:56:11 -05:00
|
|
|
params.require(:id)
|
2017-04-05 23:02:53 -05:00
|
|
|
@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}"
|
2014-06-14 02:55:06 -05:00
|
|
|
|
|
|
|
if current_user
|
2017-04-05 23:02:53 -05:00
|
|
|
user_badge = UserBadge.find_by(user_id: current_user.id, badge_id: @badge.id)
|
2019-04-29 02:32:25 -05:00
|
|
|
user_badge.notification.update read: true if user_badge && user_badge.notification
|
2017-10-30 00:21:05 -05:00
|
|
|
@badge.has_badge = true if user_badge
|
2014-06-14 02:55:06 -05:00
|
|
|
end
|
|
|
|
|
2017-04-05 23:02:53 -05:00
|
|
|
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 { @rss_description = @badge.long_description }
|
2014-04-25 13:58:41 -05:00
|
|
|
format.html { store_preloaded "badge", serialized }
|
|
|
|
format.json { render json: serialized }
|
|
|
|
end
|
2014-04-16 09:56:11 -05:00
|
|
|
end
|
2014-04-16 08:12:06 -05:00
|
|
|
end
|