DEV: Introduce TopicGuardian#can_see_topic_ids method (#18692)

Before this commit, there was no way for us to efficiently check an
array of topics for which a user can see. Therefore, this commit
introduces the `TopicGuardian#can_see_topic_ids` method which accepts an
array of `Topic#id`s and filters out the ids which the user is not
allowed to see. The `TopicGuardian#can_see_topic_ids` method is meant to
maintain feature parity with `TopicGuardian#can_see_topic?` at all
times so a consistency check has been added in our tests to ensure that
`TopicGuardian#can_see_topic_ids` returns the same result as
`TopicGuardian#can_see_topic?`. In the near future, the plan is for us
to switch to `TopicGuardian#can_see_topic_ids` completely but I'm not
doing that in this commit as we have to be careful with the performance
impact of such a change.

This method is currently not being used in the current commit but will
be relied on in a subsequent commit.
This commit is contained in:
Alan Guo Xiang Tan
2022-10-27 06:13:21 +08:00
committed by GitHub
parent d4583357cb
commit a473e352de
7 changed files with 222 additions and 18 deletions

View File

@ -27,6 +27,11 @@ RSpec.describe Guardian do
before do
Group.refresh_automatic_groups!
Guardian.enable_topic_can_see_consistency_check
end
after do
Guardian.disable_topic_can_see_consistency_check
end
it 'can be created without a user (not logged in)' do
@ -853,6 +858,7 @@ RSpec.describe Guardian do
expect(Guardian.new(user_gm).can_see?(topic)).to be_falsey
topic.category.update!(reviewable_by_group_id: group.id, topic_id: post.topic.id)
expect(Guardian.new(user_gm).can_see?(topic)).to be_truthy
end
@ -1118,7 +1124,7 @@ RSpec.describe Guardian do
context "with trashed topic" do
before do
topic.deleted_at = Time.now
topic.trash!(admin)
end
it "doesn't allow new posts from regular users" do
@ -1729,14 +1735,14 @@ RSpec.describe Guardian do
end
context 'with private message' do
fab!(:private_message) { Fabricate(:private_message_topic) }
it 'returns false at trust level 3' do
topic.archetype = 'private_message'
expect(Guardian.new(trust_level_3).can_edit?(topic)).to eq(false)
expect(Guardian.new(trust_level_3).can_edit?(private_message)).to eq(false)
end
it 'returns false at trust level 4' do
topic.archetype = 'private_message'
expect(Guardian.new(trust_level_4).can_edit?(topic)).to eq(false)
expect(Guardian.new(trust_level_4).can_edit?(private_message)).to eq(false)
end
end
@ -3965,7 +3971,7 @@ RSpec.describe Guardian do
it "should correctly detect category moderation" do
group.add(user)
category.reviewable_by_group_id = group.id
category.update!(reviewable_by_group_id: group.id)
guardian = Guardian.new(user)
# implementation detail, ensure memoization is good (hence testing twice)