OSDN Git Service

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