From ff3e1e1dd7df06627c317048f2139e196d8cb07c Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Mon, 23 Mar 2015 18:12:37 -0400 Subject: [PATCH] FIX: User's topic lists weren't consistent WRT visibility --- .../javascripts/discourse/models/user.js.es6 | 14 ++------------ lib/topic_query.rb | 8 +++++++- spec/components/topic_query_spec.rb | 12 ++++++++++++ 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/app/assets/javascripts/discourse/models/user.js.es6 b/app/assets/javascripts/discourse/models/user.js.es6 index 23816363ea..1f9141604e 100644 --- a/app/assets/javascripts/discourse/models/user.js.es6 +++ b/app/assets/javascripts/discourse/models/user.js.es6 @@ -256,12 +256,7 @@ const User = Discourse.Model.extend({ ua.action_type === Discourse.UserAction.TYPES.topics; }, - /** - The user's stat count, excluding PMs. - - @property statsCountNonPM - @type {Integer} - **/ + // The user's stat count, excluding PMs. statsCountNonPM: function() { var self = this; @@ -275,12 +270,7 @@ const User = Discourse.Model.extend({ return count; }.property('statsExcludingPms.@each.count'), - /** - The user's stats, excluding PMs. - - @property statsExcludingPms - @type {Array} - **/ + // The user's stats, excluding PMs. statsExcludingPms: function() { if (this.blank('stats')) return []; return this.get('stats').rejectProperty('isPM'); diff --git a/lib/topic_query.rb b/lib/topic_query.rb index 6ffdab0a59..c19e01b961 100644 --- a/lib/topic_query.rb +++ b/lib/topic_query.rb @@ -105,6 +105,7 @@ class TopicQuery end def list_topics_by(user) + @options[:filtered_to_user] = user.id create_list(:user_topics) do |topics| topics.where(user_id: user.id) end @@ -281,6 +282,10 @@ class TopicQuery options.reverse_merge!(@options) options.reverse_merge!(per_page: per_page_setting) + # Whether to return visible topics + options[:visible] = true if @user.nil? || @user.regular? + options[:visible] = false if @user && @user.id == options[:filtered_to_user] + # Start with a list of all topics result = Topic.unscoped @@ -310,7 +315,8 @@ class TopicQuery end result = result.limit(options[:per_page]) unless options[:limit] == false - result = result.visible if options[:visible] || @user.nil? || @user.regular? + + 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.offset(options[:page].to_i * options[:per_page]) if options[:page] diff --git a/spec/components/topic_query_spec.rb b/spec/components/topic_query_spec.rb index 378248a501..54882d9669 100644 --- a/spec/components/topic_query_spec.rb +++ b/spec/components/topic_query_spec.rb @@ -40,6 +40,18 @@ describe TopicQuery do end + context "list_topics_by" do + + it "allows users to view their own invisible topics" do + topic = Fabricate(:topic, user: user) + invisible_topic = Fabricate(:topic, user: user, visible: false) + + expect(TopicQuery.new(nil).list_topics_by(user).topics.count).to eq(1) + expect(TopicQuery.new(user).list_topics_by(user).topics.count).to eq(2) + end + + end + context 'bookmarks' do it "filters and returns bookmarks correctly" do post = Fabricate(:post)