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
Feature/member custom notifications (#9173)
ulfertsd1f419385f65
diff --git a/app/mailers/member_mailer.rb b/app/mailers/member_mailer.rbindex f19f904fdb1..138d9db8fd4 100644--- a/app/mailers/member_mailer.rb+++ b/app/mailers/member_mailer.rb@@ -40,43 +40,51 @@# need to be mailed to individually.class MemberMailer < BaseMailer- def added_project(current_user, member)+ include OpenProject::StaticRouting::UrlHelpers+ include OpenProject::TextFormatting++ def added_project(current_user, member, message)alter_project(current_user,member,- in_member_locale(member) { I18n.t(:'mail_member_added_project.subject', project: member.project.name) })+ in_member_locale(member) { I18n.t(:'mail_member_added_project.subject', project: member.project.name) },+ message)end- def updated_project(current_user, member)+ def updated_project(current_user, member, message)alter_project(current_user,member,- in_member_locale(member) { I18n.t(:'mail_member_updated_project.subject', project: member.project.name) })+ in_member_locale(member) { I18n.t(:'mail_member_updated_project.subject', project: member.project.name) },+ message)end- def updated_global(current_user, member)+ def updated_global(current_user, member, message)send_mail(current_user,member,- in_member_locale(member) { I18n.t(:'mail_member_updated_global.subject') })+ in_member_locale(member) { I18n.t(:'mail_member_updated_global.subject') },+ message)endprivate- def alter_project(current_user, member, subject)+ def alter_project(current_user, member, subject, message)send_mail(current_user,member,- subject) do+ subject,+ message) doopen_project_headers Project: member.project.identifier@project = member.projectendend- def send_mail(current_user, member, subject)+ def send_mail(current_user, member, subject, message)in_member_locale(member) doUser.execute_as(current_user) domessage_id member, current_user@roles = member.roles@principal = member.principal+ @message = messageyield if block_given?diff --git a/app/services/base_services/base_callable.rb b/app/services/base_services/base_callable.rbindex 9c424fe114e..1d93c511a2a 100644--- a/app/services/base_services/base_callable.rb+++ b/app/services/base_services/base_callable.rb@@ -36,6 +36,8 @@ module BaseServicesinclude ::WithReversibleStatedef call(*params)+ self.params = params.first+run_callbacks(:call) doperform(*params)end@@ -43,6 +45,8 @@ module BaseServicesprotected+ attr_accessor :params+def perform(*)raise NotImplementedErrorenddiff --git a/app/services/base_services/write.rb b/app/services/base_services/write.rbindex 54b1d02e7f0..0408a212e43 100644--- a/app/services/base_services/write.rb+++ b/app/services/base_services/write.rb@@ -59,7 +59,7 @@ module BaseServicesmodel: instance(params),contract_class: contract_class,contract_options: contract_options)- .call(params)+ .call(set_attributes_params(params))enddef attributes_service_class@@ -77,5 +77,9 @@ module BaseServicesdef instance_classnamespace.singularize.constantizeend++ def set_attributes_params(params)+ params+ endendenddiff --git a/app/services/groups/concerns/membership_manipulation.rb b/app/services/groups/concerns/membership_manipulation.rbindex e7f037a9956..901e9968392 100644--- a/app/services/groups/concerns/membership_manipulation.rb+++ b/app/services/groups/concerns/membership_manipulation.rb@@ -35,7 +35,7 @@ module Groups::Concernswith_error_handled do::Group.transaction do- exec_query!(params, params.delete(:send_notifications) { true })+ exec_query!(params, params.fetch(:send_notifications, true), params[:message])endendend@@ -51,12 +51,12 @@ module Groups::Concernsmessage: I18n.t(:notice_internal_server_error, app_title: Setting.app_title))end- def exec_query!(params, send_notifications)+ def exec_query!(params, send_notifications, message)affected_member_ids = modify_members_and_roles(params)touch_updated(affected_member_ids)- send_notifications(affected_member_ids) if affected_member_ids.any? && send_notifications+ send_notifications(affected_member_ids, message) if affected_member_ids.any? && send_notificationsenddef modify_members_and_roles(_params)@@ -77,8 +77,8 @@ module Groups::Concerns.touch_allend- def send_notifications(member_ids)- Notifications::GroupMemberAlteredJob.perform_later(member_ids)+ def send_notifications(member_ids, message)+ Notifications::GroupMemberAlteredJob.perform_later(member_ids, message)endendenddiff --git a/app/services/members/create_service.rb b/app/services/members/create_service.rbindex cf02d219552..5fafbee59dd 100644--- a/app/services/members/create_service.rb+++ b/app/services/members/create_service.rb@@ -29,18 +29,36 @@#++class Members::CreateService < ::BaseServices::Create- def after_perform(service_call)+ around_call :post_process++ def post_process+ service_call = yield++ return unless service_call.success?+member = service_call.result- if member.principal.is_a?(Group)- Groups::AddUsersService- .new(member.principal, current_user: user, contract_class: EmptyContract)- .call(ids: member.principal.user_ids, send_notifications: false)- end+ add_group_memberships(member)+ send_notification(member)+ end++ protected+ def send_notification(member)OpenProject::Notifications.send(OpenProject::Events::MEMBER_CREATED,- member: member)+ member: member,+ message: params[:notification_message])+ end++ def add_group_memberships(member)+ return unless member.principal.is_a?(Group)++ Groups::AddUsersService+ .new(member.principal, current_user: user, contract_class: EmptyContract)+ .call(ids: member.principal.user_ids, send_notifications: false)+ end- service_call+ def set_attributes_params(params)+ super.except(:notification_message)endenddiff --git a/app/services/members/update_service.rb b/app/services/members/update_service.rbindex 3578c4e82b0..88742287b9b 100644--- a/app/services/members/update_service.rb+++ b/app/services/members/update_service.rb@@ -31,20 +31,41 @@class Members::UpdateService < ::BaseServices::Updateinclude Members::Concerns::CleanedUp- protected-- def after_perform(service_call)- super.tap do |call|- member = call.result-- if member.principal.is_a?(Group)- Groups::UpdateRolesService- .new(member.principal, current_user: user, contract_class: EmptyContract)- .call(member: member, send_notifications: true)- else- OpenProject::Notifications.send(OpenProject::Events::MEMBER_UPDATED,- member: member)- end+ around_call :post_process++ private++ def post_process+ service_call = yield++ return unless service_call.success?++ member = service_call.result++ if member.principal.is_a?(Group)+ update_group_roles(member)+ else+ send_notification(member)endend++ def send_notification(member)+ OpenProject::Notifications.send(OpenProject::Events::MEMBER_UPDATED,+ member: member,+ message: notification_message)+ end++ def update_group_roles(member)+ Groups::UpdateRolesService+ .new(member.principal, current_user: user, contract_class: EmptyContract)+ .call(member: member, send_notifications: true, message: notification_message)+ end++ def set_attributes_params(params)+ super.except(:notification_message)+ end++ def notification_message+ params[:notification_message]+ endenddiff --git a/app/views/member_mailer/added_project.html.erb b/app/views/member_mailer/added_project.html.erbindex 4f4813b14e7..319fad82169 100644--- a/app/views/member_mailer/added_project.html.erb+++ b/app/views/member_mailer/added_project.html.erb@@ -1,4 +1,10 @@-<%= I18n.t(:'mail_member_added_project.body.added_by', project: @project.name, user: User.current.name) %>+<%=+ raw I18n.t(:"mail_member_added_project.body.added_by.#{@message.present? ? 'with_message' : 'without_message'}",+ project: link_to_project(@project, only_path: false),+ user: link_to_user(User.current, only_path: false))+%>++<%= format_text(@message) %><%= I18n.t(:'mail_member_added_project.body.roles') %><ul>diff --git a/app/views/member_mailer/added_project.text.erb b/app/views/member_mailer/added_project.text.erbindex 03600bece86..88560419c03 100644--- a/app/views/member_mailer/added_project.text.erb+++ b/app/views/member_mailer/added_project.text.erb@@ -1,4 +1,10 @@-<%= I18n.t(:'mail_member_added_project.body.added_by', project: h(@project.name), user: h(User.current.name)) %>+<%=+ I18n.t(:"mail_member_added_project.body.added_by#{@message.present? ? 'with_message' : 'without_message'}",+ project: @project.name,+ user: User.current.name)+%>++<%= @message %><%= I18n.t(:'mail_member_added_project.body.roles') %><% @roles.each do |role| %>diff --git a/app/views/member_mailer/updated_global.html.erb b/app/views/member_mailer/updated_global.html.erbindex 887869e80e4..d5ed1c509c3 100644--- a/app/views/member_mailer/updated_global.html.erb+++ b/app/views/member_mailer/updated_global.html.erb@@ -1,4 +1,9 @@-<%= I18n.t(:'mail_member_updated_global.body.added_by', user: User.current.name) %>+<%=+ raw I18n.t(:"mail_member_updated_global.body.updated_by.#{@message.present? ? 'with_message' : 'without_message'}",+ user: link_to_user(User.current, only_path: false))+%>++<%= format_text(@message) %><%= I18n.t(:'mail_member_updated_global.body.roles') %><ul>diff --git a/app/views/member_mailer/updated_global.text.erb b/app/views/member_mailer/updated_global.text.erbindex 78f4c13a55f..6c3ec539e14 100644--- a/app/views/member_mailer/updated_global.text.erb+++ b/app/views/member_mailer/updated_global.text.erb@@ -1,4 +1,9 @@-<%= I18n.t(:'mail_member_updated_global.body.added_by', user: User.current.name) %>+<%=+ I18n.t(:"mail_member_updated_global.body.updated_by.#{@message.present? ? 'with_message' : 'without_message'}",+ user: User.current.name)+%>++<%= @message %><%= I18n.t(:'mail_member_updated_global.body.roles') %><% @roles.each do |role| %>diff --git a/app/views/member_mailer/updated_project.html.erb b/app/views/member_mailer/updated_project.html.erbindex 527fb2817b0..f659df510e0 100644--- a/app/views/member_mailer/updated_project.html.erb+++ b/app/views/member_mailer/updated_project.html.erb@@ -1,4 +1,10 @@-<%= I18n.t(:'mail_member_updated_project.body.added_by', project: @project.name, user: User.current.name) %>+<%=+ raw I18n.t(:"mail_member_updated_project.body.updated_by.#{@message.present? ? 'with_message' : 'without_message'}",+ project: link_to_project(@project, only_path: false),+ user: link_to_user(User.current, only_path: false))+%>++<%= format_text(@message) %><%= I18n.t(:'mail_member_updated_project.body.roles') %><ul>diff --git a/app/views/member_mailer/updated_project.text.erb b/app/views/member_mailer/updated_project.text.erbindex bc06fb2109c..d21f09ca973 100644--- a/app/views/member_mailer/updated_project.text.erb+++ b/app/views/member_mailer/updated_project.text.erb@@ -1,4 +1,8 @@-<%= I18n.t(:'mail_member_updated_project.body.added_by', project: @project.name, user: User.current.name) %>+<%=+ I18n.t(:"mail_member_updated_project.body.updated_by.#{@message.present? ? 'with_message' : 'without_message'}",+ project: @project.name,+ user: User.current.name)+%><%= I18n.t(:'mail_member_updated_project.body.roles') %><% @roles.each do |role| %>diff --git a/app/workers/mails/member_created_job.rb b/app/workers/mails/member_created_job.rbindex 0b739adb211..522adad7e7b 100644--- a/app/workers/mails/member_created_job.rb+++ b/app/workers/mails/member_created_job.rb@@ -31,11 +31,11 @@ class Mails::MemberCreatedJob < Mails::MemberJobalias_method :send_for_project_user, :send_added_project- def send_for_group_user(current_user, user_member, group_member)+ def send_for_group_user(current_user, user_member, group_member, message)if new_roles_added?(user_member, group_member)- send_updated_project(current_user, user_member)+ send_updated_project(current_user, user_member, message)elsif all_roles_added?(user_member, group_member)- send_added_project(current_user, user_member)+ send_added_project(current_user, user_member, message)endenddiff --git a/app/workers/mails/member_job.rb b/app/workers/mails/member_job.rbindex 35fee172322..a2b630f89dd 100644--- a/app/workers/mails/member_job.rb+++ b/app/workers/mails/member_job.rb@@ -30,49 +30,50 @@ class Mails::MemberJob < ApplicationJobqueue_with_priority :notificationdef perform(current_user:,- member:)+ member:,+ message: nil)if member.project.nil?- send_updated_global(current_user, member)+ send_updated_global(current_user, member, message)elsif member.principal.is_a?(Group)every_group_user_member(member) do |user_member|- send_for_group_user(current_user, user_member, member)+ send_for_group_user(current_user, user_member, member, message)endelsif member.principal.is_a?(User)- send_for_project_user(current_user, member)+ send_for_project_user(current_user, member, message)endendprivate- def send_for_group_user(_current_user, _member, _group)+ def send_for_group_user(_current_user, _member, _group, _message)raise NotImplementedError, "subclass responsibility"end- def send_for_project_user(_current_user, _member)+ def send_for_project_user(_current_user, _member, _message)raise NotImplementedError, "subclass responsibility"end- def send_updated_global(current_user, member)- return if sending_disabled?(:updated)+ def send_updated_global(current_user, member, member_message)+ return if sending_disabled?(:updated, member_message)MemberMailer- .updated_global(current_user, member)+ .updated_global(current_user, member, member_message).deliver_nowend- def send_added_project(current_user, member)- return if sending_disabled?(:added)+ def send_added_project(current_user, member, member_message)+ return if sending_disabled?(:added, member_message)MemberMailer- .added_project(current_user, member)+ .added_project(current_user, member, member_message).deliver_nowend- def send_updated_project(current_user, member)- return if sending_disabled?(:updated)+ def send_updated_project(current_user, member, member_message)+ return if sending_disabled?(:updated, member_message)MemberMailer- .updated_project(current_user, member)+ .updated_project(current_user, member, member_message).deliver_nowend@@ -84,7 +85,7 @@ class Mails::MemberJob < ApplicationJob.each(&block)end- def sending_disabled?(setting)- !Setting.notified_events.include?("membership_#{setting}")+ def sending_disabled?(setting, message)+ message.blank? && !Setting.notified_events.include?("membership_#{setting}")endenddiff --git a/app/workers/mails/member_updated_job.rb b/app/workers/mails/member_updated_job.rbindex 0904923eb6f..5cbee83cfb7 100644--- a/app/workers/mails/member_updated_job.rb+++ b/app/workers/mails/member_updated_job.rb@@ -31,7 +31,7 @@ class Mails::MemberUpdatedJob < Mails::MemberJobalias_method :send_for_project_user, :send_updated_project- def send_for_group_user(current_user, member, _group)- send_updated_project(current_user, member)+ def send_for_group_user(current_user, member, _group, message)+ send_updated_project(current_user, member, message)endenddiff --git a/app/workers/notifications/group_member_altered_job.rb b/app/workers/notifications/group_member_altered_job.rbindex 72aad5c0c74..3e366ea5f2c 100644--- a/app/workers/notifications/group_member_altered_job.rb+++ b/app/workers/notifications/group_member_altered_job.rb@@ -31,10 +31,11 @@class Notifications::GroupMemberAlteredJob < ApplicationJobqueue_with_priority :notification- def perform(members_ids)+ def perform(members_ids, message)each_member(members_ids) do |member|OpenProject::Notifications.send(event_type(member),- member: member)+ member: member,+ message: message)endenddiff --git a/config/initializers/subscribe_listeners.rb b/config/initializers/subscribe_listeners.rbindex 2263cbbb862..57c6e26c412 100644--- a/config/initializers/subscribe_listeners.rb+++ b/config/initializers/subscribe_listeners.rb@@ -55,11 +55,13 @@ endOpenProject::Notifications.subscribe(OpenProject::Events::MEMBER_CREATED) do |payload|Mails::MemberCreatedJob.perform_later(current_user: User.current,- member: payload[:member])+ member: payload[:member],+ message: payload[:message])endOpenProject::Notifications.subscribe(OpenProject::Events::MEMBER_UPDATED) do |payload|Mails::MemberUpdatedJob.perform_later(current_user: User.current,- member: payload[:member])+ member: payload[:member],+ message: payload[:message])enddiff --git a/config/locales/en.yml b/config/locales/en.ymlindex 7b3d8a6493e..ebc2c4bd150 100644--- a/config/locales/en.yml+++ b/config/locales/en.yml@@ -1893,19 +1893,25 @@ en:mail_member_added_project:subject: "%{project} - You have been added as a member"body:- added_by: "%{user} added you as a member to the project '%{project}'."+ added_by:+ without_message: "%{user} added you as a member to the project '%{project}'."+ with_message: "%{user} added you as a member to the project '%{project}' writing:"roles: "You have the following roles:"mail_member_updated_project:subject: "%{project} - Your roles have been updated"body:- added_by: "%{user} updated the roles you have in the project '%{project}'."+ updated_by:+ without_message: "%{user} updated the roles you have in the project '%{project}'."+ with_message: "%{user} updated the roles you have in the project '%{project}' writing:"roles: "You now have the following roles:"mail_member_updated_global:subject: "Your global permissions have been updated"body:- added_by: "%{user} updated the roles you have globally."+ updated_by:+ without_message: "%{user} updated the roles you have globally."+ with_message: "%{user} updated the roles you have globally writing:"roles: "You now have the following roles:"mail_user_activation_limit_reached:diff --git a/docs/api/apiv3/endpoints/members.apib b/docs/api/apiv3/endpoints/members.apibindex d57818d5ce5..7e44d8960e7 100644--- a/docs/api/apiv3/endpoints/members.apib+++ b/docs/api/apiv3/endpoints/members.apib@@ -2,7 +2,9 @@Users and groups can become members of a project. Such a membership will also have one or more roles assigned to it. By that, memberships control the permissions a user has within a project.-There are also memberships that do not govern the permissions within a certain project but rather govern global permissions. Currently, this is only relevant for the "Create project" permission. Those memberships do not have a project associated.+There are also memberships that do not govern the permissions within a certain project but rather govern global permissions. Among the permissions that can be granted like this are the permissions to "Create project" and "Manage users". Those memberships do not have a project associated.++When creating and updating memberships, a custom message can be sent to users of new and updated memberships. This message can be provided within the `_meta` group.## Linked Properties| Link | Description | Type | Constraints | Supported operations |@@ -19,6 +21,12 @@ There are also memberships that do not govern the permissions within a certain p| createdAt | Time of creation | DateTime | not null | READ || updatedAt | Time of latest update | DateTime | not null | READ |+## Meta parameters++| Meta property | Description | Type | Constraints | Supported operations |Condition |+| :------------------------: | --------------------------------------------------- | ---- | ----------- | -------------------- |----------|+| notificationMessage | The message included in the email(s) send to the users of new or updated memberships | Formattable | | READ/WRITE | |+## View membership [/api/v3/memberships/{id}]+ Model@@ -108,6 +116,9 @@ Creates a new membership applying the attributes provided in the body.You can use the form and schema to be retrieve the valid attribute values and by that be guided towards successful creation.+By providing a `notificationMessage` within the `_meta` block of the payload, the client can include a customized message to the user+of the newly created membership. In case of a group, the message will be sent to every user belonging to the group.++ Request Create memberships+ Body@@ -128,6 +139,12 @@ You can use the form and schema to be retrieve the valid attribute values and by"href": "/api/v3/roles/8"}]+ },+ "_meta": {+ "notificationMessage": {+ "raw" {+ "Welcome to our team, Alice."+ }}}@@ -186,6 +203,9 @@ You can use the form and schema to be retrieve the valid attribute values and byUpdates the given membership by applying the attributes provided in the body.+By providing a `notificationMessage` within the `_meta` block of the payload, the client can include a customized message to the user+of the updated membership. In case of a group, the message will be sent to every user belonging to the group.++ Parameters+ id (required, integer, `1`) ... Membership id@@ -203,6 +223,11 @@ Updates the given membership by applying the attributes provided in the body."href": "/api/v3/roles/8"}]+ },+ "_meta": {+ "notificationMessage": {+ "raw": "Alice, you can now also create work packages."+ }}}@@ -457,6 +482,15 @@ Returns a collection of memberships. The client can choose to filter the members"hasDefault": false,"writable": false},+ "notificationMessage": {+ "type": "Formattable",+ "name": "Message",+ "required": false,+ "hasDefault": false,+ "writable": true,+ "options": {},+ "location": "_meta"+ },"project": {"type": "Project","name": "Project",@@ -525,17 +559,25 @@ For more details and all possible responses see the general specification of [Fo+ Body{- "project": {- "href": "/api/v3/projects/1"- },- "principal": {- "href": "/api/v3/users/5"+ "_links": {+ "project": {+ "href": "/api/v3/projects/1"+ },+ "principal": {+ "href": "/api/v3/users/5"+ },+ "roles": [+ {+ "href": "/api/v3/roles/2"+ }+ ]},- "roles": [- {- "href": "/api/v3/roles/2"+ "_meta": {+ "notificationMessage": {+ "raw": "Welcome to our team, Alice."+}- ]+ }}+ Response 200 (application/hal+json)@@ -561,6 +603,11 @@ For more details and all possible responses see the general specification of [Fo"title": "Manager"}]+ },+ "_meta": {+ "notificationMessage": {+ "raw": "Welcome to our team, Alice."+ }}},"schema": {@@ -587,6 +634,15 @@ For more details and all possible responses see the general specification of [Fo"hasDefault": false,"writable": false},+ "notificationMessage": {+ "type": "Formattable",+ "name": "Message",+ "required": false,+ "hasDefault": false,+ "writable": true,+ "options": {},+ "location": "_meta"+ },"project": {"type": "Project","name": "Project",@@ -686,6 +742,9 @@ The returned payload part of the form will be contain a merge between the alreadFor more details and all possible responses see the general specification of [Forms](/api/forms).++ Parameters+ + id (required, integer, `1`) ... Membership id+## Membership update form [POST]+ Request Update membership form@@ -693,11 +752,18 @@ For more details and all possible responses see the general specification of [Fo+ Body{- "_links": [- {- "href": "/api/v3/roles/4"- }- ]+ "_links": {+ "roles": [+ {+ "href": "/api/v3/roles/4"+ }+ ]+ },+ "_meta": {+ "notificationMessage": {+ "raw": "Alice, you can now also create work packages."+ }+ }}+ Response 200 (application/hal+json)@@ -715,6 +781,11 @@ For more details and all possible responses see the general specification of [Fo"title": "A role"}]+ },+ "_meta": {+ "notificationMessage": {+ "raw": "Alice, you can now also create work packages."+ }}},"schema": {@@ -741,6 +812,15 @@ For more details and all possible responses see the general specification of [Fo"hasDefault": false,"writable": false},+ "notificationMessage": {+ "type": "Formattable",+ "name": "Message",+ "required": false,+ "hasDefault": false,+ "writable": true,+ "options": {},+ "location": "_meta"+ },"project": {"type": "Project","name": "Project",diff --git a/docs/api/apiv3/endpoints/projects.apib b/docs/api/apiv3/endpoints/projects.apibindex c8063bd5b13..4b9d1c8a211 100644--- a/docs/api/apiv3/endpoints/projects.apib+++ b/docs/api/apiv3/endpoints/projects.apib@@ -113,7 +113,7 @@ Depending on custom fields defined for projects, additional properties might exi+ Parameters-+ id (required, integer, `1`) ... Project id+ + id (required, integer, `1`) ... Project id+ Response 200 (application/hal+json)@@ -143,7 +143,7 @@ Updates the given project by applying the attributes provided in the body.+ Parameters-+ id (required, integer, `1`) ... Project id+ + id (required, integer, `1`) ... Project id+ Request Update project@@ -234,7 +234,7 @@ the project scheduled for deletion, it is archived at once.+ Parameters-+ id (required, integer, `1`) ... Project id+ + id (required, integer, `1`) ... Project id+ Response 204 (application/hal+json)@@ -1319,7 +1319,7 @@ The embedded schema of this form describes all associations to copy with a humanFor more details and all possible responses see the general specification of [Forms](#forms).-### Available meta parameters+### Meta parameters| Meta property | Description | Type | Constraints | Supported operations |Condition || :------------------------: | --------------------------------------------------- | ---- | ----------- | -------------------- |----------|@@ -2038,7 +2038,7 @@ but are also limited to the projects that the current user is allowed to see.+ Parameters-+ id (required, integer, `1`) ... Version id+ + id (required, integer, `1`) ... Version id+ Response 200 (application/hal+json)diff --git a/lib/api/decorators/meta_form.rb b/lib/api/decorators/meta_form.rbnew file mode 100644index 00000000000..84ff5c52991--- /dev/null+++ b/lib/api/decorators/meta_form.rb@@ -0,0 +1,38 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2021 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 API+ module Decorators+ module MetaForm+ def payload_representer+ payload_representer_class+ .create(represented, meta: meta, current_user: current_user)+ end+ end+ end+enddiff --git a/lib/api/decorators/property_schema_representer.rb b/lib/api/decorators/property_schema_representer.rbindex 43ea2dd51ac..f3e6f0357c7 100644--- a/lib/api/decorators/property_schema_representer.rb+++ b/lib/api/decorators/property_schema_representer.rb@@ -98,12 +98,16 @@ module API# Derive the frontend location value to be passed# either nil, _links, or _meta depending on the inputdef derive_location(location)- return if location.nil?-- return :_links if location.to_s == 'link'- return :_meta if location.to_s == 'meta'-- raise ArgumentError, "Invalid location attribute #{location}"+ case location.to_s+ when 'link'+ :_links+ when 'meta'+ :_meta+ when ''+ nil+ else+ raise ArgumentError, "Invalid location attribute #{location}"+ endendendenddiff --git a/lib/api/utilities/endpoints/bodied.rb b/lib/api/utilities/endpoints/bodied.rbindex 5a3bc72bd3d..c5e2ca2697c 100644--- a/lib/api/utilities/endpoints/bodied.rb+++ b/lib/api/utilities/endpoints/bodied.rb@@ -72,46 +72,41 @@ module APIupdate = self-> do- update.request = self+ params = update.parse(self)+ call = update.process(self, params)- params = update.parse(current_user, request_body)-- params = instance_exec(params, &update.params_modifier)-- call = update.process(current_user,- instance_exec(params, &update.instance_generator),- params)-- update.render(current_user, call) do+ update.render(self, call) dostatus update.success_statusendendend- def parse(current_user, request_body)+ def parse(request)parse_service- .new(current_user,+ .new(request.current_user,model: model,representer: parse_representer)- .call(request_body)+ .call(request.request_body).resultend- def process(current_user, instance, params)- args = { user: current_user,+ def process(request, params)+ instance = request.instance_exec(params, &instance_generator)++ args = { user: request.current_user,model: instance,contract_class: process_contract }process_service.new(**args.compact)- .with_state(process_state.call(model: instance, params: params))- .call(**params)+ .with_state(request.instance_exec(**{ model: instance, params: params }, &process_state))+ .call(**request.instance_exec(params, ¶ms_modifier))end- def render(current_user, call)+ def render(request, call)if success?(call)yield- present_success(current_user, call)+ present_success(request, call)elsepresent_error(call)end@@ -122,7 +117,6 @@ module APIendattr_accessor :model,- :request,:api_name,:instance_generator,:parse_representer,@@ -135,7 +129,7 @@ module APIprivate- def present_success(_current_user, _call)+ def present_success(_request, _call)raise NotImplementedErrorenddiff --git a/lib/api/utilities/endpoints/delete.rb b/lib/api/utilities/endpoints/delete.rbindex 93ad2760bda..8c29b9a6fe5 100644--- a/lib/api/utilities/endpoints/delete.rb+++ b/lib/api/utilities/endpoints/delete.rb@@ -52,8 +52,7 @@ module APIdelete = self-> do- call = delete.process(current_user,- instance_exec(params, &delete.instance_generator))+ call = delete.process(self)delete.render(call) dostatus delete.success_status@@ -61,10 +60,10 @@ module APIendend- def process(current_user, instance)+ def process(request)process_service- .new(user: current_user,- model: instance)+ .new(user: request.current_user,+ model: request.instance_exec(request.params, &instance_generator)).callenddiff --git a/lib/api/utilities/endpoints/modify.rb b/lib/api/utilities/endpoints/modify.rbindex c443850fa21..a556e057cb2 100644--- a/lib/api/utilities/endpoints/modify.rb+++ b/lib/api/utilities/endpoints/modify.rb@@ -38,7 +38,7 @@ module APIprivate- def present_success(_current_user, _call)+ def present_success(_request, _call)raise NotImplementedErrorenddiff --git a/lib/api/utilities/endpoints/show.rb b/lib/api/utilities/endpoints/show.rbindex 5d03f66a21b..7d7ca6761f4 100644--- a/lib/api/utilities/endpoints/show.rb+++ b/lib/api/utilities/endpoints/show.rb@@ -51,14 +51,14 @@ module APIshow = self-> do- show.render(instance_exec(params, &show.instance_generator))+ show.render(self)endend- def render(instance)+ def render(request)render_representer- .create(instance,- current_user: User.current,+ .create(request.instance_exec(request.params, &instance_generator),+ current_user: request.current_user,embed_links: true)enddiff --git a/lib/api/v3/memberships/create_form_api.rb b/lib/api/v3/memberships/create_form_api.rbindex 0b1aeae0567..491d708e4d1 100644--- a/lib/api/v3/memberships/create_form_api.rb+++ b/lib/api/v3/memberships/create_form_api.rb@@ -35,22 +35,29 @@ module APIauthorize :manage_members, global: trueend- post &::API::V3::Utilities::Endpoints::CreateForm.new(model: Member,- instance_generator: ->(params) {- # This here is a hack to circumvent the strange- # way roles are assigned to a member within 3 models.- # As this is never saved, we do not have to care for- # that elaborate process.- # Doing this leads to the roles being displayed- # in the payload.- roles = if params[:role_ids]- Array(Role.find_by(id: params.delete(:role_ids)))- end || []+ post &::API::V3::Utilities::Endpoints::CreateForm+ .new(model: Member,+ instance_generator: ->(params) {+ # This here is a hack to circumvent the strange+ # way roles are assigned to a member within 3 models.+ # As this is never saved, we do not have to care for+ # that elaborate process.+ # Doing this leads to the roles being displayed+ # in the payload.+ roles = if params[:role_ids]+ Array(Role.find_by(id: params.delete(:role_ids)))+ end || []- Member.new(roles: roles)- },- api_name: 'Membership')- .mount+ Member.new(roles: roles)+ },+ api_name: 'Membership',+ params_modifier: ->(params) do+ params.except(:meta)+ end,+ process_state: ->(params:, **) do+ params[:meta].deep_dup+ end)+ .mountendendenddiff --git a/lib/api/v3/memberships/form_representer.rb b/lib/api/v3/memberships/form_representer.rbindex c1b336932c0..e15512aaf63 100644--- a/lib/api/v3/memberships/form_representer.rb+++ b/lib/api/v3/memberships/form_representer.rb@@ -32,6 +32,8 @@ module APImodule V3module Membershipsclass FormRepresenter < ::API::Decorators::SimpleForm+ include ::API::Decorators::MetaForm+def modelMemberenddiff --git a/lib/api/v3/memberships/membership_meta_representer.rb b/lib/api/v3/memberships/membership_meta_representer.rbnew file mode 100644index 00000000000..229db348f08--- /dev/null+++ b/lib/api/v3/memberships/membership_meta_representer.rb@@ -0,0 +1,45 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2021 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 API+ module V3+ module Memberships+ class MembershipMetaRepresenter < ::API::Decorators::Single+ include API::Decorators::FormattableProperty++ formattable_property :notification_message++ def model_required?+ false+ end+ end+ end+ end+enddiff --git a/lib/api/v3/memberships/membership_payload_representer.rb b/lib/api/v3/memberships/membership_payload_representer.rbindex 9e4de2cc148..b98197ccbd2 100644--- a/lib/api/v3/memberships/membership_payload_representer.rb+++ b/lib/api/v3/memberships/membership_payload_representer.rb@@ -33,6 +33,11 @@ module APImodule Membershipsclass MembershipPayloadRepresenter < MembershipRepresenterinclude ::API::Utilities::PayloadRepresenter+ include ::API::Utilities::MetaProperty++ def meta_representer_class+ MembershipMetaRepresenter+ endendendenddiff --git a/lib/api/v3/memberships/memberships_api.rb b/lib/api/v3/memberships/memberships_api.rbindex 2e5b2cc489e..c8cde1bcc56 100644--- a/lib/api/v3/memberships/memberships_api.rb+++ b/lib/api/v3/memberships/memberships_api.rb@@ -33,14 +33,19 @@ module APIhelpers ::API::Utilities::PageSizeHelperresources :memberships do- get &::API::V3::Utilities::Endpoints::Index.new(model: Member,- scope: -> { Member.includes(MembershipRepresenter.to_eager_load) },- api_name: 'Membership')- .mount+ get &::API::V3::Utilities::Endpoints::Index+ .new(model: Member,+ scope: -> { Member.includes(MembershipRepresenter.to_eager_load) },+ api_name: 'Membership')+ .mount- post &::API::V3::Utilities::Endpoints::Create.new(model: Member,- api_name: 'Membership')- .mount+ post &::API::V3::Utilities::Endpoints::Create+ .new(model: Member,+ api_name: 'Membership',+ params_modifier: ->(params) {+ params.except(:meta).merge(params.fetch(:meta, {}).to_h)+ })+ .mountmount ::API::V3::Memberships::AvailableProjectsAPImount ::API::V3::Memberships::Schemas::MembershipSchemaAPI@@ -54,16 +59,22 @@ module API.find(params['id'])end- get &::API::V3::Utilities::Endpoints::Show.new(model: Member,- api_name: 'Membership')- .mount+ get &::API::V3::Utilities::Endpoints::Show+ .new(model: Member,+ api_name: 'Membership')+ .mount- patch &::API::V3::Utilities::Endpoints::Update.new(model: Member,- api_name: 'Membership')- .mount+ patch &::API::V3::Utilities::Endpoints::Update+ .new(model: Member,+ api_name: 'Membership',+ params_modifier: ->(params) {+ params.except(:meta).merge(params.fetch(:meta, {}).to_h)+ })+ .mount- delete &::API::V3::Utilities::Endpoints::Delete.new(model: Member)- .mount+ delete &::API::V3::Utilities::Endpoints::Delete+ .new(model: Member)+ .mountmount ::API::V3::Memberships::UpdateFormAPIenddiff --git a/lib/api/v3/memberships/schemas/membership_schema_representer.rb b/lib/api/v3/memberships/schemas/membership_schema_representer.rbindex c67a4596311..28c11a0e7ad 100644--- a/lib/api/v3/memberships/schemas/membership_schema_representer.rb+++ b/lib/api/v3/memberships/schemas/membership_schema_representer.rb@@ -47,6 +47,13 @@ module APIschema :updated_at,type: 'DateTime'+ schema :notification_message,+ type: 'Formattable',+ name_source: ->(*) { I18n.t(:label_message) },+ writable: true,+ required: false,+ location: :meta+schema_with_allowed_link :project,has_default: false,required: false,diff --git a/lib/api/v3/memberships/update_form_api.rb b/lib/api/v3/memberships/update_form_api.rbindex a48e9444226..b46b64a236e 100644--- a/lib/api/v3/memberships/update_form_api.rb+++ b/lib/api/v3/memberships/update_form_api.rb@@ -35,9 +35,16 @@ module APIauthorize :manage_members, global: trueend- post &::API::V3::Utilities::Endpoints::UpdateForm.new(model: Member,- api_name: 'Membership')- .mount+ post &::API::V3::Utilities::Endpoints::UpdateForm+ .new(model: Member,+ api_name: 'Membership',+ params_modifier: ->(params) do+ params.except(:meta)+ end,+ process_state: ->(params:, **) do+ params[:meta].deep_dup+ end)+ .mountendendenddiff --git a/lib/api/v3/projects/copy/form_representer.rb b/lib/api/v3/projects/copy/form_representer.rbindex f28e1ac6e91..e998e17b930 100644--- a/lib/api/v3/projects/copy/form_representer.rb+++ b/lib/api/v3/projects/copy/form_representer.rb@@ -33,11 +33,12 @@ module APImodule Projectsmodule Copyclass FormRepresenter < ::API::V3::Projects::FormRepresenter+ include ::API::Decorators::MetaForm+private- def payload_representer+ def payload_representer_classProjectCopyPayloadRepresenter- .create(represented, meta: meta, current_user: current_user)end##diff --git a/lib/api/v3/utilities/endpoints/delayed_modify.rb b/lib/api/v3/utilities/endpoints/delayed_modify.rbindex 38f34fed31c..622ceb4c0ee 100644--- a/lib/api/v3/utilities/endpoints/delayed_modify.rb+++ b/lib/api/v3/utilities/endpoints/delayed_modify.rb@@ -36,11 +36,11 @@ module APIprivate- def present_success(current_user, call)- redirect_to_status(call.result)+ def present_success(request, call)+ redirect_to_status(request, call.result)end- def redirect_to_status(job)+ def redirect_to_status(request, job)request.redirect api_v3_paths.job_status(job.job_id)endenddiff --git a/lib/api/v3/utilities/endpoints/form.rb b/lib/api/v3/utilities/endpoints/form.rbindex 2d09fe0c6c4..68ead09ead6 100644--- a/lib/api/v3/utilities/endpoints/form.rb+++ b/lib/api/v3/utilities/endpoints/form.rb@@ -37,12 +37,12 @@ module APIonly_validation_errors?(api_errors(call))end- def present_success(current_user, call)+ def present_success(request, call)render_representer.new(call.result,errors: api_errors(call),meta: call.state,- current_user: current_user)+ current_user: request.current_user)enddef present_error(call)diff --git a/lib/api/v3/utilities/endpoints/index.rb b/lib/api/v3/utilities/endpoints/index.rbindex 0a0809c06e3..9fc42d2616b 100644--- a/lib/api/v3/utilities/endpoints/index.rb+++ b/lib/api/v3/utilities/endpoints/index.rb@@ -35,24 +35,24 @@ module APIindex = self-> do- query = index.parse(params)+ query = index.parse(self)- self_path = api_v3_paths.send(index.self_path)- base_scope = index.scope ? instance_exec(&index.scope) : index.model-- index.render(query, params, self_path, base_scope)+ index.render(self, query)endend- def parse(params)+ def parse(request)ParamsToQueryService- .new(model, User.current)- .call(params)+ .new(model, request.current_user)+ .call(request.params)end- def render(query, params, self_path, base_scope)+ def render(request, query)if query.valid?- render_success(query, params, self_path, base_scope)+ render_success(query,+ request.params,+ request.api_v3_paths.send(self_path),+ scope ? request.instance_exec(&scope) : model)elserender_error(query)enddiff --git a/lib/api/v3/utilities/endpoints/schema.rb b/lib/api/v3/utilities/endpoints/schema.rbindex 1fbf7d33e23..bfe0d4b9738 100644--- a/lib/api/v3/utilities/endpoints/schema.rb+++ b/lib/api/v3/utilities/endpoints/schema.rb@@ -53,21 +53,17 @@ module APIschema = self-> do- self_path = api_v3_paths.send(schema.self_path)-- schema.render(instance_exec(params, &schema.instance_generator),- self_path)+ schema.render(self)endend- def render(instance,- self_path)- contract_instance = contract.new(instance, User.current)+ def render(request)+ instance = request.instance_exec(request.params, &instance_generator)representer- .create(contract_instance,- self_link: self_path,- current_user: User.current)+ .create(contract.new(instance, request.current_user),+ self_link: request.api_v3_paths.send(self_path),+ current_user: request.current_user)enddef self_pathdiff --git a/lib/api/v3/utilities/endpoints/v3_present_single.rb b/lib/api/v3/utilities/endpoints/v3_present_single.rbindex 34058439ef9..0dc4a48147c 100644--- a/lib/api/v3/utilities/endpoints/v3_present_single.rb+++ b/lib/api/v3/utilities/endpoints/v3_present_single.rb@@ -31,10 +31,10 @@ module APImodule Utilitiesmodule Endpointsmodule V3PresentSingle- def present_success(current_user, call)+ def present_success(request, call)render_representer.create(call.result,- current_user: current_user,+ current_user: request.current_user,embed_links: true)endenddiff --git a/modules/bim/app/controllers/bim/bcf/api/v2_1/endpoints/create.rb b/modules/bim/app/controllers/bim/bcf/api/v2_1/endpoints/create.rbindex af0b8658a17..a70e1e6e687 100644--- a/modules/bim/app/controllers/bim/bcf/api/v2_1/endpoints/create.rb+++ b/modules/bim/app/controllers/bim/bcf/api/v2_1/endpoints/create.rb@@ -32,7 +32,7 @@ module Bim::Bcf::API::V2_1::Endpointsclass Create < API::Utilities::Endpoints::Createinclude ModifyMixin- def present_success(_current_user, call)+ def present_success(_request, call)render_representer.new(call.result)enddiff --git a/modules/bim/app/controllers/bim/bcf/api/v2_1/endpoints/index.rb b/modules/bim/app/controllers/bim/bcf/api/v2_1/endpoints/index.rbindex 1282a9e4241..a2c060eb7ff 100644--- a/modules/bim/app/controllers/bim/bcf/api/v2_1/endpoints/index.rb+++ b/modules/bim/app/controllers/bim/bcf/api/v2_1/endpoints/index.rb@@ -34,13 +34,13 @@ module Bim::Bcf::API::V2_1::Endpointsindex = self-> do- collection = index.scope ? instance_exec(&index.scope) : index.model-- index.render(collection)+ index.render(self)endend- def render(collection)+ def render(request)+ collection = scope ? request.instance_exec(&scope) : model+collection.map do |instance|render_representerdiff --git a/modules/bim/app/controllers/bim/bcf/api/v2_1/endpoints/show.rb b/modules/bim/app/controllers/bim/bcf/api/v2_1/endpoints/show.rbindex ac768af0b3c..423123a43f0 100644--- a/modules/bim/app/controllers/bim/bcf/api/v2_1/endpoints/show.rb+++ b/modules/bim/app/controllers/bim/bcf/api/v2_1/endpoints/show.rb@@ -30,9 +30,9 @@module Bim::Bcf::API::V2_1::Endpointsclass Show < API::Utilities::Endpoints::Show- def render(instance)+ def render(request)render_representer- .new(instance)+ .new(request.instance_exec(request.params, &instance_generator))endprivatediff --git a/modules/bim/app/controllers/bim/bcf/api/v2_1/endpoints/update.rb b/modules/bim/app/controllers/bim/bcf/api/v2_1/endpoints/update.rbindex 5b8740ed595..c8160c661c5 100644--- a/modules/bim/app/controllers/bim/bcf/api/v2_1/endpoints/update.rb+++ b/modules/bim/app/controllers/bim/bcf/api/v2_1/endpoints/update.rb@@ -32,7 +32,7 @@ module Bim::Bcf::API::V2_1::Endpointsclass Update < API::Utilities::Endpoints::Updateinclude ModifyMixin- def present_success(_current_user, call)+ def present_success(_request, call)render_representer.new(call.result)enddiff --git a/modules/two_factor_authentication/spec/features/my_two_factor_devices_spec.rb b/modules/two_factor_authentication/spec/features/my_two_factor_devices_spec.rbindex 3cdfa837b4e..1d222b55c10 100644--- a/modules/two_factor_authentication/spec/features/my_two_factor_devices_spec.rb+++ b/modules/two_factor_authentication/spec/features/my_two_factor_devices_spec.rb@@ -1,10 +1,11 @@require_relative '../spec_helper'-describe 'My Account 2FA configuration', with_2fa_ee: true, type: :feature,+describe 'My Account 2FA configuration', with_2fa_ee: true,+ type: :feature,with_config: { '2fa': { active_strategies: %i[developer totp] } },js: true dolet(:dialog) { ::Components::PasswordConfirmationDialog.new }- let(:user_password) { 'bob!' * 4 }+ let(:user_password) { 'boB!4' * 4 }let(:user) doFactoryBot.create(:user,login: 'bob',diff --git a/spec/lib/api/v3/memberships/membership_payload_representer_spec.rb b/spec/lib/api/v3/memberships/membership_payload_representer_spec.rbnew file mode 100644index 00000000000..3b023f96b3e--- /dev/null+++ b/spec/lib/api/v3/memberships/membership_payload_representer_spec.rb@@ -0,0 +1,83 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2021 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.+#++++require 'spec_helper'++describe ::API::V3::Memberships::MembershipPayloadRepresenter do+ let(:membership) { FactoryBot.build_stubbed(:member) }++ current_user { FactoryBot.build_stubbed(:user) }++ describe 'generation' do+ subject(:json) { representer.to_json }++ describe '_meta' do+ describe 'notificationMessage' do+ let(:meta) { OpenStruct.new notifiation_message: 'Come to the dark side' }+ let(:representer) do+ described_class.create(membership,+ meta: meta,+ current_user: current_user)+ end++ it_behaves_like 'formattable property', :'_meta/notificationMessage' do+ let(:value) { meta.notification_message }+ end+ end+ end+ end++ describe 'parsing' do+ subject(:parsed) { representer.from_hash parsed_hash }++ let(:representer) do+ described_class.create(OpenStruct.new,+ meta: OpenStruct.new,+ current_user: current_user)+ end++ describe '_meta' do+ context 'with meta set' do+ let(:parsed_hash) do+ {+ '_meta' => {+ 'notificationMessage' => {+ "raw" => 'Come to the dark side'+ }+ }+ }+ end++ it 'sets the parsed message' do+ expect(parsed.meta.notification_message)+ .to eql 'Come to the dark side'+ end+ end+ end+ end+enddiff --git a/spec/lib/api/v3/memberships/schemas/membership_schema_representer_spec.rb b/spec/lib/api/v3/memberships/schemas/membership_schema_representer_spec.rbindex 749c0805afb..acc4958ecad 100644--- a/spec/lib/api/v3/memberships/schemas/membership_schema_representer_spec.rb+++ b/spec/lib/api/v3/memberships/schemas/membership_schema_representer_spec.rb@@ -103,7 +103,7 @@ describe ::API::V3::Memberships::Schemas::MembershipSchemaRepresenter doit_behaves_like 'has basic schema properties' dolet(:type) { 'DateTime' }- let(:name) { Version.human_attribute_name('created_at') }+ let(:name) { Member.human_attribute_name('created_at') }let(:required) { true }let(:writable) { false }end@@ -114,12 +114,24 @@ describe ::API::V3::Memberships::Schemas::MembershipSchemaRepresenter doit_behaves_like 'has basic schema properties' dolet(:type) { 'DateTime' }- let(:name) { Version.human_attribute_name('updated_at') }+ let(:name) { Member.human_attribute_name('updated_at') }let(:required) { true }let(:writable) { false }endend+ describe 'notificationMessage' do+ let(:path) { 'notificationMessage' }++ it_behaves_like 'has basic schema properties' do+ let(:type) { 'Formattable' }+ let(:name) { I18n.t('label_message') }+ let(:required) { false }+ let(:writable) { true }+ let(:location) { :_meta }+ end+ end+describe 'project' dolet(:path) { 'project' }diff --git a/spec/requests/api/v3/projects/copy/project_copy_payload_representer_spec.rb b/spec/lib/api/v3/projects/copy/project_copy_payload_representer_spec.rbsimilarity index 100%rename from spec/requests/api/v3/projects/copy/project_copy_payload_representer_spec.rbrename to spec/lib/api/v3/projects/copy/project_copy_payload_representer_spec.rbdiff --git a/spec/requests/api/v3/projects/copy/project_copy_schema_representer_spec.rb b/spec/lib/api/v3/projects/copy/project_copy_schema_representer_spec.rbsimilarity index 100%rename from spec/requests/api/v3/projects/copy/project_copy_schema_representer_spec.rbrename to spec/lib/api/v3/projects/copy/project_copy_schema_representer_spec.rbdiff --git a/spec/mailers/member_mailer_spec.rb b/spec/mailers/member_mailer_spec.rbindex 9c940e7cdec..699ffb5c675 100644--- a/spec/mailers/member_mailer_spec.rb+++ b/spec/mailers/member_mailer_spec.rb@@ -30,6 +30,10 @@require 'spec_helper'describe MemberMailer, type: :mailer do+ include OpenProject::ObjectLinking+ include ActionView::Helpers::UrlHelper+ include OpenProject::StaticRouting::UrlHelpers+let(:current_user) { FactoryBot.build_stubbed(:user) }let(:member) doFactoryBot.build_stubbed(:member,@@ -40,6 +44,7 @@ describe MemberMailer, type: :mailer dolet(:principal) { FactoryBot.build_stubbed(:user) }let(:project) { FactoryBot.build_stubbed(:project) }let(:roles) { [FactoryBot.build_stubbed(:role), FactoryBot.build_stubbed(:role)] }+ let(:message) { nil }shared_examples_for 'has a subject' do |key|it "has a subject" do@@ -90,10 +95,6 @@ describe MemberMailer, type: :mailer doshared_examples_for 'has the expected body' dolet(:body) { subject.body.parts.detect { |part| part['Content-Type'].value == 'text/html' }.body.to_s }- it 'has the expected header' do- expect(body)- .to have_text(expected_header)- endit 'highlights the roles received' doexpected = <<~MSG@@ -107,10 +108,42 @@ describe MemberMailer, type: :mailer do.to be_html_eql(expected).at_path('body/ul')end++ context 'with a custom message' do+ let(:message) { "Some **styled** message" }++ it 'has the expected header' do+ params = {+ project: project ? link_to_project(project, only_path: false) : nil,+ user: link_to_user(current_user, only_path: false)+ }.compact++ expect(body)+ .to include(I18n.t(:"#{expected_header}.with_message", **params))+ end++ it 'includes the custom message' do+ expect(body)+ .to include("Some <strong>styled</strong> message")+ end+ end++ context 'without a custom message' do++ it 'has the expected header' do+ params = {+ project: project ? link_to_project(project, only_path: false) : nil,+ user: link_to_user(current_user, only_path: false)+ }.compact++ expect(body)+ .to include(I18n.t(:"#{expected_header}.without_message", **params))+ end+ endenddescribe '#added_project' do- subject { MemberMailer.added_project(current_user, member) }+ subject { MemberMailer.added_project(current_user, member, message) }it_behaves_like "sends a mail to the member's principal"it_behaves_like 'has a subject', :'mail_member_added_project.subject'@@ -118,14 +151,14 @@ describe MemberMailer, type: :mailer doit_behaves_like 'sets the expected openproject header'it_behaves_like 'has the expected body' dolet(:expected_header) do- "#{current_user.name} added you as a member to the project '#{project.name}'."+ "mail_member_added_project.body.added_by"endendit_behaves_like 'fails for a group'enddescribe '#updated_project' do- subject { MemberMailer.updated_project(current_user, member) }+ subject { MemberMailer.updated_project(current_user, member, message) }it_behaves_like "sends a mail to the member's principal"it_behaves_like 'has a subject', :'mail_member_updated_project.subject'@@ -133,7 +166,7 @@ describe MemberMailer, type: :mailer doit_behaves_like 'sets the expected openproject header'it_behaves_like 'has the expected body' dolet(:expected_header) do- "#{current_user.name} updated the roles you have in the project '#{project.name}'."+ "mail_member_updated_project.body.updated_by"endendit_behaves_like 'fails for a group'@@ -142,14 +175,14 @@ describe MemberMailer, type: :mailer dodescribe '#updated_global' dolet(:project) { nil }- subject { MemberMailer.updated_global(current_user, member) }+ subject { MemberMailer.updated_global(current_user, member, message) }it_behaves_like "sends a mail to the member's principal"it_behaves_like 'has a subject', :'mail_member_updated_global.subject'it_behaves_like 'sets the expected message_id header'it_behaves_like 'has the expected body' dolet(:expected_header) do- "#{current_user.name} updated the roles you have globally."+ "mail_member_updated_global.body.updated_by"endendit_behaves_like 'fails for a group'diff --git a/spec/requests/api/v3/membership_resources_spec.rb b/spec/requests/api/v3/membership_resources_spec.rbindex 8921d80691e..37e03579b63 100644--- a/spec/requests/api/v3/membership_resources_spec.rb+++ b/spec/requests/api/v3/membership_resources_spec.rb@@ -76,6 +76,11 @@ describe 'API v3 memberships resource', type: :request, content_type: :json doexpect(ActionMailer::Base.deliveries.map(&:to).flatten).to match_array expected_receivers.map(&:mail)++ if defined?(custom_message)+ expect(ActionMailer::Base.deliveries.map { |mail| mail.body.encoded })+ .to all include(OpenProject::TextFormatting::Renderer.format_text(custom_message))+ endendend@@ -369,6 +374,7 @@ describe 'API v3 memberships resource', type: :request, content_type: :json dolet(:path) { api_v3_paths.memberships }let(:principal) { other_user }let(:principal_path) { api_v3_paths.user(principal.id) }+ let(:custom_message) { 'Wish you where **here**.' }let(:body) do{_links: {@@ -383,6 +389,11 @@ describe 'API v3 memberships resource', type: :request, content_type: :json dohref: api_v3_paths.role(other_role.id)}]+ },+ _meta: {+ notificationMessage: {+ raw: custom_message+ }}}.to_jsonend@@ -459,6 +470,11 @@ describe 'API v3 memberships resource', type: :request, content_type: :json dohref: api_v3_paths.role(other_role.id)}]+ },+ _meta: {+ notificationMessage: {+ raw: custom_message+ }}}.to_jsonend@@ -482,17 +498,24 @@ describe 'API v3 memberships resource', type: :request, content_type: :json dolet(:principal_path) { api_v3_paths.placeholder_user(placeholder_user.id) }let(:body) do{- project: {- href: api_v3_paths.project(project.id)- },- principal: {- href: principal_path+ _links: {+ project: {+ href: api_v3_paths.project(project.id)+ },+ principal: {+ href: principal_path+ },+ roles: [+ {+ href: api_v3_paths.role(other_role.id)+ }+ ]},- roles: [- {- href: api_v3_paths.role(other_role.id)+ _meta: {+ notificationMessage: {+ raw: custom_message}- ]+ }}.to_jsonend@@ -519,6 +542,11 @@ describe 'API v3 memberships resource', type: :request, content_type: :json dohref: api_v3_paths.role(global_role.id)}]+ },+ _meta: {+ notificationMessage: {+ raw: custom_message+ }}}.to_jsonend@@ -686,6 +714,7 @@ describe 'API v3 memberships resource', type: :request, content_type: :json dodescribe 'PATCH api/v3/memberships/:id' dolet(:path) { api_v3_paths.membership(other_member.id) }let(:another_role) { FactoryBot.create(:role) }+ let(:custom_message) { 'Wish you where **here**.' }let(:body) do{_links: {@@ -694,6 +723,11 @@ describe 'API v3 memberships resource', type: :request, content_type: :json dohref: api_v3_paths.role(another_role.id)}]+ },+ _meta: {+ notificationMessage: {+ raw: custom_message+ }}}.to_jsonenddiff --git a/spec/requests/api/v3/memberships/create_form_resource_spec.rb b/spec/requests/api/v3/memberships/create_form_resource_spec.rbindex 6009361f49a..285188801bf 100644--- a/spec/requests/api/v3/memberships/create_form_resource_spec.rb+++ b/spec/requests/api/v3/memberships/create_form_resource_spec.rb@@ -101,17 +101,24 @@ describe ::API::V3::Memberships::CreateFormAPI, content_type: :json dolet!(:list_cf) { FactoryBot.create(:list_version_custom_field) }let(:parameters) do{- principal: {- href: api_v3_paths.user(other_user.id)+ _links: {+ principal: {+ href: api_v3_paths.user(other_user.id)+ },+ project: {+ href: api_v3_paths.project(project.id)+ },+ roles: [+ {+ href: api_v3_paths.role(role.id)+ }+ ]},- project: {- href: api_v3_paths.project(project.id)- },- roles: [- {- href: api_v3_paths.role(role.id)+ _meta: {+ notificationMessage: {+ raw: "Join the **dark** side."}- ]+ }}end@@ -137,6 +144,10 @@ describe ::API::V3::Memberships::CreateFormAPI, content_type: :json doexpect(last_response.body).to be_json_eql(api_v3_paths.role(role.id).to_json).at_path('_embedded/payload/_links/roles/0/href')++ expect(last_response.body)+ .to be_json_eql("Join the **dark** side.".to_json)+ .at_path('_embedded/payload/_meta/notificationMessage/raw')endit 'has a commit link' dodiff --git a/spec/requests/api/v3/memberships/update_form_resource_spec.rb b/spec/requests/api/v3/memberships/update_form_resource_spec.rbindex 81682198e7f..4e549810e06 100644--- a/spec/requests/api/v3/memberships/update_form_resource_spec.rb+++ b/spec/requests/api/v3/memberships/update_form_resource_spec.rb@@ -59,12 +59,18 @@ describe ::API::V3::Memberships::UpdateFormAPI, content_type: :json dohref: api_v3_paths.role(other_role.id)}]+ },+ _meta: {+ notificationMessage: {+ raw: "Join the **dark** side."+ }}}end+ current_user { user }+before do- login_as(user)post path, parameters.to_jsonend@@ -100,6 +106,12 @@ describe ::API::V3::Memberships::UpdateFormAPI, content_type: :json do.at_path('_embedded/payload/_links/roles/1/href')end+ it 'contains the notification message' do+ expect(response.body)+ .to be_json_eql("Join the **dark** side.".to_json)+ .at_path('_embedded/payload/_meta/notificationMessage/raw')+ end+it 'does not contain the project in the payload' doexpect(response.body).not_to have_json_path('_embedded/payload/_links/project')diff --git a/spec/services/base_services/behaves_like_update_service.rb b/spec/services/base_services/behaves_like_update_service.rbindex 4aa4a038bec..bad1ff8446a 100644--- a/spec/services/base_services/behaves_like_update_service.rb+++ b/spec/services/base_services/behaves_like_update_service.rb@@ -48,7 +48,7 @@ shared_examples 'BaseServices update service' domodel: model_instance,contract_class: contract_class)end- let(:call_attributes) { { name: 'Some name', identifier: 'Some identifier' } }+ let(:call_attributes) { { some: 'hash'} }let(:set_attributes_success) dotrueenddiff --git a/spec/services/groups/add_users_service_integration_spec.rb b/spec/services/groups/add_users_service_integration_spec.rbindex ba83427397e..e0e9021423b 100644--- a/spec/services/groups/add_users_service_integration_spec.rb+++ b/spec/services/groups/add_users_service_integration_spec.rb@@ -29,7 +29,7 @@require 'spec_helper'describe Groups::AddUsersService, 'integration', type: :model do- subject(:service_call) { instance.call(ids: user_ids) }+ subject(:service_call) { instance.call(ids: user_ids, message: message) }let(:projects) { FactoryBot.create_list :project, 2 }let(:role) { FactoryBot.create :role }@@ -44,6 +44,7 @@ describe Groups::AddUsersService, 'integration', type: :model dolet(:user1) { FactoryBot.create :user }let(:user2) { FactoryBot.create :user }let(:user_ids) { [user1.id, user2.id] }+ let(:message) { 'Some message' }let(:instance) dodescribed_class.new(group, current_user: current_user)@@ -75,7 +76,8 @@ describe Groups::AddUsersService, 'integration', type: :model doexpect(Notifications::GroupMemberAlteredJob).to have_received(:perform_later)- .with(a_collection_containing_exactly(*ids))+ .with(a_collection_containing_exactly(*ids),+ message)endenddiff --git a/spec/services/groups/cleanup_inherited_roles_service_integration_spec.rb b/spec/services/groups/cleanup_inherited_roles_service_integration_spec.rbindex a409e0088e4..f07e9daa5c6 100644--- a/spec/services/groups/cleanup_inherited_roles_service_integration_spec.rb+++ b/spec/services/groups/cleanup_inherited_roles_service_integration_spec.rb@@ -38,7 +38,8 @@ describe Groups::CleanupInheritedRolesService, 'integration', type: :model dolet(:role) { FactoryBot.create :role }let(:current_user) { FactoryBot.create :admin }let(:roles) { [role] }- let(:params) { {} }+ let(:params) { { message: message } }+ let(:message) { "Some message" }let!(:group) doFactoryBot.create(:group,@@ -137,7 +138,8 @@ describe Groups::CleanupInheritedRolesService, 'integration', type: :model doexpect(Notifications::GroupMemberAlteredJob).to have_received(:perform_later)- .with([first_user_member.id])+ .with([first_user_member.id],+ message)endend@@ -176,7 +178,8 @@ describe Groups::CleanupInheritedRolesService, 'integration', type: :model doexpect(Notifications::GroupMemberAlteredJob).to have_received(:perform_later)- .with([first_user_member.id])+ .with([first_user_member.id],+ message)endenddiff --git a/spec/services/groups/update_roles_service_integration_spec.rb b/spec/services/groups/update_roles_service_integration_spec.rbindex 6ba13f6d5f7..32eab49a92c 100644--- a/spec/services/groups/update_roles_service_integration_spec.rb+++ b/spec/services/groups/update_roles_service_integration_spec.rb@@ -29,7 +29,7 @@require 'spec_helper'describe Groups::UpdateRolesService, 'integration', type: :model do- subject(:service_call) { instance.call(member: member) }+ subject(:service_call) { instance.call(member: member, message: message) }let(:project) { FactoryBot.create :project }let(:role) { FactoryBot.create :role }@@ -51,6 +51,7 @@ describe Groups::UpdateRolesService, 'integration', type: :model doendlet(:users) { FactoryBot.create_list :user, 2 }let(:member) { Member.find_by(principal: group) }+ let(:message) { "Some message" }let(:instance) dodescribed_class.new(group, current_user: current_user)@@ -81,7 +82,8 @@ describe Groups::UpdateRolesService, 'integration', type: :model doexpect(Notifications::GroupMemberAlteredJob).to have_received(:perform_later)- .with(a_collection_containing_exactly(*Member.where(principal: user).pluck(:id)))+ .with(a_collection_containing_exactly(*Member.where(principal: user).pluck(:id)),+ message)endenddiff --git a/spec/services/members/create_service_spec.rb b/spec/services/members/create_service_spec.rbindex da2fcade9ef..67497d681d0 100644--- a/spec/services/members/create_service_spec.rb+++ b/spec/services/members/create_service_spec.rb@@ -33,6 +33,10 @@ require 'services/base_services/behaves_like_create_service'describe Members::CreateService, type: :model doit_behaves_like 'BaseServices create service' do+ let(:call_attributes) do+ { project_id: "1", user_id: "2", role_ids: ["2"], notification_message: "Wish you where **here**." }+ end+let!(:allow_notification_call) doallow(OpenProject::Notifications).to receive(:send)@@ -43,7 +47,8 @@ describe Members::CreateService, type: :model doexpect(OpenProject::Notifications).to receive(:send).with(OpenProject::Events::MEMBER_CREATED,- member: model_instance)+ member: model_instance,+ message: call_attributes[:notification_message])subjectenddiff --git a/spec/services/members/update_service_spec.rb b/spec/services/members/update_service_spec.rbindex 81739b5fd4e..7167f972ae2 100644--- a/spec/services/members/update_service_spec.rb+++ b/spec/services/members/update_service_spec.rb@@ -33,6 +33,10 @@ require 'services/base_services/behaves_like_update_service'describe Members::UpdateService, type: :model doit_behaves_like 'BaseServices update service' do+ let(:call_attributes) do+ { role_ids: ["2"], notification_message: "Wish you where **here**." }+ end+let!(:allow_notification_call) doallow(OpenProject::Notifications).to receive(:send)@@ -43,7 +47,8 @@ describe Members::UpdateService, type: :model doexpect(OpenProject::Notifications).to receive(:send).with(OpenProject::Events::MEMBER_UPDATED,- member: model_instance)+ member: model_instance,+ message: call_attributes[:notification_message])subjectenddiff --git a/spec/support/matchers/be_html_eql.rb b/spec/support/matchers/be_html_eql.rbindex 11030ee5887..7579b997771 100644--- a/spec/support/matchers/be_html_eql.rb+++ b/spec/support/matchers/be_html_eql.rb@@ -67,4 +67,4 @@failure_message &should_messagefailure_message_when_negated &should_not_message-end+end\ No newline at end of filediff --git a/spec/workers/mails/member_created_job_spec.rb b/spec/workers/mails/member_created_job_spec.rbindex 00fcba0cfba..35cc68149ad 100644--- a/spec/workers/mails/member_created_job_spec.rb+++ b/spec/workers/mails/member_created_job_spec.rb@@ -57,7 +57,7 @@ describe Mails::MemberCreatedJob, type: :model doexpect(MemberMailer).to have_received(:added_project)- .with(current_user, group_user_member)+ .with(current_user, group_user_member, message)endend@@ -98,7 +98,7 @@ describe Mails::MemberCreatedJob, type: :model doexpect(MemberMailer).to have_received(:updated_project)- .with(current_user, group_user_member)+ .with(current_user, group_user_member, message)endendenddiff --git a/spec/workers/mails/member_updated_job_spec.rb b/spec/workers/mails/member_updated_job_spec.rbindex 591cce75c45..42736e7139a 100644--- a/spec/workers/mails/member_updated_job_spec.rb+++ b/spec/workers/mails/member_updated_job_spec.rb@@ -47,7 +47,7 @@ describe Mails::MemberUpdatedJob, type: :model doexpect(MemberMailer).to have_received(:updated_project)- .with(current_user, group_user_member)+ .with(current_user, group_user_member, message)endenddiff --git a/spec/workers/mails/shared/member_job.rb b/spec/workers/mails/shared/member_job.rbindex 004276a86ce..6a9b136f982 100644--- a/spec/workers/mails/shared/member_job.rb+++ b/spec/workers/mails/shared/member_job.rb@@ -29,7 +29,11 @@require 'spec_helper'shared_examples 'member job' do- subject(:run_job) { described_class.perform_now(current_user: current_user, member: member) }+ subject(:run_job) do+ described_class.perform_now(current_user: current_user,+ member: member,+ message: message)+ endlet(:member) doFactoryBot.build_stubbed(:member,@@ -85,6 +89,7 @@ shared_examples 'member job' dolet(:group_user_members) { [] }let(:role) { FactoryBot.build_stubbed(:role) }let(:member_role_inherited_from) { nil }+ let(:message) { "Some message" }current_user { FactoryBot.build_stubbed(:user) }@@ -116,13 +121,25 @@ shared_examples 'member job' doexpect(MemberMailer).to have_received(:updated_global)- .with(current_user, member)+ .with(current_user, member, message)endend- context 'with sending disabled', with_settings: { notified_events: [] } do+ context 'with sending disabled and no message', with_settings: { notified_events: [] } do+ let(:message) { '' }+it_behaves_like 'sends no mail'end++ context 'with sending disabled and a message', with_settings: { notified_events: [] } do+ it 'sends mail' do+ run_job++ expect(MemberMailer)+ .to have_received(:updated_global)+ .with(current_user, member, message)+ end+ endendcontext 'with a user membership' do@@ -132,12 +149,24 @@ shared_examples 'member job' doexpect(MemberMailer).to have_received(user_project_mail_method)- .with(current_user, member)+ .with(current_user, member, message)endend- context 'with sending disabled', with_settings: { notified_events: [] } do+ context 'with sending disabled and no message', with_settings: { notified_events: [] } do+ let(:message) { '' }+it_behaves_like 'sends no mail'end++ context 'with sending disabled and a message', with_settings: { notified_events: [] } do+ it 'sends mail' do+ run_job++ expect(MemberMailer)+ .to have_received(user_project_mail_method)+ .with(current_user, member, message)+ end+ endendenddiff --git a/spec/workers/notifications/group_member_altered_job_spec.rb b/spec/workers/notifications/group_member_altered_job_spec.rbindex 453b96d1939..1711367ce51 100644--- a/spec/workers/notifications/group_member_altered_job_spec.rb+++ b/spec/workers/notifications/group_member_altered_job_spec.rb@@ -32,7 +32,7 @@ require 'spec_helper'describe Notifications::GroupMemberAlteredJob, type: :model dosubject(:service_call) do- described_class.new.perform(members_ids)+ described_class.new.perform(members_ids, message)endlet(:time) { Time.now }let(:member1) do@@ -43,6 +43,7 @@ describe Notifications::GroupMemberAlteredJob, type: :model doendlet(:members) { [member1, member2] }let(:members_ids) { members.map(&:id) }+ let(:message) { "Some message" }before doallow(OpenProject::Notifications)@@ -59,7 +60,7 @@ describe Notifications::GroupMemberAlteredJob, type: :model doexpect(OpenProject::Notifications).to have_received(:send)- .with(OpenProject::Events::MEMBER_CREATED, member: member1)+ .with(OpenProject::Events::MEMBER_CREATED, member: member1, message: message)endit 'sends an updated notification for the membership with the mismatching timestamps' do@@ -67,6 +68,6 @@ describe Notifications::GroupMemberAlteredJob, type: :model doexpect(OpenProject::Notifications).to have_received(:send)- .with(OpenProject::Events::MEMBER_UPDATED, member: member2)+ .with(OpenProject::Events::MEMBER_UPDATED, member: member2, message: message)endend