diff --git a/Changelog.md b/Changelog.md index 64c2ed26e25c60cfaf1d6ef44c68cbd5df458cc7..7a7e253661886c607b6df7ccea21d47b7d00e03a 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 7b54c392ef72243476c7314d40d4d12fcc5163bf..a8207088270bd8ea2e4529ad97dc4c27a36c3e87 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 28c9d2aafcb8bda8b5b43e4d3213e2a8a49687d4..9bc08f81b920ed051acfa9f0c74690524dc91670 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