OSDN Git Service

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