From 8a63fb0f7c2c758f28f32d1a83a6d85221eb1b0a Mon Sep 17 00:00:00 2001 From: r888888888 Date: Mon, 30 Nov 2015 16:51:07 -0800 Subject: [PATCH] enable mention for comments #2466 --- app/assets/javascripts/autocomplete.js | 2 +- app/models/comment.rb | 8 +++ app/models/forum_post.rb | 2 +- .../comments/partials/new/_form.html.erb | 2 +- .../comments/partials/show/_comment.html.erb | 1 + test/unit/comment_test.rb | 49 +++++++++++++++++++ 6 files changed, 61 insertions(+), 3 deletions(-) diff --git a/app/assets/javascripts/autocomplete.js b/app/assets/javascripts/autocomplete.js index 272582088..0b92c65bb 100644 --- a/app/assets/javascripts/autocomplete.js +++ b/app/assets/javascripts/autocomplete.js @@ -37,7 +37,7 @@ } Danbooru.Autocomplete.initialize_mention_autocomplete = function() { - var $fields = $("#forum_post_body"); + var $fields = $("#forum_post_body,.comment-form textarea"); $fields.autocomplete({ delay: 500, minLength: 2, diff --git a/app/models/comment.rb b/app/models/comment.rb index 3061d17f5..30afa6ff1 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -1,4 +1,6 @@ class Comment < ActiveRecord::Base + include Mentionable + validate :validate_creator_is_not_limited, :on => :create validates_format_of :body, :with => /\S/, :message => 'has no content' belongs_to :post @@ -10,6 +12,12 @@ class Comment < ActiveRecord::Base after_create :update_last_commented_at_on_create after_destroy :update_last_commented_at_on_destroy attr_accessible :body, :post_id, :do_not_bump_post, :is_deleted + mentionable( + :message_field => :body, + :user_field => :creator_id, + :title => "You were mentioned in a comment", + :body => lambda {|rec, user_name| "You were mentioned in a \"comment\":http://#{Danbooru.config.hostname}/posts/#{rec.post_id}#comment-#{rec.id}\n\n---\n\n[i]#{rec.creator.name} said:[/i]\n\n#{ActionController::Base.helpers.excerpt(rec.body, user_name)}"} + ) module SearchMethods def recent diff --git a/app/models/forum_post.rb b/app/models/forum_post.rb index d4f9ef9aa..5dd003a94 100644 --- a/app/models/forum_post.rb +++ b/app/models/forum_post.rb @@ -22,7 +22,7 @@ class ForumPost < ActiveRecord::Base :message_field => :body, :user_field => :creator_id, :title => "You were mentioned in a forum topic", - :body => lambda {|rec, user_name| "You were mentioned in the forum topic \"#{rec.topic.title}\":http://#{Danbooru.config.hostname}/forum_topics/#{rec.topic_id}?page=#{rec.forum_topic_page}\n\n---\n\n#{ActionController::Base.helpers.excerpt(rec.body, user_name)}"} + :body => lambda {|rec, user_name| "You were mentioned in the forum topic \"#{rec.topic.title}\":http://#{Danbooru.config.hostname}/forum_topics/#{rec.topic_id}?page=#{rec.forum_topic_page}\n\n---\n\n[i]#{rec.creator.name} said:[/i]\n\n#{ActionController::Base.helpers.excerpt(rec.body, user_name)}"} ) module SearchMethods diff --git a/app/views/comments/partials/new/_form.html.erb b/app/views/comments/partials/new/_form.html.erb index 1b649f3a5..59978a6cd 100644 --- a/app/views/comments/partials/new/_form.html.erb +++ b/app/views/comments/partials/new/_form.html.erb @@ -1,7 +1,7 @@
-<%= form_tag(comments_path, :class => "simple_form") do %> +<%= form_tag(comments_path, :class => "simple_form comment-form") do %> <%= hidden_field "comment", "post_id", :value => post.id %> <%= dtext_field "comment", "body", :input_id => "comment_response_for_#{post.id}", :preview_id => "dtext-preview-for-#{post.id}" %> <%= submit_tag "Post", :data => { :disable_with => "Submitting..." } %> diff --git a/app/views/comments/partials/show/_comment.html.erb b/app/views/comments/partials/show/_comment.html.erb index e63efed59..942f0c82a 100644 --- a/app/views/comments/partials/show/_comment.html.erb +++ b/app/views/comments/partials/show/_comment.html.erb @@ -1,4 +1,5 @@ <% if CurrentUser.is_moderator? || !comment.is_deleted? %> +

diff --git a/test/unit/comment_test.rb b/test/unit/comment_test.rb index 50682dc25..46ec5f858 100644 --- a/test/unit/comment_test.rb +++ b/test/unit/comment_test.rb @@ -14,6 +14,55 @@ class CommentTest < ActiveSupport::TestCase CurrentUser.ip_addr = nil end + + context "that mentions a user" do + setup do + @post = FactoryGirl.create(:post) + Danbooru.config.stubs(:member_comment_limit).returns(100) + Danbooru.config.stubs(:member_comment_time_threshold).returns(1.week.from_now) + end + + context "in a quote block" do + setup do + @user2 = FactoryGirl.create(:user, :created_at => 2.weeks.ago) + end + + should "not create a dmail" do + assert_difference("Dmail.count", 0) do + FactoryGirl.create(:comment, :post_id => @post.id, :body => "[quote]@#{@user2.name}[/quote]") + end + + assert_difference("Dmail.count", 0) do + FactoryGirl.create(:comment, :post_id => @post.id, :body => "[quote]@#{@user2.name}[/quote] blah [quote]@#{@user2.name}[/quote]") + end + + assert_difference("Dmail.count", 0) do + FactoryGirl.create(:comment, :post_id => @post.id, :body => "[quote][quote]@#{@user2.name}[/quote][/quote]") + end + + assert_difference("Dmail.count", 1) do + FactoryGirl.create(:comment, :post_id => @post.id, :body => "[quote]@#{@user2.name}[/quote] @#{@user2.name}") + end + end + end + + context "outside a quote block" do + setup do + @user2 = FactoryGirl.create(:user) + @comment = FactoryGirl.build(:comment, :post_id => @post.id, :body => "Hey @#{@user2.name} check this out!") + end + + should "create a dmail" do + assert_difference("Dmail.count", 1) do + @comment.save + end + + dmail = Dmail.last + assert_equal("You were mentioned in a \"comment\":http://#{Danbooru.config.hostname}/posts/#{@comment.post_id}#comment-#{@comment.id}\n\n---\n\n[i]#{CurrentUser.name} said:[/i]\n\nHey @#{@user2.name} check this out!", dmail.body) + end + end + end + context "created by a limited user" do setup do Danbooru.config.stubs(:member_comment_limit).returns(5)