OSDN Git Service

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