diff --git a/lib/diaspora/message_renderer.rb b/lib/diaspora/message_renderer.rb index bf36efde2d82bd68a0d9547ee91af35cf2b8cb72..38bdf58630a6d05cf5e78e6c8049fbff7e4be9cf 100644 --- a/lib/diaspora/message_renderer.rb +++ b/lib/diaspora/message_renderer.rb @@ -95,6 +95,12 @@ module Diaspora def normalize @message = self.class.normalize(@message) end + + def diaspora_links + @message = @message.gsub(DiasporaFederation::Federation::DiasporaUrlParser::DIASPORA_URL_REGEX) {|match_str| + Regexp.last_match(2) == "post" ? AppConfig.url_to("/posts/#{Regexp.last_match(3)}") : match_str + } + end end DEFAULTS = {mentioned_people: [], @@ -158,6 +164,7 @@ module Diaspora def plain_text opts={} process(opts) { make_mentions_plain_text + diaspora_links squish append_and_truncate } @@ -167,6 +174,7 @@ module Diaspora def plain_text_without_markdown opts={} process(opts) { make_mentions_plain_text + diaspora_links strip_markdown squish append_and_truncate @@ -177,6 +185,7 @@ module Diaspora def plain_text_for_json opts={} process(opts) { normalize + diaspora_links camo_urls if AppConfig.privacy.camo.proxy_markdown_images? } end @@ -186,6 +195,7 @@ module Diaspora process(opts) { escape normalize + diaspora_links render_mentions render_tags squish @@ -198,6 +208,7 @@ module Diaspora process(opts) { process_newlines normalize + diaspora_links camo_urls if AppConfig.privacy.camo.proxy_markdown_images? markdownify render_mentions diff --git a/spec/lib/diaspora/message_renderer_spec.rb b/spec/lib/diaspora/message_renderer_spec.rb index 700e3c3487bf6896853bc3099798ab98ef5cdb99..7789e243e852c3e0f5d006292e2f5e64b817449f 100644 --- a/spec/lib/diaspora/message_renderer_spec.rb +++ b/spec/lib/diaspora/message_renderer_spec.rb @@ -1,11 +1,6 @@ # frozen_string_literal: true describe Diaspora::MessageRenderer do - MESSAGE_NORMALIZTIONS = { - "\u202a#\u200eUSA\u202c" => "#USA", - "à´³àµâ€" => "à´³àµâ€" - } - def message(text, opts={}) Diaspora::MessageRenderer.new(text, opts) end @@ -100,6 +95,20 @@ describe Diaspora::MessageRenderer do end end end + + context "with diaspora:// links" do + it "replaces diaspora:// links with pod-local links" do + target = FactoryGirl.create(:status_message) + expect( + message("Have a look at diaspora://#{target.diaspora_handle}/post/#{target.guid}.").html + ).to match(/Have a look at #{AppConfig.url_to("/posts/#{target.guid}")}./) + end + + it "doesn't touch invalid diaspora:// links" do + text = "You can create diaspora://author/type/guid links!" + expect(message(text).html).to match(/#{text}/) + end + end end describe "#markdownified" do @@ -128,8 +137,11 @@ describe Diaspora::MessageRenderer do end it "normalizes" do - MESSAGE_NORMALIZTIONS.each do |input, output| - expect(message(input).plain_text_for_json).to eq output + { + "\u202a#\u200eUSA\u202c" => "<p><a class=\"tag\" href=\"/tags/USA\">#USA</a></p>\n", + "à´³àµâ€" => "<p>à´³àµâ€</p>\n" + }.each do |input, output| + expect(message(input).markdownified).to eq output end end @@ -180,6 +192,25 @@ describe Diaspora::MessageRenderer do entities = '& ß ' ' "' expect(message(entities).markdownified).to eq "<p>#{entities}</p>\n" end + + context "with diaspora:// links" do + it "replaces diaspora:// links with pod-local links" do + target1 = FactoryGirl.create(:status_message) + target2 = FactoryGirl.create(:status_message) + text = "Have a look at [this post](diaspora://#{target1.diaspora_handle}/post/#{target1.guid}) and " \ + "this one too diaspora://#{target2.diaspora_handle}/post/#{target2.guid}." + + rendered = message(text).markdownified + + expect(rendered).to match(%r{at <a href="#{AppConfig.url_to("/posts/#{target1.guid}")}">this post</a> and}) + expect(rendered).to match(/this one too #{AppConfig.url_to("/posts/#{target2.guid}")}./) + end + + it "doesn't touch invalid diaspora:// links" do + text = "You can create diaspora://author/type/guid links!" + expect(message(text).markdownified).to match(/#{text}/) + end + end end end @@ -210,6 +241,25 @@ describe Diaspora::MessageRenderer do expect(msg.plain_text_without_markdown).to eq "@#{alice.diaspora_handle} is cool" end end + + context "with diaspora:// links" do + it "replaces diaspora:// links with pod-local links" do + target1 = FactoryGirl.create(:status_message) + target2 = FactoryGirl.create(:status_message) + text = "Have a look at [this post](diaspora://#{target1.diaspora_handle}/post/#{target1.guid}) and " \ + "this one too diaspora://#{target2.diaspora_handle}/post/#{target2.guid}." + + rendered = message(text).plain_text_without_markdown + + expect(rendered).to match(/look at this post \(#{AppConfig.url_to("/posts/#{target1.guid}")}\) and/) + expect(rendered).to match(/this one too #{AppConfig.url_to("/posts/#{target2.guid}")}./) + end + + it "doesn't touch invalid diaspora:// links" do + text = "You can create diaspora://author/type/guid links!" + expect(message(text).plain_text_without_markdown).to match(/#{text}/) + end + end end describe "#urls" do @@ -241,9 +291,31 @@ describe Diaspora::MessageRenderer do describe "#plain_text_for_json" do it "normalizes" do - MESSAGE_NORMALIZTIONS.each do |input, output| + { + "\u202a#\u200eUSA\u202c" => "#USA", + "à´³àµâ€" => "à´³àµâ€" + }.each do |input, output| expect(message(input).plain_text_for_json).to eq output end end + + context "with diaspora:// links" do + it "replaces diaspora:// links with pod-local links" do + target1 = FactoryGirl.create(:status_message) + target2 = FactoryGirl.create(:status_message) + text = "Have a look at [this post](diaspora://#{target1.diaspora_handle}/post/#{target1.guid}) and " \ + "this one too diaspora://#{target2.diaspora_handle}/post/#{target2.guid}." + + rendered = message(text).plain_text_for_json + + expect(rendered).to match(/look at \[this post\]\(#{AppConfig.url_to("/posts/#{target1.guid}")}\) and/) + expect(rendered).to match(/this one too #{AppConfig.url_to("/posts/#{target2.guid}")}./) + end + + it "doesn't touch invalid diaspora:// links" do + text = "You can create diaspora://author/type/guid links!" + expect(message(text).plain_text_for_json).to match(/#{text}/) + end + end end end