From c372e36cb627c079412bf7d98e622bf74446a9aa Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Mon, 25 Mar 2013 13:12:15 -0400 Subject: [PATCH] Don't count @mentions as links in a post --- app/models/post.rb | 12 +++++++++++- spec/models/post_spec.rb | 11 ++++++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/app/models/post.rb b/app/models/post.rb index a340590b8..21bf9fe7d 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -110,7 +110,17 @@ class Post < ActiveRecord::Base def link_count return 0 unless raw.present? - cooked_document.search("a[href]").count + + # Don't include @mentions in the link count + total = 0 + cooked_document.search("a[href]").each do |l| + html_class = l.attributes['class'] + if html_class.present? + next if html_class.to_s == 'mention' && l.attributes['href'].to_s =~ /^\/users\// + end + total +=1 + end + total end def max_mention_validator diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb index efa6edf41..91758c8d3 100644 --- a/spec/models/post_spec.rb +++ b/spec/models/post_spec.rb @@ -149,16 +149,21 @@ describe Post do let(:visitor) { Fabricate(:user, trust_level: TrustLevel.levels[:visitor]) } let(:post_one_link) { Fabricate.build(:post, post_args.merge(raw: "[sherlock](http://www.bbc.co.uk/programmes/b018ttws)", user: visitor)) } let(:post_two_links) { Fabricate.build(:post, post_args.merge(raw: "discourse twitter", user: visitor)) } + let(:post_with_mentions) { Fabricate.build(:post, post_args.merge(raw: "hello @#{visitor.username} how are you doing?") )} - it "returns 0 images for an empty post" do + it "returns 0 links for an empty post" do Fabricate.build(:post).link_count.should == 0 end - it "finds images from markdown" do + it "returns 0 links for a post with mentions" do + post_with_mentions.link_count.should == 0 + end + + it "finds links from markdown" do post_one_link.link_count.should == 1 end - it "finds images from HTML" do + it "finds links from HTML" do post_two_links.link_count.should == 2 end