From 2ce98b49d75cefa2d8edccdffffd16155bbf0532 Mon Sep 17 00:00:00 2001 From: Augier Date: Tue, 14 Oct 2014 21:01:33 +0200 Subject: [PATCH] Fix issue #4416 Conflicts: spec/helpers/notifier_helper_spec.rb --- Gemfile | 2 +- Gemfile.lock | 6 +++--- app/models/services/facebook.rb | 2 +- app/models/status_message.rb | 4 ++-- lib/diaspora/markdownify/html.rb | 2 +- lib/diaspora/message_renderer.rb | 6 ++++++ spec/helpers/notifier_helper_spec.rb | 7 +++---- spec/lib/diaspora/message_renderer_spec.rb | 9 ++++++++- spec/models/services/facebook_spec.rb | 4 ++-- 9 files changed, 27 insertions(+), 15 deletions(-) diff --git a/Gemfile b/Gemfile index 1391fb5a7..02d4ff733 100644 --- a/Gemfile +++ b/Gemfile @@ -105,8 +105,8 @@ gem 'messagebus_ruby_api', '1.0.3' # Parsing gem 'nokogiri', '1.6.1' -gem 'rails_autolink', '1.1.6' gem 'redcarpet', '3.2.0' +gem 'twitter-text', '1.9.2' gem 'roxml', '3.1.6' gem 'ruby-oembed', '0.8.10' gem 'opengraph_parser', '0.2.3' diff --git a/Gemfile.lock b/Gemfile.lock index efd3b0152..27c859367 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -399,8 +399,6 @@ GEM remotipart (~> 1.0) safe_yaml (~> 1.0) sass-rails (~> 4.0) - rails_autolink (1.1.6) - rails (> 3.1) railties (4.1.6) actionpack (= 4.1.6) activesupport (= 4.1.6) @@ -498,6 +496,8 @@ GEM faraday (~> 0.8, < 0.10) multi_json (~> 1.0) simple_oauth (~> 0.2) + twitter-text (1.9.2) + unf (~> 0.1.0) typhoeus (0.6.9) ethon (>= 0.7.1) tzinfo (1.2.2) @@ -601,7 +601,6 @@ DEPENDENCIES rails-i18n (= 4.0.3) rails-timeago (= 2.11.0) rails_admin (= 0.6.5) - rails_autolink (= 1.1.6) rb-fsevent (= 0.9.4) rb-inotify (= 0.9.5) redcarpet (= 3.2.0) @@ -620,6 +619,7 @@ DEPENDENCIES test_after_commit (= 0.3.0) timecop (= 0.7.1) twitter (= 4.8.1) + twitter-text (= 1.9.2) typhoeus (= 0.6.9) uglifier (= 2.5.3) unicorn (= 4.8.3) diff --git a/app/models/services/facebook.rb b/app/models/services/facebook.rb index f49fd2e51..65d1006c3 100644 --- a/app/models/services/facebook.rb +++ b/app/models/services/facebook.rb @@ -29,7 +29,7 @@ class Services::Facebook < Service {message: message, access_token: access_token, - link: URI.extract(message, ['https', 'http']).first + link: post.message.urls.first } end diff --git a/app/models/status_message.rb b/app/models/status_message.rb index fa04daf4d..5d857c640 100644 --- a/app/models/status_message.rb +++ b/app/models/status_message.rb @@ -148,13 +148,13 @@ class StatusMessage < Post end def contains_oembed_url_in_text? - urls = URI.extract(self.raw_message, ['http', 'https']) + urls = self.message.urls self.oembed_url = urls.find{ |url| !TRUSTED_OEMBED_PROVIDERS.find(url).nil? } end def contains_open_graph_url_in_text? return nil if self.contains_oembed_url_in_text? - self.open_graph_url = URI.extract(self.raw_message, ['http', 'https'])[0] + self.open_graph_url = self.message.urls[0] end def address diff --git a/lib/diaspora/markdownify/html.rb b/lib/diaspora/markdownify/html.rb index 60209dcd6..04b2e8f16 100644 --- a/lib/diaspora/markdownify/html.rb +++ b/lib/diaspora/markdownify/html.rb @@ -4,7 +4,7 @@ module Diaspora include ActionView::Helpers::TextHelper def autolink link, type - auto_link(link, link: :urls, html: { target: "_blank" }) + Twitter::Autolink.auto_link_urls(link, url_target: "_blank") end end end diff --git a/lib/diaspora/message_renderer.rb b/lib/diaspora/message_renderer.rb index 66a6bfd1c..3714e4487 100644 --- a/lib/diaspora/message_renderer.rb +++ b/lib/diaspora/message_renderer.rb @@ -213,6 +213,12 @@ module Diaspora end end + # Extracts all the urls from the raw message and return them in the form of a string + # Different URLs are seperated with a space + def urls + @urls ||= Twitter::Extractor.extract_urls(@raw_message) + end + def raw @raw_message end diff --git a/spec/helpers/notifier_helper_spec.rb b/spec/helpers/notifier_helper_spec.rb index 2edd221cd..52697a7c1 100644 --- a/spec/helpers/notifier_helper_spec.rb +++ b/spec/helpers/notifier_helper_spec.rb @@ -8,12 +8,11 @@ describe NotifierHelper, :type => :helper do describe '#post_message' do before do # post for truncate test - @post = FactoryGirl.create(:status_message) - @post.text = "hi dude! "*10 + @post = FactoryGirl.create(:status_message, text: "hi dude! "*10) @truncated_post = "hi dude! hi dude! hi dude! hi dude! hi dude! hi dude! hi dude! hi dude! hi du..." # post for markdown test - @markdown_post = FactoryGirl.create(:status_message) - @markdown_post.text = "[link](http://diasporafoundation.org) **bold text** *other text*" + @markdown_post = FactoryGirl.create(:status_message, + text: "[link](http://diasporafoundation.org) **bold text** *other text*") @striped_markdown_post = "link (http://diasporafoundation.org) bold text other text" end diff --git a/spec/lib/diaspora/message_renderer_spec.rb b/spec/lib/diaspora/message_renderer_spec.rb index a0b428250..129415d63 100644 --- a/spec/lib/diaspora/message_renderer_spec.rb +++ b/spec/lib/diaspora/message_renderer_spec.rb @@ -147,7 +147,7 @@ describe Diaspora::MessageRenderer do it 'should process text with both a hashtag and a link' do expect( message("Test #tag?\nhttps://joindiaspora.com\n").markdownified - ).to eq %{

