From 421d47da1ed626913810baafe7ed3de7d9bf717b Mon Sep 17 00:00:00 2001
From: Penar Musaraj <pmusaraj@gmail.com>
Date: Tue, 29 Jan 2019 21:54:29 -0500
Subject: [PATCH] FIX: user and group mentions in subfolder installs

---
 lib/pretty_text.rb                  |  4 ++--
 spec/components/pretty_text_spec.rb | 17 +++++++++++++++++
 2 files changed, 19 insertions(+), 2 deletions(-)

diff --git a/lib/pretty_text.rb b/lib/pretty_text.rb
index 249196fcd13..bad230f57d6 100644
--- a/lib/pretty_text.rb
+++ b/lib/pretty_text.rb
@@ -440,10 +440,10 @@ module PrettyText
 
         case type
         when USER_TYPE
-          element['href'] = "/u/#{name}"
+          element['href'] = "#{Discourse::base_uri}/u/#{name}"
         when GROUP_TYPE
           element['class'] = 'mention-group'
-          element['href'] = "/groups/#{name}"
+          element['href'] = "#{Discourse::base_uri}/groups/#{name}"
         end
       end
     end
diff --git a/spec/components/pretty_text_spec.rb b/spec/components/pretty_text_spec.rb
index 1e0cfe9c73c..1490e38b3ac 100644
--- a/spec/components/pretty_text_spec.rb
+++ b/spec/components/pretty_text_spec.rb
@@ -265,6 +265,23 @@ describe PrettyText do
       end
     end
 
+    context 'subfolder' do
+      before do
+        GlobalSetting.stubs(:relative_url_root).returns('/forum')
+        Discourse.stubs(:base_uri).returns("/forum")
+      end
+
+      it "handles user and group mentions correctly" do
+        Fabricate(:user, username: 'user1')
+        Fabricate(:group, name: 'groupA', mentionable_level: Group::ALIAS_LEVELS[:everyone])
+
+        input = 'hi there @user1 and @groupA'
+        expected = '<p>hi there <a class="mention" href="/forum/u/user1">@user1</a> and <a class="mention-group" href="/forum/groups/groupa">@groupA</a></p>'
+
+        expect(PrettyText.cook(input)).to eq(expected)
+      end
+    end
+
     it "does not create mention for a non mentionable group" do
       group = Fabricate(:group, mentionable_level: Group::ALIAS_LEVELS[:nobody])