Class: TopicsFilter

Inherits:
Object
  • Object
show all
Defined in:
lib/topics_filter.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(guardian:, scope: Topic.all) ⇒ TopicsFilter

Returns a new instance of TopicsFilter.



6
7
8
9
10
# File 'lib/topics_filter.rb', line 6

def initialize(guardian:, scope: Topic.all)
  @guardian = guardian
  @scope = scope
  @topic_notification_levels = Set.new
end

Instance Attribute Details

#topic_notification_levelsObject (readonly)

Returns the value of attribute topic_notification_levels.



4
5
6
# File 'lib/topics_filter.rb', line 4

def topic_notification_levels
  @topic_notification_levels
end

Class Method Details

.add_filter_by_status(status, &blk) ⇒ Object



96
97
98
# File 'lib/topics_filter.rb', line 96

def self.add_filter_by_status(status, &blk)
  custom_status_filters[status] = blk
end

.custom_status_filtersObject



100
101
102
# File 'lib/topics_filter.rb', line 100

def self.custom_status_filters
  @custom_status_filters ||= {}
end

Instance Method Details

#filter_from_query_string(query_string) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/topics_filter.rb', line 15

def filter_from_query_string(query_string)
  return @scope if query_string.blank?

  filters = {}

  query_string.scan(
    /(?<key_prefix>(?:-|=|-=|=-))?(?<key>[\w-]+):(?<value>[^\s]+)/,
  ) do |key_prefix, key, value|
    key = FILTER_ALIASES[key] || key

    filters[key] ||= {}
    filters[key]["key_prefixes"] ||= []
    filters[key]["key_prefixes"] << key_prefix
    filters[key]["values"] ||= []
    filters[key]["values"] << value
  end

  filters.each do |filter, hash|
    key_prefixes = hash["key_prefixes"]
    values = hash["values"]

    filter_values = extract_and_validate_value_for(filter, values)

    case filter
    when "activity-before"
      filter_by_activity(before: filter_values)
    when "activity-after"
      filter_by_activity(after: filter_values)
    when "category"
      filter_categories(values: key_prefixes.zip(filter_values))
    when "created-after"
      filter_by_created(after: filter_values)
    when "created-before"
      filter_by_created(before: filter_values)
    when "created-by"
      filter_created_by_user(usernames: filter_values.flat_map { |value| value.split(",") })
    when "in"
      filter_in(values: filter_values)
    when "latest-post-after"
      filter_by_latest_post(after: filter_values)
    when "latest-post-before"
      filter_by_latest_post(before: filter_values)
    when "likes-min"
      filter_by_number_of_likes(min: filter_values)
    when "likes-max"
      filter_by_number_of_likes(max: filter_values)
    when "likes-op-min"
      filter_by_number_of_likes_in_first_post(min: filter_values)
    when "likes-op-max"
      filter_by_number_of_likes_in_first_post(max: filter_values)
    when "order"
      order_by(values: filter_values)
    when "posts-min"
      filter_by_number_of_posts(min: filter_values)
    when "posts-max"
      filter_by_number_of_posts(max: filter_values)
    when "posters-min"
      filter_by_number_of_posters(min: filter_values)
    when "posters-max"
      filter_by_number_of_posters(max: filter_values)
    when "status"
      filter_values.each { |status| @scope = filter_status(status: status) }
    when "tag_group"
      filter_tag_groups(values: key_prefixes.zip(filter_values))
    when "tag"
      filter_tags(values: key_prefixes.zip(filter_values))
    when "views-min"
      filter_by_number_of_views(min: filter_values)
    when "views-max"
      filter_by_number_of_views(max: filter_values)
    else
      if custom_filter =
           DiscoursePluginRegistry.custom_filter_mappings.find { |hash| hash.key?(filter) }
        @scope = custom_filter[filter].call(@scope, filter_values)
      end
    end
  end

  @scope
end

#filter_status(status:, category_id: nil) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/topics_filter.rb', line 104

def filter_status(status:, category_id: nil)
  case status
  when "open"
    @scope = @scope.where("NOT topics.closed AND NOT topics.archived")
  when "closed"
    @scope = @scope.where("topics.closed")
  when "archived"
    @scope = @scope.where("topics.archived")
  when "listed"
    @scope = @scope.where("topics.visible")
  when "unlisted"
    @scope = @scope.where("NOT topics.visible")
  when "deleted"
    category = category_id.present? ? Category.find_by(id: category_id) : nil

    if @guardian.can_see_deleted_topics?(category)
      @scope = @scope.unscope(where: :deleted_at).where("topics.deleted_at IS NOT NULL")
    end
  when "public"
    @scope = @scope.joins(:category).where("NOT categories.read_restricted")
  else
    if custom_filter = TopicsFilter.custom_status_filters[status]
      @scope = custom_filter.call(@scope)
    end
  end

  @scope
end