diff --git a/app/logical/concerns/mentionable.rb b/app/logical/concerns/mentionable.rb index f0a8e16ee..fca768bec 100644 --- a/app/logical/concerns/mentionable.rb +++ b/app/logical/concerns/mentionable.rb @@ -28,12 +28,14 @@ module Mentionable text_was = send(:attribute_before_last_save, message_field) names = DText.parse_mentions(text) - DText.parse_mentions(text_was) + users = names.map { |name| User.find_by_name(name) }.uniq + users = users.without(CurrentUser.user) - names.uniq.each do |name| - body = self.instance_exec(name, &self.class.mentionable_option(:body)) - title = self.instance_exec(name, &self.class.mentionable_option(:title)) + users.each do |user| + body = self.instance_exec(user.name, &self.class.mentionable_option(:body)) + title = self.instance_exec(user.name, &self.class.mentionable_option(:title)) - Dmail.create_automated(to_name: name, title: title, body: body) + Dmail.create_automated(to: user, title: title, body: body) end end end diff --git a/test/unit/forum_post_test.rb b/test/unit/forum_post_test.rb index 9e7e41c2f..a6d6eb224 100644 --- a/test/unit/forum_post_test.rb +++ b/test/unit/forum_post_test.rb @@ -60,6 +60,12 @@ class ForumPostTest < ActiveSupport::TestCase EOS end end + + should "not send a mention to yourself" do + assert_no_difference("Dmail.count") do + @forum_post = as(@user) { create(:forum_post, body: "hi from @#{@user.name}") } + end + end end context "that belongs to a topic with several pages of posts" do