OSDN Git Service

ITwitterCredentialとアクセス手段ごとの具象クラスを追加
[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 #nullable enable
29
30 using System;
31 using System.Collections.Generic;
32 using System.Diagnostics;
33 using System.IO;
34 using System.Linq;
35 using System.Net;
36 using System.Net.Http;
37 using System.Reflection;
38 using System.Runtime.CompilerServices;
39 using System.Text;
40 using System.Text.RegularExpressions;
41 using System.Threading;
42 using System.Threading.Tasks;
43 using System.Windows.Forms;
44 using OpenTween.Api;
45 using OpenTween.Api.DataModel;
46 using OpenTween.Api.GraphQL;
47 using OpenTween.Api.TwitterV2;
48 using OpenTween.Connection;
49 using OpenTween.Models;
50 using OpenTween.Setting;
51
52 namespace OpenTween
53 {
54     public class Twitter : IDisposable
55     {
56         #region Regexp from twitter-text-js
57
58         // The code in this region code block incorporates works covered by
59         // the following copyright and permission notices:
60         //
61         //   Copyright 2011 Twitter, Inc.
62         //
63         //   Licensed under the Apache License, Version 2.0 (the "License"); you
64         //   may not use this work except in compliance with the License. You
65         //   may obtain a copy of the License in the LICENSE file, or at:
66         //
67         //   http://www.apache.org/licenses/LICENSE-2.0
68         //
69         //   Unless required by applicable law or agreed to in writing, software
70         //   distributed under the License is distributed on an "AS IS" BASIS,
71         //   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
72         //   implied. See the License for the specific language governing
73         //   permissions and limitations under the License.
74
75         // Hashtag用正規表現
76         private const string LatinAccents = @"\u00c0-\u00d6\u00d8-\u00f6\u00f8-\u00ff\u0100-\u024f\u0253\u0254\u0256\u0257\u0259\u025b\u0263\u0268\u026f\u0272\u0289\u028b\u02bb\u1e00-\u1eff";
77         private const string NonLatinHashtagChars = @"\u0400-\u04ff\u0500-\u0527\u1100-\u11ff\u3130-\u3185\uA960-\uA97F\uAC00-\uD7AF\uD7B0-\uD7FF";
78         private const string CJHashtagCharacters = @"\u30A1-\u30FA\u30FC\u3005\uFF66-\uFF9F\uFF10-\uFF19\uFF21-\uFF3A\uFF41-\uFF5A\u3041-\u309A\u3400-\u4DBF\p{IsCJKUnifiedIdeographs}";
79         private const string HashtagBoundary = @"^|$|\s|「|」|。|\.|!";
80         private const string HashtagAlpha = $"[A-Za-z_{LatinAccents}{NonLatinHashtagChars}{CJHashtagCharacters}]";
81         private const string HashtagAlphanumeric = $"[A-Za-z0-9_{LatinAccents}{NonLatinHashtagChars}{CJHashtagCharacters}]";
82         private const string HashtagTerminator = $"[^A-Za-z0-9_{LatinAccents}{NonLatinHashtagChars}{CJHashtagCharacters}]";
83         public const string Hashtag = $"({HashtagBoundary})(#|#)({HashtagAlphanumeric}*{HashtagAlpha}{HashtagAlphanumeric}*)(?={HashtagTerminator}|{HashtagBoundary})";
84         // URL正規表現
85         private const string UrlValidPrecedingChars = @"(?:[^A-Za-z0-9@@$##\ufffe\ufeff\uffff\u202a-\u202e]|^)";
86         public const string UrlInvalidWithoutProtocolPrecedingChars = @"[-_./]$";
87         private const string UrlInvalidDomainChars = @"\!'#%&'\(\)*\+,\\\-\.\/:;<=>\?@\[\]\^_{|}~\$\u2000-\u200a\u0009-\u000d\u0020\u0085\u00a0\u1680\u180e\u2028\u2029\u202f\u205f\u3000\ufffe\ufeff\uffff\u202a-\u202e";
88         private const string UrlValidDomainChars = $@"[^{UrlInvalidDomainChars}]";
89         private const string UrlValidSubdomain = $@"(?:(?:{UrlValidDomainChars}(?:[_-]|{UrlValidDomainChars})*)?{UrlValidDomainChars}\.)";
90         private const string UrlValidDomainName = $@"(?:(?:{UrlValidDomainChars}(?:-|{UrlValidDomainChars})*)?{UrlValidDomainChars}\.)";
91         private const string UrlValidGTLD = @"(?:(?: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]|$))";
92         private const string UrlValidCCTLD = @"(?:(?: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]|$))";
93         private const string UrlValidPunycode = @"(?:xn--[0-9a-z]+)";
94         private const string UrlValidDomain = $@"(?<domain>{UrlValidSubdomain}*{UrlValidDomainName}(?:{UrlValidGTLD}|{UrlValidCCTLD})|{UrlValidPunycode})";
95         public const string UrlValidAsciiDomain = $@"(?:(?:[a-z0-9{LatinAccents}]+)\.)+(?:{UrlValidGTLD}|{UrlValidCCTLD}|{UrlValidPunycode})";
96         public const string UrlInvalidShortDomain = $"^{UrlValidDomainName}{UrlValidCCTLD}$";
97         private const string UrlValidPortNumber = @"[0-9]+";
98
99         private const string UrlValidGeneralPathChars = $@"[a-z0-9!*';:=+,.$/%#\[\]\-_~|&{LatinAccents}]";
100         private const string UrlBalanceParens = $@"(?:\({UrlValidGeneralPathChars}+\))";
101         private const string UrlValidPathEndingChars = $@"(?:[+\-a-z0-9=_#/{LatinAccents}]|{UrlBalanceParens})";
102         private const string Pth = "(?:" +
103             "(?:" +
104                 $"{UrlValidGeneralPathChars}*" +
105                 $"(?:{UrlBalanceParens}{UrlValidGeneralPathChars}*)*" +
106                 UrlValidPathEndingChars +
107                 $")|(?:@{UrlValidGeneralPathChars}+/)" +
108             ")";
109
110         private const string Qry = @"(?<query>\?[a-z0-9!?*'();:&=+$/%#\[\]\-_.,~|]*[a-z0-9_&=#/])?";
111         public const string RgUrl = $@"(?<before>{UrlValidPrecedingChars})" +
112                                     "(?<url>(?<protocol>https?://)?" +
113                                     $"(?<domain>{UrlValidDomain})" +
114                                     $"(?::{UrlValidPortNumber})?" +
115                                     $"(?<path>/{Pth}*)?" +
116                                     Qry +
117                                     ")";
118
119         #endregion
120
121         /// <summary>
122         /// Twitter API のステータスページのURL
123         /// </summary>
124         public const string ServiceAvailabilityStatusUrl = "https://api.twitterstat.us/";
125
126         /// <summary>
127         /// ツイートへのパーマリンクURLを判定する正規表現
128         /// </summary>
129         public static readonly Regex StatusUrlRegex = new(@"https?://([^.]+\.)?twitter\.com/(#!/)?(?<ScreenName>[a-zA-Z0-9_]+)/status(es)?/(?<StatusId>[0-9]+)(/photo)?", RegexOptions.IgnoreCase);
130
131         /// <summary>
132         /// attachment_url に指定可能な URL を判定する正規表現
133         /// </summary>
134         public static readonly Regex AttachmentUrlRegex = new(
135             @"https?://(
136    twitter\.com/[0-9A-Za-z_]+/status/[0-9]+
137  | mobile\.twitter\.com/[0-9A-Za-z_]+/status/[0-9]+
138  | twitter\.com/messages/compose\?recipient_id=[0-9]+(&.+)?
139 )$",
140             RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
141
142         /// <summary>
143         /// FavstarやaclogなどTwitter関連サービスのパーマリンクURLからステータスIDを抽出する正規表現
144         /// </summary>
145         public static readonly Regex ThirdPartyStatusUrlRegex = new(
146             @"https?://(?:[^.]+\.)?(?:
147   favstar\.fm/users/[a-zA-Z0-9_]+/status/       # Favstar
148 | favstar\.fm/t/                                # Favstar (short)
149 | aclog\.koba789\.com/i/                        # aclog
150 | frtrt\.net/solo_status\.php\?status=          # RtRT
151 )(?<StatusId>[0-9]+)",
152             RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
153
154         /// <summary>
155         /// DM送信かどうかを判定する正規表現
156         /// </summary>
157         public static readonly Regex DMSendTextRegex = new(@"^DM? +(?<id>[a-zA-Z0-9_]+) +(?<body>.*)", RegexOptions.IgnoreCase | RegexOptions.Singleline);
158
159         public TwitterApi Api { get; }
160
161         public TwitterConfiguration Configuration { get; private set; }
162
163         public TwitterTextConfiguration TextConfiguration { get; private set; }
164
165         public bool GetFollowersSuccess { get; private set; } = false;
166
167         public bool GetNoRetweetSuccess { get; private set; } = false;
168
169         private delegate void GetIconImageDelegate(PostClass post);
170
171         private readonly object lockObj = new();
172         private ISet<long> followerId = new HashSet<long>();
173         private long[] noRTId = Array.Empty<long>();
174
175         private readonly TwitterPostFactory postFactory;
176
177         private string? previousStatusId = null;
178
179         public Twitter(TwitterApi api)
180         {
181             this.postFactory = new(TabInformations.GetInstance());
182
183             this.Api = api;
184             this.Configuration = TwitterConfiguration.DefaultConfiguration();
185             this.TextConfiguration = TwitterTextConfiguration.DefaultConfiguration();
186         }
187
188         public TwitterApiAccessLevel AccessLevel
189             => MyCommon.TwitterApiInfo.AccessLevel;
190
191         protected void ResetApiStatus()
192             => MyCommon.TwitterApiInfo.Reset();
193
194         public void ClearAuthInfo()
195         {
196             Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
197             this.ResetApiStatus();
198         }
199
200         public void VerifyCredentials()
201         {
202             try
203             {
204                 this.VerifyCredentialsAsync().Wait();
205             }
206             catch (AggregateException ex) when (ex.InnerException is WebApiException)
207             {
208                 throw new WebApiException(ex.InnerException.Message, ex);
209             }
210         }
211
212         public async Task VerifyCredentialsAsync()
213         {
214             var user = await this.Api.AccountVerifyCredentials()
215                 .ConfigureAwait(false);
216
217             this.UpdateUserStats(user);
218         }
219
220         public void Initialize(string token, string tokenSecret, string username, long userId)
221         {
222             // OAuth認証
223             if (MyCommon.IsNullOrEmpty(token) || MyCommon.IsNullOrEmpty(tokenSecret) || MyCommon.IsNullOrEmpty(username))
224             {
225                 Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
226             }
227             this.ResetApiStatus();
228             this.Api.Initialize(token, tokenSecret, userId, username);
229         }
230
231         public void Initialize(ITwitterCredential credential, string username, long userId)
232         {
233             // OAuth認証
234             if (credential is TwitterCredentialNone)
235                 Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
236
237             this.ResetApiStatus();
238             this.Api.Initialize(credential, userId, username);
239         }
240
241         public async Task<PostClass?> PostStatus(PostStatusParams param)
242         {
243             this.CheckAccountState();
244
245             if (Twitter.DMSendTextRegex.IsMatch(param.Text))
246             {
247                 var mediaId = param.MediaIds != null && param.MediaIds.Any() ? param.MediaIds[0] : (long?)null;
248
249                 await this.SendDirectMessage(param.Text, mediaId)
250                     .ConfigureAwait(false);
251                 return null;
252             }
253
254             TwitterStatus status;
255
256             if (this.Api.AppToken.AuthType == APIAuthType.TwitterComCookie)
257             {
258                 var request = new CreateTweetRequest
259                 {
260                     TweetText = param.Text,
261                     InReplyToTweetId = param.InReplyToStatusId?.ToTwitterStatusId(),
262                     ExcludeReplyUserIds = param.ExcludeReplyUserIds.Select(x => x.ToString()).ToArray(),
263                     MediaIds = param.MediaIds.Select(x => x.ToString()).ToArray(),
264                     AttachmentUrl = param.AttachmentUrl,
265                 };
266
267                 status = await request.Send(this.Api.Connection)
268                     .ConfigureAwait(false);
269             }
270             else
271             {
272                 var response = await this.Api.StatusesUpdate(
273                         param.Text,
274                         param.InReplyToStatusId?.ToTwitterStatusId(),
275                         param.MediaIds,
276                         param.AutoPopulateReplyMetadata,
277                         param.ExcludeReplyUserIds,
278                         param.AttachmentUrl
279                     )
280                     .ConfigureAwait(false);
281
282                 status = await response.LoadJsonAsync()
283                     .ConfigureAwait(false);
284             }
285
286             this.UpdateUserStats(status.User);
287
288             if (status.IdStr == this.previousStatusId)
289                 throw new WebApiException("OK:Delaying?");
290
291             this.previousStatusId = status.IdStr;
292
293             // 投稿したものを返す
294             var post = this.CreatePostsFromStatusData(status);
295             if (this.ReadOwnPost) post.IsRead = true;
296             return post;
297         }
298
299         public async Task DeleteTweet(TwitterStatusId tweetId)
300         {
301             if (this.Api.AppToken.AuthType == APIAuthType.TwitterComCookie)
302             {
303                 var request = new DeleteTweetRequest
304                 {
305                     TweetId = tweetId,
306                 };
307                 await request.Send(this.Api.Connection);
308             }
309             else
310             {
311                 await this.Api.StatusesDestroy(tweetId)
312                     .IgnoreResponse();
313             }
314         }
315
316         public async Task<long> UploadMedia(IMediaItem item, string? mediaCategory = null)
317         {
318             this.CheckAccountState();
319
320             var mediaType = item.Extension switch
321             {
322                 ".png" => "image/png",
323                 ".jpg" => "image/jpeg",
324                 ".jpeg" => "image/jpeg",
325                 ".gif" => "image/gif",
326                 _ => "application/octet-stream",
327             };
328
329             var initResponse = await this.Api.MediaUploadInit(item.Size, mediaType, mediaCategory)
330                 .ConfigureAwait(false);
331
332             var initMedia = await initResponse.LoadJsonAsync()
333                 .ConfigureAwait(false);
334
335             var mediaId = initMedia.MediaId;
336
337             await this.Api.MediaUploadAppend(mediaId, 0, item)
338                 .ConfigureAwait(false);
339
340             var response = await this.Api.MediaUploadFinalize(mediaId)
341                 .ConfigureAwait(false);
342
343             var media = await response.LoadJsonAsync()
344                 .ConfigureAwait(false);
345
346             while (media.ProcessingInfo is TwitterUploadMediaResult.MediaProcessingInfo processingInfo)
347             {
348                 switch (processingInfo.State)
349                 {
350                     case "pending":
351                         break;
352                     case "in_progress":
353                         break;
354                     case "succeeded":
355                         goto succeeded;
356                     case "failed":
357                         throw new WebApiException($"Err:Upload failed ({processingInfo.Error?.Name})");
358                     default:
359                         throw new WebApiException($"Err:Invalid state ({processingInfo.State})");
360                 }
361
362                 await Task.Delay(TimeSpan.FromSeconds(processingInfo.CheckAfterSecs ?? 5))
363                     .ConfigureAwait(false);
364
365                 media = await this.Api.MediaUploadStatus(mediaId)
366                     .ConfigureAwait(false);
367             }
368
369             succeeded:
370             return media.MediaId;
371         }
372
373         public async Task SendDirectMessage(string postStr, long? mediaId = null)
374         {
375             this.CheckAccountState();
376             this.CheckAccessLevel(TwitterApiAccessLevel.ReadWriteAndDirectMessage);
377
378             var mc = Twitter.DMSendTextRegex.Match(postStr);
379
380             var body = mc.Groups["body"].Value;
381             var recipientName = mc.Groups["id"].Value;
382
383             var recipient = await this.GetUserInfo(recipientName)
384                 .ConfigureAwait(false);
385
386             var response = await this.Api.DirectMessagesEventsNew(recipient.Id, body, mediaId)
387                 .ConfigureAwait(false);
388
389             var messageEventSingle = await response.LoadJsonAsync()
390                 .ConfigureAwait(false);
391
392             await this.CreateDirectMessagesEventFromJson(messageEventSingle, read: true)
393                 .ConfigureAwait(false);
394         }
395
396         public async Task<PostClass?> PostRetweet(PostId id, bool read)
397         {
398             this.CheckAccountState();
399
400             // データ部分の生成
401             var post = TabInformations.GetInstance()[id];
402             if (post == null)
403                 throw new WebApiException("Err:Target isn't found.");
404
405             var target = post.RetweetedId ?? id;  // 再RTの場合は元発言をRT
406
407             if (this.Api.AppToken.AuthType == APIAuthType.TwitterComCookie)
408             {
409                 var request = new CreateRetweetRequest
410                 {
411                     TweetId = target.ToTwitterStatusId(),
412                 };
413                 await request.Send(this.Api.Connection).ConfigureAwait(false);
414                 return null;
415             }
416
417             var response = await this.Api.StatusesRetweet(target.ToTwitterStatusId())
418                 .ConfigureAwait(false);
419
420             var status = await response.LoadJsonAsync()
421                 .ConfigureAwait(false);
422
423             // 二重取得回避
424             lock (this.lockObj)
425             {
426                 var statusId = new TwitterStatusId(status.IdStr);
427                 if (TabInformations.GetInstance().ContainsKey(statusId))
428                     return null;
429             }
430
431             // Retweet判定
432             if (status.RetweetedStatus == null)
433                 throw new WebApiException("Invalid Json!");
434
435             // Retweetしたものを返す
436             return this.CreatePostsFromStatusData(status) with
437             {
438                 IsMe = true,
439                 IsRead = this.ReadOwnPost ? true : read,
440                 IsOwl = false,
441             };
442         }
443
444         public async Task DeleteRetweet(PostClass post)
445         {
446             if (post.RetweetedId == null)
447                 throw new ArgumentException("post is not retweeted status", nameof(post));
448
449             if (this.Api.AppToken.AuthType == APIAuthType.TwitterComCookie)
450             {
451                 var request = new DeleteRetweetRequest
452                 {
453                     SourceTweetId = post.RetweetedId.ToTwitterStatusId(),
454                 };
455                 await request.Send(this.Api.Connection).ConfigureAwait(false);
456             }
457             else
458             {
459                 await this.Api.StatusesDestroy(post.StatusId.ToTwitterStatusId())
460                     .IgnoreResponse();
461             }
462         }
463
464         public async Task<TwitterUser> GetUserInfo(string screenName)
465         {
466             if (this.Api.AppToken.AuthType == APIAuthType.TwitterComCookie)
467             {
468                 var request = new UserByScreenNameRequest
469                 {
470                     ScreenName = screenName,
471                 };
472                 var response = await request.Send(this.Api.Connection)
473                     .ConfigureAwait(false);
474
475                 return response.ToTwitterUser();
476             }
477             else
478             {
479                 var user = await this.Api.UsersShow(screenName)
480                     .ConfigureAwait(false);
481
482                 return user;
483             }
484         }
485
486         public string Username
487             => this.Api.CurrentScreenName;
488
489         public long UserId
490             => this.Api.CurrentUserId;
491
492         public static MyCommon.ACCOUNT_STATE AccountState { get; set; } = MyCommon.ACCOUNT_STATE.Valid;
493
494         public bool RestrictFavCheck { get; set; }
495
496         public bool ReadOwnPost { get; set; }
497
498         public int FollowersCount { get; private set; }
499
500         public int FriendsCount { get; private set; }
501
502         public int StatusesCount { get; private set; }
503
504         public string Location { get; private set; } = "";
505
506         public string Bio { get; private set; } = "";
507
508         /// <summary>ユーザーのフォロワー数などの情報を更新します</summary>
509         private void UpdateUserStats(TwitterUser self)
510         {
511             this.FollowersCount = self.FollowersCount;
512             this.FriendsCount = self.FriendsCount;
513             this.StatusesCount = self.StatusesCount;
514             this.Location = self.Location ?? "";
515             this.Bio = self.Description ?? "";
516         }
517
518         /// <summary>
519         /// 渡された取得件数がWORKERTYPEに応じた取得可能範囲に収まっているか検証する
520         /// </summary>
521         public static bool VerifyApiResultCount(MyCommon.WORKERTYPE type, int count)
522             => count >= 20 && count <= GetMaxApiResultCount(type);
523
524         /// <summary>
525         /// 渡された取得件数が更新時の取得可能範囲に収まっているか検証する
526         /// </summary>
527         public static bool VerifyMoreApiResultCount(int count)
528             => count >= 20 && count <= 200;
529
530         /// <summary>
531         /// 渡された取得件数が起動時の取得可能範囲に収まっているか検証する
532         /// </summary>
533         public static bool VerifyFirstApiResultCount(int count)
534             => count >= 20 && count <= 200;
535
536         /// <summary>
537         /// WORKERTYPEに応じた取得可能な最大件数を取得する
538         /// </summary>
539         public static int GetMaxApiResultCount(MyCommon.WORKERTYPE type)
540         {
541             // 参照: REST APIs - 各endpointのcountパラメータ
542             // https://dev.twitter.com/rest/public
543             return type switch
544             {
545                 MyCommon.WORKERTYPE.Timeline => 100,
546                 MyCommon.WORKERTYPE.Reply => 200,
547                 MyCommon.WORKERTYPE.UserTimeline => 200,
548                 MyCommon.WORKERTYPE.Favorites => 200,
549                 MyCommon.WORKERTYPE.List => 200, // 不明
550                 MyCommon.WORKERTYPE.PublicSearch => 100,
551                 _ => throw new InvalidOperationException("Invalid type: " + type),
552             };
553         }
554
555         /// <summary>
556         /// WORKERTYPEに応じた取得件数を取得する
557         /// </summary>
558         public static int GetApiResultCount(MyCommon.WORKERTYPE type, bool more, bool startup)
559         {
560             if (SettingManager.Instance.Common.UseAdditionalCount)
561             {
562                 switch (type)
563                 {
564                     case MyCommon.WORKERTYPE.Favorites:
565                         if (SettingManager.Instance.Common.FavoritesCountApi != 0)
566                             return SettingManager.Instance.Common.FavoritesCountApi;
567                         break;
568                     case MyCommon.WORKERTYPE.List:
569                         if (SettingManager.Instance.Common.ListCountApi != 0)
570                             return SettingManager.Instance.Common.ListCountApi;
571                         break;
572                     case MyCommon.WORKERTYPE.PublicSearch:
573                         if (SettingManager.Instance.Common.SearchCountApi != 0)
574                             return SettingManager.Instance.Common.SearchCountApi;
575                         break;
576                     case MyCommon.WORKERTYPE.UserTimeline:
577                         if (SettingManager.Instance.Common.UserTimelineCountApi != 0)
578                             return SettingManager.Instance.Common.UserTimelineCountApi;
579                         break;
580                 }
581                 if (more && SettingManager.Instance.Common.MoreCountApi != 0)
582                 {
583                     return Math.Min(SettingManager.Instance.Common.MoreCountApi, GetMaxApiResultCount(type));
584                 }
585                 if (startup && SettingManager.Instance.Common.FirstCountApi != 0 && type != MyCommon.WORKERTYPE.Reply)
586                 {
587                     return Math.Min(SettingManager.Instance.Common.FirstCountApi, GetMaxApiResultCount(type));
588                 }
589             }
590
591             // 上記に当てはまらない場合の共通処理
592             var count = SettingManager.Instance.Common.CountApi;
593
594             if (type == MyCommon.WORKERTYPE.Reply)
595                 count = SettingManager.Instance.Common.CountApiReply;
596
597             return Math.Min(count, GetMaxApiResultCount(type));
598         }
599
600         public async Task GetHomeTimelineApi(bool read, HomeTabModel tab, bool more, bool startup)
601         {
602             this.CheckAccountState();
603
604             var count = GetApiResultCount(MyCommon.WORKERTYPE.Timeline, more, startup);
605
606             TwitterStatus[] statuses;
607             if (SettingManager.Instance.Common.EnableTwitterV2Api)
608             {
609                 var request = new GetTimelineRequest(this.UserId)
610                 {
611                     MaxResults = count,
612                     UntilId = more ? tab.OldestId as TwitterStatusId : null,
613                 };
614
615                 var response = await request.Send(this.Api.Connection)
616                     .ConfigureAwait(false);
617
618                 if (response.Data == null || response.Data.Length == 0)
619                     return;
620
621                 var tweetIds = response.Data.Select(x => x.Id).ToList();
622
623                 statuses = await this.Api.StatusesLookup(tweetIds)
624                     .ConfigureAwait(false);
625             }
626             else
627             {
628                 var maxId = more ? tab.OldestId : null;
629
630                 statuses = await this.Api.StatusesHomeTimeline(count, maxId as TwitterStatusId)
631                     .ConfigureAwait(false);
632             }
633
634             var minimumId = this.CreatePostsFromJson(statuses, MyCommon.WORKERTYPE.Timeline, tab, read);
635             if (minimumId != null)
636                 tab.OldestId = minimumId;
637         }
638
639         public async Task GetMentionsTimelineApi(bool read, MentionsTabModel tab, bool more, bool startup)
640         {
641             this.CheckAccountState();
642
643             var count = GetApiResultCount(MyCommon.WORKERTYPE.Reply, more, startup);
644
645             TwitterStatus[] statuses;
646             if (more)
647             {
648                 statuses = await this.Api.StatusesMentionsTimeline(count, maxId: tab.OldestId as TwitterStatusId)
649                     .ConfigureAwait(false);
650             }
651             else
652             {
653                 statuses = await this.Api.StatusesMentionsTimeline(count)
654                     .ConfigureAwait(false);
655             }
656
657             var minimumId = this.CreatePostsFromJson(statuses, MyCommon.WORKERTYPE.Reply, tab, read);
658             if (minimumId != null)
659                 tab.OldestId = minimumId;
660         }
661
662         public async Task GetUserTimelineApi(bool read, UserTimelineTabModel tab, bool more)
663         {
664             this.CheckAccountState();
665
666             var count = GetApiResultCount(MyCommon.WORKERTYPE.UserTimeline, more, false);
667
668             TwitterStatus[] statuses;
669             if (this.Api.AppToken.AuthType == APIAuthType.TwitterComCookie)
670             {
671                 var userId = tab.UserId;
672                 if (MyCommon.IsNullOrEmpty(userId))
673                 {
674                     var user = await this.GetUserInfo(tab.ScreenName)
675                         .ConfigureAwait(false);
676
677                     userId = user.IdStr;
678                     tab.UserId = user.IdStr;
679                 }
680
681                 var request = new UserTweetsAndRepliesRequest(userId)
682                 {
683                     Count = count,
684                     Cursor = more ? tab.CursorBottom : tab.CursorTop,
685                 };
686                 var response = await request.Send(this.Api.Connection)
687                     .ConfigureAwait(false);
688
689                 statuses = response.Tweets
690                     .Where(x => !x.IsTombstone)
691                     .Select(x => x.ToTwitterStatus())
692                     .Where(x => x.User.IdStr == userId) // リプライツリーに含まれる他ユーザーのツイートを除外
693                     .ToArray();
694
695                 tab.CursorBottom = response.CursorBottom;
696
697                 if (!more)
698                     tab.CursorTop = response.CursorTop;
699             }
700             else
701             {
702                 if (more)
703                 {
704                     statuses = await this.Api.StatusesUserTimeline(tab.ScreenName, count, maxId: tab.OldestId as TwitterStatusId)
705                         .ConfigureAwait(false);
706                 }
707                 else
708                 {
709                     statuses = await this.Api.StatusesUserTimeline(tab.ScreenName, count)
710                         .ConfigureAwait(false);
711                 }
712             }
713
714             var minimumId = this.CreatePostsFromJson(statuses, MyCommon.WORKERTYPE.UserTimeline, tab, read);
715
716             if (minimumId != null)
717                 tab.OldestId = minimumId;
718         }
719
720         public async Task<PostClass> GetStatusApi(bool read, TwitterStatusId id)
721         {
722             this.CheckAccountState();
723
724             TwitterStatus status;
725             if (this.Api.AppToken.AuthType == APIAuthType.TwitterComCookie)
726             {
727                 var request = new TweetDetailRequest
728                 {
729                     FocalTweetId = id,
730                 };
731                 var tweets = await request.Send(this.Api.Connection).ConfigureAwait(false);
732                 status = tweets.Select(x => x.ToTwitterStatus())
733                     .Where(x => x.IdStr == id.Id)
734                     .FirstOrDefault() ?? throw new WebApiException("Empty result set");
735             }
736             else
737             {
738                 status = await this.Api.StatusesShow(id)
739                     .ConfigureAwait(false);
740             }
741
742             var item = this.CreatePostsFromStatusData(status);
743
744             item.IsRead = read;
745             if (item.IsMe && !read && this.ReadOwnPost) item.IsRead = true;
746
747             return item;
748         }
749
750         public async Task GetStatusApi(bool read, TwitterStatusId id, TabModel tab)
751         {
752             var post = await this.GetStatusApi(read, id)
753                 .ConfigureAwait(false);
754
755             // 非同期アイコン取得&StatusDictionaryに追加
756             if (tab != null && tab.IsInnerStorageTabType)
757                 tab.AddPostQueue(post);
758             else
759                 TabInformations.GetInstance().AddPost(post);
760         }
761
762         private PostClass CreatePostsFromStatusData(TwitterStatus status)
763             => this.CreatePostsFromStatusData(status, favTweet: false);
764
765         private PostClass CreatePostsFromStatusData(TwitterStatus status, bool favTweet)
766             => this.postFactory.CreateFromStatus(status, this.UserId, this.followerId, favTweet);
767
768         private PostId? CreatePostsFromJson(TwitterStatus[] items, MyCommon.WORKERTYPE gType, TabModel? tab, bool read)
769         {
770             PostId? minimumId = null;
771
772             var posts = items.Select(x => this.CreatePostsFromStatusData(x)).ToArray();
773
774             TwitterPostFactory.AdjustSortKeyForPromotedPost(posts);
775
776             foreach (var post in posts)
777             {
778                 if (!post.IsPromoted)
779                 {
780                     if (minimumId == null || minimumId > post.StatusId)
781                         minimumId = post.StatusId;
782                 }
783
784                 // 二重取得回避
785                 lock (this.lockObj)
786                 {
787                     var id = post.StatusId;
788                     if (tab == null)
789                     {
790                         if (TabInformations.GetInstance().ContainsKey(id)) continue;
791                     }
792                     else
793                     {
794                         if (tab.Contains(id)) continue;
795                     }
796                 }
797
798                 // RT禁止ユーザーによるもの
799                 if (gType != MyCommon.WORKERTYPE.UserTimeline &&
800                     post.RetweetedByUserId != null && this.noRTId.Contains(post.RetweetedByUserId.Value)) continue;
801
802                 post.IsRead = read;
803                 if (post.IsMe && !read && this.ReadOwnPost) post.IsRead = true;
804
805                 if (tab != null && tab.IsInnerStorageTabType)
806                     tab.AddPostQueue(post);
807                 else
808                     TabInformations.GetInstance().AddPost(post);
809             }
810
811             return minimumId;
812         }
813
814         private PostId? CreatePostsFromSearchJson(TwitterStatus[] statuses, PublicSearchTabModel tab, bool read, bool more)
815         {
816             PostId? minimumId = null;
817
818             var posts = statuses.Select(x => this.CreatePostsFromStatusData(x)).ToArray();
819
820             TwitterPostFactory.AdjustSortKeyForPromotedPost(posts);
821
822             foreach (var post in posts)
823             {
824                 if (!post.IsPromoted)
825                 {
826                     if (minimumId == null || minimumId > post.StatusId)
827                         minimumId = post.StatusId;
828
829                     if (!more && (tab.SinceId == null || post.StatusId > tab.SinceId))
830                         tab.SinceId = post.StatusId;
831                 }
832
833                 // 二重取得回避
834                 lock (this.lockObj)
835                 {
836                     if (tab.Contains(post.StatusId))
837                         continue;
838                 }
839
840                 post.IsRead = read;
841                 if ((post.IsMe && !read) && this.ReadOwnPost) post.IsRead = true;
842
843                 tab.AddPostQueue(post);
844             }
845
846             return minimumId;
847         }
848
849         private long? CreateFavoritePostsFromJson(TwitterStatus[] items, bool read)
850         {
851             var favTab = TabInformations.GetInstance().FavoriteTab;
852             long? minimumId = null;
853
854             foreach (var status in items)
855             {
856                 if (minimumId == null || minimumId.Value > status.Id)
857                     minimumId = status.Id;
858
859                 // 二重取得回避
860                 lock (this.lockObj)
861                 {
862                     if (favTab.Contains(new TwitterStatusId(status.IdStr)))
863                         continue;
864                 }
865
866                 var post = this.CreatePostsFromStatusData(status, true);
867
868                 post.IsRead = read;
869
870                 TabInformations.GetInstance().AddPost(post);
871             }
872
873             return minimumId;
874         }
875
876         public async Task GetListStatus(bool read, ListTimelineTabModel tab, bool more, bool startup)
877         {
878             var count = GetApiResultCount(MyCommon.WORKERTYPE.List, more, startup);
879
880             TwitterStatus[] statuses;
881             if (this.Api.AppToken.AuthType == APIAuthType.TwitterComCookie)
882             {
883                 var request = new ListLatestTweetsTimelineRequest(tab.ListInfo.Id.ToString())
884                 {
885                     Count = count,
886                     Cursor = more ? tab.CursorBottom : tab.CursorTop,
887                 };
888                 var response = await request.Send(this.Api.Connection)
889                     .ConfigureAwait(false);
890
891                 var convertedStatuses = response.Tweets
892                     .Where(x => !x.IsTombstone)
893                     .Select(x => x.ToTwitterStatus());
894
895                 if (!SettingManager.Instance.Common.IsListsIncludeRts)
896                     convertedStatuses = convertedStatuses.Where(x => x.RetweetedStatus == null);
897
898                 statuses = convertedStatuses.ToArray();
899                 tab.CursorBottom = response.CursorBottom;
900
901                 if (!more)
902                     tab.CursorTop = response.CursorTop;
903             }
904             else if (more)
905             {
906                 statuses = await this.Api.ListsStatuses(tab.ListInfo.Id, count, maxId: tab.OldestId as TwitterStatusId, includeRTs: SettingManager.Instance.Common.IsListsIncludeRts)
907                     .ConfigureAwait(false);
908             }
909             else
910             {
911                 statuses = await this.Api.ListsStatuses(tab.ListInfo.Id, count, includeRTs: SettingManager.Instance.Common.IsListsIncludeRts)
912                     .ConfigureAwait(false);
913             }
914
915             var minimumId = this.CreatePostsFromJson(statuses, MyCommon.WORKERTYPE.List, tab, read);
916
917             if (minimumId != null)
918                 tab.OldestId = minimumId;
919         }
920
921         /// <summary>
922         /// startStatusId からリプライ先の発言を辿る。発言は posts 以外からは検索しない。
923         /// </summary>
924         /// <returns>posts の中から検索されたリプライチェインの末端</returns>
925         internal static PostClass FindTopOfReplyChain(IDictionary<PostId, PostClass> posts, PostId startStatusId)
926         {
927             if (!posts.ContainsKey(startStatusId))
928                 throw new ArgumentException("startStatusId (" + startStatusId.Id + ") が posts の中から見つかりませんでした。", nameof(startStatusId));
929
930             var nextPost = posts[startStatusId];
931             while (nextPost.InReplyToStatusId != null)
932             {
933                 if (!posts.ContainsKey(nextPost.InReplyToStatusId))
934                     break;
935                 nextPost = posts[nextPost.InReplyToStatusId];
936             }
937
938             return nextPost;
939         }
940
941         public async Task GetRelatedResult(bool read, RelatedPostsTabModel tab)
942         {
943             var targetPost = tab.TargetPost;
944
945             if (targetPost.RetweetedId != null)
946             {
947                 var originalPost = targetPost with
948                 {
949                     StatusId = targetPost.RetweetedId,
950                     RetweetedId = null,
951                     RetweetedBy = null,
952                 };
953                 targetPost = originalPost;
954             }
955
956             var relPosts = new Dictionary<PostId, PostClass>();
957             if (targetPost.TextFromApi.Contains("@") && targetPost.InReplyToStatusId == null)
958             {
959                 // 検索結果対応
960                 var p = TabInformations.GetInstance()[targetPost.StatusId];
961                 if (p != null && p.InReplyToStatusId != null)
962                 {
963                     targetPost = p;
964                 }
965                 else
966                 {
967                     p = await this.GetStatusApi(read, targetPost.StatusId.ToTwitterStatusId())
968                         .ConfigureAwait(false);
969                     targetPost = p;
970                 }
971             }
972             relPosts.Add(targetPost.StatusId, targetPost);
973
974             Exception? lastException = null;
975
976             // in_reply_to_status_id を使用してリプライチェインを辿る
977             var nextPost = FindTopOfReplyChain(relPosts, targetPost.StatusId);
978             var loopCount = 1;
979             while (nextPost.InReplyToStatusId != null && loopCount++ <= 20)
980             {
981                 var inReplyToId = nextPost.InReplyToStatusId;
982
983                 var inReplyToPost = TabInformations.GetInstance()[inReplyToId];
984                 if (inReplyToPost == null)
985                 {
986                     try
987                     {
988                         inReplyToPost = await this.GetStatusApi(read, inReplyToId.ToTwitterStatusId())
989                             .ConfigureAwait(false);
990                     }
991                     catch (WebApiException ex)
992                     {
993                         lastException = ex;
994                         break;
995                     }
996                 }
997
998                 relPosts.Add(inReplyToPost.StatusId, inReplyToPost);
999
1000                 nextPost = FindTopOfReplyChain(relPosts, nextPost.StatusId);
1001             }
1002
1003             // MRTとかに対応のためツイート内にあるツイートを指すURLを取り込む
1004             var text = targetPost.Text;
1005             var ma = Twitter.StatusUrlRegex.Matches(text).Cast<Match>()
1006                 .Concat(Twitter.ThirdPartyStatusUrlRegex.Matches(text).Cast<Match>());
1007             foreach (var match in ma)
1008             {
1009                 var statusId = new TwitterStatusId(match.Groups["StatusId"].Value);
1010                 if (!relPosts.ContainsKey(statusId))
1011                 {
1012                     var p = TabInformations.GetInstance()[statusId];
1013                     if (p == null)
1014                     {
1015                         try
1016                         {
1017                             p = await this.GetStatusApi(read, statusId)
1018                                 .ConfigureAwait(false);
1019                         }
1020                         catch (WebApiException ex)
1021                         {
1022                             lastException = ex;
1023                             break;
1024                         }
1025                     }
1026
1027                     if (p != null)
1028                         relPosts.Add(p.StatusId, p);
1029                 }
1030             }
1031
1032             try
1033             {
1034                 var firstPost = nextPost;
1035                 var posts = await this.GetConversationPosts(firstPost, targetPost)
1036                     .ConfigureAwait(false);
1037
1038                 foreach (var post in posts.OrderBy(x => x.StatusId))
1039                 {
1040                     if (relPosts.ContainsKey(post.StatusId))
1041                         continue;
1042
1043                     // リプライチェーンが繋がらないツイートは除外
1044                     if (post.InReplyToStatusId == null || !relPosts.ContainsKey(post.InReplyToStatusId))
1045                         continue;
1046
1047                     relPosts.Add(post.StatusId, post);
1048                 }
1049             }
1050             catch (WebException ex)
1051             {
1052                 lastException = ex;
1053             }
1054
1055             relPosts.Values.ToList().ForEach(p =>
1056             {
1057                 var post = p with { };
1058                 if (post.IsMe && !read && this.ReadOwnPost)
1059                     post.IsRead = true;
1060                 else
1061                     post.IsRead = read;
1062
1063                 tab.AddPostQueue(post);
1064             });
1065
1066             if (lastException != null)
1067                 throw new WebApiException(lastException.Message, lastException);
1068         }
1069
1070         private async Task<PostClass[]> GetConversationPosts(PostClass firstPost, PostClass targetPost)
1071         {
1072             var conversationId = firstPost.StatusId;
1073             var query = $"conversation_id:{conversationId.Id}";
1074
1075             if (targetPost.InReplyToUser != null && targetPost.InReplyToUser != targetPost.ScreenName)
1076                 query += $" (from:{targetPost.ScreenName} to:{targetPost.InReplyToUser}) OR (from:{targetPost.InReplyToUser} to:{targetPost.ScreenName})";
1077             else
1078                 query += $" from:{targetPost.ScreenName} to:{targetPost.ScreenName}";
1079
1080             var statuses = await this.Api.SearchTweets(query, count: 100)
1081                 .ConfigureAwait(false);
1082
1083             return statuses.Statuses.Select(x => this.CreatePostsFromStatusData(x)).ToArray();
1084         }
1085
1086         public async Task GetSearch(bool read, PublicSearchTabModel tab, bool more)
1087         {
1088             var count = GetApiResultCount(MyCommon.WORKERTYPE.PublicSearch, more, false);
1089
1090             TwitterStatus[] statuses;
1091             if (this.Api.AppToken.AuthType == APIAuthType.TwitterComCookie)
1092             {
1093                 var request = new SearchTimelineRequest(tab.SearchWords)
1094                 {
1095                     Count = count,
1096                     Cursor = more ? tab.CursorBottom : tab.CursorTop,
1097                 };
1098                 var response = await request.Send(this.Api.Connection)
1099                     .ConfigureAwait(false);
1100
1101                 statuses = response.Tweets
1102                     .Where(x => !x.IsTombstone)
1103                     .Select(x => x.ToTwitterStatus())
1104                     .ToArray();
1105
1106                 tab.CursorBottom = response.CursorBottom;
1107
1108                 if (!more)
1109                     tab.CursorTop = response.CursorTop;
1110             }
1111             else
1112             {
1113                 TwitterStatusId? maxId = null;
1114                 TwitterStatusId? sinceId = null;
1115                 if (more)
1116                 {
1117                     maxId = tab.OldestId as TwitterStatusId;
1118                 }
1119                 else
1120                 {
1121                     sinceId = tab.SinceId as TwitterStatusId;
1122                 }
1123
1124                 var searchResult = await this.Api.SearchTweets(tab.SearchWords, tab.SearchLang, count, maxId, sinceId)
1125                     .ConfigureAwait(false);
1126
1127                 statuses = searchResult.Statuses;
1128             }
1129
1130             if (!TabInformations.GetInstance().ContainsTab(tab))
1131                 return;
1132
1133             var minimumId = this.CreatePostsFromSearchJson(statuses, tab, read, more);
1134
1135             if (minimumId != null)
1136                 tab.OldestId = minimumId;
1137         }
1138
1139         public async Task GetDirectMessageEvents(bool read, DirectMessagesTabModel dmTab, bool backward)
1140         {
1141             this.CheckAccountState();
1142             this.CheckAccessLevel(TwitterApiAccessLevel.ReadWriteAndDirectMessage);
1143
1144             var count = 50;
1145
1146             TwitterMessageEventList eventList;
1147             if (backward)
1148             {
1149                 eventList = await this.Api.DirectMessagesEventsList(count, dmTab.NextCursor)
1150                     .ConfigureAwait(false);
1151             }
1152             else
1153             {
1154                 eventList = await this.Api.DirectMessagesEventsList(count)
1155                     .ConfigureAwait(false);
1156             }
1157
1158             dmTab.NextCursor = eventList.NextCursor;
1159
1160             await this.CreateDirectMessagesEventFromJson(eventList, read)
1161                 .ConfigureAwait(false);
1162         }
1163
1164         private async Task CreateDirectMessagesEventFromJson(TwitterMessageEventSingle eventSingle, bool read)
1165         {
1166             var eventList = new TwitterMessageEventList
1167             {
1168                 Apps = new Dictionary<string, TwitterMessageEventList.App>(),
1169                 Events = new[] { eventSingle.Event },
1170             };
1171
1172             await this.CreateDirectMessagesEventFromJson(eventList, read)
1173                 .ConfigureAwait(false);
1174         }
1175
1176         private async Task CreateDirectMessagesEventFromJson(TwitterMessageEventList eventList, bool read)
1177         {
1178             var events = eventList.Events
1179                 .Where(x => x.Type == "message_create")
1180                 .ToArray();
1181
1182             if (events.Length == 0)
1183                 return;
1184
1185             var userIds = Enumerable.Concat(
1186                 events.Select(x => x.MessageCreate.SenderId),
1187                 events.Select(x => x.MessageCreate.Target.RecipientId)
1188             ).Distinct().ToArray();
1189
1190             var users = (await this.Api.UsersLookup(userIds).ConfigureAwait(false))
1191                 .ToDictionary(x => x.IdStr);
1192
1193             var apps = eventList.Apps ?? new Dictionary<string, TwitterMessageEventList.App>();
1194
1195             this.CreateDirectMessagesEventFromJson(events, users, apps, read);
1196         }
1197
1198         private void CreateDirectMessagesEventFromJson(
1199             IEnumerable<TwitterMessageEvent> events,
1200             IReadOnlyDictionary<string, TwitterUser> users,
1201             IReadOnlyDictionary<string, TwitterMessageEventList.App> apps,
1202             bool read)
1203         {
1204             var dmTab = TabInformations.GetInstance().DirectMessageTab;
1205
1206             foreach (var eventItem in events)
1207             {
1208                 var post = this.postFactory.CreateFromDirectMessageEvent(eventItem, users, apps, this.UserId);
1209
1210                 post.IsRead = read;
1211                 if (post.IsMe && !read && this.ReadOwnPost)
1212                     post.IsRead = true;
1213
1214                 dmTab.AddPostQueue(post);
1215             }
1216         }
1217
1218         public async Task GetFavoritesApi(bool read, FavoritesTabModel tab, bool backward)
1219         {
1220             this.CheckAccountState();
1221
1222             var count = GetApiResultCount(MyCommon.WORKERTYPE.Favorites, backward, false);
1223
1224             TwitterStatus[] statuses;
1225             if (backward)
1226             {
1227                 statuses = await this.Api.FavoritesList(count, maxId: tab.OldestId)
1228                     .ConfigureAwait(false);
1229             }
1230             else
1231             {
1232                 statuses = await this.Api.FavoritesList(count)
1233                     .ConfigureAwait(false);
1234             }
1235
1236             var minimumId = this.CreateFavoritePostsFromJson(statuses, read);
1237
1238             if (minimumId != null)
1239                 tab.OldestId = minimumId.Value;
1240         }
1241
1242         /// <summary>
1243         /// フォロワーIDを更新します
1244         /// </summary>
1245         /// <exception cref="WebApiException"/>
1246         public async Task RefreshFollowerIds()
1247         {
1248             if (MyCommon.EndingFlag) return;
1249
1250             var cursor = -1L;
1251             var newFollowerIds = Enumerable.Empty<long>();
1252             do
1253             {
1254                 var ret = await this.Api.FollowersIds(cursor)
1255                     .ConfigureAwait(false);
1256
1257                 if (ret.Ids == null)
1258                     throw new WebApiException("ret.ids == null");
1259
1260                 newFollowerIds = newFollowerIds.Concat(ret.Ids);
1261                 cursor = ret.NextCursor;
1262             }
1263             while (cursor != 0);
1264
1265             this.followerId = newFollowerIds.ToHashSet();
1266             TabInformations.GetInstance().RefreshOwl(this.followerId);
1267
1268             this.GetFollowersSuccess = true;
1269         }
1270
1271         /// <summary>
1272         /// RT 非表示ユーザーを更新します
1273         /// </summary>
1274         /// <exception cref="WebApiException"/>
1275         public async Task RefreshNoRetweetIds()
1276         {
1277             if (MyCommon.EndingFlag) return;
1278
1279             this.noRTId = await this.Api.NoRetweetIds()
1280                 .ConfigureAwait(false);
1281
1282             this.GetNoRetweetSuccess = true;
1283         }
1284
1285         /// <summary>
1286         /// t.co の文字列長などの設定情報を更新します
1287         /// </summary>
1288         /// <exception cref="WebApiException"/>
1289         public async Task RefreshConfiguration()
1290         {
1291             this.Configuration = await this.Api.Configuration()
1292                 .ConfigureAwait(false);
1293
1294             // TextConfiguration 相当の JSON を得る API が存在しないため、TransformedURLLength のみ help/configuration.json に合わせて更新する
1295             this.TextConfiguration.TransformedURLLength = this.Configuration.ShortUrlLengthHttps;
1296         }
1297
1298         public async Task GetListsApi()
1299         {
1300             this.CheckAccountState();
1301
1302             var ownedLists = await TwitterLists.GetAllItemsAsync(x =>
1303                 this.Api.ListsOwnerships(this.Username, cursor: x, count: 1000))
1304                     .ConfigureAwait(false);
1305
1306             var subscribedLists = await TwitterLists.GetAllItemsAsync(x =>
1307                 this.Api.ListsSubscriptions(this.Username, cursor: x, count: 1000))
1308                     .ConfigureAwait(false);
1309
1310             TabInformations.GetInstance().SubscribableLists = Enumerable.Concat(ownedLists, subscribedLists)
1311                 .Select(x => new ListElement(x, this))
1312                 .ToList();
1313         }
1314
1315         public async Task DeleteList(long listId)
1316         {
1317             await this.Api.ListsDestroy(listId)
1318                 .IgnoreResponse()
1319                 .ConfigureAwait(false);
1320
1321             var tabinfo = TabInformations.GetInstance();
1322
1323             tabinfo.SubscribableLists = tabinfo.SubscribableLists
1324                 .Where(x => x.Id != listId)
1325                 .ToList();
1326         }
1327
1328         public async Task<ListElement> EditList(long listId, string new_name, bool isPrivate, string description)
1329         {
1330             var response = await this.Api.ListsUpdate(listId, new_name, description, isPrivate)
1331                 .ConfigureAwait(false);
1332
1333             var list = await response.LoadJsonAsync()
1334                 .ConfigureAwait(false);
1335
1336             return new ListElement(list, this);
1337         }
1338
1339         public async Task<long> GetListMembers(long listId, List<UserInfo> lists, long cursor)
1340         {
1341             this.CheckAccountState();
1342
1343             var users = await this.Api.ListsMembers(listId, cursor)
1344                 .ConfigureAwait(false);
1345
1346             Array.ForEach(users.Users, u => lists.Add(new UserInfo(u)));
1347
1348             return users.NextCursor;
1349         }
1350
1351         public async Task CreateListApi(string listName, bool isPrivate, string description)
1352         {
1353             this.CheckAccountState();
1354
1355             var response = await this.Api.ListsCreate(listName, description, isPrivate)
1356                 .ConfigureAwait(false);
1357
1358             var list = await response.LoadJsonAsync()
1359                 .ConfigureAwait(false);
1360
1361             TabInformations.GetInstance().SubscribableLists.Add(new ListElement(list, this));
1362         }
1363
1364         public async Task<bool> ContainsUserAtList(long listId, string user)
1365         {
1366             this.CheckAccountState();
1367
1368             try
1369             {
1370                 await this.Api.ListsMembersShow(listId, user)
1371                     .ConfigureAwait(false);
1372
1373                 return true;
1374             }
1375             catch (TwitterApiException ex)
1376                 when (ex.Errors.Any(x => x.Code == TwitterErrorCode.NotFound))
1377             {
1378                 return false;
1379             }
1380         }
1381
1382         public async Task<TwitterApiStatus?> GetInfoApi()
1383         {
1384             if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return null;
1385
1386             if (MyCommon.EndingFlag) return null;
1387
1388             var limits = await this.Api.ApplicationRateLimitStatus()
1389                 .ConfigureAwait(false);
1390
1391             MyCommon.TwitterApiInfo.UpdateFromJson(limits);
1392
1393             return MyCommon.TwitterApiInfo;
1394         }
1395
1396         /// <summary>
1397         /// ブロック中のユーザーを更新します
1398         /// </summary>
1399         /// <exception cref="WebApiException"/>
1400         public async Task RefreshBlockIds()
1401         {
1402             if (MyCommon.EndingFlag) return;
1403
1404             var cursor = -1L;
1405             var newBlockIds = Enumerable.Empty<long>();
1406             do
1407             {
1408                 var ret = await this.Api.BlocksIds(cursor)
1409                     .ConfigureAwait(false);
1410
1411                 newBlockIds = newBlockIds.Concat(ret.Ids);
1412                 cursor = ret.NextCursor;
1413             }
1414             while (cursor != 0);
1415
1416             var blockIdsSet = newBlockIds.ToHashSet();
1417             blockIdsSet.Remove(this.UserId); // 元のソースにあったので一応残しておく
1418
1419             TabInformations.GetInstance().BlockIds = blockIdsSet;
1420         }
1421
1422         /// <summary>
1423         /// ミュート中のユーザーIDを更新します
1424         /// </summary>
1425         /// <exception cref="WebApiException"/>
1426         public async Task RefreshMuteUserIdsAsync()
1427         {
1428             if (MyCommon.EndingFlag) return;
1429
1430             var ids = await TwitterIds.GetAllItemsAsync(x => this.Api.MutesUsersIds(x))
1431                 .ConfigureAwait(false);
1432
1433             TabInformations.GetInstance().MuteUserIds = ids.ToHashSet();
1434         }
1435
1436         public string[] GetHashList()
1437             => this.postFactory.GetReceivedHashtags();
1438
1439         private void CheckAccountState()
1440         {
1441             if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
1442                 throw new WebApiException("Auth error. Check your account");
1443         }
1444
1445         private void CheckAccessLevel(TwitterApiAccessLevel accessLevelFlags)
1446         {
1447             if (!this.AccessLevel.HasFlag(accessLevelFlags))
1448                 throw new WebApiException("Auth Err:try to re-authorization.");
1449         }
1450
1451         public int GetTextLengthRemain(string postText)
1452         {
1453             var matchDm = Twitter.DMSendTextRegex.Match(postText);
1454             if (matchDm.Success)
1455                 return this.GetTextLengthRemainDM(matchDm.Groups["body"].Value);
1456
1457             return this.GetTextLengthRemainWeighted(postText);
1458         }
1459
1460         private int GetTextLengthRemainDM(string postText)
1461         {
1462             var textLength = 0;
1463
1464             var pos = 0;
1465             while (pos < postText.Length)
1466             {
1467                 textLength++;
1468
1469                 if (char.IsSurrogatePair(postText, pos))
1470                     pos += 2; // サロゲートペアの場合は2文字分進める
1471                 else
1472                     pos++;
1473             }
1474
1475             var urls = TweetExtractor.ExtractUrls(postText);
1476             foreach (var url in urls)
1477             {
1478                 var shortUrlLength = url.StartsWith("https://", StringComparison.OrdinalIgnoreCase)
1479                     ? this.Configuration.ShortUrlLengthHttps
1480                     : this.Configuration.ShortUrlLength;
1481
1482                 textLength += shortUrlLength - url.Length;
1483             }
1484
1485             return this.Configuration.DmTextCharacterLimit - textLength;
1486         }
1487
1488         private int GetTextLengthRemainWeighted(string postText)
1489         {
1490             var config = this.TextConfiguration;
1491             var totalWeight = 0;
1492
1493             int GetWeightFromCodepoint(int codepoint)
1494             {
1495                 foreach (var weightRange in config.Ranges)
1496                 {
1497                     if (codepoint >= weightRange.Start && codepoint <= weightRange.End)
1498                         return weightRange.Weight;
1499                 }
1500
1501                 return config.DefaultWeight;
1502             }
1503
1504             var urls = TweetExtractor.ExtractUrlEntities(postText).ToArray();
1505             var emojis = config.EmojiParsingEnabled
1506                 ? TweetExtractor.ExtractEmojiEntities(postText).ToArray()
1507                 : Array.Empty<TwitterEntityEmoji>();
1508
1509             var codepoints = postText.ToCodepoints().ToArray();
1510             var index = 0;
1511             while (index < codepoints.Length)
1512             {
1513                 var urlEntity = urls.FirstOrDefault(x => x.Indices[0] == index);
1514                 if (urlEntity != null)
1515                 {
1516                     totalWeight += config.TransformedURLLength * config.Scale;
1517                     index = urlEntity.Indices[1];
1518                     continue;
1519                 }
1520
1521                 var emojiEntity = emojis.FirstOrDefault(x => x.Indices[0] == index);
1522                 if (emojiEntity != null)
1523                 {
1524                     totalWeight += GetWeightFromCodepoint(codepoints[index]);
1525                     index = emojiEntity.Indices[1];
1526                     continue;
1527                 }
1528
1529                 var codepoint = codepoints[index];
1530                 totalWeight += GetWeightFromCodepoint(codepoint);
1531
1532                 index++;
1533             }
1534
1535             var remainWeight = config.MaxWeightedTweetLength * config.Scale - totalWeight;
1536
1537             return remainWeight / config.Scale;
1538         }
1539
1540         /// <summary>
1541         /// プロフィール画像のサイズを指定したURLを生成
1542         /// </summary>
1543         /// <remarks>
1544         /// https://developer.twitter.com/en/docs/twitter-api/v1/accounts-and-users/user-profile-images-and-banners を参照
1545         /// </remarks>
1546         public static string CreateProfileImageUrl(string normalUrl, string size)
1547         {
1548             return size switch
1549             {
1550                 "original" => normalUrl.Replace("_normal.", "."),
1551                 "normal" => normalUrl,
1552                 "bigger" or "mini" => normalUrl.Replace("_normal.", $"_{size}."),
1553                 _ => throw new ArgumentException($"Invalid size: ${size}", nameof(size)),
1554             };
1555         }
1556
1557         public static string DecideProfileImageSize(int sizePx)
1558         {
1559             return sizePx switch
1560             {
1561                 <= 24 => "mini",
1562                 <= 48 => "normal",
1563                 <= 73 => "bigger",
1564                 _ => "original",
1565             };
1566         }
1567
1568         public bool IsDisposed { get; private set; } = false;
1569
1570         protected virtual void Dispose(bool disposing)
1571         {
1572             if (this.IsDisposed)
1573                 return;
1574
1575             if (disposing)
1576             {
1577                 this.Api.Dispose();
1578             }
1579
1580             this.IsDisposed = true;
1581         }
1582
1583         public void Dispose()
1584         {
1585             this.Dispose(true);
1586             GC.SuppressFinalize(this);
1587         }
1588     }
1589 }