using OpenTween.Api.DataModel;
using OpenTween.Connection;
using OpenTween.Models;
+using System.Drawing.Imaging;
+using OpenTween.Setting;
namespace OpenTween
{
private List<string> _hashList = new List<string>();
//max_idで古い発言を取得するために保持(lists分は個別タブで管理)
- private long minHomeTimeline = long.MaxValue;
- private long minMentions = long.MaxValue;
private long minDirectmessage = long.MaxValue;
private long minDirectmessageSent = long.MaxValue;
+ private long previousStatusId = -1L;
+
//private FavoriteQueue favQueue;
//private List<PostClass> _deletemessages = new List<PostClass>();
this.ResetApiStatus();
this.Api.Initialize(token, tokenSecret, userId, username);
_uname = username.ToLowerInvariant();
- if (SettingCommon.Instance.UserstreamStartup) this.ReconnectUserStream();
+ if (SettingManager.Common.UserstreamStartup) this.ReconnectUserStream();
}
public string PreProcessUrl(string orgData)
return orgData;
}
- private string GetPlainText(string orgData)
- {
- return WebUtility.HtmlDecode(Regex.Replace(orgData, "(?<tagStart><a [^>]+>)(?<text>[^<]+)(?<tagEnd></a>)", "${text}"));
- }
-
- // htmlの簡易サニタイズ(詳細表示に不要なタグの除去)
-
- private string SanitizeHtml(string orgdata)
- {
- var retdata = orgdata;
-
- retdata = Regex.Replace(retdata, "<(script|object|applet|image|frameset|fieldset|legend|style).*" +
- "</(script|object|applet|image|frameset|fieldset|legend|style)>", "", RegexOptions.IgnoreCase);
-
- retdata = Regex.Replace(retdata, "<(frame|link|iframe|img)>", "", RegexOptions.IgnoreCase);
-
- return retdata;
- }
-
- private string AdjustHtml(string orgData)
- {
- var retStr = orgData;
- //var m = Regex.Match(retStr, "<a [^>]+>[#|#](?<1>[a-zA-Z0-9_]+)</a>");
- //while (m.Success)
- //{
- // lock (LockObj)
- // {
- // _hashList.Add("#" + m.Groups(1).Value);
- // }
- // m = m.NextMatch;
- //}
- retStr = Regex.Replace(retStr, "<a [^>]*href=\"/", "<a href=\"https://twitter.com/");
- retStr = retStr.Replace("<a href=", "<a target=\"_self\" href=");
- retStr = Regex.Replace(retStr, @"(\r\n?|\n)", "<br>"); // CRLF, CR, LF は全て <br> に置換する
-
- //半角スペースを置換(Thanks @anis774)
- var ret = false;
- do
- {
- ret = EscapeSpace(ref retStr);
- } while (!ret);
-
- return SanitizeHtml(retStr);
- }
-
- private bool EscapeSpace(ref string html)
- {
- //半角スペースを置換(Thanks @anis774)
- var isTag = false;
- for (int i = 0; i < html.Length; i++)
- {
- if (html[i] == '<')
- {
- isTag = true;
- }
- if (html[i] == '>')
- {
- isTag = false;
- }
-
- if ((!isTag) && (html[i] == ' '))
- {
- html = html.Remove(i, 1);
- html = html.Insert(i, " ");
- return false;
- }
- }
- return true;
- }
-
- private struct PostInfo
- {
- public string CreatedAt;
- public string Id;
- public string Text;
- public string UserId;
- public PostInfo(string Created, string IdStr, string txt, string uid)
- {
- CreatedAt = Created;
- Id = IdStr;
- Text = txt;
- UserId = uid;
- }
- public bool Equals(PostInfo dst)
- {
- if (this.CreatedAt == dst.CreatedAt && this.Id == dst.Id && this.Text == dst.Text && this.UserId == dst.UserId)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- }
-
- static private PostInfo _prev = new PostInfo("", "", "", "");
- private bool IsPostRestricted(TwitterStatus status)
- {
- var _current = new PostInfo("", "", "", "");
-
- _current.CreatedAt = status.CreatedAt;
- _current.Id = status.IdStr;
- if (status.Text == null)
- {
- _current.Text = "";
- }
- else
- {
- _current.Text = status.Text;
- }
- _current.UserId = status.User.IdStr;
-
- if (_current.Equals(_prev))
- {
- return true;
- }
- _prev.CreatedAt = _current.CreatedAt;
- _prev.Id = _current.Id;
- _prev.Text = _current.Text;
- _prev.UserId = _current.UserId;
-
- return false;
- }
-
public async Task PostStatus(string postStr, long? reply_to, IReadOnlyList<long> mediaIds = null)
{
this.CheckAccountState();
this.UpdateUserStats(status.User);
- if (IsPostRestricted(status))
- {
+ if (status.Id == this.previousStatusId)
throw new WebApiException("OK:Delaying?");
- }
+
+ this.previousStatusId = status.Id;
}
- public async Task PostStatusWithMultipleMedia(string postStr, long? reply_to, IMediaItem[] mediaItems)
+ public Task<long> UploadMedia(IMediaItem item)
+ => this.UploadMedia(item, SettingManager.Common.AlphaPNGWorkaround);
+
+ public async Task<long> UploadMedia(IMediaItem item, bool alphaPNGWorkaround)
{
this.CheckAccountState();
- if (Twitter.DMSendTextRegex.IsMatch(postStr))
+ LazyJson<TwitterUploadMediaResult> response;
+
+ using (var origImage = item.CreateImage())
{
- await this.SendDirectMessage(postStr)
- .ConfigureAwait(false);
- return;
+ if (alphaPNGWorkaround && this.AddAlphaChannelIfNeeded(origImage.Image, out var newImage))
+ {
+ using (var newMediaItem = new MemoryImageMediaItem(newImage))
+ {
+ response = await this.Api.MediaUpload(newMediaItem)
+ .ConfigureAwait(false);
+ }
+ }
+ else
+ {
+ response = await this.Api.MediaUpload(item)
+ .ConfigureAwait(false);
+ }
}
- if (mediaItems.Length == 0)
- throw new WebApiException("Err:Invalid Files!");
-
- var uploadTasks = from m in mediaItems
- select this.UploadMedia(m);
-
- var mediaIds = await Task.WhenAll(uploadTasks)
+ var media = await response.LoadJsonAsync()
.ConfigureAwait(false);
- await this.PostStatus(postStr, reply_to, mediaIds)
- .ConfigureAwait(false);
+ return media.MediaId;
}
- public async Task<long> UploadMedia(IMediaItem item)
- {
- this.CheckAccountState();
+ /// <summary>
+ /// pic.twitter.com アップロード時に JPEG への変換を回避するための加工を行う
+ /// </summary>
+ /// <remarks>
+ /// pic.twitter.com へのアップロード時に、アルファチャンネルを持たない PNG 画像が
+ /// JPEG 形式に変換され画質が低下する問題を回避します。
+ /// PNG 以外の画像や、すでにアルファチャンネルを持つ PNG 画像に対しては何もしません。
+ /// </remarks>
+ /// <returns>加工が行われた場合は true、そうでない場合は false</returns>
+ private bool AddAlphaChannelIfNeeded(Image origImage, out MemoryImage newImage)
+ {
+ newImage = null;
+
+ // PNG 画像以外に対しては何もしない
+ if (origImage.RawFormat.Guid != ImageFormat.Png.Guid)
+ return false;
- var response = await this.Api.MediaUpload(item)
- .ConfigureAwait(false);
+ using (var bitmap = new Bitmap(origImage))
+ {
+ // アルファ値が 255 以外のピクセルが含まれていた場合は何もしない
+ foreach (var x in Enumerable.Range(0, bitmap.Width))
+ {
+ foreach (var y in Enumerable.Range(0, bitmap.Height))
+ {
+ if (bitmap.GetPixel(x, y).A != 255)
+ return false;
+ }
+ }
- var media = await response.LoadJsonAsync()
- .ConfigureAwait(false);
+ // 左上の 1px だけアルファ値を 254 にする
+ var pixel = bitmap.GetPixel(0, 0);
+ var newPixel = Color.FromArgb(pixel.A - 1, pixel.R, pixel.G, pixel.B);
+ bitmap.SetPixel(0, 0, newPixel);
- return media.MediaId;
+ // MemoryImage 作成時に画像はコピーされるため、この後 bitmap は破棄しても問題ない
+ newImage = MemoryImage.CopyFromImage(bitmap);
+
+ return true;
+ }
}
public async Task SendDirectMessage(string postStr)
this.CheckAccountState();
//データ部分の生成
- var target = id;
var post = TabInformations.GetInstance()[id];
if (post == null)
- {
throw new WebApiException("Err:Target isn't found.");
- }
- if (TabInformations.GetInstance()[id].RetweetedId != null)
- {
- target = TabInformations.GetInstance()[id].RetweetedId.Value; //再RTの場合は元発言をRT
- }
+
+ var target = post.RetweetedId ?? id; //再RTの場合は元発言をRT
var response = await this.Api.StatusesRetweet(target)
.ConfigureAwait(false);
var status = await response.LoadJsonAsync()
.ConfigureAwait(false);
- //ReTweetしたものをTLに追加
- post = CreatePostsFromStatusData(status);
- if (post == null)
- throw new WebApiException("Invalid Json!");
-
//二重取得回避
lock (LockObj)
{
- if (TabInformations.GetInstance().ContainsKey(post.StatusId))
+ if (TabInformations.GetInstance().ContainsKey(status.Id))
return;
}
+
//Retweet判定
- if (post.RetweetedId == null)
+ if (status.RetweetedStatus == null)
throw new WebApiException("Invalid Json!");
+
+ //ReTweetしたものをTLに追加
+ post = CreatePostsFromStatusData(status);
+
//ユーザー情報
post.IsMe = true;
public bool RestrictFavCheck { get; set; }
-#region "バージョンアップ"
- public void GetTweenBinary(string strVer)
- {
- try
- {
- //本体
- if (!(new HttpVarious()).GetDataToFile("http://tween.sourceforge.jp/Tween" + strVer + ".gz?" + DateTime.Now.ToString("yyMMddHHmmss") + Environment.TickCount.ToString(),
- Path.Combine(MyCommon.settingPath, "TweenNew.exe")))
- {
- throw new WebApiException("Err:Download failed");
- }
- //英語リソース
- if (!Directory.Exists(Path.Combine(MyCommon.settingPath, "en")))
- {
- Directory.CreateDirectory(Path.Combine(MyCommon.settingPath, "en"));
- }
- if (!(new HttpVarious()).GetDataToFile("http://tween.sourceforge.jp/TweenResEn" + strVer + ".gz?" + DateTime.Now.ToString("yyMMddHHmmss") + Environment.TickCount.ToString(),
- Path.Combine(Path.Combine(MyCommon.settingPath, "en"), "Tween.resourcesNew.dll")))
- {
- throw new WebApiException("Err:Download failed");
- }
- //その他言語圏のリソース。取得失敗しても継続
- //UIの言語圏のリソース
- var curCul = "";
- if (!Thread.CurrentThread.CurrentUICulture.IsNeutralCulture)
- {
- var idx = Thread.CurrentThread.CurrentUICulture.Name.LastIndexOf('-');
- if (idx > -1)
- {
- curCul = Thread.CurrentThread.CurrentUICulture.Name.Substring(0, idx);
- }
- else
- {
- curCul = Thread.CurrentThread.CurrentUICulture.Name;
- }
- }
- else
- {
- curCul = Thread.CurrentThread.CurrentUICulture.Name;
- }
- if (!string.IsNullOrEmpty(curCul) && curCul != "en" && curCul != "ja")
- {
- if (!Directory.Exists(Path.Combine(MyCommon.settingPath, curCul)))
- {
- Directory.CreateDirectory(Path.Combine(MyCommon.settingPath, curCul));
- }
- if (!(new HttpVarious()).GetDataToFile("http://tween.sourceforge.jp/TweenRes" + curCul + strVer + ".gz?" + DateTime.Now.ToString("yyMMddHHmmss") + Environment.TickCount.ToString(),
- Path.Combine(Path.Combine(MyCommon.settingPath, curCul), "Tween.resourcesNew.dll")))
- {
- //return "Err:Download failed";
- }
- }
- //スレッドの言語圏のリソース
- string curCul2;
- if (!Thread.CurrentThread.CurrentCulture.IsNeutralCulture)
- {
- var idx = Thread.CurrentThread.CurrentCulture.Name.LastIndexOf('-');
- if (idx > -1)
- {
- curCul2 = Thread.CurrentThread.CurrentCulture.Name.Substring(0, idx);
- }
- else
- {
- curCul2 = Thread.CurrentThread.CurrentCulture.Name;
- }
- }
- else
- {
- curCul2 = Thread.CurrentThread.CurrentCulture.Name;
- }
- if (!string.IsNullOrEmpty(curCul2) && curCul2 != "en" && curCul2 != curCul)
- {
- if (!Directory.Exists(Path.Combine(MyCommon.settingPath, curCul2)))
- {
- Directory.CreateDirectory(Path.Combine(MyCommon.settingPath, curCul2));
- }
- if (!(new HttpVarious()).GetDataToFile("http://tween.sourceforge.jp/TweenRes" + curCul2 + strVer + ".gz?" + DateTime.Now.ToString("yyMMddHHmmss") + Environment.TickCount.ToString(),
- Path.Combine(Path.Combine(MyCommon.settingPath, curCul2), "Tween.resourcesNew.dll")))
- {
- //return "Err:Download failed";
- }
- }
-
- //アップデータ
- if (!(new HttpVarious()).GetDataToFile("http://tween.sourceforge.jp/TweenUp3.gz?" + DateTime.Now.ToString("yyMMddHHmmss") + Environment.TickCount.ToString(),
- Path.Combine(MyCommon.settingPath, "TweenUp3.exe")))
- {
- throw new WebApiException("Err:Download failed");
- }
- //シリアライザDLL
- if (!(new HttpVarious()).GetDataToFile("http://tween.sourceforge.jp/TweenDll" + strVer + ".gz?" + DateTime.Now.ToString("yyMMddHHmmss") + Environment.TickCount.ToString(),
- Path.Combine(MyCommon.settingPath, "TweenNew.XmlSerializers.dll")))
- {
- throw new WebApiException("Err:Download failed");
- }
- }
- catch (Exception ex)
- {
- throw new WebApiException("Err:Download failed", ex);
- }
- }
-#endregion
-
public bool ReadOwnPost
{
get
return 20;
}
- if (SettingCommon.Instance.UseAdditionalCount)
+ if (SettingManager.Common.UseAdditionalCount)
{
switch (type)
{
case MyCommon.WORKERTYPE.Favorites:
- if (SettingCommon.Instance.FavoritesCountApi != 0)
- return SettingCommon.Instance.FavoritesCountApi;
+ if (SettingManager.Common.FavoritesCountApi != 0)
+ return SettingManager.Common.FavoritesCountApi;
break;
case MyCommon.WORKERTYPE.List:
- if (SettingCommon.Instance.ListCountApi != 0)
- return SettingCommon.Instance.ListCountApi;
+ if (SettingManager.Common.ListCountApi != 0)
+ return SettingManager.Common.ListCountApi;
break;
case MyCommon.WORKERTYPE.PublicSearch:
- if (SettingCommon.Instance.SearchCountApi != 0)
- return SettingCommon.Instance.SearchCountApi;
+ if (SettingManager.Common.SearchCountApi != 0)
+ return SettingManager.Common.SearchCountApi;
break;
case MyCommon.WORKERTYPE.UserTimeline:
- if (SettingCommon.Instance.UserTimelineCountApi != 0)
- return SettingCommon.Instance.UserTimelineCountApi;
+ if (SettingManager.Common.UserTimelineCountApi != 0)
+ return SettingManager.Common.UserTimelineCountApi;
break;
}
- if (more && SettingCommon.Instance.MoreCountApi != 0)
+ if (more && SettingManager.Common.MoreCountApi != 0)
{
- return Math.Min(SettingCommon.Instance.MoreCountApi, GetMaxApiResultCount(type));
+ return Math.Min(SettingManager.Common.MoreCountApi, GetMaxApiResultCount(type));
}
- if (startup && SettingCommon.Instance.FirstCountApi != 0 && type != MyCommon.WORKERTYPE.Reply)
+ if (startup && SettingManager.Common.FirstCountApi != 0 && type != MyCommon.WORKERTYPE.Reply)
{
- return Math.Min(SettingCommon.Instance.FirstCountApi, GetMaxApiResultCount(type));
+ return Math.Min(SettingManager.Common.FirstCountApi, GetMaxApiResultCount(type));
}
}
// 上記に当てはまらない場合の共通処理
- var count = SettingCommon.Instance.CountApi;
+ var count = SettingManager.Common.CountApi;
if (type == MyCommon.WORKERTYPE.Reply)
- count = SettingCommon.Instance.CountApiReply;
+ count = SettingManager.Common.CountApiReply;
return Math.Min(count, GetMaxApiResultCount(type));
}
- public async Task GetTimelineApi(bool read, MyCommon.WORKERTYPE gType, bool more, bool startup)
+ public async Task GetHomeTimelineApi(bool read, HomeTabModel tab, bool more, bool startup)
{
this.CheckAccountState();
- var count = GetApiResultCount(gType, more, startup);
+ var count = GetApiResultCount(MyCommon.WORKERTYPE.Timeline, more, startup);
TwitterStatus[] statuses;
- if (gType == MyCommon.WORKERTYPE.Timeline)
+ if (more)
{
- if (more)
- {
- statuses = await this.Api.StatusesHomeTimeline(count, maxId: this.minHomeTimeline)
- .ConfigureAwait(false);
- }
- else
- {
- statuses = await this.Api.StatusesHomeTimeline(count)
- .ConfigureAwait(false);
- }
+ statuses = await this.Api.StatusesHomeTimeline(count, maxId: tab.OldestId)
+ .ConfigureAwait(false);
}
else
{
- if (more)
- {
- statuses = await this.Api.StatusesMentionsTimeline(count, maxId: this.minMentions)
- .ConfigureAwait(false);
- }
- else
- {
- statuses = await this.Api.StatusesMentionsTimeline(count)
- .ConfigureAwait(false);
- }
+ statuses = await this.Api.StatusesHomeTimeline(count)
+ .ConfigureAwait(false);
}
- var minimumId = CreatePostsFromJson(statuses, gType, null, read);
+ var minimumId = this.CreatePostsFromJson(statuses, MyCommon.WORKERTYPE.Timeline, tab, read);
+
+ if (minimumId != null && minimumId.Value < tab.OldestId)
+ tab.OldestId = minimumId.Value;
+ }
+
+ public async Task GetMentionsTimelineApi(bool read, MentionsTabModel tab, bool more, bool startup)
+ {
+ this.CheckAccountState();
+
+ var count = GetApiResultCount(MyCommon.WORKERTYPE.Reply, more, startup);
- if (minimumId != null)
+ TwitterStatus[] statuses;
+ if (more)
{
- if (gType == MyCommon.WORKERTYPE.Timeline)
- this.minHomeTimeline = minimumId.Value;
- else
- this.minMentions = minimumId.Value;
+ statuses = await this.Api.StatusesMentionsTimeline(count, maxId: tab.OldestId)
+ .ConfigureAwait(false);
+ }
+ else
+ {
+ statuses = await this.Api.StatusesMentionsTimeline(count)
+ .ConfigureAwait(false);
}
+
+ var minimumId = this.CreatePostsFromJson(statuses, MyCommon.WORKERTYPE.Reply, tab, read);
+
+ if (minimumId != null && minimumId.Value < tab.OldestId)
+ tab.OldestId = minimumId.Value;
}
- public async Task GetUserTimelineApi(bool read, string userName, TabClass tab, bool more)
+ public async Task GetUserTimelineApi(bool read, string userName, UserTimelineTabModel tab, bool more)
{
this.CheckAccountState();
TwitterStatus[] statuses;
if (string.IsNullOrEmpty(userName))
{
- var target = tab.User;
+ var target = tab.ScreenName;
if (string.IsNullOrEmpty(target)) return;
userName = target;
statuses = await this.Api.StatusesUserTimeline(userName, count)
var minimumId = CreatePostsFromJson(statuses, MyCommon.WORKERTYPE.UserTimeline, tab, read);
- if (minimumId != null)
+ if (minimumId != null && minimumId.Value < tab.OldestId)
tab.OldestId = minimumId.Value;
}
.ConfigureAwait(false);
var item = CreatePostsFromStatusData(status);
- if (item == null)
- throw new WebApiException("Err:Can't create post");
item.IsRead = read;
if (item.IsMe && !read && _readOwnPost) item.IsRead = true;
return item;
}
- public async Task GetStatusApi(bool read, long id, TabClass tab)
+ public async Task GetStatusApi(bool read, long id, TabModel tab)
{
var post = await this.GetStatusApi(read, id)
.ConfigureAwait(false);
//非同期アイコン取得&StatusDictionaryに追加
if (tab != null && tab.IsInnerStorageTabType)
- tab.AddPostToInnerStorage(post);
+ tab.AddPostQueue(post);
else
TabInformations.GetInstance().AddPost(post);
}
//Id
post.RetweetedId = retweeted.Id;
//本文
- post.TextFromApi = retweeted.Text;
+ post.TextFromApi = retweeted.FullText;
entities = retweeted.MergedEntities;
sourceHtml = retweeted.Source;
//Reply先
//以下、ユーザー情報
var user = retweeted.User;
-
- if (user == null || user.ScreenName == null || status.User.ScreenName == null) return null;
-
- post.UserId = user.Id;
- post.ScreenName = user.ScreenName;
- post.Nickname = user.Name.Trim();
- post.ImageUrl = user.ProfileImageUrlHttps;
- post.IsProtect = user.Protected;
+ if (user != null)
+ {
+ post.UserId = user.Id;
+ post.ScreenName = user.ScreenName;
+ post.Nickname = user.Name.Trim();
+ post.ImageUrl = user.ProfileImageUrlHttps;
+ post.IsProtect = user.Protected;
+ }
+ else
+ {
+ post.UserId = 0L;
+ post.ScreenName = "?????";
+ post.Nickname = "Unknown User";
+ }
//Retweetした人
- post.RetweetedBy = status.User.ScreenName;
- post.RetweetedByUserId = status.User.Id;
- post.IsMe = post.RetweetedBy.ToLowerInvariant().Equals(_uname);
+ if (status.User != null)
+ {
+ post.RetweetedBy = status.User.ScreenName;
+ post.RetweetedByUserId = status.User.Id;
+ post.IsMe = post.RetweetedBy.ToLowerInvariant().Equals(_uname);
+ }
+ else
+ {
+ post.RetweetedBy = "?????";
+ post.RetweetedByUserId = 0L;
+ }
}
else
{
post.CreatedAt = MyCommon.DateTimeParse(status.CreatedAt);
//本文
- post.TextFromApi = status.Text;
+ post.TextFromApi = status.FullText;
entities = status.MergedEntities;
sourceHtml = status.Source;
post.InReplyToStatusId = status.InReplyToStatusId;
//以下、ユーザー情報
var user = status.User;
-
- if (user == null || user.ScreenName == null) return null;
-
- post.UserId = user.Id;
- post.ScreenName = user.ScreenName;
- post.Nickname = user.Name.Trim();
- post.ImageUrl = user.ProfileImageUrlHttps;
- post.IsProtect = user.Protected;
- post.IsMe = post.ScreenName.ToLowerInvariant().Equals(_uname);
+ if (user != null)
+ {
+ post.UserId = user.Id;
+ post.ScreenName = user.ScreenName;
+ post.Nickname = user.Name.Trim();
+ post.ImageUrl = user.ProfileImageUrlHttps;
+ post.IsProtect = user.Protected;
+ post.IsMe = post.ScreenName.ToLowerInvariant().Equals(_uname);
+ }
+ else
+ {
+ post.UserId = 0L;
+ post.ScreenName = "?????";
+ post.Nickname = "Unknown User";
+ }
}
//HTMLに整形
string textFromApi = post.TextFromApi;
post.TextFromApi = this.ReplaceTextFromApi(post.TextFromApi, entities);
post.TextFromApi = WebUtility.HtmlDecode(post.TextFromApi);
post.TextFromApi = post.TextFromApi.Replace("<3", "\u2661");
+ post.AccessibleText = this.CreateAccessibleText(textFromApi, entities, (status.RetweetedStatus ?? status).QuotedStatus);
+ post.AccessibleText = WebUtility.HtmlDecode(post.AccessibleText);
+ post.AccessibleText = post.AccessibleText.Replace("<3", "\u2661");
post.QuoteStatusIds = GetQuoteTweetStatusIds(entities)
.Where(x => x != post.StatusId && x != post.RetweetedId)
.Select(x => new PostClass.ExpandedUrlInfo(x.Url, x.ExpandedUrl))
.ToArray();
+ // メモリ使用量削減 (同一のテキストであれば同一の string インスタンスを参照させる)
+ if (post.Text == post.TextFromApi)
+ post.Text = post.TextFromApi;
+ if (post.AccessibleText == post.TextFromApi)
+ post.AccessibleText = post.TextFromApi;
+
+ // 他の発言と重複しやすい (共通化できる) 文字列は string.Intern を通す
+ post.ScreenName = string.Intern(post.ScreenName);
+ post.Nickname = string.Intern(post.Nickname);
+ post.ImageUrl = string.Intern(post.ImageUrl);
+ post.RetweetedBy = post.RetweetedBy != null ? string.Intern(post.RetweetedBy) : null;
+
//Source整形
- var source = ParseSource(sourceHtml);
- post.Source = source.Item1;
- post.SourceUri = source.Item2;
+ var (sourceText, sourceUri) = ParseSource(sourceHtml);
+ post.Source = string.Intern(sourceText);
+ post.SourceUri = sourceUri;
post.IsReply = post.ReplyToList.Contains(_uname);
post.IsExcludeReply = false;
var match = Twitter.StatusUrlRegex.Match(url);
if (match.Success)
{
- long statusId;
- if (long.TryParse(match.Groups["StatusId"].Value, out statusId))
+ if (long.TryParse(match.Groups["StatusId"].Value, out var statusId))
yield return statusId;
}
}
}
- private long? CreatePostsFromJson(TwitterStatus[] items, MyCommon.WORKERTYPE gType, TabClass tab, bool read)
+ private long? CreatePostsFromJson(TwitterStatus[] items, MyCommon.WORKERTYPE gType, TabModel tab, bool read)
{
long? minimumId = null;
foreach (var status in items)
{
- PostClass post = null;
- post = CreatePostsFromStatusData(status);
- if (post == null) continue;
-
- if (minimumId == null || minimumId.Value > post.StatusId)
- minimumId = post.StatusId;
+ if (minimumId == null || minimumId.Value > status.Id)
+ minimumId = status.Id;
//二重取得回避
lock (LockObj)
{
if (tab == null)
{
- if (TabInformations.GetInstance().ContainsKey(post.StatusId)) continue;
+ if (TabInformations.GetInstance().ContainsKey(status.Id)) continue;
}
else
{
- if (tab.Contains(post.StatusId)) continue;
+ if (tab.Contains(status.Id)) continue;
}
}
//RT禁止ユーザーによるもの
if (gType != MyCommon.WORKERTYPE.UserTimeline &&
- post.RetweetedByUserId != null && this.noRTId.Contains(post.RetweetedByUserId.Value)) continue;
+ status.RetweetedStatus != null && this.noRTId.Contains(status.User.Id)) continue;
+
+ var post = CreatePostsFromStatusData(status);
post.IsRead = read;
if (post.IsMe && !read && _readOwnPost) post.IsRead = true;
- //非同期アイコン取得&StatusDictionaryに追加
if (tab != null && tab.IsInnerStorageTabType)
- tab.AddPostToInnerStorage(post);
+ tab.AddPostQueue(post);
else
TabInformations.GetInstance().AddPost(post);
}
return minimumId;
}
- private long? CreatePostsFromSearchJson(TwitterSearchResult items, TabClass tab, bool read, int count, bool more)
+ private long? CreatePostsFromSearchJson(TwitterSearchResult items, PublicSearchTabModel tab, bool read, bool more)
{
long? minimumId = null;
- foreach (var result in items.Statuses)
+ foreach (var status in items.Statuses)
{
- var post = CreatePostsFromStatusData(result);
- if (post == null)
- continue;
+ if (minimumId == null || minimumId.Value > status.Id)
+ minimumId = status.Id;
- if (minimumId == null || minimumId.Value > post.StatusId)
- minimumId = post.StatusId;
-
- if (!more && post.StatusId > tab.SinceId) tab.SinceId = post.StatusId;
+ if (!more && status.Id > tab.SinceId) tab.SinceId = status.Id;
//二重取得回避
lock (LockObj)
{
- if (tab == null)
- {
- if (TabInformations.GetInstance().ContainsKey(post.StatusId)) continue;
- }
- else
- {
- if (tab.Contains(post.StatusId)) continue;
- }
+ if (tab.Contains(status.Id)) continue;
}
+ var post = CreatePostsFromStatusData(status);
+
post.IsRead = read;
if ((post.IsMe && !read) && this._readOwnPost) post.IsRead = true;
- //非同期アイコン取得&StatusDictionaryに追加
- if (tab != null && tab.IsInnerStorageTabType)
- tab.AddPostToInnerStorage(post);
- else
- TabInformations.GetInstance().AddPost(post);
+ tab.AddPostQueue(post);
}
return minimumId;
}
- private void CreateFavoritePostsFromJson(TwitterStatus[] item, bool read)
+ private long? CreateFavoritePostsFromJson(TwitterStatus[] items, bool read)
{
var favTab = TabInformations.GetInstance().GetTabByType(MyCommon.TabUsageType.Favorites);
+ long? minimumId = null;
- foreach (var status in item)
+ foreach (var status in items)
{
+ if (minimumId == null || minimumId.Value > status.Id)
+ minimumId = status.Id;
+
//二重取得回避
lock (LockObj)
{
}
var post = CreatePostsFromStatusData(status, true);
- if (post == null) continue;
post.IsRead = read;
TabInformations.GetInstance().AddPost(post);
}
+
+ return minimumId;
}
- public async Task GetListStatus(bool read, TabClass tab, bool more, bool startup)
+ public async Task GetListStatus(bool read, ListTimelineTabModel tab, bool more, bool startup)
{
var count = GetApiResultCount(MyCommon.WORKERTYPE.List, more, startup);
TwitterStatus[] statuses;
if (more)
{
- statuses = await this.Api.ListsStatuses(tab.ListInfo.Id, count, maxId: tab.OldestId, includeRTs: SettingCommon.Instance.IsListsIncludeRts)
+ statuses = await this.Api.ListsStatuses(tab.ListInfo.Id, count, maxId: tab.OldestId, includeRTs: SettingManager.Common.IsListsIncludeRts)
.ConfigureAwait(false);
}
else
{
- statuses = await this.Api.ListsStatuses(tab.ListInfo.Id, count, includeRTs: SettingCommon.Instance.IsListsIncludeRts)
+ statuses = await this.Api.ListsStatuses(tab.ListInfo.Id, count, includeRTs: SettingManager.Common.IsListsIncludeRts)
.ConfigureAwait(false);
}
var minimumId = CreatePostsFromJson(statuses, MyCommon.WORKERTYPE.List, tab, read);
- if (minimumId != null)
+ if (minimumId != null && minimumId.Value < tab.OldestId)
tab.OldestId = minimumId.Value;
}
return nextPost;
}
- public async Task GetRelatedResult(bool read, TabClass tab)
+ public async Task GetRelatedResult(bool read, RelatedPostsTabModel tab)
{
+ var targetPost = tab.TargetPost;
var relPosts = new Dictionary<Int64, PostClass>();
- if (tab.RelationTargetPost.TextFromApi.Contains("@") && tab.RelationTargetPost.InReplyToStatusId == null)
+ if (targetPost.TextFromApi.Contains("@") && targetPost.InReplyToStatusId == null)
{
//検索結果対応
- var p = TabInformations.GetInstance()[tab.RelationTargetPost.StatusId];
+ var p = TabInformations.GetInstance()[targetPost.StatusId];
if (p != null && p.InReplyToStatusId != null)
{
- tab.RelationTargetPost = p;
+ targetPost = p;
}
else
{
- p = await this.GetStatusApi(read, tab.RelationTargetPost.StatusId)
+ p = await this.GetStatusApi(read, targetPost.StatusId)
.ConfigureAwait(false);
- tab.RelationTargetPost = p;
+ targetPost = p;
}
}
- relPosts.Add(tab.RelationTargetPost.StatusId, tab.RelationTargetPost);
+ relPosts.Add(targetPost.StatusId, targetPost);
Exception lastException = null;
// in_reply_to_status_id を使用してリプライチェインを辿る
- var nextPost = FindTopOfReplyChain(relPosts, tab.RelationTargetPost.StatusId);
+ var nextPost = FindTopOfReplyChain(relPosts, targetPost.StatusId);
var loopCount = 1;
while (nextPost.InReplyToStatusId != null && loopCount++ <= 20)
{
}
//MRTとかに対応のためツイート内にあるツイートを指すURLを取り込む
- var text = tab.RelationTargetPost.Text;
+ var text = targetPost.Text;
var ma = Twitter.StatusUrlRegex.Matches(text).Cast<Match>()
.Concat(Twitter.ThirdPartyStatusUrlRegex.Matches(text).Cast<Match>());
foreach (var _match in ma)
{
- Int64 _statusId;
- if (Int64.TryParse(_match.Groups["StatusId"].Value, out _statusId))
+ if (Int64.TryParse(_match.Groups["StatusId"].Value, out var _statusId))
{
if (relPosts.ContainsKey(_statusId))
continue;
else
p.IsRead = read;
- tab.AddPostToInnerStorage(p);
+ tab.AddPostQueue(p);
});
if (lastException != null)
throw new WebApiException(lastException.Message, lastException);
}
- public async Task GetSearch(bool read, TabClass tab, bool more)
+ public async Task GetSearch(bool read, PublicSearchTabModel tab, bool more)
{
var count = GetApiResultCount(MyCommon.WORKERTYPE.PublicSearch, more, false);
if (!TabInformations.GetInstance().ContainsTab(tab))
return;
- var minimumId = this.CreatePostsFromSearchJson(searchResult, tab, read, count, more);
+ var minimumId = this.CreatePostsFromSearchJson(searchResult, tab, read, more);
- if (minimumId != null)
+ if (minimumId != null && minimumId.Value < tab.OldestId)
tab.OldestId = minimumId.Value;
}
post.TextFromApi = this.ReplaceTextFromApi(textFromApi, message.Entities);
post.TextFromApi = WebUtility.HtmlDecode(post.TextFromApi);
post.TextFromApi = post.TextFromApi.Replace("<3", "\u2661");
+ post.AccessibleText = this.CreateAccessibleText(textFromApi, message.Entities, quoteStatus: null);
+ post.AccessibleText = WebUtility.HtmlDecode(post.AccessibleText);
+ post.AccessibleText = post.AccessibleText.Replace("<3", "\u2661");
post.IsFav = false;
post.QuoteStatusIds = GetQuoteTweetStatusIds(message.Entities).Distinct().ToArray();
post.Nickname = user.Name.Trim();
post.ImageUrl = user.ProfileImageUrlHttps;
post.IsProtect = user.Protected;
+
+ // メモリ使用量削減 (同一のテキストであれば同一の string インスタンスを参照させる)
+ if (post.Text == post.TextFromApi)
+ post.Text = post.TextFromApi;
+ if (post.AccessibleText == post.TextFromApi)
+ post.AccessibleText = post.TextFromApi;
+
+ // 他の発言と重複しやすい (共通化できる) 文字列は string.Intern を通す
+ post.ScreenName = string.Intern(post.ScreenName);
+ post.Nickname = string.Intern(post.Nickname);
+ post.ImageUrl = string.Intern(post.ImageUrl);
}
catch(Exception ex)
{
post.IsDm = true;
var dmTab = TabInformations.GetInstance().GetTabByType(MyCommon.TabUsageType.DirectMessage);
- dmTab.AddPostToInnerStorage(post);
+ dmTab.AddPostQueue(post);
}
}
CreateDirectMessagesFromJson(messages, gType, read);
}
- public async Task GetFavoritesApi(bool read, bool more)
+ public async Task GetFavoritesApi(bool read, FavoritesTabModel tab, bool backward)
{
this.CheckAccountState();
- var count = GetApiResultCount(MyCommon.WORKERTYPE.Favorites, more, false);
+ var count = GetApiResultCount(MyCommon.WORKERTYPE.Favorites, backward, false);
- var statuses = await this.Api.FavoritesList(count)
- .ConfigureAwait(false);
+ TwitterStatus[] statuses;
+ if (backward)
+ {
+ statuses = await this.Api.FavoritesList(count, maxId: tab.OldestId)
+ .ConfigureAwait(false);
+ }
+ else
+ {
+ statuses = await this.Api.FavoritesList(count)
+ .ConfigureAwait(false);
+ }
+
+ var minimumId = this.CreateFavoritePostsFromJson(statuses, read);
- CreateFavoritePostsFromJson(statuses, read);
+ if (minimumId != null && minimumId.Value < tab.OldestId)
+ tab.OldestId = minimumId.Value;
}
private string ReplaceTextFromApi(string text, TwitterEntities entities)
{
foreach (var m in entities.Media)
{
- if (m.AltText != null)
- {
- text = text.Replace(m.Url, string.Format(Properties.Resources.ImageAltText, m.AltText));
- }
- else
+ if (!string.IsNullOrEmpty(m.DisplayUrl)) text = text.Replace(m.Url, m.DisplayUrl);
+ }
+ }
+ }
+ return text;
+ }
+
+ private string CreateAccessibleText(string text, TwitterEntities entities, TwitterStatus quoteStatus)
+ {
+ if (entities == null)
+ return text;
+
+ if (entities.Urls != null)
+ {
+ foreach (var entity in entities.Urls)
+ {
+ if (quoteStatus != null)
+ {
+ var matchStatusUrl = Twitter.StatusUrlRegex.Match(entity.ExpandedUrl);
+ if (matchStatusUrl.Success && matchStatusUrl.Groups["StatusId"].Value == quoteStatus.IdStr)
{
- if (!string.IsNullOrEmpty(m.DisplayUrl)) text = text.Replace(m.Url, m.DisplayUrl);
+ var quoteText = this.CreateAccessibleText(quoteStatus.FullText, quoteStatus.MergedEntities, quoteStatus: null);
+ text = text.Replace(entity.Url, string.Format(Properties.Resources.QuoteStatus_AccessibleText, quoteStatus.User.ScreenName, quoteText));
}
}
+ else if (!string.IsNullOrEmpty(entity.DisplayUrl))
+ {
+ text = text.Replace(entity.Url, entity.DisplayUrl);
+ }
}
}
+
+ if (entities.Media != null)
+ {
+ foreach (var entity in entities.Media)
+ {
+ if (!string.IsNullOrEmpty(entity.AltText))
+ {
+ text = text.Replace(entity.Url, string.Format(Properties.Resources.ImageAltText, entity.AltText));
+ }
+ else if (!string.IsNullOrEmpty(entity.DisplayUrl))
+ {
+ text = text.Replace(entity.Url, entity.DisplayUrl);
+ }
+ }
+ }
+
return text;
}
{
this.CheckAccountState();
- var ownedLists = await TwitterLists.GetAllItemsAsync(x => this.Api.ListsOwnerships(this.Username, cursor: x))
- .ConfigureAwait(false);
+ var ownedLists = await TwitterLists.GetAllItemsAsync(x =>
+ this.Api.ListsOwnerships(this.Username, cursor: x, count: 1000))
+ .ConfigureAwait(false);
- var subscribedLists = await TwitterLists.GetAllItemsAsync(x => this.Api.ListsSubscriptions(this.Username, cursor: x))
- .ConfigureAwait(false);
+ var subscribedLists = await TwitterLists.GetAllItemsAsync(x =>
+ this.Api.ListsSubscriptions(this.Username, cursor: x, count: 1000))
+ .ConfigureAwait(false);
TabInformations.GetInstance().SubscribableLists = Enumerable.Concat(ownedLists, subscribedLists)
.Select(x => new ListElement(x, this))
/// <summary>
/// Twitter APIから得たHTML形式のsource文字列を分析し、source名とURLに分離します
/// </summary>
- public static Tuple<string, Uri> ParseSource(string sourceHtml)
+ internal static (string SourceText, Uri SourceUri) ParseSource(string sourceHtml)
{
if (string.IsNullOrEmpty(sourceHtml))
- return Tuple.Create<string, Uri>("", null);
+ return ("", null);
string sourceText;
Uri sourceUri;
sourceUri = null;
}
- return Tuple.Create(sourceText, sourceUri);
+ return (sourceText, sourceUri);
}
public async Task<TwitterApiStatus> GetInfoApi()
throw new WebApiException("Auth Err:try to re-authorization.");
}
- private void CheckStatusCode(HttpStatusCode httpStatus, string responseText,
- [CallerMemberName] string callerMethodName = "")
- {
- if (httpStatus == HttpStatusCode.OK)
- {
- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
- return;
- }
-
- if (string.IsNullOrWhiteSpace(responseText))
- {
- if (httpStatus == HttpStatusCode.Unauthorized)
- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
-
- throw new WebApiException("Err:" + httpStatus + "(" + callerMethodName + ")");
- }
-
- try
- {
- var errors = TwitterError.ParseJson(responseText).Errors;
- if (errors == null || !errors.Any())
- {
- throw new WebApiException("Err:" + httpStatus + "(" + callerMethodName + ")", responseText);
- }
-
- foreach (var error in errors)
- {
- if (error.Code == TwitterErrorCode.InvalidToken ||
- error.Code == TwitterErrorCode.SuspendedAccount)
- {
- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
- }
- }
-
- throw new WebApiException("Err:" + string.Join(",", errors.Select(x => x.ToString())) + "(" + callerMethodName + ")", responseText);
- }
- catch (SerializationException) { }
-
- throw new WebApiException("Err:" + httpStatus + "(" + callerMethodName + ")", responseText);
- }
-
public int GetTextLengthRemain(string postText)
{
var matchDm = Twitter.DMSendTextRegex.Match(postText);
{
try
{
- var status = TwitterStatus.ParseJson(line);
- this.CreatePostsFromJson(new[] { status }, MyCommon.WORKERTYPE.UserStream, null, false);
+ var status = TwitterStatusCompat.ParseJson(line);
+ this.CreatePostsFromJson(new[] { status.Normalize() }, MyCommon.WORKERTYPE.UserStream, null, false);
}
catch (SerializationException ex)
{
evt.Username = eventData.Source.ScreenName;
evt.IsMe = evt.Username.ToLowerInvariant().Equals(this.Username.ToLowerInvariant());
- MyCommon.EVENTTYPE eventType;
- eventTable.TryGetValue(eventData.Event, out eventType);
+ eventTable.TryGetValue(eventData.Event, out var eventType);
evt.Eventtype = eventType;
- TwitterStreamEvent<TwitterStatus> tweetEvent;
+ TwitterStreamEvent<TwitterStatusCompat> tweetEvent;
+ TwitterStatus tweet;
switch (eventData.Event)
{
return;
case "favorite":
case "unfavorite":
- tweetEvent = TwitterStreamEvent<TwitterStatus>.ParseJson(content);
- evt.Target = "@" + tweetEvent.TargetObject.User.ScreenName + ":" + WebUtility.HtmlDecode(tweetEvent.TargetObject.Text);
- evt.Id = tweetEvent.TargetObject.Id;
+ tweetEvent = TwitterStreamEvent<TwitterStatusCompat>.ParseJson(content);
+ tweet = tweetEvent.TargetObject.Normalize();
+ evt.Target = "@" + tweet.User.ScreenName + ":" + WebUtility.HtmlDecode(tweet.FullText);
+ evt.Id = tweet.Id;
- if (SettingCommon.Instance.IsRemoveSameEvent)
+ if (SettingManager.Common.IsRemoveSameEvent)
{
if (this.StoredEvent.Any(ev => ev.Username == evt.Username && ev.Eventtype == evt.Eventtype && ev.Target == evt.Target))
return;
var tabinfo = TabInformations.GetInstance();
- PostClass post;
- var statusId = tweetEvent.TargetObject.Id;
- if (!tabinfo.Posts.TryGetValue(statusId, out post))
+ var statusId = tweet.Id;
+ if (!tabinfo.Posts.TryGetValue(statusId, out var post))
break;
if (eventData.Event == "favorite")
{
var favTab = tabinfo.GetTabByType(MyCommon.TabUsageType.Favorites);
- if (!favTab.Contains(post.StatusId))
- favTab.AddPostImmediately(post.StatusId, post.IsRead);
+ favTab.AddPostQueue(post);
if (tweetEvent.Source.Id == this.UserId)
{
{
post.FavoritedCount++;
- if (SettingCommon.Instance.FavEventUnread)
+ if (SettingManager.Common.FavEventUnread)
tabinfo.SetReadAllTab(post.StatusId, read: false);
}
}
case "quoted_tweet":
if (evt.IsMe) return;
- tweetEvent = TwitterStreamEvent<TwitterStatus>.ParseJson(content);
- evt.Target = "@" + tweetEvent.TargetObject.User.ScreenName + ":" + WebUtility.HtmlDecode(tweetEvent.TargetObject.Text);
- evt.Id = tweetEvent.TargetObject.Id;
+ tweetEvent = TwitterStreamEvent<TwitterStatusCompat>.ParseJson(content);
+ tweet = tweetEvent.TargetObject.Normalize();
+ evt.Target = "@" + tweet.User.ScreenName + ":" + WebUtility.HtmlDecode(tweet.FullText);
+ evt.Id = tweet.Id;
- if (SettingCommon.Instance.IsRemoveSameEvent)
+ if (SettingManager.Common.IsRemoveSameEvent)
{
if (this.StoredEvent.Any(ev => ev.Username == evt.Username && ev.Eventtype == evt.Eventtype && ev.Target == evt.Target))
return;
}
public bool UserStreamActive
- => this.userStream == null ? false : this.userStream.IsStreamActive;
+ => this.userStream != null && this.userStream.IsStreamActive;
public void StartUserStream()
{
public void ReconnectUserStream()
{
- this.StartUserStream();
+ if (this.userStream != null)
+ {
+ this.StartUserStream();
+ }
}
private class TwitterUserstream : IDisposable
private async Task UserStreamLoop(CancellationToken cancellationToken)
{
- TimeSpan? sleep = null;
+ TimeSpan sleep = TimeSpan.Zero;
for (;;)
{
- if (sleep != null)
+ if (sleep != TimeSpan.Zero)
{
- await Task.Delay(sleep.Value, cancellationToken)
+ await Task.Delay(sleep, cancellationToken)
.ConfigureAwait(false);
- sleep = null;
+ sleep = TimeSpan.Zero;
}
if (!MyCommon.IsNetworkAvailable())