OSDN Git Service

HttpTwitter.FollowerIdsメソッドをTwitterApiクラスに置き換え
[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.Runtime.CompilerServices;
33 using System.Runtime.Serialization;
34 using System.Runtime.Serialization.Json;
35 using System.Text;
36 using System.Text.RegularExpressions;
37 using System.Threading;
38 using System.Threading.Tasks;
39 using System.Web;
40 using System.Xml;
41 using System.Xml.Linq;
42 using System.Xml.XPath;
43 using System;
44 using System.Reflection;
45 using System.Collections.Generic;
46 using System.Drawing;
47 using System.Windows.Forms;
48 using OpenTween.Api;
49 using OpenTween.Api.DataModel;
50 using OpenTween.Connection;
51
52 namespace OpenTween
53 {
54     public class Twitter : IDisposable
55     {
56         #region Regexp from twitter-text-js
57
58         // The code in this region code block incorporates works covered by
59         // the following copyright and permission notices:
60         //
61         //   Copyright 2011 Twitter, Inc.
62         //
63         //   Licensed under the Apache License, Version 2.0 (the "License"); you
64         //   may not use this work except in compliance with the License. You
65         //   may obtain a copy of the License in the LICENSE file, or at:
66         //
67         //   http://www.apache.org/licenses/LICENSE-2.0
68         //
69         //   Unless required by applicable law or agreed to in writing, software
70         //   distributed under the License is distributed on an "AS IS" BASIS,
71         //   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
72         //   implied. See the License for the specific language governing
73         //   permissions and limitations under the License.
74
75         //Hashtag用正規表現
76         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";
77         private const string NON_LATIN_HASHTAG_CHARS = @"\u0400-\u04ff\u0500-\u0527\u1100-\u11ff\u3130-\u3185\uA960-\uA97F\uAC00-\uD7AF\uD7B0-\uD7FF";
78         //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";
79         private const string CJ_HASHTAG_CHARACTERS = @"\u30A1-\u30FA\u30FC\u3005\uFF66-\uFF9F\uFF10-\uFF19\uFF21-\uFF3A\uFF41-\uFF5A\u3041-\u309A\u3400-\u4DBF\p{IsCJKUnifiedIdeographs}";
80         private const string HASHTAG_BOUNDARY = @"^|$|\s|「|」|。|\.|!";
81         private const string HASHTAG_ALPHA = "[a-z_" + LATIN_ACCENTS + NON_LATIN_HASHTAG_CHARS + CJ_HASHTAG_CHARACTERS + "]";
82         private const string HASHTAG_ALPHANUMERIC = "[a-z0-9_" + LATIN_ACCENTS + NON_LATIN_HASHTAG_CHARS + CJ_HASHTAG_CHARACTERS + "]";
83         private const string HASHTAG_TERMINATOR = "[^a-z0-9_" + LATIN_ACCENTS + NON_LATIN_HASHTAG_CHARS + CJ_HASHTAG_CHARACTERS + "]";
84         public const string HASHTAG = "(" + HASHTAG_BOUNDARY + ")(#|#)(" + HASHTAG_ALPHANUMERIC + "*" + HASHTAG_ALPHA + HASHTAG_ALPHANUMERIC + "*)(?=" + HASHTAG_TERMINATOR + "|" + HASHTAG_BOUNDARY + ")";
85         //URL正規表現
86         private const string url_valid_preceding_chars = @"(?:[^A-Za-z0-9@@$##\ufffe\ufeff\uffff\u202a-\u202e]|^)";
87         public const string url_invalid_without_protocol_preceding_chars = @"[-_./]$";
88         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";
89         private const string url_valid_domain_chars = @"[^" + url_invalid_domain_chars + "]";
90         private const string url_valid_subdomain = @"(?:(?:" + url_valid_domain_chars + @"(?:[_-]|" + url_valid_domain_chars + @")*)?" + url_valid_domain_chars + @"\.)";
91         private const string url_valid_domain_name = @"(?:(?:" + url_valid_domain_chars + @"(?:-|" + url_valid_domain_chars + @")*)?" + url_valid_domain_chars + @"\.)";
92         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]|$))";
93         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]|$))";
94         private const string url_valid_punycode = @"(?:xn--[0-9a-z]+)";
95         private const string url_valid_domain = @"(?<domain>" + url_valid_subdomain + "*" + url_valid_domain_name + "(?:" + url_valid_GTLD + "|" + url_valid_CCTLD + ")|" + url_valid_punycode + ")";
96         public const string url_valid_ascii_domain = @"(?:(?:[a-z0-9" + LATIN_ACCENTS + @"]+)\.)+(?:" + url_valid_GTLD + "|" + url_valid_CCTLD + "|" + url_valid_punycode + ")";
97         public const string url_invalid_short_domain = "^" + url_valid_domain_name + url_valid_CCTLD + "$";
98         private const string url_valid_port_number = @"[0-9]+";
99
100         private const string url_valid_general_path_chars = @"[a-z0-9!*';:=+,.$/%#\[\]\-_~|&" + LATIN_ACCENTS + "]";
101         private const string url_balance_parens = @"(?:\(" + url_valid_general_path_chars + @"+\))";
102         private const string url_valid_path_ending_chars = @"(?:[+\-a-z0-9=_#/" + LATIN_ACCENTS + "]|" + url_balance_parens + ")";
103         private const string pth = "(?:" +
104             "(?:" +
105                 url_valid_general_path_chars + "*" +
106                 "(?:" + url_balance_parens + url_valid_general_path_chars + "*)*" +
107                 url_valid_path_ending_chars +
108                 ")|(?:@" + url_valid_general_path_chars + "+/)" +
109             ")";
110         private const string qry = @"(?<query>\?[a-z0-9!?*'();:&=+$/%#\[\]\-_.,~|]*[a-z0-9_&=#/])?";
111         public const string rgUrl = @"(?<before>" + url_valid_preceding_chars + ")" +
112                                     "(?<url>(?<protocol>https?://)?" +
113                                     "(?<domain>" + url_valid_domain + ")" +
114                                     "(?::" + url_valid_port_number + ")?" +
115                                     "(?<path>/" + pth + "*)?" +
116                                     qry +
117                                     ")";
118
119         #endregion
120
121         /// <summary>
122         /// Twitter API のステータスページのURL
123         /// </summary>
124         public const string ServiceAvailabilityStatusUrl = "https://status.io.watchmouse.com/7617";
125
126         /// <summary>
127         /// ツイートへのパーマリンクURLを判定する正規表現
128         /// </summary>
129         public static readonly Regex StatusUrlRegex = new Regex(@"https?://([^.]+\.)?twitter\.com/(#!/)?(?<ScreenName>[a-zA-Z0-9_]+)/status(es)?/(?<StatusId>[0-9]+)(/photo)?", RegexOptions.IgnoreCase);
130
131         /// <summary>
132         /// FavstarやaclogなどTwitter関連サービスのパーマリンクURLからステータスIDを抽出する正規表現
133         /// </summary>
134         public static readonly Regex ThirdPartyStatusUrlRegex = new Regex(@"https?://(?:[^.]+\.)?(?:
135   favstar\.fm/users/[a-zA-Z0-9_]+/status/       # Favstar
136 | favstar\.fm/t/                                # Favstar (short)
137 | aclog\.koba789\.com/i/                        # aclog
138 | frtrt\.net/solo_status\.php\?status=          # RtRT
139 )(?<StatusId>[0-9]+)", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
140
141         /// <summary>
142         /// DM送信かどうかを判定する正規表現
143         /// </summary>
144         public static readonly Regex DMSendTextRegex = new Regex(@"^DM? +(?<id>[a-zA-Z0-9_]+) +(?<body>.*)", RegexOptions.IgnoreCase | RegexOptions.Singleline);
145
146         public TwitterConfiguration Configuration { get; private set; }
147
148         delegate void GetIconImageDelegate(PostClass post);
149         private readonly object LockObj = new object();
150         private ISet<long> followerId = new HashSet<long>();
151         private bool _GetFollowerResult = false;
152         private long[] noRTId = new long[0];
153         private bool _GetNoRetweetResult = false;
154
155         //プロパティからアクセスされる共通情報
156         private string _uname;
157
158         private bool _readOwnPost;
159         private List<string> _hashList = new List<string>();
160
161         //max_idで古い発言を取得するために保持(lists分は個別タブで管理)
162         private long minHomeTimeline = long.MaxValue;
163         private long minMentions = long.MaxValue;
164         private long minDirectmessage = long.MaxValue;
165         private long minDirectmessageSent = long.MaxValue;
166
167         //private FavoriteQueue favQueue;
168
169         private HttpTwitter twCon = new HttpTwitter();
170         private TwitterApi Api { get; }
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 Authenticate(string username, string password)
198         {
199             this.ResetApiStatus();
200
201             HttpStatusCode res;
202             var content = "";
203             try
204             {
205                 res = twCon.AuthUserAndPass(username, password, ref content);
206             }
207             catch(Exception ex)
208             {
209                 throw new WebApiException("Err:" + ex.Message, ex);
210             }
211
212             this.CheckStatusCode(res, content);
213
214             _uname = username.ToLowerInvariant();
215             if (SettingCommon.Instance.UserstreamStartup) this.ReconnectUserStream();
216         }
217
218         public string StartAuthentication()
219         {
220             //OAuth PIN Flow
221             this.ResetApiStatus();
222             try
223             {
224                 string pinPageUrl = null;
225                 var res = twCon.AuthGetRequestToken(ref pinPageUrl);
226                 if (!res)
227                     throw new WebApiException("Err:Failed to access auth server.");
228
229                 return pinPageUrl;
230             }
231             catch (Exception ex)
232             {
233                 throw new WebApiException("Err:Failed to access auth server.", ex);
234             }
235         }
236
237         public void Authenticate(string pinCode)
238         {
239             this.ResetApiStatus();
240
241             HttpStatusCode res;
242             try
243             {
244                 res = twCon.AuthGetAccessToken(pinCode);
245             }
246             catch (Exception ex)
247             {
248                 throw new WebApiException("Err:Failed to access auth acc server.", ex);
249             }
250
251             this.CheckStatusCode(res, null);
252
253             _uname = Username.ToLowerInvariant();
254             if (SettingCommon.Instance.UserstreamStartup) this.ReconnectUserStream();
255         }
256
257         public void ClearAuthInfo()
258         {
259             Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
260             this.ResetApiStatus();
261             twCon.ClearAuthInfo();
262         }
263
264         public void VerifyCredentials()
265         {
266             HttpStatusCode res;
267             var content = "";
268             try
269             {
270                 res = twCon.VerifyCredentials(ref content);
271             }
272             catch (Exception ex)
273             {
274                 throw new WebApiException("Err:" + ex.Message, ex);
275             }
276
277             this.CheckStatusCode(res, content);
278
279             try
280             {
281                 var user = TwitterUser.ParseJson(content);
282
283                 this.twCon.AuthenticatedUserId = user.Id;
284                 this.UpdateUserStats(user);
285             }
286             catch (SerializationException ex)
287             {
288                 MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
289                 throw new WebApiException("Err:Json Parse Error(DataContractJsonSerializer)", content, ex);
290             }
291         }
292
293         public void Initialize(string token, string tokenSecret, string username, long userId)
294         {
295             //OAuth認証
296             if (string.IsNullOrEmpty(token) || string.IsNullOrEmpty(tokenSecret) || string.IsNullOrEmpty(username))
297             {
298                 Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
299             }
300             this.ResetApiStatus();
301             twCon.Initialize(token, tokenSecret, username, userId);
302             _uname = username.ToLowerInvariant();
303             if (SettingCommon.Instance.UserstreamStartup) this.ReconnectUserStream();
304         }
305
306         public string PreProcessUrl(string orgData)
307         {
308             int posl1;
309             var posl2 = 0;
310             //var IDNConveter = new IdnMapping();
311             var href = "<a href=\"";
312
313             while (true)
314             {
315                 if (orgData.IndexOf(href, posl2, StringComparison.Ordinal) > -1)
316                 {
317                     var urlStr = "";
318                     // IDN展開
319                     posl1 = orgData.IndexOf(href, posl2, StringComparison.Ordinal);
320                     posl1 += href.Length;
321                     posl2 = orgData.IndexOf("\"", posl1, StringComparison.Ordinal);
322                     urlStr = orgData.Substring(posl1, posl2 - posl1);
323
324                     if (!urlStr.StartsWith("http://", StringComparison.Ordinal)
325                         && !urlStr.StartsWith("https://", StringComparison.Ordinal)
326                         && !urlStr.StartsWith("ftp://", StringComparison.Ordinal))
327                     {
328                         continue;
329                     }
330
331                     var replacedUrl = MyCommon.IDNEncode(urlStr);
332                     if (replacedUrl == null) continue;
333                     if (replacedUrl == urlStr) continue;
334
335                     orgData = orgData.Replace("<a href=\"" + urlStr, "<a href=\"" + replacedUrl);
336                     posl2 = 0;
337                 }
338                 else
339                 {
340                     break;
341                 }
342             }
343             return orgData;
344         }
345
346         private string GetPlainText(string orgData)
347         {
348             return WebUtility.HtmlDecode(Regex.Replace(orgData, "(?<tagStart><a [^>]+>)(?<text>[^<]+)(?<tagEnd></a>)", "${text}"));
349         }
350
351         // htmlの簡易サニタイズ(詳細表示に不要なタグの除去)
352
353         private string SanitizeHtml(string orgdata)
354         {
355             var retdata = orgdata;
356
357             retdata = Regex.Replace(retdata, "<(script|object|applet|image|frameset|fieldset|legend|style).*" +
358                 "</(script|object|applet|image|frameset|fieldset|legend|style)>", "", RegexOptions.IgnoreCase);
359
360             retdata = Regex.Replace(retdata, "<(frame|link|iframe|img)>", "", RegexOptions.IgnoreCase);
361
362             return retdata;
363         }
364
365         private string AdjustHtml(string orgData)
366         {
367             var retStr = orgData;
368             //var m = Regex.Match(retStr, "<a [^>]+>[#|#](?<1>[a-zA-Z0-9_]+)</a>");
369             //while (m.Success)
370             //{
371             //    lock (LockObj)
372             //    {
373             //        _hashList.Add("#" + m.Groups(1).Value);
374             //    }
375             //    m = m.NextMatch;
376             //}
377             retStr = Regex.Replace(retStr, "<a [^>]*href=\"/", "<a href=\"https://twitter.com/");
378             retStr = retStr.Replace("<a href=", "<a target=\"_self\" href=");
379             retStr = Regex.Replace(retStr, @"(\r\n?|\n)", "<br>"); // CRLF, CR, LF は全て <br> に置換する
380
381             //半角スペースを置換(Thanks @anis774)
382             var ret = false;
383             do
384             {
385                 ret = EscapeSpace(ref retStr);
386             } while (!ret);
387
388             return SanitizeHtml(retStr);
389         }
390
391         private bool EscapeSpace(ref string html)
392         {
393             //半角スペースを置換(Thanks @anis774)
394             var isTag = false;
395             for (int i = 0; i < html.Length; i++)
396             {
397                 if (html[i] == '<')
398                 {
399                     isTag = true;
400                 }
401                 if (html[i] == '>')
402                 {
403                     isTag = false;
404                 }
405
406                 if ((!isTag) && (html[i] == ' '))
407                 {
408                     html = html.Remove(i, 1);
409                     html = html.Insert(i, "&nbsp;");
410                     return false;
411                 }
412             }
413             return true;
414         }
415
416         private struct PostInfo
417         {
418             public string CreatedAt;
419             public string Id;
420             public string Text;
421             public string UserId;
422             public PostInfo(string Created, string IdStr, string txt, string uid)
423             {
424                 CreatedAt = Created;
425                 Id = IdStr;
426                 Text = txt;
427                 UserId = uid;
428             }
429             public bool Equals(PostInfo dst)
430             {
431                 if (this.CreatedAt == dst.CreatedAt && this.Id == dst.Id && this.Text == dst.Text && this.UserId == dst.UserId)
432                 {
433                     return true;
434                 }
435                 else
436                 {
437                     return false;
438                 }
439             }
440         }
441
442         static private PostInfo _prev = new PostInfo("", "", "", "");
443         private bool IsPostRestricted(TwitterStatus status)
444         {
445             var _current = new PostInfo("", "", "", "");
446
447             _current.CreatedAt = status.CreatedAt;
448             _current.Id = status.IdStr;
449             if (status.Text == null)
450             {
451                 _current.Text = "";
452             }
453             else
454             {
455                 _current.Text = status.Text;
456             }
457             _current.UserId = status.User.IdStr;
458
459             if (_current.Equals(_prev))
460             {
461                 return true;
462             }
463             _prev.CreatedAt = _current.CreatedAt;
464             _prev.Id = _current.Id;
465             _prev.Text = _current.Text;
466             _prev.UserId = _current.UserId;
467
468             return false;
469         }
470
471         public async Task PostStatus(string postStr, long? reply_to, IReadOnlyList<long> mediaIds = null)
472         {
473             this.CheckAccountState();
474
475             if (mediaIds == null &&
476                 Twitter.DMSendTextRegex.IsMatch(postStr))
477             {
478                 await this.SendDirectMessage(postStr)
479                     .ConfigureAwait(false);
480                 return;
481             }
482
483             var response = await this.Api.StatusesUpdate(postStr, reply_to, mediaIds)
484                 .ConfigureAwait(false);
485
486             var status = await response.LoadJsonAsync()
487                 .ConfigureAwait(false);
488
489             this.UpdateUserStats(status.User);
490
491             if (IsPostRestricted(status))
492             {
493                 throw new WebApiException("OK:Delaying?");
494             }
495         }
496
497         public async Task PostStatusWithMultipleMedia(string postStr, long? reply_to, IMediaItem[] mediaItems)
498         {
499             this.CheckAccountState();
500
501             if (Twitter.DMSendTextRegex.IsMatch(postStr))
502             {
503                 await this.SendDirectMessage(postStr)
504                     .ConfigureAwait(false);
505                 return;
506             }
507
508             if (mediaItems.Length == 0)
509                 throw new WebApiException("Err:Invalid Files!");
510
511             var uploadTasks = from m in mediaItems
512                               select this.UploadMedia(m);
513
514             var mediaIds = await Task.WhenAll(uploadTasks)
515                 .ConfigureAwait(false);
516
517             await this.PostStatus(postStr, reply_to, mediaIds)
518                 .ConfigureAwait(false);
519         }
520
521         public async Task<long> UploadMedia(IMediaItem item)
522         {
523             this.CheckAccountState();
524
525             var response = await this.Api.MediaUpload(item)
526                 .ConfigureAwait(false);
527
528             var media = await response.LoadJsonAsync()
529                 .ConfigureAwait(false);
530
531             return media.MediaId;
532         }
533
534         public async Task SendDirectMessage(string postStr)
535         {
536             this.CheckAccountState();
537             this.CheckAccessLevel(TwitterApiAccessLevel.ReadWriteAndDirectMessage);
538
539             var mc = Twitter.DMSendTextRegex.Match(postStr);
540
541             var response = await this.Api.DirectMessagesNew(mc.Groups["body"].Value, mc.Groups["id"].Value)
542                 .ConfigureAwait(false);
543
544             var dm = await response.LoadJsonAsync()
545                 .ConfigureAwait(false);
546
547             this.UpdateUserStats(dm.Sender);
548         }
549
550         public void PostRetweet(long id, bool read)
551         {
552             this.CheckAccountState();
553
554             //データ部分の生成
555             var target = id;
556             var post = TabInformations.GetInstance()[id];
557             if (post == null)
558             {
559                 throw new WebApiException("Err:Target isn't found.");
560             }
561             if (TabInformations.GetInstance()[id].RetweetedId != null)
562             {
563                 target = TabInformations.GetInstance()[id].RetweetedId.Value; //再RTの場合は元発言をRT
564             }
565
566             HttpStatusCode res;
567             var content = "";
568             try
569             {
570                 res = twCon.RetweetStatus(target, ref content);
571             }
572             catch(Exception ex)
573             {
574                 throw new WebApiException("Err:" + ex.Message, ex);
575             }
576
577             this.CheckStatusCode(res, content);
578
579             TwitterStatus status;
580             try
581             {
582                 status = TwitterStatus.ParseJson(content);
583             }
584             catch(SerializationException ex)
585             {
586                 MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
587                 throw new WebApiException("Err:Json Parse Error(DataContractJsonSerializer)", content, ex);
588             }
589             catch(Exception ex)
590             {
591                 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
592                 throw new WebApiException("Err:Invalid Json!", content, ex);
593             }
594
595             //ReTweetしたものをTLに追加
596             post = CreatePostsFromStatusData(status);
597             if (post == null)
598                 throw new WebApiException("Invalid Json!", content);
599
600             //二重取得回避
601             lock (LockObj)
602             {
603                 if (TabInformations.GetInstance().ContainsKey(post.StatusId))
604                     return;
605             }
606             //Retweet判定
607             if (post.RetweetedId == null)
608                 throw new WebApiException("Invalid Json!", content);
609             //ユーザー情報
610             post.IsMe = true;
611
612             post.IsRead = read;
613             post.IsOwl = false;
614             if (_readOwnPost) post.IsRead = true;
615             post.IsDm = false;
616
617             TabInformations.GetInstance().AddPost(post);
618         }
619
620         public string Username
621         {
622             get
623             {
624                 return twCon.AuthenticatedUsername;
625             }
626         }
627
628         public long UserId
629         {
630             get
631             {
632                 return twCon.AuthenticatedUserId;
633             }
634         }
635
636         public string Password
637         {
638             get
639             {
640                 return twCon.Password;
641             }
642         }
643
644         private static MyCommon.ACCOUNT_STATE _accountState = MyCommon.ACCOUNT_STATE.Valid;
645         public static MyCommon.ACCOUNT_STATE AccountState
646         {
647             get
648             {
649                 return _accountState;
650             }
651             set
652             {
653                 _accountState = value;
654             }
655         }
656
657         public bool RestrictFavCheck { get; set; }
658
659 #region "バージョンアップ"
660         public void GetTweenBinary(string strVer)
661         {
662             try
663             {
664                 //本体
665                 if (!(new HttpVarious()).GetDataToFile("http://tween.sourceforge.jp/Tween" + strVer + ".gz?" + DateTime.Now.ToString("yyMMddHHmmss") + Environment.TickCount.ToString(),
666                                                     Path.Combine(MyCommon.settingPath, "TweenNew.exe")))
667                 {
668                     throw new WebApiException("Err:Download failed");
669                 }
670                 //英語リソース
671                 if (!Directory.Exists(Path.Combine(MyCommon.settingPath, "en")))
672                 {
673                     Directory.CreateDirectory(Path.Combine(MyCommon.settingPath, "en"));
674                 }
675                 if (!(new HttpVarious()).GetDataToFile("http://tween.sourceforge.jp/TweenResEn" + strVer + ".gz?" + DateTime.Now.ToString("yyMMddHHmmss") + Environment.TickCount.ToString(),
676                                                     Path.Combine(Path.Combine(MyCommon.settingPath, "en"), "Tween.resourcesNew.dll")))
677                 {
678                     throw new WebApiException("Err:Download failed");
679                 }
680                 //その他言語圏のリソース。取得失敗しても継続
681                 //UIの言語圏のリソース
682                 var curCul = "";
683                 if (!Thread.CurrentThread.CurrentUICulture.IsNeutralCulture)
684                 {
685                     var idx = Thread.CurrentThread.CurrentUICulture.Name.LastIndexOf('-');
686                     if (idx > -1)
687                     {
688                         curCul = Thread.CurrentThread.CurrentUICulture.Name.Substring(0, idx);
689                     }
690                     else
691                     {
692                         curCul = Thread.CurrentThread.CurrentUICulture.Name;
693                     }
694                 }
695                 else
696                 {
697                     curCul = Thread.CurrentThread.CurrentUICulture.Name;
698                 }
699                 if (!string.IsNullOrEmpty(curCul) && curCul != "en" && curCul != "ja")
700                 {
701                     if (!Directory.Exists(Path.Combine(MyCommon.settingPath, curCul)))
702                     {
703                         Directory.CreateDirectory(Path.Combine(MyCommon.settingPath, curCul));
704                     }
705                     if (!(new HttpVarious()).GetDataToFile("http://tween.sourceforge.jp/TweenRes" + curCul + strVer + ".gz?" + DateTime.Now.ToString("yyMMddHHmmss") + Environment.TickCount.ToString(),
706                                                         Path.Combine(Path.Combine(MyCommon.settingPath, curCul), "Tween.resourcesNew.dll")))
707                     {
708                         //return "Err:Download failed";
709                     }
710                 }
711                 //スレッドの言語圏のリソース
712                 string curCul2;
713                 if (!Thread.CurrentThread.CurrentCulture.IsNeutralCulture)
714                 {
715                     var idx = Thread.CurrentThread.CurrentCulture.Name.LastIndexOf('-');
716                     if (idx > -1)
717                     {
718                         curCul2 = Thread.CurrentThread.CurrentCulture.Name.Substring(0, idx);
719                     }
720                     else
721                     {
722                         curCul2 = Thread.CurrentThread.CurrentCulture.Name;
723                     }
724                 }
725                 else
726                 {
727                     curCul2 = Thread.CurrentThread.CurrentCulture.Name;
728                 }
729                 if (!string.IsNullOrEmpty(curCul2) && curCul2 != "en" && curCul2 != curCul)
730                 {
731                     if (!Directory.Exists(Path.Combine(MyCommon.settingPath, curCul2)))
732                     {
733                         Directory.CreateDirectory(Path.Combine(MyCommon.settingPath, curCul2));
734                     }
735                     if (!(new HttpVarious()).GetDataToFile("http://tween.sourceforge.jp/TweenRes" + curCul2 + strVer + ".gz?" + DateTime.Now.ToString("yyMMddHHmmss") + Environment.TickCount.ToString(),
736                                                     Path.Combine(Path.Combine(MyCommon.settingPath, curCul2), "Tween.resourcesNew.dll")))
737                     {
738                         //return "Err:Download failed";
739                     }
740                 }
741
742                 //アップデータ
743                 if (!(new HttpVarious()).GetDataToFile("http://tween.sourceforge.jp/TweenUp3.gz?" + DateTime.Now.ToString("yyMMddHHmmss") + Environment.TickCount.ToString(),
744                                                     Path.Combine(MyCommon.settingPath, "TweenUp3.exe")))
745                 {
746                     throw new WebApiException("Err:Download failed");
747                 }
748                 //シリアライザDLL
749                 if (!(new HttpVarious()).GetDataToFile("http://tween.sourceforge.jp/TweenDll" + strVer + ".gz?" + DateTime.Now.ToString("yyMMddHHmmss") + Environment.TickCount.ToString(),
750                                                     Path.Combine(MyCommon.settingPath, "TweenNew.XmlSerializers.dll")))
751                 {
752                     throw new WebApiException("Err:Download failed");
753                 }
754             }
755             catch (Exception ex)
756             {
757                 throw new WebApiException("Err:Download failed", ex);
758             }
759         }
760 #endregion
761
762         public bool ReadOwnPost
763         {
764             get
765             {
766                 return _readOwnPost;
767             }
768             set
769             {
770                 _readOwnPost = value;
771             }
772         }
773
774         public int FollowersCount { get; private set; }
775         public int FriendsCount { get; private set; }
776         public int StatusesCount { get; private set; }
777         public string Location { get; private set; } = "";
778         public string Bio { get; private set; } = "";
779
780         /// <summary>ユーザーのフォロワー数などの情報を更新します</summary>
781         private void UpdateUserStats(TwitterUser self)
782         {
783             this.FollowersCount = self.FollowersCount;
784             this.FriendsCount = self.FriendsCount;
785             this.StatusesCount = self.StatusesCount;
786             this.Location = self.Location;
787             this.Bio = self.Description;
788         }
789
790         /// <summary>
791         /// 渡された取得件数がWORKERTYPEに応じた取得可能範囲に収まっているか検証する
792         /// </summary>
793         public static bool VerifyApiResultCount(MyCommon.WORKERTYPE type, int count)
794         {
795             return count >= 20 && count <= GetMaxApiResultCount(type);
796         }
797
798         /// <summary>
799         /// 渡された取得件数が更新時の取得可能範囲に収まっているか検証する
800         /// </summary>
801         public static bool VerifyMoreApiResultCount(int count)
802         {
803             return count >= 20 && count <= 200;
804         }
805
806         /// <summary>
807         /// 渡された取得件数が起動時の取得可能範囲に収まっているか検証する
808         /// </summary>
809         public static bool VerifyFirstApiResultCount(int count)
810         {
811             return count >= 20 && count <= 200;
812         }
813
814         /// <summary>
815         /// WORKERTYPEに応じた取得可能な最大件数を取得する
816         /// </summary>
817         public static int GetMaxApiResultCount(MyCommon.WORKERTYPE type)
818         {
819             // 参照: REST APIs - 各endpointのcountパラメータ
820             // https://dev.twitter.com/rest/public
821             switch (type)
822             {
823                 case MyCommon.WORKERTYPE.Timeline:
824                 case MyCommon.WORKERTYPE.Reply:
825                 case MyCommon.WORKERTYPE.UserTimeline:
826                 case MyCommon.WORKERTYPE.Favorites:
827                 case MyCommon.WORKERTYPE.DirectMessegeRcv:
828                 case MyCommon.WORKERTYPE.DirectMessegeSnt:
829                 case MyCommon.WORKERTYPE.List:  // 不明
830                     return 200;
831
832                 case MyCommon.WORKERTYPE.PublicSearch:
833                     return 100;
834
835                 default:
836                     throw new InvalidOperationException("Invalid type: " + type);
837             }
838         }
839
840         /// <summary>
841         /// WORKERTYPEに応じた取得件数を取得する
842         /// </summary>
843         public static int GetApiResultCount(MyCommon.WORKERTYPE type, bool more, bool startup)
844         {
845             if (type == MyCommon.WORKERTYPE.DirectMessegeRcv ||
846                 type == MyCommon.WORKERTYPE.DirectMessegeSnt)
847             {
848                 return 20;
849             }
850
851             if (SettingCommon.Instance.UseAdditionalCount)
852             {
853                 switch (type)
854                 {
855                     case MyCommon.WORKERTYPE.Favorites:
856                         if (SettingCommon.Instance.FavoritesCountApi != 0)
857                             return SettingCommon.Instance.FavoritesCountApi;
858                         break;
859                     case MyCommon.WORKERTYPE.List:
860                         if (SettingCommon.Instance.ListCountApi != 0)
861                             return SettingCommon.Instance.ListCountApi;
862                         break;
863                     case MyCommon.WORKERTYPE.PublicSearch:
864                         if (SettingCommon.Instance.SearchCountApi != 0)
865                             return SettingCommon.Instance.SearchCountApi;
866                         break;
867                     case MyCommon.WORKERTYPE.UserTimeline:
868                         if (SettingCommon.Instance.UserTimelineCountApi != 0)
869                             return SettingCommon.Instance.UserTimelineCountApi;
870                         break;
871                 }
872                 if (more && SettingCommon.Instance.MoreCountApi != 0)
873                 {
874                     return Math.Min(SettingCommon.Instance.MoreCountApi, GetMaxApiResultCount(type));
875                 }
876                 if (startup && SettingCommon.Instance.FirstCountApi != 0 && type != MyCommon.WORKERTYPE.Reply)
877                 {
878                     return Math.Min(SettingCommon.Instance.FirstCountApi, GetMaxApiResultCount(type));
879                 }
880             }
881
882             // 上記に当てはまらない場合の共通処理
883             var count = SettingCommon.Instance.CountApi;
884
885             if (type == MyCommon.WORKERTYPE.Reply)
886                 count = SettingCommon.Instance.CountApiReply;
887
888             return Math.Min(count, GetMaxApiResultCount(type));
889         }
890
891         public void GetTimelineApi(bool read,
892                                 MyCommon.WORKERTYPE gType,
893                                 bool more,
894                                 bool startup)
895         {
896             this.CheckAccountState();
897
898             HttpStatusCode res;
899             var content = "";
900             var count = GetApiResultCount(gType, more, startup);
901
902             try
903             {
904                 if (gType == MyCommon.WORKERTYPE.Timeline)
905                 {
906                     if (more)
907                     {
908                         res = twCon.HomeTimeline(count, this.minHomeTimeline, null, ref content);
909                     }
910                     else
911                     {
912                         res = twCon.HomeTimeline(count, null, null, ref content);
913                     }
914                 }
915                 else
916                 {
917                     if (more)
918                     {
919                         res = twCon.Mentions(count, this.minMentions, null, ref content);
920                     }
921                     else
922                     {
923                         res = twCon.Mentions(count, null, null, ref content);
924                     }
925                 }
926             }
927             catch(Exception ex)
928             {
929                 throw new WebApiException("Err:" + ex.Message, ex);
930             }
931
932             this.CheckStatusCode(res, content);
933
934             var minimumId = CreatePostsFromJson(content, gType, null, read);
935
936             if (minimumId != null)
937             {
938                 if (gType == MyCommon.WORKERTYPE.Timeline)
939                     this.minHomeTimeline = minimumId.Value;
940                 else
941                     this.minMentions = minimumId.Value;
942             }
943         }
944
945         public void GetUserTimelineApi(bool read,
946                                          string userName,
947                                          TabClass tab,
948                                          bool more)
949         {
950             this.CheckAccountState();
951
952             HttpStatusCode res;
953             var content = "";
954             var count = GetApiResultCount(MyCommon.WORKERTYPE.UserTimeline, more, false);
955
956             try
957             {
958                 if (string.IsNullOrEmpty(userName))
959                 {
960                     var target = tab.User;
961                     if (string.IsNullOrEmpty(target)) return;
962                     userName = target;
963                     res = twCon.UserTimeline(null, target, count, null, null, ref content);
964                 }
965                 else
966                 {
967                     if (more)
968                     {
969                         res = twCon.UserTimeline(null, userName, count, tab.OldestId, null, ref content);
970                     }
971                     else
972                     {
973                         res = twCon.UserTimeline(null, userName, count, null, null, ref content);
974                     }
975                 }
976             }
977             catch(Exception ex)
978             {
979                 throw new WebApiException("Err:" + ex.Message, ex);
980             }
981
982             if (res == HttpStatusCode.Unauthorized)
983                 throw new WebApiException("Err:@" + userName + "'s Tweets are protected.");
984
985             this.CheckStatusCode(res, content);
986
987             var minimumId = CreatePostsFromJson(content, MyCommon.WORKERTYPE.UserTimeline, tab, read);
988
989             if (minimumId != null)
990                 tab.OldestId = minimumId.Value;
991         }
992
993         public async Task<PostClass> GetStatusApi(bool read, long id)
994         {
995             this.CheckAccountState();
996
997             var status = await this.Api.StatusesShow(id)
998                 .ConfigureAwait(false);
999
1000             var item = CreatePostsFromStatusData(status);
1001             if (item == null)
1002                 throw new WebApiException("Err:Can't create post");
1003
1004             item.IsRead = read;
1005             if (item.IsMe && !read && _readOwnPost) item.IsRead = true;
1006
1007             return item;
1008         }
1009
1010         public async Task GetStatusApi(bool read, long id, TabClass tab)
1011         {
1012             var post = await this.GetStatusApi(read, id)
1013                 .ConfigureAwait(false);
1014
1015             //非同期アイコン取得&StatusDictionaryに追加
1016             if (tab != null && tab.IsInnerStorageTabType)
1017                 tab.AddPostToInnerStorage(post);
1018             else
1019                 TabInformations.GetInstance().AddPost(post);
1020         }
1021
1022         private PostClass CreatePostsFromStatusData(TwitterStatus status)
1023         {
1024             return CreatePostsFromStatusData(status, false);
1025         }
1026
1027         private PostClass CreatePostsFromStatusData(TwitterStatus status, bool favTweet)
1028         {
1029             var post = new PostClass();
1030             TwitterEntities entities;
1031             string sourceHtml;
1032
1033             post.StatusId = status.Id;
1034             if (status.RetweetedStatus != null)
1035             {
1036                 var retweeted = status.RetweetedStatus;
1037
1038                 post.CreatedAt = MyCommon.DateTimeParse(retweeted.CreatedAt);
1039
1040                 //Id
1041                 post.RetweetedId = retweeted.Id;
1042                 //本文
1043                 post.TextFromApi = retweeted.Text;
1044                 entities = retweeted.MergedEntities;
1045                 sourceHtml = retweeted.Source;
1046                 //Reply先
1047                 post.InReplyToStatusId = retweeted.InReplyToStatusId;
1048                 post.InReplyToUser = retweeted.InReplyToScreenName;
1049                 post.InReplyToUserId = status.InReplyToUserId;
1050
1051                 if (favTweet)
1052                 {
1053                     post.IsFav = true;
1054                 }
1055                 else
1056                 {
1057                     //幻覚fav対策
1058                     var tc = TabInformations.GetInstance().GetTabByType(MyCommon.TabUsageType.Favorites);
1059                     post.IsFav = tc.Contains(retweeted.Id);
1060                 }
1061
1062                 if (retweeted.Coordinates != null)
1063                     post.PostGeo = new PostClass.StatusGeo(retweeted.Coordinates.Coordinates[0], retweeted.Coordinates.Coordinates[1]);
1064
1065                 //以下、ユーザー情報
1066                 var user = retweeted.User;
1067
1068                 if (user == null || user.ScreenName == null || status.User.ScreenName == null) return null;
1069
1070                 post.UserId = user.Id;
1071                 post.ScreenName = user.ScreenName;
1072                 post.Nickname = user.Name.Trim();
1073                 post.ImageUrl = user.ProfileImageUrlHttps;
1074                 post.IsProtect = user.Protected;
1075
1076                 //Retweetした人
1077                 post.RetweetedBy = status.User.ScreenName;
1078                 post.RetweetedByUserId = status.User.Id;
1079                 post.IsMe = post.RetweetedBy.ToLowerInvariant().Equals(_uname);
1080             }
1081             else
1082             {
1083                 post.CreatedAt = MyCommon.DateTimeParse(status.CreatedAt);
1084                 //本文
1085                 post.TextFromApi = status.Text;
1086                 entities = status.MergedEntities;
1087                 sourceHtml = status.Source;
1088                 post.InReplyToStatusId = status.InReplyToStatusId;
1089                 post.InReplyToUser = status.InReplyToScreenName;
1090                 post.InReplyToUserId = status.InReplyToUserId;
1091
1092                 if (favTweet)
1093                 {
1094                     post.IsFav = true;
1095                 }
1096                 else
1097                 {
1098                     //幻覚fav対策
1099                     var tc = TabInformations.GetInstance().GetTabByType(MyCommon.TabUsageType.Favorites);
1100                     post.IsFav = tc.Contains(post.StatusId) && TabInformations.GetInstance()[post.StatusId].IsFav;
1101                 }
1102
1103                 if (status.Coordinates != null)
1104                     post.PostGeo = new PostClass.StatusGeo(status.Coordinates.Coordinates[0], status.Coordinates.Coordinates[1]);
1105
1106                 //以下、ユーザー情報
1107                 var user = status.User;
1108
1109                 if (user == null || user.ScreenName == null) return null;
1110
1111                 post.UserId = user.Id;
1112                 post.ScreenName = user.ScreenName;
1113                 post.Nickname = user.Name.Trim();
1114                 post.ImageUrl = user.ProfileImageUrlHttps;
1115                 post.IsProtect = user.Protected;
1116                 post.IsMe = post.ScreenName.ToLowerInvariant().Equals(_uname);
1117             }
1118             //HTMLに整形
1119             string textFromApi = post.TextFromApi;
1120             post.Text = CreateHtmlAnchor(textFromApi, post.ReplyToList, entities, post.Media);
1121             post.TextFromApi = textFromApi;
1122             post.TextFromApi = this.ReplaceTextFromApi(post.TextFromApi, entities);
1123             post.TextFromApi = WebUtility.HtmlDecode(post.TextFromApi);
1124             post.TextFromApi = post.TextFromApi.Replace("<3", "\u2661");
1125
1126             post.QuoteStatusIds = GetQuoteTweetStatusIds(entities)
1127                 .Where(x => x != post.StatusId && x != post.RetweetedId)
1128                 .Distinct().ToArray();
1129
1130             post.ExpandedUrls = entities.OfType<TwitterEntityUrl>()
1131                 .Select(x => new PostClass.ExpandedUrlInfo(x.Url, x.ExpandedUrl))
1132                 .ToArray();
1133
1134             //Source整形
1135             var source = ParseSource(sourceHtml);
1136             post.Source = source.Item1;
1137             post.SourceUri = source.Item2;
1138
1139             post.IsReply = post.ReplyToList.Contains(_uname);
1140             post.IsExcludeReply = false;
1141
1142             if (post.IsMe)
1143             {
1144                 post.IsOwl = false;
1145             }
1146             else
1147             {
1148                 if (followerId.Count > 0) post.IsOwl = !followerId.Contains(post.UserId);
1149             }
1150
1151             post.IsDm = false;
1152             return post;
1153         }
1154
1155         /// <summary>
1156         /// ツイートに含まれる引用ツイートのURLからステータスIDを抽出
1157         /// </summary>
1158         public static IEnumerable<long> GetQuoteTweetStatusIds(IEnumerable<TwitterEntity> entities)
1159         {
1160             var urls = entities.OfType<TwitterEntityUrl>().Select(x => x.ExpandedUrl);
1161
1162             return GetQuoteTweetStatusIds(urls);
1163         }
1164
1165         public static IEnumerable<long> GetQuoteTweetStatusIds(IEnumerable<string> urls)
1166         {
1167             foreach (var url in urls)
1168             {
1169                 var match = Twitter.StatusUrlRegex.Match(url);
1170                 if (match.Success)
1171                 {
1172                     long statusId;
1173                     if (long.TryParse(match.Groups["StatusId"].Value, out statusId))
1174                         yield return statusId;
1175                 }
1176             }
1177         }
1178
1179         private long? CreatePostsFromJson(string content, MyCommon.WORKERTYPE gType, TabClass tab, bool read)
1180         {
1181             TwitterStatus[] items;
1182             try
1183             {
1184                 items = TwitterStatus.ParseJsonArray(content);
1185             }
1186             catch(SerializationException ex)
1187             {
1188                 MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
1189                 throw new WebApiException("Json Parse Error(DataContractJsonSerializer)", content, ex);
1190             }
1191             catch(Exception ex)
1192             {
1193                 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
1194                 throw new WebApiException("Invalid Json!", content, ex);
1195             }
1196
1197             long? minimumId = null;
1198
1199             foreach (var status in items)
1200             {
1201                 PostClass post = null;
1202                 post = CreatePostsFromStatusData(status);
1203                 if (post == null) continue;
1204
1205                 if (minimumId == null || minimumId.Value > post.StatusId)
1206                     minimumId = post.StatusId;
1207
1208                 //二重取得回避
1209                 lock (LockObj)
1210                 {
1211                     if (tab == null)
1212                     {
1213                         if (TabInformations.GetInstance().ContainsKey(post.StatusId)) continue;
1214                     }
1215                     else
1216                     {
1217                         if (tab.Contains(post.StatusId)) continue;
1218                     }
1219                 }
1220
1221                 //RT禁止ユーザーによるもの
1222                 if (gType != MyCommon.WORKERTYPE.UserTimeline &&
1223                     post.RetweetedByUserId != null && this.noRTId.Contains(post.RetweetedByUserId.Value)) continue;
1224
1225                 post.IsRead = read;
1226                 if (post.IsMe && !read && _readOwnPost) post.IsRead = true;
1227
1228                 //非同期アイコン取得&StatusDictionaryに追加
1229                 if (tab != null && tab.IsInnerStorageTabType)
1230                     tab.AddPostToInnerStorage(post);
1231                 else
1232                     TabInformations.GetInstance().AddPost(post);
1233             }
1234
1235             return minimumId;
1236         }
1237
1238         private long? CreatePostsFromSearchJson(string content, TabClass tab, bool read, int count, bool more)
1239         {
1240             TwitterSearchResult items;
1241             try
1242             {
1243                 items = TwitterSearchResult.ParseJson(content);
1244             }
1245             catch (SerializationException ex)
1246             {
1247                 MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
1248                 throw new WebApiException("Json Parse Error(DataContractJsonSerializer)", content, ex);
1249             }
1250             catch (Exception ex)
1251             {
1252                 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
1253                 throw new WebApiException("Invalid Json!", content, ex);
1254             }
1255
1256             long? minimumId = null;
1257
1258             foreach (var result in items.Statuses)
1259             {
1260                 var post = CreatePostsFromStatusData(result);
1261                 if (post == null)
1262                     continue;
1263
1264                 if (minimumId == null || minimumId.Value > post.StatusId)
1265                     minimumId = post.StatusId;
1266
1267                 if (!more && post.StatusId > tab.SinceId) tab.SinceId = post.StatusId;
1268                 //二重取得回避
1269                 lock (LockObj)
1270                 {
1271                     if (tab == null)
1272                     {
1273                         if (TabInformations.GetInstance().ContainsKey(post.StatusId)) continue;
1274                     }
1275                     else
1276                     {
1277                         if (tab.Contains(post.StatusId)) continue;
1278                     }
1279                 }
1280
1281                 post.IsRead = read;
1282                 if ((post.IsMe && !read) && this._readOwnPost) post.IsRead = true;
1283
1284                 //非同期アイコン取得&StatusDictionaryに追加
1285                 if (tab != null && tab.IsInnerStorageTabType)
1286                     tab.AddPostToInnerStorage(post);
1287                 else
1288                     TabInformations.GetInstance().AddPost(post);
1289             }
1290
1291             return minimumId;
1292         }
1293
1294         private void CreateFavoritePostsFromJson(string content, bool read)
1295         {
1296             TwitterStatus[] item;
1297             try
1298             {
1299                 item = TwitterStatus.ParseJsonArray(content);
1300             }
1301             catch (SerializationException ex)
1302             {
1303                 MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
1304                 throw new WebApiException("Json Parse Error(DataContractJsonSerializer)", content, ex);
1305             }
1306             catch (Exception ex)
1307             {
1308                 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
1309                 throw new WebApiException("Invalid Json!", content, ex);
1310             }
1311
1312             var favTab = TabInformations.GetInstance().GetTabByType(MyCommon.TabUsageType.Favorites);
1313
1314             foreach (var status in item)
1315             {
1316                 //二重取得回避
1317                 lock (LockObj)
1318                 {
1319                     if (favTab.Contains(status.Id)) continue;
1320                 }
1321
1322                 var post = CreatePostsFromStatusData(status, true);
1323                 if (post == null) continue;
1324
1325                 post.IsRead = read;
1326
1327                 TabInformations.GetInstance().AddPost(post);
1328             }
1329         }
1330
1331         public void GetListStatus(bool read,
1332                                 TabClass tab,
1333                                 bool more,
1334                                 bool startup)
1335         {
1336             HttpStatusCode res;
1337             var content = "";
1338             var count = GetApiResultCount(MyCommon.WORKERTYPE.List, more, startup);
1339
1340             try
1341             {
1342                 if (more)
1343                 {
1344                     res = twCon.GetListsStatuses(tab.ListInfo.UserId, tab.ListInfo.Id, count, tab.OldestId, null, SettingCommon.Instance.IsListsIncludeRts, ref content);
1345                 }
1346                 else
1347                 {
1348                     res = twCon.GetListsStatuses(tab.ListInfo.UserId, tab.ListInfo.Id, count, null, null, SettingCommon.Instance.IsListsIncludeRts, ref content);
1349                 }
1350             }
1351             catch(Exception ex)
1352             {
1353                 throw new WebApiException("Err:" + ex.Message, ex);
1354             }
1355
1356             this.CheckStatusCode(res, content);
1357
1358             var minimumId = CreatePostsFromJson(content, MyCommon.WORKERTYPE.List, tab, read);
1359
1360             if (minimumId != null)
1361                 tab.OldestId = minimumId.Value;
1362         }
1363
1364         /// <summary>
1365         /// startStatusId からリプライ先の発言を辿る。発言は posts 以外からは検索しない。
1366         /// </summary>
1367         /// <returns>posts の中から検索されたリプライチェインの末端</returns>
1368         internal static PostClass FindTopOfReplyChain(IDictionary<Int64, PostClass> posts, Int64 startStatusId)
1369         {
1370             if (!posts.ContainsKey(startStatusId))
1371                 throw new ArgumentException("startStatusId (" + startStatusId + ") が posts の中から見つかりませんでした。", nameof(startStatusId));
1372
1373             var nextPost = posts[startStatusId];
1374             while (nextPost.InReplyToStatusId != null)
1375             {
1376                 if (!posts.ContainsKey(nextPost.InReplyToStatusId.Value))
1377                     break;
1378                 nextPost = posts[nextPost.InReplyToStatusId.Value];
1379             }
1380
1381             return nextPost;
1382         }
1383
1384         public async Task GetRelatedResult(bool read, TabClass tab)
1385         {
1386             var relPosts = new Dictionary<Int64, PostClass>();
1387             if (tab.RelationTargetPost.TextFromApi.Contains("@") && tab.RelationTargetPost.InReplyToStatusId == null)
1388             {
1389                 //検索結果対応
1390                 var p = TabInformations.GetInstance()[tab.RelationTargetPost.StatusId];
1391                 if (p != null && p.InReplyToStatusId != null)
1392                 {
1393                     tab.RelationTargetPost = p;
1394                 }
1395                 else
1396                 {
1397                     p = await this.GetStatusApi(read, tab.RelationTargetPost.StatusId)
1398                         .ConfigureAwait(false);
1399                     tab.RelationTargetPost = p;
1400                 }
1401             }
1402             relPosts.Add(tab.RelationTargetPost.StatusId, tab.RelationTargetPost);
1403
1404             Exception lastException = null;
1405
1406             // in_reply_to_status_id を使用してリプライチェインを辿る
1407             var nextPost = FindTopOfReplyChain(relPosts, tab.RelationTargetPost.StatusId);
1408             var loopCount = 1;
1409             while (nextPost.InReplyToStatusId != null && loopCount++ <= 20)
1410             {
1411                 var inReplyToId = nextPost.InReplyToStatusId.Value;
1412
1413                 var inReplyToPost = TabInformations.GetInstance()[inReplyToId];
1414                 if (inReplyToPost == null)
1415                 {
1416                     try
1417                     {
1418                         inReplyToPost = await this.GetStatusApi(read, inReplyToId)
1419                             .ConfigureAwait(false);
1420                     }
1421                     catch (WebApiException ex)
1422                     {
1423                         lastException = ex;
1424                         break;
1425                     }
1426                 }
1427
1428                 relPosts.Add(inReplyToPost.StatusId, inReplyToPost);
1429
1430                 nextPost = FindTopOfReplyChain(relPosts, nextPost.StatusId);
1431             }
1432
1433             //MRTとかに対応のためツイート内にあるツイートを指すURLを取り込む
1434             var text = tab.RelationTargetPost.Text;
1435             var ma = Twitter.StatusUrlRegex.Matches(text).Cast<Match>()
1436                 .Concat(Twitter.ThirdPartyStatusUrlRegex.Matches(text).Cast<Match>());
1437             foreach (var _match in ma)
1438             {
1439                 Int64 _statusId;
1440                 if (Int64.TryParse(_match.Groups["StatusId"].Value, out _statusId))
1441                 {
1442                     if (relPosts.ContainsKey(_statusId))
1443                         continue;
1444
1445                     var p = TabInformations.GetInstance()[_statusId];
1446                     if (p == null)
1447                     {
1448                         try
1449                         {
1450                             p = await this.GetStatusApi(read, _statusId)
1451                                 .ConfigureAwait(false);
1452                         }
1453                         catch (WebApiException ex)
1454                         {
1455                             lastException = ex;
1456                             break;
1457                         }
1458                     }
1459
1460                     if (p != null)
1461                         relPosts.Add(p.StatusId, p);
1462                 }
1463             }
1464
1465             relPosts.Values.ToList().ForEach(p =>
1466             {
1467                 if (p.IsMe && !read && this._readOwnPost)
1468                     p.IsRead = true;
1469                 else
1470                     p.IsRead = read;
1471
1472                 tab.AddPostToInnerStorage(p);
1473             });
1474
1475             if (lastException != null)
1476                 throw new WebApiException(lastException.Message, lastException);
1477         }
1478
1479         public void GetSearch(bool read,
1480                             TabClass tab,
1481                             bool more)
1482         {
1483             HttpStatusCode res;
1484             var content = "";
1485             var count = GetApiResultCount(MyCommon.WORKERTYPE.PublicSearch, more, false);
1486             long? maxId = null;
1487             long? sinceId = null;
1488             if (more)
1489             {
1490                 maxId = tab.OldestId - 1;
1491             }
1492             else
1493             {
1494                 sinceId = tab.SinceId;
1495             }
1496
1497             try
1498             {
1499                 // TODO:一時的に40>100件に 件数変更UI作成の必要あり
1500                 res = twCon.Search(tab.SearchWords, tab.SearchLang, count, maxId, sinceId, ref content);
1501             }
1502             catch(Exception ex)
1503             {
1504                 throw new WebApiException("Err:" + ex.Message, ex);
1505             }
1506             switch (res)
1507             {
1508                 case HttpStatusCode.BadRequest:
1509                     throw new WebApiException("Invalid query", content);
1510                 case HttpStatusCode.NotFound:
1511                     throw new WebApiException("Invalid query", content);
1512                 case HttpStatusCode.PaymentRequired: //API Documentには420と書いてあるが、該当コードがないので402にしてある
1513                     throw new WebApiException("Search API Limit?", content);
1514                 case HttpStatusCode.OK:
1515                     break;
1516                 default:
1517                     throw new WebApiException("Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")", content);
1518             }
1519
1520             if (!TabInformations.GetInstance().ContainsTab(tab))
1521                 return;
1522
1523             var minimumId =  this.CreatePostsFromSearchJson(content, tab, read, count, more);
1524
1525             if (minimumId != null)
1526                 tab.OldestId = minimumId.Value;
1527         }
1528
1529         private void CreateDirectMessagesFromJson(string content, MyCommon.WORKERTYPE gType, bool read)
1530         {
1531             TwitterDirectMessage[] item;
1532             try
1533             {
1534                 if (gType == MyCommon.WORKERTYPE.UserStream)
1535                 {
1536                     item = new[] { TwitterStreamEventDirectMessage.ParseJson(content).DirectMessage };
1537                 }
1538                 else
1539                 {
1540                     item = TwitterDirectMessage.ParseJsonArray(content);
1541                 }
1542             }
1543             catch(SerializationException ex)
1544             {
1545                 MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
1546                 throw new WebApiException("Json Parse Error(DataContractJsonSerializer)", content, ex);
1547             }
1548             catch(Exception ex)
1549             {
1550                 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
1551                 throw new WebApiException("Invalid Json!", content, ex);
1552             }
1553
1554             foreach (var message in item)
1555             {
1556                 var post = new PostClass();
1557                 try
1558                 {
1559                     post.StatusId = message.Id;
1560                     if (gType != MyCommon.WORKERTYPE.UserStream)
1561                     {
1562                         if (gType == MyCommon.WORKERTYPE.DirectMessegeRcv)
1563                         {
1564                             if (minDirectmessage > post.StatusId) minDirectmessage = post.StatusId;
1565                         }
1566                         else
1567                         {
1568                             if (minDirectmessageSent > post.StatusId) minDirectmessageSent = post.StatusId;
1569                         }
1570                     }
1571
1572                     //二重取得回避
1573                     lock (LockObj)
1574                     {
1575                         if (TabInformations.GetInstance().GetTabByType(MyCommon.TabUsageType.DirectMessage).Contains(post.StatusId)) continue;
1576                     }
1577                     //sender_id
1578                     //recipient_id
1579                     post.CreatedAt = MyCommon.DateTimeParse(message.CreatedAt);
1580                     //本文
1581                     var textFromApi = message.Text;
1582                     //HTMLに整形
1583                     post.Text = CreateHtmlAnchor(textFromApi, post.ReplyToList, message.Entities, post.Media);
1584                     post.TextFromApi = this.ReplaceTextFromApi(textFromApi, message.Entities);
1585                     post.TextFromApi = WebUtility.HtmlDecode(post.TextFromApi);
1586                     post.TextFromApi = post.TextFromApi.Replace("<3", "\u2661");
1587                     post.IsFav = false;
1588
1589                     post.QuoteStatusIds = GetQuoteTweetStatusIds(message.Entities).Distinct().ToArray();
1590
1591                     post.ExpandedUrls = message.Entities.OfType<TwitterEntityUrl>()
1592                         .Select(x => new PostClass.ExpandedUrlInfo(x.Url, x.ExpandedUrl))
1593                         .ToArray();
1594
1595                     //以下、ユーザー情報
1596                     TwitterUser user;
1597                     if (gType == MyCommon.WORKERTYPE.UserStream)
1598                     {
1599                         if (twCon.AuthenticatedUsername.Equals(message.Recipient.ScreenName, StringComparison.CurrentCultureIgnoreCase))
1600                         {
1601                             user = message.Sender;
1602                             post.IsMe = false;
1603                             post.IsOwl = true;
1604                         }
1605                         else
1606                         {
1607                             user = message.Recipient;
1608                             post.IsMe = true;
1609                             post.IsOwl = false;
1610                         }
1611                     }
1612                     else
1613                     {
1614                         if (gType == MyCommon.WORKERTYPE.DirectMessegeRcv)
1615                         {
1616                             user = message.Sender;
1617                             post.IsMe = false;
1618                             post.IsOwl = true;
1619                         }
1620                         else
1621                         {
1622                             user = message.Recipient;
1623                             post.IsMe = true;
1624                             post.IsOwl = false;
1625                         }
1626                     }
1627
1628                     post.UserId = user.Id;
1629                     post.ScreenName = user.ScreenName;
1630                     post.Nickname = user.Name.Trim();
1631                     post.ImageUrl = user.ProfileImageUrlHttps;
1632                     post.IsProtect = user.Protected;
1633                 }
1634                 catch(Exception ex)
1635                 {
1636                     MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
1637                     MessageBox.Show("Parse Error(CreateDirectMessagesFromJson)");
1638                     continue;
1639                 }
1640
1641                 post.IsRead = read;
1642                 if (post.IsMe && !read && _readOwnPost) post.IsRead = true;
1643                 post.IsReply = false;
1644                 post.IsExcludeReply = false;
1645                 post.IsDm = true;
1646
1647                 var dmTab = TabInformations.GetInstance().GetTabByType(MyCommon.TabUsageType.DirectMessage);
1648                 dmTab.AddPostToInnerStorage(post);
1649             }
1650         }
1651
1652         public void GetDirectMessageApi(bool read,
1653                                 MyCommon.WORKERTYPE gType,
1654                                 bool more)
1655         {
1656             this.CheckAccountState();
1657             this.CheckAccessLevel(TwitterApiAccessLevel.ReadWriteAndDirectMessage);
1658
1659             HttpStatusCode res;
1660             var content = "";
1661             var count = GetApiResultCount(gType, more, false);
1662
1663             try
1664             {
1665                 if (gType == MyCommon.WORKERTYPE.DirectMessegeRcv)
1666                 {
1667                     if (more)
1668                     {
1669                         res = twCon.DirectMessages(count, minDirectmessage, null, ref content);
1670                     }
1671                     else
1672                     {
1673                         res = twCon.DirectMessages(count, null, null, ref content);
1674                     }
1675                 }
1676                 else
1677                 {
1678                     if (more)
1679                     {
1680                         res = twCon.DirectMessagesSent(count, minDirectmessageSent, null, ref content);
1681                     }
1682                     else
1683                     {
1684                         res = twCon.DirectMessagesSent(count, null, null, ref content);
1685                     }
1686                 }
1687             }
1688             catch(Exception ex)
1689             {
1690                 throw new WebApiException("Err:" + ex.Message, ex);
1691             }
1692
1693             this.CheckStatusCode(res, content);
1694
1695             CreateDirectMessagesFromJson(content, gType, read);
1696         }
1697
1698         public void GetFavoritesApi(bool read,
1699                             bool more)
1700         {
1701             this.CheckAccountState();
1702
1703             HttpStatusCode res;
1704             var content = "";
1705             var count = GetApiResultCount(MyCommon.WORKERTYPE.Favorites, more, false);
1706
1707             try
1708             {
1709                 res = twCon.Favorites(count, ref content);
1710             }
1711             catch(Exception ex)
1712             {
1713                 throw new WebApiException("Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")", ex);
1714             }
1715
1716             this.CheckStatusCode(res, content);
1717
1718             CreateFavoritePostsFromJson(content, read);
1719         }
1720
1721         private string ReplaceTextFromApi(string text, TwitterEntities entities)
1722         {
1723             if (entities != null)
1724             {
1725                 if (entities.Urls != null)
1726                 {
1727                     foreach (var m in entities.Urls)
1728                     {
1729                         if (!string.IsNullOrEmpty(m.DisplayUrl)) text = text.Replace(m.Url, m.DisplayUrl);
1730                     }
1731                 }
1732                 if (entities.Media != null)
1733                 {
1734                     foreach (var m in entities.Media)
1735                     {
1736                         if (m.AltText != null)
1737                         {
1738                             text = text.Replace(m.Url, string.Format(Properties.Resources.ImageAltText, m.AltText));
1739                         }
1740                         else
1741                         {
1742                             if (!string.IsNullOrEmpty(m.DisplayUrl)) text = text.Replace(m.Url, m.DisplayUrl);
1743                         }
1744                     }
1745                 }
1746             }
1747             return text;
1748         }
1749
1750         /// <summary>
1751         /// フォロワーIDを更新します
1752         /// </summary>
1753         /// <exception cref="WebApiException"/>
1754         public async Task RefreshFollowerIds()
1755         {
1756             if (MyCommon._endingFlag) return;
1757
1758             var cursor = -1L;
1759             var newFollowerIds = new HashSet<long>();
1760             do
1761             {
1762                 var ret = await this.Api.FollowersIds(cursor)
1763                     .ConfigureAwait(false);
1764
1765                 if (ret.Ids == null)
1766                     throw new WebApiException("ret.ids == null");
1767
1768                 newFollowerIds.UnionWith(ret.Ids);
1769                 cursor = ret.NextCursor;
1770             } while (cursor != 0);
1771
1772             this.followerId = newFollowerIds;
1773             TabInformations.GetInstance().RefreshOwl(this.followerId);
1774
1775             this._GetFollowerResult = true;
1776         }
1777
1778         public bool GetFollowersSuccess
1779         {
1780             get
1781             {
1782                 return _GetFollowerResult;
1783             }
1784         }
1785
1786         /// <summary>
1787         /// RT 非表示ユーザーを更新します
1788         /// </summary>
1789         /// <exception cref="WebApiException"/>
1790         public void RefreshNoRetweetIds()
1791         {
1792             if (MyCommon._endingFlag) return;
1793
1794             this.noRTId = this.NoRetweetIdsApi();
1795
1796             this._GetNoRetweetResult = true;
1797         }
1798
1799         private long[] NoRetweetIdsApi()
1800         {
1801             this.CheckAccountState();
1802
1803             HttpStatusCode res;
1804             var content = "";
1805             try
1806             {
1807                 res = twCon.NoRetweetIds(ref content);
1808             }
1809             catch(Exception e)
1810             {
1811                 throw new WebApiException("Err:" + e.Message + "(" + MethodBase.GetCurrentMethod().Name + ")", e);
1812             }
1813
1814             this.CheckStatusCode(res, content);
1815
1816             try
1817             {
1818                 return MyCommon.CreateDataFromJson<long[]>(content);
1819             }
1820             catch(SerializationException e)
1821             {
1822                 var ex = new WebApiException("Err:Json Parse Error(DataContractJsonSerializer)", content, e);
1823                 MyCommon.TraceOut(ex);
1824                 throw ex;
1825             }
1826             catch(Exception e)
1827             {
1828                 var ex = new WebApiException("Err:Invalid Json!", content, e);
1829                 MyCommon.TraceOut(ex);
1830                 throw ex;
1831             }
1832         }
1833
1834         public bool GetNoRetweetSuccess
1835         {
1836             get
1837             {
1838                 return _GetNoRetweetResult;
1839             }
1840         }
1841
1842         /// <summary>
1843         /// t.co の文字列長などの設定情報を更新します
1844         /// </summary>
1845         /// <exception cref="WebApiException"/>
1846         public void RefreshConfiguration()
1847         {
1848             this.Configuration = this.ConfigurationApi();
1849         }
1850
1851         private TwitterConfiguration ConfigurationApi()
1852         {
1853             HttpStatusCode res;
1854             var content = "";
1855             try
1856             {
1857                 res = twCon.GetConfiguration(ref content);
1858             }
1859             catch(Exception e)
1860             {
1861                 throw new WebApiException("Err:" + e.Message + "(" + MethodBase.GetCurrentMethod().Name + ")", e);
1862             }
1863
1864             this.CheckStatusCode(res, content);
1865
1866             try
1867             {
1868                 return TwitterConfiguration.ParseJson(content);
1869             }
1870             catch(SerializationException e)
1871             {
1872                 var ex = new WebApiException("Err:Json Parse Error(DataContractJsonSerializer)", content, e);
1873                 MyCommon.TraceOut(ex);
1874                 throw ex;
1875             }
1876             catch(Exception e)
1877             {
1878                 var ex = new WebApiException("Err:Invalid Json!", content, e);
1879                 MyCommon.TraceOut(ex);
1880                 throw ex;
1881             }
1882         }
1883
1884         public void GetListsApi()
1885         {
1886             this.CheckAccountState();
1887
1888             HttpStatusCode res;
1889             IEnumerable<ListElement> lists;
1890             var content = "";
1891
1892             try
1893             {
1894                 res = twCon.GetLists(this.Username, ref content);
1895             }
1896             catch (Exception ex)
1897             {
1898                 throw new WebApiException("Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")", ex);
1899             }
1900
1901             this.CheckStatusCode(res, content);
1902
1903             try
1904             {
1905                 lists = TwitterList.ParseJsonArray(content)
1906                     .Select(x => new ListElement(x, this));
1907             }
1908             catch (SerializationException ex)
1909             {
1910                 MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
1911                 throw new WebApiException("Err:Json Parse Error(DataContractJsonSerializer)", content, ex);
1912             }
1913             catch (Exception ex)
1914             {
1915                 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
1916                 throw new WebApiException("Err:Invalid Json!", content, ex);
1917             }
1918
1919             try
1920             {
1921                 res = twCon.GetListsSubscriptions(this.Username, ref content);
1922             }
1923             catch (Exception ex)
1924             {
1925                 throw new WebApiException("Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")", ex);
1926             }
1927
1928             this.CheckStatusCode(res, content);
1929
1930             try
1931             {
1932                 lists = lists.Concat(TwitterList.ParseJsonArray(content)
1933                     .Select(x => new ListElement(x, this)));
1934             }
1935             catch (SerializationException ex)
1936             {
1937                 MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
1938                 throw new WebApiException("Err:Json Parse Error(DataContractJsonSerializer)", content, ex);
1939             }
1940             catch (Exception ex)
1941             {
1942                 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
1943                 throw new WebApiException("Err:Invalid Json!", content, ex);
1944             }
1945
1946             TabInformations.GetInstance().SubscribableLists = lists.ToList();
1947         }
1948
1949         public void DeleteList(string list_id)
1950         {
1951             HttpStatusCode res;
1952             var content = "";
1953
1954             try
1955             {
1956                 res = twCon.DeleteListID(this.Username, list_id, ref content);
1957             }
1958             catch(Exception ex)
1959             {
1960                 throw new WebApiException("Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")", ex);
1961             }
1962
1963             this.CheckStatusCode(res, content);
1964         }
1965
1966         public ListElement EditList(string list_id, string new_name, bool isPrivate, string description)
1967         {
1968             HttpStatusCode res;
1969             var content = "";
1970
1971             try
1972             {
1973                 res = twCon.UpdateListID(this.Username, list_id, new_name, isPrivate, description, ref content);
1974             }
1975             catch(Exception ex)
1976             {
1977                 throw new WebApiException("Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")", ex);
1978             }
1979
1980             this.CheckStatusCode(res, content);
1981
1982             try
1983             {
1984                 var le = TwitterList.ParseJson(content);
1985                 return  new ListElement(le, this);
1986             }
1987             catch(SerializationException ex)
1988             {
1989                 MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
1990                 throw new WebApiException("Err:Json Parse Error(DataContractJsonSerializer)", content, ex);
1991             }
1992             catch(Exception ex)
1993             {
1994                 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
1995                 throw new WebApiException("Err:Invalid Json!", content, ex);
1996             }
1997         }
1998
1999         public long GetListMembers(string list_id, List<UserInfo> lists, long cursor)
2000         {
2001             this.CheckAccountState();
2002
2003             HttpStatusCode res;
2004             var content = "";
2005             try
2006             {
2007                 res = twCon.GetListMembers(this.Username, list_id, cursor, ref content);
2008             }
2009             catch(Exception ex)
2010             {
2011                 throw new WebApiException("Err:" + ex.Message);
2012             }
2013
2014             this.CheckStatusCode(res, content);
2015
2016             try
2017             {
2018                 var users = TwitterUsers.ParseJson(content);
2019                 Array.ForEach<TwitterUser>(
2020                     users.Users,
2021                     u => lists.Add(new UserInfo(u)));
2022
2023                 return users.NextCursor;
2024             }
2025             catch(SerializationException ex)
2026             {
2027                 MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
2028                 throw new WebApiException("Err:Json Parse Error(DataContractJsonSerializer)", content, ex);
2029             }
2030             catch(Exception ex)
2031             {
2032                 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
2033                 throw new WebApiException("Err:Invalid Json!", content, ex);
2034             }
2035         }
2036
2037         public void CreateListApi(string listName, bool isPrivate, string description)
2038         {
2039             this.CheckAccountState();
2040
2041             HttpStatusCode res;
2042             var content = "";
2043             try
2044             {
2045                 res = twCon.CreateLists(listName, isPrivate, description, ref content);
2046             }
2047             catch(Exception ex)
2048             {
2049                 throw new WebApiException("Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")", ex);
2050             }
2051
2052             this.CheckStatusCode(res, content);
2053
2054             try
2055             {
2056                 var le = TwitterList.ParseJson(content);
2057                 TabInformations.GetInstance().SubscribableLists.Add(new ListElement(le, this));
2058             }
2059             catch(SerializationException ex)
2060             {
2061                 MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
2062                 throw new WebApiException("Err:Json Parse Error(DataContractJsonSerializer)", content, ex);
2063             }
2064             catch(Exception ex)
2065             {
2066                 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
2067                 throw new WebApiException("Err:Invalid Json!", content, ex);
2068             }
2069         }
2070
2071         public bool ContainsUserAtList(string listId, string user)
2072         {
2073             this.CheckAccountState();
2074
2075             HttpStatusCode res;
2076             var content = "";
2077
2078             try
2079             {
2080                 res = this.twCon.ShowListMember(listId, user, ref content);
2081             }
2082             catch(Exception ex)
2083             {
2084                 throw new WebApiException("Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")", ex);
2085             }
2086
2087             if (res == HttpStatusCode.NotFound)
2088             {
2089                 return false;
2090             }
2091
2092             this.CheckStatusCode(res, content);
2093
2094             try
2095             {
2096                 TwitterUser.ParseJson(content);
2097                 return true;
2098             }
2099             catch(Exception)
2100             {
2101                 return false;
2102             }
2103         }
2104
2105         public void AddUserToList(string listId, string user)
2106         {
2107             HttpStatusCode res;
2108             var content = "";
2109
2110             try
2111             {
2112                 res = twCon.CreateListMembers(listId, user, ref content);
2113             }
2114             catch(Exception ex)
2115             {
2116                 throw new WebApiException("Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")", ex);
2117             }
2118
2119             this.CheckStatusCode(res, content);
2120         }
2121
2122         public void RemoveUserToList(string listId, string user)
2123         {
2124             HttpStatusCode res;
2125             var content = "";
2126
2127             try
2128             {
2129                 res = twCon.DeleteListMembers(listId, user, ref content);
2130             }
2131             catch(Exception ex)
2132             {
2133                 throw new WebApiException("Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")", ex);
2134             }
2135
2136             this.CheckStatusCode(res, content);
2137         }
2138
2139         public string CreateHtmlAnchor(string text, List<string> AtList, TwitterEntities entities, List<MediaInfo> media)
2140         {
2141             if (entities != null)
2142             {
2143                 if (entities.Hashtags != null)
2144                 {
2145                     lock (this.LockObj)
2146                     {
2147                         this._hashList.AddRange(entities.Hashtags.Select(x => "#" + x.Text));
2148                     }
2149                 }
2150                 if (entities.UserMentions != null)
2151                 {
2152                     foreach (var ent in entities.UserMentions)
2153                     {
2154                         var screenName = ent.ScreenName.ToLowerInvariant();
2155                         if (!AtList.Contains(screenName))
2156                             AtList.Add(screenName);
2157                     }
2158                 }
2159                 if (entities.Media != null)
2160                 {
2161                     if (media != null)
2162                     {
2163                         foreach (var ent in entities.Media)
2164                         {
2165                             if (!media.Any(x => x.Url == ent.MediaUrl))
2166                             {
2167                                 if (ent.VideoInfo != null &&
2168                                     ent.Type == "animated_gif" || ent.Type == "video")
2169                                 {
2170                                     //var videoUrl = ent.VideoInfo.Variants
2171                                     //    .Where(v => v.ContentType == "video/mp4")
2172                                     //    .OrderByDescending(v => v.Bitrate)
2173                                     //    .Select(v => v.Url).FirstOrDefault();
2174                                     media.Add(new MediaInfo(ent.MediaUrl, ent.AltText, ent.ExpandedUrl));
2175                                 }
2176                                 else
2177                                     media.Add(new MediaInfo(ent.MediaUrl, ent.AltText, videoUrl: null));
2178                             }
2179                         }
2180                     }
2181                 }
2182             }
2183
2184             // PostClass.ExpandedUrlInfo を使用して非同期に URL 展開を行うためここでは expanded_url を使用しない
2185             text = TweetFormatter.AutoLinkHtml(text, entities, keepTco: true);
2186
2187             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>");
2188             text = PreProcessUrl(text); //IDN置換
2189
2190             return text;
2191         }
2192
2193         private static readonly Uri SourceUriBase = new Uri("https://twitter.com/");
2194
2195         /// <summary>
2196         /// Twitter APIから得たHTML形式のsource文字列を分析し、source名とURLに分離します
2197         /// </summary>
2198         public static Tuple<string, Uri> ParseSource(string sourceHtml)
2199         {
2200             if (string.IsNullOrEmpty(sourceHtml))
2201                 return Tuple.Create<string, Uri>("", null);
2202
2203             string sourceText;
2204             Uri sourceUri;
2205
2206             // sourceHtmlの例: <a href="http://twitter.com" rel="nofollow">Twitter Web Client</a>
2207
2208             var match = Regex.Match(sourceHtml, "^<a href=\"(?<uri>.+?)\".*?>(?<text>.+)</a>$", RegexOptions.IgnoreCase);
2209             if (match.Success)
2210             {
2211                 sourceText = WebUtility.HtmlDecode(match.Groups["text"].Value);
2212                 try
2213                 {
2214                     var uriStr = WebUtility.HtmlDecode(match.Groups["uri"].Value);
2215                     sourceUri = new Uri(SourceUriBase, uriStr);
2216                 }
2217                 catch (UriFormatException)
2218                 {
2219                     sourceUri = null;
2220                 }
2221             }
2222             else
2223             {
2224                 sourceText = WebUtility.HtmlDecode(sourceHtml);
2225                 sourceUri = null;
2226             }
2227
2228             return Tuple.Create(sourceText, sourceUri);
2229         }
2230
2231         public TwitterApiStatus GetInfoApi()
2232         {
2233             if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return null;
2234
2235             if (MyCommon._endingFlag) return null;
2236
2237             HttpStatusCode res;
2238             var content = "";
2239             try
2240             {
2241                 res = twCon.RateLimitStatus(ref content);
2242             }
2243             catch (Exception)
2244             {
2245                 this.ResetApiStatus();
2246                 return null;
2247             }
2248
2249             this.CheckStatusCode(res, content);
2250
2251             try
2252             {
2253                 MyCommon.TwitterApiInfo.UpdateFromJson(content);
2254                 return MyCommon.TwitterApiInfo;
2255             }
2256             catch (Exception ex)
2257             {
2258                 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
2259                 MyCommon.TwitterApiInfo.Reset();
2260                 return null;
2261             }
2262         }
2263
2264         /// <summary>
2265         /// ブロック中のユーザーを更新します
2266         /// </summary>
2267         /// <exception cref="WebApiException"/>
2268         public void RefreshBlockIds()
2269         {
2270             if (MyCommon._endingFlag) return;
2271
2272             var cursor = -1L;
2273             var newBlockIds = new HashSet<long>();
2274             do
2275             {
2276                 var ret = this.GetBlockIdsApi(cursor);
2277                 newBlockIds.UnionWith(ret.Ids);
2278                 cursor = ret.NextCursor;
2279             } while (cursor != 0);
2280
2281             newBlockIds.Remove(this.UserId); // 元のソースにあったので一応残しておく
2282
2283             TabInformations.GetInstance().BlockIds = newBlockIds;
2284         }
2285
2286         public TwitterIds GetBlockIdsApi(long cursor)
2287         {
2288             this.CheckAccountState();
2289
2290             HttpStatusCode res;
2291             var content = "";
2292             try
2293             {
2294                 res = twCon.GetBlockUserIds(ref content, cursor);
2295             }
2296             catch(Exception e)
2297             {
2298                 throw new WebApiException("Err:" + e.Message + "(" + MethodBase.GetCurrentMethod().Name + ")", e);
2299             }
2300
2301             this.CheckStatusCode(res, content);
2302
2303             try
2304             {
2305                 return TwitterIds.ParseJson(content);
2306             }
2307             catch(SerializationException e)
2308             {
2309                 var ex = new WebApiException("Err:Json Parse Error(DataContractJsonSerializer)", content, e);
2310                 MyCommon.TraceOut(ex);
2311                 throw ex;
2312             }
2313             catch(Exception e)
2314             {
2315                 var ex = new WebApiException("Err:Invalid Json!", content, e);
2316                 MyCommon.TraceOut(ex);
2317                 throw ex;
2318             }
2319         }
2320
2321         /// <summary>
2322         /// ミュート中のユーザーIDを更新します
2323         /// </summary>
2324         /// <exception cref="WebApiException"/>
2325         public async Task RefreshMuteUserIdsAsync()
2326         {
2327             if (MyCommon._endingFlag) return;
2328
2329             var ids = await TwitterIds.GetAllItemsAsync(this.GetMuteUserIdsApiAsync)
2330                 .ConfigureAwait(false);
2331
2332             TabInformations.GetInstance().MuteUserIds = new HashSet<long>(ids);
2333         }
2334
2335         public async Task<TwitterIds> GetMuteUserIdsApiAsync(long cursor)
2336         {
2337             var content = "";
2338
2339             try
2340             {
2341                 var res = await Task.Run(() => twCon.GetMuteUserIds(ref content, cursor))
2342                     .ConfigureAwait(false);
2343
2344                 this.CheckStatusCode(res, content);
2345
2346                 return TwitterIds.ParseJson(content);
2347             }
2348             catch (WebException ex)
2349             {
2350                 var ex2 = new WebApiException("Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")", content, ex);
2351                 MyCommon.TraceOut(ex2);
2352                 throw ex2;
2353             }
2354             catch (SerializationException ex)
2355             {
2356                 var ex2 = new WebApiException("Err:Json Parse Error(DataContractJsonSerializer)", content, ex);
2357                 MyCommon.TraceOut(ex2);
2358                 throw ex2;
2359             }
2360         }
2361
2362         public string[] GetHashList()
2363         {
2364             string[] hashArray;
2365             lock (LockObj)
2366             {
2367                 hashArray = _hashList.ToArray();
2368                 _hashList.Clear();
2369             }
2370             return hashArray;
2371         }
2372
2373         public string AccessToken
2374         {
2375             get
2376             {
2377                 return twCon.AccessToken;
2378             }
2379         }
2380
2381         public string AccessTokenSecret
2382         {
2383             get
2384             {
2385                 return twCon.AccessTokenSecret;
2386             }
2387         }
2388
2389         private void CheckAccountState()
2390         {
2391             if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
2392                 throw new WebApiException("Auth error. Check your account");
2393         }
2394
2395         private void CheckAccessLevel(TwitterApiAccessLevel accessLevelFlags)
2396         {
2397             if (!this.AccessLevel.HasFlag(accessLevelFlags))
2398                 throw new WebApiException("Auth Err:try to re-authorization.");
2399         }
2400
2401         private void CheckStatusCode(HttpStatusCode httpStatus, string responseText,
2402             [CallerMemberName] string callerMethodName = "")
2403         {
2404             if (httpStatus == HttpStatusCode.OK)
2405             {
2406                 Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
2407                 return;
2408             }
2409
2410             if (string.IsNullOrWhiteSpace(responseText))
2411             {
2412                 if (httpStatus == HttpStatusCode.Unauthorized)
2413                     Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
2414
2415                 throw new WebApiException("Err:" + httpStatus + "(" + callerMethodName + ")");
2416             }
2417
2418             try
2419             {
2420                 var errors = TwitterError.ParseJson(responseText).Errors;
2421                 if (errors == null || !errors.Any())
2422                 {
2423                     throw new WebApiException("Err:" + httpStatus + "(" + callerMethodName + ")", responseText);
2424                 }
2425
2426                 foreach (var error in errors)
2427                 {
2428                     if (error.Code == TwitterErrorCode.InvalidToken ||
2429                         error.Code == TwitterErrorCode.SuspendedAccount)
2430                     {
2431                         Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
2432                     }
2433                 }
2434
2435                 throw new WebApiException("Err:" + string.Join(",", errors.Select(x => x.ToString())) + "(" + callerMethodName + ")", responseText);
2436             }
2437             catch (SerializationException) { }
2438
2439             throw new WebApiException("Err:" + httpStatus + "(" + callerMethodName + ")", responseText);
2440         }
2441
2442         public int GetTextLengthRemain(string postText)
2443         {
2444             var matchDm = Twitter.DMSendTextRegex.Match(postText);
2445             if (matchDm.Success)
2446                 return this.GetTextLengthRemainInternal(matchDm.Groups["body"].Value, isDm: true);
2447
2448             return this.GetTextLengthRemainInternal(postText, isDm: false);
2449         }
2450
2451         private int GetTextLengthRemainInternal(string postText, bool isDm)
2452         {
2453             var textLength = 0;
2454
2455             var pos = 0;
2456             while (pos < postText.Length)
2457             {
2458                 textLength++;
2459
2460                 if (char.IsSurrogatePair(postText, pos))
2461                     pos += 2; // サロゲートペアの場合は2文字分進める
2462                 else
2463                     pos++;
2464             }
2465
2466             var urls = TweetExtractor.ExtractUrls(postText);
2467             foreach (var url in urls)
2468             {
2469                 var shortUrlLength = url.StartsWith("https://", StringComparison.OrdinalIgnoreCase)
2470                     ? this.Configuration.ShortUrlLengthHttps
2471                     : this.Configuration.ShortUrlLength;
2472
2473                 textLength += shortUrlLength - url.Length;
2474             }
2475
2476             if (isDm)
2477                 return this.Configuration.DmTextCharacterLimit - textLength;
2478             else
2479                 return 140 - textLength;
2480         }
2481
2482
2483 #region "UserStream"
2484         private string trackWord_ = "";
2485         public string TrackWord
2486         {
2487             get
2488             {
2489                 return trackWord_;
2490             }
2491             set
2492             {
2493                 trackWord_ = value;
2494             }
2495         }
2496         private bool allAtReply_ = false;
2497         public bool AllAtReply
2498         {
2499             get
2500             {
2501                 return allAtReply_;
2502             }
2503             set
2504             {
2505                 allAtReply_ = value;
2506             }
2507         }
2508
2509         public event EventHandler NewPostFromStream;
2510         public event EventHandler UserStreamStarted;
2511         public event EventHandler UserStreamStopped;
2512         public event EventHandler<PostDeletedEventArgs> PostDeleted;
2513         public event EventHandler<UserStreamEventReceivedEventArgs> UserStreamEventReceived;
2514         private DateTime _lastUserstreamDataReceived;
2515         private TwitterUserstream userStream;
2516
2517         public class FormattedEvent
2518         {
2519             public MyCommon.EVENTTYPE Eventtype { get; set; }
2520             public DateTime CreatedAt { get; set; }
2521             public string Event { get; set; }
2522             public string Username { get; set; }
2523             public string Target { get; set; }
2524             public Int64 Id { get; set; }
2525             public bool IsMe { get; set; }
2526         }
2527
2528         public List<FormattedEvent> storedEvent_ = new List<FormattedEvent>();
2529         public List<FormattedEvent> StoredEvent
2530         {
2531             get
2532             {
2533                 return storedEvent_;
2534             }
2535             set
2536             {
2537                 storedEvent_ = value;
2538             }
2539         }
2540
2541         private readonly IReadOnlyDictionary<string, MyCommon.EVENTTYPE> eventTable = new Dictionary<string, MyCommon.EVENTTYPE>
2542         {
2543             ["favorite"] = MyCommon.EVENTTYPE.Favorite,
2544             ["unfavorite"] = MyCommon.EVENTTYPE.Unfavorite,
2545             ["follow"] = MyCommon.EVENTTYPE.Follow,
2546             ["list_member_added"] = MyCommon.EVENTTYPE.ListMemberAdded,
2547             ["list_member_removed"] = MyCommon.EVENTTYPE.ListMemberRemoved,
2548             ["block"] = MyCommon.EVENTTYPE.Block,
2549             ["unblock"] = MyCommon.EVENTTYPE.Unblock,
2550             ["user_update"] = MyCommon.EVENTTYPE.UserUpdate,
2551             ["deleted"] = MyCommon.EVENTTYPE.Deleted,
2552             ["list_created"] = MyCommon.EVENTTYPE.ListCreated,
2553             ["list_destroyed"] = MyCommon.EVENTTYPE.ListDestroyed,
2554             ["list_updated"] = MyCommon.EVENTTYPE.ListUpdated,
2555             ["unfollow"] = MyCommon.EVENTTYPE.Unfollow,
2556             ["list_user_subscribed"] = MyCommon.EVENTTYPE.ListUserSubscribed,
2557             ["list_user_unsubscribed"] = MyCommon.EVENTTYPE.ListUserUnsubscribed,
2558             ["mute"] = MyCommon.EVENTTYPE.Mute,
2559             ["unmute"] = MyCommon.EVENTTYPE.Unmute,
2560             ["quoted_tweet"] = MyCommon.EVENTTYPE.QuotedTweet,
2561         };
2562
2563         public bool IsUserstreamDataReceived
2564         {
2565             get
2566             {
2567                 return DateTime.Now.Subtract(this._lastUserstreamDataReceived).TotalSeconds < 31;
2568             }
2569         }
2570
2571         private void userStream_StatusArrived(string line)
2572         {
2573             this._lastUserstreamDataReceived = DateTime.Now;
2574             if (string.IsNullOrEmpty(line)) return;
2575
2576             if (line.First() != '{' || line.Last() != '}')
2577             {
2578                 MyCommon.TraceOut("Invalid JSON (StatusArrived):" + Environment.NewLine + line);
2579                 return;
2580             }
2581
2582             var isDm = false;
2583
2584             try
2585             {
2586                 using (var jsonReader = JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(line), XmlDictionaryReaderQuotas.Max))
2587                 {
2588                     var xElm = XElement.Load(jsonReader);
2589                     if (xElm.Element("friends") != null)
2590                     {
2591                         Debug.WriteLine("friends");
2592                         return;
2593                     }
2594                     else if (xElm.Element("delete") != null)
2595                     {
2596                         Debug.WriteLine("delete");
2597                         Int64 id;
2598                         XElement idElm;
2599                         if ((idElm = xElm.Element("delete").Element("direct_message")?.Element("id")) != null)
2600                         {
2601                             id = 0;
2602                             long.TryParse(idElm.Value, out id);
2603
2604                             this.PostDeleted?.Invoke(this, new PostDeletedEventArgs(id));
2605                         }
2606                         else if ((idElm = xElm.Element("delete").Element("status")?.Element("id")) != null)
2607                         {
2608                             id = 0;
2609                             long.TryParse(idElm.Value, out id);
2610
2611                             this.PostDeleted?.Invoke(this, new PostDeletedEventArgs(id));
2612                         }
2613                         else
2614                         {
2615                             MyCommon.TraceOut("delete:" + line);
2616                             return;
2617                         }
2618                         for (int i = this.StoredEvent.Count - 1; i >= 0; i--)
2619                         {
2620                             var sEvt = this.StoredEvent[i];
2621                             if (sEvt.Id == id && (sEvt.Event == "favorite" || sEvt.Event == "unfavorite"))
2622                             {
2623                                 this.StoredEvent.RemoveAt(i);
2624                             }
2625                         }
2626                         return;
2627                     }
2628                     else if (xElm.Element("limit") != null)
2629                     {
2630                         Debug.WriteLine(line);
2631                         return;
2632                     }
2633                     else if (xElm.Element("event") != null)
2634                     {
2635                         Debug.WriteLine("event: " + xElm.Element("event").Value);
2636                         CreateEventFromJson(line);
2637                         return;
2638                     }
2639                     else if (xElm.Element("direct_message") != null)
2640                     {
2641                         Debug.WriteLine("direct_message");
2642                         isDm = true;
2643                     }
2644                     else if (xElm.Element("retweeted_status") != null)
2645                     {
2646                         var sourceUserId = xElm.XPathSelectElement("/user/id_str").Value;
2647                         var targetUserId = xElm.XPathSelectElement("/retweeted_status/user/id_str").Value;
2648
2649                         // 自分に関係しないリツイートの場合は無視する
2650                         var selfUserId = this.UserId.ToString();
2651                         if (sourceUserId == selfUserId || targetUserId == selfUserId)
2652                         {
2653                             // 公式 RT をイベントとしても扱う
2654                             var evt = CreateEventFromRetweet(xElm);
2655                             if (evt != null)
2656                             {
2657                                 this.StoredEvent.Insert(0, evt);
2658
2659                                 this.UserStreamEventReceived?.Invoke(this, new UserStreamEventReceivedEventArgs(evt));
2660                             }
2661                         }
2662
2663                         // 従来通り公式 RT の表示も行うため return しない
2664                     }
2665                     else if (xElm.Element("scrub_geo") != null)
2666                     {
2667                         try
2668                         {
2669                             TabInformations.GetInstance().ScrubGeoReserve(long.Parse(xElm.Element("scrub_geo").Element("user_id").Value),
2670                                                                         long.Parse(xElm.Element("scrub_geo").Element("up_to_status_id").Value));
2671                         }
2672                         catch(Exception)
2673                         {
2674                             MyCommon.TraceOut("scrub_geo:" + line);
2675                         }
2676                         return;
2677                     }
2678                 }
2679
2680                 if (isDm)
2681                 {
2682                     CreateDirectMessagesFromJson(line, MyCommon.WORKERTYPE.UserStream, false);
2683                 }
2684                 else
2685                 {
2686                     CreatePostsFromJson("[" + line + "]", MyCommon.WORKERTYPE.Timeline, null, false);
2687                 }
2688             }
2689             catch (WebApiException ex)
2690             {
2691                 MyCommon.TraceOut(ex);
2692                 return;
2693             }
2694             catch(NullReferenceException)
2695             {
2696                 MyCommon.TraceOut("NullRef StatusArrived: " + line);
2697             }
2698
2699             this.NewPostFromStream?.Invoke(this, EventArgs.Empty);
2700         }
2701
2702         /// <summary>
2703         /// UserStreamsから受信した公式RTをイベントに変換します
2704         /// </summary>
2705         private FormattedEvent CreateEventFromRetweet(XElement xElm)
2706         {
2707             return new FormattedEvent
2708             {
2709                 Eventtype = MyCommon.EVENTTYPE.Retweet,
2710                 Event = "retweet",
2711                 CreatedAt = MyCommon.DateTimeParse(xElm.XPathSelectElement("/created_at").Value),
2712                 IsMe = xElm.XPathSelectElement("/user/id_str").Value == this.UserId.ToString(),
2713                 Username = xElm.XPathSelectElement("/user/screen_name").Value,
2714                 Target = string.Format("@{0}:{1}", new[]
2715                 {
2716                     xElm.XPathSelectElement("/retweeted_status/user/screen_name").Value,
2717                     WebUtility.HtmlDecode(xElm.XPathSelectElement("/retweeted_status/text").Value),
2718                 }),
2719                 Id = long.Parse(xElm.XPathSelectElement("/retweeted_status/id_str").Value),
2720             };
2721         }
2722
2723         private void CreateEventFromJson(string content)
2724         {
2725             TwitterStreamEvent eventData = null;
2726             try
2727             {
2728                 eventData = TwitterStreamEvent.ParseJson(content);
2729             }
2730             catch(SerializationException ex)
2731             {
2732                 MyCommon.TraceOut(ex, "Event Serialize Exception!" + Environment.NewLine + content);
2733             }
2734             catch(Exception ex)
2735             {
2736                 MyCommon.TraceOut(ex, "Event Exception!" + Environment.NewLine + content);
2737             }
2738
2739             var evt = new FormattedEvent();
2740             evt.CreatedAt = MyCommon.DateTimeParse(eventData.CreatedAt);
2741             evt.Event = eventData.Event;
2742             evt.Username = eventData.Source.ScreenName;
2743             evt.IsMe = evt.Username.ToLowerInvariant().Equals(this.Username.ToLowerInvariant());
2744
2745             MyCommon.EVENTTYPE eventType;
2746             eventTable.TryGetValue(eventData.Event, out eventType);
2747             evt.Eventtype = eventType;
2748
2749             TwitterStreamEvent<TwitterStatus> tweetEvent;
2750
2751             switch (eventData.Event)
2752             {
2753                 case "access_revoked":
2754                 case "access_unrevoked":
2755                 case "user_delete":
2756                 case "user_suspend":
2757                     return;
2758                 case "follow":
2759                     if (eventData.Target.ScreenName.ToLowerInvariant().Equals(_uname))
2760                     {
2761                         if (!this.followerId.Contains(eventData.Source.Id)) this.followerId.Add(eventData.Source.Id);
2762                     }
2763                     else
2764                     {
2765                         return;    //Block後のUndoをすると、SourceとTargetが逆転したfollowイベントが帰ってくるため。
2766                     }
2767                     evt.Target = "";
2768                     break;
2769                 case "unfollow":
2770                     evt.Target = "@" + eventData.Target.ScreenName;
2771                     break;
2772                 case "favorited_retweet":
2773                 case "retweeted_retweet":
2774                     return;
2775                 case "favorite":
2776                 case "unfavorite":
2777                     tweetEvent = TwitterStreamEvent<TwitterStatus>.ParseJson(content);
2778                     evt.Target = "@" + tweetEvent.TargetObject.User.ScreenName + ":" + WebUtility.HtmlDecode(tweetEvent.TargetObject.Text);
2779                     evt.Id = tweetEvent.TargetObject.Id;
2780
2781                     if (SettingCommon.Instance.IsRemoveSameEvent)
2782                     {
2783                         if (this.StoredEvent.Any(ev => ev.Username == evt.Username && ev.Eventtype == evt.Eventtype && ev.Target == evt.Target))
2784                             return;
2785                     }
2786
2787                     var tabinfo = TabInformations.GetInstance();
2788
2789                     PostClass post;
2790                     var statusId = tweetEvent.TargetObject.Id;
2791                     if (!tabinfo.Posts.TryGetValue(statusId, out post))
2792                         break;
2793
2794                     if (eventData.Event == "favorite")
2795                     {
2796                         var favTab = tabinfo.GetTabByType(MyCommon.TabUsageType.Favorites);
2797                         if (!favTab.Contains(post.StatusId))
2798                             favTab.AddPostImmediately(post.StatusId, post.IsRead);
2799
2800                         if (tweetEvent.Source.Id == this.UserId)
2801                         {
2802                             post.IsFav = true;
2803                         }
2804                         else if (tweetEvent.Target.Id == this.UserId)
2805                         {
2806                             post.FavoritedCount++;
2807
2808                             if (SettingCommon.Instance.FavEventUnread)
2809                                 tabinfo.SetReadAllTab(post.StatusId, read: false);
2810                         }
2811                     }
2812                     else // unfavorite
2813                     {
2814                         if (tweetEvent.Source.Id == this.UserId)
2815                         {
2816                             post.IsFav = false;
2817                         }
2818                         else if (tweetEvent.Target.Id == this.UserId)
2819                         {
2820                             post.FavoritedCount = Math.Max(0, post.FavoritedCount - 1);
2821                         }
2822                     }
2823                     break;
2824                 case "quoted_tweet":
2825                     if (evt.IsMe) return;
2826
2827                     tweetEvent = TwitterStreamEvent<TwitterStatus>.ParseJson(content);
2828                     evt.Target = "@" + tweetEvent.TargetObject.User.ScreenName + ":" + WebUtility.HtmlDecode(tweetEvent.TargetObject.Text);
2829                     evt.Id = tweetEvent.TargetObject.Id;
2830
2831                     if (SettingCommon.Instance.IsRemoveSameEvent)
2832                     {
2833                         if (this.StoredEvent.Any(ev => ev.Username == evt.Username && ev.Eventtype == evt.Eventtype && ev.Target == evt.Target))
2834                             return;
2835                     }
2836                     break;
2837                 case "list_member_added":
2838                 case "list_member_removed":
2839                 case "list_created":
2840                 case "list_destroyed":
2841                 case "list_updated":
2842                 case "list_user_subscribed":
2843                 case "list_user_unsubscribed":
2844                     var listEvent = TwitterStreamEvent<TwitterList>.ParseJson(content);
2845                     evt.Target = listEvent.TargetObject.FullName;
2846                     break;
2847                 case "block":
2848                     if (!TabInformations.GetInstance().BlockIds.Contains(eventData.Target.Id)) TabInformations.GetInstance().BlockIds.Add(eventData.Target.Id);
2849                     evt.Target = "";
2850                     break;
2851                 case "unblock":
2852                     if (TabInformations.GetInstance().BlockIds.Contains(eventData.Target.Id)) TabInformations.GetInstance().BlockIds.Remove(eventData.Target.Id);
2853                     evt.Target = "";
2854                     break;
2855                 case "user_update":
2856                     evt.Target = "";
2857                     break;
2858                 
2859                 // Mute / Unmute
2860                 case "mute":
2861                     evt.Target = "@" + eventData.Target.ScreenName;
2862                     if (!TabInformations.GetInstance().MuteUserIds.Contains(eventData.Target.Id))
2863                     {
2864                         TabInformations.GetInstance().MuteUserIds.Add(eventData.Target.Id);
2865                     }
2866                     break;
2867                 case "unmute":
2868                     evt.Target = "@" + eventData.Target.ScreenName;
2869                     if (TabInformations.GetInstance().MuteUserIds.Contains(eventData.Target.Id))
2870                     {
2871                         TabInformations.GetInstance().MuteUserIds.Remove(eventData.Target.Id);
2872                     }
2873                     break;
2874
2875                 default:
2876                     MyCommon.TraceOut("Unknown Event:" + evt.Event + Environment.NewLine + content);
2877                     break;
2878             }
2879             this.StoredEvent.Insert(0, evt);
2880
2881             this.UserStreamEventReceived?.Invoke(this, new UserStreamEventReceivedEventArgs(evt));
2882         }
2883
2884         private void userStream_Started()
2885         {
2886             this.UserStreamStarted?.Invoke(this, EventArgs.Empty);
2887         }
2888
2889         private void userStream_Stopped()
2890         {
2891             this.UserStreamStopped?.Invoke(this, EventArgs.Empty);
2892         }
2893
2894         public bool UserStreamEnabled
2895         {
2896             get
2897             {
2898                 return userStream == null ? false : userStream.Enabled;
2899             }
2900         }
2901
2902         public void StartUserStream()
2903         {
2904             if (userStream != null)
2905             {
2906                 StopUserStream();
2907             }
2908             userStream = new TwitterUserstream(twCon);
2909             userStream.StatusArrived += userStream_StatusArrived;
2910             userStream.Started += userStream_Started;
2911             userStream.Stopped += userStream_Stopped;
2912             userStream.Start(this.AllAtReply, this.TrackWord);
2913         }
2914
2915         public void StopUserStream()
2916         {
2917             userStream?.Dispose();
2918             userStream = null;
2919             if (!MyCommon._endingFlag)
2920             {
2921                 this.UserStreamStopped?.Invoke(this, EventArgs.Empty);
2922             }
2923         }
2924
2925         public void ReconnectUserStream()
2926         {
2927             if (userStream != null)
2928             {
2929                 this.StartUserStream();
2930             }
2931         }
2932
2933         private class TwitterUserstream : IDisposable
2934         {
2935             public event Action<string> StatusArrived;
2936             public event Action Stopped;
2937             public event Action Started;
2938             private HttpTwitter twCon;
2939
2940             private Thread _streamThread;
2941             private bool _streamActive;
2942
2943             private bool _allAtreplies = false;
2944             private string _trackwords = "";
2945
2946             public TwitterUserstream(HttpTwitter twitterConnection)
2947             {
2948                 twCon = (HttpTwitter)twitterConnection.Clone();
2949             }
2950
2951             public void Start(bool allAtReplies, string trackwords)
2952             {
2953                 this.AllAtReplies = allAtReplies;
2954                 this.TrackWords = trackwords;
2955                 _streamActive = true;
2956                 if (_streamThread != null && _streamThread.IsAlive) return;
2957                 _streamThread = new Thread(UserStreamLoop);
2958                 _streamThread.Name = "UserStreamReceiver";
2959                 _streamThread.IsBackground = true;
2960                 _streamThread.Start();
2961             }
2962
2963             public bool Enabled
2964             {
2965                 get
2966                 {
2967                     return _streamActive;
2968                 }
2969             }
2970
2971             public bool AllAtReplies
2972             {
2973                 get
2974                 {
2975                     return _allAtreplies;
2976                 }
2977                 set
2978                 {
2979                     _allAtreplies = value;
2980                 }
2981             }
2982
2983             public string TrackWords
2984             {
2985                 get
2986                 {
2987                     return _trackwords;
2988                 }
2989                 set
2990                 {
2991                     _trackwords = value;
2992                 }
2993             }
2994
2995             private void UserStreamLoop()
2996             {
2997                 var sleepSec = 0;
2998                 do
2999                 {
3000                     Stream st = null;
3001                     StreamReader sr = null;
3002                     try
3003                     {
3004                         if (!MyCommon.IsNetworkAvailable())
3005                         {
3006                             sleepSec = 30;
3007                             continue;
3008                         }
3009
3010                         Started?.Invoke();
3011
3012                         var res = twCon.UserStream(ref st, _allAtreplies, _trackwords, Networking.GetUserAgentString());
3013
3014                         switch (res)
3015                         {
3016                             case HttpStatusCode.OK:
3017                                 Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
3018                                 break;
3019                             case HttpStatusCode.Unauthorized:
3020                                 Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
3021                                 sleepSec = 120;
3022                                 continue;
3023                         }
3024
3025                         if (st == null)
3026                         {
3027                             sleepSec = 30;
3028                             //MyCommon.TraceOut("Stop:stream is null")
3029                             continue;
3030                         }
3031
3032                         sr = new StreamReader(st);
3033
3034                         while (_streamActive && !sr.EndOfStream && Twitter.AccountState == MyCommon.ACCOUNT_STATE.Valid)
3035                         {
3036                             StatusArrived?.Invoke(sr.ReadLine());
3037                             //this.LastTime = Now;
3038                         }
3039
3040                         if (sr.EndOfStream || Twitter.AccountState == MyCommon.ACCOUNT_STATE.Invalid)
3041                         {
3042                             sleepSec = 30;
3043                             //MyCommon.TraceOut("Stop:EndOfStream")
3044                             continue;
3045                         }
3046                         break;
3047                     }
3048                     catch(WebException ex)
3049                     {
3050                         if (ex.Status == WebExceptionStatus.Timeout)
3051                         {
3052                             sleepSec = 30;                        //MyCommon.TraceOut("Stop:Timeout")
3053                         }
3054                         else if (ex.Response != null && (int)((HttpWebResponse)ex.Response).StatusCode == 420)
3055                         {
3056                             //MyCommon.TraceOut("Stop:Connection Limit")
3057                             break;
3058                         }
3059                         else
3060                         {
3061                             sleepSec = 30;
3062                             //MyCommon.TraceOut("Stop:WebException " + ex.Status.ToString())
3063                         }
3064                     }
3065                     catch(ThreadAbortException)
3066                     {
3067                         break;
3068                     }
3069                     catch(IOException)
3070                     {
3071                         sleepSec = 30;
3072                         //MyCommon.TraceOut("Stop:IOException with Active." + Environment.NewLine + ex.Message)
3073                     }
3074                     catch(ArgumentException ex)
3075                     {
3076                         //System.ArgumentException: ストリームを読み取れませんでした。
3077                         //サーバー側もしくは通信経路上で切断された場合?タイムアウト頻発後発生
3078                         sleepSec = 30;
3079                         MyCommon.TraceOut(ex, "Stop:ArgumentException");
3080                     }
3081                     catch(Exception ex)
3082                     {
3083                         MyCommon.TraceOut("Stop:Exception." + Environment.NewLine + ex.Message);
3084                         MyCommon.ExceptionOut(ex);
3085                         sleepSec = 30;
3086                     }
3087                     finally
3088                     {
3089                         if (_streamActive)
3090                         {
3091                             Stopped?.Invoke();
3092                         }
3093                         twCon.RequestAbort();
3094                         sr?.Close();
3095                         if (sleepSec > 0)
3096                         {
3097                             var ms = 0;
3098                             while (_streamActive && ms < sleepSec * 1000)
3099                             {
3100                                 Thread.Sleep(500);
3101                                 ms += 500;
3102                             }
3103                         }
3104                         sleepSec = 0;
3105                     }
3106                 } while (this._streamActive);
3107
3108                 if (_streamActive)
3109                 {
3110                     Stopped?.Invoke();
3111                 }
3112                 MyCommon.TraceOut("Stop:EndLoop");
3113             }
3114
3115 #region "IDisposable Support"
3116             private bool disposedValue; // 重複する呼び出しを検出するには
3117
3118             // IDisposable
3119             protected virtual void Dispose(bool disposing)
3120             {
3121                 if (!this.disposedValue)
3122                 {
3123                     if (disposing)
3124                     {
3125                         _streamActive = false;
3126                         if (_streamThread != null && _streamThread.IsAlive)
3127                         {
3128                             _streamThread.Abort();
3129                         }
3130                     }
3131                 }
3132                 this.disposedValue = true;
3133             }
3134
3135             //protected Overrides void Finalize()
3136             //{
3137             //    // このコードを変更しないでください。クリーンアップ コードを上の Dispose(bool disposing) に記述します。
3138             //    Dispose(false)
3139             //    MyBase.Finalize()
3140             //}
3141
3142             // このコードは、破棄可能なパターンを正しく実装できるように Visual Basic によって追加されました。
3143             public void Dispose()
3144             {
3145                 // このコードを変更しないでください。クリーンアップ コードを上の Dispose(bool disposing) に記述します。
3146                 Dispose(true);
3147                 GC.SuppressFinalize(this);
3148             }
3149 #endregion
3150
3151         }
3152 #endregion
3153
3154 #region "IDisposable Support"
3155         private bool disposedValue; // 重複する呼び出しを検出するには
3156
3157         // IDisposable
3158         protected virtual void Dispose(bool disposing)
3159         {
3160             if (!this.disposedValue)
3161             {
3162                 if (disposing)
3163                 {
3164                     this.StopUserStream();
3165                 }
3166             }
3167             this.disposedValue = true;
3168         }
3169
3170         //protected Overrides void Finalize()
3171         //{
3172         //    // このコードを変更しないでください。クリーンアップ コードを上の Dispose(bool disposing) に記述します。
3173         //    Dispose(false)
3174         //    MyBase.Finalize()
3175         //}
3176
3177         // このコードは、破棄可能なパターンを正しく実装できるように Visual Basic によって追加されました。
3178         public void Dispose()
3179         {
3180             // このコードを変更しないでください。クリーンアップ コードを上の Dispose(bool disposing) に記述します。
3181             Dispose(true);
3182             GC.SuppressFinalize(this);
3183         }
3184 #endregion
3185     }
3186
3187     public class PostDeletedEventArgs : EventArgs
3188     {
3189         public long StatusId { get; }
3190
3191         public PostDeletedEventArgs(long statusId)
3192         {
3193             this.StatusId = statusId;
3194         }
3195     }
3196
3197     public class UserStreamEventReceivedEventArgs : EventArgs
3198     {
3199         public Twitter.FormattedEvent EventData { get; }
3200
3201         public UserStreamEventReceivedEventArgs(Twitter.FormattedEvent eventData)
3202         {
3203             this.EventData = eventData;
3204         }
3205     }
3206 }