OSDN Git Service

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