From 211e5cd1bc2900a337cc8fdf21fbe07702625bcc Mon Sep 17 00:00:00 2001
From: HankG <hankgrabowski@kolabnow.com>
Date: Sun, 8 Apr 2018 12:10:35 -0400
Subject: [PATCH] Bugfix 7714 twitter char overflow

closes #7791
---
 Changelog.md                         |  1 +
 app/models/services/twitter.rb       |  4 ++--
 spec/models/services/twitter_spec.rb | 12 +++++++-----
 3 files changed, 10 insertions(+), 7 deletions(-)

diff --git a/Changelog.md b/Changelog.md
index 64c2ed26e2..7a7e253661 100644
--- a/Changelog.md
+++ b/Changelog.md
@@ -11,6 +11,7 @@
 * Show error message when creating posts with invalid aspects [#7742](https://github.com/diaspora/diaspora/pull/7742)
 * Fix mention syntax backport for two immediately consecutive mentions [#7777](https://github.com/diaspora/diaspora/pull/7777)
 * Fix link to 'make yourself an admin' [#7783](https://github.com/diaspora/diaspora/pull/7783)
+* Fix calculation of content lengths when cross-posting to twitter [#7791](https://github.com/diaspora/diaspora/pull/7791)
 
 ## Features
 * Make public stream accessible for logged out users [#7775](https://github.com/diaspora/diaspora/pull/7775)
diff --git a/app/models/services/twitter.rb b/app/models/services/twitter.rb
index 7b54c392ef..a820708827 100644
--- a/app/models/services/twitter.rb
+++ b/app/models/services/twitter.rb
@@ -4,7 +4,7 @@ class Services::Twitter < Service
   include Rails.application.routes.url_helpers
 
   MAX_CHARACTERS = 280
-  SHORTENED_URL_LENGTH = 21
+  SHORTENED_URL_LENGTH = 23
   LINK_PATTERN = %r{https?://\S+}
 
   def provider
@@ -69,7 +69,7 @@ class Services::Twitter < Service
       host: AppConfig.pod_uri.authority
     )
 
-    truncated_text = post_text.truncate max_characters - SHORTENED_URL_LENGTH + 1
+    truncated_text = post_text.truncate max_characters - SHORTENED_URL_LENGTH - 1
     truncated_text = restore_truncated_url truncated_text, post_text, max_characters
 
     "#{truncated_text} #{post_url}"
diff --git a/spec/models/services/twitter_spec.rb b/spec/models/services/twitter_spec.rb
index 28c9d2aafc..9bc08f81b9 100644
--- a/spec/models/services/twitter_spec.rb
+++ b/spec/models/services/twitter_spec.rb
@@ -54,7 +54,9 @@ describe Services::Twitter, :type => :model do
     it "should truncate a long message" do
       long_message = SecureRandom.hex(360)
       long_post = double(message: double(plain_text_without_markdown: long_message), id: 1, photos: [])
-      expect(@service.send(:build_twitter_post, long_post).length).to be < long_message.length
+      answer = @service.send(:build_twitter_post, long_post)
+      expect(answer.length).to be < long_message.length
+      expect(answer).to include "http:"
     end
 
     it "should not truncate a long message with an http url" do
@@ -104,11 +106,11 @@ describe Services::Twitter, :type => :model do
     end
 
     it "should not truncate a message of maximum length" do
-        exact_size_message = SecureRandom.hex(70)
-        exact_size_post = double(message: double(plain_text_without_markdown: exact_size_message), id:  1, photos: [])
-        answer = @service.send(:build_twitter_post, exact_size_post)
+      exact_size_message = SecureRandom.hex(140)
+      exact_size_post = double(message: double(plain_text_without_markdown: exact_size_message), id: 1, photos: [])
+      answer = @service.send(:build_twitter_post, exact_size_post)
 
-        expect(answer).to match exact_size_message
+      expect(answer).to match exact_size_message
     end
 
   end
-- 
GitLab