From 10747e12e7a0074d42e85f72b3883c9421310f6f Mon Sep 17 00:00:00 2001 From: Caio <117518+caiosba@users.noreply.github.com> Date: Thu, 21 Mar 2024 21:48:45 -0300 Subject: [PATCH] Fixing how feed filters are applied until a list is chosen. (#1832) Reference: CV2-4398. --- app/models/feed.rb | 9 +++-- test/controllers/elastic_search_10_test.rb | 39 ++++++++++++++++++++++ test/models/bot/smooch_5_test.rb | 8 +++-- test/models/request_test.rb | 6 ++++ 4 files changed, 57 insertions(+), 5 deletions(-) diff --git a/app/models/feed.rb b/app/models/feed.rb index 0a2aab0538..22854259b1 100755 --- a/app/models/feed.rb +++ b/app/models/feed.rb @@ -49,9 +49,12 @@ def get_team_filters(feed_team_ids = nil) conditions = { shared: true } conditions[:team_id] = feed_team_ids if feed_team_ids.is_a?(Array) self.feed_teams.where(conditions).find_each do |ft| - filter = ft.filters - filter = self.saved_search&.filters.to_h if self.team_id == ft.team_id - filters << filter.to_h.reject{ |k, _v| PROHIBITED_FILTERS.include?(k.to_s) }.merge({ 'team_id' => ft.team_id }) + saved_search = self.team_id == ft.team_id ? self.saved_search : ft.saved_search + if saved_search.blank? # Do not share anything from this team if they haven't chosen a list yet + filters << { 'team_id' => ft.team_id, 'report_status' => ['none'] } + else + filters << saved_search.filters.to_h.reject{ |k, _v| PROHIBITED_FILTERS.include?(k.to_s) }.merge({ 'team_id' => ft.team_id }) + end end filters end diff --git a/test/controllers/elastic_search_10_test.rb b/test/controllers/elastic_search_10_test.rb index 4dd96cee74..47f651b3f3 100644 --- a/test/controllers/elastic_search_10_test.rb +++ b/test/controllers/elastic_search_10_test.rb @@ -440,4 +440,43 @@ def setup assert_empty result.medias.map(&:id) Team.current = nil end + + test "should not apply feed filters until a list is chosen" do + t1 = create_team + create_project_media team: t1, disable_es_callbacks: false + create_project_media team: t1, disable_es_callbacks: false + ss1 = create_saved_search team: t1, filters: {} + f = create_feed team: t1, saved_search: nil, data_points: [1, 2], published: true + t2 = create_team + create_project_media team: t2, disable_es_callbacks: false + ss2 = create_saved_search team: t2, filters: {} + ft = create_feed_team feed: f, team: t2, saved_search: nil, shared: true + sleep 2 + Team.current = t1 + query = { feed_id: f.id, feed_view: 'media', show_similar: true } + + # No workspace has chosen a list yet + assert_equal 0, CheckSearch.new(query.to_json, nil, t1.id).number_of_results + + # Only the first workspace has chosen a list + f.saved_search = ss1 + f.save! + assert_equal 2, CheckSearch.new(query.to_json, nil, t1.id).number_of_results + + # Only the second workspace has chosen a list + f.saved_search = nil + f.save! + ft.saved_search = ss2 + ft.save! + assert_equal 1, CheckSearch.new(query.to_json, nil, t1.id).number_of_results + + # Both workspaces have chosen a list + f.saved_search = ss1 + f.save! + ft.saved_search = ss2 + ft.save! + assert_equal 3, CheckSearch.new(query.to_json, nil, t1.id).number_of_results + + Team.current = nil + end end diff --git a/test/models/bot/smooch_5_test.rb b/test/models/bot/smooch_5_test.rb index 2c6aeae8ac..f26e0d90c8 100644 --- a/test/models/bot/smooch_5_test.rb +++ b/test/models/bot/smooch_5_test.rb @@ -46,9 +46,13 @@ def teardown f1 = create_feed team_id: t1.id, published: true, data_points: [1, 2] f1.teams << t2 FeedTeam.update_all(shared: true) + ft = FeedTeam.last + ft_ss = create_saved_search team: t2, filters: {} + ft.saved_search = ft_ss + ft.save! f1.teams << t3 - ft_ss = create_saved_search team: t1, filters: { keyword: 'Bar' } - f1.saved_search = ft_ss + f_ss = create_saved_search team: t1, filters: { keyword: 'Bar' } + f1.saved_search = f_ss f1.save! u = create_bot_user [t1, t2, t3, t4].each { |t| TeamUser.create!(user: u, team: t, role: 'editor') } diff --git a/test/models/request_test.rb b/test/models/request_test.rb index 806eece43d..00a62ccaae 100644 --- a/test/models/request_test.rb +++ b/test/models/request_test.rb @@ -258,6 +258,12 @@ def setup f.teams << t2 f.teams << t3 f.teams << t4 + ss2 = create_saved_search team: t2, filters: {} + ss3 = create_saved_search team: t3, filters: {} + ss4 = create_saved_search team: t4, filters: {} + FeedTeam.where(team: t2, feed: f).update_all(saved_search_id: ss2.id) + FeedTeam.where(team: t3, feed: f).update_all(saved_search_id: ss3.id) + FeedTeam.where(team: t4, feed: f).update_all(saved_search_id: ss4.id) FeedTeam.update_all(shared: true) f.teams << t5 m = create_uploaded_image