Skip to content

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

fix mentioning multiple users in a single line

ulfertscaf4588dcf5c
diff --git a/app/services/notifications/create_from_model_service.rb b/app/services/notifications/create_from_model_service.rb
index e79062956fe..51973a8a0aa 100644
--- a/app/services/notifications/create_from_model_service.rb
+++ b/app/services/notifications/create_from_model_service.rb
@@ -27,22 +27,34 @@
#++
class Notifications::CreateFromModelService
- MENTION_USER_ID_PATTERN =
- '<mention[^>]*(?:data-type="user"[^>]*data-id="(\d+)")|(?:data-id="(\d+)"[^>]*data-type="user")[^>]*>)|(?:\buser#(\d+)\b'
+ MENTION_USER_TAG_ID_PATTERN =
+ '<mention[^>]*(?:data-type="user"[^>]*data-id="(\d+)")|(?:data-id="(\d+)"[^>]*data-type="user")[^>]*>'
+ .freeze
+ MENTION_USER_HASH_ID_PATTERN =
+ '\buser#(\d+)\b'
.freeze
MENTION_USER_LOGIN_PATTERN =
'\buser:"(.+?)"'.freeze
- MENTION_GROUP_ID_PATTERN =
- '<mention[^>]*(?:data-type="group"[^>]*data-id="(\d+)")|(?:data-id="(\d+)"[^>]*data-type="group")[^>]*>)|(?:\bgroup#(\d+)\b'
+ MENTION_GROUP_TAG_ID_PATTERN =
+ '<mention[^>]*(?:data-type="group"[^>]*data-id="(\d+)")|(?:data-id="(\d+)"[^>]*data-type="group")[^>]*>'
+ .freeze
+ MENTION_GROUP_HASH_ID_PATTERN =
+ '\bgroup#(\d+)\b'
.freeze
COMBINED_MENTION_PATTERN =
- "(?:#{MENTION_USER_ID_PATTERN})|(?:#{MENTION_USER_LOGIN_PATTERN})|(?:#{MENTION_GROUP_ID_PATTERN})".freeze
+ [MENTION_USER_TAG_ID_PATTERN,
+ MENTION_USER_HASH_ID_PATTERN,
+ MENTION_USER_LOGIN_PATTERN,
+ MENTION_GROUP_TAG_ID_PATTERN,
+ MENTION_GROUP_HASH_ID_PATTERN]
+ .map { |pattern| "(?:#{pattern})" }
+ .join('|').freeze
# Skip looking for mentions in quoted lines completely.
# We need to allow an optional single white space before the ">", because the `#text_for_mentions`
# method appends a white space to the journal details. With the notes it's not the case.
- NON_QUOTED_LINES = '^(?! ?> ).*'.freeze
- MENTION_PATTERN = Regexp.new("#{NON_QUOTED_LINES}#{COMBINED_MENTION_PATTERN}")
+ QUOTED_LINES_PATTERN = /^ ?> .*$/
+ MENTION_PATTERN = Regexp.new(COMBINED_MENTION_PATTERN)
def initialize(model)
self.model = model
@@ -230,6 +242,10 @@ class Notifications::CreateFromModelService
.where(user: user_scope.where(id: User.allowed(strategy.permission, project)))
end
+ # Returns the text of the model (currently suited to work package description and subject) eligible
+ # to be looked at for mentions of users and groups:
+ # * only lines added
+ # * excluding quoted lines
def text_for_mentions
potential_text = ""
potential_text << journal.notes if journal.try(:notes)
@@ -241,7 +257,8 @@ class Notifications::CreateFromModelService
potential_text << "\n#{Redmine::Helpers::Diff.new(*details.reverse).additions.join(' ')}"
end
end
- potential_text
+
+ potential_text.gsub(QUOTED_LINES_PATTERN, '')
end
def mentioned_ids
diff --git a/spec/services/notifications/create_from_model_service_work_package_spec.rb b/spec/services/notifications/create_from_model_service_work_package_spec.rb
index 85670ff61f3..4dc7b3e2f1f 100644
--- a/spec/services/notifications/create_from_model_service_work_package_spec.rb
+++ b/spec/services/notifications/create_from_model_service_work_package_spec.rb
@@ -817,7 +817,7 @@ describe Notifications::CreateFromModelService,
end
end
- context "when the added text contains a user mention tag in one way" do
+ context "when the added text contains a user mention tag with the attributes in one order" do
let(:note) do
<<~NOTE
Hello <mention class="mention" data-id="#{recipient.id}" data-type="user" data-text="@#{recipient.name}">@#{recipient.name}</mention>
@@ -836,7 +836,7 @@ describe Notifications::CreateFromModelService,
end
end
- context "when the added text contains a user mention tag in the other way" do
+ context "when the added text contains a user mention tag with the attributes in another order" do
let(:note) do
<<~NOTE
Hello <mention class="mention" data-type="user" data-id="#{recipient.id}" data-text="@#{recipient.name}">@#{recipient.name}</mention>
@@ -1038,6 +1038,80 @@ describe Notifications::CreateFromModelService,
end
end
end
+
+ context 'with users and groups' do
+ let(:group_role) { create(:role, permissions: %i[view_work_packages]) }
+ let(:group) do
+ create(:group, members: recipient) do |group|
+ Members::CreateService
+ .new(user: User.system, contract_class: EmptyContract)
+ .call(project:, principal: group, roles: [group_role])
+ end
+ end
+ let(:other_recipient) do
+ create(:user,
+ member_in_project: project,
+ member_with_permissions: permissions,
+ notification_settings: [build(:notification_setting, **notification_settings_all_true)])
+ end
+ let(:notification_group_recipient) { build_stubbed(:notification, recipient:) }
+ let(:notification_other_recipient) { build_stubbed(:notification, recipient: other_recipient) }
+
+ context 'with two tag based mention in the same line' do
+ let(:note) do
+ <<~NOTE.squish
+ Hello
+ <mention class="mention"
+ data-id="#{group.id}"
+ data-type="group"
+ data-text="@#{group.name}">@#{group.name}
+ </mention>
+ <mention class="mention"
+ data-id="#{other_recipient.id}"
+ data-type="user"
+ data-text="@#{other_recipient.name}">@#{other_recipient.name}
+ </mention>,
+ check this.
+ NOTE
+ end
+
+ let(:notification_channel_reasons) do
+ {
+ read_ian: false,
+ reason: :mentioned,
+ mail_alert_sent: false,
+ mail_reminder_sent: false
+ }
+ end
+
+ it 'creates two notification and returns them' do
+ notifications_service = instance_double(Notifications::CreateService)
+
+ allow(Notifications::CreateService)
+ .to receive(:new)
+ .with(user: author)
+ .and_return(notifications_service)
+
+ allow(notifications_service)
+ .to receive(:call) do |args|
+ if args[:recipient_id] == recipient.id &&
+ args.slice(*notification_channel_reasons.keys) == notification_channel_reasons
+ ServiceResult.success(result: notification_group_recipient)
+ elsif args[:recipient_id] == other_recipient.id &&
+ args.slice(*notification_channel_reasons.keys) == notification_channel_reasons
+ ServiceResult.success(result: notification_other_recipient)
+ else
+ expect(true)
+ .to be(false),
+ "Notification::CreateService received unexpected args: #{args.inspect}"
+ end
+ end
+
+ expect(call.all_results)
+ .to match_array([notification_group_recipient, notification_other_recipient])
+ end
+ end
+ end
end
describe 'in the journal notes' do