OSDN Git Service

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