Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEATURE: Show all events in past (add deleted_at in discourse_calendar_post_event_dates) + add show_past_event in settings #199

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/controllers/discourse_post_event/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ def ics_request?
end

def filtered_events_params
params.permit(:post_id)
# Http params permited
params.permit(:post_id, :after, :before, :deleted)
end
end
end
4 changes: 3 additions & 1 deletion app/models/discourse_post_event/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ def create_or_update_event_date
starts_at_changed = saved_change_to_original_starts_at
ends_at_changed = saved_change_to_original_ends_at

# No one of date changed => Must keep the event
return if !starts_at_changed && !ends_at_changed

event_dates.update_all(finished_at: Time.current)
# One of date changed => Must delete the event_date and create new event_date on this event
event_dates.update_all(deleted_at: Time.current)
set_next_date
end

Expand Down
7 changes: 4 additions & 3 deletions app/models/discourse_post_event/event_date.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ class EventDate < ActiveRecord::Base
self.table_name = 'discourse_calendar_post_event_dates'
belongs_to :event

scope :pending, -> { where(finished_at: nil) }
scope :expired, -> { where('ends_at IS NOT NULL AND ends_at < ?', Time.now) }
scope :not_expired, -> { where('ends_at IS NULL OR ends_at > ?', Time.now) }
scope :pending, -> { where('deleted_at IS NULL AND finished_at IS NULL') }
scope :expired, -> { where('deleted_at IS NULL AND ends_at IS NOT NULL AND ends_at < ?', Time.now) }
scope :not_expired, -> { where('deleted_at IS NULL AND (ends_at IS NULL OR ends_at > ?)', Time.now) }

after_commit :upsert_topic_custom_field, on: %i[create]
def upsert_topic_custom_field
Expand Down Expand Up @@ -60,6 +60,7 @@ def ended?
# event_id :integer
# starts_at :datetime
# ends_at :datetime
# deleted_at :datetime
# reminder_counter :integer default(0)
# event_will_start_sent_at :datetime
# event_started_sent_at :datetime
Expand Down
3 changes: 3 additions & 0 deletions config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ discourse_calendar:
calendar_enabled:
default: false
client: true
show_past_events:
default: false
client: true
holiday_calendar_topic_id:
default: ""
client: true
Expand Down
12 changes: 12 additions & 0 deletions db/migrate/20211211094100_add_date_delete_to_post_event_dates.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

class AddDateDeleteToPostEventDates < ActiveRecord::Migration[6.0]
def up
add_column :discourse_calendar_post_event_dates, :deleted_at, :datetime
add_index :discourse_calendar_post_event_dates, :deleted_at
end

def down
remove_column :discourse_calendar_post_event_dates, :deleted_at
end
end
16 changes: 16 additions & 0 deletions db/migrate/20211211224500_update_date_delete_from_finish_at.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

class UpdateDateDeleteFromFinishAt < ActiveRecord::Migration[6.0]
def up
execute "update discourse_calendar_post_event_dates SET deleted_at = now() WHERE finished_at is not NULL;
UPDATE discourse_calendar_post_event_dates SET deleted_at = NULL
FROM (
SELECT DISTINCT ON (event_id)
id
FROM discourse_calendar_post_event_dates
where finished_at is not null
ORDER BY event_id, updated_at DESC
) AS sq
WHERE discourse_calendar_post_event_dates.id=sq.id;"
end
end
24 changes: 22 additions & 2 deletions lib/discourse_post_event/event_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def self.search(user, params = {})
topics = listable_topics(guardian)
pms = private_messages(user)

# Commun data SQL query
events = DiscoursePostEvent::Event
.select("discourse_post_event_events.*, dcped.starts_at")
.joins(post: :topic)
Expand All @@ -15,20 +16,39 @@ def self.search(user, params = {})
.joins("LEFT JOIN discourse_calendar_post_event_dates dcped ON dcped.event_id = discourse_post_event_events.id")
.order("dcped.starts_at ASC")

# Filter events after this date
if params[:after]
events = events.where("dcped.starts_at >= ? OR dcped.ends_at >= ? ", params[:after], params[:after])
end

# Filter events before this date
if params[:before]
events = events.where("dcped.ends_at <= ? OR (dcped.ends_at IS NULL AND dcped.starts_at <= ?)", params[:before], params[:before])
end

# All events deleted
events = events.where("dcped.deleted_at IS NULL")

if params[:expired]
# The second part below makes the query ignore events that have non-expired event-dates
# Filter the expired events
events = events
.where("dcped.finished_at IS NOT NULL AND (dcped.ends_at IS NOT NULL AND dcped.ends_at < ?)", Time.now)
.where("discourse_post_event_events.id NOT IN (SELECT DISTINCT event_id FROM discourse_calendar_post_event_dates WHERE event_id = discourse_post_event_events.id AND finished_at IS NULL)")
else
events = events.where("dcped.finished_at IS NULL AND (dcped.ends_at IS NULL OR dcped.ends_at > ?)", Time.now)
# Only future events
if not SiteSetting.show_past_events
events = events.where("(dcped.ends_at IS NOT NULL AND dcped.ends_at > ?) OR (dcped.ends_at IS NULL AND dcped.starts_at > ?)", Time.now, Time.now)
end
end

#
if params[:post_id]
events = events.where(id: Array(params[:post_id]))
end

# Filter events from sategory
if params[:category_id].present?
# And sub categories
if params[:include_subcategories].present?
events = events.where(topics: { category_id: Category.subcategory_ids(params[:category_id].to_i) })
else
Expand Down