calendar_subscriptions.rb 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. require 'icalendar/tzinfo'
  3. class CalendarSubscriptions
  4. def initialize(user)
  5. @user = user
  6. @preferences = {}
  7. @time_zone = Setting.get('timezone_default_sanitized')
  8. default_preferences = Setting.where(area: 'Defaults::CalendarSubscriptions')
  9. default_preferences.each do |calendar_subscription|
  10. next if calendar_subscription.name !~ %r{\Adefaults_calendar_subscriptions_(.*)\z}
  11. object_name = $1 # rubocop:disable Lint/OutOfRangeRegexpRef
  12. @preferences[ object_name ] = calendar_subscription.state_current[:value]
  13. end
  14. return if @user.preferences[:calendar_subscriptions].blank?
  15. @preferences = @preferences.merge(@user.preferences[:calendar_subscriptions])
  16. end
  17. def all
  18. events_data = []
  19. @preferences.each_key do |object_name|
  20. result = generic_call(object_name)
  21. events_data += result
  22. end
  23. to_ical(events_data)
  24. end
  25. def generic(object_name, method_name = 'all')
  26. events_data = generic_call(object_name, method_name)
  27. to_ical(events_data)
  28. end
  29. def generic_call(object_name, method_name = 'all')
  30. method_name ||= 'all'
  31. events_data = []
  32. if @preferences[ object_name ].present?
  33. sub_class_name = object_name.to_s.capitalize
  34. object = "CalendarSubscriptions::#{sub_class_name}".constantize
  35. instance = object.new(@user, @preferences[ object_name ], @time_zone)
  36. method = instance.method(method_name)
  37. events_data += method.call
  38. end
  39. events_data
  40. end
  41. def to_ical(events_data)
  42. cal = Icalendar::Calendar.new
  43. tz = ActiveSupport::TimeZone.find_tzinfo(@time_zone)
  44. # https://github.com/zammad/zammad/issues/3989
  45. # https://datatracker.ietf.org/doc/html/rfc5545#section-3.2.19
  46. if events_data.first.present?
  47. timezone = tz.ical_timezone(DateTime.parse(events_data.first[:dtstart].to_s))
  48. cal.add_timezone(timezone)
  49. end
  50. events_data.each do |event_data|
  51. cal.event do |e|
  52. dtstart = DateTime.parse(event_data[:dtstart].to_s)
  53. dtend = DateTime.parse(event_data[:dtend].to_s)
  54. # by design all new/open ticket events are scheduled at midnight:
  55. # skip adding TZ offset
  56. if !event_data[:type].match('new_open')
  57. dtstart = tz.utc_to_local(dtstart)
  58. dtend = tz.utc_to_local(dtend)
  59. end
  60. e.dtstart = Icalendar::Values::DateTime.new(dtstart, 'tzid' => @time_zone)
  61. e.dtend = Icalendar::Values::DateTime.new(dtend, 'tzid' => @time_zone)
  62. if event_data[:alarm]
  63. e.alarm do |a|
  64. a.action = 'DISPLAY'
  65. a.summary = event_data[:alarm][:summary]
  66. a.trigger = event_data[:alarm][:trigger]
  67. end
  68. end
  69. e.summary = event_data[:summary]
  70. e.description = event_data[:description]
  71. e.ip_class = 'PRIVATE'
  72. end
  73. end
  74. cal.to_ical
  75. end
  76. end