Skip to content

A platform for community discussion. Free, open, simple.

FIX: Re-check category, tag and channel visibility in cached Rewinds

Régis Hanol058ace52fb49
diff --git a/plugins/discourse-rewind/app/services/discourse_rewind/action/chat_usage.rb b/plugins/discourse-rewind/app/services/discourse_rewind/action/chat_usage.rb
index 88fef2790b5..ddf7ce97602 100644
--- a/plugins/discourse-rewind/app/services/discourse_rewind/action/chat_usage.rb
+++ b/plugins/discourse-rewind/app/services/discourse_rewind/action/chat_usage.rb
@@ -38,11 +38,7 @@ module DiscourseRewind
channel_usage =
messages
.joins(:chat_channel)
- .joins(
- "INNER JOIN categories ON categories.id = chat_channels.chatable_id AND chat_channels.chatable_type = 'Category'",
- )
- .where(chat_channels: { type: "CategoryChannel" })
- .where(categories: { read_restricted: false })
+ .merge(self.class.public_category_channels)
.group("chat_channels.id", "chat_channels.slug")
.order("COUNT(*) DESC", "chat_channels.id")
.limit(5)
@@ -98,6 +94,23 @@ module DiscourseRewind
}
end
+ def self.public_category_channels
+ Chat::Channel.public_channels.where(categories: { read_restricted: false })
+ end
+
+ def self.filter_for_viewer(report, **)
+ favorite_channels = report[:data][:favorite_channels]
+ public_channel_ids =
+ public_category_channels.where(id: favorite_channels.pluck(:channel_id)).pluck(:id)
+
+ report.deep_merge(
+ data: {
+ favorite_channels:
+ favorite_channels.select { |channel| channel[:channel_id].in?(public_channel_ids) },
+ },
+ )
+ end
+
def enabled?
Discourse.plugins_by_name["chat"]&.enabled?
end
diff --git a/plugins/discourse-rewind/app/services/discourse_rewind/action/most_viewed_categories.rb b/plugins/discourse-rewind/app/services/discourse_rewind/action/most_viewed_categories.rb
index 0af5c49b738..7896b98d2c7 100644
--- a/plugins/discourse-rewind/app/services/discourse_rewind/action/most_viewed_categories.rb
+++ b/plugins/discourse-rewind/app/services/discourse_rewind/action/most_viewed_categories.rb
@@ -1,6 +1,5 @@
# frozen_string_literal: true
-# Topics visited grouped by category
module DiscourseRewind
module Action
class MostViewedCategories < BaseReport
@@ -13,29 +12,36 @@ module DiscourseRewind
],
identifier: "most-viewed-categories",
}
+
def call
return FakeData if should_use_fake_data?
most_viewed_categories =
TopicViewItem
- .joins(:topic)
- .joins("INNER JOIN categories ON categories.id = topics.category_id")
- .where(
- user: user,
- viewed_at: date,
- categories: {
- id: user.guardian.allowed_category_ids,
- read_restricted: false,
- },
- )
+ .joins(topic: :category)
+ .merge(self.class.public_categories)
+ .where(user:, viewed_at: date)
.group("categories.id, categories.name")
.order("COUNT(*) DESC, categories.id")
.limit(4)
.pluck("categories.id, categories.name")
- .map { |category_id, name| { category_id: category_id, name: name } }
+ .map { |category_id, name| { category_id:, name: } }
{ data: most_viewed_categories, identifier: "most-viewed-categories" }
end
+
+ def self.public_categories
+ Category.where(read_restricted: false)
+ end
+
+ def self.filter_for_viewer(report, **)
+ public_category_ids =
+ public_categories.where(id: report[:data].pluck(:category_id)).pluck(:id)
+
+ report.merge(
+ data: report[:data].select { |category| category[:category_id].in?(public_category_ids) },
+ )
+ end
end
end
end
diff --git a/plugins/discourse-rewind/app/services/discourse_rewind/action/most_viewed_tags.rb b/plugins/discourse-rewind/app/services/discourse_rewind/action/most_viewed_tags.rb
index f50e0459fda..fb92c749f68 100644
--- a/plugins/discourse-rewind/app/services/discourse_rewind/action/most_viewed_tags.rb
+++ b/plugins/discourse-rewind/app/services/discourse_rewind/action/most_viewed_tags.rb
@@ -1,6 +1,5 @@
# frozen_string_literal: true
-# Topics visited grouped by tag
module DiscourseRewind
module Action
class MostViewedTags < BaseReport
@@ -19,19 +18,27 @@ module DiscourseRewind
most_viewed_tags =
TopicViewItem
- .joins(:topic)
+ .joins(topic: :tags)
.merge(Topic.listable_topics.secured)
- .joins("INNER JOIN topic_tags ON topic_tags.topic_id = topics.id")
- .joins("INNER JOIN tags ON tags.id = topic_tags.tag_id")
- .where(user: user, viewed_at: date, tags: { id: Tag.visible(Guardian.new).pluck(:id) })
+ .where(user:, viewed_at: date, tags: { id: self.class.public_tags.select(:id) })
.group("tags.id, tags.name, tags.slug")
.order("COUNT(DISTINCT topic_views.topic_id) DESC, tags.id")
.limit(4)
.pluck("tags.id, tags.slug, tags.name")
- .map { |tag_id, slug, name| { tag_id: tag_id, slug: slug, name: name } }
+ .map { |tag_id, slug, name| { tag_id:, slug:, name: } }
{ data: most_viewed_tags, identifier: "most-viewed-tags" }
end
+
+ def self.public_tags
+ Tag.visible(Guardian.new)
+ end
+
+ def self.filter_for_viewer(report, **)
+ visible_tag_ids = public_tags.where(id: report[:data].pluck(:tag_id)).pluck(:id)
+
+ report.merge(data: report[:data].select { |tag| tag[:tag_id].in?(visible_tag_ids) })
+ end
end
end
end
diff --git a/plugins/discourse-rewind/spec/actions/chat_usage_spec.rb b/plugins/discourse-rewind/spec/actions/chat_usage_spec.rb
index 7c3c3e184df..f728086b9ec 100644
--- a/plugins/discourse-rewind/spec/actions/chat_usage_spec.rb
+++ b/plugins/discourse-rewind/spec/actions/chat_usage_spec.rb
@@ -47,4 +47,19 @@ RSpec.describe DiscourseRewind::Action::ChatUsage do
end
end
end
+
+ describe ".filter_for_viewer" do
+ it "drops favorite channels that are no longer public, even for the owner" do
+ report = {
+ data: {
+ favorite_channels:
+ [public_channel, private_channel].map { |channel| { channel_id: channel.id } },
+ },
+ }
+
+ filtered = described_class.filter_for_viewer(report, guardian: user.guardian, for_user: user)
+
+ expect(filtered[:data][:favorite_channels]).to eq([{ channel_id: public_channel.id }])
+ end
+ end
end
diff --git a/plugins/discourse-rewind/spec/actions/most_viewed_categories_spec.rb b/plugins/discourse-rewind/spec/actions/most_viewed_categories_spec.rb
index e6523acf78c..5cb47add201 100644
--- a/plugins/discourse-rewind/spec/actions/most_viewed_categories_spec.rb
+++ b/plugins/discourse-rewind/spec/actions/most_viewed_categories_spec.rb
@@ -109,4 +109,15 @@ RSpec.describe DiscourseRewind::Action::MostViewedCategories do
end
end
end
+
+ describe ".filter_for_viewer" do
+ it "drops categories that are no longer public, even for the owner" do
+ report = { data: [category_1, category_2].map { |category| { category_id: category.id } } }
+ category_2.update!(read_restricted: true)
+
+ filtered = described_class.filter_for_viewer(report, guardian: user.guardian, for_user: user)
+
+ expect(filtered[:data]).to eq([{ category_id: category_1.id }])
+ end
+ end
end
diff --git a/plugins/discourse-rewind/spec/actions/most_viewed_tags_spec.rb b/plugins/discourse-rewind/spec/actions/most_viewed_tags_spec.rb
index c4dbb443043..9b3e842de29 100644
--- a/plugins/discourse-rewind/spec/actions/most_viewed_tags_spec.rb
+++ b/plugins/discourse-rewind/spec/actions/most_viewed_tags_spec.rb
@@ -184,4 +184,19 @@ RSpec.describe DiscourseRewind::Action::MostViewedTags do
end
end
end
+
+ describe ".filter_for_viewer" do
+ it "drops tags that are no longer visible to everyone, even for the owner" do
+ report = { data: [tag_1, tag_2].map { |tag| { tag_id: tag.id } } }
+ group = Fabricate(:group)
+ group.add(user)
+ tag_group = Fabricate(:tag_group, tags: [tag_2])
+ tag_group.permissions = [[group, TagGroupPermission.permission_types[:full]]]
+ tag_group.save!
+
+ filtered = described_class.filter_for_viewer(report, guardian: user.guardian, for_user: user)
+
+ expect(filtered[:data]).to eq([{ tag_id: tag_1.id }])
+ end
+ end
end