From 3f16fe3d8066776d7c89099a367264bfdf6b5894 Mon Sep 17 00:00:00 2001 From: evazion Date: Wed, 3 Feb 2021 23:46:59 -0600 Subject: [PATCH] Fix #4680: @-ing yourself sends you a DMail. Don't send a dmail when the user @-mentions themselves, whether in an edit or in the original message. --- app/logical/concerns/mentionable.rb | 10 ++++++---- test/unit/forum_post_test.rb | 6 ++++++ 2 files changed, 12 insertions(+), 4 deletions(-) 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