OSDN Git Service

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