2018-09-06 10:07:33 +00:00
|
|
|
# frozen_string_literal: true
|
2012-11-05 18:12:26 +00:00
|
|
|
|
2018-09-06 10:07:33 +00:00
|
|
|
class EventFilter
|
2020-06-19 18:08:39 +00:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
2018-09-06 10:07:33 +00:00
|
|
|
attr_accessor :filter
|
|
|
|
|
|
|
|
ALL = 'all'
|
|
|
|
PUSH = 'push'
|
|
|
|
MERGED = 'merged'
|
|
|
|
ISSUE = 'issue'
|
|
|
|
COMMENTS = 'comments'
|
|
|
|
TEAM = 'team'
|
2020-03-24 12:09:42 +00:00
|
|
|
WIKI = 'wiki'
|
2020-06-19 18:08:39 +00:00
|
|
|
DESIGNS = 'designs'
|
2018-09-06 10:07:33 +00:00
|
|
|
|
|
|
|
def initialize(filter)
|
|
|
|
# Split using comma to maintain backward compatibility Ex/ "filter1,filter2"
|
|
|
|
filter = filter.to_s.split(',')[0].to_s
|
2019-10-11 06:06:27 +00:00
|
|
|
@filter = filters.include?(filter) ? filter : ALL
|
2012-11-05 18:12:26 +00:00
|
|
|
end
|
|
|
|
|
2018-09-06 10:07:33 +00:00
|
|
|
def active?(key)
|
|
|
|
filter == key.to_s
|
2012-11-05 18:12:26 +00:00
|
|
|
end
|
|
|
|
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2014-09-25 22:07:40 +00:00
|
|
|
def apply_filter(events)
|
2018-09-06 10:07:33 +00:00
|
|
|
case filter
|
|
|
|
when PUSH
|
2020-05-28 15:08:02 +00:00
|
|
|
events.pushed_action
|
2018-09-06 10:07:33 +00:00
|
|
|
when MERGED
|
2020-05-28 15:08:02 +00:00
|
|
|
events.merged_action
|
2018-09-06 10:07:33 +00:00
|
|
|
when COMMENTS
|
2020-05-28 15:08:02 +00:00
|
|
|
events.commented_action
|
2018-09-06 10:07:33 +00:00
|
|
|
when TEAM
|
2020-05-28 15:08:02 +00:00
|
|
|
events.where(action: [:joined, :left, :expired])
|
2018-09-06 10:07:33 +00:00
|
|
|
when ISSUE
|
2020-05-28 15:08:02 +00:00
|
|
|
events.where(action: [:created, :updated, :closed, :reopened], target_type: 'Issue')
|
2020-03-24 12:09:42 +00:00
|
|
|
when WIKI
|
|
|
|
wiki_events(events)
|
2020-06-19 18:08:39 +00:00
|
|
|
when DESIGNS
|
|
|
|
design_events(events)
|
2012-11-05 18:12:26 +00:00
|
|
|
else
|
2018-09-06 10:07:33 +00:00
|
|
|
events
|
2016-11-22 17:11:34 +00:00
|
|
|
end
|
2012-11-05 18:12:26 +00:00
|
|
|
end
|
2018-09-06 10:07:33 +00:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2019-10-11 06:06:27 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-03-24 12:09:42 +00:00
|
|
|
def wiki_events(events)
|
|
|
|
events.for_wiki_page
|
|
|
|
end
|
|
|
|
|
2020-06-19 18:08:39 +00:00
|
|
|
def design_events(events)
|
2020-07-17 00:09:37 +00:00
|
|
|
events.for_design
|
2020-06-19 18:08:39 +00:00
|
|
|
end
|
|
|
|
|
2019-10-11 06:06:27 +00:00
|
|
|
def filters
|
2020-06-19 18:08:39 +00:00
|
|
|
[ALL, PUSH, MERGED, ISSUE, COMMENTS, TEAM, WIKI, DESIGNS]
|
|
|
|
end
|
2012-11-05 18:12:26 +00:00
|
|
|
end
|
2019-10-11 06:06:27 +00:00
|
|
|
|
|
|
|
EventFilter.prepend_if_ee('EE::EventFilter')
|