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
Implement previewable scheduled meeting mailer based on common layout
Oliver Güntherc5c52d795982
diff --git a/app/helpers/mail_layout_helper.rb b/app/helpers/mail_layout_helper.rbnew file mode 100644index 00000000000..766621c13a0--- /dev/null+++ b/app/helpers/mail_layout_helper.rb@@ -0,0 +1,71 @@+#-- 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 docs/COPYRIGHT.rdoc for more details.+#++++module MailLayoutHelper+ def placeholder_table_styles(options = {})+ default_options = {+ style: 'table-layout:fixed;border-collapse:separate;border-spacing:0;font-family:Helvetica;' <<+ (options[:style].present? ? options.delete(:style) : ''),+ cellspacing: "0",+ cellpadding: "0"+ }++ default_options.merge(options).map { |k, v| "#{k}=#{v}" }.join(' ')+ end++ def placeholder_text_styles(**overwrites)+ {+ color: '#878787',+ 'line-height': '24px',+ 'font-size': '14px',+ 'white-space': 'normal',+ overflow: 'hidden',+ 'max-width': '100%',+ width: '100%'+ }.merge(overwrites)+ .map { |k, v| "#{k}: #{v}" }+ .join('; ')+ end++ def action_button(&block)+ render(+ partial: 'mailer/mailer_button',+ locals: { block: }+ )+ end++ def placeholder_cell(number, vertical:)+ style = if vertical+ "max-width:#{number}; min-width:#{number}; width:#{number}"+ else+ "line-height:#{number}; max-width:0; min-width:0; height:#{number}; width:0; font-size:#{number}"+ end++ content_tag('td', ' '.html_safe, style:)+ end+enddiff --git a/app/helpers/mail_notification_helper.rb b/app/helpers/mail_notification_helper.rbindex a81c2c0ae77..1b7f1cdd299 100644--- a/app/helpers/mail_notification_helper.rb+++ b/app/helpers/mail_notification_helper.rb@@ -57,39 +57,4 @@ module MailNotificationHelpercolor_id = selected_color(status)Color.find(color_id).color_styles.map { |k, v| "#{k}:#{v};" }.join(' ') if color_idend-- def placeholder_table_styles(options = {})- default_options = {- style: 'table-layout:fixed;border-collapse:separate;border-spacing:0;font-family:Helvetica;' <<- (options[:style].present? ? options.delete(:style) : ''),- cellspacing: "0",- cellpadding: "0"- }-- default_options.merge(options).map { |k, v| "#{k}=#{v}" }.join(' ')- end-- def placeholder_text_styles(**overwrites)- {- color: '#878787',- 'line-height': '24px',- 'font-size': '14px',- 'white-space': 'normal',- overflow: 'hidden',- 'max-width': '100%',- width: '100%'- }.merge(overwrites)- .map { |k, v| "#{k}: #{v}" }- .join('; ')- end-- def placeholder_cell(number, vertical:)- style = if vertical- "max-width:#{number}; min-width:#{number}; width:#{number}"- else- "line-height:#{number}; max-width:0; min-width:0; height:#{number}; width:0; font-size:#{number}"- end-- content_tag('td', ' '.html_safe, style:)- endenddiff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rbindex 0af90c29a2c..f1d703f40c8 100644--- a/app/mailers/application_mailer.rb+++ b/app/mailers/application_mailer.rb@@ -31,7 +31,8 @@ class ApplicationMailer < ActionMailer::Basehelper :application, # for format_text:work_packages, # for css classes- :custom_fields # for show_value+ :custom_fields, # for show_value+ :mail_layout # for layoutinginclude OpenProject::LocaleHelperdiff --git a/app/views/announcement_mailer/announce.html.erb b/app/views/announcement_mailer/announce.html.erbindex 20c8be32ddd..0a814918708 100644--- a/app/views/announcement_mailer/announce.html.erb+++ b/app/views/announcement_mailer/announce.html.erb@@ -1,5 +1,5 @@<%= render layout: 'mailer/spacer_table' do %>- <%= render partial: 'mailer/notification_mailer_header',+ <%= render partial: 'mailer/mailer_header',locals: {summary: header_summary,salutation: salutation,diff --git a/app/views/digest_mailer/work_packages.html.erb b/app/views/digest_mailer/work_packages.html.erbindex 4318594c6b2..e78a53f9e5c 100644--- a/app/views/digest_mailer/work_packages.html.erb+++ b/app/views/digest_mailer/work_packages.html.erb@@ -8,7 +8,7 @@<tr><%= placeholder_cell('12px', vertical: true) %><td>- <%= render partial: 'mailer/notification_mailer_header',+ <%= render partial: 'mailer/mailer_header',locals: {summary: "#{I18n.t(:'mail.digests.you_have')} #{digest_summary_text(@notification_ids.length, @mentioned_count)}",button_href: notifications_center_url,diff --git a/app/views/mailer/_mailer_button.html.erb b/app/views/mailer/_mailer_button.html.erbnew file mode 100644index 00000000000..42db2321b68--- /dev/null+++ b/app/views/mailer/_mailer_button.html.erb@@ -0,0 +1,22 @@+<table <%= placeholder_table_styles(style: 'font-size:14px;') %>>+ <tr>+ <td>+ <table <%= placeholder_table_styles(width: '100%', style: 'width:100%;') %>>+ <tr>+ <td>+ </td>+ </tr>+ </table>+ </td>+ <%= placeholder_cell('10px', vertical: true) %>+ <td>+ <table <%= placeholder_table_styles %>>+ <tr>+ <td style="padding: 8px 12px; border: 1px solid #878787; border-radius: 16px; overflow: hidden; text-overflow: ellipsis;white-space: nowrap;">+ <%= capture(&block) %>+ </td>+ </tr>+ </table>+ </td>+ </tr>+</table>diff --git a/app/views/mailer/_notification_mailer_header.html.erb b/app/views/mailer/_mailer_header.html.erbsimilarity index 96%rename from app/views/mailer/_notification_mailer_header.html.erbrename to app/views/mailer/_mailer_header.html.erbindex 210cceb658a..9182f57074f 100644--- a/app/views/mailer/_notification_mailer_header.html.erb+++ b/app/views/mailer/_mailer_header.html.erb@@ -15,6 +15,7 @@<tr><%= placeholder_cell('8px', vertical: false) %></tr>+ <% if local_assigns[:summary] %><tr><td><span style="font-size:16px; color: #1A67A3; font-weight: 600;">@@ -22,6 +23,7 @@</span></td></tr>+ <% end %><tr><%= placeholder_cell('24px', vertical: false) %></tr>diff --git a/app/views/sharing_mailer/shared_work_package.html.erb b/app/views/sharing_mailer/shared_work_package.html.erbindex a33aadd93e2..f48c31c372d 100644--- a/app/views/sharing_mailer/shared_work_package.html.erb+++ b/app/views/sharing_mailer/shared_work_package.html.erb@@ -16,7 +16,7 @@end%>- <%= render partial: 'mailer/notification_mailer_header',+ <%= render partial: 'mailer/mailer_header',locals: {summary:,button_href: shared_work_package_path(@work_package.id),diff --git a/app/views/work_package_mailer/mentioned.html.erb b/app/views/work_package_mailer/mentioned.html.erbindex 986b6596dc1..8b51803494c 100644--- a/app/views/work_package_mailer/mentioned.html.erb+++ b/app/views/work_package_mailer/mentioned.html.erb@@ -8,7 +8,7 @@<tr><%= placeholder_cell('12px', vertical: true) %><td>- <%= render partial: 'mailer/notification_mailer_header',+ <%= render partial: 'mailer/mailer_header',locals: {summary: I18n.t(:'mail.work_packages.mentioned_by', user: @journal.user),button_href: notifications_path(@work_package.id),diff --git a/modules/meeting/app/mailers/meeting_mailer.rb b/modules/meeting/app/mailers/meeting_mailer.rbindex 209441dafac..de6c39387a5 100644--- a/modules/meeting/app/mailers/meeting_mailer.rb+++ b/modules/meeting/app/mailers/meeting_mailer.rb@@ -26,8 +26,6 @@# See COPYRIGHT and LICENSE files for more details.#++--class MeetingMailer < UserMailerdef invited(meeting, user, author)@author = author@@ -36,20 +34,24 @@ class MeetingMailer < UserMaileropen_project_headers 'Project' => @meeting.project.identifier,'Meeting-Id' => @meeting.id- User.execute_as(user) do- timezone = Time.zone || Time.zone_default+ with_attached_ics(meeting, user) do+ subject = "[#{@meeting.project.name}] #{@meeting.title}"+ mail(to: user.mail, subject:)+ end+ end- @formatted_timezone = format_timezone_offset timezone, @meeting.start_time+ def rescheduled(meeting, user, actor, changes:)+ @actor = actor+ @meeting = meeting+ @changes = changes- ::Meetings::ICalService- .new(user:, meeting: @meeting)- .call- .on_success do |call|- attachments['meeting.ics'] = call.result+ open_project_headers 'Project' => @meeting.project.identifier,+ 'Meeting-Id' => @meeting.id- subject = "[#{@meeting.project.name}] #{@meeting.title}"- mail(to: user.mail, subject:)- end+ with_attached_ics(meeting, user) do+ subject = "[#{@meeting.project.name}] "+ subject << I18n.t('meeting.email.rescheduled.header', title: @meeting.title, actor: @actor)+ mail(to: user.mail, subject:)endend@@ -59,23 +61,35 @@ class MeetingMailer < UserMailerset_headers @meeting- User.execute_as(user) do+ with_attached_ics(meeting, user) dotimezone = Time.zone || Time.zone_default-@formatted_timezone = format_timezone_offset timezone, @meeting.start_time+ subject = "[#{@meeting.project.name}] #{@meeting.title}"+ mail(to: user.mail, subject:)+ end+ end++ private++ def with_attached_ics(meeting, user)+ User.execute_as(user) do- ::Meetings::ICalService+ call = ::Meetings::ICalService.new(user:, meeting: @meeting).call- .on_success do |call|++ call.on_success doattachments['meeting.ics'] = call.result- mail(to: user.mail, subject: "[#{@meeting.project.name}] #{I18n.t(:label_meeting)}: #{@meeting.title}")++ yield+ end++ call.on_failure do+ Rails.logger.error { "Failed to create ICS attachment for meeting #{meeting.id}: #{call.message}" }endendend- private-def set_headers(meeting)open_project_headers 'Project' => meeting.project.identifier, 'Meeting-Id' => meeting.idheaders['Content-Type'] = 'text/calendar; charset=utf-8; method="PUBLISH"; name="meeting.ics"'diff --git a/modules/meeting/app/models/meeting.rb b/modules/meeting/app/models/meeting.rbindex 2c1c555bd09..809e67c6156 100644--- a/modules/meeting/app/models/meeting.rb+++ b/modules/meeting/app/models/meeting.rb@@ -62,11 +62,11 @@ class Meeting < ApplicationRecordacts_as_watchable permission: :view_meetingsacts_as_searchable columns: [- "#{table_name}.title",- "#{MeetingContent.table_name}.text",- "#{MeetingAgendaItem.table_name}.title",- "#{MeetingAgendaItem.table_name}.notes"- ],+ "#{table_name}.title",+ "#{MeetingContent.table_name}.text",+ "#{MeetingAgendaItem.table_name}.title",+ "#{MeetingAgendaItem.table_name}.notes"+ ],include: %i[contents project agenda_items],references: %i[meeting_contents agenda_items],date_column: "#{table_name}.created_at"@@ -87,11 +87,11 @@ class Meeting < ApplicationRecordendvalidate :validate_date_and_time- before_save :update_start_time!+ before_save :update_start_time!before_save :add_new_participants_as_watcher-after_initialize :set_initial_values+ after_update :send_rescheduling_mail, if: -> { saved_change_to_start_time? || saved_change_to_duration? }enum state: {open: 0, # 0 -> default, leave values for future states between open and closed@@ -155,7 +155,7 @@ class Meeting < ApplicationRecordchangeable_participants = participants.select(&:invited).collect(&:user)changeable_participants = changeable_participants + participants.select(&:attended).collect(&:user)changeable_participants = changeable_participants + \- User.allowed_members(:view_meetings, project)+ User.allowed_members(:view_meetings, project)changeable_participants.compact@@ -220,6 +220,7 @@ class Meeting < ApplicationRecordendalias :original_participants_attributes= :participants_attributes=+def participants_attributes=(attrs)attrs.each do |participant|participant['_destroy'] = true if !(participant['attended'] || participant['invited'])@@ -317,6 +318,20 @@ class Meeting < ApplicationRecordenddef send_participant_added_mail(participant)- MeetingMailer.invited(self, participant.user, User.current).deliver_later+ if persisted?+ MeetingMailer.invited(self, participant.user, User.current).deliver_later+ end+ end++ def send_rescheduling_mail+ MeetingNotificationService+ .new(self)+ .call :rescheduled,+ changes: {+ old_start: saved_change_to_start_time? ? saved_change_to_start_time.first : start_time,+ new_start: start_time,+ old_duration: saved_change_to_duration? ? saved_change_to_duration.first : duration,+ new_duration: duration+ }endenddiff --git a/modules/meeting/app/services/meeting_notification_service.rb b/modules/meeting/app/services/meeting_notification_service.rbindex be7b2ff82fc..80cc0c76dd3 100644--- a/modules/meeting/app/services/meeting_notification_service.rb+++ b/modules/meeting/app/services/meeting_notification_service.rb@@ -5,21 +5,21 @@ class MeetingNotificationService@meeting = meetingend- def call(action, include_author: false)- recipients_with_errors = send_notifications!(action, include_author:)+ def call(action, include_author: false, **)+ recipients_with_errors = send_notifications!(action, include_author:, **)ServiceResult.new(success: recipients_with_errors.empty?, errors: recipients_with_errors)endprivate- def send_notifications!(action, include_author:)+ def send_notifications!(action, include_author:, **params)author_mail = meeting.author.mailrecipients_with_errors = []meeting.participants.includes(:user).find_each do |recipient|next if recipient.mail == author_mail && !include_author- MeetingMailer.send(action, meeting, recipient.user, User.current).deliver_later+ MeetingMailer.send(action, meeting, recipient.user, User.current, **params).deliver_laterrescue StandardError => eRails.logger.error do"Failed to deliver #{action} notification to #{recipient.mail}: #{e.message}"diff --git a/modules/meeting/app/views/meeting_mailer/rescheduled.html.erb b/modules/meeting/app/views/meeting_mailer/rescheduled.html.erbnew file mode 100644index 00000000000..f48b44261cd--- /dev/null+++ b/modules/meeting/app/views/meeting_mailer/rescheduled.html.erb@@ -0,0 +1,98 @@+<%#-- 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.++++#%>++<%= render layout: 'mailer/spacer_table' do %>+ <%= render partial: 'mailer/mailer_header',+ locals: {+ salutation: "#{@meeting.project.name}: #{@meeting.title}",+ summary: I18n.t('meeting.email.rescheduled.summary', title: @meeting.title, actor: @actor),+ } %>++ <%= render layout: 'mailer/border_table' do %>+ <tr>+ <%= placeholder_cell('24px', vertical: true) %>+ <td>+ <table>+ <tr>+ <td style="<%= placeholder_text_styles(width: '30%') %>">+ <%= t('meeting.email.rescheduled.old_date_time') %>+ </td>+ <td style="<%= placeholder_text_styles('font-weight': 'bold') %>">+ <s>+ <%= format_time_as_date @changes[:old_start] %> <%= format_time @changes[:old_start], false %>+ -<%= format_time (@changes[:old_start] + @changes[:old_duration].hours), false %> <%= Time.zone %>+ </s>+ </td>+ </tr>+ <tr>+ <td style="<%= placeholder_text_styles(width: '30%') %>">+ <%= t('meeting.email.rescheduled.new_date_time') %>+ </td>+ <td style="<%= placeholder_text_styles %>">+ <%= format_time_as_date @changes[:new_start] %> <%= format_time @changes[:new_start], false %>+ -<%= format_time (@changes[:new_start] + @changes[:new_duration].hours), false %> <%= Time.zone %>+ </td>+ </tr>+ <% if @meeting.location.present? %>+ <tr>+ <td style="<%= placeholder_text_styles(width: '30%') %>">+ <%= Meeting.human_attribute_name(:location) %>+ </td>+ <td style="<%= placeholder_text_styles %>">+ <%= auto_link @meeting.location %>+ </td>+ </tr>+ <% end %>+ <tr>+ <td style="<%= placeholder_text_styles(width: '30%') %>">+ <%= Meeting.human_attribute_name(:project) %>+ </td>+ <td style="<%= placeholder_text_styles %>">+ <%= link_to @meeting.project.name, project_url(@meeting.project) %>+ </td>+ </tr>+ </table>+ </td>+ </tr>+ <% end %>++ <table>+ <tr>+ <%= placeholder_cell('20px', vertical: false) %>+ </tr>+ </table>++ <%= action_button do %>+ <a href="<%= meeting_url(@meeting) %>"+ target="_blank"+ style="color: #333333; text-decoration: none; font-size: 14px;white-space: nowrap;">+ <%= I18n.t(:'meeting.email.open_meeting_link') %>+ </a>+ <% end %>+<% end %>diff --git a/modules/meeting/app/views/meeting_mailer/rescheduled.text.erb b/modules/meeting/app/views/meeting_mailer/rescheduled.text.erbnew file mode 100644index 00000000000..d601b7196b1--- /dev/null+++ b/modules/meeting/app/views/meeting_mailer/rescheduled.text.erb@@ -0,0 +1,41 @@+<%#-- 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.++++#%>++<%= @meeting.project.name %>: <%= @meeting.title %> (<%= meeting_url(@meeting) %>)+<%= @meeting.author %>++<%= t('meeting.email.rescheduled.body',+ actor: @actor,+ title: @meeting.title) %>++<%= t('meeting.email.rescheduled.old_date_time') %>:+<%= format_time_as_date @changes[:old_start] %> <%= format_time @changes[:old_start], false %> - <%= format_time (@changes[:old_start] + @changes[:old_duration]), false %> <%= Time.zone %>++<%= t('meeting.email.rescheduled.new_date_time') %>:+<%= format_time_as_date @changes[:new_start] %> <%= format_time @changes[:new_start], false %> - <%= format_time (@changes[:new_start] + @changes[:new_duration]), false %> <%= Time.zone %>diff --git a/modules/meeting/config/locales/en.yml b/modules/meeting/config/locales/en.ymlindex 4aeffbdd485..2cf2a08e40f 100644--- a/modules/meeting/config/locales/en.yml+++ b/modules/meeting/config/locales/en.yml@@ -110,6 +110,14 @@ en:meeting:+ email:+ open_meeting_link: "Open meeting"+ rescheduled:+ header: "Meeting %{title} rescheduled by %{actor}"+ summary: "Meeting has been rescheduled by %{actor}"+ body: "The meeting %{title} has been rescheduled by %{actor}."+ old_date_time: "Old date/time"+ new_date_time: "New date/time"label_mail_all_participants: "Send email to all participants"types:classic: 'Classic'diff --git a/spec/mailers/previews/meeting_mailer_preview.rb b/spec/mailers/previews/meeting_mailer_preview.rbnew file mode 100644index 00000000000..0b5687177ea--- /dev/null+++ b/spec/mailers/previews/meeting_mailer_preview.rb@@ -0,0 +1,46 @@+#-- 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.+#++++class MeetingMailerPreview < ActionMailer::Preview+ # Preview emails at http://localhost:3000/rails/mailers/meeting_mailer++ def rescheduled+ actor = FactoryBot.build_stubbed(:user, lastname: 'Actor')+ user = FactoryBot.build_stubbed(:user)+ meeting = FactoryBot.build_stubbed(:meeting, start_time: 1.day.from_now, duration: 1.0)++ changes = {+ old_start: meeting.start_time,+ old_duration: meeting.duration,+ new_start: 5.days.from_now,+ new_duration: 2.5+ }++ MeetingMailer.rescheduled(meeting, user, actor, changes:)+ end+end