DEV: Add 'expired' and 'include_subcategories' filters to EventFinder

DEV: Add ‘expired’ and ‘include_subcategories’ filters to EventFinder

diff --git a/app/models/discourse_post_event/event.rb b/app/models/discourse_post_event/event.rb
index 0013daf..02a9ac2 100644
--- a/app/models/discourse_post_event/event.rb
+++ b/app/models/discourse_post_event/event.rb
@@ -38,6 +38,7 @@ module DiscoursePostEvent
     belongs_to :post, foreign_key: :id
 
     scope :visible, -> { where(deleted_at: nil) }
+    scope :expired, -> { where('COALESCE(ends_at, starts_at) < ?', Time.now) }
     scope :not_expired, -> {
       where(<<-SQL, now: Time.now)
         (ends_at IS NOT NULL AND ends_at > :now) OR
diff --git a/lib/discourse_post_event/event_finder.rb b/lib/discourse_post_event/event_finder.rb
index aacfb94..1d5b86f 100644
--- a/lib/discourse_post_event/event_finder.rb
+++ b/lib/discourse_post_event/event_finder.rb
@@ -7,18 +7,32 @@ module DiscoursePostEvent
       topics = listable_topics(guardian)
       pms = private_messages(user)
 
-      events = DiscoursePostEvent::Event
-        .visible
-        .not_expired
+      events = DiscoursePostEvent::Event.visible
+
+      if params[:expired]
+        events = events.expired
+      else
+        events = events.not_expired
+      end
 
       if params[:post_id]
         events = events.where(id: Array(params[:post_id]))
       end
 
-      events.joins(post: :topic)
+      events = events.joins(post: :topic)
         .merge(Post.secured(guardian))
         .merge(topics.or(pms).distinct)
         .order(starts_at: :asc)
+
+      if params[:category_id].present?
+        if params[:include_subcategories].present?
+          events = events.where(topics: { category_id: Category.subcategory_ids(params[:category_id].to_i) })
+        else
+          events = events.where(topics: { category_id: params[:category_id].to_i })
+        end
+      end
+
+      events
     end
 
     private

GitHub sha: c224449c