OSDN Git Service

HttpTwitter.GetConfigurationメソッドを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 async Task RefreshNoRetweetIds()
1791         {
1792             if (MyCommon._endingFlag) return;
1793
1794             this.noRTId = await this.Api.NoRetweetIds()
1795                 .ConfigureAwait(false);
1796
1797             this._GetNoRetweetResult = true;
1798         }
1799
1800         public bool GetNoRetweetSuccess
1801         {
1802             get
1803             {
1804                 return _GetNoRetweetResult;
1805             }
1806         }
1807
1808         /// <summary>
1809         /// t.co の文字列長などの設定情報を更新します
1810         /// </summary>
1811         /// <exception cref="WebApiException"/>
1812         public async Task RefreshConfiguration()
1813         {
1814             this.Configuration = await this.Api.Configuration()
1815                 .ConfigureAwait(false);
1816         }
1817
1818         public void GetListsApi()
1819         {
1820             this.CheckAccountState();
1821
1822             HttpStatusCode res;
1823             IEnumerable<ListElement> lists;
1824             var content = "";
1825
1826             try
1827             {
1828                 res = twCon.GetLists(this.Username, ref content);
1829             }
1830             catch (Exception ex)
1831             {
1832                 throw new WebApiException("Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")", ex);
1833             }
1834
1835             this.CheckStatusCode(res, content);
1836
1837             try
1838             {
1839                 lists = TwitterList.ParseJsonArray(content)
1840                     .Select(x => new ListElement(x, this));
1841             }
1842             catch (SerializationException ex)
1843             {
1844                 MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
1845                 throw new WebApiException("Err:Json Parse Error(DataContractJsonSerializer)", content, ex);
1846             }
1847             catch (Exception ex)
1848             {
1849                 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
1850                 throw new WebApiException("Err:Invalid Json!", content, ex);
1851             }
1852
1853             try
1854             {
1855                 res = twCon.GetListsSubscriptions(this.Username, ref content);
1856             }
1857             catch (Exception ex)
1858             {
1859                 throw new WebApiException("Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")", ex);
1860             }
1861
1862             this.CheckStatusCode(res, content);
1863
1864             try
1865             {
1866                 lists = lists.Concat(TwitterList.ParseJsonArray(content)
1867                     .Select(x => new ListElement(x, this)));
1868             }
1869             catch (SerializationException ex)
1870             {
1871                 MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
1872                 throw new WebApiException("Err:Json Parse Error(DataContractJsonSerializer)", content, ex);
1873             }
1874             catch (Exception ex)
1875             {
1876                 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
1877                 throw new WebApiException("Err:Invalid Json!", content, ex);
1878             }
1879
1880             TabInformations.GetInstance().SubscribableLists = lists.ToList();
1881         }
1882
1883         public void DeleteList(string list_id)
1884         {
1885             HttpStatusCode res;
1886             var content = "";
1887
1888             try
1889             {
1890                 res = twCon.DeleteListID(this.Username, list_id, ref content);
1891             }
1892             catch(Exception ex)
1893             {
1894                 throw new WebApiException("Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")", ex);
1895             }
1896
1897             this.CheckStatusCode(res, content);
1898         }
1899
1900         public ListElement EditList(string list_id, string new_name, bool isPrivate, string description)
1901         {
1902             HttpStatusCode res;
1903             var content = "";
1904
1905             try
1906             {
1907                 res = twCon.UpdateListID(this.Username, list_id, new_name, isPrivate, description, ref content);
1908             }
1909             catch(Exception ex)
1910             {
1911                 throw new WebApiException("Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")", ex);
1912             }
1913
1914             this.CheckStatusCode(res, content);
1915
1916             try
1917             {
1918                 var le = TwitterList.ParseJson(content);
1919                 return  new ListElement(le, this);
1920             }
1921             catch(SerializationException ex)
1922             {
1923                 MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
1924                 throw new WebApiException("Err:Json Parse Error(DataContractJsonSerializer)", content, ex);
1925             }
1926             catch(Exception ex)
1927             {
1928                 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
1929                 throw new WebApiException("Err:Invalid Json!", content, ex);
1930             }
1931         }
1932
1933         public long GetListMembers(string list_id, List<UserInfo> lists, long cursor)
1934         {
1935             this.CheckAccountState();
1936
1937             HttpStatusCode res;
1938             var content = "";
1939             try
1940             {
1941                 res = twCon.GetListMembers(this.Username, list_id, cursor, ref content);
1942             }
1943             catch(Exception ex)
1944             {
1945                 throw new WebApiException("Err:" + ex.Message);
1946             }
1947
1948             this.CheckStatusCode(res, content);
1949
1950             try
1951             {
1952                 var users = TwitterUsers.ParseJson(content);
1953                 Array.ForEach<TwitterUser>(
1954                     users.Users,
1955                     u => lists.Add(new UserInfo(u)));
1956
1957                 return users.NextCursor;
1958             }
1959             catch(SerializationException ex)
1960             {
1961                 MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
1962                 throw new WebApiException("Err:Json Parse Error(DataContractJsonSerializer)", content, ex);
1963             }
1964             catch(Exception ex)
1965             {
1966                 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
1967                 throw new WebApiException("Err:Invalid Json!", content, ex);
1968             }
1969         }
1970
1971         public void CreateListApi(string listName, bool isPrivate, string description)
1972         {
1973             this.CheckAccountState();
1974
1975             HttpStatusCode res;
1976             var content = "";
1977             try
1978             {
1979                 res = twCon.CreateLists(listName, isPrivate, description, ref content);
1980             }
1981             catch(Exception ex)
1982             {
1983                 throw new WebApiException("Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")", ex);
1984             }
1985
1986             this.CheckStatusCode(res, content);
1987
1988             try
1989             {
1990                 var le = TwitterList.ParseJson(content);
1991                 TabInformations.GetInstance().SubscribableLists.Add(new ListElement(le, this));
1992             }
1993             catch(SerializationException ex)
1994             {
1995                 MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
1996                 throw new WebApiException("Err:Json Parse Error(DataContractJsonSerializer)", content, ex);
1997             }
1998             catch(Exception ex)
1999             {
2000                 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
2001                 throw new WebApiException("Err:Invalid Json!", content, ex);
2002             }
2003         }
2004
2005         public bool ContainsUserAtList(string listId, string user)
2006         {
2007             this.CheckAccountState();
2008
2009             HttpStatusCode res;
2010             var content = "";
2011
2012             try
2013             {
2014                 res = this.twCon.ShowListMember(listId, user, ref content);
2015             }
2016             catch(Exception ex)
2017             {
2018                 throw new WebApiException("Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")", ex);
2019             }
2020
2021             if (res == HttpStatusCode.NotFound)
2022             {
2023                 return false;
2024             }
2025
2026             this.CheckStatusCode(res, content);
2027
2028             try
2029             {
2030                 TwitterUser.ParseJson(content);
2031                 return true;
2032             }
2033             catch(Exception)
2034             {
2035                 return false;
2036             }
2037         }
2038
2039         public void AddUserToList(string listId, string user)
2040         {
2041             HttpStatusCode res;
2042             var content = "";
2043
2044             try
2045             {
2046                 res = twCon.CreateListMembers(listId, user, ref content);
2047             }
2048             catch(Exception ex)
2049             {
2050                 throw new WebApiException("Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")", ex);
2051             }
2052
2053             this.CheckStatusCode(res, content);
2054         }
2055
2056         public void RemoveUserToList(string listId, string user)
2057         {
2058             HttpStatusCode res;
2059             var content = "";
2060
2061             try
2062             {
2063                 res = twCon.DeleteListMembers(listId, user, ref content);
2064             }
2065             catch(Exception ex)
2066             {
2067                 throw new WebApiException("Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")", ex);
2068             }
2069
2070             this.CheckStatusCode(res, content);
2071         }
2072
2073         public string CreateHtmlAnchor(string text, List<string> AtList, TwitterEntities entities, List<MediaInfo> media)
2074         {
2075             if (entities != null)
2076             {
2077                 if (entities.Hashtags != null)
2078                 {
2079                     lock (this.LockObj)
2080                     {
2081                         this._hashList.AddRange(entities.Hashtags.Select(x => "#" + x.Text));
2082                     }
2083                 }
2084                 if (entities.UserMentions != null)
2085                 {
2086                     foreach (var ent in entities.UserMentions)
2087                     {
2088                         var screenName = ent.ScreenName.ToLowerInvariant();
2089                         if (!AtList.Contains(screenName))
2090                             AtList.Add(screenName);
2091                     }
2092                 }
2093                 if (entities.Media != null)
2094                 {
2095                     if (media != null)
2096                     {
2097                         foreach (var ent in entities.Media)
2098                         {
2099                             if (!media.Any(x => x.Url == ent.MediaUrl))
2100                             {
2101                                 if (ent.VideoInfo != null &&
2102                                     ent.Type == "animated_gif" || ent.Type == "video")
2103                                 {
2104                                     //var videoUrl = ent.VideoInfo.Variants
2105                                     //    .Where(v => v.ContentType == "video/mp4")
2106                                     //    .OrderByDescending(v => v.Bitrate)
2107                                     //    .Select(v => v.Url).FirstOrDefault();
2108                                     media.Add(new MediaInfo(ent.MediaUrl, ent.AltText, ent.ExpandedUrl));
2109                                 }
2110                                 else
2111                                     media.Add(new MediaInfo(ent.MediaUrl, ent.AltText, videoUrl: null));
2112                             }
2113                         }
2114                     }
2115                 }
2116             }
2117
2118             // PostClass.ExpandedUrlInfo を使用して非同期に URL 展開を行うためここでは expanded_url を使用しない
2119             text = TweetFormatter.AutoLinkHtml(text, entities, keepTco: true);
2120
2121             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>");
2122             text = PreProcessUrl(text); //IDN置換
2123
2124             return text;
2125         }
2126
2127         private static readonly Uri SourceUriBase = new Uri("https://twitter.com/");
2128
2129         /// <summary>
2130         /// Twitter APIから得たHTML形式のsource文字列を分析し、source名とURLに分離します
2131         /// </summary>
2132         public static Tuple<string, Uri> ParseSource(string sourceHtml)
2133         {
2134             if (string.IsNullOrEmpty(sourceHtml))
2135                 return Tuple.Create<string, Uri>("", null);
2136
2137             string sourceText;
2138             Uri sourceUri;
2139
2140             // sourceHtmlの例: <a href="http://twitter.com" rel="nofollow">Twitter Web Client</a>
2141
2142             var match = Regex.Match(sourceHtml, "^<a href=\"(?<uri>.+?)\".*?>(?<text>.+)</a>$", RegexOptions.IgnoreCase);
2143             if (match.Success)
2144             {
2145                 sourceText = WebUtility.HtmlDecode(match.Groups["text"].Value);
2146                 try
2147                 {
2148                     var uriStr = WebUtility.HtmlDecode(match.Groups["uri"].Value);
2149                     sourceUri = new Uri(SourceUriBase, uriStr);
2150                 }
2151                 catch (UriFormatException)
2152                 {
2153                     sourceUri = null;
2154                 }
2155             }
2156             else
2157             {
2158                 sourceText = WebUtility.HtmlDecode(sourceHtml);
2159                 sourceUri = null;
2160             }
2161
2162             return Tuple.Create(sourceText, sourceUri);
2163         }
2164
2165         public TwitterApiStatus GetInfoApi()
2166         {
2167             if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return null;
2168
2169             if (MyCommon._endingFlag) return null;
2170
2171             HttpStatusCode res;
2172             var content = "";
2173             try
2174             {
2175                 res = twCon.RateLimitStatus(ref content);
2176             }
2177             catch (Exception)
2178             {
2179                 this.ResetApiStatus();
2180                 return null;
2181             }
2182
2183             this.CheckStatusCode(res, content);
2184
2185             try
2186             {
2187                 MyCommon.TwitterApiInfo.UpdateFromJson(content);
2188                 return MyCommon.TwitterApiInfo;
2189             }
2190             catch (Exception ex)
2191             {
2192                 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
2193                 MyCommon.TwitterApiInfo.Reset();
2194                 return null;
2195             }
2196         }
2197
2198         /// <summary>
2199         /// ブロック中のユーザーを更新します
2200         /// </summary>
2201         /// <exception cref="WebApiException"/>
2202         public async Task RefreshBlockIds()
2203         {
2204             if (MyCommon._endingFlag) return;
2205
2206             var cursor = -1L;
2207             var newBlockIds = new HashSet<long>();
2208             do
2209             {
2210                 var ret = await this.Api.BlocksIds(cursor)
2211                     .ConfigureAwait(false);
2212
2213                 newBlockIds.UnionWith(ret.Ids);
2214                 cursor = ret.NextCursor;
2215             } while (cursor != 0);
2216
2217             newBlockIds.Remove(this.UserId); // 元のソースにあったので一応残しておく
2218
2219             TabInformations.GetInstance().BlockIds = newBlockIds;
2220         }
2221
2222         /// <summary>
2223         /// ミュート中のユーザーIDを更新します
2224         /// </summary>
2225         /// <exception cref="WebApiException"/>
2226         public async Task RefreshMuteUserIdsAsync()
2227         {
2228             if (MyCommon._endingFlag) return;
2229
2230             var ids = await TwitterIds.GetAllItemsAsync(x => this.Api.MutesUsersIds(x))
2231                 .ConfigureAwait(false);
2232
2233             TabInformations.GetInstance().MuteUserIds = new HashSet<long>(ids);
2234         }
2235
2236         public string[] GetHashList()
2237         {
2238             string[] hashArray;
2239             lock (LockObj)
2240             {
2241                 hashArray = _hashList.ToArray();
2242                 _hashList.Clear();
2243             }
2244             return hashArray;
2245         }
2246
2247         public string AccessToken
2248         {
2249             get
2250             {
2251                 return twCon.AccessToken;
2252             }
2253         }
2254
2255         public string AccessTokenSecret
2256         {
2257             get
2258             {
2259                 return twCon.AccessTokenSecret;
2260             }
2261         }
2262
2263         private void CheckAccountState()
2264         {
2265             if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
2266                 throw new WebApiException("Auth error. Check your account");
2267         }
2268
2269         private void CheckAccessLevel(TwitterApiAccessLevel accessLevelFlags)
2270         {
2271             if (!this.AccessLevel.HasFlag(accessLevelFlags))
2272                 throw new WebApiException("Auth Err:try to re-authorization.");
2273         }
2274
2275         private void CheckStatusCode(HttpStatusCode httpStatus, string responseText,
2276             [CallerMemberName] string callerMethodName = "")
2277         {
2278             if (httpStatus == HttpStatusCode.OK)
2279             {
2280                 Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
2281                 return;
2282             }
2283
2284             if (string.IsNullOrWhiteSpace(responseText))
2285             {
2286                 if (httpStatus == HttpStatusCode.Unauthorized)
2287                     Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
2288
2289                 throw new WebApiException("Err:" + httpStatus + "(" + callerMethodName + ")");
2290             }
2291
2292             try
2293             {
2294                 var errors = TwitterError.ParseJson(responseText).Errors;
2295                 if (errors == null || !errors.Any())
2296                 {
2297                     throw new WebApiException("Err:" + httpStatus + "(" + callerMethodName + ")", responseText);
2298                 }
2299
2300                 foreach (var error in errors)
2301                 {
2302                     if (error.Code == TwitterErrorCode.InvalidToken ||
2303                         error.Code == TwitterErrorCode.SuspendedAccount)
2304                     {
2305                         Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
2306                     }
2307                 }
2308
2309                 throw new WebApiException("Err:" + string.Join(",", errors.Select(x => x.ToString())) + "(" + callerMethodName + ")", responseText);
2310             }
2311             catch (SerializationException) { }
2312
2313             throw new WebApiException("Err:" + httpStatus + "(" + callerMethodName + ")", responseText);
2314         }
2315
2316         public int GetTextLengthRemain(string postText)
2317         {
2318             var matchDm = Twitter.DMSendTextRegex.Match(postText);
2319             if (matchDm.Success)
2320                 return this.GetTextLengthRemainInternal(matchDm.Groups["body"].Value, isDm: true);
2321
2322             return this.GetTextLengthRemainInternal(postText, isDm: false);
2323         }
2324
2325         private int GetTextLengthRemainInternal(string postText, bool isDm)
2326         {
2327             var textLength = 0;
2328
2329             var pos = 0;
2330             while (pos < postText.Length)
2331             {
2332                 textLength++;
2333
2334                 if (char.IsSurrogatePair(postText, pos))
2335                     pos += 2; // サロゲートペアの場合は2文字分進める
2336                 else
2337                     pos++;
2338             }
2339
2340             var urls = TweetExtractor.ExtractUrls(postText);
2341             foreach (var url in urls)
2342             {
2343                 var shortUrlLength = url.StartsWith("https://", StringComparison.OrdinalIgnoreCase)
2344                     ? this.Configuration.ShortUrlLengthHttps
2345                     : this.Configuration.ShortUrlLength;
2346
2347                 textLength += shortUrlLength - url.Length;
2348             }
2349
2350             if (isDm)
2351                 return this.Configuration.DmTextCharacterLimit - textLength;
2352             else
2353                 return 140 - textLength;
2354         }
2355
2356
2357 #region "UserStream"
2358         private string trackWord_ = "";
2359         public string TrackWord
2360         {
2361             get
2362             {
2363                 return trackWord_;
2364             }
2365             set
2366             {
2367                 trackWord_ = value;
2368             }
2369         }
2370         private bool allAtReply_ = false;
2371         public bool AllAtReply
2372         {
2373             get
2374             {
2375                 return allAtReply_;
2376             }
2377             set
2378             {
2379                 allAtReply_ = value;
2380             }
2381         }
2382
2383         public event EventHandler NewPostFromStream;
2384         public event EventHandler UserStreamStarted;
2385         public event EventHandler UserStreamStopped;
2386         public event EventHandler<PostDeletedEventArgs> PostDeleted;
2387         public event EventHandler<UserStreamEventReceivedEventArgs> UserStreamEventReceived;
2388         private DateTime _lastUserstreamDataReceived;
2389         private TwitterUserstream userStream;
2390
2391         public class FormattedEvent
2392         {
2393             public MyCommon.EVENTTYPE Eventtype { get; set; }
2394             public DateTime CreatedAt { get; set; }
2395             public string Event { get; set; }
2396             public string Username { get; set; }
2397             public string Target { get; set; }
2398             public Int64 Id { get; set; }
2399             public bool IsMe { get; set; }
2400         }
2401
2402         public List<FormattedEvent> storedEvent_ = new List<FormattedEvent>();
2403         public List<FormattedEvent> StoredEvent
2404         {
2405             get
2406             {
2407                 return storedEvent_;
2408             }
2409             set
2410             {
2411                 storedEvent_ = value;
2412             }
2413         }
2414
2415         private readonly IReadOnlyDictionary<string, MyCommon.EVENTTYPE> eventTable = new Dictionary<string, MyCommon.EVENTTYPE>
2416         {
2417             ["favorite"] = MyCommon.EVENTTYPE.Favorite,
2418             ["unfavorite"] = MyCommon.EVENTTYPE.Unfavorite,
2419             ["follow"] = MyCommon.EVENTTYPE.Follow,
2420             ["list_member_added"] = MyCommon.EVENTTYPE.ListMemberAdded,
2421             ["list_member_removed"] = MyCommon.EVENTTYPE.ListMemberRemoved,
2422             ["block"] = MyCommon.EVENTTYPE.Block,
2423             ["unblock"] = MyCommon.EVENTTYPE.Unblock,
2424             ["user_update"] = MyCommon.EVENTTYPE.UserUpdate,
2425             ["deleted"] = MyCommon.EVENTTYPE.Deleted,
2426             ["list_created"] = MyCommon.EVENTTYPE.ListCreated,
2427             ["list_destroyed"] = MyCommon.EVENTTYPE.ListDestroyed,
2428             ["list_updated"] = MyCommon.EVENTTYPE.ListUpdated,
2429             ["unfollow"] = MyCommon.EVENTTYPE.Unfollow,
2430             ["list_user_subscribed"] = MyCommon.EVENTTYPE.ListUserSubscribed,
2431             ["list_user_unsubscribed"] = MyCommon.EVENTTYPE.ListUserUnsubscribed,
2432             ["mute"] = MyCommon.EVENTTYPE.Mute,
2433             ["unmute"] = MyCommon.EVENTTYPE.Unmute,
2434             ["quoted_tweet"] = MyCommon.EVENTTYPE.QuotedTweet,
2435         };
2436
2437         public bool IsUserstreamDataReceived
2438         {
2439             get
2440             {
2441                 return DateTime.Now.Subtract(this._lastUserstreamDataReceived).TotalSeconds < 31;
2442             }
2443         }
2444
2445         private void userStream_StatusArrived(string line)
2446         {
2447             this._lastUserstreamDataReceived = DateTime.Now;
2448             if (string.IsNullOrEmpty(line)) return;
2449
2450             if (line.First() != '{' || line.Last() != '}')
2451             {
2452                 MyCommon.TraceOut("Invalid JSON (StatusArrived):" + Environment.NewLine + line);
2453                 return;
2454             }
2455
2456             var isDm = false;
2457
2458             try
2459             {
2460                 using (var jsonReader = JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(line), XmlDictionaryReaderQuotas.Max))
2461                 {
2462                     var xElm = XElement.Load(jsonReader);
2463                     if (xElm.Element("friends") != null)
2464                     {
2465                         Debug.WriteLine("friends");
2466                         return;
2467                     }
2468                     else if (xElm.Element("delete") != null)
2469                     {
2470                         Debug.WriteLine("delete");
2471                         Int64 id;
2472                         XElement idElm;
2473                         if ((idElm = xElm.Element("delete").Element("direct_message")?.Element("id")) != null)
2474                         {
2475                             id = 0;
2476                             long.TryParse(idElm.Value, out id);
2477
2478                             this.PostDeleted?.Invoke(this, new PostDeletedEventArgs(id));
2479                         }
2480                         else if ((idElm = xElm.Element("delete").Element("status")?.Element("id")) != null)
2481                         {
2482                             id = 0;
2483                             long.TryParse(idElm.Value, out id);
2484
2485                             this.PostDeleted?.Invoke(this, new PostDeletedEventArgs(id));
2486                         }
2487                         else
2488                         {
2489                             MyCommon.TraceOut("delete:" + line);
2490                             return;
2491                         }
2492                         for (int i = this.StoredEvent.Count - 1; i >= 0; i--)
2493                         {
2494                             var sEvt = this.StoredEvent[i];
2495                             if (sEvt.Id == id && (sEvt.Event == "favorite" || sEvt.Event == "unfavorite"))
2496                             {
2497                                 this.StoredEvent.RemoveAt(i);
2498                             }
2499                         }
2500                         return;
2501                     }
2502                     else if (xElm.Element("limit") != null)
2503                     {
2504                         Debug.WriteLine(line);
2505                         return;
2506                     }
2507                     else if (xElm.Element("event") != null)
2508                     {
2509                         Debug.WriteLine("event: " + xElm.Element("event").Value);
2510                         CreateEventFromJson(line);
2511                         return;
2512                     }
2513                     else if (xElm.Element("direct_message") != null)
2514                     {
2515                         Debug.WriteLine("direct_message");
2516                         isDm = true;
2517                     }
2518                     else if (xElm.Element("retweeted_status") != null)
2519                     {
2520                         var sourceUserId = xElm.XPathSelectElement("/user/id_str").Value;
2521                         var targetUserId = xElm.XPathSelectElement("/retweeted_status/user/id_str").Value;
2522
2523                         // 自分に関係しないリツイートの場合は無視する
2524                         var selfUserId = this.UserId.ToString();
2525                         if (sourceUserId == selfUserId || targetUserId == selfUserId)
2526                         {
2527                             // 公式 RT をイベントとしても扱う
2528                             var evt = CreateEventFromRetweet(xElm);
2529                             if (evt != null)
2530                             {
2531                                 this.StoredEvent.Insert(0, evt);
2532
2533                                 this.UserStreamEventReceived?.Invoke(this, new UserStreamEventReceivedEventArgs(evt));
2534                             }
2535                         }
2536
2537                         // 従来通り公式 RT の表示も行うため return しない
2538                     }
2539                     else if (xElm.Element("scrub_geo") != null)
2540                     {
2541                         try
2542                         {
2543                             TabInformations.GetInstance().ScrubGeoReserve(long.Parse(xElm.Element("scrub_geo").Element("user_id").Value),
2544                                                                         long.Parse(xElm.Element("scrub_geo").Element("up_to_status_id").Value));
2545                         }
2546                         catch(Exception)
2547                         {
2548                             MyCommon.TraceOut("scrub_geo:" + line);
2549                         }
2550                         return;
2551                     }
2552                 }
2553
2554                 if (isDm)
2555                 {
2556                     CreateDirectMessagesFromJson(line, MyCommon.WORKERTYPE.UserStream, false);
2557                 }
2558                 else
2559                 {
2560                     CreatePostsFromJson("[" + line + "]", MyCommon.WORKERTYPE.Timeline, null, false);
2561                 }
2562             }
2563             catch (WebApiException ex)
2564             {
2565                 MyCommon.TraceOut(ex);
2566                 return;
2567             }
2568             catch(NullReferenceException)
2569             {
2570                 MyCommon.TraceOut("NullRef StatusArrived: " + line);
2571             }
2572
2573             this.NewPostFromStream?.Invoke(this, EventArgs.Empty);
2574         }
2575
2576         /// <summary>
2577         /// UserStreamsから受信した公式RTをイベントに変換します
2578         /// </summary>
2579         private FormattedEvent CreateEventFromRetweet(XElement xElm)
2580         {
2581             return new FormattedEvent
2582             {
2583                 Eventtype = MyCommon.EVENTTYPE.Retweet,
2584                 Event = "retweet",
2585                 CreatedAt = MyCommon.DateTimeParse(xElm.XPathSelectElement("/created_at").Value),
2586                 IsMe = xElm.XPathSelectElement("/user/id_str").Value == this.UserId.ToString(),
2587                 Username = xElm.XPathSelectElement("/user/screen_name").Value,
2588                 Target = string.Format("@{0}:{1}", new[]
2589                 {
2590                     xElm.XPathSelectElement("/retweeted_status/user/screen_name").Value,
2591                     WebUtility.HtmlDecode(xElm.XPathSelectElement("/retweeted_status/text").Value),
2592                 }),
2593                 Id = long.Parse(xElm.XPathSelectElement("/retweeted_status/id_str").Value),
2594             };
2595         }
2596
2597         private void CreateEventFromJson(string content)
2598         {
2599             TwitterStreamEvent eventData = null;
2600             try
2601             {
2602                 eventData = TwitterStreamEvent.ParseJson(content);
2603             }
2604             catch(SerializationException ex)
2605             {
2606                 MyCommon.TraceOut(ex, "Event Serialize Exception!" + Environment.NewLine + content);
2607             }
2608             catch(Exception ex)
2609             {
2610                 MyCommon.TraceOut(ex, "Event Exception!" + Environment.NewLine + content);
2611             }
2612
2613             var evt = new FormattedEvent();
2614             evt.CreatedAt = MyCommon.DateTimeParse(eventData.CreatedAt);
2615             evt.Event = eventData.Event;
2616             evt.Username = eventData.Source.ScreenName;
2617             evt.IsMe = evt.Username.ToLowerInvariant().Equals(this.Username.ToLowerInvariant());
2618
2619             MyCommon.EVENTTYPE eventType;
2620             eventTable.TryGetValue(eventData.Event, out eventType);
2621             evt.Eventtype = eventType;
2622
2623             TwitterStreamEvent<TwitterStatus> tweetEvent;
2624
2625             switch (eventData.Event)
2626             {
2627                 case "access_revoked":
2628                 case "access_unrevoked":
2629                 case "user_delete":
2630                 case "user_suspend":
2631                     return;
2632                 case "follow":
2633                     if (eventData.Target.ScreenName.ToLowerInvariant().Equals(_uname))
2634                     {
2635                         if (!this.followerId.Contains(eventData.Source.Id)) this.followerId.Add(eventData.Source.Id);
2636                     }
2637                     else
2638                     {
2639                         return;    //Block後のUndoをすると、SourceとTargetが逆転したfollowイベントが帰ってくるため。
2640                     }
2641                     evt.Target = "";
2642                     break;
2643                 case "unfollow":
2644                     evt.Target = "@" + eventData.Target.ScreenName;
2645                     break;
2646                 case "favorited_retweet":
2647                 case "retweeted_retweet":
2648                     return;
2649                 case "favorite":
2650                 case "unfavorite":
2651                     tweetEvent = TwitterStreamEvent<TwitterStatus>.ParseJson(content);
2652                     evt.Target = "@" + tweetEvent.TargetObject.User.ScreenName + ":" + WebUtility.HtmlDecode(tweetEvent.TargetObject.Text);
2653                     evt.Id = tweetEvent.TargetObject.Id;
2654
2655                     if (SettingCommon.Instance.IsRemoveSameEvent)
2656                     {
2657                         if (this.StoredEvent.Any(ev => ev.Username == evt.Username && ev.Eventtype == evt.Eventtype && ev.Target == evt.Target))
2658                             return;
2659                     }
2660
2661                     var tabinfo = TabInformations.GetInstance();
2662
2663                     PostClass post;
2664                     var statusId = tweetEvent.TargetObject.Id;
2665                     if (!tabinfo.Posts.TryGetValue(statusId, out post))
2666                         break;
2667
2668                     if (eventData.Event == "favorite")
2669                     {
2670                         var favTab = tabinfo.GetTabByType(MyCommon.TabUsageType.Favorites);
2671                         if (!favTab.Contains(post.StatusId))
2672                             favTab.AddPostImmediately(post.StatusId, post.IsRead);
2673
2674                         if (tweetEvent.Source.Id == this.UserId)
2675                         {
2676                             post.IsFav = true;
2677                         }
2678                         else if (tweetEvent.Target.Id == this.UserId)
2679                         {
2680                             post.FavoritedCount++;
2681
2682                             if (SettingCommon.Instance.FavEventUnread)
2683                                 tabinfo.SetReadAllTab(post.StatusId, read: false);
2684                         }
2685                     }
2686                     else // unfavorite
2687                     {
2688                         if (tweetEvent.Source.Id == this.UserId)
2689                         {
2690                             post.IsFav = false;
2691                         }
2692                         else if (tweetEvent.Target.Id == this.UserId)
2693                         {
2694                             post.FavoritedCount = Math.Max(0, post.FavoritedCount - 1);
2695                         }
2696                     }
2697                     break;
2698                 case "quoted_tweet":
2699                     if (evt.IsMe) return;
2700
2701                     tweetEvent = TwitterStreamEvent<TwitterStatus>.ParseJson(content);
2702                     evt.Target = "@" + tweetEvent.TargetObject.User.ScreenName + ":" + WebUtility.HtmlDecode(tweetEvent.TargetObject.Text);
2703                     evt.Id = tweetEvent.TargetObject.Id;
2704
2705                     if (SettingCommon.Instance.IsRemoveSameEvent)
2706                     {
2707                         if (this.StoredEvent.Any(ev => ev.Username == evt.Username && ev.Eventtype == evt.Eventtype && ev.Target == evt.Target))
2708                             return;
2709                     }
2710                     break;
2711                 case "list_member_added":
2712                 case "list_member_removed":
2713                 case "list_created":
2714                 case "list_destroyed":
2715                 case "list_updated":
2716                 case "list_user_subscribed":
2717                 case "list_user_unsubscribed":
2718                     var listEvent = TwitterStreamEvent<TwitterList>.ParseJson(content);
2719                     evt.Target = listEvent.TargetObject.FullName;
2720                     break;
2721                 case "block":
2722                     if (!TabInformations.GetInstance().BlockIds.Contains(eventData.Target.Id)) TabInformations.GetInstance().BlockIds.Add(eventData.Target.Id);
2723                     evt.Target = "";
2724                     break;
2725                 case "unblock":
2726                     if (TabInformations.GetInstance().BlockIds.Contains(eventData.Target.Id)) TabInformations.GetInstance().BlockIds.Remove(eventData.Target.Id);
2727                     evt.Target = "";
2728                     break;
2729                 case "user_update":
2730                     evt.Target = "";
2731                     break;
2732                 
2733                 // Mute / Unmute
2734                 case "mute":
2735                     evt.Target = "@" + eventData.Target.ScreenName;
2736                     if (!TabInformations.GetInstance().MuteUserIds.Contains(eventData.Target.Id))
2737                     {
2738                         TabInformations.GetInstance().MuteUserIds.Add(eventData.Target.Id);
2739                     }
2740                     break;
2741                 case "unmute":
2742                     evt.Target = "@" + eventData.Target.ScreenName;
2743                     if (TabInformations.GetInstance().MuteUserIds.Contains(eventData.Target.Id))
2744                     {
2745                         TabInformations.GetInstance().MuteUserIds.Remove(eventData.Target.Id);
2746                     }
2747                     break;
2748
2749                 default:
2750                     MyCommon.TraceOut("Unknown Event:" + evt.Event + Environment.NewLine + content);
2751                     break;
2752             }
2753             this.StoredEvent.Insert(0, evt);
2754
2755             this.UserStreamEventReceived?.Invoke(this, new UserStreamEventReceivedEventArgs(evt));
2756         }
2757
2758         private void userStream_Started()
2759         {
2760             this.UserStreamStarted?.Invoke(this, EventArgs.Empty);
2761         }
2762
2763         private void userStream_Stopped()
2764         {
2765             this.UserStreamStopped?.Invoke(this, EventArgs.Empty);
2766         }
2767
2768         public bool UserStreamEnabled
2769         {
2770             get
2771             {
2772                 return userStream == null ? false : userStream.Enabled;
2773             }
2774         }
2775
2776         public void StartUserStream()
2777         {
2778             if (userStream != null)
2779             {
2780                 StopUserStream();
2781             }
2782             userStream = new TwitterUserstream(twCon);
2783             userStream.StatusArrived += userStream_StatusArrived;
2784             userStream.Started += userStream_Started;
2785             userStream.Stopped += userStream_Stopped;
2786             userStream.Start(this.AllAtReply, this.TrackWord);
2787         }
2788
2789         public void StopUserStream()
2790         {
2791             userStream?.Dispose();
2792             userStream = null;
2793             if (!MyCommon._endingFlag)
2794             {
2795                 this.UserStreamStopped?.Invoke(this, EventArgs.Empty);
2796             }
2797         }
2798
2799         public void ReconnectUserStream()
2800         {
2801             if (userStream != null)
2802             {
2803                 this.StartUserStream();
2804             }
2805         }
2806
2807         private class TwitterUserstream : IDisposable
2808         {
2809             public event Action<string> StatusArrived;
2810             public event Action Stopped;
2811             public event Action Started;
2812             private HttpTwitter twCon;
2813
2814             private Thread _streamThread;
2815             private bool _streamActive;
2816
2817             private bool _allAtreplies = false;
2818             private string _trackwords = "";
2819
2820             public TwitterUserstream(HttpTwitter twitterConnection)
2821             {
2822                 twCon = (HttpTwitter)twitterConnection.Clone();
2823             }
2824
2825             public void Start(bool allAtReplies, string trackwords)
2826             {
2827                 this.AllAtReplies = allAtReplies;
2828                 this.TrackWords = trackwords;
2829                 _streamActive = true;
2830                 if (_streamThread != null && _streamThread.IsAlive) return;
2831                 _streamThread = new Thread(UserStreamLoop);
2832                 _streamThread.Name = "UserStreamReceiver";
2833                 _streamThread.IsBackground = true;
2834                 _streamThread.Start();
2835             }
2836
2837             public bool Enabled
2838             {
2839                 get
2840                 {
2841                     return _streamActive;
2842                 }
2843             }
2844
2845             public bool AllAtReplies
2846             {
2847                 get
2848                 {
2849                     return _allAtreplies;
2850                 }
2851                 set
2852                 {
2853                     _allAtreplies = value;
2854                 }
2855             }
2856
2857             public string TrackWords
2858             {
2859                 get
2860                 {
2861                     return _trackwords;
2862                 }
2863                 set
2864                 {
2865                     _trackwords = value;
2866                 }
2867             }
2868
2869             private void UserStreamLoop()
2870             {
2871                 var sleepSec = 0;
2872                 do
2873                 {
2874                     Stream st = null;
2875                     StreamReader sr = null;
2876                     try
2877                     {
2878                         if (!MyCommon.IsNetworkAvailable())
2879                         {
2880                             sleepSec = 30;
2881                             continue;
2882                         }
2883
2884                         Started?.Invoke();
2885
2886                         var res = twCon.UserStream(ref st, _allAtreplies, _trackwords, Networking.GetUserAgentString());
2887
2888                         switch (res)
2889                         {
2890                             case HttpStatusCode.OK:
2891                                 Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
2892                                 break;
2893                             case HttpStatusCode.Unauthorized:
2894                                 Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
2895                                 sleepSec = 120;
2896                                 continue;
2897                         }
2898
2899                         if (st == null)
2900                         {
2901                             sleepSec = 30;
2902                             //MyCommon.TraceOut("Stop:stream is null")
2903                             continue;
2904                         }
2905
2906                         sr = new StreamReader(st);
2907
2908                         while (_streamActive && !sr.EndOfStream && Twitter.AccountState == MyCommon.ACCOUNT_STATE.Valid)
2909                         {
2910                             StatusArrived?.Invoke(sr.ReadLine());
2911                             //this.LastTime = Now;
2912                         }
2913
2914                         if (sr.EndOfStream || Twitter.AccountState == MyCommon.ACCOUNT_STATE.Invalid)
2915                         {
2916                             sleepSec = 30;
2917                             //MyCommon.TraceOut("Stop:EndOfStream")
2918                             continue;
2919                         }
2920                         break;
2921                     }
2922                     catch(WebException ex)
2923                     {
2924                         if (ex.Status == WebExceptionStatus.Timeout)
2925                         {
2926                             sleepSec = 30;                        //MyCommon.TraceOut("Stop:Timeout")
2927                         }
2928                         else if (ex.Response != null && (int)((HttpWebResponse)ex.Response).StatusCode == 420)
2929                         {
2930                             //MyCommon.TraceOut("Stop:Connection Limit")
2931                             break;
2932                         }
2933                         else
2934                         {
2935                             sleepSec = 30;
2936                             //MyCommon.TraceOut("Stop:WebException " + ex.Status.ToString())
2937                         }
2938                     }
2939                     catch(ThreadAbortException)
2940                     {
2941                         break;
2942                     }
2943                     catch(IOException)
2944                     {
2945                         sleepSec = 30;
2946                         //MyCommon.TraceOut("Stop:IOException with Active." + Environment.NewLine + ex.Message)
2947                     }
2948                     catch(ArgumentException ex)
2949                     {
2950                         //System.ArgumentException: ストリームを読み取れませんでした。
2951                         //サーバー側もしくは通信経路上で切断された場合?タイムアウト頻発後発生
2952                         sleepSec = 30;
2953                         MyCommon.TraceOut(ex, "Stop:ArgumentException");
2954                     }
2955                     catch(Exception ex)
2956                     {
2957                         MyCommon.TraceOut("Stop:Exception." + Environment.NewLine + ex.Message);
2958                         MyCommon.ExceptionOut(ex);
2959                         sleepSec = 30;
2960                     }
2961                     finally
2962                     {
2963                         if (_streamActive)
2964                         {
2965                             Stopped?.Invoke();
2966                         }
2967                         twCon.RequestAbort();
2968                         sr?.Close();
2969                         if (sleepSec > 0)
2970                         {
2971                             var ms = 0;
2972                             while (_streamActive && ms < sleepSec * 1000)
2973                             {
2974                                 Thread.Sleep(500);
2975                                 ms += 500;
2976                             }
2977                         }
2978                         sleepSec = 0;
2979                     }
2980                 } while (this._streamActive);
2981
2982                 if (_streamActive)
2983                 {
2984                     Stopped?.Invoke();
2985                 }
2986                 MyCommon.TraceOut("Stop:EndLoop");
2987             }
2988
2989 #region "IDisposable Support"
2990             private bool disposedValue; // 重複する呼び出しを検出するには
2991
2992             // IDisposable
2993             protected virtual void Dispose(bool disposing)
2994             {
2995                 if (!this.disposedValue)
2996                 {
2997                     if (disposing)
2998                     {
2999                         _streamActive = false;
3000                         if (_streamThread != null && _streamThread.IsAlive)
3001                         {
3002                             _streamThread.Abort();
3003                         }
3004                     }
3005                 }
3006                 this.disposedValue = true;
3007             }
3008
3009             //protected Overrides void Finalize()
3010             //{
3011             //    // このコードを変更しないでください。クリーンアップ コードを上の Dispose(bool disposing) に記述します。
3012             //    Dispose(false)
3013             //    MyBase.Finalize()
3014             //}
3015
3016             // このコードは、破棄可能なパターンを正しく実装できるように Visual Basic によって追加されました。
3017             public void Dispose()
3018             {
3019                 // このコードを変更しないでください。クリーンアップ コードを上の Dispose(bool disposing) に記述します。
3020                 Dispose(true);
3021                 GC.SuppressFinalize(this);
3022             }
3023 #endregion
3024
3025         }
3026 #endregion
3027
3028 #region "IDisposable Support"
3029         private bool disposedValue; // 重複する呼び出しを検出するには
3030
3031         // IDisposable
3032         protected virtual void Dispose(bool disposing)
3033         {
3034             if (!this.disposedValue)
3035             {
3036                 if (disposing)
3037                 {
3038                     this.StopUserStream();
3039                 }
3040             }
3041             this.disposedValue = true;
3042         }
3043
3044         //protected Overrides void Finalize()
3045         //{
3046         //    // このコードを変更しないでください。クリーンアップ コードを上の Dispose(bool disposing) に記述します。
3047         //    Dispose(false)
3048         //    MyBase.Finalize()
3049         //}
3050
3051         // このコードは、破棄可能なパターンを正しく実装できるように Visual Basic によって追加されました。
3052         public void Dispose()
3053         {
3054             // このコードを変更しないでください。クリーンアップ コードを上の Dispose(bool disposing) に記述します。
3055             Dispose(true);
3056             GC.SuppressFinalize(this);
3057         }
3058 #endregion
3059     }
3060
3061     public class PostDeletedEventArgs : EventArgs
3062     {
3063         public long StatusId { get; }
3064
3065         public PostDeletedEventArgs(long statusId)
3066         {
3067             this.StatusId = statusId;
3068         }
3069     }
3070
3071     public class UserStreamEventReceivedEventArgs : EventArgs
3072     {
3073         public Twitter.FormattedEvent EventData { get; }
3074
3075         public UserStreamEventReceivedEventArgs(Twitter.FormattedEvent eventData)
3076         {
3077             this.EventData = eventData;
3078         }
3079     }
3080 }