2013-02-06 03:16:51 +08:00
|
|
|
# A class we can use to serialize the site data
|
|
|
|
require_dependency 'score_calculator'
|
|
|
|
require_dependency 'trust_level'
|
|
|
|
|
|
|
|
class Site
|
|
|
|
include ActiveModel::Serialization
|
|
|
|
|
2013-05-13 16:04:03 +08:00
|
|
|
def initialize(guardian)
|
|
|
|
@guardian = guardian
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def site_setting
|
|
|
|
SiteSetting
|
|
|
|
end
|
|
|
|
|
|
|
|
def post_action_types
|
|
|
|
PostActionType.ordered
|
|
|
|
end
|
|
|
|
|
2014-02-06 06:54:16 +08:00
|
|
|
def topic_flag_types
|
|
|
|
post_action_types.where(name_key: ['inappropriate', 'spam', 'notify_moderators'])
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def notification_types
|
2013-03-01 20:07:44 +08:00
|
|
|
Notification.types
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def trust_levels
|
|
|
|
TrustLevel.all
|
|
|
|
end
|
2013-02-07 23:45:24 +08:00
|
|
|
|
2014-09-21 04:05:21 +08:00
|
|
|
def groups
|
2015-09-03 05:46:04 +08:00
|
|
|
@groups ||= Group.order(:name).map { |g| { id: g.id, name: g.name } }
|
2013-07-16 13:44:07 +08:00
|
|
|
end
|
|
|
|
|
2014-09-27 02:48:34 +08:00
|
|
|
def user_fields
|
|
|
|
UserField.all
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def categories
|
2013-07-16 13:44:07 +08:00
|
|
|
@categories ||= begin
|
|
|
|
categories = Category
|
|
|
|
.secured(@guardian)
|
2015-09-03 05:46:04 +08:00
|
|
|
.includes(:topic_only_relative_url, :subcategories)
|
2013-11-07 05:56:49 +08:00
|
|
|
.order(:position)
|
2015-02-20 14:30:31 +08:00
|
|
|
|
|
|
|
unless SiteSetting.allow_uncategorized_topics
|
|
|
|
categories = categories.where('categories.id <> ?', SiteSetting.uncategorized_category_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
categories = categories.to_a
|
2013-07-16 13:44:07 +08:00
|
|
|
|
|
|
|
allowed_topic_create = Set.new(Category.topic_create_allowed(@guardian).pluck(:id))
|
|
|
|
|
2014-02-25 03:52:21 +08:00
|
|
|
by_id = {}
|
2014-04-17 17:17:39 +08:00
|
|
|
|
2014-06-18 09:21:53 +08:00
|
|
|
category_user = {}
|
|
|
|
unless @guardian.anonymous?
|
|
|
|
category_user = Hash[*CategoryUser.where(user: @guardian.user).pluck(:category_id, :notification_level).flatten]
|
|
|
|
end
|
|
|
|
|
|
|
|
categories.each do |category|
|
|
|
|
category.notification_level = category_user[category.id]
|
2013-07-16 13:44:07 +08:00
|
|
|
category.permission = CategoryGroup.permission_types[:full] if allowed_topic_create.include?(category.id)
|
2015-09-03 05:46:04 +08:00
|
|
|
category.has_children = category.subcategories.present?
|
2014-02-25 03:52:21 +08:00
|
|
|
by_id[category.id] = category
|
2013-07-16 13:44:07 +08:00
|
|
|
end
|
2014-02-25 03:52:21 +08:00
|
|
|
|
2015-09-03 05:46:04 +08:00
|
|
|
categories.reject! { |c| c.parent_category_id && !by_id[c.parent_category_id] }
|
2013-07-16 13:44:07 +08:00
|
|
|
categories
|
|
|
|
end
|
2013-02-07 23:45:24 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2015-09-04 01:18:46 +08:00
|
|
|
def suppressed_from_homepage_category_ids
|
|
|
|
categories.select { |c| c.suppress_from_homepage == true }.map(&:id)
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def archetypes
|
2013-03-01 02:54:12 +08:00
|
|
|
Archetype.list.reject { |t| t.id == Archetype.private_message }
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2014-02-25 03:24:18 +08:00
|
|
|
def self.json_for(guardian)
|
2013-10-15 06:50:30 +08:00
|
|
|
|
|
|
|
if guardian.anonymous? && SiteSetting.login_required
|
2014-01-20 21:41:11 +08:00
|
|
|
return {
|
|
|
|
periods: TopTopic.periods.map(&:to_s),
|
|
|
|
filters: Discourse.filters.map(&:to_s),
|
2015-08-18 03:44:08 +08:00
|
|
|
user_fields: UserField.all.map do |userfield|
|
|
|
|
UserFieldSerializer.new(userfield, root: false, scope: guardian)
|
|
|
|
end
|
2014-01-20 21:41:11 +08:00
|
|
|
}.to_json
|
2013-10-15 06:50:30 +08:00
|
|
|
end
|
|
|
|
|
2013-05-13 16:04:03 +08:00
|
|
|
site = Site.new(guardian)
|
2014-08-13 01:30:28 +08:00
|
|
|
MultiJson.dump(SiteSerializer.new(site, root: false, scope: guardian))
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|