mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
Add rubocop to our build. (#5004)
This commit is contained in:
@@ -46,7 +46,6 @@ class TopicQuery
|
||||
no_definitions)
|
||||
end
|
||||
|
||||
|
||||
# Maps `order` to a columns in `topics`
|
||||
SORTABLE_MAPPING = {
|
||||
'likes' => 'like_count',
|
||||
@@ -80,21 +79,21 @@ class TopicQuery
|
||||
|
||||
def self.apply_custom_filters(results, topic_query)
|
||||
if @custom_filters
|
||||
@custom_filters.each do |key,filter|
|
||||
@custom_filters.each do |key, filter|
|
||||
results = filter.call(results, topic_query)
|
||||
end
|
||||
end
|
||||
results
|
||||
end
|
||||
|
||||
def initialize(user=nil, options={})
|
||||
def initialize(user = nil, options = {})
|
||||
options.assert_valid_keys(TopicQuery.valid_options)
|
||||
@options = options.dup
|
||||
@user = user
|
||||
@guardian = Guardian.new(@user)
|
||||
end
|
||||
|
||||
def joined_topic_user(list=nil)
|
||||
def joined_topic_user(list = nil)
|
||||
(list || Topic).joins("LEFT OUTER JOIN topic_users AS tu ON (topics.id = tu.topic_id AND tu.user_id = #{@user.id.to_i})")
|
||||
end
|
||||
|
||||
@@ -108,8 +107,8 @@ class TopicQuery
|
||||
if topic.private_message?
|
||||
|
||||
group_ids = topic.topic_allowed_groups
|
||||
.where('group_id IN (SELECT group_id FROM group_users WHERE user_id = :user_id)', user_id: @user.id)
|
||||
.pluck(:group_id)
|
||||
.where('group_id IN (SELECT group_id FROM group_users WHERE user_id = :user_id)', user_id: @user.id)
|
||||
.pluck(:group_id)
|
||||
{
|
||||
topic: topic,
|
||||
my_group_ids: group_ids,
|
||||
@@ -151,7 +150,7 @@ class TopicQuery
|
||||
builder.add_results(random_suggested(topic, builder.results_left, builder.excluded_topic_ids)) unless builder.full?
|
||||
end
|
||||
|
||||
params = {unordered: true}
|
||||
params = { unordered: true }
|
||||
if topic.private_message?
|
||||
params[:preload_posters] = true
|
||||
end
|
||||
@@ -170,19 +169,19 @@ class TopicQuery
|
||||
end
|
||||
|
||||
def list_new
|
||||
create_list(:new, {unordered: true}, new_results)
|
||||
create_list(:new, { unordered: true }, new_results)
|
||||
end
|
||||
|
||||
def list_unread
|
||||
create_list(:unread, {unordered: true}, unread_results)
|
||||
create_list(:unread, { unordered: true }, unread_results)
|
||||
end
|
||||
|
||||
def list_posted
|
||||
create_list(:posted) {|l| l.where('tu.posted') }
|
||||
create_list(:posted) { |l| l.where('tu.posted') }
|
||||
end
|
||||
|
||||
def list_bookmarks
|
||||
create_list(:bookmarks) {|l| l.where('tu.bookmarked') }
|
||||
create_list(:bookmarks) { |l| l.where('tu.bookmarked') }
|
||||
end
|
||||
|
||||
def list_top_for(period)
|
||||
@@ -207,14 +206,14 @@ class TopicQuery
|
||||
def not_archived(list, user)
|
||||
list.joins("LEFT JOIN user_archived_messages um
|
||||
ON um.user_id = #{user.id.to_i} AND um.topic_id = topics.id")
|
||||
.where('um.user_id IS NULL')
|
||||
.where('um.user_id IS NULL')
|
||||
end
|
||||
|
||||
def list_private_messages(user)
|
||||
list = private_messages_for(user, :user)
|
||||
|
||||
list = not_archived(list, user)
|
||||
.where('NOT (topics.participant_count = 1 AND topics.user_id = ?)', user.id)
|
||||
.where('NOT (topics.participant_count = 1 AND topics.user_id = ?)', user.id)
|
||||
|
||||
create_list(:private_messages, {}, list)
|
||||
end
|
||||
@@ -262,8 +261,8 @@ class TopicQuery
|
||||
def list_category_topic_ids(category)
|
||||
query = default_results(category: category.id)
|
||||
pinned_ids = query.where('pinned_at IS NOT NULL AND category_id = ?', category.id)
|
||||
.limit(nil)
|
||||
.order('pinned_at DESC').pluck(:id)
|
||||
.limit(nil)
|
||||
.order('pinned_at DESC').pluck(:id)
|
||||
non_pinned_ids = query.where('pinned_at IS NULL OR category_id <> ?', category.id).pluck(:id)
|
||||
(pinned_ids + non_pinned_ids)
|
||||
end
|
||||
@@ -276,8 +275,8 @@ class TopicQuery
|
||||
|
||||
def self.new_filter(list, treat_as_new_topic_start_date)
|
||||
list.where("topics.created_at >= :created_at", created_at: treat_as_new_topic_start_date)
|
||||
.where("tu.last_read_post_number IS NULL")
|
||||
.where("COALESCE(tu.notification_level, :tracking) >= :tracking", tracking: TopicUser.notification_levels[:tracking])
|
||||
.where("tu.last_read_post_number IS NULL")
|
||||
.where("COALESCE(tu.notification_level, :tracking) >= :tracking", tracking: TopicUser.notification_levels[:tracking])
|
||||
end
|
||||
|
||||
def self.unread_filter(list, user_id, opts)
|
||||
@@ -293,8 +292,8 @@ class TopicQuery
|
||||
col_name = opts[:staff] ? "highest_staff_post_number" : "highest_post_number"
|
||||
|
||||
list
|
||||
.where("tu.last_read_post_number < topics.#{col_name}")
|
||||
.where("COALESCE(tu.notification_level, :regular) >= :tracking",
|
||||
.where("tu.last_read_post_number < topics.#{col_name}")
|
||||
.where("COALESCE(tu.notification_level, :regular) >= :tracking",
|
||||
regular: TopicUser.notification_levels[:regular], tracking: TopicUser.notification_levels[:tracking])
|
||||
end
|
||||
|
||||
@@ -322,12 +321,12 @@ class TopicQuery
|
||||
|
||||
end
|
||||
|
||||
def create_list(filter, options={}, topics = nil)
|
||||
def create_list(filter, options = {}, topics = nil)
|
||||
topics ||= default_results(options)
|
||||
topics = yield(topics) if block_given?
|
||||
|
||||
options = options.merge(@options)
|
||||
if ["activity","default"].include?(options[:order] || "activity") &&
|
||||
if ["activity", "default"].include?(options[:order] || "activity") &&
|
||||
!options[:unordered] &&
|
||||
filter != :private_messages
|
||||
topics = prioritize_pinned_topics(topics, options)
|
||||
@@ -348,7 +347,7 @@ class TopicQuery
|
||||
end
|
||||
|
||||
topics.each do |t|
|
||||
t.allowed_user_ids = filter == :private_messages ? t.allowed_users.map{|u| u.id} : []
|
||||
t.allowed_user_ids = filter == :private_messages ? t.allowed_users.map { |u| u.id } : []
|
||||
end
|
||||
|
||||
list = TopicList.new(filter, @user, topics, options.merge(@options))
|
||||
@@ -356,7 +355,7 @@ class TopicQuery
|
||||
list
|
||||
end
|
||||
|
||||
def latest_results(options={})
|
||||
def latest_results(options = {})
|
||||
result = default_results(options)
|
||||
result = remove_muted_topics(result, @user) unless options && options[:state] == "muted".freeze
|
||||
result = remove_muted_categories(result, @user, exclude: options[:category])
|
||||
@@ -370,12 +369,12 @@ class TopicQuery
|
||||
result
|
||||
end
|
||||
|
||||
def unread_results(options={})
|
||||
def unread_results(options = {})
|
||||
result = TopicQuery.unread_filter(
|
||||
default_results(options.reverse_merge(:unordered => true)),
|
||||
default_results(options.reverse_merge(unordered: true)),
|
||||
@user&.id,
|
||||
staff: @user&.staff?)
|
||||
.order('CASE WHEN topics.user_id = tu.user_id THEN 1 ELSE 2 END')
|
||||
.order('CASE WHEN topics.user_id = tu.user_id THEN 1 ELSE 2 END')
|
||||
|
||||
self.class.results_filter_callbacks.each do |filter_callback|
|
||||
result = filter_callback.call(:unread, result, @user, options)
|
||||
@@ -384,10 +383,10 @@ class TopicQuery
|
||||
suggested_ordering(result, options)
|
||||
end
|
||||
|
||||
def new_results(options={})
|
||||
def new_results(options = {})
|
||||
# TODO does this make sense or should it be ordered on created_at
|
||||
# it is ordering on bumped_at now
|
||||
result = TopicQuery.new_filter(default_results(options.reverse_merge(:unordered => true)), @user.user_option.treat_as_new_topic_start_date)
|
||||
result = TopicQuery.new_filter(default_results(options.reverse_merge(unordered: true)), @user.user_option.treat_as_new_topic_start_date)
|
||||
result = remove_muted_topics(result, @user)
|
||||
result = remove_muted_categories(result, @user, exclude: options[:category])
|
||||
result = remove_muted_tags(result, @user, options)
|
||||
@@ -436,8 +435,8 @@ class TopicQuery
|
||||
end
|
||||
|
||||
result = result.joins("LEFT OUTER JOIN topic_users AS tu ON (topics.id = tu.topic_id AND tu.user_id = #{user.id.to_i})")
|
||||
.order("topics.bumped_at DESC")
|
||||
.private_messages
|
||||
.order("topics.bumped_at DESC")
|
||||
.private_messages
|
||||
|
||||
result = result.limit(options[:per_page]) unless options[:limit] == false
|
||||
result = result.visible if options[:visible] || @user.nil? || @user.regular?
|
||||
@@ -489,9 +488,8 @@ class TopicQuery
|
||||
category_id
|
||||
end
|
||||
|
||||
|
||||
# Create results based on a bunch of default options
|
||||
def default_results(options={})
|
||||
def default_results(options = {})
|
||||
options.reverse_merge!(@options)
|
||||
options.reverse_merge!(per_page: per_page_setting)
|
||||
|
||||
@@ -504,7 +502,7 @@ class TopicQuery
|
||||
|
||||
if @user
|
||||
result = result.joins("LEFT OUTER JOIN topic_users AS tu ON (topics.id = tu.topic_id AND tu.user_id = #{@user.id.to_i})")
|
||||
.references('tu')
|
||||
.references('tu')
|
||||
end
|
||||
|
||||
category_id = get_category_id(options[:category])
|
||||
@@ -559,7 +557,7 @@ class TopicQuery
|
||||
end
|
||||
elsif @options[:no_tags]
|
||||
# the following will do: ("topics"."id" NOT IN (SELECT DISTINCT "topic_tags"."topic_id" FROM "topic_tags"))
|
||||
result = result.where.not(:id => TopicTag.select(:topic_id).uniq)
|
||||
result = result.where.not(id: TopicTag.select(:topic_id).uniq)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -578,7 +576,7 @@ class TopicQuery
|
||||
result = result.limit(options[:per_page]) unless options[:limit] == false
|
||||
|
||||
result = result.visible if options[:visible]
|
||||
result = result.where.not(topics: {id: options[:except_topic_ids]}).references(:topics) if options[:except_topic_ids]
|
||||
result = result.where.not(topics: { id: options[:except_topic_ids] }).references(:topics) if options[:except_topic_ids]
|
||||
|
||||
if options[:page]
|
||||
offset = options[:page].to_i * options[:per_page]
|
||||
@@ -641,7 +639,7 @@ class TopicQuery
|
||||
end
|
||||
end
|
||||
|
||||
if (filter=options[:filter]) && @user
|
||||
if (filter = options[:filter]) && @user
|
||||
action =
|
||||
if filter == "bookmarked"
|
||||
PostActionType.types[:bookmark]
|
||||
@@ -665,7 +663,7 @@ class TopicQuery
|
||||
result = result.where('topics.posts_count <= ?', options[:max_posts]) if options[:max_posts].present?
|
||||
result = result.where('topics.posts_count >= ?', options[:min_posts]) if options[:min_posts].present?
|
||||
|
||||
result = TopicQuery.apply_custom_filters(result,self)
|
||||
result = TopicQuery.apply_custom_filters(result, self)
|
||||
|
||||
@guardian.filter_allowed_categories(result)
|
||||
end
|
||||
@@ -677,12 +675,12 @@ class TopicQuery
|
||||
|
||||
list
|
||||
end
|
||||
def remove_muted_categories(list, user, opts=nil)
|
||||
def remove_muted_categories(list, user, opts = nil)
|
||||
category_id = get_category_id(opts[:exclude]) if opts
|
||||
|
||||
if user
|
||||
list = list.references("cu")
|
||||
.where("
|
||||
.where("
|
||||
NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM category_users cu
|
||||
@@ -699,7 +697,7 @@ class TopicQuery
|
||||
|
||||
list
|
||||
end
|
||||
def remove_muted_tags(list, user, opts=nil)
|
||||
def remove_muted_tags(list, user, opts = nil)
|
||||
if user.nil? || !SiteSetting.tagging_enabled || !SiteSetting.remove_muted_tags_from_latest
|
||||
list
|
||||
else
|
||||
@@ -732,7 +730,7 @@ class TopicQuery
|
||||
def new_messages(params)
|
||||
|
||||
TopicQuery.new_filter(messages_for_groups_or_user(params[:my_group_ids]), Time.at(SiteSetting.min_new_topics_time).to_datetime)
|
||||
.limit(params[:count])
|
||||
.limit(params[:count])
|
||||
|
||||
end
|
||||
|
||||
@@ -741,7 +739,7 @@ class TopicQuery
|
||||
messages_for_groups_or_user(params[:my_group_ids]),
|
||||
@user&.id,
|
||||
staff: @user&.staff?)
|
||||
.limit(params[:count])
|
||||
.limit(params[:count])
|
||||
end
|
||||
|
||||
def related_messages_user(params)
|
||||
@@ -811,7 +809,7 @@ class TopicQuery
|
||||
query.order('topics.bumped_at DESC')
|
||||
end
|
||||
|
||||
def random_suggested(topic, count, excluded_topic_ids=[])
|
||||
def random_suggested(topic, count, excluded_topic_ids = [])
|
||||
result = default_results(unordered: true, per_page: count).where(closed: false, archived: false)
|
||||
excluded_topic_ids += Category.topic_ids.to_a
|
||||
result = result.where("topics.id NOT IN (?)", excluded_topic_ids) unless excluded_topic_ids.empty?
|
||||
@@ -829,7 +827,7 @@ class TopicQuery
|
||||
# of muted, big edge case
|
||||
#
|
||||
# we over select in case cache is stale
|
||||
max = (count*1.3).to_i
|
||||
max = (count * 1.3).to_i
|
||||
ids = RandomTopicSelector.next(max) + RandomTopicSelector.next(max, topic.category)
|
||||
|
||||
result.where(id: ids.uniq)
|
||||
|
Reference in New Issue
Block a user