Skip to content

Commit

Permalink
FIX: sort calendar_details for better holidays support (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZogStriP committed Nov 19, 2020
1 parent 5c424a1 commit 44521c6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 38 deletions.
9 changes: 5 additions & 4 deletions jobs/scheduled/create_holiday_events.rb
Expand Up @@ -11,6 +11,7 @@ def execute(args)
require "holidays" unless defined?(Holidays)

regions_and_user_ids = Hash.new { |h, k| h[k] = [] }

UserCustomField
.where(name: ::DiscourseCalendar::REGION_CUSTOM_FIELD)
.pluck(:user_id, :value)
Expand All @@ -31,13 +32,13 @@ def execute(args)
.map { |user_id, timezone| [user_id, (TZInfo::Timezone.get(timezone) rescue nil)] }
.to_h

regions_and_user_ids.keys.each do |region|
holidays = Holidays
regions_and_user_ids.each do |region, user_ids|
Holidays
.between(Date.today, 6.months.from_now, [region], :observed)
.filter { |holiday| (1..5) === holiday[:date].wday }
.each do |holiday|

regions_and_user_ids[region].each do |user_id|
user_ids.each do |user_id|
next unless usernames[user_id]

date = if tz = user_ids_and_timezones[user_id]
Expand Down Expand Up @@ -65,7 +66,7 @@ def hour_adjustment
return if SiteSetting.all_day_event_start_time.empty? || SiteSetting.all_day_event_end_time.empty?

@holiday_hour ||= begin
split = SiteSetting.all_day_event_start_time.split(':')
split = SiteSetting.all_day_event_start_time.split(":")
{ hour: split.first, min: split.second }
end
end
Expand Down
57 changes: 23 additions & 34 deletions plugin.rb
Expand Up @@ -392,48 +392,37 @@ def valid_event
end

add_to_serializer(:post, :calendar_details) do
result = []
grouped_events = {}
grouped = {}
standalones = []

CalendarEvent.where(topic_id: object.topic_id).each do |event|
CalendarEvent.where(topic_id: object.topic_id).order(:start_date, :end_date).find_each do |event|
if event.post_id
# Events with no `post_id` are holidays

result <<
{
type: :standalone,
post_number: event.post_number,
message: event.description,
from: event.start_date,
to: event.end_date,
username: event.username,
recurring: event.recurrence,
post_url: Post.url('-', event.topic_id, event.post_number)
}
standalones << {
type: :standalone,
post_number: event.post_number,
message: event.description,
from: event.start_date,
to: event.end_date,
username: event.username,
recurring: event.recurrence,
post_url: Post.url("-", event.topic_id, event.post_number)
}
else
identifier =
"#{event.region.split('_').first}-#{
event.start_date.strftime('%W')
}-#{(event.end_date || event.start_date).strftime('%W')}"
identifier = "#{event.region.split("_").first}-#{event.start_date.strftime("%W")}"

if grouped_events[identifier]
grouped_events[identifier][:to] = event.start_date
else
grouped_events[identifier] = {
type: :grouped,
name: event.description,
from: event.start_date,
usernames: [],
identifier: identifier
}
end
grouped[identifier] ||= {
type: :grouped,
name: event.description,
from: event.start_date,
usernames: []
}

grouped_events[identifier][:usernames] << event.username
grouped_events[identifier][:usernames].uniq!
grouped[identifier][:usernames] << event.username
grouped[identifier][:usernames].uniq!
end
end

result.concat(grouped_events.values)
standalones + grouped.values
end

add_to_serializer(:post, :include_calendar_details?) { object.is_first_post? }
Expand Down

0 comments on commit 44521c6

Please sign in to comment.