OSDN Git Service

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