OSDN Git Service

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