diff options
author | Jason Robinson <jaywink@basshero.org> | 2013-05-28 20:51:57 +0400 |
---|---|---|
committer | Jason Robinson <jaywink@basshero.org> | 2013-06-08 18:38:07 +0400 |
commit | 3fa9f6414d8f3ee431b62e97b396138724040bca (patch) | |
tree | 696b28e3a2f1562846e3f99ba442dd67aad477d0 /app/models/services | |
parent | a5615449056ddbfdd13c977ee965c910dffc810a (diff) |
Fix Twitter crossposting (#2758). Refactor some service posting related code.
Diffstat (limited to 'app/models/services')
-rw-r--r-- | app/models/services/facebook.rb | 4 | ||||
-rw-r--r-- | app/models/services/twitter.rb | 53 |
2 files changed, 44 insertions, 13 deletions
diff --git a/app/models/services/facebook.rb b/app/models/services/facebook.rb index 83d4da631..7eef3470c 100644 --- a/app/models/services/facebook.rb +++ b/app/models/services/facebook.rb @@ -29,10 +29,6 @@ class Services::Facebook < Service {:message => message, :access_token => self.access_token, :link => URI.extract(message, ['https', 'http']).first} end - def public_message(post, url) - super(post, MAX_CHARACTERS, url) - end - def profile_photo_url "https://graph.facebook.com/#{self.uid}/picture?type=large&access_token=#{URI.escape(self.access_token)}" end diff --git a/app/models/services/twitter.rb b/app/models/services/twitter.rb index a640f6f5c..0df15d9d8 100644 --- a/app/models/services/twitter.rb +++ b/app/models/services/twitter.rb @@ -1,4 +1,7 @@ class Services::Twitter < Service + include ActionView::Helpers::TextHelper + include MarkdownifyHelper + MAX_CHARACTERS = 140 SHORTENED_URL_LENGTH = 21 @@ -8,21 +11,53 @@ class Services::Twitter < Service def post(post, url='') Rails.logger.debug("event=post_to_service type=twitter sender_id=#{self.user_id}") - message = public_message(post, url) - tweet = client.update(message) - post.tweet_id = tweet.id + (0...20).each do |retry_count| + begin + message = build_twitter_post(post, url, retry_count) + @tweet = client.update(message) + break + rescue Twitter::Error::Forbidden => e + if e.message != 'Status is over 140 characters' || retry_count == 20 + raise e + end + end + end + post.tweet_id = @tweet.id post.save end - - def public_message(post, url) - buffer_amt = 0 - URI.extract( post.text(:plain_text => true), ['http','https'] ) do |a_url| - buffer_amt += (a_url.length - SHORTENED_URL_LENGTH) + def adjust_length_for_urls(post_text) + real_length = post_text.length + URI.extract( post_text, ['http','https'] ) do |a_url| + # add or subtract from real length - urls for tweets are always + # shortened to SHORTENED_URL_LENGTH + if a_url.length >= SHORTENED_URL_LENGTH + real_length -= a_url.length - SHORTENED_URL_LENGTH + else + real_length += SHORTENED_URL_LENGTH - a_url.length + end end + return real_length + end + def add_post_link(post, post_text, maxchars) + post_url = Rails.application.routes.url_helpers.short_post_url( + post, + :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}" + end + + def build_twitter_post(post, url, retry_count=0) + maxchars = MAX_CHARACTERS - retry_count*5 + post_text = strip_markdown(post.text(:plain_text => true)) #if photos, always include url, otherwise not for short posts - super(post, MAX_CHARACTERS + buffer_amt, url, post.photos.any?) + if adjust_length_for_urls(post_text) > maxchars || post.photos.any? + post_text = add_post_link(post, post_text, maxchars) + end + return post_text end def profile_photo_url |