From fdad3488986f633ffc3d5a0d10c3929b9ba06bd5 Mon Sep 17 00:00:00 2001
From: Steffen van Bergerem <svbergerem@online.de>
Date: Sat, 30 May 2015 03:58:04 +0200
Subject: [PATCH] Fix tag rendering in emails

closes #6009
---
 Changelog.md                                |  1 +
 lib/diaspora/markdownify/email.rb           | 20 +------
 lib/diaspora/taggable.rb                    |  9 ++++
 spec/lib/diaspora/markdownify_email_spec.rb |  6 +--
 spec/lib/diaspora/taggable_spec.rb          | 59 +++++++++++++++++++++
 5 files changed, 73 insertions(+), 22 deletions(-)
 create mode 100644 spec/lib/diaspora/taggable_spec.rb

diff --git a/Changelog.md b/Changelog.md
index 1a9f3c307d..92e20ed021 100644
--- a/Changelog.md
+++ b/Changelog.md
@@ -46,6 +46,7 @@
 * Add case insensitive unconfirmed email addresses as authentication key [#5967](https://github.com/diaspora/diaspora/pull/5967)
 * Fix liking on single post views when accessed via GUID [#5978](https://github.com/diaspora/diaspora/pull/5978)
 * Only return the current_users participation for post interactions [#6007](https://github.com/diaspora/diaspora/pull/6007)
+* Fix tag rendering in emails [#6009](https://github.com/diaspora/diaspora/pull/6009)
 
 ## Features
 * Hide post title of limited post in comment notification email [#5843](https://github.com/diaspora/diaspora/pull/5843)
diff --git a/lib/diaspora/markdownify/email.rb b/lib/diaspora/markdownify/email.rb
index 1e3263127d..f5d2dd2909 100644
--- a/lib/diaspora/markdownify/email.rb
+++ b/lib/diaspora/markdownify/email.rb
@@ -1,26 +1,8 @@
 module Diaspora
   module Markdownify
     class Email < Redcarpet::Render::HTML
-      include Rails.application.routes.url_helpers
-      TAG_REGEX = /(?:^|\s)#([#{ActsAsTaggableOn::Tag.tag_text_regexp}]+)/u
       def preprocess(text)
-        process_tags(text)
-      end
-
-      private
-      def tags(text)
-        text.scan(TAG_REGEX).map { |match| match[0] }
-      end
-
-      def process_tags(text)
-        return text unless text.match(TAG_REGEX)
-        tags(text).each do |tag|
-          text.gsub!(/##{tag}/) do |tag|
-            opts = {:name => ActsAsTaggableOn::Tag.normalize(tag)}.merge(Rails.application.config.action_mailer.default_url_options)
-            "[#{tag}](#{tag_url(opts)})"
-          end
-        end
-        text
+        Diaspora::Taggable.format_tags_for_mail text
       end
     end
   end
diff --git a/lib/diaspora/taggable.rb b/lib/diaspora/taggable.rb
index 3e340df5d8..88d4f3290e 100644
--- a/lib/diaspora/taggable.rb
+++ b/lib/diaspora/taggable.rb
@@ -58,5 +58,14 @@ module Diaspora
         %{#{pre}<a class="tag" href="/tags/#{url_bit}">#{clickable}</a>}
       }.html_safe
     end
+
+    def self.format_tags_for_mail(text)
+      regex = /(?<=^|\s|>)#([#{ActsAsTaggableOn::Tag.tag_text_regexp}]+|<3)/u
+      text.gsub(regex) do |tag|
+        opts = {name: ActsAsTaggableOn::Tag.normalize(tag)}
+               .merge(Rails.application.config.action_mailer.default_url_options)
+        "[#{tag}](#{Rails.application.routes.url_helpers.tag_url(opts)})"
+      end
+    end
   end
 end
diff --git a/spec/lib/diaspora/markdownify_email_spec.rb b/spec/lib/diaspora/markdownify_email_spec.rb
index 6a64f7f1db..6c4b69e172 100644
--- a/spec/lib/diaspora/markdownify_email_spec.rb
+++ b/spec/lib/diaspora/markdownify_email_spec.rb
@@ -12,8 +12,8 @@ describe Diaspora::Markdownify::Email do
     end
 
     it 'should autolink multiple hashtags' do
-      markdownified = @html.preprocess("There are #two #Tags")
-      expect(markdownified).to eq("There are [#two](http://localhost:9887/tags/two) [#Tags](http://localhost:9887/tags/tags)")
+      markdownified = @html.preprocess("oh #l #loL")
+      expect(markdownified).to eq("oh [#l](http://localhost:9887/tags/l) [#loL](http://localhost:9887/tags/lol)")
     end
 
     it 'should not autolink headers' do
@@ -33,4 +33,4 @@ describe Diaspora::Markdownify::Email do
       expect(rendered).to eq("<h1>Header</h1>\n\n<p><a href=\"http://localhost:9887/tags/messages\">#messages</a> containing <a href=\"http://localhost:9887/tags/hashtags\">#hashtags</a> should render properly</p>")
     end
   end
-end
\ No newline at end of file
+end
diff --git a/spec/lib/diaspora/taggable_spec.rb b/spec/lib/diaspora/taggable_spec.rb
new file mode 100644
index 0000000000..bd4ab617f5
--- /dev/null
+++ b/spec/lib/diaspora/taggable_spec.rb
@@ -0,0 +1,59 @@
+require "spec_helper"
+
+describe Diaspora::Taggable do
+  describe "#format_tags" do
+    context "when there are no tags in the text" do
+      it "returns the input text" do
+        text = Diaspora::Taggable.format_tags("There are no tags.")
+        expect(text).to eq("There are no tags.")
+      end
+    end
+
+    context "when there is a tag in the text" do
+      it "autolinks the hashtag" do
+        text = Diaspora::Taggable.format_tags("There is a #hashtag.")
+        expect(text).to eq("There is a <a class=\"tag\" href=\"/tags/hashtag\">#hashtag</a>.")
+      end
+
+      it "autolinks #<3" do
+        text = Diaspora::Taggable.format_tags("#<3")
+        expect(text).to eq("<a class=\"tag\" href=\"/tags/<3\">#&lt;3</a>")
+      end
+    end
+
+    context "with multiple tags" do
+      it "autolinks the hashtags" do
+        text = Diaspora::Taggable.format_tags("#l #lol")
+        expect(text).to eq("<a class=\"tag\" href=\"/tags/l\">#l</a> <a class=\"tag\" href=\"/tags/lol\">#lol</a>")
+      end
+    end
+  end
+
+  describe "#format_tags_for_mail" do
+    context "when there are no tags in the text" do
+      it "returns the input text" do
+        text = Diaspora::Taggable.format_tags_for_mail("There are no tags.")
+        expect(text).to eq("There are no tags.")
+      end
+    end
+
+    context "when there is a tag in the text" do
+      it "autolinks and normalizes the hashtag" do
+        text = Diaspora::Taggable.format_tags_for_mail("There is a #hashTag.")
+        expect(text).to eq("There is a [#hashTag](http://localhost:9887/tags/hashtag).")
+      end
+
+      it "autolinks #<3" do
+        text = Diaspora::Taggable.format_tags_for_mail("#<3")
+        expect(text).to eq("[#<3](http://localhost:9887/tags/%3C3)")
+      end
+    end
+
+    context "with multiple tags" do
+      it "autolinks the hashtags" do
+        text = Diaspora::Taggable.format_tags_for_mail("#l #lol")
+        expect(text).to eq("[#l](http://localhost:9887/tags/l) [#lol](http://localhost:9887/tags/lol)")
+      end
+    end
+  end
+end
-- 
GitLab