From 65a14ccc2cdd602faf8b1c1b4553736d4f2a8df5 Mon Sep 17 00:00:00 2001 From: Andrew Smith Date: Sat, 10 Aug 2013 14:40:53 -0700 Subject: [PATCH] Fixed problem where the add_post_link method would truncate the status update in the middle of a link. This led to sending Twitter broken links. Refactored :extract_truncated_link method Removed complicated while loop in avoiding cutting off links, using refactoring from MrZYX --- app/models/services/twitter.rb | 22 ++++++++++++++++++++-- spec/models/services/twitter_spec.rb | 18 ++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/app/models/services/twitter.rb b/app/models/services/twitter.rb index 0df15d9d8..3e264e4ca 100644 --- a/app/models/services/twitter.rb +++ b/app/models/services/twitter.rb @@ -4,6 +4,8 @@ class Services::Twitter < Service MAX_CHARACTERS = 140 SHORTENED_URL_LENGTH = 21 + + LINK_PATTERN = %r{https?://\S+} def provider "twitter" @@ -46,8 +48,10 @@ class Services::Twitter < Service :protocol => AppConfig.pod_uri.scheme, :host => AppConfig.pod_uri.authority ) - truncated = truncate(post_text, :length => (maxchars - (SHORTENED_URL_LENGTH+1) )) - post_text = "#{truncated} #{post_url}" + truncated_text = truncate post_text, length: maxchars - SHORTENED_URL_LENGTH + 1 + truncated_text = restore_truncated_url truncated_text, post_text, maxchars + + "#{truncated_text} #{post_url}" end def build_twitter_post(post, url, retry_count=0) @@ -82,4 +86,18 @@ class Services::Twitter < Service oauth_token_secret: self.access_secret ) end + + def restore_truncated_url truncated_text, post_text, maxchars + return truncated_text if truncated_text !~ /#{LINK_PATTERN}\Z/ + + url = post_text.match(LINK_PATTERN, truncated_text.rindex('http'))[0] + truncated_text = truncate( + post_text, + length: maxchars - SHORTENED_URL_LENGTH + 2, + separator: ' ', + omission: '' + ) + + "#{truncated_text} #{url} ..." + end end diff --git a/spec/models/services/twitter_spec.rb b/spec/models/services/twitter_spec.rb index d63f3ad69..0135da258 100644 --- a/spec/models/services/twitter_spec.rb +++ b/spec/models/services/twitter_spec.rb @@ -71,6 +71,24 @@ describe Services::Twitter do answer.should_not match /\.\.\./ end + + it "should not cut links when truncating a post" do + long_message = SecureRandom.hex(40) + " http://joindiaspora.com/a-very-long-url-name-that-will-be-shortened.html " + SecureRandom.hex(55) + long_post = stub(:text => long_message, :id => 1, :photos => []) + answer = @service.build_twitter_post(long_post, '') + + answer.should match /\.\.\./ + answer.should match /shortened\.html/ + end + + it "should append the otherwise-cut link when truncating a post" do + long_message = "http://joindiaspora.com/a-very-long-decoy-url.html " + SecureRandom.hex(20) + " http://joindiaspora.com/a-very-long-url-name-that-will-be-shortened.html " + SecureRandom.hex(55) + " http://joindiaspora.com/a-very-long-decoy-url-part-2.html" + long_post = stub(:text => long_message, :id => 1, :photos => []) + answer = @service.build_twitter_post(long_post, '') + + answer.should match /\.\.\./ + answer.should match /shortened\.html/ + end it "should not truncate a long message with an https url" do long_message = " https://joindiaspora.com/a-very-long-url-name-that-will-be-shortened.html " + @long_message_end