diff --git a/app/controllers/list_controller.rb b/app/controllers/list_controller.rb index 659e66f008a..bf07fdc0282 100644 --- a/app/controllers/list_controller.rb +++ b/app/controllers/list_controller.rb @@ -71,15 +71,24 @@ class ListController < ApplicationController list = TopicQuery.new(user, list_opts).public_send("list_#{filter}") - if @category.present? && guardian.can_create_shared_draft? - shared_drafts = TopicQuery.new( - user, - category: SiteSetting.shared_drafts_category, - destination_category_id: list_opts[:category] - ).list_latest + 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 - if shared_drafts.present? && shared_drafts.topics.present? - list.shared_drafts = shared_drafts.topics + if shared_drafts.present? && shared_drafts.topics.present? + list.shared_drafts = shared_drafts.topics + end end end diff --git a/app/models/topic.rb b/app/models/topic.rb index 3ab042196a8..cb6e1e23686 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -31,7 +31,7 @@ class Topic < ActiveRecord::Base def_delegator :notifier, :mute!, :notify_muted! def_delegator :notifier, :toggle_mute, :toggle_mute - attr_accessor :allowed_user_ids, :tags_changed + attr_accessor :allowed_user_ids, :tags_changed, :includes_destination_category DiscourseEvent.on(:site_setting_saved) do |site_setting| if site_setting.name.to_s == "slug_generation_method" && site_setting.saved_change_to_value? diff --git a/app/models/topic_list.rb b/app/models/topic_list.rb index 6de3eafcbe6..5aae90f7782 100644 --- a/app/models/topic_list.rb +++ b/app/models/topic_list.rb @@ -26,18 +26,21 @@ class TopicList end end - attr_accessor :more_topics_url, - :prev_topics_url, - :draft, - :draft_key, - :draft_sequence, - :filter, - :for_period, - :per_page, - :top_tags, - :current_user, - :tags, - :shared_drafts + attr_accessor( + :more_topics_url, + :prev_topics_url, + :draft, + :draft_key, + :draft_sequence, + :filter, + :for_period, + :per_page, + :top_tags, + :current_user, + :tags, + :shared_drafts, + :category + ) def initialize(filter, current_user, topics, opts = nil) @filter = filter diff --git a/app/serializers/topic_list_item_serializer.rb b/app/serializers/topic_list_item_serializer.rb index 663cc2c9a26..1f77b45d751 100644 --- a/app/serializers/topic_list_item_serializer.rb +++ b/app/serializers/topic_list_item_serializer.rb @@ -30,8 +30,9 @@ class TopicListItemSerializer < ListableTopicSerializer end def category_id + # If it's a shared draft, show the destination topic instead - if object.category_id == SiteSetting.shared_drafts_category.to_i && object.shared_draft + if object.includes_destination_category && object.shared_draft return object.shared_draft.category_id end diff --git a/lib/topic_query.rb b/lib/topic_query.rb index a6cbacf3f49..563eedf67a0 100644 --- a/lib/topic_query.rb +++ b/lib/topic_query.rb @@ -482,17 +482,19 @@ class TopicQuery end def apply_shared_drafts(result, category_id, options) - viewing_shared = category_id && category_id == SiteSetting.shared_drafts_category.to_i + drafts_category_id = SiteSetting.shared_drafts_category.to_i + viewing_shared = category_id && category_id == drafts_category_id if guardian.can_create_shared_draft? - result = result.includes(:shared_draft).references(:shared_draft) - if options[:destination_category_id] destination_category_id = get_category_id(options[:destination_category_id]) - return result.where("shared_drafts.category_id" => destination_category_id) + topic_ids = SharedDraft.where(category_id: destination_category_id).pluck(:topic_id) + return result.where(id: topic_ids) + elsif viewing_shared + result = result.includes(:shared_draft).references(:shared_draft) + else + return result.where('topics.category_id != ?', drafts_category_id) end - - return result.where("shared_drafts.id IS NULL") unless viewing_shared end result