FEATURE: ability to register custom filters for posts (#12938)

Allow plugins to extend TopicView to filter posts
This commit is contained in:
Krzysztof Kotlarek
2021-05-10 08:57:58 +10:00
committed by GitHub
parent f337d59ed9
commit a4bd1806d9
4 changed files with 67 additions and 0 deletions

View File

@@ -55,6 +55,29 @@ describe TopicView do
end
end
context 'custom filters' do
fab!(:p0) { Fabricate(:post, topic: topic) }
fab!(:p1) { Fabricate(:post, topic: topic, wiki: true) }
it 'allows to register custom filters' do
tv = TopicView.new(topic.id, evil_trout, { filter: 'wiki' })
expect(tv.filter_posts({ filter: "wiki" })).to eq([p0, p1])
TopicView.add_custom_filter("wiki") do |posts, topic_view|
posts.where(wiki: true)
end
tv = TopicView.new(topic.id, evil_trout, { filter: 'wiki' })
expect(tv.filter_posts).to eq([p1])
tv = TopicView.new(topic.id, evil_trout, { filter: 'whatever' })
expect(tv.filter_posts).to eq([p0, p1])
ensure
TopicView.instance_variable_set(:@custom_filters, [])
end
end
context "setup_filtered_posts" do
describe "filters posts with ignored users" do
fab!(:ignored_user) { Fabricate(:ignored_user, user: evil_trout, ignored_user: user) }