Skip to content

Commit

Permalink
Restored TweetLength and renamed it to EstimateTweetLength
Browse files Browse the repository at this point in the history
  • Loading branch information
linvi committed Mar 28, 2018
1 parent c06107a commit 90c8936
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 33 deletions.
60 changes: 30 additions & 30 deletions Testinvi/Tweetinvi.Core/StringExtensionsTests.cs
Expand Up @@ -18,7 +18,7 @@ public void TestLengthWith2Urls()
{
string test = "Hello http://tweetinvi.codeplex.com/salutLescopains 3615 Gerard www.linviIsMe.com piloupe";

//int twitterLength = StringExtension.TweetLength(test);
//int twitterLength = StringExtension.EstimateTweetLength(test);

//Assert.AreEqual(twitterLength, 73);
}
Expand All @@ -29,7 +29,7 @@ public void TestLengthWith2UrlsAndHttps()
{
string test = "Hello https://tweetinvi.codeplex.com/salutLescopains 3615 Gerard www.linviIsMe.com piloupe";

//int twitterLength = StringExtension.TweetLength(test);
//int twitterLength = StringExtension.EstimateTweetLength(test);

//Assert.AreEqual(twitterLength, 73);
}
Expand All @@ -41,7 +41,7 @@ public void TestLengthWithURLFollowedByDotAndSingleChar()
{
string test = "Hello https://tweetinvi.codeplex.com.a 3615 Gerard www.linviIsMe.com piloupe";

//int twitterLength = StringExtension.TweetLength(test);
//int twitterLength = StringExtension.EstimateTweetLength(test);

//Assert.AreEqual(twitterLength, 75);
}
Expand All @@ -52,7 +52,7 @@ public void TestLengthWithURLFollowedByDotAndTwoChars()
{
string test = "Hello https://tweetinvi.codeplex.com.au 3615 Gerard www.linviIsMe.com piloupe";

//int twitterLength = StringExtension.TweetLength(test);
//int twitterLength = StringExtension.EstimateTweetLength(test);

//Assert.AreEqual(twitterLength, 73);
}
Expand All @@ -63,7 +63,7 @@ public void TestLengthWithURLFollowedByArgsAndDot()
{
string test = "Hello https://tweetinvi.codeplex.com/salutLescopains.a 3615 Gerard www.linviIsMe.com piloupe";

//int twitterLength = StringExtension.TweetLength(test);
//int twitterLength = StringExtension.EstimateTweetLength(test);

//Assert.AreEqual(twitterLength, 73);
}
Expand All @@ -74,30 +74,30 @@ public void TestLengthWithSmallUrl()
{
string test = "www.co.co";

//int twitterLength = StringExtension.TweetLength(test);
//int twitterLength = StringExtension.EstimateTweetLength(test);

//Assert.AreEqual(twitterLength, 23);
}

private void TestURLWithMultiplePrefix(string url, int expectedLength)
{
//var basicTweetURL = string.Format("Hello there http:// {0} bye!", url);
//Assert.AreEqual(basicTweetURL.TweetLength(), expectedLength);
//Assert.AreEqual(basicTweetURL.EstimateTweetLength(), expectedLength);

//var wwwTweetURL = string.Format("Hello there http:// www.{0} bye!", url);
//Assert.AreEqual(wwwTweetURL.TweetLength(), expectedLength);
//Assert.AreEqual(wwwTweetURL.EstimateTweetLength(), expectedLength);

//var httpTweetURL = string.Format("Hello there http:// http://{0} bye!", url);
//Assert.AreEqual(httpTweetURL.TweetLength(), expectedLength);
//Assert.AreEqual(httpTweetURL.EstimateTweetLength(), expectedLength);

//var httpsTweetURL = string.Format("Hello there http:// https://{0} bye!", url);
//Assert.AreEqual(httpsTweetURL.TweetLength(), expectedLength);
//Assert.AreEqual(httpsTweetURL.EstimateTweetLength(), expectedLength);

//var httpwwwTweetURL = string.Format("Hello there http:// http://{0} bye!", url);
//Assert.AreEqual(httpwwwTweetURL.TweetLength(), expectedLength);
//Assert.AreEqual(httpwwwTweetURL.EstimateTweetLength(), expectedLength);

//var httpswwwTweetURL = string.Format("Hello there http:// https://{0} bye!", url);
//Assert.AreEqual(httpswwwTweetURL.TweetLength(), expectedLength);
//Assert.AreEqual(httpswwwTweetURL.EstimateTweetLength(), expectedLength);
}

[TestMethod]
Expand Down Expand Up @@ -151,41 +151,41 @@ public void URLWithOnly2CharsAtTheEnd()

