Skip to content

A platform for community discussion. Free, open, simple.

FIX: Handle nil starts_at in ICS feeds and default to recent events (#38669)

Rafael dos Santos Silva9c20eb5da08f
diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb
index ae56e54b112..16507492465 100644
--- a/app/controllers/users_controller.rb
+++ b/app/controllers/users_controller.rb
@@ -1936,10 +1936,9 @@ class UsersController < ApplicationController
format.ics do
bookmark_query = Bookmark.with_reminders.where(user_id: user.id)
- if params[:after].present?
- after_date = params[:after] == "now" ? Time.current : params[:after].to_datetime
- bookmark_query = bookmark_query.where("reminder_at >= ?", after_date)
- end
+ after_param = params[:after].presence || 3.months.ago.iso8601
+ after_date = after_param == "now" ? Time.current : after_param.to_datetime
+ bookmark_query = bookmark_query.where("reminder_at >= ?", after_date)
@bookmark_reminders =
bookmark_query
diff --git a/plugins/discourse-calendar/app/controllers/discourse_post_event/events_controller.rb b/plugins/discourse-calendar/app/controllers/discourse_post_event/events_controller.rb
index 509967442e4..80d60f36d3a 100644
--- a/plugins/discourse-calendar/app/controllers/discourse_post_event/events_controller.rb
+++ b/plugins/discourse-calendar/app/controllers/discourse_post_event/events_controller.rb
@@ -6,8 +6,15 @@ module DiscoursePostEvent
skip_before_action :check_xhr, only: [:index], if: :ics_request?
def index
+ search_params = filtered_events_params.to_h
+
+ if ics_request?
+ search_params["after"] ||= 3.months.ago.iso8601
+ search_params["order"] ||= "asc"
+ end
+
@events =
- DiscoursePostEvent::EventFinder.search(current_user, filtered_events_params).includes(
+ DiscoursePostEvent::EventFinder.search(current_user, search_params).includes(
:event_dates,
post: {
topic: %i[tags category],
diff --git a/plugins/discourse-calendar/app/views/discourse_post_event/events/index.ics.erb b/plugins/discourse-calendar/app/views/discourse_post_event/events/index.ics.erb
index e0597f1fabd..dc609bf2b30 100644
--- a/plugins/discourse-calendar/app/views/discourse_post_event/events/index.ics.erb
+++ b/plugins/discourse-calendar/app/views/discourse_post_event/events/index.ics.erb
@@ -2,6 +2,7 @@ BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//Discourse//<%= Discourse.current_hostname %>//<%= Discourse.full_version %>//EN
<%- @events.each do |event| -%>
+<%- next if event.starts_at.nil? -%>
BEGIN:VEVENT
UID:calendar_event_#<%= event.id %>@<%= Discourse.current_hostname %>
DTSTAMP:<%= Time.now.utc.strftime("%Y%m%dT%H%M%SZ") %>
diff --git a/plugins/discourse-calendar/spec/requests/events_controller_spec.rb b/plugins/discourse-calendar/spec/requests/events_controller_spec.rb
index 5aeab2f1d29..566b9335a5a 100644
--- a/plugins/discourse-calendar/spec/requests/events_controller_spec.rb
+++ b/plugins/discourse-calendar/spec/requests/events_controller_spec.rb
@@ -119,6 +119,27 @@ module DiscoursePostEvent
expect(body).to include("END:VEVENT")
end
+ it "excludes events older than 3 months from ics feed by default" do
+ recent_event = Fabricate(:event, original_starts_at: 1.day.from_now, name: "Future Event")
+ old_event = Fabricate(:event, original_starts_at: 4.months.ago, name: "Old Event")
+
+ get "/discourse-post-event/events.ics"
+
+ expect(response.status).to eq(200)
+ body = response.body
+ expect(body).to include("SUMMARY:Future Event")
+ expect(body).not_to include("SUMMARY:Old Event")
+ end
+
+ it "skips events with nil starts_at in ics feed" do
+ event = Fabricate(:event, original_starts_at: 1.day.from_now, name: "Valid Event")
+
+ get "/discourse-post-event/events.ics"
+
+ expect(response.status).to eq(200)
+ expect(response.body).to include("SUMMARY:Valid Event")
+ end
+
context "when include_interested is requested for an attending user" do
fab!(:target_user, :user)
fab!(:going_event) do
diff --git a/spec/requests/users_controller_spec.rb b/spec/requests/users_controller_spec.rb
index 6c17452c37b..55c14661729 100644
--- a/spec/requests/users_controller_spec.rb
+++ b/spec/requests/users_controller_spec.rb
@@ -7368,6 +7368,21 @@ RSpec.describe UsersController do
ICS
end
+ it "excludes bookmark reminders older than 3 months from .ics feed" do
+ bookmark1.update_columns(name: nil, reminder_at: 4.months.ago)
+ bookmark2.update!(name: "Recent reminder", reminder_at: 1.day.from_now)
+ bookmark3.update_columns(name: nil, reminder_at: 2.months.ago)
+
+ sign_in(user1)
+ get "/u/#{user1.username}/bookmarks.ics"
+ expect(response.status).to eq(200)
+
+ body = response.body
+ expect(body).not_to include("bookmark_reminder_##{bookmark1.id}")
+ expect(body).to include("bookmark_reminder_##{bookmark2.id}")
+ expect(body).to include("bookmark_reminder_##{bookmark3.id}")
+ end
+
it "does not show another user's bookmarks" do
sign_in(Fabricate(:user))
get "/u/#{bookmark3.user.username}/bookmarks.json"