Bugfix 7714 twitter char overflow

closes #7791
This commit is contained in:
HankG 2018-04-08 12:10:35 -04:00 committed by Dennis Schubert
parent 6918dbc761
commit 211e5cd1bc
No known key found for this signature in database
GPG key ID: 5A0304BEA7966D7E
3 changed files with 10 additions and 7 deletions

View file

@ -11,6 +11,7 @@
* Show error message when creating posts with invalid aspects [#7742](https://github.com/diaspora/diaspora/pull/7742) * 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 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 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 ## Features
* Make public stream accessible for logged out users [#7775](https://github.com/diaspora/diaspora/pull/7775) * Make public stream accessible for logged out users [#7775](https://github.com/diaspora/diaspora/pull/7775)

View file

@ -4,7 +4,7 @@ class Services::Twitter < Service
include Rails.application.routes.url_helpers include Rails.application.routes.url_helpers
MAX_CHARACTERS = 280 MAX_CHARACTERS = 280
SHORTENED_URL_LENGTH = 21 SHORTENED_URL_LENGTH = 23
LINK_PATTERN = %r{https?://\S+} LINK_PATTERN = %r{https?://\S+}
def provider def provider
@ -69,7 +69,7 @@ class Services::Twitter < Service
host: AppConfig.pod_uri.authority 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 = restore_truncated_url truncated_text, post_text, max_characters
"#{truncated_text} #{post_url}" "#{truncated_text} #{post_url}"

View file

@ -54,7 +54,9 @@ describe Services::Twitter, :type => :model do
it "should truncate a long message" do it "should truncate a long message" do
long_message = SecureRandom.hex(360) long_message = SecureRandom.hex(360)
long_post = double(message: double(plain_text_without_markdown: long_message), id: 1, photos: []) 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 end
it "should not truncate a long message with an http url" do it "should not truncate a long message with an http url" do
@ -104,11 +106,11 @@ describe Services::Twitter, :type => :model do
end end
it "should not truncate a message of maximum length" do it "should not truncate a message of maximum length" do
exact_size_message = SecureRandom.hex(70) exact_size_message = SecureRandom.hex(140)
exact_size_post = double(message: double(plain_text_without_markdown: exact_size_message), id: 1, photos: []) 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) 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
end end