//int expectedLength = 48;
//var basicTweetURL = string.Format("Hello there http:// {0} bye!", url);
//Assert.AreEqual(basicTweetURL.TweetLength(), expectedLength);
//Assert.AreEqual(basicTweetURL.EstimateTweetLength(), expectedLength);

//var wwwTweetURL = string.Format("Hello there http:// www.{0} bye!", url);
//Assert.AreEqual(wwwTweetURL.TweetLength(), expectedLength);
//Assert.AreEqual(wwwTweetURL.EstimateTweetLength(), expectedLength);

//var httpTweetURL = string.Format("Hello there http:// http://{0} bye!", url);
//Assert.AreEqual(httpTweetURL.TweetLength(), expectedLength);
//Assert.AreEqual(httpTweetURL.EstimateTweetLength(), expectedLength);

//var httpsTweetURL = string.Format("Hello there http:// https://{0} bye!", url);
//Assert.AreEqual(httpsTweetURL.TweetLength(), expectedLength);
//Assert.AreEqual(httpsTweetURL.EstimateTweetLength(), expectedLength);

//var httpwwwTweetURL = string.Format("Hello there http:// http://{0} bye!", url);
//Assert.AreEqual(httpwwwTweetURL.TweetLength(), expectedLength);
//Assert.AreEqual(httpwwwTweetURL.EstimateTweetLength(), expectedLength);

//var httpswwwTweetURL = string.Format("Hello there http:// https://{0} bye!", url);
//Assert.AreEqual(httpswwwTweetURL.TweetLength(), expectedLength);
//Assert.AreEqual(httpswwwTweetURL.EstimateTweetLength(), expectedLength);
}

[TestMethod]
[Ignore]
public void URLWithOnly2CharsAtTheEnd_ButWithASlashCharacter()
{
//var url = "NOW-FREE/4 Parties! Live Shows/Music/Art Walk Weekend. https://pbsc.co/eg/4b MAP, & interactive for every Smart/iphone: goo.gl/.";
//Assert.AreEqual(url.TweetLength(), 146);
//Assert.AreEqual(url.EstimateTweetLength(), 146);

//var url2 = "NOW-FREE/4 Parties! Live Shows/Music/Art Walk Weekend. https://pbsc.co/eg/4b MAP, & interactive for every Smart/iphone: goo.gl/dqkd.";
//Assert.AreEqual(url2.TweetLength(), 146);
//Assert.AreEqual(url2.EstimateTweetLength(), 146);
}

[TestMethod]
[Ignore]
public void URLWithEqualsCharacters()
{
//var message = "The quick brown fox jumps over the lazy dog. My dog is freaking amazing. https://www.google.nl/search?q=dog&source=lnms&tbm=isch&sa=X&ei=IZ7fU-CwJIO50QWtmICoCA&ved=0CAgQ_AUoAQ&biw=1528&bih=876";
//Assert.AreEqual(message.TweetLength(), 96);
//Assert.AreEqual(message.EstimateTweetLength(), 96);
}

