A platform for community discussion. Free, open, simple.
FIX: Do not reset Going attendees on date/time change (#43797)
Martin Brennan6e70bfbcb923
diff --git a/plugins/discourse-events/app/models/discourse_events/events/event.rb b/plugins/discourse-events/app/models/discourse_events/events/event.rbindex 7c3d45d275f..ecf375dd0ad 100644--- a/plugins/discourse-events/app/models/discourse_events/events/event.rb+++ b/plugins/discourse-events/app/models/discourse_events/events/event.rb@@ -141,20 +141,19 @@ module DiscourseEventsenddef create_or_update_event_date- set_next_date if dates_changed?- end-- def set_next_date- return if closed+ return unless dates_changed?+ return set_next_recurrent_event_date if advancing_recurrence?+ return unless sync_event_date- next_date_result = calculate_next_date+ reset_non_going_invitees+ notify_if_new_event+ publish_update!+ end- return event_dates.update_all(finished_at: Time.current) if next_date_result.nil?+ def set_next_recurrent_event_date+ return unless sync_event_date- starts_at, ends_at = next_date_result- finish_previous_event_dates(starts_at) if dates_changed?- upsert_event_date(starts_at, ends_at)- reset_invitee_notifications+ reset_invitees_for_next_occurrencenotify_if_new_eventpublish_update!end@@ -710,6 +709,26 @@ module DiscourseEventssaved_change_to_original_starts_at || saved_change_to_original_ends_atend+ def advancing_recurrence?+ recurrence.present? && original_starts_at <= Time.current+ end++ def sync_event_date+ return false if closed++ next_date_result = calculate_next_date++ if next_date_result.nil?+ event_dates.update_all(finished_at: Time.current)+ return false+ end++ starts_at, ends_at = next_date_result+ finish_previous_event_dates(starts_at) if dates_changed?+ upsert_event_date(starts_at, ends_at)+ true+ end+def recurrence_expired?recurrence_until.present? && recurrence_until < Time.currentend@@ -747,7 +766,13 @@ module DiscourseEventsendend- def reset_invitee_notifications+ def reset_non_going_invitees+ invitees+ .where.not(status: Invitee.statuses[:going])+ .update_all(status: nil, notified: false, recurring: false)+ end++ def reset_invitees_for_next_occurrenceinvitees.where("status != :going OR recurring = FALSE",going: Invitee.statuses[:going],@@ -755,12 +780,10 @@ module DiscourseEventsenddef notify_if_new_event- is_generating_future_recurrence = recurrence.present? && original_starts_at <= Time.current+ return if advancing_recurrence?- unless is_generating_future_recurrence- notify_invitees!- notify_missing_invitees!- end+ notify_invitees!+ notify_missing_invitees!enddef calculate_next_recurring_datediff --git a/plugins/discourse-events/jobs/scheduled/monitor_event_dates.rb b/plugins/discourse-events/jobs/scheduled/monitor_event_dates.rbindex 0aef2206706..d78ceaf859f 100644--- a/plugins/discourse-events/jobs/scheduled/monitor_event_dates.rb+++ b/plugins/discourse-events/jobs/scheduled/monitor_event_dates.rb@@ -45,7 +45,7 @@ module Jobsreturn if !event_date.ended?event_date.update!(finished_at: Time.current)- # The occurrence goes along with the event: `set_next_date` below moves+ # The occurrence goes along with the event: `set_next_recurrent_event_date` below moves# the event on to the next one, so it can no longer name the one that ended.DiscourseEvent.trigger(:discourse_post_event_event_ended, event_date.event, event_date)MessageBus.publish(@@ -55,7 +55,7 @@ module Jobs)return if event_date.event.recurrence.blank?- event_date.event.set_next_date+ event_date.event.set_next_recurrent_event_dateevent_date.event.set_topic_bumpenddiff --git a/plugins/discourse-events/spec/integration/recurrence_spec.rb b/plugins/discourse-events/spec/integration/recurrence_spec.rbindex ae6d72c5c29..47d6c513b5f 100644--- a/plugins/discourse-events/spec/integration/recurrence_spec.rb+++ b/plugins/discourse-events/spec/integration/recurrence_spec.rb@@ -26,9 +26,9 @@ describe "discourse_post_event_recurrence" doDiscourseEvents::Events::Invitee.create_attendance!(going_user.id, post_event_1.id, :going)post_event_1.update!(original_starts_at: starts_at + 10.minutes)- post_event_1.set_next_date+ post_event_1.set_next_recurrent_event_datepost_event_1.update!(original_starts_at: starts_at - 10.minutes)- post_event_1.set_next_date+ post_event_1.set_next_recurrent_event_dateexpect(going_user@@ -42,7 +42,7 @@ describe "discourse_post_event_recurrence" dobefore { post_event_1.update!(recurrence: "every_month") }it "sets the next month at the same weekday" do- post_event_1.set_next_date+ post_event_1.set_next_recurrent_event_dateexpect(post_event_1.starts_at).to eq_time(Time.zone.parse("2020-10-08 19:00"))end@@ -52,7 +52,7 @@ describe "discourse_post_event_recurrence" dobefore { post_event_1.update!(recurrence: "every_week") }it "sets the next week at the same weekday" do- post_event_1.set_next_date+ post_event_1.set_next_recurrent_event_dateexpect(post_event_1.starts_at).to eq_time(Time.zone.parse("2020-09-17 19:00"))end@@ -62,7 +62,7 @@ describe "discourse_post_event_recurrence" dobefore { post_event_1.update!(recurrence: "every_two_weeks") }it "sets in two weeks at the same weekday" do- post_event_1.set_next_date+ post_event_1.set_next_recurrent_event_dateexpect(post_event_1.starts_at).to eq_time(Time.zone.parse("2020-09-24 19:00"))end@@ -72,7 +72,7 @@ describe "discourse_post_event_recurrence" dobefore { post_event_1.update!(recurrence: "every_four_weeks") }it "sets in four weeks at the same weekday" do- post_event_1.set_next_date+ post_event_1.set_next_recurrent_event_dateexpect(post_event_1.starts_at).to eq_time(Time.zone.parse("2020-10-08 19:00"))end@@ -82,7 +82,7 @@ describe "discourse_post_event_recurrence" dobefore { post_event_1.update!(recurrence: "every_day") }it "sets the next day" do- post_event_1.set_next_date+ post_event_1.set_next_recurrent_event_dateexpect(post_event_1.starts_at).to eq_time(Time.zone.parse("2020-09-11 19:00"))end@@ -99,7 +99,7 @@ describe "discourse_post_event_recurrence" doit "sets the next day" dofreeze_time(post_event_1.original_starts_at + 1.minute)- post_event_1.set_next_date+ post_event_1.set_next_recurrent_event_dateexpect(post_event_1.starts_at).to eq_time(Time.zone.parse("2020-09-14 19:00"))end@@ -112,7 +112,7 @@ describe "discourse_post_event_recurrence" doit "sets the next month at the same weekday" dofreeze_time(starts_at + 1.day)- post_event_1.set_next_date+ post_event_1.set_next_recurrent_event_dateexpect(post_event_1.starts_at).to eq_time(Time.zone.parse("2020-10-08 19:00"))end@@ -138,7 +138,7 @@ describe "discourse_post_event_recurrence" dorecurring: true,)- post_event_1.set_next_date+ post_event_1.set_next_recurrent_event_dateonce = post_event_1.invitees.find_by(user_id: going_once_user.id)recurring = post_event_1.invitees.find_by(user_id: going_recurring_user.id)@@ -154,7 +154,7 @@ describe "discourse_post_event_recurrence" doit "does not generate the next occurrence" doinitial_starts_at = post_event_1.starts_at- post_event_1.set_next_date+ post_event_1.set_next_recurrent_event_dateexpect(post_event_1.starts_at).to eq_time(initial_starts_at)enddiff --git a/plugins/discourse-events/spec/jobs/regular/discourse_post_event/send_reminder_spec.rb b/plugins/discourse-events/spec/jobs/regular/discourse_post_event/send_reminder_spec.rbindex 984c1c187f5..14a3706da6d 100644--- a/plugins/discourse-events/spec/jobs/regular/discourse_post_event/send_reminder_spec.rb+++ b/plugins/discourse-events/spec/jobs/regular/discourse_post_event/send_reminder_spec.rb@@ -515,17 +515,17 @@ describe Jobs::DiscoursePostEventSendReminder dodef advance_to_next_occurrencefreeze_time(recurring_event.original_starts_at + 1.day + 1.hour)recurring_event.event_dates.pending.update_all(finished_at: 1.hour.ago)- recurring_event.set_next_date+ recurring_event.set_next_recurrent_event_dateendit "sends reminders for the first occurrence" do- recurring_event.set_next_date+ recurring_event.set_next_recurrent_event_dateexpect { send_reminder }.to change { going_user.reload.unread_notifications }.by(1)endit "sends reminders for subsequent occurrences" do- recurring_event.set_next_date+ recurring_event.set_next_recurrent_event_datefirst_event_date = recurring_event.event_dates.pending.firstsend_reminder@@ -542,7 +542,7 @@ describe Jobs::DiscoursePostEventSendReminder doit "handles timezone-specific events" dorecurring_event.update!(timezone: "America/Los_Angeles")- recurring_event.set_next_date+ recurring_event.set_next_recurrent_event_dateexpect { send_reminder }.to change { going_user.reload.unread_notifications }.by(1)@@ -612,7 +612,7 @@ describe Jobs::DiscoursePostEventSendReminder doendit "prevents duplicate reminders for same occurrence" do- recurring_event.set_next_date+ recurring_event.set_next_recurrent_event_dateexpect { send_reminder }.to change { going_user.reload.unread_notifications }.by(1)expect { send_reminder }.not_to change { going_user.reload.unread_notifications }@@ -655,7 +655,7 @@ describe Jobs::DiscoursePostEventSendReminder dofreeze_time(Time.find_zone("America/New_York").parse("2025-11-05 10:50"))- dst_event.set_next_date+ dst_event.set_next_recurrent_event_datemonitor_job = Jobs::DiscourseCalendar::MonitorEventDates.newmonitor_job.execute({})diff --git a/plugins/discourse-events/spec/jobs/scheduled/monitor_event_dates_spec.rb b/plugins/discourse-events/spec/jobs/scheduled/monitor_event_dates_spec.rbindex fee08c08892..cb389f2809d 100644--- a/plugins/discourse-events/spec/jobs/scheduled/monitor_event_dates_spec.rb+++ b/plugins/discourse-events/spec/jobs/scheduled/monitor_event_dates_spec.rb@@ -174,6 +174,33 @@ describe Jobs::DiscourseCalendar::MonitorEventDates do)end+ it "clears one-occurrence invitees when a recurring event advances" do+ going_once_user = Fabricate(:user)+ going_recurring_user = Fabricate(:user)+ invitee_klass = DiscourseEvents::Events::Invitee++ past_event.update!(recurrence: "every_week")+ invitee_klass.create_attendance!(going_once_user.id, past_event.id, :going)+ invitee_klass.create_attendance!(+ going_recurring_user.id,+ past_event.id,+ :going,+ recurring: true,+ )++ freeze_time 8.days.after+ job.execute({})++ going_once_invitee = past_event.invitees.find_by(user_id: going_once_user.id)+ going_recurring_invitee = past_event.invitees.find_by(user_id: going_recurring_user.id)++ expect(going_once_invitee).to have_attributes(status: nil, notified: false, recurring: false)+ expect(going_recurring_invitee).to have_attributes(+ status: invitee_klass.statuses[:going],+ recurring: true,+ )+ end+it "does not process closed events" dopast_event.update!(recurrence: "every_week", closed: true)initial_event_date = past_event.event_dates.firstdiff --git a/plugins/discourse-events/spec/models/discourse_events/events/event_spec.rb b/plugins/discourse-events/spec/models/discourse_events/events/event_spec.rbindex 8eb6e764b3c..5e238724a33 100644--- a/plugins/discourse-events/spec/models/discourse_events/events/event_spec.rb+++ b/plugins/discourse-events/spec/models/discourse_events/events/event_spec.rb@@ -554,6 +554,48 @@ describe DiscourseEvents::Events::Event doexpect(post_event.starts_at).to eq_time(alt_starts_at)expect(post_event.ends_at).to eq_time(alt_ends_at)end++ it "preserves going invitees and resets other responses when the date changes" do+ going_user = Fabricate(:user)+ interested_user = Fabricate(:user)+ invitee_klass = DiscourseEvents::Events::Invitee++ post_event.create_invitees(+ [+ {+ user_id: going_user.id,+ status: invitee_klass.statuses[:going],+ notified: true,+ recurring: false,+ },+ {+ user_id: interested_user.id,+ status: invitee_klass.statuses[:interested],+ notified: true,+ recurring: false,+ },+ ],+ )++ post_event.update_with_params!(+ original_starts_at: alt_starts_at,+ original_ends_at: alt_ends_at,+ )++ going_invitee = post_event.invitees.find_by(user_id: going_user.id)+ interested_invitee = post_event.invitees.find_by(user_id: interested_user.id)++ expect(going_invitee).to have_attributes(+ status: invitee_klass.statuses[:going],+ notified: true,+ recurring: false,+ )+ expect(interested_invitee).to have_attributes(+ status: nil,+ notified: true,+ recurring: false,+ )+ endendcontext "when the associated post is not the OP" dodiff --git a/plugins/discourse-events/spec/serializers/discourse_events/events/event_serializer_spec.rb b/plugins/discourse-events/spec/serializers/discourse_events/events/event_serializer_spec.rbindex 9ba27634b8e..25b17dad25b 100644--- a/plugins/discourse-events/spec/serializers/discourse_events/events/event_serializer_spec.rb+++ b/plugins/discourse-events/spec/serializers/discourse_events/events/event_serializer_spec.rb@@ -326,7 +326,7 @@ describe DiscourseEvents::Events::EventSerializer do)end- before { recurring_event.set_next_date }+ before { recurring_event.set_next_recurrent_event_date }it "serializes starts_at using the next occurrence" dojson =diff --git a/plugins/discourse-events/spec/system/post_event_spec.rb b/plugins/discourse-events/spec/system/post_event_spec.rbindex 477994dfd01..c7111be39d7 100644--- a/plugins/discourse-events/spec/system/post_event_spec.rb+++ b/plugins/discourse-events/spec/system/post_event_spec.rb@@ -417,7 +417,7 @@ describe "Post event" dopost = PostCreator.create!(admin, title:, raw:)event = DiscourseEvents::Events::Event.find_by(post:)- event.set_next_date+ event.set_next_recurrent_event_datesign_in(viewer)