OSDN Git Service

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