[TestMethod]
Expand All @@ -194,8 +194,8 @@ public void TweetWithURLAndMedia_URLHasNoSpaceBeforeIt()
{
var text = "abcdefghijklmnopqrstuvwxy abcdefghijklmnopqrstuvwxy abcdefghijklmnopqrstuvwxy abcdefghijklmophttp://bit.ly/tinyurlwiki";

//Assert.AreEqual(Tweet.Length(text), 118);
//Assert.AreEqual(Tweet.Length(text, new PublishTweetOptionalParameters()
//Assert.AreEqual(Tweet.EstimateTweetLength(text), 118);
//Assert.AreEqual(Tweet.EstimateTweetLength(text, new PublishTweetOptionalParameters()
//{
// MediaBinaries = new List<byte[]> { new byte[10] }
//}), 142);
Expand All @@ -208,8 +208,8 @@ public void TweetWithURLAndMedia()
{
var text = "abcdefghijklmnopqrstuvwxy abcdefghijklmnopqrstuvwxy abcdefghijklmnopqrstuvwxy abcdefghijklmop http://bit.ly/tinyurlwiki";

//Assert.AreEqual(Tweet.Length(text), 117);
//Assert.AreEqual(Tweet.Length(text, new PublishTweetOptionalParameters()
//Assert.AreEqual(Tweet.EstimateTweetLength(text), 117);
//Assert.AreEqual(Tweet.EstimateTweetLength(text, new PublishTweetOptionalParameters()
//{
// MediaBinaries = new List<byte[]> { new byte[10] }
//}), 141);
Expand All @@ -222,9 +222,9 @@ public void TweetWith2URLsAndMedia2()
var textOnly = "How Real Estate Agents Get More Closings By Using SMS Text Messaging - DialMyCalls.com";
var text = "How Real Estate Agents Get More Closings By Using SMS Text Messaging - DialMyCalls.com http://bit.ly/1kmezw9";

//Assert.AreEqual(Tweet.Length(textOnly), 94);
//Assert.AreEqual(Tweet.Length(text), 118);
//Assert.AreEqual(Tweet.Length(text, new PublishTweetOptionalParameters()
//Assert.AreEqual(Tweet.EstimateTweetLength(textOnly), 94);
//Assert.AreEqual(Tweet.EstimateTweetLength(text), 118);
//Assert.AreEqual(Tweet.EstimateTweetLength(text, new PublishTweetOptionalParameters()
//{
// MediaBinaries = new List<byte[]> { new byte[10] }
//}), 142);
Expand All @@ -234,14 +234,14 @@ public void TweetWith2URLsAndMedia2()
[Ignore]
public void TweetWithUTF32Character()
{
//Assert.AreEqual(6, Tweet.Length("sa🚒osa"));
//Assert.AreEqual(6, Tweet.EstimateTweetLength("sa🚒osa"));
}

[TestMethod]
[Ignore]
public void TweetLengthWithSpecialUTFCharacters()
{
//var l = Tweet.Length("sa 🎅⛄️🎅 done");
//var l = Tweet.EstimateTweetLength("sa 🎅⛄️🎅 done");
//Assert.AreEqual(l, 11);
}

Expand Down
39 changes: 37 additions & 2 deletions Tweetinvi.Controllers/Tweet/TweetController.cs
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using Tweetinvi.Controllers.Upload;
using Tweetinvi.Core.Controllers;
using Tweetinvi.Core.Extensions;
using Tweetinvi.Core.Factories;
using Tweetinvi.Models;
using Tweetinvi.Models.DTO;
Expand Down Expand Up @@ -63,15 +64,49 @@ public ITweet PublishTweetInReplyTo(string text, ITweetIdentifier tweet)
public bool CanBePublished(IPublishTweetParameters publishTweetParameters)
{
return true;
//return TweetinviConsts.MAX_TWEET_SIZE >= Length(publishTweetParameters);
//return TweetinviConsts.MAX_TWEET_SIZE >= EstimateTweetLength(publishTweetParameters);
}

public bool CanBePublished(string text, IPublishTweetOptionalParameters publishTweetOptionalParameters = null)
{
return true;
//return TweetinviConsts.MAX_TWEET_SIZE >= Length(text, publishTweetOptionalParameters);
//return TweetinviConsts.MAX_TWEET_SIZE >= EstimateTweetLength(text, publishTweetOptionalParameters);
}

public static int EstimateTweetLength(IPublishTweetParameters publishTweetParameters)
{
return EstimateTweetLength(publishTweetParameters.Text, publishTweetParameters.Parameters);
}

public static int EstimateTweetLength(string text, IPublishTweetOptionalParameters publishTweetOptionalParameters = null)
{
var textLength = text == null ? 0 : StringExtension.EstimateTweetLength(text);

if (text == null || publishTweetOptionalParameters == null)
{
return textLength;
}

if (publishTweetOptionalParameters.QuotedTweet != null)
{
var newText = text.TrimEnd();

textLength = StringExtension.EstimateTweetLength(newText);
textLength += 1; // for the space that needs to be added before the link to quoted tweet.
textLength += TweetinviConsts.MEDIA_CONTENT_SIZE;
}

if (!publishTweetOptionalParameters.Medias.IsNullOrEmpty() ||
!publishTweetOptionalParameters.MediaIds.IsNullOrEmpty() ||
!publishTweetOptionalParameters.MediaBinaries.IsNullOrEmpty())
{
textLength += TweetinviConsts.MEDIA_CONTENT_SIZE;
}

return textLength;
}


private ITweetDTO InternalPublishTweet(IPublishTweetParameters parameters)
{
// The exceptions have to be raised before the QueryGenerator as
Expand Down
2 changes: 1 addition & 1 deletion Tweetinvi.Core/Core/Extensions/StringExtension.cs
Expand Up @@ -78,7 +78,7 @@ public static ITweetTextParts TweetParts(this string tweetText)
/// <returns>Size of the current Tweet</returns>
[Obsolete("The value returned are no longer correct as Twitter changed their counting algorithm. " +
"Please use twitter-text official implementations in the meantime (https://github.com/twitter/twitter-text).")]
internal static int TweetLength(this string tweet, bool willBePublishedWithMedia = false)
public static int EstimateTweetLength( string tweet, bool willBePublishedWithMedia = false)
{
if (tweet == null)
{
Expand Down

0 comments on commit 90c8936

Please sign in to comment.