OSDN Git Service

ツイート文字数の280文字への上限緩和 (weightedLength) に対応
[opentween/open-tween.git] / OpenTween / Twitter.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2007-2011 kiri_feather (@kiri_feather) <kiri.feather@gmail.com>
3 //           (c) 2008-2011 Moz (@syo68k)
4 //           (c) 2008-2011 takeshik (@takeshik) <http://www.takeshik.org/>
5 //           (c) 2010-2011 anis774 (@anis774) <http://d.hatena.ne.jp/anis774/>
6 //           (c) 2010-2011 fantasticswallow (@f_swallow) <http://twitter.com/f_swallow>
7 //           (c) 2011      Egtra (@egtra) <http://dev.activebasic.com/egtra/>
8 //           (c) 2013      kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
9 // All rights reserved.
10 //
11 // This file is part of OpenTween.
12 //
13 // This program is free software; you can redistribute it and/or modify it
14 // under the terms of the GNU General Public License as published by the Free
15 // Software Foundation; either version 3 of the License, or (at your option)
16 // any later version.
17 //
18 // This program is distributed in the hope that it will be useful, but
19 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 // for more details.
22 //
23 // You should have received a copy of the GNU General Public License along
24 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
25 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
26 // Boston, MA 02110-1301, USA.
27
28 using System.Diagnostics;
29 using System.IO;
30 using System.Linq;
31 using System.Net;
32 using System.Net.Http;
33 using System.Runtime.CompilerServices;
34 using System.Runtime.Serialization;
35 using System.Runtime.Serialization.Json;
36 using System.Text;
37 using System.Text.RegularExpressions;
38 using System.Threading;
39 using System.Threading.Tasks;
40 using System.Web;
41 using System.Xml;
42 using System.Xml.Linq;
43 using System.Xml.XPath;
44 using System;
45 using System.Reflection;
46 using System.Collections.Generic;
47 using System.Drawing;
48 using System.Windows.Forms;
49 using OpenTween.Api;
50 using OpenTween.Api.DataModel;
51 using OpenTween.Connection;
52 using OpenTween.Models;
53 using System.Drawing.Imaging;
54 using OpenTween.Setting;
55
56 namespace OpenTween
57 {
58     public class Twitter : IDisposable
59     {
60         #region Regexp from twitter-text-js
61
62         // The code in this region code block incorporates works covered by
63         // the following copyright and permission notices:
64         //
65         //   Copyright 2011 Twitter, Inc.
66         //
67         //   Licensed under the Apache License, Version 2.0 (the "License"); you
68         //   may not use this work except in compliance with the License. You
69         //   may obtain a copy of the License in the LICENSE file, or at:
70         //
71         //   http://www.apache.org/licenses/LICENSE-2.0
72         //
73         //   Unless required by applicable law or agreed to in writing, software
74         //   distributed under the License is distributed on an "AS IS" BASIS,
75         //   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
76         //   implied. See the License for the specific language governing
77         //   permissions and limitations under the License.
78
79         //Hashtag用正規表現
80         private const string LATIN_ACCENTS = @"\u00c0-\u00d6\u00d8-\u00f6\u00f8-\u00ff\u0100-\u024f\u0253\u0254\u0256\u0257\u0259\u025b\u0263\u0268\u026f\u0272\u0289\u028b\u02bb\u1e00-\u1eff";
81         private const string NON_LATIN_HASHTAG_CHARS = @"\u0400-\u04ff\u0500-\u0527\u1100-\u11ff\u3130-\u3185\uA960-\uA97F\uAC00-\uD7AF\uD7B0-\uD7FF";
82         //private const string CJ_HASHTAG_CHARACTERS = @"\u30A1-\u30FA\uFF66-\uFF9F\uFF10-\uFF19\uFF21-\uFF3A\uFF41-\uFF5A\u3041-\u3096\u3400-\u4DBF\u4E00-\u9FFF\u20000-\u2A6DF\u2A700-\u2B73F\u2B740-\u2B81F\u2F800-\u2FA1F";
83         private const string CJ_HASHTAG_CHARACTERS = @"\u30A1-\u30FA\u30FC\u3005\uFF66-\uFF9F\uFF10-\uFF19\uFF21-\uFF3A\uFF41-\uFF5A\u3041-\u309A\u3400-\u4DBF\p{IsCJKUnifiedIdeographs}";
84         private const string HASHTAG_BOUNDARY = @"^|$|\s|「|」|。|\.|!";
85         private const string HASHTAG_ALPHA = "[a-z_" + LATIN_ACCENTS + NON_LATIN_HASHTAG_CHARS + CJ_HASHTAG_CHARACTERS + "]";
86         private const string HASHTAG_ALPHANUMERIC = "[a-z0-9_" + LATIN_ACCENTS + NON_LATIN_HASHTAG_CHARS + CJ_HASHTAG_CHARACTERS + "]";
87         private const string HASHTAG_TERMINATOR = "[^a-z0-9_" + LATIN_ACCENTS + NON_LATIN_HASHTAG_CHARS + CJ_HASHTAG_CHARACTERS + "]";
88         public const string HASHTAG = "(" + HASHTAG_BOUNDARY + ")(#|#)(" + HASHTAG_ALPHANUMERIC + "*" + HASHTAG_ALPHA + HASHTAG_ALPHANUMERIC + "*)(?=" + HASHTAG_TERMINATOR + "|" + HASHTAG_BOUNDARY + ")";
89         //URL正規表現
90         private const string url_valid_preceding_chars = @"(?:[^A-Za-z0-9@@$##\ufffe\ufeff\uffff\u202a-\u202e]|^)";
91         public const string url_invalid_without_protocol_preceding_chars = @"[-_./]$";
92         private const string url_invalid_domain_chars = @"\!'#%&'\(\)*\+,\\\-\.\/:;<=>\?@\[\]\^_{|}~\$\u2000-\u200a\u0009-\u000d\u0020\u0085\u00a0\u1680\u180e\u2028\u2029\u202f\u205f\u3000\ufffe\ufeff\uffff\u202a-\u202e";
93         private const string url_valid_domain_chars = @"[^" + url_invalid_domain_chars + "]";
94         private const string url_valid_subdomain = @"(?:(?:" + url_valid_domain_chars + @"(?:[_-]|" + url_valid_domain_chars + @")*)?" + url_valid_domain_chars + @"\.)";
95         private const string url_valid_domain_name = @"(?:(?:" + url_valid_domain_chars + @"(?:-|" + url_valid_domain_chars + @")*)?" + url_valid_domain_chars + @"\.)";
96         private const string url_valid_GTLD = @"(?:(?:aero|asia|biz|cat|com|coop|edu|gov|info|int|jobs|mil|mobi|museum|name|net|org|pro|tel|travel|xxx)(?=[^0-9a-zA-Z]|$))";
97         private const string url_valid_CCTLD = @"(?:(?:ac|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|cr|cs|cu|cv|cx|cy|cz|dd|de|dj|dk|dm|do|dz|ec|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|io|iq|ir|is|it|je|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|mv|mw|mx|my|mz|na|nc|ne|nf|ng|ni|nl|no|np|nr|nu|nz|om|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|ps|pt|pw|py|qa|re|ro|rs|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|ss|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tl|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|za|zm|zw)(?=[^0-9a-zA-Z]|$))";
98         private const string url_valid_punycode = @"(?:xn--[0-9a-z]+)";
99         private const string url_valid_domain = @"(?<domain>" + url_valid_subdomain + "*" + url_valid_domain_name + "(?:" + url_valid_GTLD + "|" + url_valid_CCTLD + ")|" + url_valid_punycode + ")";
100         public const string url_valid_ascii_domain = @"(?:(?:[a-z0-9" + LATIN_ACCENTS + @"]+)\.)+(?:" + url_valid_GTLD + "|" + url_valid_CCTLD + "|" + url_valid_punycode + ")";
101         public const string url_invalid_short_domain = "^" + url_valid_domain_name + url_valid_CCTLD + "$";
102         private const string url_valid_port_number = @"[0-9]+";
103
104         private const string url_valid_general_path_chars = @"[a-z0-9!*';:=+,.$/%#\[\]\-_~|&" + LATIN_ACCENTS + "]";
105         private const string url_balance_parens = @"(?:\(" + url_valid_general_path_chars + @"+\))";
106         private const string url_valid_path_ending_chars = @"(?:[+\-a-z0-9=_#/" + LATIN_ACCENTS + "]|" + url_balance_parens + ")";
107         private const string pth = "(?:" +
108             "(?:" +
109                 url_valid_general_path_chars + "*" +
110                 "(?:" + url_balance_parens + url_valid_general_path_chars + "*)*" +
111                 url_valid_path_ending_chars +
112                 ")|(?:@" + url_valid_general_path_chars + "+/)" +
113             ")";
114         private const string qry = @"(?<query>\?[a-z0-9!?*'();:&=+$/%#\[\]\-_.,~|]*[a-z0-9_&=#/])?";
115         public const string rgUrl = @"(?<before>" + url_valid_preceding_chars + ")" +
116                                     "(?<url>(?<protocol>https?://)?" +
117                                     "(?<domain>" + url_valid_domain + ")" +
118                                     "(?::" + url_valid_port_number + ")?" +
119                                     "(?<path>/" + pth + "*)?" +
120                                     qry +
121                                     ")";
122
123         #endregion
124
125         /// <summary>
126         /// Twitter API のステータスページのURL
127         /// </summary>
128         public const string ServiceAvailabilityStatusUrl = "https://status.io.watchmouse.com/7617";
129
130         /// <summary>
131         /// ツイートへのパーマリンクURLを判定する正規表現
132         /// </summary>
133         public static readonly Regex StatusUrlRegex = new Regex(@"https?://([^.]+\.)?twitter\.com/(#!/)?(?<ScreenName>[a-zA-Z0-9_]+)/status(es)?/(?<StatusId>[0-9]+)(/photo)?", RegexOptions.IgnoreCase);
134
135         /// <summary>
136         /// FavstarやaclogなどTwitter関連サービスのパーマリンクURLからステータスIDを抽出する正規表現
137         /// </summary>
138         public static readonly Regex ThirdPartyStatusUrlRegex = new Regex(@"https?://(?:[^.]+\.)?(?:
139   favstar\.fm/users/[a-zA-Z0-9_]+/status/       # Favstar
140 | favstar\.fm/t/                                # Favstar (short)
141 | aclog\.koba789\.com/i/                        # aclog
142 | frtrt\.net/solo_status\.php\?status=          # RtRT
143 )(?<StatusId>[0-9]+)", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
144
145         /// <summary>
146         /// DM送信かどうかを判定する正規表現
147         /// </summary>
148         public static readonly Regex DMSendTextRegex = new Regex(@"^DM? +(?<id>[a-zA-Z0-9_]+) +(?<body>.*)", RegexOptions.IgnoreCase | RegexOptions.Singleline);
149
150         public TwitterApi Api { get; }
151         public TwitterConfiguration Configuration { get; private set; }
152         public TwitterTextConfiguration TextConfiguration { get; private set; }
153
154         delegate void GetIconImageDelegate(PostClass post);
155         private readonly object LockObj = new object();
156         private ISet<long> followerId = new HashSet<long>();
157         private bool _GetFollowerResult = false;
158         private long[] noRTId = new long[0];
159         private bool _GetNoRetweetResult = false;
160
161         //プロパティからアクセスされる共通情報
162         private string _uname;
163
164         private bool _readOwnPost;
165         private List<string> _hashList = new List<string>();
166
167         //max_idで古い発言を取得するために保持(lists分は個別タブで管理)
168         private long minDirectmessage = long.MaxValue;
169         private long minDirectmessageSent = long.MaxValue;
170
171         private long previousStatusId = -1L;
172
173         //private FavoriteQueue favQueue;
174
175         //private List<PostClass> _deletemessages = new List<PostClass>();
176
177         public Twitter() : this(new TwitterApi())
178         {
179         }
180
181         public Twitter(TwitterApi api)
182         {
183             this.Api = api;
184             this.Configuration = TwitterConfiguration.DefaultConfiguration();
185             this.TextConfiguration = TwitterTextConfiguration.DefaultConfiguration();
186         }
187
188         public TwitterApiAccessLevel AccessLevel
189         {
190             get
191             {
192                 return MyCommon.TwitterApiInfo.AccessLevel;
193             }
194         }
195
196         protected void ResetApiStatus()
197         {
198             MyCommon.TwitterApiInfo.Reset();
199         }
200
201         public void ClearAuthInfo()
202         {
203             Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
204             this.ResetApiStatus();
205         }
206
207         [Obsolete]
208         public void VerifyCredentials()
209         {
210             try
211             {
212                 this.VerifyCredentialsAsync().Wait();
213             }
214             catch (AggregateException ex) when (ex.InnerException is WebApiException)
215             {
216                 throw new WebApiException(ex.InnerException.Message, ex);
217             }
218         }
219
220         public async Task VerifyCredentialsAsync()
221         {
222             var user = await this.Api.AccountVerifyCredentials()
223                 .ConfigureAwait(false);
224
225             this.UpdateUserStats(user);
226         }
227
228         public void Initialize(string token, string tokenSecret, string username, long userId)
229         {
230             //OAuth認証
231             if (string.IsNullOrEmpty(token) || string.IsNullOrEmpty(tokenSecret) || string.IsNullOrEmpty(username))
232             {
233                 Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
234             }
235             this.ResetApiStatus();
236             this.Api.Initialize(token, tokenSecret, userId, username);
237             _uname = username.ToLowerInvariant();
238             if (SettingManager.Common.UserstreamStartup) this.ReconnectUserStream();
239         }
240
241         public string PreProcessUrl(string orgData)
242         {
243             int posl1;
244             var posl2 = 0;
245             //var IDNConveter = new IdnMapping();
246             var href = "<a href=\"";
247
248             while (true)
249             {
250                 if (orgData.IndexOf(href, posl2, StringComparison.Ordinal) > -1)
251                 {
252                     var urlStr = "";
253                     // IDN展開
254                     posl1 = orgData.IndexOf(href, posl2, StringComparison.Ordinal);
255                     posl1 += href.Length;
256                     posl2 = orgData.IndexOf("\"", posl1, StringComparison.Ordinal);
257                     urlStr = orgData.Substring(posl1, posl2 - posl1);
258
259                     if (!urlStr.StartsWith("http://", StringComparison.Ordinal)
260                         && !urlStr.StartsWith("https://", StringComparison.Ordinal)
261                         && !urlStr.StartsWith("ftp://", StringComparison.Ordinal))
262                     {
263                         continue;
264                     }
265
266                     var replacedUrl = MyCommon.IDNEncode(urlStr);
267                     if (replacedUrl == null) continue;
268                     if (replacedUrl == urlStr) continue;
269
270                     orgData = orgData.Replace("<a href=\"" + urlStr, "<a href=\"" + replacedUrl);
271                     posl2 = 0;
272                 }
273                 else
274                 {
275                     break;
276                 }
277             }
278             return orgData;
279         }
280
281         public async Task PostStatus(string postStr, long? reply_to, IReadOnlyList<long> mediaIds = null)
282         {
283             this.CheckAccountState();
284
285             if (Twitter.DMSendTextRegex.IsMatch(postStr))
286             {
287                 await this.SendDirectMessage(postStr)
288                     .ConfigureAwait(false);
289                 return;
290             }
291
292             var response = await this.Api.StatusesUpdate(postStr, reply_to, mediaIds)
293                 .ConfigureAwait(false);
294
295             var status = await response.LoadJsonAsync()
296                 .ConfigureAwait(false);
297
298             this.UpdateUserStats(status.User);
299
300             if (status.Id == this.previousStatusId)
301                 throw new WebApiException("OK:Delaying?");
302
303             this.previousStatusId = status.Id;
304         }
305
306         public Task<long> UploadMedia(IMediaItem item)
307             => this.UploadMedia(item, SettingManager.Common.AlphaPNGWorkaround);
308
309         public async Task<long> UploadMedia(IMediaItem item, bool alphaPNGWorkaround)
310         {
311             this.CheckAccountState();
312
313             LazyJson<TwitterUploadMediaResult> response;
314
315             using (var origImage = item.CreateImage())
316             {
317                 if (alphaPNGWorkaround && this.AddAlphaChannelIfNeeded(origImage.Image, out var newImage))
318                 {
319                     using (var newMediaItem = new MemoryImageMediaItem(newImage))
320                     {
321                         response = await this.Api.MediaUpload(newMediaItem)
322                             .ConfigureAwait(false);
323                     }
324                 }
325                 else
326                 {
327                     response = await this.Api.MediaUpload(item)
328                         .ConfigureAwait(false);
329                 }
330             }
331
332             var media = await response.LoadJsonAsync()
333                 .ConfigureAwait(false);
334
335             return media.MediaId;
336         }
337
338         /// <summary>
339         /// pic.twitter.com アップロード時に JPEG への変換を回避するための加工を行う
340         /// </summary>
341         /// <remarks>
342         /// pic.twitter.com へのアップロード時に、アルファチャンネルを持たない PNG 画像が
343         /// JPEG 形式に変換され画質が低下する問題を回避します。
344         /// PNG 以外の画像や、すでにアルファチャンネルを持つ PNG 画像に対しては何もしません。
345         /// </remarks>
346         /// <returns>加工が行われた場合は true、そうでない場合は false</returns>
347         private bool AddAlphaChannelIfNeeded(Image origImage, out MemoryImage newImage)
348         {
349             newImage = null;
350
351             // PNG 画像以外に対しては何もしない
352             if (origImage.RawFormat.Guid != ImageFormat.Png.Guid)
353                 return false;
354
355             using (var bitmap = new Bitmap(origImage))
356             {
357                 // アルファ値が 255 以外のピクセルが含まれていた場合は何もしない
358                 foreach (var x in Enumerable.Range(0, bitmap.Width))
359                 {
360                     foreach (var y in Enumerable.Range(0, bitmap.Height))
361                     {
362                         if (bitmap.GetPixel(x, y).A != 255)
363                             return false;
364                     }
365                 }
366
367                 // 左上の 1px だけアルファ値を 254 にする
368                 var pixel = bitmap.GetPixel(0, 0);
369                 var newPixel = Color.FromArgb(pixel.A - 1, pixel.R, pixel.G, pixel.B);
370                 bitmap.SetPixel(0, 0, newPixel);
371
372                 // MemoryImage 作成時に画像はコピーされるため、この後 bitmap は破棄しても問題ない
373                 newImage = MemoryImage.CopyFromImage(bitmap);
374
375                 return true;
376             }
377         }
378
379         public async Task SendDirectMessage(string postStr)
380         {
381             this.CheckAccountState();
382             this.CheckAccessLevel(TwitterApiAccessLevel.ReadWriteAndDirectMessage);
383
384             var mc = Twitter.DMSendTextRegex.Match(postStr);
385
386             var response = await this.Api.DirectMessagesNew(mc.Groups["body"].Value, mc.Groups["id"].Value)
387                 .ConfigureAwait(false);
388
389             var dm = await response.LoadJsonAsync()
390                 .ConfigureAwait(false);
391
392             this.UpdateUserStats(dm.Sender);
393         }
394
395         public async Task PostRetweet(long id, bool read)
396         {
397             this.CheckAccountState();
398
399             //データ部分の生成
400             var post = TabInformations.GetInstance()[id];
401             if (post == null)
402                 throw new WebApiException("Err:Target isn't found.");
403
404             var target = post.RetweetedId ?? id;  //再RTの場合は元発言をRT
405
406             var response = await this.Api.StatusesRetweet(target)
407                 .ConfigureAwait(false);
408
409             var status = await response.LoadJsonAsync()
410                 .ConfigureAwait(false);
411
412             //二重取得回避
413             lock (LockObj)
414             {
415                 if (TabInformations.GetInstance().ContainsKey(status.Id))
416                     return;
417             }
418
419             //Retweet判定
420             if (status.RetweetedStatus == null)
421                 throw new WebApiException("Invalid Json!");
422
423             //ReTweetしたものをTLに追加
424             post = CreatePostsFromStatusData(status);
425             
426             //ユーザー情報
427             post.IsMe = true;
428
429             post.IsRead = read;
430             post.IsOwl = false;
431             if (_readOwnPost) post.IsRead = true;
432             post.IsDm = false;
433
434             TabInformations.GetInstance().AddPost(post);
435         }
436
437         public string Username
438             => this.Api.CurrentScreenName;
439
440         public long UserId
441             => this.Api.CurrentUserId;
442
443         private static MyCommon.ACCOUNT_STATE _accountState = MyCommon.ACCOUNT_STATE.Valid;
444         public static MyCommon.ACCOUNT_STATE AccountState
445         {
446             get
447             {
448                 return _accountState;
449             }
450             set
451             {
452                 _accountState = value;
453             }
454         }
455
456         public bool RestrictFavCheck { get; set; }
457
458         public bool ReadOwnPost
459         {
460             get
461             {
462                 return _readOwnPost;
463             }
464             set
465             {
466                 _readOwnPost = value;
467             }
468         }
469
470         public int FollowersCount { get; private set; }
471         public int FriendsCount { get; private set; }
472         public int StatusesCount { get; private set; }
473         public string Location { get; private set; } = "";
474         public string Bio { get; private set; } = "";
475
476         /// <summary>ユーザーのフォロワー数などの情報を更新します</summary>
477         private void UpdateUserStats(TwitterUser self)
478         {
479             this.FollowersCount = self.FollowersCount;
480             this.FriendsCount = self.FriendsCount;
481             this.StatusesCount = self.StatusesCount;
482             this.Location = self.Location;
483             this.Bio = self.Description;
484         }
485
486         /// <summary>
487         /// 渡された取得件数がWORKERTYPEに応じた取得可能範囲に収まっているか検証する
488         /// </summary>
489         public static bool VerifyApiResultCount(MyCommon.WORKERTYPE type, int count)
490         {
491             return count >= 20 && count <= GetMaxApiResultCount(type);
492         }
493
494         /// <summary>
495         /// 渡された取得件数が更新時の取得可能範囲に収まっているか検証する
496         /// </summary>
497         public static bool VerifyMoreApiResultCount(int count)
498         {
499             return count >= 20 && count <= 200;
500         }
501
502         /// <summary>
503         /// 渡された取得件数が起動時の取得可能範囲に収まっているか検証する
504         /// </summary>
505         public static bool VerifyFirstApiResultCount(int count)
506         {
507             return count >= 20 && count <= 200;
508         }
509
510         /// <summary>
511         /// WORKERTYPEに応じた取得可能な最大件数を取得する
512         /// </summary>
513         public static int GetMaxApiResultCount(MyCommon.WORKERTYPE type)
514         {
515             // 参照: REST APIs - 各endpointのcountパラメータ
516             // https://dev.twitter.com/rest/public
517             switch (type)
518             {
519                 case MyCommon.WORKERTYPE.Timeline:
520                 case MyCommon.WORKERTYPE.Reply:
521                 case MyCommon.WORKERTYPE.UserTimeline:
522                 case MyCommon.WORKERTYPE.Favorites:
523                 case MyCommon.WORKERTYPE.DirectMessegeRcv:
524                 case MyCommon.WORKERTYPE.DirectMessegeSnt:
525                 case MyCommon.WORKERTYPE.List:  // 不明
526                     return 200;
527
528                 case MyCommon.WORKERTYPE.PublicSearch:
529                     return 100;
530
531                 default:
532                     throw new InvalidOperationException("Invalid type: " + type);
533             }
534         }
535
536         /// <summary>
537         /// WORKERTYPEに応じた取得件数を取得する
538         /// </summary>
539         public static int GetApiResultCount(MyCommon.WORKERTYPE type, bool more, bool startup)
540         {
541             if (type == MyCommon.WORKERTYPE.DirectMessegeRcv ||
542                 type == MyCommon.WORKERTYPE.DirectMessegeSnt)
543             {
544                 return 20;
545             }
546
547             if (SettingManager.Common.UseAdditionalCount)
548             {
549                 switch (type)
550                 {
551                     case MyCommon.WORKERTYPE.Favorites:
552                         if (SettingManager.Common.FavoritesCountApi != 0)
553                             return SettingManager.Common.FavoritesCountApi;
554                         break;
555                     case MyCommon.WORKERTYPE.List:
556                         if (SettingManager.Common.ListCountApi != 0)
557                             return SettingManager.Common.ListCountApi;
558                         break;
559                     case MyCommon.WORKERTYPE.PublicSearch:
560                         if (SettingManager.Common.SearchCountApi != 0)
561                             return SettingManager.Common.SearchCountApi;
562                         break;
563                     case MyCommon.WORKERTYPE.UserTimeline:
564                         if (SettingManager.Common.UserTimelineCountApi != 0)
565                             return SettingManager.Common.UserTimelineCountApi;
566                         break;
567                 }
568                 if (more && SettingManager.Common.MoreCountApi != 0)
569                 {
570                     return Math.Min(SettingManager.Common.MoreCountApi, GetMaxApiResultCount(type));
571                 }
572                 if (startup && SettingManager.Common.FirstCountApi != 0 && type != MyCommon.WORKERTYPE.Reply)
573                 {
574                     return Math.Min(SettingManager.Common.FirstCountApi, GetMaxApiResultCount(type));
575                 }
576             }
577
578             // 上記に当てはまらない場合の共通処理
579             var count = SettingManager.Common.CountApi;
580
581             if (type == MyCommon.WORKERTYPE.Reply)
582                 count = SettingManager.Common.CountApiReply;
583
584             return Math.Min(count, GetMaxApiResultCount(type));
585         }
586
587         public async Task GetHomeTimelineApi(bool read, HomeTabModel tab, bool more, bool startup)
588         {
589             this.CheckAccountState();
590
591             var count = GetApiResultCount(MyCommon.WORKERTYPE.Timeline, more, startup);
592
593             TwitterStatus[] statuses;
594             if (more)
595             {
596                 statuses = await this.Api.StatusesHomeTimeline(count, maxId: tab.OldestId)
597                     .ConfigureAwait(false);
598             }
599             else
600             {
601                 statuses = await this.Api.StatusesHomeTimeline(count)
602                     .ConfigureAwait(false);
603             }
604
605             var minimumId = this.CreatePostsFromJson(statuses, MyCommon.WORKERTYPE.Timeline, tab, read);
606             if (minimumId != null)
607                 tab.OldestId = minimumId.Value;
608         }
609
610         public async Task GetMentionsTimelineApi(bool read, MentionsTabModel tab, bool more, bool startup)
611         {
612             this.CheckAccountState();
613
614             var count = GetApiResultCount(MyCommon.WORKERTYPE.Reply, more, startup);
615
616             TwitterStatus[] statuses;
617             if (more)
618             {
619                 statuses = await this.Api.StatusesMentionsTimeline(count, maxId: tab.OldestId)
620                     .ConfigureAwait(false);
621             }
622             else
623             {
624                 statuses = await this.Api.StatusesMentionsTimeline(count)
625                     .ConfigureAwait(false);
626             }
627
628             var minimumId = this.CreatePostsFromJson(statuses, MyCommon.WORKERTYPE.Reply, tab, read);
629             if (minimumId != null)
630                 tab.OldestId = minimumId.Value;
631         }
632
633         public async Task GetUserTimelineApi(bool read, string userName, UserTimelineTabModel tab, bool more)
634         {
635             this.CheckAccountState();
636
637             var count = GetApiResultCount(MyCommon.WORKERTYPE.UserTimeline, more, false);
638
639             TwitterStatus[] statuses;
640             if (string.IsNullOrEmpty(userName))
641             {
642                 var target = tab.ScreenName;
643                 if (string.IsNullOrEmpty(target)) return;
644                 userName = target;
645                 statuses = await this.Api.StatusesUserTimeline(userName, count)
646                     .ConfigureAwait(false);
647             }
648             else
649             {
650                 if (more)
651                 {
652                     statuses = await this.Api.StatusesUserTimeline(userName, count, maxId: tab.OldestId)
653                         .ConfigureAwait(false);
654                 }
655                 else
656                 {
657                     statuses = await this.Api.StatusesUserTimeline(userName, count)
658                         .ConfigureAwait(false);
659                 }
660             }
661
662             var minimumId = CreatePostsFromJson(statuses, MyCommon.WORKERTYPE.UserTimeline, tab, read);
663
664             if (minimumId != null)
665                 tab.OldestId = minimumId.Value;
666         }
667
668         public async Task<PostClass> GetStatusApi(bool read, long id)
669         {
670             this.CheckAccountState();
671
672             var status = await this.Api.StatusesShow(id)
673                 .ConfigureAwait(false);
674
675             var item = CreatePostsFromStatusData(status);
676
677             item.IsRead = read;
678             if (item.IsMe && !read && _readOwnPost) item.IsRead = true;
679
680             return item;
681         }
682
683         public async Task GetStatusApi(bool read, long id, TabModel tab)
684         {
685             var post = await this.GetStatusApi(read, id)
686                 .ConfigureAwait(false);
687
688             //非同期アイコン取得&StatusDictionaryに追加
689             if (tab != null && tab.IsInnerStorageTabType)
690                 tab.AddPostQueue(post);
691             else
692                 TabInformations.GetInstance().AddPost(post);
693         }
694
695         private PostClass CreatePostsFromStatusData(TwitterStatus status)
696         {
697             return CreatePostsFromStatusData(status, false);
698         }
699
700         private PostClass CreatePostsFromStatusData(TwitterStatus status, bool favTweet)
701         {
702             var post = new PostClass();
703             TwitterEntities entities;
704             string sourceHtml;
705
706             post.StatusId = status.Id;
707             if (status.RetweetedStatus != null)
708             {
709                 var retweeted = status.RetweetedStatus;
710
711                 post.CreatedAt = MyCommon.DateTimeParse(retweeted.CreatedAt);
712
713                 //Id
714                 post.RetweetedId = retweeted.Id;
715                 //本文
716                 post.TextFromApi = retweeted.FullText;
717                 entities = retweeted.MergedEntities;
718                 sourceHtml = retweeted.Source;
719                 //Reply先
720                 post.InReplyToStatusId = retweeted.InReplyToStatusId;
721                 post.InReplyToUser = retweeted.InReplyToScreenName;
722                 post.InReplyToUserId = status.InReplyToUserId;
723
724                 if (favTweet)
725                 {
726                     post.IsFav = true;
727                 }
728                 else
729                 {
730                     //幻覚fav対策
731                     var tc = TabInformations.GetInstance().GetTabByType(MyCommon.TabUsageType.Favorites);
732                     post.IsFav = tc.Contains(retweeted.Id);
733                 }
734
735                 if (retweeted.Coordinates != null)
736                     post.PostGeo = new PostClass.StatusGeo(retweeted.Coordinates.Coordinates[0], retweeted.Coordinates.Coordinates[1]);
737
738                 //以下、ユーザー情報
739                 var user = retweeted.User;
740                 if (user != null)
741                 {
742                     post.UserId = user.Id;
743                     post.ScreenName = user.ScreenName;
744                     post.Nickname = user.Name.Trim();
745                     post.ImageUrl = user.ProfileImageUrlHttps;
746                     post.IsProtect = user.Protected;
747                 }
748                 else
749                 {
750                     post.UserId = 0L;
751                     post.ScreenName = "?????";
752                     post.Nickname = "Unknown User";
753                 }
754
755                 //Retweetした人
756                 if (status.User != null)
757                 {
758                     post.RetweetedBy = status.User.ScreenName;
759                     post.RetweetedByUserId = status.User.Id;
760                     post.IsMe = post.RetweetedBy.ToLowerInvariant().Equals(_uname);
761                 }
762                 else
763                 {
764                     post.RetweetedBy = "?????";
765                     post.RetweetedByUserId = 0L;
766                 }
767             }
768             else
769             {
770                 post.CreatedAt = MyCommon.DateTimeParse(status.CreatedAt);
771                 //本文
772                 post.TextFromApi = status.FullText;
773                 entities = status.MergedEntities;
774                 sourceHtml = status.Source;
775                 post.InReplyToStatusId = status.InReplyToStatusId;
776                 post.InReplyToUser = status.InReplyToScreenName;
777                 post.InReplyToUserId = status.InReplyToUserId;
778
779                 if (favTweet)
780                 {
781                     post.IsFav = true;
782                 }
783                 else
784                 {
785                     //幻覚fav対策
786                     var tc = TabInformations.GetInstance().GetTabByType(MyCommon.TabUsageType.Favorites);
787                     post.IsFav = tc.Contains(post.StatusId) && TabInformations.GetInstance()[post.StatusId].IsFav;
788                 }
789
790                 if (status.Coordinates != null)
791                     post.PostGeo = new PostClass.StatusGeo(status.Coordinates.Coordinates[0], status.Coordinates.Coordinates[1]);
792
793                 //以下、ユーザー情報
794                 var user = status.User;
795                 if (user != null)
796                 {
797                     post.UserId = user.Id;
798                     post.ScreenName = user.ScreenName;
799                     post.Nickname = user.Name.Trim();
800                     post.ImageUrl = user.ProfileImageUrlHttps;
801                     post.IsProtect = user.Protected;
802                     post.IsMe = post.ScreenName.ToLowerInvariant().Equals(_uname);
803                 }
804                 else
805                 {
806                     post.UserId = 0L;
807                     post.ScreenName = "?????";
808                     post.Nickname = "Unknown User";
809                 }
810             }
811             //HTMLに整形
812             string textFromApi = post.TextFromApi;
813             post.Text = CreateHtmlAnchor(textFromApi, post.ReplyToList, entities, post.Media);
814             post.TextFromApi = textFromApi;
815             post.TextFromApi = this.ReplaceTextFromApi(post.TextFromApi, entities);
816             post.TextFromApi = WebUtility.HtmlDecode(post.TextFromApi);
817             post.TextFromApi = post.TextFromApi.Replace("<3", "\u2661");
818             post.AccessibleText = this.CreateAccessibleText(textFromApi, entities, (status.RetweetedStatus ?? status).QuotedStatus);
819             post.AccessibleText = WebUtility.HtmlDecode(post.AccessibleText);
820             post.AccessibleText = post.AccessibleText.Replace("<3", "\u2661");
821
822             post.QuoteStatusIds = GetQuoteTweetStatusIds(entities)
823                 .Where(x => x != post.StatusId && x != post.RetweetedId)
824                 .Distinct().ToArray();
825
826             post.ExpandedUrls = entities.OfType<TwitterEntityUrl>()
827                 .Select(x => new PostClass.ExpandedUrlInfo(x.Url, x.ExpandedUrl))
828                 .ToArray();
829
830             // メモリ使用量削減 (同一のテキストであれば同一の string インスタンスを参照させる)
831             if (post.Text == post.TextFromApi)
832                 post.Text = post.TextFromApi;
833             if (post.AccessibleText == post.TextFromApi)
834                 post.AccessibleText = post.TextFromApi;
835
836             // 他の発言と重複しやすい (共通化できる) 文字列は string.Intern を通す
837             post.ScreenName = string.Intern(post.ScreenName);
838             post.Nickname = string.Intern(post.Nickname);
839             post.ImageUrl = string.Intern(post.ImageUrl);
840             post.RetweetedBy = post.RetweetedBy != null ? string.Intern(post.RetweetedBy) : null;
841
842             //Source整形
843             var (sourceText, sourceUri) = ParseSource(sourceHtml);
844             post.Source = string.Intern(sourceText);
845             post.SourceUri = sourceUri;
846
847             post.IsReply = post.RetweetedId == null && post.ReplyToList.Contains(_uname);
848             post.IsExcludeReply = false;
849
850             if (post.IsMe)
851             {
852                 post.IsOwl = false;
853             }
854             else
855             {
856                 if (followerId.Count > 0) post.IsOwl = !followerId.Contains(post.UserId);
857             }
858
859             post.IsDm = false;
860             return post;
861         }
862
863         /// <summary>
864         /// ツイートに含まれる引用ツイートのURLからステータスIDを抽出
865         /// </summary>
866         public static IEnumerable<long> GetQuoteTweetStatusIds(IEnumerable<TwitterEntity> entities)
867         {
868             var urls = entities.OfType<TwitterEntityUrl>().Select(x => x.ExpandedUrl);
869
870             return GetQuoteTweetStatusIds(urls);
871         }
872
873         public static IEnumerable<long> GetQuoteTweetStatusIds(IEnumerable<string> urls)
874         {
875             foreach (var url in urls)
876             {
877                 var match = Twitter.StatusUrlRegex.Match(url);
878                 if (match.Success)
879                 {
880                     if (long.TryParse(match.Groups["StatusId"].Value, out var statusId))
881                         yield return statusId;
882                 }
883             }
884         }
885
886         private long? CreatePostsFromJson(TwitterStatus[] items, MyCommon.WORKERTYPE gType, TabModel tab, bool read)
887         {
888             long? minimumId = null;
889
890             foreach (var status in items)
891             {
892                 if (minimumId == null || minimumId.Value > status.Id)
893                     minimumId = status.Id;
894
895                 //二重取得回避
896                 lock (LockObj)
897                 {
898                     if (tab == null)
899                     {
900                         if (TabInformations.GetInstance().ContainsKey(status.Id)) continue;
901                     }
902                     else
903                     {
904                         if (tab.Contains(status.Id)) continue;
905                     }
906                 }
907
908                 //RT禁止ユーザーによるもの
909                 if (gType != MyCommon.WORKERTYPE.UserTimeline &&
910                     status.RetweetedStatus != null && this.noRTId.Contains(status.User.Id)) continue;
911
912                 var post = CreatePostsFromStatusData(status);
913
914                 post.IsRead = read;
915                 if (post.IsMe && !read && _readOwnPost) post.IsRead = true;
916
917                 if (tab != null && tab.IsInnerStorageTabType)
918                     tab.AddPostQueue(post);
919                 else
920                     TabInformations.GetInstance().AddPost(post);
921             }
922
923             return minimumId;
924         }
925
926         private long? CreatePostsFromSearchJson(TwitterSearchResult items, PublicSearchTabModel tab, bool read, bool more)
927         {
928             long? minimumId = null;
929
930             foreach (var status in items.Statuses)
931             {
932                 if (minimumId == null || minimumId.Value > status.Id)
933                     minimumId = status.Id;
934
935                 if (!more && status.Id > tab.SinceId) tab.SinceId = status.Id;
936                 //二重取得回避
937                 lock (LockObj)
938                 {
939                     if (tab.Contains(status.Id)) continue;
940                 }
941
942                 var post = CreatePostsFromStatusData(status);
943
944                 post.IsRead = read;
945                 if ((post.IsMe && !read) && this._readOwnPost) post.IsRead = true;
946
947                 tab.AddPostQueue(post);
948             }
949
950             return minimumId;
951         }
952
953         private long? CreateFavoritePostsFromJson(TwitterStatus[] items, bool read)
954         {
955             var favTab = TabInformations.GetInstance().GetTabByType(MyCommon.TabUsageType.Favorites);
956             long? minimumId = null;
957
958             foreach (var status in items)
959             {
960                 if (minimumId == null || minimumId.Value > status.Id)
961                     minimumId = status.Id;
962
963                 //二重取得回避
964                 lock (LockObj)
965                 {
966                     if (favTab.Contains(status.Id)) continue;
967                 }
968
969                 var post = CreatePostsFromStatusData(status, true);
970
971                 post.IsRead = read;
972
973                 TabInformations.GetInstance().AddPost(post);
974             }
975
976             return minimumId;
977         }
978
979         public async Task GetListStatus(bool read, ListTimelineTabModel tab, bool more, bool startup)
980         {
981             var count = GetApiResultCount(MyCommon.WORKERTYPE.List, more, startup);
982
983             TwitterStatus[] statuses;
984             if (more)
985             {
986                 statuses = await this.Api.ListsStatuses(tab.ListInfo.Id, count, maxId: tab.OldestId, includeRTs: SettingManager.Common.IsListsIncludeRts)
987                     .ConfigureAwait(false);
988             }
989             else
990             {
991                 statuses = await this.Api.ListsStatuses(tab.ListInfo.Id, count, includeRTs: SettingManager.Common.IsListsIncludeRts)
992                     .ConfigureAwait(false);
993             }
994
995             var minimumId = CreatePostsFromJson(statuses, MyCommon.WORKERTYPE.List, tab, read);
996
997             if (minimumId != null)
998                 tab.OldestId = minimumId.Value;
999         }
1000
1001         /// <summary>
1002         /// startStatusId からリプライ先の発言を辿る。発言は posts 以外からは検索しない。
1003         /// </summary>
1004         /// <returns>posts の中から検索されたリプライチェインの末端</returns>
1005         internal static PostClass FindTopOfReplyChain(IDictionary<Int64, PostClass> posts, Int64 startStatusId)
1006         {
1007             if (!posts.ContainsKey(startStatusId))
1008                 throw new ArgumentException("startStatusId (" + startStatusId + ") が posts の中から見つかりませんでした。", nameof(startStatusId));
1009
1010             var nextPost = posts[startStatusId];
1011             while (nextPost.InReplyToStatusId != null)
1012             {
1013                 if (!posts.ContainsKey(nextPost.InReplyToStatusId.Value))
1014                     break;
1015                 nextPost = posts[nextPost.InReplyToStatusId.Value];
1016             }
1017
1018             return nextPost;
1019         }
1020
1021         public async Task GetRelatedResult(bool read, RelatedPostsTabModel tab)
1022         {
1023             var targetPost = tab.TargetPost;
1024             var relPosts = new Dictionary<Int64, PostClass>();
1025             if (targetPost.TextFromApi.Contains("@") && targetPost.InReplyToStatusId == null)
1026             {
1027                 //検索結果対応
1028                 var p = TabInformations.GetInstance()[targetPost.StatusId];
1029                 if (p != null && p.InReplyToStatusId != null)
1030                 {
1031                     targetPost = p;
1032                 }
1033                 else
1034                 {
1035                     p = await this.GetStatusApi(read, targetPost.StatusId)
1036                         .ConfigureAwait(false);
1037                     targetPost = p;
1038                 }
1039             }
1040             relPosts.Add(targetPost.StatusId, targetPost);
1041
1042             Exception lastException = null;
1043
1044             // in_reply_to_status_id を使用してリプライチェインを辿る
1045             var nextPost = FindTopOfReplyChain(relPosts, targetPost.StatusId);
1046             var loopCount = 1;
1047             while (nextPost.InReplyToStatusId != null && loopCount++ <= 20)
1048             {
1049                 var inReplyToId = nextPost.InReplyToStatusId.Value;
1050
1051                 var inReplyToPost = TabInformations.GetInstance()[inReplyToId];
1052                 if (inReplyToPost == null)
1053                 {
1054                     try
1055                     {
1056                         inReplyToPost = await this.GetStatusApi(read, inReplyToId)
1057                             .ConfigureAwait(false);
1058                     }
1059                     catch (WebApiException ex)
1060                     {
1061                         lastException = ex;
1062                         break;
1063                     }
1064                 }
1065
1066                 relPosts.Add(inReplyToPost.StatusId, inReplyToPost);
1067
1068                 nextPost = FindTopOfReplyChain(relPosts, nextPost.StatusId);
1069             }
1070
1071             //MRTとかに対応のためツイート内にあるツイートを指すURLを取り込む
1072             var text = targetPost.Text;
1073             var ma = Twitter.StatusUrlRegex.Matches(text).Cast<Match>()
1074                 .Concat(Twitter.ThirdPartyStatusUrlRegex.Matches(text).Cast<Match>());
1075             foreach (var _match in ma)
1076             {
1077                 if (Int64.TryParse(_match.Groups["StatusId"].Value, out var _statusId))
1078                 {
1079                     if (relPosts.ContainsKey(_statusId))
1080                         continue;
1081
1082                     var p = TabInformations.GetInstance()[_statusId];
1083                     if (p == null)
1084                     {
1085                         try
1086                         {
1087                             p = await this.GetStatusApi(read, _statusId)
1088                                 .ConfigureAwait(false);
1089                         }
1090                         catch (WebApiException ex)
1091                         {
1092                             lastException = ex;
1093                             break;
1094                         }
1095                     }
1096
1097                     if (p != null)
1098                         relPosts.Add(p.StatusId, p);
1099                 }
1100             }
1101
1102             relPosts.Values.ToList().ForEach(p =>
1103             {
1104                 if (p.IsMe && !read && this._readOwnPost)
1105                     p.IsRead = true;
1106                 else
1107                     p.IsRead = read;
1108
1109                 tab.AddPostQueue(p);
1110             });
1111
1112             if (lastException != null)
1113                 throw new WebApiException(lastException.Message, lastException);
1114         }
1115
1116         public async Task GetSearch(bool read, PublicSearchTabModel tab, bool more)
1117         {
1118             var count = GetApiResultCount(MyCommon.WORKERTYPE.PublicSearch, more, false);
1119
1120             long? maxId = null;
1121             long? sinceId = null;
1122             if (more)
1123             {
1124                 maxId = tab.OldestId - 1;
1125             }
1126             else
1127             {
1128                 sinceId = tab.SinceId;
1129             }
1130
1131             var searchResult = await this.Api.SearchTweets(tab.SearchWords, tab.SearchLang, count, maxId, sinceId)
1132                 .ConfigureAwait(false);
1133
1134             if (!TabInformations.GetInstance().ContainsTab(tab))
1135                 return;
1136
1137             var minimumId = this.CreatePostsFromSearchJson(searchResult, tab, read, more);
1138
1139             if (minimumId != null)
1140                 tab.OldestId = minimumId.Value;
1141         }
1142
1143         private void CreateDirectMessagesFromJson(TwitterDirectMessage[] item, MyCommon.WORKERTYPE gType, bool read)
1144         {
1145             foreach (var message in item)
1146             {
1147                 var post = new PostClass();
1148                 try
1149                 {
1150                     post.StatusId = message.Id;
1151                     if (gType != MyCommon.WORKERTYPE.UserStream)
1152                     {
1153                         if (gType == MyCommon.WORKERTYPE.DirectMessegeRcv)
1154                         {
1155                             if (minDirectmessage > post.StatusId) minDirectmessage = post.StatusId;
1156                         }
1157                         else
1158                         {
1159                             if (minDirectmessageSent > post.StatusId) minDirectmessageSent = post.StatusId;
1160                         }
1161                     }
1162
1163                     //二重取得回避
1164                     lock (LockObj)
1165                     {
1166                         if (TabInformations.GetInstance().GetTabByType(MyCommon.TabUsageType.DirectMessage).Contains(post.StatusId)) continue;
1167                     }
1168                     //sender_id
1169                     //recipient_id
1170                     post.CreatedAt = MyCommon.DateTimeParse(message.CreatedAt);
1171                     //本文
1172                     var textFromApi = message.Text;
1173                     //HTMLに整形
1174                     post.Text = CreateHtmlAnchor(textFromApi, post.ReplyToList, message.Entities, post.Media);
1175                     post.TextFromApi = this.ReplaceTextFromApi(textFromApi, message.Entities);
1176                     post.TextFromApi = WebUtility.HtmlDecode(post.TextFromApi);
1177                     post.TextFromApi = post.TextFromApi.Replace("<3", "\u2661");
1178                     post.AccessibleText = this.CreateAccessibleText(textFromApi, message.Entities, quoteStatus: null);
1179                     post.AccessibleText = WebUtility.HtmlDecode(post.AccessibleText);
1180                     post.AccessibleText = post.AccessibleText.Replace("<3", "\u2661");
1181                     post.IsFav = false;
1182
1183                     post.QuoteStatusIds = GetQuoteTweetStatusIds(message.Entities).Distinct().ToArray();
1184
1185                     post.ExpandedUrls = message.Entities.OfType<TwitterEntityUrl>()
1186                         .Select(x => new PostClass.ExpandedUrlInfo(x.Url, x.ExpandedUrl))
1187                         .ToArray();
1188
1189                     //以下、ユーザー情報
1190                     TwitterUser user;
1191                     if (gType == MyCommon.WORKERTYPE.UserStream)
1192                     {
1193                         if (this.Api.CurrentUserId == message.Recipient.Id)
1194                         {
1195                             user = message.Sender;
1196                             post.IsMe = false;
1197                             post.IsOwl = true;
1198                         }
1199                         else
1200                         {
1201                             user = message.Recipient;
1202                             post.IsMe = true;
1203                             post.IsOwl = false;
1204                         }
1205                     }
1206                     else
1207                     {
1208                         if (gType == MyCommon.WORKERTYPE.DirectMessegeRcv)
1209                         {
1210                             user = message.Sender;
1211                             post.IsMe = false;
1212                             post.IsOwl = true;
1213                         }
1214                         else
1215                         {
1216                             user = message.Recipient;
1217                             post.IsMe = true;
1218                             post.IsOwl = false;
1219                         }
1220                     }
1221
1222                     post.UserId = user.Id;
1223                     post.ScreenName = user.ScreenName;
1224                     post.Nickname = user.Name.Trim();
1225                     post.ImageUrl = user.ProfileImageUrlHttps;
1226                     post.IsProtect = user.Protected;
1227
1228                     // メモリ使用量削減 (同一のテキストであれば同一の string インスタンスを参照させる)
1229                     if (post.Text == post.TextFromApi)
1230                         post.Text = post.TextFromApi;
1231                     if (post.AccessibleText == post.TextFromApi)
1232                         post.AccessibleText = post.TextFromApi;
1233
1234                     // 他の発言と重複しやすい (共通化できる) 文字列は string.Intern を通す
1235                     post.ScreenName = string.Intern(post.ScreenName);
1236                     post.Nickname = string.Intern(post.Nickname);
1237                     post.ImageUrl = string.Intern(post.ImageUrl);
1238                 }
1239                 catch(Exception ex)
1240                 {
1241                     MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name);
1242                     MessageBox.Show("Parse Error(CreateDirectMessagesFromJson)");
1243                     continue;
1244                 }
1245
1246                 post.IsRead = read;
1247                 if (post.IsMe && !read && _readOwnPost) post.IsRead = true;
1248                 post.IsReply = false;
1249                 post.IsExcludeReply = false;
1250                 post.IsDm = true;
1251
1252                 var dmTab = TabInformations.GetInstance().GetTabByType(MyCommon.TabUsageType.DirectMessage);
1253                 dmTab.AddPostQueue(post);
1254             }
1255         }
1256
1257         public async Task GetDirectMessageApi(bool read, MyCommon.WORKERTYPE gType, bool more)
1258         {
1259             this.CheckAccountState();
1260             this.CheckAccessLevel(TwitterApiAccessLevel.ReadWriteAndDirectMessage);
1261
1262             var count = GetApiResultCount(gType, more, false);
1263
1264             TwitterDirectMessage[] messages;
1265             if (gType == MyCommon.WORKERTYPE.DirectMessegeRcv)
1266             {
1267                 if (more)
1268                 {
1269                     messages = await this.Api.DirectMessagesRecv(count, maxId: this.minDirectmessage)
1270                         .ConfigureAwait(false);
1271                 }
1272                 else
1273                 {
1274                     messages = await this.Api.DirectMessagesRecv(count)
1275                         .ConfigureAwait(false);
1276                 }
1277             }
1278             else
1279             {
1280                 if (more)
1281                 {
1282                     messages = await this.Api.DirectMessagesSent(count, maxId: this.minDirectmessageSent)
1283                         .ConfigureAwait(false);
1284                 }
1285                 else
1286                 {
1287                     messages = await this.Api.DirectMessagesSent(count)
1288                         .ConfigureAwait(false);
1289                 }
1290             }
1291
1292             CreateDirectMessagesFromJson(messages, gType, read);
1293         }
1294
1295         public async Task GetFavoritesApi(bool read, FavoritesTabModel tab, bool backward)
1296         {
1297             this.CheckAccountState();
1298
1299             var count = GetApiResultCount(MyCommon.WORKERTYPE.Favorites, backward, false);
1300
1301             TwitterStatus[] statuses;
1302             if (backward)
1303             {
1304                 statuses = await this.Api.FavoritesList(count, maxId: tab.OldestId)
1305                     .ConfigureAwait(false);
1306             }
1307             else
1308             {
1309                 statuses = await this.Api.FavoritesList(count)
1310                     .ConfigureAwait(false);
1311             }
1312
1313             var minimumId = this.CreateFavoritePostsFromJson(statuses, read);
1314
1315             if (minimumId != null)
1316                 tab.OldestId = minimumId.Value;
1317         }
1318
1319         private string ReplaceTextFromApi(string text, TwitterEntities entities)
1320         {
1321             if (entities != null)
1322             {
1323                 if (entities.Urls != null)
1324                 {
1325                     foreach (var m in entities.Urls)
1326                     {
1327                         if (!string.IsNullOrEmpty(m.DisplayUrl)) text = text.Replace(m.Url, m.DisplayUrl);
1328                     }
1329                 }
1330                 if (entities.Media != null)
1331                 {
1332                     foreach (var m in entities.Media)
1333                     {
1334                         if (!string.IsNullOrEmpty(m.DisplayUrl)) text = text.Replace(m.Url, m.DisplayUrl);
1335                     }
1336                 }
1337             }
1338             return text;
1339         }
1340
1341         private string CreateAccessibleText(string text, TwitterEntities entities, TwitterStatus quoteStatus)
1342         {
1343             if (entities == null)
1344                 return text;
1345
1346             if (entities.Urls != null)
1347             {
1348                 foreach (var entity in entities.Urls)
1349                 {
1350                     if (quoteStatus != null)
1351                     {
1352                         var matchStatusUrl = Twitter.StatusUrlRegex.Match(entity.ExpandedUrl);
1353                         if (matchStatusUrl.Success && matchStatusUrl.Groups["StatusId"].Value == quoteStatus.IdStr)
1354                         {
1355                             var quoteText = this.CreateAccessibleText(quoteStatus.FullText, quoteStatus.MergedEntities, quoteStatus: null);
1356                             text = text.Replace(entity.Url, string.Format(Properties.Resources.QuoteStatus_AccessibleText, quoteStatus.User.ScreenName, quoteText));
1357                         }
1358                     }
1359                     else if (!string.IsNullOrEmpty(entity.DisplayUrl))
1360                     {
1361                         text = text.Replace(entity.Url, entity.DisplayUrl);
1362                     }
1363                 }
1364             }
1365
1366             if (entities.Media != null)
1367             {
1368                 foreach (var entity in entities.Media)
1369                 {
1370                     if (!string.IsNullOrEmpty(entity.AltText))
1371                     {
1372                         text = text.Replace(entity.Url, string.Format(Properties.Resources.ImageAltText, entity.AltText));
1373                     }
1374                     else if (!string.IsNullOrEmpty(entity.DisplayUrl))
1375                     {
1376                         text = text.Replace(entity.Url, entity.DisplayUrl);
1377                     }
1378                 }
1379             }
1380
1381             return text;
1382         }
1383
1384         /// <summary>
1385         /// フォロワーIDを更新します
1386         /// </summary>
1387         /// <exception cref="WebApiException"/>
1388         public async Task RefreshFollowerIds()
1389         {
1390             if (MyCommon._endingFlag) return;
1391
1392             var cursor = -1L;
1393             var newFollowerIds = new HashSet<long>();
1394             do
1395             {
1396                 var ret = await this.Api.FollowersIds(cursor)
1397                     .ConfigureAwait(false);
1398
1399                 if (ret.Ids == null)
1400                     throw new WebApiException("ret.ids == null");
1401
1402                 newFollowerIds.UnionWith(ret.Ids);
1403                 cursor = ret.NextCursor;
1404             } while (cursor != 0);
1405
1406             this.followerId = newFollowerIds;
1407             TabInformations.GetInstance().RefreshOwl(this.followerId);
1408
1409             this._GetFollowerResult = true;
1410         }
1411
1412         public bool GetFollowersSuccess
1413         {
1414             get
1415             {
1416                 return _GetFollowerResult;
1417             }
1418         }
1419
1420         /// <summary>
1421         /// RT 非表示ユーザーを更新します
1422         /// </summary>
1423         /// <exception cref="WebApiException"/>
1424         public async Task RefreshNoRetweetIds()
1425         {
1426             if (MyCommon._endingFlag) return;
1427
1428             this.noRTId = await this.Api.NoRetweetIds()
1429                 .ConfigureAwait(false);
1430
1431             this._GetNoRetweetResult = true;
1432         }
1433
1434         public bool GetNoRetweetSuccess
1435         {
1436             get
1437             {
1438                 return _GetNoRetweetResult;
1439             }
1440         }
1441
1442         /// <summary>
1443         /// t.co の文字列長などの設定情報を更新します
1444         /// </summary>
1445         /// <exception cref="WebApiException"/>
1446         public async Task RefreshConfiguration()
1447         {
1448             this.Configuration = await this.Api.Configuration()
1449                 .ConfigureAwait(false);
1450         }
1451
1452         public async Task GetListsApi()
1453         {
1454             this.CheckAccountState();
1455
1456             var ownedLists = await TwitterLists.GetAllItemsAsync(x =>
1457                 this.Api.ListsOwnerships(this.Username, cursor: x, count: 1000))
1458                     .ConfigureAwait(false);
1459
1460             var subscribedLists = await TwitterLists.GetAllItemsAsync(x =>
1461                 this.Api.ListsSubscriptions(this.Username, cursor: x, count: 1000))
1462                     .ConfigureAwait(false);
1463
1464             TabInformations.GetInstance().SubscribableLists = Enumerable.Concat(ownedLists, subscribedLists)
1465                 .Select(x => new ListElement(x, this))
1466                 .ToList();
1467         }
1468
1469         public async Task DeleteList(long listId)
1470         {
1471             await this.Api.ListsDestroy(listId)
1472                 .IgnoreResponse()
1473                 .ConfigureAwait(false);
1474
1475             var tabinfo = TabInformations.GetInstance();
1476
1477             tabinfo.SubscribableLists = tabinfo.SubscribableLists
1478                 .Where(x => x.Id != listId)
1479                 .ToList();
1480         }
1481
1482         public async Task<ListElement> EditList(long listId, string new_name, bool isPrivate, string description)
1483         {
1484             var response = await this.Api.ListsUpdate(listId, new_name, description, isPrivate)
1485                 .ConfigureAwait(false);
1486
1487             var list = await response.LoadJsonAsync()
1488                 .ConfigureAwait(false);
1489
1490             return new ListElement(list, this);
1491         }
1492
1493         public async Task<long> GetListMembers(long listId, List<UserInfo> lists, long cursor)
1494         {
1495             this.CheckAccountState();
1496
1497             var users = await this.Api.ListsMembers(listId, cursor)
1498                 .ConfigureAwait(false);
1499
1500             Array.ForEach(users.Users, u => lists.Add(new UserInfo(u)));
1501
1502             return users.NextCursor;
1503         }
1504
1505         public async Task CreateListApi(string listName, bool isPrivate, string description)
1506         {
1507             this.CheckAccountState();
1508
1509             var response = await this.Api.ListsCreate(listName, description, isPrivate)
1510                 .ConfigureAwait(false);
1511
1512             var list = await response.LoadJsonAsync()
1513                 .ConfigureAwait(false);
1514
1515             TabInformations.GetInstance().SubscribableLists.Add(new ListElement(list, this));
1516         }
1517
1518         public async Task<bool> ContainsUserAtList(long listId, string user)
1519         {
1520             this.CheckAccountState();
1521
1522             try
1523             {
1524                 await this.Api.ListsMembersShow(listId, user)
1525                     .ConfigureAwait(false);
1526
1527                 return true;
1528             }
1529             catch (TwitterApiException ex)
1530                 when (ex.ErrorResponse.Errors.Any(x => x.Code == TwitterErrorCode.NotFound))
1531             {
1532                 return false;
1533             }
1534         }
1535
1536         public string CreateHtmlAnchor(string text, List<string> AtList, TwitterEntities entities, List<MediaInfo> media)
1537         {
1538             if (entities != null)
1539             {
1540                 if (entities.Hashtags != null)
1541                 {
1542                     lock (this.LockObj)
1543                     {
1544                         this._hashList.AddRange(entities.Hashtags.Select(x => "#" + x.Text));
1545                     }
1546                 }
1547                 if (entities.UserMentions != null)
1548                 {
1549                     foreach (var ent in entities.UserMentions)
1550                     {
1551                         var screenName = ent.ScreenName.ToLowerInvariant();
1552                         if (!AtList.Contains(screenName))
1553                             AtList.Add(screenName);
1554                     }
1555                 }
1556                 if (entities.Media != null)
1557                 {
1558                     if (media != null)
1559                     {
1560                         foreach (var ent in entities.Media)
1561                         {
1562                             if (!media.Any(x => x.Url == ent.MediaUrl))
1563                             {
1564                                 if (ent.VideoInfo != null &&
1565                                     ent.Type == "animated_gif" || ent.Type == "video")
1566                                 {
1567                                     //var videoUrl = ent.VideoInfo.Variants
1568                                     //    .Where(v => v.ContentType == "video/mp4")
1569                                     //    .OrderByDescending(v => v.Bitrate)
1570                                     //    .Select(v => v.Url).FirstOrDefault();
1571                                     media.Add(new MediaInfo(ent.MediaUrl, ent.AltText, ent.ExpandedUrl));
1572                                 }
1573                                 else
1574                                     media.Add(new MediaInfo(ent.MediaUrl, ent.AltText, videoUrl: null));
1575                             }
1576                         }
1577                     }
1578                 }
1579             }
1580
1581             // PostClass.ExpandedUrlInfo を使用して非同期に URL 展開を行うためここでは expanded_url を使用しない
1582             text = TweetFormatter.AutoLinkHtml(text, entities, keepTco: true);
1583
1584             text = Regex.Replace(text, "(^|[^a-zA-Z0-9_/&##@@>=.~])(sm|nm)([0-9]{1,10})", "$1<a href=\"http://www.nicovideo.jp/watch/$2$3\">$2$3</a>");
1585             text = PreProcessUrl(text); //IDN置換
1586
1587             return text;
1588         }
1589
1590         private static readonly Uri SourceUriBase = new Uri("https://twitter.com/");
1591
1592         /// <summary>
1593         /// Twitter APIから得たHTML形式のsource文字列を分析し、source名とURLに分離します
1594         /// </summary>
1595         internal static (string SourceText, Uri SourceUri) ParseSource(string sourceHtml)
1596         {
1597             if (string.IsNullOrEmpty(sourceHtml))
1598                 return ("", null);
1599
1600             string sourceText;
1601             Uri sourceUri;
1602
1603             // sourceHtmlの例: <a href="http://twitter.com" rel="nofollow">Twitter Web Client</a>
1604
1605             var match = Regex.Match(sourceHtml, "^<a href=\"(?<uri>.+?)\".*?>(?<text>.+)</a>$", RegexOptions.IgnoreCase);
1606             if (match.Success)
1607             {
1608                 sourceText = WebUtility.HtmlDecode(match.Groups["text"].Value);
1609                 try
1610                 {
1611                     var uriStr = WebUtility.HtmlDecode(match.Groups["uri"].Value);
1612                     sourceUri = new Uri(SourceUriBase, uriStr);
1613                 }
1614                 catch (UriFormatException)
1615                 {
1616                     sourceUri = null;
1617                 }
1618             }
1619             else
1620             {
1621                 sourceText = WebUtility.HtmlDecode(sourceHtml);
1622                 sourceUri = null;
1623             }
1624
1625             return (sourceText, sourceUri);
1626         }
1627
1628         public async Task<TwitterApiStatus> GetInfoApi()
1629         {
1630             if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return null;
1631
1632             if (MyCommon._endingFlag) return null;
1633
1634             var limits = await this.Api.ApplicationRateLimitStatus()
1635                 .ConfigureAwait(false);
1636
1637             MyCommon.TwitterApiInfo.UpdateFromJson(limits);
1638
1639             return MyCommon.TwitterApiInfo;
1640         }
1641
1642         /// <summary>
1643         /// ブロック中のユーザーを更新します
1644         /// </summary>
1645         /// <exception cref="WebApiException"/>
1646         public async Task RefreshBlockIds()
1647         {
1648             if (MyCommon._endingFlag) return;
1649
1650             var cursor = -1L;
1651             var newBlockIds = new HashSet<long>();
1652             do
1653             {
1654                 var ret = await this.Api.BlocksIds(cursor)
1655                     .ConfigureAwait(false);
1656
1657                 newBlockIds.UnionWith(ret.Ids);
1658                 cursor = ret.NextCursor;
1659             } while (cursor != 0);
1660
1661             newBlockIds.Remove(this.UserId); // 元のソースにあったので一応残しておく
1662
1663             TabInformations.GetInstance().BlockIds = newBlockIds;
1664         }
1665
1666         /// <summary>
1667         /// ミュート中のユーザーIDを更新します
1668         /// </summary>
1669         /// <exception cref="WebApiException"/>
1670         public async Task RefreshMuteUserIdsAsync()
1671         {
1672             if (MyCommon._endingFlag) return;
1673
1674             var ids = await TwitterIds.GetAllItemsAsync(x => this.Api.MutesUsersIds(x))
1675                 .ConfigureAwait(false);
1676
1677             TabInformations.GetInstance().MuteUserIds = new HashSet<long>(ids);
1678         }
1679
1680         public string[] GetHashList()
1681         {
1682             string[] hashArray;
1683             lock (LockObj)
1684             {
1685                 hashArray = _hashList.ToArray();
1686                 _hashList.Clear();
1687             }
1688             return hashArray;
1689         }
1690
1691         public string AccessToken
1692             => ((TwitterApiConnection)this.Api.Connection).AccessToken;
1693
1694         public string AccessTokenSecret
1695             => ((TwitterApiConnection)this.Api.Connection).AccessSecret;
1696
1697         private void CheckAccountState()
1698         {
1699             if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
1700                 throw new WebApiException("Auth error. Check your account");
1701         }
1702
1703         private void CheckAccessLevel(TwitterApiAccessLevel accessLevelFlags)
1704         {
1705             if (!this.AccessLevel.HasFlag(accessLevelFlags))
1706                 throw new WebApiException("Auth Err:try to re-authorization.");
1707         }
1708
1709         public int GetTextLengthRemain(string postText)
1710         {
1711             var matchDm = Twitter.DMSendTextRegex.Match(postText);
1712             if (matchDm.Success)
1713                 return this.GetTextLengthRemainDM(matchDm.Groups["body"].Value);
1714
1715             return this.GetTextLengthRemainWeighted(postText);
1716         }
1717
1718         private int GetTextLengthRemainDM(string postText)
1719         {
1720             var textLength = 0;
1721
1722             var pos = 0;
1723             while (pos < postText.Length)
1724             {
1725                 textLength++;
1726
1727                 if (char.IsSurrogatePair(postText, pos))
1728                     pos += 2; // サロゲートペアの場合は2文字分進める
1729                 else
1730                     pos++;
1731             }
1732
1733             var urls = TweetExtractor.ExtractUrls(postText);
1734             foreach (var url in urls)
1735             {
1736                 var shortUrlLength = url.StartsWith("https://", StringComparison.OrdinalIgnoreCase)
1737                     ? this.Configuration.ShortUrlLengthHttps
1738                     : this.Configuration.ShortUrlLength;
1739
1740                 textLength += shortUrlLength - url.Length;
1741             }
1742
1743             return this.Configuration.DmTextCharacterLimit - textLength;
1744         }
1745
1746         private int GetTextLengthRemainWeighted(string postText)
1747         {
1748             var config = this.TextConfiguration;
1749             var totalWeight = 0;
1750
1751             var urls = TweetExtractor.ExtractUrlEntities(postText).ToArray();
1752
1753             var pos = 0;
1754             while (pos < postText.Length)
1755             {
1756                 var urlEntity = urls.FirstOrDefault(x => x.Indices[0] == pos);
1757                 if (urlEntity != null)
1758                 {
1759                     totalWeight += config.TransformedURLLength * config.Scale;
1760
1761                     var urlLength = urlEntity.Indices[1] - urlEntity.Indices[0];
1762                     pos += urlLength;
1763
1764                     continue;
1765                 }
1766
1767                 var codepoint = char.ConvertToUtf32(postText, pos);
1768                 var weight = config.DefaultWeight;
1769
1770                 foreach (var weightRange in config.Ranges)
1771                 {
1772                     if (codepoint >= weightRange.Start && codepoint <= weightRange.End)
1773                     {
1774                         weight = weightRange.Weight;
1775                         break;
1776                     }
1777                 }
1778
1779                 totalWeight += weight;
1780
1781                 var isSurrogatePair = codepoint > 0xffff;
1782                 if (isSurrogatePair)
1783                     pos += 2; // サロゲートペアの場合は2文字分進める
1784                 else
1785                     pos++;
1786             }
1787
1788             var remainWeight = config.MaxWeightedTweetLength * config.Scale - totalWeight;
1789
1790             return remainWeight / config.Scale;
1791         }
1792
1793
1794 #region "UserStream"
1795         private string trackWord_ = "";
1796         public string TrackWord
1797         {
1798             get
1799             {
1800                 return trackWord_;
1801             }
1802             set
1803             {
1804                 trackWord_ = value;
1805             }
1806         }
1807         private bool allAtReply_ = false;
1808         public bool AllAtReply
1809         {
1810             get
1811             {
1812                 return allAtReply_;
1813             }
1814             set
1815             {
1816                 allAtReply_ = value;
1817             }
1818         }
1819
1820         public event EventHandler NewPostFromStream;
1821         public event EventHandler UserStreamStarted;
1822         public event EventHandler UserStreamStopped;
1823         public event EventHandler<PostDeletedEventArgs> PostDeleted;
1824         public event EventHandler<UserStreamEventReceivedEventArgs> UserStreamEventReceived;
1825         private DateTime _lastUserstreamDataReceived;
1826         private TwitterUserstream userStream;
1827
1828         public class FormattedEvent
1829         {
1830             public MyCommon.EVENTTYPE Eventtype { get; set; }
1831             public DateTime CreatedAt { get; set; }
1832             public string Event { get; set; }
1833             public string Username { get; set; }
1834             public string Target { get; set; }
1835             public Int64 Id { get; set; }
1836             public bool IsMe { get; set; }
1837         }
1838
1839         public List<FormattedEvent> storedEvent_ = new List<FormattedEvent>();
1840         public List<FormattedEvent> StoredEvent
1841         {
1842             get
1843             {
1844                 return storedEvent_;
1845             }
1846             set
1847             {
1848                 storedEvent_ = value;
1849             }
1850         }
1851
1852         private readonly IReadOnlyDictionary<string, MyCommon.EVENTTYPE> eventTable = new Dictionary<string, MyCommon.EVENTTYPE>
1853         {
1854             ["favorite"] = MyCommon.EVENTTYPE.Favorite,
1855             ["unfavorite"] = MyCommon.EVENTTYPE.Unfavorite,
1856             ["follow"] = MyCommon.EVENTTYPE.Follow,
1857             ["list_member_added"] = MyCommon.EVENTTYPE.ListMemberAdded,
1858             ["list_member_removed"] = MyCommon.EVENTTYPE.ListMemberRemoved,
1859             ["block"] = MyCommon.EVENTTYPE.Block,
1860             ["unblock"] = MyCommon.EVENTTYPE.Unblock,
1861             ["user_update"] = MyCommon.EVENTTYPE.UserUpdate,
1862             ["deleted"] = MyCommon.EVENTTYPE.Deleted,
1863             ["list_created"] = MyCommon.EVENTTYPE.ListCreated,
1864             ["list_destroyed"] = MyCommon.EVENTTYPE.ListDestroyed,
1865             ["list_updated"] = MyCommon.EVENTTYPE.ListUpdated,
1866             ["unfollow"] = MyCommon.EVENTTYPE.Unfollow,
1867             ["list_user_subscribed"] = MyCommon.EVENTTYPE.ListUserSubscribed,
1868             ["list_user_unsubscribed"] = MyCommon.EVENTTYPE.ListUserUnsubscribed,
1869             ["mute"] = MyCommon.EVENTTYPE.Mute,
1870             ["unmute"] = MyCommon.EVENTTYPE.Unmute,
1871             ["quoted_tweet"] = MyCommon.EVENTTYPE.QuotedTweet,
1872         };
1873
1874         public bool IsUserstreamDataReceived
1875         {
1876             get
1877             {
1878                 return DateTime.Now.Subtract(this._lastUserstreamDataReceived).TotalSeconds < 31;
1879             }
1880         }
1881
1882         private void userStream_StatusArrived(string line)
1883         {
1884             this._lastUserstreamDataReceived = DateTime.Now;
1885             if (string.IsNullOrEmpty(line)) return;
1886
1887             if (line.First() != '{' || line.Last() != '}')
1888             {
1889                 MyCommon.TraceOut("Invalid JSON (StatusArrived):" + Environment.NewLine + line);
1890                 return;
1891             }
1892
1893             var isDm = false;
1894
1895             try
1896             {
1897                 using (var jsonReader = JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(line), XmlDictionaryReaderQuotas.Max))
1898                 {
1899                     var xElm = XElement.Load(jsonReader);
1900                     if (xElm.Element("friends") != null)
1901                     {
1902                         Debug.WriteLine("friends");
1903                         return;
1904                     }
1905                     else if (xElm.Element("delete") != null)
1906                     {
1907                         Debug.WriteLine("delete");
1908                         Int64 id;
1909                         XElement idElm;
1910                         if ((idElm = xElm.Element("delete").Element("direct_message")?.Element("id")) != null)
1911                         {
1912                             id = 0;
1913                             long.TryParse(idElm.Value, out id);
1914
1915                             this.PostDeleted?.Invoke(this, new PostDeletedEventArgs(id));
1916                         }
1917                         else if ((idElm = xElm.Element("delete").Element("status")?.Element("id")) != null)
1918                         {
1919                             id = 0;
1920                             long.TryParse(idElm.Value, out id);
1921
1922                             this.PostDeleted?.Invoke(this, new PostDeletedEventArgs(id));
1923                         }
1924                         else
1925                         {
1926                             MyCommon.TraceOut("delete:" + line);
1927                             return;
1928                         }
1929                         for (int i = this.StoredEvent.Count - 1; i >= 0; i--)
1930                         {
1931                             var sEvt = this.StoredEvent[i];
1932                             if (sEvt.Id == id && (sEvt.Event == "favorite" || sEvt.Event == "unfavorite"))
1933                             {
1934                                 this.StoredEvent.RemoveAt(i);
1935                             }
1936                         }
1937                         return;
1938                     }
1939                     else if (xElm.Element("limit") != null)
1940                     {
1941                         Debug.WriteLine(line);
1942                         return;
1943                     }
1944                     else if (xElm.Element("event") != null)
1945                     {
1946                         Debug.WriteLine("event: " + xElm.Element("event").Value);
1947                         CreateEventFromJson(line);
1948                         return;
1949                     }
1950                     else if (xElm.Element("direct_message") != null)
1951                     {
1952                         Debug.WriteLine("direct_message");
1953                         isDm = true;
1954                     }
1955                     else if (xElm.Element("retweeted_status") != null)
1956                     {
1957                         var sourceUserId = xElm.XPathSelectElement("/user/id_str").Value;
1958                         var targetUserId = xElm.XPathSelectElement("/retweeted_status/user/id_str").Value;
1959
1960                         // 自分に関係しないリツイートの場合は無視する
1961                         var selfUserId = this.UserId.ToString();
1962                         if (sourceUserId == selfUserId || targetUserId == selfUserId)
1963                         {
1964                             // 公式 RT をイベントとしても扱う
1965                             var evt = CreateEventFromRetweet(xElm);
1966                             if (evt != null)
1967                             {
1968                                 this.StoredEvent.Insert(0, evt);
1969
1970                                 this.UserStreamEventReceived?.Invoke(this, new UserStreamEventReceivedEventArgs(evt));
1971                             }
1972                         }
1973
1974                         // 従来通り公式 RT の表示も行うため return しない
1975                     }
1976                     else if (xElm.Element("scrub_geo") != null)
1977                     {
1978                         try
1979                         {
1980                             TabInformations.GetInstance().ScrubGeoReserve(long.Parse(xElm.Element("scrub_geo").Element("user_id").Value),
1981                                                                         long.Parse(xElm.Element("scrub_geo").Element("up_to_status_id").Value));
1982                         }
1983                         catch(Exception)
1984                         {
1985                             MyCommon.TraceOut("scrub_geo:" + line);
1986                         }
1987                         return;
1988                     }
1989                 }
1990
1991                 if (isDm)
1992                 {
1993                     try
1994                     {
1995                         var message = TwitterStreamEventDirectMessage.ParseJson(line).DirectMessage;
1996                         this.CreateDirectMessagesFromJson(new[] { message }, MyCommon.WORKERTYPE.UserStream, false);
1997                     }
1998                     catch (SerializationException ex)
1999                     {
2000                         throw TwitterApiException.CreateFromException(ex, line);
2001                     }
2002                 }
2003                 else
2004                 {
2005                     try
2006                     {
2007                         var status = TwitterStatusCompat.ParseJson(line);
2008                         this.CreatePostsFromJson(new[] { status.Normalize() }, MyCommon.WORKERTYPE.UserStream, null, false);
2009                     }
2010                     catch (SerializationException ex)
2011                     {
2012                         throw TwitterApiException.CreateFromException(ex, line);
2013                     }
2014                 }
2015             }
2016             catch (WebApiException ex)
2017             {
2018                 MyCommon.TraceOut(ex);
2019                 return;
2020             }
2021             catch (XmlException)
2022             {
2023                 MyCommon.TraceOut("XmlException (StatusArrived): " + line);
2024             }
2025             catch(NullReferenceException)
2026             {
2027                 MyCommon.TraceOut("NullRef StatusArrived: " + line);
2028             }
2029
2030             this.NewPostFromStream?.Invoke(this, EventArgs.Empty);
2031         }
2032
2033         /// <summary>
2034         /// UserStreamsから受信した公式RTをイベントに変換します
2035         /// </summary>
2036         private FormattedEvent CreateEventFromRetweet(XElement xElm)
2037         {
2038             return new FormattedEvent
2039             {
2040                 Eventtype = MyCommon.EVENTTYPE.Retweet,
2041                 Event = "retweet",
2042                 CreatedAt = MyCommon.DateTimeParse(xElm.XPathSelectElement("/created_at").Value),
2043                 IsMe = xElm.XPathSelectElement("/user/id_str").Value == this.UserId.ToString(),
2044                 Username = xElm.XPathSelectElement("/user/screen_name").Value,
2045                 Target = string.Format("@{0}:{1}", new[]
2046                 {
2047                     xElm.XPathSelectElement("/retweeted_status/user/screen_name").Value,
2048                     WebUtility.HtmlDecode(xElm.XPathSelectElement("/retweeted_status/text").Value),
2049                 }),
2050                 Id = long.Parse(xElm.XPathSelectElement("/retweeted_status/id_str").Value),
2051             };
2052         }
2053
2054         private void CreateEventFromJson(string content)
2055         {
2056             TwitterStreamEvent eventData = null;
2057             try
2058             {
2059                 eventData = TwitterStreamEvent.ParseJson(content);
2060             }
2061             catch(SerializationException ex)
2062             {
2063                 MyCommon.TraceOut(ex, "Event Serialize Exception!" + Environment.NewLine + content);
2064             }
2065             catch(Exception ex)
2066             {
2067                 MyCommon.TraceOut(ex, "Event Exception!" + Environment.NewLine + content);
2068             }
2069
2070             var evt = new FormattedEvent();
2071             evt.CreatedAt = MyCommon.DateTimeParse(eventData.CreatedAt);
2072             evt.Event = eventData.Event;
2073             evt.Username = eventData.Source.ScreenName;
2074             evt.IsMe = evt.Username.ToLowerInvariant().Equals(this.Username.ToLowerInvariant());
2075
2076             eventTable.TryGetValue(eventData.Event, out var eventType);
2077             evt.Eventtype = eventType;
2078
2079             TwitterStreamEvent<TwitterStatusCompat> tweetEvent;
2080             TwitterStatus tweet;
2081
2082             switch (eventData.Event)
2083             {
2084                 case "access_revoked":
2085                 case "access_unrevoked":
2086                 case "user_delete":
2087                 case "user_suspend":
2088                     return;
2089                 case "follow":
2090                     if (eventData.Target.ScreenName.ToLowerInvariant().Equals(_uname))
2091                     {
2092                         if (!this.followerId.Contains(eventData.Source.Id)) this.followerId.Add(eventData.Source.Id);
2093                     }
2094                     else
2095                     {
2096                         return;    //Block後のUndoをすると、SourceとTargetが逆転したfollowイベントが帰ってくるため。
2097                     }
2098                     evt.Target = "";
2099                     break;
2100                 case "unfollow":
2101                     evt.Target = "@" + eventData.Target.ScreenName;
2102                     break;
2103                 case "favorited_retweet":
2104                 case "retweeted_retweet":
2105                     return;
2106                 case "favorite":
2107                 case "unfavorite":
2108                     tweetEvent = TwitterStreamEvent<TwitterStatusCompat>.ParseJson(content);
2109                     tweet = tweetEvent.TargetObject.Normalize();
2110                     evt.Target = "@" + tweet.User.ScreenName + ":" + WebUtility.HtmlDecode(tweet.FullText);
2111                     evt.Id = tweet.Id;
2112
2113                     if (SettingManager.Common.IsRemoveSameEvent)
2114                     {
2115                         if (this.StoredEvent.Any(ev => ev.Username == evt.Username && ev.Eventtype == evt.Eventtype && ev.Target == evt.Target))
2116                             return;
2117                     }
2118
2119                     var tabinfo = TabInformations.GetInstance();
2120
2121                     var statusId = tweet.Id;
2122                     if (!tabinfo.Posts.TryGetValue(statusId, out var post))
2123                         break;
2124
2125                     if (eventData.Event == "favorite")
2126                     {
2127                         var favTab = tabinfo.GetTabByType(MyCommon.TabUsageType.Favorites);
2128                         favTab.AddPostQueue(post);
2129
2130                         if (tweetEvent.Source.Id == this.UserId)
2131                         {
2132                             post.IsFav = true;
2133                         }
2134                         else if (tweetEvent.Target.Id == this.UserId)
2135                         {
2136                             post.FavoritedCount++;
2137
2138                             if (SettingManager.Common.FavEventUnread)
2139                                 tabinfo.SetReadAllTab(post.StatusId, read: false);
2140                         }
2141                     }
2142                     else // unfavorite
2143                     {
2144                         if (tweetEvent.Source.Id == this.UserId)
2145                         {
2146                             post.IsFav = false;
2147                         }
2148                         else if (tweetEvent.Target.Id == this.UserId)
2149                         {
2150                             post.FavoritedCount = Math.Max(0, post.FavoritedCount - 1);
2151                         }
2152                     }
2153                     break;
2154                 case "quoted_tweet":
2155                     if (evt.IsMe) return;
2156
2157                     tweetEvent = TwitterStreamEvent<TwitterStatusCompat>.ParseJson(content);
2158                     tweet = tweetEvent.TargetObject.Normalize();
2159                     evt.Target = "@" + tweet.User.ScreenName + ":" + WebUtility.HtmlDecode(tweet.FullText);
2160                     evt.Id = tweet.Id;
2161
2162                     if (SettingManager.Common.IsRemoveSameEvent)
2163                     {
2164                         if (this.StoredEvent.Any(ev => ev.Username == evt.Username && ev.Eventtype == evt.Eventtype && ev.Target == evt.Target))
2165                             return;
2166                     }
2167                     break;
2168                 case "list_member_added":
2169                 case "list_member_removed":
2170                 case "list_created":
2171                 case "list_destroyed":
2172                 case "list_updated":
2173                 case "list_user_subscribed":
2174                 case "list_user_unsubscribed":
2175                     var listEvent = TwitterStreamEvent<TwitterList>.ParseJson(content);
2176                     evt.Target = listEvent.TargetObject.FullName;
2177                     break;
2178                 case "block":
2179                     if (!TabInformations.GetInstance().BlockIds.Contains(eventData.Target.Id)) TabInformations.GetInstance().BlockIds.Add(eventData.Target.Id);
2180                     evt.Target = "";
2181                     break;
2182                 case "unblock":
2183                     if (TabInformations.GetInstance().BlockIds.Contains(eventData.Target.Id)) TabInformations.GetInstance().BlockIds.Remove(eventData.Target.Id);
2184                     evt.Target = "";
2185                     break;
2186                 case "user_update":
2187                     evt.Target = "";
2188                     break;
2189                 
2190                 // Mute / Unmute
2191                 case "mute":
2192                     evt.Target = "@" + eventData.Target.ScreenName;
2193                     if (!TabInformations.GetInstance().MuteUserIds.Contains(eventData.Target.Id))
2194                     {
2195                         TabInformations.GetInstance().MuteUserIds.Add(eventData.Target.Id);
2196                     }
2197                     break;
2198                 case "unmute":
2199                     evt.Target = "@" + eventData.Target.ScreenName;
2200                     if (TabInformations.GetInstance().MuteUserIds.Contains(eventData.Target.Id))
2201                     {
2202                         TabInformations.GetInstance().MuteUserIds.Remove(eventData.Target.Id);
2203                     }
2204                     break;
2205
2206                 default:
2207                     MyCommon.TraceOut("Unknown Event:" + evt.Event + Environment.NewLine + content);
2208                     break;
2209             }
2210             this.StoredEvent.Insert(0, evt);
2211
2212             this.UserStreamEventReceived?.Invoke(this, new UserStreamEventReceivedEventArgs(evt));
2213         }
2214
2215         private void userStream_Started()
2216         {
2217             this.UserStreamStarted?.Invoke(this, EventArgs.Empty);
2218         }
2219
2220         private void userStream_Stopped()
2221         {
2222             this.UserStreamStopped?.Invoke(this, EventArgs.Empty);
2223         }
2224
2225         public bool UserStreamActive
2226             => this.userStream != null && this.userStream.IsStreamActive;
2227
2228         public void StartUserStream()
2229         {
2230             var newStream = new TwitterUserstream(this.Api);
2231
2232             newStream.StatusArrived += userStream_StatusArrived;
2233             newStream.Started += userStream_Started;
2234             newStream.Stopped += userStream_Stopped;
2235
2236             newStream.Start(this.AllAtReply, this.TrackWord);
2237
2238             var oldStream = Interlocked.Exchange(ref this.userStream, newStream);
2239             oldStream?.Dispose();
2240         }
2241
2242         public void StopUserStream()
2243         {
2244             var oldStream = Interlocked.Exchange(ref this.userStream, null);
2245             oldStream?.Dispose();
2246         }
2247
2248         public void ReconnectUserStream()
2249         {
2250             if (this.userStream != null)
2251             {
2252                 this.StartUserStream();
2253             }
2254         }
2255
2256         private class TwitterUserstream : IDisposable
2257         {
2258             public bool AllAtReplies { get; private set; }
2259             public string TrackWords { get; private set; }
2260
2261             public bool IsStreamActive { get; private set; }
2262
2263             public event Action<string> StatusArrived;
2264             public event Action Stopped;
2265             public event Action Started;
2266
2267             private TwitterApi twitterApi;
2268
2269             private Task streamTask;
2270             private CancellationTokenSource streamCts;
2271
2272             public TwitterUserstream(TwitterApi twitterApi)
2273             {
2274                 this.twitterApi = twitterApi;
2275             }
2276
2277             public void Start(bool allAtReplies, string trackwords)
2278             {
2279                 this.AllAtReplies = allAtReplies;
2280                 this.TrackWords = trackwords;
2281
2282                 var cts = new CancellationTokenSource();
2283
2284                 this.streamCts = cts;
2285                 this.streamTask = Task.Run(async () =>
2286                 {
2287                     try
2288                     {
2289                         await this.UserStreamLoop(cts.Token)
2290                             .ConfigureAwait(false);
2291                     }
2292                     catch (OperationCanceledException) { }
2293                 });
2294             }
2295
2296             public void Stop()
2297             {
2298                 this.streamCts?.Cancel();
2299
2300                 // streamTask の完了を待たずに IsStreamActive を false にセットする
2301                 this.IsStreamActive = false;
2302                 this.Stopped?.Invoke();
2303             }
2304
2305             private async Task UserStreamLoop(CancellationToken cancellationToken)
2306             {
2307                 TimeSpan sleep = TimeSpan.Zero;
2308                 for (;;)
2309                 {
2310                     if (sleep != TimeSpan.Zero)
2311                     {
2312                         await Task.Delay(sleep, cancellationToken)
2313                             .ConfigureAwait(false);
2314                         sleep = TimeSpan.Zero;
2315                     }
2316
2317                     if (!MyCommon.IsNetworkAvailable())
2318                     {
2319                         sleep = TimeSpan.FromSeconds(30);
2320                         continue;
2321                     }
2322
2323                     this.IsStreamActive = true;
2324                     this.Started?.Invoke();
2325
2326                     try
2327                     {
2328                         var replies = this.AllAtReplies ? "all" : null;
2329
2330                         using (var stream = await this.twitterApi.UserStreams(replies, this.TrackWords)
2331                             .ConfigureAwait(false))
2332                         using (var reader = new StreamReader(stream))
2333                         {
2334                             while (!reader.EndOfStream)
2335                             {
2336                                 var line = await reader.ReadLineAsync()
2337                                     .ConfigureAwait(false);
2338
2339                                 cancellationToken.ThrowIfCancellationRequested();
2340
2341                                 this.StatusArrived?.Invoke(line);
2342                             }
2343                         }
2344
2345                         // キャンセルされていないのにストリームが終了した場合
2346                         sleep = TimeSpan.FromSeconds(30);
2347                     }
2348                     catch (TwitterApiException) { sleep = TimeSpan.FromSeconds(30); }
2349                     catch (IOException) { sleep = TimeSpan.FromSeconds(30); }
2350                     catch (OperationCanceledException)
2351                     {
2352                         if (cancellationToken.IsCancellationRequested)
2353                             throw;
2354
2355                         // cancellationToken によるキャンセルではない(=タイムアウトエラー)
2356                         sleep = TimeSpan.FromSeconds(30);
2357                     }
2358                     catch (Exception ex)
2359                     {
2360                         MyCommon.ExceptionOut(ex);
2361                         sleep = TimeSpan.FromSeconds(30);
2362                     }
2363                     finally
2364                     {
2365                         this.IsStreamActive = false;
2366                         this.Stopped?.Invoke();
2367                     }
2368                 }
2369             }
2370
2371             private bool disposed = false;
2372
2373             public void Dispose()
2374             {
2375                 if (this.disposed)
2376                     return;
2377
2378                 this.disposed = true;
2379
2380                 this.Stop();
2381
2382                 this.Started = null;
2383                 this.Stopped = null;
2384                 this.StatusArrived = null;
2385             }
2386         }
2387 #endregion
2388
2389 #region "IDisposable Support"
2390         private bool disposedValue; // 重複する呼び出しを検出するには
2391
2392         // IDisposable
2393         protected virtual void Dispose(bool disposing)
2394         {
2395             if (!this.disposedValue)
2396             {
2397                 if (disposing)
2398                 {
2399                     this.StopUserStream();
2400                 }
2401             }
2402             this.disposedValue = true;
2403         }
2404
2405         //protected Overrides void Finalize()
2406         //{
2407         //    // このコードを変更しないでください。クリーンアップ コードを上の Dispose(bool disposing) に記述します。
2408         //    Dispose(false)
2409         //    MyBase.Finalize()
2410         //}
2411
2412         // このコードは、破棄可能なパターンを正しく実装できるように Visual Basic によって追加されました。
2413         public void Dispose()
2414         {
2415             // このコードを変更しないでください。クリーンアップ コードを上の Dispose(bool disposing) に記述します。
2416             Dispose(true);
2417             GC.SuppressFinalize(this);
2418         }
2419 #endregion
2420     }
2421
2422     public class PostDeletedEventArgs : EventArgs
2423     {
2424         public long StatusId { get; }
2425
2426         public PostDeletedEventArgs(long statusId)
2427         {
2428             this.StatusId = statusId;
2429         }
2430     }
2431
2432     public class UserStreamEventReceivedEventArgs : EventArgs
2433     {
2434         public Twitter.FormattedEvent EventData { get; }
2435
2436         public UserStreamEventReceivedEventArgs(Twitter.FormattedEvent eventData)
2437         {
2438             this.EventData = eventData;
2439         }
2440     }
2441 }