OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Allow downloading ICS file of meeting by extracting meeting_mailer
Oliver Günther8cd76995af71
diff --git a/modules/meeting/app/components/meetings/header_component.rb b/modules/meeting/app/components/meetings/header_component.rbindex 8804eb93054..ab40533134e 100644--- a/modules/meeting/app/components/meetings/header_component.rb+++ b/modules/meeting/app/components/meetings/header_component.rb@@ -90,6 +90,7 @@ module Meetingsrender(Primer::Alpha::ActionMenu.new) do |menu|menu.with_show_button(icon: "kebab-horizontal", 'aria-label': t("label_meeting_actions"), test_selector: 'op-meetings-header-action-trigger')edit_action_item(menu) if edit_enabled?+ download_ics_item(menu)delete_action_item(menu) if delete_enabled?endend@@ -104,6 +105,13 @@ module Meetingsendend+ def download_ics_item(menu)+ menu.with_item(label: t(:label_icalendar_download),+ href: download_ics_meeting_path(@meeting)) do |item|+ item.with_leading_visual_icon(icon: :download)+ end+ end+def delete_action_item(menu)menu.with_item(label: t("label_meeting_delete"),scheme: :danger,diff --git a/modules/meeting/app/controllers/meetings_controller.rb b/modules/meeting/app/controllers/meetings_controller.rbindex c4ff5fdc0ed..699d49af440 100644--- a/modules/meeting/app/controllers/meetings_controller.rb+++ b/modules/meeting/app/controllers/meetings_controller.rb@@ -200,6 +200,16 @@ class MeetingsController < ApplicationControllerrespond_with_turbo_streamsend+ def download_ics+ ::Meetings::ICalService+ .new(user: current_user, meeting: @meeting)+ .call+ .on_failure { |call| render_500(message: call.message) }+ .on_success do |call|+ send_data call.result, filename: filename_for_content_disposition("#{@meeting.title}.ics")+ end+ end+privatedef load_querydiff --git a/modules/meeting/app/mailers/meeting_mailer.rb b/modules/meeting/app/mailers/meeting_mailer.rbindex e6ea617c425..fedc207be80 100644--- a/modules/meeting/app/mailers/meeting_mailer.rb+++ b/modules/meeting/app/mailers/meeting_mailer.rb@@ -26,8 +26,7 @@# See COPYRIGHT and LICENSE files for more details.#++-require 'icalendar'-require 'icalendar/tzinfo'+class MeetingMailer < UserMailerdef content_for_review(content, content_type, user)@@ -55,8 +54,13 @@ class MeetingMailer < UserMailer@formatted_timezone = format_timezone_offset timezone, @meeting.start_time- attachments['meeting.ics'] = generate_ical timezone, @meeting, @content_type- mail(to: user.mail, subject: ical_subject(@meeting, @content_type))+ ::Meetings::ICalService+ .new(user:, meeting: @meeting)+ .call+ .on_success do |call|+ attachments['meeting.ics'] = call.result+ mail(to: user.mail, subject: "[#{@meeting.project.name}] #{I18n.t(:label_meeting)}: #{@meeting.title}")+ endendend@@ -72,40 +76,4 @@ class MeetingMailer < UserMaileroffset = ::ActiveSupport::TimeZone.seconds_to_utc_offset time.utc_offest_for_timezone(timezone), true"(GMT#{offset}) #{timezone.name}"end-- def ical_subject(meeting, content_type)- "[#{meeting.project.name}] #{I18n.t(:"label_#{content_type}")}: #{meeting.title}"- end-- # rubocop:disable Metrics/AbcSize- def generate_ical(timezone, meeting, content_type)- calendar = ::Icalendar::Calendar.new-- tzinfo = timezone.tzinfo- calendar.add_timezone tzinfo.ical_timezone(meeting.start_time)- tzid = tzinfo.canonical_identifier-- calendar.event do |e|- e.dtstart = ical_datetime meeting.start_time, tzid- e.dtend = ical_datetime meeting.end_time, tzid- e.url = meeting_url(meeting)- e.summary = "[#{meeting.project.name}] #{meeting.title}"- e.description = ical_subject(meeting, content_type)- e.uid = "#{meeting.id}@#{meeting.project.identifier}"- e.organizer = ical_organizer meeting- e.location = meeting.location.presence- end-- calendar.publish- calendar.to_ical- end- # rubocop:enable Metrics/AbcSize-- 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)- endenddiff --git a/modules/meeting/app/services/meetings/ical_service.rb b/modules/meeting/app/services/meetings/ical_service.rbnew file mode 100644index 00000000000..d7cece9aa91--- /dev/null+++ b/modules/meeting/app/services/meetings/ical_service.rb@@ -0,0 +1,92 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2023 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+ class ICalService+ attr_reader :user, :meeting, :timezone, :url_helpers++ def initialize(meeting:, user:)+ @user = user+ @meeting = meeting+ @url_helpers = OpenProject::StaticRouting::StaticUrlHelpers.new+ end++ def call+ User.execute_as(user) do+ @timezone = Time.zone || Time.zone_default+ ServiceResult.success(result: generate_ical)+ end+ rescue StandardError => e+ Rails.logger.error("Failed to generate ICS for meeting #{@meeting.id}: #{e.message}")+ ServiceResult.failure(message: e.message)+ end++ private++ # rubocop:disable Metrics/AbcSize+ def generate_ical+ ical_event do |e|+ tzinfo = timezone.tzinfo+ tzid = tzinfo.canonical_identifier++ e.dtstart = ical_datetime meeting.start_time, tzid+ e.dtend = ical_datetime meeting.end_time, tzid+ e.url = url_helpers.meeting_url(meeting)+ e.summary = "[#{meeting.project.name}] #{meeting.title}"+ e.description = ical_subject(meeting)+ e.uid = "#{meeting.id}@#{meeting.project.identifier}"+ e.organizer = ical_organizer meeting+ e.location = meeting.location.presence+ end+ end+ # rubocop:enable Metrics/AbcSize++ def ical_event(&)+ calendar = ::Icalendar::Calendar.new++ calendar.event(&)++ calendar.publish+ calendar.to_ical+ end++ def ical_subject(meeting)+ "[#{meeting.project.name}] #{I18n.t(:label_meeting)}: #{meeting.title}"+ 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+enddiff --git a/modules/meeting/config/locales/en.yml b/modules/meeting/config/locales/en.ymlindex dd72e526b92..72c56d895f5 100644--- a/modules/meeting/config/locales/en.yml+++ b/modules/meeting/config/locales/en.yml@@ -101,6 +101,7 @@ en:label_author: "Creator"label_notify: "Send for review"label_icalendar: "Send iCalendar"+ label_icalendar_download: "Download iCalendar event"label_version: "Version"label_time_zone: "Time zone"label_start_date: "Start date"diff --git a/modules/meeting/config/routes.rb b/modules/meeting/config/routes.rbindex 11bb685ea99..48ed9a81e34 100644--- a/modules/meeting/config/routes.rb+++ b/modules/meeting/config/routes.rb@@ -47,6 +47,7 @@ OpenProject::Application.routes.draw doresources :meetings domember doget :cancel_edit+ get :download_icsput :update_titleput :update_detailsput :update_participantsdiff --git a/modules/meeting/lib/open_project/meeting/engine.rb b/modules/meeting/lib/open_project/meeting/engine.rbindex 1e7d4bab3d3..1c7cbe17434 100644--- a/modules/meeting/lib/open_project/meeting/engine.rb+++ b/modules/meeting/lib/open_project/meeting/engine.rb@@ -39,7 +39,7 @@ module OpenProject::Meetingbundled: true doproject_module :meetings dopermission :view_meetings,- { meetings: %i[index show],+ { meetings: %i[index show download_ics],meeting_agendas: %i[history show diff],meeting_minutes: %i[history show diff] },permissible_on: :projectdiff --git a/modules/meeting/spec/controllers/meeting_contents_controller_spec.rb b/modules/meeting/spec/controllers/meeting_contents_controller_spec.rbindex 664c35a09aa..9c9b17e996f 100644--- a/modules/meeting/spec/controllers/meeting_contents_controller_spec.rb+++ b/modules/meeting/spec/controllers/meeting_contents_controller_spec.rb@@ -114,7 +114,7 @@ RSpec.describe MeetingContentsController doendit 'produces a flash message containing the mail addresses raising the error' do- put 'notify', params: { meeting_id: meeting.id }+ put 'notify', params: { meeting_id: meeting.id }meeting.participants.each do |participant|expect(flash[:error]).to include(participant.name)enddiff --git a/modules/meeting/spec/features/structured_meetings/structured_meeting_crud_spec.rb b/modules/meeting/spec/features/structured_meetings/structured_meeting_crud_spec.rbindex 05f0f5a748b..a483d33706c 100644--- a/modules/meeting/spec/features/structured_meetings/structured_meeting_crud_spec.rb+++ b/modules/meeting/spec/features/structured_meetings/structured_meeting_crud_spec.rb@@ -174,6 +174,26 @@ RSpec.describe 'Structured meetings CRUD',expect(page).to have_current_path project_meetings_path(project)end+ context 'exporting as ICS' do+ before do+ @download_list = DownloadList.new+ end++ after do+ DownloadList.clear+ end++ subject { @download_list.refresh_from(page).latest_download.to_s }++ it 'can export the meeting as ICS' do+ click_button('op-meetings-header-action-trigger')++ click_link I18n.t(:label_icalendar_download)++ expect(subject).to end_with ".ics"+ end+ end+context 'with a work package reference to another' dolet!(:meeting) { create(:structured_meeting, project:, author: current_user) }let!(:other_project) { create(:project) }diff --git a/modules/meeting/spec/mailers/meeting_mailer_spec.rb b/modules/meeting/spec/mailers/meeting_mailer_spec.rbindex 2095c188e61..f2877756dbe 100644--- a/modules/meeting/spec/mailers/meeting_mailer_spec.rb+++ b/modules/meeting/spec/mailers/meeting_mailer_spec.rb@@ -186,7 +186,7 @@ RSpec.describe MeetingMailer doexpect(entry.dtstart.utc).to eq meeting.start_timeexpect(entry.dtend.utc).to eq meeting.start_time + 1.hourexpect(entry.summary).to eq '[My project] Important meeting'- expect(entry.description).to eq "[My project] Agenda: Important meeting"+ expect(entry.description).to eq "[My project] Meeting: Important meeting"expect(entry.location).to eq(meeting.location.presence)enddiff --git a/modules/meeting/spec/services/meetings/ical_service_spec.rb b/modules/meeting/spec/services/meetings/ical_service_spec.rbnew file mode 100644index 00000000000..2831f148f0d--- /dev/null+++ b/modules/meeting/spec/services/meetings/ical_service_spec.rb@@ -0,0 +1,80 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2023 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 'spec_helper'++RSpec.describe Meetings::ICalService, type: :model do+ let(:user) { build_stubbed(:user, firstname: 'Bob', lastname: 'Barker') }+ let(:project) { build_stubbed(:project, name: 'My Project') }++ let(:meeting) do+ build_stubbed(:meeting,+ author: user,+ project:,+ title: 'Important meeting',+ location: 'https://example.com/meet/important-meeting',+ start_time: Time.zone.parse("2021-01-19T10:00:00Z"),+ duration: 1.0)+ end++ let(:service) { described_class.new(user:, meeting:) }+ let(:result) { service.call.result }++ subject(:entry) { Icalendar::Event.parse(result).first }++ describe '#call' do+ it 'returns a success' do+ expect(service.call).to be_success+ end++ context 'when exception is raised' do+ subject { service.call }++ before do+ allow(service).to receive(:generate_ical).and_raise StandardError.new("Oh noes")+ end++ it 'returns a failure' do+ expect(subject).to be_failure+ expect(subject.message).to eq("Oh noes")+ end+ end++ it 'renders the ICS file', :aggregate_failures do+ expect(result).to be_a String++ expect(entry.dtstart.utc).to eq meeting.start_time+ expect(entry.dtend.utc).to eq meeting.start_time + 1.hour+ expect(entry.summary).to eq '[My Project] Important meeting'+ expect(entry.description).to eq "[My Project] Meeting: Important meeting"+ expect(entry.location).to eq(meeting.location.presence)+ expect(entry.dtstart).to eq Time.zone.parse("2021-01-19T10:00:00Z").in_time_zone("Europe/Berlin")+ expect(entry.dtend).to eq (Time.zone.parse("2021-01-19T10:00:00Z") + 1.hour).in_time_zone("Europe/Berlin")+ end+ end+end