From f38289795250f84fb2cb7e81ed0037780c08f7fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Tue, 5 Apr 2016 00:43:11 +0200 Subject: [PATCH] FIX: don't extract link with 'mailto' scheme --- app/models/topic_link.rb | 14 +++++++------- spec/models/topic_link_spec.rb | 9 +++++++++ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/app/models/topic_link.rb b/app/models/topic_link.rb index aafaff14b..193ccbc8c 100644 --- a/app/models/topic_link.rb +++ b/app/models/topic_link.rb @@ -110,25 +110,25 @@ class TopicLink < ActiveRecord::Base PrettyText .extract_links(post.cooked) - .map{|u| [u, URI.parse(u.url)] rescue nil} - .reject{|_, p| p.nil?} - .uniq{|_, p| p} + .map { |u| [u, URI.parse(u.url)] rescue nil } + .reject { |_, p| p.nil? || "mailto".freeze == p.scheme } + .uniq { |_, p| p } .each do |link, parsed| begin - url = link.url internal = false topic_id = nil post_number = nil + parsed_path = parsed.path || "" if Discourse.store.has_been_uploaded?(url) internal = Discourse.store.internal? - elsif (parsed.host == Discourse.current_hostname && parsed.path.start_with?(Discourse.base_uri)) || !parsed.host + elsif (parsed.host == Discourse.current_hostname && parsed_path.start_with?(Discourse.base_uri)) || !parsed.host internal = true - parsed.path.slice!(Discourse.base_uri) + parsed_path.slice!(Discourse.base_uri) - route = Rails.application.routes.recognize_path(parsed.path) + route = Rails.application.routes.recognize_path(parsed_path) # We aren't interested in tracking internal links to users next if route[:controller] == 'users' diff --git a/spec/models/topic_link_spec.rb b/spec/models/topic_link_spec.rb index a8338311b..483b4eebf 100644 --- a/spec/models/topic_link_spec.rb +++ b/spec/models/topic_link_spec.rb @@ -164,6 +164,15 @@ http://b.com/#{'a'*500} end end + context "mail link" do + let(:post) { topic.posts.create(user: user, raw: "[email]bar@example.com[/email]") } + + it 'does not extract a link' do + TopicLink.extract_from(post) + expect(topic.topic_links).to be_blank + end + end + context "quote links" do it "sets quote correctly" do linked_post = topic.posts.create(user: user, raw: "my test post")