Skip to content

Commit

Permalink
CV2-3800: filter feed by organization (#1703)
Browse files Browse the repository at this point in the history
  • Loading branch information
melsawy authored Oct 24, 2023
1 parent d520b8f commit 34bf7f8
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 11 deletions.
10 changes: 5 additions & 5 deletions app/models/feed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ def filters
end

# Filters defined by each team
def get_team_filters
def get_team_filters(feed_team_ids = nil)
filters = []
self.feed_teams.each do |ft|
if ft.sharing_enabled?
filters << ft.filters.to_h.reject{ |k, _v| PROHIBITED_FILTERS.include?(k.to_s) }.merge({ 'team_id' => ft.team_id })
end
conditions = { shared: true }
conditions[:team_id] = feed_team_ids unless feed_team_ids.blank?
self.feed_teams.where(conditions).find_each do |ft|
filters << ft.filters.to_h.reject{ |k, _v| PROHIBITED_FILTERS.include?(k.to_s) }.merge({ 'team_id' => ft.team_id })
end
filters
end
Expand Down
4 changes: 0 additions & 4 deletions app/models/feed_team.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ def requests_filters=(filters)
self.send(:set_requests_filters, filters)
end

def sharing_enabled?
self.shared
end

def filters
self.saved_search&.filters.to_h
end
Expand Down
6 changes: 4 additions & 2 deletions lib/check_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ def initialize(options, file = nil, team_id = Team.current&.id)

def team_condition(team_id = nil)
if feed_query?
FeedTeam.where(feed_id: @feed.id, team_id: Team.current&.id).last.shared ? @feed.team_ids : [0] # Invalidate the query if the current team is not sharing content
feed_teams = @options['feed_team_ids'].blank? ? @feed.team_ids : (@feed.team_ids & @options['feed_team_ids'])
is_shared = FeedTeam.where(feed_id: @feed.id, team_id: Team.current&.id, shared: true).last
is_shared ? feed_teams : [0] # Invalidate the query if the current team is not sharing content
else
team_id || Team.current&.id
end
Expand Down Expand Up @@ -746,7 +748,7 @@ def hit_es_for_range_filter
def build_feed_conditions
return {} unless feed_query?
conditions = []
@feed.get_team_filters.each do |filters|
@feed.get_team_filters(@options['feed_team_ids']).each do |filters|
team_id = filters['team_id'].to_i
conditions << CheckSearch.new(filters.to_json, nil, team_id).medias_query
end
Expand Down
25 changes: 25 additions & 0 deletions test/controllers/elastic_search_9_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -264,5 +264,30 @@ def setup
assert_equal [pm1.id, pm3.id, pm2.id], result.medias.map(&:id)
end

test "should filter feed by organization" do
f = create_feed
t1 = create_team ; f.teams << t1
t2 = create_team ; f.teams << t2
t3 = create_team ; f.teams << t3
FeedTeam.update_all(shared: true)
pm1 = create_project_media team: t1, disable_es_callbacks: false
pm2 = create_project_media team: t2, disable_es_callbacks: false
pm3 = create_project_media team: t3, disable_es_callbacks: false
sleep 2
u = create_user
create_team_user team: t1, user: u, role: 'admin'
with_current_user_and_team(u, t1) do
query = { feed_id: f.id }
result = CheckSearch.new(query.to_json)
assert_equal [pm1.id, pm2.id, pm3.id], result.medias.map(&:id).sort
query[:feed_team_ids] = [t1.id, t3.id]
result = CheckSearch.new(query.to_json)
assert_equal [pm1.id, pm3.id], result.medias.map(&:id).sort
query[:feed_team_ids] = [t2.id]
result = CheckSearch.new(query.to_json)
assert_equal [pm2.id], result.medias.map(&:id)
end
end

# Please add new tests to test/controllers/elastic_search_10_test.rb
end

0 comments on commit 34bf7f8

Please sign in to comment.