OSDN Git Service

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