Skip to content

Commit 90c8936

Browse files
committed
Restored TweetLength and renamed it to EstimateTweetLength
1 parent c06107a commit 90c8936

File tree

3 files changed

+68
-33
lines changed

3 files changed

+68
-33
lines changed

Testinvi/Tweetinvi.Core/StringExtensionsTests.cs

+30-30
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public void TestLengthWith2Urls()
1818
{
1919
string test = "Hello http://tweetinvi.codeplex.com/salutLescopains 3615 Gerard www.linviIsMe.com piloupe";
2020

21-
//int twitterLength = StringExtension.TweetLength(test);
21+
//int twitterLength = StringExtension.EstimateTweetLength(test);
2222

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

32-
//int twitterLength = StringExtension.TweetLength(test);
32+
//int twitterLength = StringExtension.EstimateTweetLength(test);
3333

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

44-
//int twitterLength = StringExtension.TweetLength(test);
44+
//int twitterLength = StringExtension.EstimateTweetLength(test);
4545

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

55-
//int twitterLength = StringExtension.TweetLength(test);
55+
//int twitterLength = StringExtension.EstimateTweetLength(test);
5656

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

66-
//int twitterLength = StringExtension.TweetLength(test);
66+
//int twitterLength = StringExtension.EstimateTweetLength(test);
6767

6868
//Assert.AreEqual(twitterLength, 73);
6969
}
@@ -74,30 +74,30 @@ public void TestLengthWithSmallUrl()
7474
{
7575
string test = "www.co.co";
7676

77-
//int twitterLength = StringExtension.TweetLength(test);
77+
//int twitterLength = StringExtension.EstimateTweetLength(test);
7878

7979
//Assert.AreEqual(twitterLength, 23);
8080
}
8181

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

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

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

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

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

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

103103
[TestMethod]
@@ -151,41 +151,41 @@ public void URLWithOnly2CharsAtTheEnd()
151151

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

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

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

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

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

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

172172
[TestMethod]
173173
[Ignore]
174174
public void URLWithOnly2CharsAtTheEnd_ButWithASlashCharacter()
175175
{
176176
//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/.";
177-
//Assert.AreEqual(url.TweetLength(), 146);
177+
//Assert.AreEqual(url.EstimateTweetLength(), 146);
178178

179179
//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.";
180-
//Assert.AreEqual(url2.TweetLength(), 146);
180+
//Assert.AreEqual(url2.EstimateTweetLength(), 146);
181181
}
182182

183183
[TestMethod]
184184
[Ignore]
185185
public void URLWithEqualsCharacters()
186186
{
187187
//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";
188-
//Assert.AreEqual(message.TweetLength(), 96);
188+
//Assert.AreEqual(message.EstimateTweetLength(), 96);
189189
}
190190

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

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

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

225-
//Assert.AreEqual(Tweet.Length(textOnly), 94);
226-
//Assert.AreEqual(Tweet.Length(text), 118);
227-
//Assert.AreEqual(Tweet.Length(text, new PublishTweetOptionalParameters()
225+
//Assert.AreEqual(Tweet.EstimateTweetLength(textOnly), 94);
226+
//Assert.AreEqual(Tweet.EstimateTweetLength(text), 118);
227+
//Assert.AreEqual(Tweet.EstimateTweetLength(text, new PublishTweetOptionalParameters()
228228
//{
229229
// MediaBinaries = new List<byte[]> { new byte[10] }
230230
//}), 142);
@@ -234,14 +234,14 @@ public void TweetWith2URLsAndMedia2()
234234
[Ignore]
235235
public void TweetWithUTF32Character()
236236
{
237-
//Assert.AreEqual(6, Tweet.Length("sa🚒osa"));
237+
//Assert.AreEqual(6, Tweet.EstimateTweetLength("sa🚒osa"));
238238
}
239239

240240
[TestMethod]
241241
[Ignore]
242242
public void TweetLengthWithSpecialUTFCharacters()
243243
{
244-
//var l = Tweet.Length("sa 🎅⛄️🎅 done");
244+
//var l = Tweet.EstimateTweetLength("sa 🎅⛄️🎅 done");
245245
//Assert.AreEqual(l, 11);
246246
}
247247

Tweetinvi.Controllers/Tweet/TweetController.cs

+37-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using Tweetinvi.Controllers.Upload;
55
using Tweetinvi.Core.Controllers;
6+
using Tweetinvi.Core.Extensions;
67
using Tweetinvi.Core.Factories;
78
using Tweetinvi.Models;
89
using Tweetinvi.Models.DTO;
@@ -63,15 +64,49 @@ public ITweet PublishTweetInReplyTo(string text, ITweetIdentifier tweet)
6364
public bool CanBePublished(IPublishTweetParameters publishTweetParameters)
6465
{
6566
return true;
66-
//return TweetinviConsts.MAX_TWEET_SIZE >= Length(publishTweetParameters);
67+
//return TweetinviConsts.MAX_TWEET_SIZE >= EstimateTweetLength(publishTweetParameters);
6768
}
6869

6970
public bool CanBePublished(string text, IPublishTweetOptionalParameters publishTweetOptionalParameters = null)
7071
{
7172
return true;
72-
//return TweetinviConsts.MAX_TWEET_SIZE >= Length(text, publishTweetOptionalParameters);
73+
//return TweetinviConsts.MAX_TWEET_SIZE >= EstimateTweetLength(text, publishTweetOptionalParameters);
7374
}
7475

76+
public static int EstimateTweetLength(IPublishTweetParameters publishTweetParameters)
77+
{
78+
return EstimateTweetLength(publishTweetParameters.Text, publishTweetParameters.Parameters);
79+
}
80+
81+
public static int EstimateTweetLength(string text, IPublishTweetOptionalParameters publishTweetOptionalParameters = null)
82+
{
83+
var textLength = text == null ? 0 : StringExtension.EstimateTweetLength(text);
84+
85+
if (text == null || publishTweetOptionalParameters == null)
86+
{
87+
return textLength;
88+
}
89+
90+
if (publishTweetOptionalParameters.QuotedTweet != null)
91+
{
92+
var newText = text.TrimEnd();
93+
94+
textLength = StringExtension.EstimateTweetLength(newText);
95+
textLength += 1; // for the space that needs to be added before the link to quoted tweet.
96+
textLength += TweetinviConsts.MEDIA_CONTENT_SIZE;
97+
}
98+
99+
if (!publishTweetOptionalParameters.Medias.IsNullOrEmpty() ||
100+
!publishTweetOptionalParameters.MediaIds.IsNullOrEmpty() ||
101+
!publishTweetOptionalParameters.MediaBinaries.IsNullOrEmpty())
102+
{
103+
textLength += TweetinviConsts.MEDIA_CONTENT_SIZE;
104+
}
105+
106+
return textLength;
107+
}
108+
109+
75110
private ITweetDTO InternalPublishTweet(IPublishTweetParameters parameters)
76111
{
77112
// The exceptions have to be raised before the QueryGenerator as

Tweetinvi.Core/Core/Extensions/StringExtension.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public static ITweetTextParts TweetParts(this string tweetText)
7878
/// <returns>Size of the current Tweet</returns>
7979
[Obsolete("The value returned are no longer correct as Twitter changed their counting algorithm. " +
8080
"Please use twitter-text official implementations in the meantime (https://github.com/twitter/twitter-text).")]
81-
internal static int TweetLength(this string tweet, bool willBePublishedWithMedia = false)
81+
public static int EstimateTweetLength( string tweet, bool willBePublishedWithMedia = false)
8282
{
8383
if (tweet == null)
8484
{

0 commit comments

Comments
 (0)