OSDN Git Service

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