Test #tag?
\nhttps://joindiaspora.com

\n} + ).to eq %{

Test #tag?
\nhttps://joindiaspora.com

\n} end it 'should process text with a header' do @@ -172,4 +172,11 @@ describe Diaspora::MessageRenderer do expect(message(text).plain_text_without_markdown).to eq text end end + + describe "#urls" do + it "extracts the urls from the raw message" do + text = "[Perdu](http://perdu.com/) and [DuckDuckGo](https://duckduckgo.com/) can help you" + expect(message(text).urls).to eql ["http://perdu.com/", "https://duckduckgo.com/"] + end + end end diff --git a/spec/models/services/facebook_spec.rb b/spec/models/services/facebook_spec.rb index adb4fc8bc..022a42e24 100644 --- a/spec/models/services/facebook_spec.rb +++ b/spec/models/services/facebook_spec.rb @@ -25,7 +25,7 @@ describe Services::Facebook, :type => :model do end it 'removes text formatting markdown from post text' do - message = double + message = double(urls: []) expect(message).to receive(:plain_text_without_markdown).and_return("") post = double(message: message, photos: []) post_params = @service.create_post_params(post) @@ -33,7 +33,7 @@ describe Services::Facebook, :type => :model do it 'does not add post link when no photos' do message = "Some text." - post = double(message: double(plain_text_without_markdown: message), photos: []) + post = double(message: double(plain_text_without_markdown: message, urls: []), photos: []) post_params = @service.create_post_params(post) expect(post_params[:message]).not_to include "http" end