2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
class ListController < ApplicationController
|
2015-01-08 16:44:27 -05:00
|
|
|
include TopicListResponder
|
2019-08-15 13:41:06 -04:00
|
|
|
include TopicQueryParams
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
skip_before_action :check_xhr
|
2014-01-17 17:52:06 -05:00
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
before_action :set_category, only: [
|
2017-03-02 10:56:04 -05:00
|
|
|
:category_default,
|
2014-01-17 17:52:06 -05:00
|
|
|
# filtered topics lists
|
2015-09-02 14:25:18 -04:00
|
|
|
Discourse.filters.map { |f| :"category_#{f}" },
|
|
|
|
Discourse.filters.map { |f| :"category_none_#{f}" },
|
2014-02-03 10:08:00 -05:00
|
|
|
# top summaries
|
|
|
|
:category_top,
|
|
|
|
:category_none_top,
|
2014-01-17 17:52:06 -05:00
|
|
|
# top pages (ie. with a period)
|
2015-09-02 14:25:18 -04:00
|
|
|
TopTopic.periods.map { |p| :"category_top_#{p}" },
|
|
|
|
TopTopic.periods.map { |p| :"category_none_top_#{p}" },
|
2014-01-17 17:52:06 -05:00
|
|
|
# category feeds
|
|
|
|
:category_feed,
|
|
|
|
].flatten
|
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
before_action :ensure_logged_in, except: [
|
2014-01-13 19:02:14 -05:00
|
|
|
:topics_by,
|
|
|
|
# anonymous filters
|
2014-01-17 17:52:06 -05:00
|
|
|
Discourse.anonymous_filters,
|
2015-09-02 14:25:18 -04:00
|
|
|
Discourse.anonymous_filters.map { |f| "#{f}_feed" },
|
2014-04-04 13:05:40 -04:00
|
|
|
# anonymous categorized filters
|
2017-03-02 10:56:04 -05:00
|
|
|
:category_default,
|
2015-09-02 14:25:18 -04:00
|
|
|
Discourse.anonymous_filters.map { |f| :"category_#{f}" },
|
|
|
|
Discourse.anonymous_filters.map { |f| :"category_none_#{f}" },
|
2014-04-04 13:05:40 -04:00
|
|
|
# category feeds
|
|
|
|
:category_feed,
|
2016-03-31 09:10:50 -04:00
|
|
|
# user topics feed
|
|
|
|
:user_topics_feed,
|
2014-04-04 13:05:40 -04:00
|
|
|
# top summaries
|
2014-01-17 17:52:06 -05:00
|
|
|
:top,
|
2014-04-04 13:05:40 -04:00
|
|
|
:category_top,
|
|
|
|
:category_none_top,
|
|
|
|
# top pages (ie. with a period)
|
2015-09-02 14:25:18 -04:00
|
|
|
TopTopic.periods.map { |p| :"top_#{p}" },
|
2017-03-13 04:54:10 -04:00
|
|
|
TopTopic.periods.map { |p| :"top_#{p}_feed" },
|
2015-09-02 14:25:18 -04:00
|
|
|
TopTopic.periods.map { |p| :"category_top_#{p}" },
|
|
|
|
TopTopic.periods.map { |p| :"category_none_top_#{p}" },
|
2018-03-14 07:40:28 -04:00
|
|
|
:group_topics
|
2014-01-13 19:02:14 -05:00
|
|
|
].flatten
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
# Create our filters
|
2015-09-02 14:25:18 -04:00
|
|
|
Discourse.filters.each do |filter|
|
2014-01-17 17:52:06 -05:00
|
|
|
define_method(filter) do |options = nil|
|
2013-06-19 22:11:14 -04:00
|
|
|
list_opts = build_topic_list_options
|
2014-01-17 17:52:06 -05:00
|
|
|
list_opts.merge!(options) if options
|
2013-08-24 13:38:02 -04:00
|
|
|
user = list_target_user
|
2020-06-29 14:22:02 -04:00
|
|
|
if params[:category].blank? && filter == :latest && !SiteSetting.show_category_definitions_in_topic_lists
|
|
|
|
list_opts[:no_definitions] = true
|
|
|
|
end
|
2015-09-02 14:25:18 -04:00
|
|
|
|
2013-08-21 19:18:54 -04:00
|
|
|
list = TopicQuery.new(user, list_opts).public_send("list_#{filter}")
|
2016-01-20 11:55:58 -05:00
|
|
|
|
2018-03-28 15:36:12 -04:00
|
|
|
if guardian.can_create_shared_draft? && @category.present?
|
|
|
|
if @category.id == SiteSetting.shared_drafts_category.to_i
|
|
|
|
# On shared drafts, show the destination category
|
|
|
|
list.topics.each do |t|
|
|
|
|
t.includes_destination_category = true
|
|
|
|
end
|
|
|
|
else
|
|
|
|
# When viewing a non-shared draft category, find topics whose
|
|
|
|
# destination are this category
|
|
|
|
shared_drafts = TopicQuery.new(
|
|
|
|
user,
|
|
|
|
category: SiteSetting.shared_drafts_category,
|
|
|
|
destination_category_id: list_opts[:category]
|
|
|
|
).list_latest
|
2018-03-13 15:59:12 -04:00
|
|
|
|
2018-03-28 15:36:12 -04:00
|
|
|
if shared_drafts.present? && shared_drafts.topics.present?
|
|
|
|
list.shared_drafts = shared_drafts.topics
|
|
|
|
end
|
2018-03-13 15:59:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-02-13 06:34:22 -05:00
|
|
|
list.more_topics_url = construct_url_with(:next, list_opts)
|
|
|
|
list.prev_topics_url = construct_url_with(:prev, list_opts)
|
2013-12-23 18:50:36 -05:00
|
|
|
if Discourse.anonymous_filters.include?(filter)
|
2013-10-11 12:35:12 -04:00
|
|
|
@description = SiteSetting.site_description
|
|
|
|
@rss = filter
|
2014-10-30 14:26:35 -04:00
|
|
|
|
2015-03-30 11:40:44 -04:00
|
|
|
# Note the first is the default and we don't add a title
|
2015-06-08 12:07:35 -04:00
|
|
|
if (filter.to_s != current_homepage) && use_crawler_layout?
|
2015-07-29 09:39:20 -04:00
|
|
|
filter_title = I18n.t("js.filters.#{filter.to_s}.title", count: 0)
|
2017-04-06 14:52:06 -04:00
|
|
|
if list_opts[:category] && @category
|
|
|
|
@title = I18n.t('js.filters.with_category', filter: filter_title, category: @category.name)
|
2014-10-30 14:26:35 -04:00
|
|
|
else
|
|
|
|
@title = I18n.t('js.filters.with_topics', filter: filter_title)
|
|
|
|
end
|
2017-06-12 13:50:30 -04:00
|
|
|
@title << " - #{SiteSetting.title}"
|
2019-07-08 02:12:39 -04:00
|
|
|
elsif @category.blank? && (filter.to_s == current_homepage) && SiteSetting.short_site_description.present?
|
2018-12-12 05:46:14 -05:00
|
|
|
@title = "#{SiteSetting.title} - #{SiteSetting.short_site_description}"
|
2014-10-30 14:26:35 -04:00
|
|
|
end
|
2013-10-11 12:35:12 -04:00
|
|
|
end
|
2014-10-30 14:26:35 -04:00
|
|
|
|
2015-01-08 16:44:27 -05:00
|
|
|
respond_with_list(list)
|
2013-02-07 10:45:24 -05:00
|
|
|
end
|
2014-01-17 17:52:06 -05:00
|
|
|
|
2014-02-03 10:08:00 -05:00
|
|
|
define_method("category_#{filter}") do
|
2015-10-01 12:24:07 -04:00
|
|
|
canonical_url "#{Discourse.base_url_no_prefix}#{@category.url}"
|
2019-05-06 21:27:05 -04:00
|
|
|
self.public_send(filter, category: @category.id)
|
2014-01-17 17:52:06 -05:00
|
|
|
end
|
|
|
|
|
2014-02-03 10:08:00 -05:00
|
|
|
define_method("category_none_#{filter}") do
|
2019-05-06 21:27:05 -04:00
|
|
|
self.public_send(filter, category: @category.id, no_subcategories: true)
|
2014-01-17 17:52:06 -05:00
|
|
|
end
|
2013-07-05 16:49:06 -04:00
|
|
|
end
|
|
|
|
|
2017-03-02 10:56:04 -05:00
|
|
|
def category_default
|
2017-07-03 03:55:22 -04:00
|
|
|
canonical_url "#{Discourse.base_url_no_prefix}#{@category.url}"
|
2017-05-08 15:17:58 -04:00
|
|
|
view_method = @category.default_view
|
|
|
|
view_method = 'latest' unless %w(latest top).include?(view_method)
|
|
|
|
|
2019-10-28 13:56:47 -04:00
|
|
|
self.public_send(view_method, category: @category.id)
|
2017-03-02 10:56:04 -05:00
|
|
|
end
|
|
|
|
|
2017-03-01 16:36:06 -05:00
|
|
|
def topics_by
|
|
|
|
list_opts = build_topic_list_options
|
2017-12-07 01:23:27 -05:00
|
|
|
target_user = fetch_user_from_params({ include_inactive: current_user.try(:staff?) || (current_user && SiteSetting.show_inactive_accounts) }, [:user_stat, :user_option])
|
2020-06-03 09:33:02 -04:00
|
|
|
ensure_can_see_profile!(target_user)
|
|
|
|
|
2017-03-01 18:26:26 -05:00
|
|
|
list = generate_list_for("topics_by", target_user, list_opts)
|
2019-10-29 04:46:42 -04:00
|
|
|
list.more_topics_url = construct_url_with(:next, list_opts)
|
|
|
|
list.prev_topics_url = construct_url_with(:prev, list_opts)
|
2017-03-01 16:36:06 -05:00
|
|
|
respond_with_list(list)
|
|
|
|
end
|
|
|
|
|
2018-03-14 07:40:28 -04:00
|
|
|
def group_topics
|
|
|
|
group = Group.find_by(name: params[:group_name])
|
|
|
|
raise Discourse::NotFound unless group
|
|
|
|
guardian.ensure_can_see_group!(group)
|
2019-08-14 09:30:04 -04:00
|
|
|
guardian.ensure_can_see_group_members!(group)
|
2018-03-14 07:40:28 -04:00
|
|
|
|
|
|
|
list_opts = build_topic_list_options
|
|
|
|
list = generate_list_for("group_topics", group, list_opts)
|
2019-10-29 04:46:42 -04:00
|
|
|
list.more_topics_url = construct_url_with(:next, list_opts)
|
|
|
|
list.prev_topics_url = construct_url_with(:prev, list_opts)
|
2018-03-14 07:40:28 -04:00
|
|
|
respond_with_list(list)
|
|
|
|
end
|
|
|
|
|
2017-03-01 16:36:06 -05:00
|
|
|
def self.generate_message_route(action)
|
2013-10-19 09:21:17 -04:00
|
|
|
define_method("#{action}") do
|
2020-06-22 09:48:24 -04:00
|
|
|
if action == :private_messages_tag && !guardian.can_tag_pms?
|
|
|
|
raise Discourse::NotFound
|
|
|
|
end
|
|
|
|
|
2013-10-19 09:21:17 -04:00
|
|
|
list_opts = build_topic_list_options
|
2016-06-27 22:01:00 -04:00
|
|
|
target_user = fetch_user_from_params({ include_inactive: current_user.try(:staff?) }, [:user_stat, :user_option])
|
2017-03-01 16:36:06 -05:00
|
|
|
guardian.ensure_can_see_private_messages!(target_user.id)
|
2013-10-19 09:21:17 -04:00
|
|
|
list = generate_list_for(action.to_s, target_user, list_opts)
|
2017-03-01 16:36:06 -05:00
|
|
|
url_prefix = "topics"
|
2019-10-29 04:46:42 -04:00
|
|
|
list.more_topics_url = construct_url_with(:next, list_opts, url_prefix)
|
|
|
|
list.prev_topics_url = construct_url_with(:prev, list_opts, url_prefix)
|
2015-01-08 16:44:27 -05:00
|
|
|
respond_with_list(list)
|
2013-10-19 09:21:17 -04:00
|
|
|
end
|
2013-08-30 12:32:05 -04:00
|
|
|
end
|
|
|
|
|
2017-03-01 16:36:06 -05:00
|
|
|
%i{
|
|
|
|
private_messages
|
|
|
|
private_messages_sent
|
|
|
|
private_messages_unread
|
|
|
|
private_messages_archive
|
|
|
|
private_messages_group
|
|
|
|
private_messages_group_archive
|
2018-02-13 15:46:25 -05:00
|
|
|
private_messages_tag
|
2017-03-01 16:36:06 -05:00
|
|
|
}.each do |action|
|
|
|
|
generate_message_route(action)
|
|
|
|
end
|
|
|
|
|
2015-09-02 14:25:18 -04:00
|
|
|
def latest_feed
|
|
|
|
discourse_expires_in 1.minute
|
|
|
|
|
2020-08-03 03:03:58 -04:00
|
|
|
options = { order: 'created' }.merge(build_topic_list_options)
|
|
|
|
|
2015-09-02 14:25:18 -04:00
|
|
|
@title = "#{SiteSetting.title} - #{I18n.t("rss_description.latest")}"
|
|
|
|
@link = "#{Discourse.base_url}/latest"
|
|
|
|
@atom_link = "#{Discourse.base_url}/latest.rss"
|
|
|
|
@description = I18n.t("rss_description.latest")
|
2020-08-03 03:03:58 -04:00
|
|
|
@topic_list = TopicQuery.new(nil, options).list_latest
|
2015-09-02 14:25:18 -04:00
|
|
|
|
|
|
|
render 'list', formats: [:rss]
|
|
|
|
end
|
|
|
|
|
2016-02-29 12:58:51 -05:00
|
|
|
def top_feed
|
|
|
|
discourse_expires_in 1.minute
|
|
|
|
|
|
|
|
@title = "#{SiteSetting.title} - #{I18n.t("rss_description.top")}"
|
|
|
|
@link = "#{Discourse.base_url}/top"
|
|
|
|
@atom_link = "#{Discourse.base_url}/top.rss"
|
|
|
|
@description = I18n.t("rss_description.top")
|
2017-03-13 04:54:10 -04:00
|
|
|
@topic_list = TopicQuery.new(nil).list_top_for(SiteSetting.top_page_default_timeframe.to_sym)
|
2016-02-29 12:58:51 -05:00
|
|
|
|
|
|
|
render 'list', formats: [:rss]
|
|
|
|
end
|
|
|
|
|
2013-02-27 22:36:12 -05:00
|
|
|
def category_feed
|
|
|
|
guardian.ensure_can_see!(@category)
|
2013-10-16 01:39:18 -04:00
|
|
|
discourse_expires_in 1.minute
|
|
|
|
|
2017-06-12 13:50:30 -04:00
|
|
|
@title = "#{@category.name} - #{SiteSetting.title}"
|
2018-09-07 10:03:30 -04:00
|
|
|
@link = "#{Discourse.base_url_no_prefix}#{@category.url}"
|
|
|
|
@atom_link = "#{Discourse.base_url_no_prefix}#{@category.url}.rss"
|
2015-09-02 14:25:18 -04:00
|
|
|
@description = "#{I18n.t('topics_in_category', category: @category.name)} #{@category.description}"
|
2017-10-18 04:43:47 -04:00
|
|
|
@topic_list = TopicQuery.new(current_user).list_new_in_category(@category)
|
2014-01-17 17:52:06 -05:00
|
|
|
|
2016-03-31 09:10:50 -04:00
|
|
|
render 'list', formats: [:rss]
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_topics_feed
|
|
|
|
discourse_expires_in 1.minute
|
|
|
|
target_user = fetch_user_from_params
|
2020-06-03 09:33:02 -04:00
|
|
|
ensure_can_see_profile!(target_user)
|
2016-03-31 09:10:50 -04:00
|
|
|
|
|
|
|
@title = "#{SiteSetting.title} - #{I18n.t("rss_description.user_topics", username: target_user.username)}"
|
2017-03-28 14:27:54 -04:00
|
|
|
@link = "#{Discourse.base_url}/u/#{target_user.username}/activity/topics"
|
|
|
|
@atom_link = "#{Discourse.base_url}/u/#{target_user.username}/activity/topics.rss"
|
2016-03-31 09:10:50 -04:00
|
|
|
@description = I18n.t("rss_description.user_topics", username: target_user.username)
|
2019-05-06 21:27:05 -04:00
|
|
|
|
|
|
|
@topic_list = TopicQuery
|
|
|
|
.new(nil, order: 'created')
|
|
|
|
.public_send("list_topics_by", target_user)
|
2016-03-31 09:10:50 -04:00
|
|
|
|
2013-10-16 01:39:18 -04:00
|
|
|
render 'list', formats: [:rss]
|
2013-02-27 22:36:12 -05:00
|
|
|
end
|
2014-11-24 12:40:33 -05:00
|
|
|
|
2014-08-28 14:34:31 -04:00
|
|
|
def top(options = nil)
|
|
|
|
options ||= {}
|
2016-10-11 11:56:46 -04:00
|
|
|
period = ListController.best_period_for(current_user.try(:previous_visit_at), options[:category])
|
2019-05-06 21:57:55 -04:00
|
|
|
public_send("top_#{period}", options)
|
2013-12-23 18:50:36 -05:00
|
|
|
end
|
|
|
|
|
2014-02-03 10:08:00 -05:00
|
|
|
def category_top
|
2015-09-02 14:25:18 -04:00
|
|
|
top(category: @category.id)
|
2014-01-17 17:52:06 -05:00
|
|
|
end
|
|
|
|
|
2014-02-03 10:08:00 -05:00
|
|
|
def category_none_top
|
2015-09-02 14:25:18 -04:00
|
|
|
top(category: @category.id, no_subcategories: true)
|
2014-01-17 17:52:06 -05:00
|
|
|
end
|
|
|
|
|
2014-01-13 19:02:14 -05:00
|
|
|
TopTopic.periods.each do |period|
|
2014-01-17 17:52:06 -05:00
|
|
|
define_method("top_#{period}") do |options = nil|
|
|
|
|
top_options = build_topic_list_options
|
|
|
|
top_options.merge!(options) if options
|
|
|
|
top_options[:per_page] = SiteSetting.topics_per_period_in_top_page
|
2015-09-02 14:25:18 -04:00
|
|
|
|
2014-01-13 19:02:14 -05:00
|
|
|
user = list_target_user
|
2014-05-07 13:04:39 -04:00
|
|
|
list = TopicQuery.new(user, top_options).list_top_for(period)
|
2014-08-28 14:34:31 -04:00
|
|
|
list.for_period = period
|
2015-02-13 06:34:22 -05:00
|
|
|
list.more_topics_url = construct_url_with(:next, top_options)
|
|
|
|
list.prev_topics_url = construct_url_with(:prev, top_options)
|
2017-03-13 04:54:10 -04:00
|
|
|
@rss = "top_#{period}"
|
2014-10-30 14:26:35 -04:00
|
|
|
|
|
|
|
if use_crawler_layout?
|
2017-06-12 13:50:30 -04:00
|
|
|
@title = I18n.t("js.filters.top.#{period}.title") + " - #{SiteSetting.title}"
|
2014-10-30 14:26:35 -04:00
|
|
|
end
|
|
|
|
|
2015-01-08 16:44:27 -05:00
|
|
|
respond_with_list(list)
|
2014-01-13 19:02:14 -05:00
|
|
|
end
|
|
|
|
|
2014-02-03 10:08:00 -05:00
|
|
|
define_method("category_top_#{period}") do
|
2019-05-06 21:27:05 -04:00
|
|
|
self.public_send("top_#{period}", category: @category.id)
|
2014-01-17 17:52:06 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2014-02-03 10:08:00 -05:00
|
|
|
define_method("category_none_top_#{period}") do
|
2019-05-06 21:27:05 -04:00
|
|
|
self.public_send("top_#{period}",
|
|
|
|
category: @category.id,
|
|
|
|
no_subcategories: true
|
|
|
|
)
|
2014-01-17 17:52:06 -05:00
|
|
|
end
|
2014-02-03 10:08:00 -05:00
|
|
|
|
2017-03-13 04:54:10 -04:00
|
|
|
# rss feed
|
|
|
|
define_method("top_#{period}_feed") do |options = nil|
|
|
|
|
discourse_expires_in 1.minute
|
|
|
|
|
|
|
|
@description = I18n.t("rss_description.top_#{period}")
|
|
|
|
@title = "#{SiteSetting.title} - #{@description}"
|
|
|
|
@link = "#{Discourse.base_url}/top/#{period}"
|
|
|
|
@atom_link = "#{Discourse.base_url}/top/#{period}.rss"
|
|
|
|
@topic_list = TopicQuery.new(nil).list_top_for(period)
|
|
|
|
|
|
|
|
render 'list', formats: [:rss]
|
|
|
|
end
|
2013-12-13 17:18:28 -05:00
|
|
|
end
|
|
|
|
|
2014-01-17 17:52:06 -05:00
|
|
|
protected
|
|
|
|
|
2019-10-29 07:58:00 -04:00
|
|
|
def next_page_params
|
|
|
|
page_params.merge(page: params[:page].to_i + 1)
|
2014-02-25 12:15:20 -05:00
|
|
|
end
|
|
|
|
|
2019-10-29 07:58:00 -04:00
|
|
|
def prev_page_params
|
2014-03-03 12:56:37 -05:00
|
|
|
pg = params[:page].to_i
|
|
|
|
if pg > 1
|
2019-10-29 07:58:00 -04:00
|
|
|
page_params.merge(page: pg - 1)
|
2014-03-03 12:56:37 -05:00
|
|
|
else
|
2019-10-29 07:58:00 -04:00
|
|
|
page_params.merge(page: nil)
|
2014-03-03 12:56:37 -05:00
|
|
|
end
|
2014-02-25 12:15:20 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-10-29 07:58:00 -04:00
|
|
|
def page_params
|
2015-09-02 14:25:18 -04:00
|
|
|
route_params = { format: 'json' }
|
2019-10-30 13:22:32 -04:00
|
|
|
|
|
|
|
if @category.present?
|
|
|
|
slug_path = @category.slug_path
|
|
|
|
|
|
|
|
route_params[:category_slug_path_with_id] =
|
|
|
|
(slug_path + [@category.id.to_s]).join("/")
|
|
|
|
end
|
|
|
|
|
2019-12-11 21:49:21 -05:00
|
|
|
route_params[:username] = UrlHelper.encode_component(params[:username]) if params[:username].present?
|
2013-11-14 12:35:18 -05:00
|
|
|
route_params
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-06-19 22:11:14 -04:00
|
|
|
def set_category
|
2019-10-30 13:22:32 -04:00
|
|
|
parts = params.require(:category_slug_path_with_id).split('/')
|
|
|
|
|
|
|
|
if !parts.empty? && parts.last =~ /\A\d+\Z/
|
|
|
|
id = parts.pop.to_i
|
2013-10-23 14:40:39 -04:00
|
|
|
end
|
2019-10-30 13:22:32 -04:00
|
|
|
slug_path = parts unless parts.empty?
|
2013-10-23 14:40:39 -04:00
|
|
|
|
2019-10-30 13:22:32 -04:00
|
|
|
if id.present?
|
|
|
|
@category = Category.find_by_id(id)
|
|
|
|
elsif slug_path.present?
|
2020-04-22 07:12:33 -04:00
|
|
|
@category = Category.find_by_slug_path(slug_path)
|
2015-12-28 01:28:16 -05:00
|
|
|
|
2019-10-30 13:22:32 -04:00
|
|
|
# Legacy paths
|
2020-02-11 10:01:12 -05:00
|
|
|
if @category.nil? && parts.last =~ /\A\d+-category/
|
2019-10-30 13:22:32 -04:00
|
|
|
@category = Category.find_by_id(parts.last.to_i)
|
|
|
|
end
|
2015-12-28 01:28:16 -05:00
|
|
|
end
|
|
|
|
|
2019-10-30 13:22:32 -04:00
|
|
|
raise Discourse::NotFound.new("category not found", check_permalinks: true) if @category.nil?
|
|
|
|
|
2020-07-07 08:25:36 -04:00
|
|
|
if !guardian.can_see?(@category)
|
|
|
|
if SiteSetting.detailed_404
|
|
|
|
raise Discourse::InvalidAccess
|
|
|
|
else
|
|
|
|
raise Discourse::NotFound
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
current_slug = params.require(:category_slug_path_with_id)
|
|
|
|
real_slug = @category.full_slug("/")
|
2020-07-29 10:17:57 -04:00
|
|
|
|
|
|
|
if SiteSetting.slug_generation_method == "encoded"
|
|
|
|
current_slug = current_slug.split("/").map { |slug| CGI.escape(slug) }.join("/")
|
|
|
|
end
|
|
|
|
|
2020-07-07 08:25:36 -04:00
|
|
|
if current_slug != real_slug
|
|
|
|
url = request.fullpath.gsub(current_slug, real_slug)
|
|
|
|
return redirect_to path(url), status: 301
|
|
|
|
end
|
|
|
|
|
2019-10-30 13:22:32 -04:00
|
|
|
params[:category] = @category.id.to_s
|
2014-02-08 17:10:48 -05:00
|
|
|
|
2020-05-23 00:56:13 -04:00
|
|
|
@description_meta = if @category.uncategorized?
|
|
|
|
I18n.t('category.uncategorized_description', locale: SiteSetting.default_locale)
|
|
|
|
else
|
|
|
|
@category.description_text
|
|
|
|
end
|
|
|
|
@description_meta = SiteSetting.site_description if @description_meta.blank?
|
|
|
|
|
2017-03-02 15:06:47 -05:00
|
|
|
if use_crawler_layout?
|
|
|
|
@subcategories = @category.subcategories.select { |c| guardian.can_see?(c) }
|
|
|
|
end
|
2013-06-19 22:11:14 -04:00
|
|
|
end
|
|
|
|
|
2013-08-24 13:38:02 -04:00
|
|
|
def list_target_user
|
|
|
|
if params[:user_id] && guardian.is_staff?
|
|
|
|
User.find(params[:user_id].to_i)
|
|
|
|
else
|
|
|
|
current_user
|
|
|
|
end
|
|
|
|
end
|
2013-10-19 09:21:17 -04:00
|
|
|
|
|
|
|
def generate_list_for(action, target_user, opts)
|
2019-05-06 21:27:05 -04:00
|
|
|
TopicQuery.new(current_user, opts).public_send("list_#{action}", target_user)
|
2013-10-19 09:21:17 -04:00
|
|
|
end
|
|
|
|
|
2015-02-13 06:34:22 -05:00
|
|
|
def construct_url_with(action, opts, url_prefix = nil)
|
2014-02-03 10:08:00 -05:00
|
|
|
method = url_prefix.blank? ? "#{action_name}_path" : "#{url_prefix}_#{action_name}_path"
|
2019-10-30 13:22:32 -04:00
|
|
|
|
|
|
|
page_params =
|
|
|
|
case action
|
|
|
|
when :prev
|
|
|
|
prev_page_params
|
|
|
|
when :next
|
|
|
|
next_page_params
|
|
|
|
else
|
|
|
|
raise "unreachable"
|
|
|
|
end
|
|
|
|
|
|
|
|
opts = opts.dup
|
2019-11-06 15:25:45 -05:00
|
|
|
if SiteSetting.unicode_usernames && opts[:group_name]
|
2019-12-11 21:49:21 -05:00
|
|
|
opts[:group_name] = UrlHelper.encode_component(opts[:group_name])
|
2019-11-06 15:25:45 -05:00
|
|
|
end
|
2019-10-30 13:22:32 -04:00
|
|
|
opts.delete(:category) if page_params.include?(:category_slug_path_with_id)
|
|
|
|
|
|
|
|
public_send(method, opts.merge(page_params)).sub('.json?', '?')
|
2014-02-25 12:15:20 -05:00
|
|
|
end
|
|
|
|
|
2020-06-03 09:33:02 -04:00
|
|
|
def ensure_can_see_profile!(target_user = nil)
|
|
|
|
raise Discourse::NotFound unless guardian.can_see_profile?(target_user)
|
|
|
|
end
|
|
|
|
|
2017-03-22 16:54:12 -04:00
|
|
|
def self.best_period_for(previous_visit_at, category_id = nil)
|
2019-10-21 06:32:27 -04:00
|
|
|
default_period = ((category_id && Category.where(id: category_id).pluck_first(:default_top_period)) ||
|
2017-03-22 16:54:12 -04:00
|
|
|
SiteSetting.top_page_default_timeframe).to_sym
|
|
|
|
|
|
|
|
best_period_with_topics_for(previous_visit_at, category_id, default_period) || default_period
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.best_period_with_topics_for(previous_visit_at, category_id = nil, default_period = SiteSetting.top_page_default_timeframe)
|
2019-06-04 04:57:50 -04:00
|
|
|
best_periods_for(previous_visit_at, default_period.to_sym).find do |period|
|
2014-05-14 05:49:50 -04:00
|
|
|
top_topics = TopTopic.where("#{period}_score > 0")
|
2015-09-02 14:25:18 -04:00
|
|
|
top_topics = top_topics.joins(:topic).where("topics.category_id = ?", category_id) if category_id
|
2016-11-23 21:29:44 -05:00
|
|
|
top_topics = top_topics.limit(SiteSetting.topics_per_period_in_top_page)
|
2019-06-04 04:57:50 -04:00
|
|
|
top_topics.count == SiteSetting.topics_per_period_in_top_page
|
2014-05-07 13:04:39 -04:00
|
|
|
end
|
2016-11-23 21:29:44 -05:00
|
|
|
end
|
|
|
|
|
2017-03-22 16:54:12 -04:00
|
|
|
def self.best_periods_for(date, default_period = :all)
|
2019-06-04 04:57:50 -04:00
|
|
|
return [default_period, :all].uniq unless date
|
|
|
|
|
2014-05-07 13:04:39 -04:00
|
|
|
periods = []
|
2019-06-04 04:57:50 -04:00
|
|
|
periods << :daily if date > (1.week + 1.day).ago
|
|
|
|
periods << :weekly if date > (1.month + 1.week).ago
|
|
|
|
periods << :monthly if date > (3.months + 3.weeks).ago
|
|
|
|
periods << :quarterly if date > (1.year + 1.month).ago
|
|
|
|
periods << :yearly if date > 3.years.ago
|
|
|
|
periods << :all
|
2014-05-07 13:04:39 -04:00
|
|
|
periods
|
2014-01-13 19:02:14 -05:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|