-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17377 from opf/feature/recurring-meeting-ics
ICS download option for recurring meeting
- Loading branch information
Showing
13 changed files
with
411 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#-- copyright | ||
# OpenProject is an open source project management software. | ||
# Copyright (C) the OpenProject GmbH | ||
# | ||
# This program is free software; you can redistribute it and/or | ||
# modify it under the terms of the GNU General Public License version 3. | ||
# | ||
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: | ||
# Copyright (C) 2006-2013 Jean-Philippe Lang | ||
# Copyright (C) 2010-2013 the ChiliProject Team | ||
# | ||
# This program is free software; you can redistribute it and/or | ||
# modify it under the terms of the GNU General Public License | ||
# as published by the Free Software Foundation; either version 2 | ||
# of the License, or (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program; if not, write to the Free Software | ||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
# | ||
# See COPYRIGHT and LICENSE files for more details. | ||
#++ | ||
require "icalendar" | ||
require "icalendar/tzinfo" | ||
|
||
module Meetings | ||
module ICalHelpers | ||
def ical_event(start_time, &) | ||
calendar = build_icalendar(start_time) | ||
calendar.event(&) | ||
calendar.publish | ||
calendar.to_ical | ||
end | ||
|
||
def build_icalendar(start_time) | ||
::Icalendar::Calendar.new.tap do |calendar| | ||
calendar.prodid = "-//OpenProject GmbH//#{OpenProject::VERSION}//Meeting//EN" | ||
ical_timezone = timezone.tzinfo.ical_timezone start_time | ||
calendar.add_timezone ical_timezone | ||
end | ||
end | ||
|
||
def add_attendees(event, meeting) | ||
meeting.participants.includes(:user).find_each do |participant| | ||
user = participant.user | ||
next unless user | ||
|
||
address = Icalendar::Values::CalAddress.new( | ||
"mailto:#{user.mail}", | ||
{ | ||
"CN" => user.name, | ||
"EMAIL" => user.mail, | ||
"PARTSTAT" => "NEEDS-ACTION", | ||
"RSVP" => "TRUE", | ||
"CUTYPE" => "INDIVIDUAL", | ||
"ROLE" => "REQ-PARTICIPANT" | ||
} | ||
) | ||
|
||
event.append_attendee(address) | ||
end | ||
end | ||
|
||
def ical_uid(suffix) | ||
"#{Setting.app_title}-#{Setting.host_name}-#{suffix}".dasherize | ||
end | ||
|
||
def ical_datetime(time, timezone_id) | ||
Icalendar::Values::DateTime.new time.in_time_zone(timezone_id), "tzid" => timezone_id | ||
end | ||
|
||
def ical_organizer(meeting) | ||
Icalendar::Values::CalAddress.new("mailto:#{meeting.author.mail}", cn: meeting.author.name) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
modules/meeting/app/services/recurring_meetings/ical_service.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
#-- copyright | ||
# OpenProject is an open source project management software. | ||
# Copyright (C) the OpenProject GmbH | ||
# | ||
# This program is free software; you can redistribute it and/or | ||
# modify it under the terms of the GNU General Public License version 3. | ||
# | ||
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: | ||
# Copyright (C) 2006-2013 Jean-Philippe Lang | ||
# Copyright (C) 2010-2013 the ChiliProject Team | ||
# | ||
# This program is free software; you can redistribute it and/or | ||
# modify it under the terms of the GNU General Public License | ||
# as published by the Free Software Foundation; either version 2 | ||
# of the License, or (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program; if not, write to the Free Software | ||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
# | ||
# See COPYRIGHT and LICENSE files for more details. | ||
#++ | ||
require "icalendar" | ||
require "icalendar/tzinfo" | ||
|
||
module RecurringMeetings | ||
class ICalService | ||
include ::Meetings::ICalHelpers | ||
attr_reader :user, | ||
:series, | ||
:schedule, | ||
:timezone, | ||
:calendar, | ||
:url_helpers | ||
|
||
delegate :template, to: :series | ||
|
||
def initialize(series:, user:) | ||
@user = user | ||
@series = series | ||
@url_helpers = OpenProject::StaticRouting::StaticUrlHelpers.new | ||
end | ||
|
||
def call # rubocop:disable Metrics/AbcSize | ||
User.execute_as(user) do | ||
@timezone = Time.zone || Time.zone_default | ||
@calendar = build_icalendar(series.start_time) | ||
@schedule = series.schedule | ||
ServiceResult.success(result: generate_ical) | ||
end | ||
rescue StandardError => e | ||
Rails.logger.error("Failed to generate ICS for meeting series #{@series.id}: #{e.message}") | ||
ServiceResult.failure(message: e.message) | ||
end | ||
|
||
private | ||
|
||
def tzinfo | ||
timezone.tzinfo | ||
end | ||
|
||
def tzid | ||
tzinfo.canonical_identifier | ||
end | ||
|
||
def generate_ical | ||
series_event | ||
occurrences_events | ||
|
||
calendar.publish | ||
calendar.to_ical | ||
end | ||
|
||
def series_event # rubocop:disable Metrics/AbcSize | ||
calendar.event do |e| | ||
base_series_attributes(e) | ||
|
||
e.dtstart = ical_datetime template.start_time, tzid | ||
e.dtend = ical_datetime template.end_time, tzid | ||
e.url = url_helpers.project_recurring_meeting_url(series.project, series) | ||
e.location = template.location.presence | ||
|
||
add_attendees(e, template) | ||
e.exdate = cancelled_schedules | ||
end | ||
end | ||
|
||
def occurrences_events | ||
upcoming_instantiated_schedules.find_each do |schedule| | ||
occurrence_event(schedule.start_time, schedule.meeting) | ||
end | ||
end | ||
|
||
def occurrence_event(schedule_start_time, meeting) # rubocop:disable Metrics/AbcSize | ||
calendar.event do |e| | ||
base_series_attributes(e) | ||
|
||
e.recurrence_id = ical_datetime schedule_start_time, tzid | ||
e.dtstart = ical_datetime meeting.start_time, tzid | ||
e.dtend = ical_datetime meeting.end_time, tzid | ||
e.url = url_helpers.project_meeting_url(meeting.project, meeting) | ||
e.location = meeting.location.presence | ||
|
||
add_attendees(e, meeting) | ||
end | ||
end | ||
|
||
def upcoming_instantiated_schedules | ||
series | ||
.scheduled_meetings | ||
.not_cancelled | ||
.instantiated | ||
.includes(:meeting) | ||
end | ||
|
||
def base_series_attributes(event) # rubocop:disable Metrics/AbcSize | ||
event.uid = ical_uid("meeting-series-#{series.id}") | ||
event.rrule = schedule.rrules.first.to_ical # We currently only have one recurrence rule | ||
event.summary = "[#{series.project.name}] #{series.title}" | ||
event.description = "[#{series.project.name}] #{I18n.t(:label_meeting_series)}: #{series.title}" | ||
event.organizer = ical_organizer(series) | ||
end | ||
|
||
def cancelled_schedules | ||
series | ||
.scheduled_meetings | ||
.cancelled | ||
.pluck(:start_time) | ||
.map { |time| Icalendar::Values::DateTime.new time.in_time_zone(tzid), "tzid" => tzid } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.