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.
11 // This file is part of OpenTween.
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)
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
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.
31 using System.Collections.Generic;
32 using System.Diagnostics;
36 using System.Net.Http;
37 using System.Reflection;
38 using System.Runtime.CompilerServices;
40 using System.Text.RegularExpressions;
41 using System.Threading;
42 using System.Threading.Tasks;
43 using System.Windows.Forms;
45 using OpenTween.Api.DataModel;
46 using OpenTween.Api.TwitterV2;
47 using OpenTween.Connection;
48 using OpenTween.Models;
49 using OpenTween.Setting;
53 public class Twitter : IDisposable
55 #region Regexp from twitter-text-js
57 // The code in this region code block incorporates works covered by
58 // the following copyright and permission notices:
60 // Copyright 2011 Twitter, Inc.
62 // Licensed under the Apache License, Version 2.0 (the "License"); you
63 // may not use this work except in compliance with the License. You
64 // may obtain a copy of the License in the LICENSE file, or at:
66 // http://www.apache.org/licenses/LICENSE-2.0
68 // Unless required by applicable law or agreed to in writing, software
69 // distributed under the License is distributed on an "AS IS" BASIS,
70 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
71 // implied. See the License for the specific language governing
72 // permissions and limitations under the License.
75 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";
76 private const string NonLatinHashtagChars = @"\u0400-\u04ff\u0500-\u0527\u1100-\u11ff\u3130-\u3185\uA960-\uA97F\uAC00-\uD7AF\uD7B0-\uD7FF";
77 private const string CJHashtagCharacters = @"\u30A1-\u30FA\u30FC\u3005\uFF66-\uFF9F\uFF10-\uFF19\uFF21-\uFF3A\uFF41-\uFF5A\u3041-\u309A\u3400-\u4DBF\p{IsCJKUnifiedIdeographs}";
78 private const string HashtagBoundary = @"^|$|\s|「|」|。|\.|!";
79 private const string HashtagAlpha = $"[A-Za-z_{LatinAccents}{NonLatinHashtagChars}{CJHashtagCharacters}]";
80 private const string HashtagAlphanumeric = $"[A-Za-z0-9_{LatinAccents}{NonLatinHashtagChars}{CJHashtagCharacters}]";
81 private const string HashtagTerminator = $"[^A-Za-z0-9_{LatinAccents}{NonLatinHashtagChars}{CJHashtagCharacters}]";
82 public const string Hashtag = $"({HashtagBoundary})(#|#)({HashtagAlphanumeric}*{HashtagAlpha}{HashtagAlphanumeric}*)(?={HashtagTerminator}|{HashtagBoundary})";
84 private const string UrlValidPrecedingChars = @"(?:[^A-Za-z0-9@@$##\ufffe\ufeff\uffff\u202a-\u202e]|^)";
85 public const string UrlInvalidWithoutProtocolPrecedingChars = @"[-_./]$";
86 private const string UrlInvalidDomainChars = @"\!'#%&'\(\)*\+,\\\-\.\/:;<=>\?@\[\]\^_{|}~\$\u2000-\u200a\u0009-\u000d\u0020\u0085\u00a0\u1680\u180e\u2028\u2029\u202f\u205f\u3000\ufffe\ufeff\uffff\u202a-\u202e";
87 private const string UrlValidDomainChars = $@"[^{UrlInvalidDomainChars}]";
88 private const string UrlValidSubdomain = $@"(?:(?:{UrlValidDomainChars}(?:[_-]|{UrlValidDomainChars})*)?{UrlValidDomainChars}\.)";
89 private const string UrlValidDomainName = $@"(?:(?:{UrlValidDomainChars}(?:-|{UrlValidDomainChars})*)?{UrlValidDomainChars}\.)";
90 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]|$))";
91 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]|$))";
92 private const string UrlValidPunycode = @"(?:xn--[0-9a-z]+)";
93 private const string UrlValidDomain = $@"(?<domain>{UrlValidSubdomain}*{UrlValidDomainName}(?:{UrlValidGTLD}|{UrlValidCCTLD})|{UrlValidPunycode})";
94 public const string UrlValidAsciiDomain = $@"(?:(?:[a-z0-9{LatinAccents}]+)\.)+(?:{UrlValidGTLD}|{UrlValidCCTLD}|{UrlValidPunycode})";
95 public const string UrlInvalidShortDomain = $"^{UrlValidDomainName}{UrlValidCCTLD}$";
96 private const string UrlValidPortNumber = @"[0-9]+";
98 private const string UrlValidGeneralPathChars = $@"[a-z0-9!*';:=+,.$/%#\[\]\-_~|&{LatinAccents}]";
99 private const string UrlBalanceParens = $@"(?:\({UrlValidGeneralPathChars}+\))";
100 private const string UrlValidPathEndingChars = $@"(?:[+\-a-z0-9=_#/{LatinAccents}]|{UrlBalanceParens})";
101 private const string Pth = "(?:" +
103 $"{UrlValidGeneralPathChars}*" +
104 $"(?:{UrlBalanceParens}{UrlValidGeneralPathChars}*)*" +
105 UrlValidPathEndingChars +
106 $")|(?:@{UrlValidGeneralPathChars}+/)" +
109 private const string Qry = @"(?<query>\?[a-z0-9!?*'();:&=+$/%#\[\]\-_.,~|]*[a-z0-9_&=#/])?";
110 public const string RgUrl = $@"(?<before>{UrlValidPrecedingChars})" +
111 "(?<url>(?<protocol>https?://)?" +
112 $"(?<domain>{UrlValidDomain})" +
113 $"(?::{UrlValidPortNumber})?" +
114 $"(?<path>/{Pth}*)?" +
121 /// Twitter API のステータスページのURL
123 public const string ServiceAvailabilityStatusUrl = "https://api.twitterstat.us/";
126 /// ツイートへのパーマリンクURLを判定する正規表現
128 public static readonly Regex StatusUrlRegex = new(@"https?://([^.]+\.)?twitter\.com/(#!/)?(?<ScreenName>[a-zA-Z0-9_]+)/status(es)?/(?<StatusId>[0-9]+)(/photo)?", RegexOptions.IgnoreCase);
131 /// attachment_url に指定可能な URL を判定する正規表現
133 public static readonly Regex AttachmentUrlRegex = new(
135 twitter\.com/[0-9A-Za-z_]+/status/[0-9]+
136 | mobile\.twitter\.com/[0-9A-Za-z_]+/status/[0-9]+
137 | twitter\.com/messages/compose\?recipient_id=[0-9]+(&.+)?
139 RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
142 /// FavstarやaclogなどTwitter関連サービスのパーマリンクURLからステータスIDを抽出する正規表現
144 public static readonly Regex ThirdPartyStatusUrlRegex = new(
145 @"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]+)",
151 RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
154 /// DM送信かどうかを判定する正規表現
156 public static readonly Regex DMSendTextRegex = new(@"^DM? +(?<id>[a-zA-Z0-9_]+) +(?<body>.*)", RegexOptions.IgnoreCase | RegexOptions.Singleline);
158 public TwitterApi Api { get; }
160 public TwitterConfiguration Configuration { get; private set; }
162 public TwitterTextConfiguration TextConfiguration { get; private set; }
164 public bool GetFollowersSuccess { get; private set; } = false;
166 public bool GetNoRetweetSuccess { get; private set; } = false;
168 private delegate void GetIconImageDelegate(PostClass post);
170 private readonly object lockObj = new();
171 private ISet<long> followerId = new HashSet<long>();
172 private long[] noRTId = Array.Empty<long>();
174 private readonly TwitterPostFactory postFactory;
176 private string? nextCursorDirectMessage = null;
178 private long previousStatusId = -1L;
180 public Twitter(TwitterApi api)
182 this.postFactory = new(TabInformations.GetInstance());
185 this.Configuration = TwitterConfiguration.DefaultConfiguration();
186 this.TextConfiguration = TwitterTextConfiguration.DefaultConfiguration();
189 public TwitterApiAccessLevel AccessLevel
190 => MyCommon.TwitterApiInfo.AccessLevel;
192 protected void ResetApiStatus()
193 => MyCommon.TwitterApiInfo.Reset();
195 public void ClearAuthInfo()
197 Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
198 this.ResetApiStatus();
201 public void VerifyCredentials()
205 this.VerifyCredentialsAsync().Wait();
207 catch (AggregateException ex) when (ex.InnerException is WebApiException)
209 throw new WebApiException(ex.InnerException.Message, ex);
213 public async Task VerifyCredentialsAsync()
215 var user = await this.Api.AccountVerifyCredentials()
216 .ConfigureAwait(false);
218 this.UpdateUserStats(user);
221 public void Initialize(string token, string tokenSecret, string username, long userId)
224 if (MyCommon.IsNullOrEmpty(token) || MyCommon.IsNullOrEmpty(tokenSecret) || MyCommon.IsNullOrEmpty(username))
226 Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
228 this.ResetApiStatus();
229 this.Api.Initialize(token, tokenSecret, userId, username);
232 public async Task<PostClass?> PostStatus(PostStatusParams param)
234 this.CheckAccountState();
236 if (Twitter.DMSendTextRegex.IsMatch(param.Text))
238 var mediaId = param.MediaIds != null && param.MediaIds.Any() ? param.MediaIds[0] : (long?)null;
240 await this.SendDirectMessage(param.Text, mediaId)
241 .ConfigureAwait(false);
245 var response = await this.Api.StatusesUpdate(
247 param.InReplyToStatusId,
249 param.AutoPopulateReplyMetadata,
250 param.ExcludeReplyUserIds,
253 .ConfigureAwait(false);
255 var status = await response.LoadJsonAsync()
256 .ConfigureAwait(false);
258 this.UpdateUserStats(status.User);
260 if (status.Id == this.previousStatusId)
261 throw new WebApiException("OK:Delaying?");
263 this.previousStatusId = status.Id;
266 var post = this.CreatePostsFromStatusData(status);
267 if (this.ReadOwnPost) post.IsRead = true;
271 public async Task<long> UploadMedia(IMediaItem item, string? mediaCategory = null)
273 this.CheckAccountState();
275 var mediaType = item.Extension switch
277 ".png" => "image/png",
278 ".jpg" => "image/jpeg",
279 ".jpeg" => "image/jpeg",
280 ".gif" => "image/gif",
281 _ => "application/octet-stream",
284 var initResponse = await this.Api.MediaUploadInit(item.Size, mediaType, mediaCategory)
285 .ConfigureAwait(false);
287 var initMedia = await initResponse.LoadJsonAsync()
288 .ConfigureAwait(false);
290 var mediaId = initMedia.MediaId;
292 await this.Api.MediaUploadAppend(mediaId, 0, item)
293 .ConfigureAwait(false);
295 var response = await this.Api.MediaUploadFinalize(mediaId)
296 .ConfigureAwait(false);
298 var media = await response.LoadJsonAsync()
299 .ConfigureAwait(false);
301 while (media.ProcessingInfo is TwitterUploadMediaResult.MediaProcessingInfo processingInfo)
303 switch (processingInfo.State)
312 throw new WebApiException($"Err:Upload failed ({processingInfo.Error?.Name})");
314 throw new WebApiException($"Err:Invalid state ({processingInfo.State})");
317 await Task.Delay(TimeSpan.FromSeconds(processingInfo.CheckAfterSecs ?? 5))
318 .ConfigureAwait(false);
320 media = await this.Api.MediaUploadStatus(mediaId)
321 .ConfigureAwait(false);
325 return media.MediaId;
328 public async Task SendDirectMessage(string postStr, long? mediaId = null)
330 this.CheckAccountState();
331 this.CheckAccessLevel(TwitterApiAccessLevel.ReadWriteAndDirectMessage);
333 var mc = Twitter.DMSendTextRegex.Match(postStr);
335 var body = mc.Groups["body"].Value;
336 var recipientName = mc.Groups["id"].Value;
338 var recipient = await this.Api.UsersShow(recipientName)
339 .ConfigureAwait(false);
341 var response = await this.Api.DirectMessagesEventsNew(recipient.Id, body, mediaId)
342 .ConfigureAwait(false);
344 var messageEventSingle = await response.LoadJsonAsync()
345 .ConfigureAwait(false);
347 await this.CreateDirectMessagesEventFromJson(messageEventSingle, read: true)
348 .ConfigureAwait(false);
351 public async Task<PostClass?> PostRetweet(long id, bool read)
353 this.CheckAccountState();
356 var post = TabInformations.GetInstance()[id];
358 throw new WebApiException("Err:Target isn't found.");
360 var target = post.RetweetedId ?? id; // 再RTの場合は元発言をRT
362 var response = await this.Api.StatusesRetweet(target)
363 .ConfigureAwait(false);
365 var status = await response.LoadJsonAsync()
366 .ConfigureAwait(false);
371 if (TabInformations.GetInstance().ContainsKey(status.Id))
376 if (status.RetweetedStatus == null)
377 throw new WebApiException("Invalid Json!");
380 post = this.CreatePostsFromStatusData(status);
387 if (this.ReadOwnPost) post.IsRead = true;
393 public string Username
394 => this.Api.CurrentScreenName;
397 => this.Api.CurrentUserId;
399 public static MyCommon.ACCOUNT_STATE AccountState { get; set; } = MyCommon.ACCOUNT_STATE.Valid;
401 public bool RestrictFavCheck { get; set; }
403 public bool ReadOwnPost { get; set; }
405 public int FollowersCount { get; private set; }
407 public int FriendsCount { get; private set; }
409 public int StatusesCount { get; private set; }
411 public string Location { get; private set; } = "";
413 public string Bio { get; private set; } = "";
415 /// <summary>ユーザーのフォロワー数などの情報を更新します</summary>
416 private void UpdateUserStats(TwitterUser self)
418 this.FollowersCount = self.FollowersCount;
419 this.FriendsCount = self.FriendsCount;
420 this.StatusesCount = self.StatusesCount;
421 this.Location = self.Location ?? "";
422 this.Bio = self.Description ?? "";
426 /// 渡された取得件数がWORKERTYPEに応じた取得可能範囲に収まっているか検証する
428 public static bool VerifyApiResultCount(MyCommon.WORKERTYPE type, int count)
429 => count >= 20 && count <= GetMaxApiResultCount(type);
432 /// 渡された取得件数が更新時の取得可能範囲に収まっているか検証する
434 public static bool VerifyMoreApiResultCount(int count)
435 => count >= 20 && count <= 200;
438 /// 渡された取得件数が起動時の取得可能範囲に収まっているか検証する
440 public static bool VerifyFirstApiResultCount(int count)
441 => count >= 20 && count <= 200;
444 /// WORKERTYPEに応じた取得可能な最大件数を取得する
446 public static int GetMaxApiResultCount(MyCommon.WORKERTYPE type)
448 // 参照: REST APIs - 各endpointのcountパラメータ
449 // https://dev.twitter.com/rest/public
452 MyCommon.WORKERTYPE.Timeline => 100,
453 MyCommon.WORKERTYPE.Reply => 200,
454 MyCommon.WORKERTYPE.UserTimeline => 200,
455 MyCommon.WORKERTYPE.Favorites => 200,
456 MyCommon.WORKERTYPE.List => 200, // 不明
457 MyCommon.WORKERTYPE.PublicSearch => 100,
458 _ => throw new InvalidOperationException("Invalid type: " + type),
463 /// WORKERTYPEに応じた取得件数を取得する
465 public static int GetApiResultCount(MyCommon.WORKERTYPE type, bool more, bool startup)
467 if (SettingManager.Instance.Common.UseAdditionalCount)
471 case MyCommon.WORKERTYPE.Favorites:
472 if (SettingManager.Instance.Common.FavoritesCountApi != 0)
473 return SettingManager.Instance.Common.FavoritesCountApi;
475 case MyCommon.WORKERTYPE.List:
476 if (SettingManager.Instance.Common.ListCountApi != 0)
477 return SettingManager.Instance.Common.ListCountApi;
479 case MyCommon.WORKERTYPE.PublicSearch:
480 if (SettingManager.Instance.Common.SearchCountApi != 0)
481 return SettingManager.Instance.Common.SearchCountApi;
483 case MyCommon.WORKERTYPE.UserTimeline:
484 if (SettingManager.Instance.Common.UserTimelineCountApi != 0)
485 return SettingManager.Instance.Common.UserTimelineCountApi;
488 if (more && SettingManager.Instance.Common.MoreCountApi != 0)
490 return Math.Min(SettingManager.Instance.Common.MoreCountApi, GetMaxApiResultCount(type));
492 if (startup && SettingManager.Instance.Common.FirstCountApi != 0 && type != MyCommon.WORKERTYPE.Reply)
494 return Math.Min(SettingManager.Instance.Common.FirstCountApi, GetMaxApiResultCount(type));
499 var count = SettingManager.Instance.Common.CountApi;
501 if (type == MyCommon.WORKERTYPE.Reply)
502 count = SettingManager.Instance.Common.CountApiReply;
504 return Math.Min(count, GetMaxApiResultCount(type));
507 public async Task GetHomeTimelineApi(bool read, HomeTabModel tab, bool more, bool startup)
509 this.CheckAccountState();
511 var count = GetApiResultCount(MyCommon.WORKERTYPE.Timeline, more, startup);
513 var request = new GetTimelineRequest(this.UserId)
516 UntilId = more ? tab.OldestId.ToString() : null,
519 var response = await request.Send(this.Api.Connection)
520 .ConfigureAwait(false);
522 if (response.Data == null || response.Data.Length == 0)
525 var tweetIds = response.Data.Select(x => x.Id).ToList();
527 var statuses = await this.Api.StatusesLookup(tweetIds)
528 .ConfigureAwait(false);
530 var minimumId = this.CreatePostsFromJson(statuses, MyCommon.WORKERTYPE.Timeline, tab, read);
531 if (minimumId != null)
532 tab.OldestId = minimumId.Value;
535 public async Task GetMentionsTimelineApi(bool read, MentionsTabModel tab, bool more, bool startup)
537 this.CheckAccountState();
539 var count = GetApiResultCount(MyCommon.WORKERTYPE.Reply, more, startup);
541 TwitterStatus[] statuses;
544 statuses = await this.Api.StatusesMentionsTimeline(count, maxId: tab.OldestId)
545 .ConfigureAwait(false);
549 statuses = await this.Api.StatusesMentionsTimeline(count)
550 .ConfigureAwait(false);
553 var minimumId = this.CreatePostsFromJson(statuses, MyCommon.WORKERTYPE.Reply, tab, read);
554 if (minimumId != null)
555 tab.OldestId = minimumId.Value;
558 public async Task GetUserTimelineApi(bool read, string userName, UserTimelineTabModel tab, bool more)
560 this.CheckAccountState();
562 var count = GetApiResultCount(MyCommon.WORKERTYPE.UserTimeline, more, false);
564 TwitterStatus[] statuses;
565 if (MyCommon.IsNullOrEmpty(userName))
567 var target = tab.ScreenName;
568 if (MyCommon.IsNullOrEmpty(target)) return;
570 statuses = await this.Api.StatusesUserTimeline(userName, count)
571 .ConfigureAwait(false);
577 statuses = await this.Api.StatusesUserTimeline(userName, count, maxId: tab.OldestId)
578 .ConfigureAwait(false);
582 statuses = await this.Api.StatusesUserTimeline(userName, count)
583 .ConfigureAwait(false);
587 var minimumId = this.CreatePostsFromJson(statuses, MyCommon.WORKERTYPE.UserTimeline, tab, read);
589 if (minimumId != null)
590 tab.OldestId = minimumId.Value;
593 public async Task<PostClass> GetStatusApi(bool read, long id)
595 this.CheckAccountState();
597 var status = await this.Api.StatusesShow(id)
598 .ConfigureAwait(false);
600 var item = this.CreatePostsFromStatusData(status);
603 if (item.IsMe && !read && this.ReadOwnPost) item.IsRead = true;
608 public async Task GetStatusApi(bool read, long id, TabModel tab)
610 var post = await this.GetStatusApi(read, id)
611 .ConfigureAwait(false);
613 // 非同期アイコン取得&StatusDictionaryに追加
614 if (tab != null && tab.IsInnerStorageTabType)
615 tab.AddPostQueue(post);
617 TabInformations.GetInstance().AddPost(post);
620 private PostClass CreatePostsFromStatusData(TwitterStatus status)
621 => this.CreatePostsFromStatusData(status, favTweet: false);
623 private PostClass CreatePostsFromStatusData(TwitterStatus status, bool favTweet)
624 => this.postFactory.CreateFromStatus(status, this.UserId, this.followerId, favTweet);
626 private long? CreatePostsFromJson(TwitterStatus[] items, MyCommon.WORKERTYPE gType, TabModel? tab, bool read)
628 long? minimumId = null;
630 foreach (var status in items)
632 if (minimumId == null || minimumId.Value > status.Id)
633 minimumId = status.Id;
640 if (TabInformations.GetInstance().ContainsKey(status.Id)) continue;
644 if (tab.Contains(status.Id)) continue;
649 if (gType != MyCommon.WORKERTYPE.UserTimeline &&
650 status.RetweetedStatus != null && this.noRTId.Contains(status.User.Id)) continue;
652 var post = this.CreatePostsFromStatusData(status);
655 if (post.IsMe && !read && this.ReadOwnPost) post.IsRead = true;
657 if (tab != null && tab.IsInnerStorageTabType)
658 tab.AddPostQueue(post);
660 TabInformations.GetInstance().AddPost(post);
666 private long? CreatePostsFromSearchJson(TwitterSearchResult items, PublicSearchTabModel tab, bool read, bool more)
668 long? minimumId = null;
670 foreach (var status in items.Statuses)
672 if (minimumId == null || minimumId.Value > status.Id)
673 minimumId = status.Id;
675 if (!more && status.Id > tab.SinceId) tab.SinceId = status.Id;
679 if (tab.Contains(status.Id)) continue;
682 var post = this.CreatePostsFromStatusData(status);
685 if ((post.IsMe && !read) && this.ReadOwnPost) post.IsRead = true;
687 tab.AddPostQueue(post);
693 private long? CreateFavoritePostsFromJson(TwitterStatus[] items, bool read)
695 var favTab = TabInformations.GetInstance().FavoriteTab;
696 long? minimumId = null;
698 foreach (var status in items)
700 if (minimumId == null || minimumId.Value > status.Id)
701 minimumId = status.Id;
706 if (favTab.Contains(status.Id)) continue;
709 var post = this.CreatePostsFromStatusData(status, true);
713 TabInformations.GetInstance().AddPost(post);
719 public async Task GetListStatus(bool read, ListTimelineTabModel tab, bool more, bool startup)
721 var count = GetApiResultCount(MyCommon.WORKERTYPE.List, more, startup);
723 TwitterStatus[] statuses;
726 statuses = await this.Api.ListsStatuses(tab.ListInfo.Id, count, maxId: tab.OldestId, includeRTs: SettingManager.Instance.Common.IsListsIncludeRts)
727 .ConfigureAwait(false);
731 statuses = await this.Api.ListsStatuses(tab.ListInfo.Id, count, includeRTs: SettingManager.Instance.Common.IsListsIncludeRts)
732 .ConfigureAwait(false);
735 var minimumId = this.CreatePostsFromJson(statuses, MyCommon.WORKERTYPE.List, tab, read);
737 if (minimumId != null)
738 tab.OldestId = minimumId.Value;
742 /// startStatusId からリプライ先の発言を辿る。発言は posts 以外からは検索しない。
744 /// <returns>posts の中から検索されたリプライチェインの末端</returns>
745 internal static PostClass FindTopOfReplyChain(IDictionary<long, PostClass> posts, long startStatusId)
747 if (!posts.ContainsKey(startStatusId))
748 throw new ArgumentException("startStatusId (" + startStatusId + ") が posts の中から見つかりませんでした。", nameof(startStatusId));
750 var nextPost = posts[startStatusId];
751 while (nextPost.InReplyToStatusId != null)
753 if (!posts.ContainsKey(nextPost.InReplyToStatusId.Value))
755 nextPost = posts[nextPost.InReplyToStatusId.Value];
761 public async Task GetRelatedResult(bool read, RelatedPostsTabModel tab)
763 var targetPost = tab.TargetPost;
765 if (targetPost.RetweetedId != null)
767 var originalPost = targetPost.Clone();
768 originalPost.StatusId = targetPost.RetweetedId.Value;
769 originalPost.RetweetedId = null;
770 originalPost.RetweetedBy = null;
771 targetPost = originalPost;
774 var relPosts = new Dictionary<long, PostClass>();
775 if (targetPost.TextFromApi.Contains("@") && targetPost.InReplyToStatusId == null)
778 var p = TabInformations.GetInstance()[targetPost.StatusId];
779 if (p != null && p.InReplyToStatusId != null)
785 p = await this.GetStatusApi(read, targetPost.StatusId)
786 .ConfigureAwait(false);
790 relPosts.Add(targetPost.StatusId, targetPost);
792 Exception? lastException = null;
794 // in_reply_to_status_id を使用してリプライチェインを辿る
795 var nextPost = FindTopOfReplyChain(relPosts, targetPost.StatusId);
797 while (nextPost.InReplyToStatusId != null && loopCount++ <= 20)
799 var inReplyToId = nextPost.InReplyToStatusId.Value;
801 var inReplyToPost = TabInformations.GetInstance()[inReplyToId];
802 if (inReplyToPost == null)
806 inReplyToPost = await this.GetStatusApi(read, inReplyToId)
807 .ConfigureAwait(false);
809 catch (WebApiException ex)
816 relPosts.Add(inReplyToPost.StatusId, inReplyToPost);
818 nextPost = FindTopOfReplyChain(relPosts, nextPost.StatusId);
821 // MRTとかに対応のためツイート内にあるツイートを指すURLを取り込む
822 var text = targetPost.Text;
823 var ma = Twitter.StatusUrlRegex.Matches(text).Cast<Match>()
824 .Concat(Twitter.ThirdPartyStatusUrlRegex.Matches(text).Cast<Match>());
825 foreach (var match in ma)
827 if (long.TryParse(match.Groups["StatusId"].Value, out var statusId))
829 if (relPosts.ContainsKey(statusId))
832 var p = TabInformations.GetInstance()[statusId];
837 p = await this.GetStatusApi(read, statusId)
838 .ConfigureAwait(false);
840 catch (WebApiException ex)
848 relPosts.Add(p.StatusId, p);
854 var firstPost = nextPost;
855 var posts = await this.GetConversationPosts(firstPost, targetPost)
856 .ConfigureAwait(false);
858 foreach (var post in posts.OrderBy(x => x.StatusId))
860 if (relPosts.ContainsKey(post.StatusId))
863 // リプライチェーンが繋がらないツイートは除外
864 if (post.InReplyToStatusId == null || !relPosts.ContainsKey(post.InReplyToStatusId.Value))
867 relPosts.Add(post.StatusId, post);
870 catch (WebException ex)
875 relPosts.Values.ToList().ForEach(p =>
877 var post = p.Clone();
878 if (post.IsMe && !read && this.ReadOwnPost)
883 tab.AddPostQueue(post);
886 if (lastException != null)
887 throw new WebApiException(lastException.Message, lastException);
890 private async Task<PostClass[]> GetConversationPosts(PostClass firstPost, PostClass targetPost)
892 var conversationId = firstPost.StatusId;
893 var query = $"conversation_id:{conversationId}";
895 if (targetPost.InReplyToUser != null && targetPost.InReplyToUser != targetPost.ScreenName)
896 query += $" (from:{targetPost.ScreenName} to:{targetPost.InReplyToUser}) OR (from:{targetPost.InReplyToUser} to:{targetPost.ScreenName})";
898 query += $" from:{targetPost.ScreenName} to:{targetPost.ScreenName}";
900 var statuses = await this.Api.SearchTweets(query, count: 100)
901 .ConfigureAwait(false);
903 return statuses.Statuses.Select(x => this.CreatePostsFromStatusData(x)).ToArray();
906 public async Task GetSearch(bool read, PublicSearchTabModel tab, bool more)
908 var count = GetApiResultCount(MyCommon.WORKERTYPE.PublicSearch, more, false);
911 long? sinceId = null;
914 maxId = tab.OldestId - 1;
918 sinceId = tab.SinceId;
921 var searchResult = await this.Api.SearchTweets(tab.SearchWords, tab.SearchLang, count, maxId, sinceId)
922 .ConfigureAwait(false);
924 if (!TabInformations.GetInstance().ContainsTab(tab))
927 var minimumId = this.CreatePostsFromSearchJson(searchResult, tab, read, more);
929 if (minimumId != null)
930 tab.OldestId = minimumId.Value;
933 public async Task GetDirectMessageEvents(bool read, bool backward)
935 this.CheckAccountState();
936 this.CheckAccessLevel(TwitterApiAccessLevel.ReadWriteAndDirectMessage);
940 TwitterMessageEventList eventList;
943 eventList = await this.Api.DirectMessagesEventsList(count, this.nextCursorDirectMessage)
944 .ConfigureAwait(false);
948 eventList = await this.Api.DirectMessagesEventsList(count)
949 .ConfigureAwait(false);
952 this.nextCursorDirectMessage = eventList.NextCursor;
954 await this.CreateDirectMessagesEventFromJson(eventList, read)
955 .ConfigureAwait(false);
958 private async Task CreateDirectMessagesEventFromJson(TwitterMessageEventSingle eventSingle, bool read)
960 var eventList = new TwitterMessageEventList
962 Apps = new Dictionary<string, TwitterMessageEventList.App>(),
963 Events = new[] { eventSingle.Event },
966 await this.CreateDirectMessagesEventFromJson(eventList, read)
967 .ConfigureAwait(false);
970 private async Task CreateDirectMessagesEventFromJson(TwitterMessageEventList eventList, bool read)
972 var events = eventList.Events
973 .Where(x => x.Type == "message_create")
976 if (events.Length == 0)
979 var userIds = Enumerable.Concat(
980 events.Select(x => x.MessageCreate.SenderId),
981 events.Select(x => x.MessageCreate.Target.RecipientId)
982 ).Distinct().ToArray();
984 var users = (await this.Api.UsersLookup(userIds).ConfigureAwait(false))
985 .ToDictionary(x => x.IdStr);
987 var apps = eventList.Apps ?? new Dictionary<string, TwitterMessageEventList.App>();
989 this.CreateDirectMessagesEventFromJson(events, users, apps, read);
992 private void CreateDirectMessagesEventFromJson(
993 IEnumerable<TwitterMessageEvent> events,
994 IReadOnlyDictionary<string, TwitterUser> users,
995 IReadOnlyDictionary<string, TwitterMessageEventList.App> apps,
998 var dmTab = TabInformations.GetInstance().DirectMessageTab;
1000 foreach (var eventItem in events)
1002 var post = this.postFactory.CreateFromDirectMessageEvent(eventItem, users, apps, this.UserId);
1005 if (post.IsMe && !read && this.ReadOwnPost)
1008 dmTab.AddPostQueue(post);
1012 public async Task GetFavoritesApi(bool read, FavoritesTabModel tab, bool backward)
1014 this.CheckAccountState();
1016 var count = GetApiResultCount(MyCommon.WORKERTYPE.Favorites, backward, false);
1018 TwitterStatus[] statuses;
1021 statuses = await this.Api.FavoritesList(count, maxId: tab.OldestId)
1022 .ConfigureAwait(false);
1026 statuses = await this.Api.FavoritesList(count)
1027 .ConfigureAwait(false);
1030 var minimumId = this.CreateFavoritePostsFromJson(statuses, read);
1032 if (minimumId != null)
1033 tab.OldestId = minimumId.Value;
1039 /// <exception cref="WebApiException"/>
1040 public async Task RefreshFollowerIds()
1042 if (MyCommon.EndingFlag) return;
1045 var newFollowerIds = Enumerable.Empty<long>();
1048 var ret = await this.Api.FollowersIds(cursor)
1049 .ConfigureAwait(false);
1051 if (ret.Ids == null)
1052 throw new WebApiException("ret.ids == null");
1054 newFollowerIds = newFollowerIds.Concat(ret.Ids);
1055 cursor = ret.NextCursor;
1057 while (cursor != 0);
1059 this.followerId = newFollowerIds.ToHashSet();
1060 TabInformations.GetInstance().RefreshOwl(this.followerId);
1062 this.GetFollowersSuccess = true;
1066 /// RT 非表示ユーザーを更新します
1068 /// <exception cref="WebApiException"/>
1069 public async Task RefreshNoRetweetIds()
1071 if (MyCommon.EndingFlag) return;
1073 this.noRTId = await this.Api.NoRetweetIds()
1074 .ConfigureAwait(false);
1076 this.GetNoRetweetSuccess = true;
1080 /// t.co の文字列長などの設定情報を更新します
1082 /// <exception cref="WebApiException"/>
1083 public async Task RefreshConfiguration()
1085 this.Configuration = await this.Api.Configuration()
1086 .ConfigureAwait(false);
1088 // TextConfiguration 相当の JSON を得る API が存在しないため、TransformedURLLength のみ help/configuration.json に合わせて更新する
1089 this.TextConfiguration.TransformedURLLength = this.Configuration.ShortUrlLengthHttps;
1092 public async Task GetListsApi()
1094 this.CheckAccountState();
1096 var ownedLists = await TwitterLists.GetAllItemsAsync(x =>
1097 this.Api.ListsOwnerships(this.Username, cursor: x, count: 1000))
1098 .ConfigureAwait(false);
1100 var subscribedLists = await TwitterLists.GetAllItemsAsync(x =>
1101 this.Api.ListsSubscriptions(this.Username, cursor: x, count: 1000))
1102 .ConfigureAwait(false);
1104 TabInformations.GetInstance().SubscribableLists = Enumerable.Concat(ownedLists, subscribedLists)
1105 .Select(x => new ListElement(x, this))
1109 public async Task DeleteList(long listId)
1111 await this.Api.ListsDestroy(listId)
1113 .ConfigureAwait(false);
1115 var tabinfo = TabInformations.GetInstance();
1117 tabinfo.SubscribableLists = tabinfo.SubscribableLists
1118 .Where(x => x.Id != listId)
1122 public async Task<ListElement> EditList(long listId, string new_name, bool isPrivate, string description)
1124 var response = await this.Api.ListsUpdate(listId, new_name, description, isPrivate)
1125 .ConfigureAwait(false);
1127 var list = await response.LoadJsonAsync()
1128 .ConfigureAwait(false);
1130 return new ListElement(list, this);
1133 public async Task<long> GetListMembers(long listId, List<UserInfo> lists, long cursor)
1135 this.CheckAccountState();
1137 var users = await this.Api.ListsMembers(listId, cursor)
1138 .ConfigureAwait(false);
1140 Array.ForEach(users.Users, u => lists.Add(new UserInfo(u)));
1142 return users.NextCursor;
1145 public async Task CreateListApi(string listName, bool isPrivate, string description)
1147 this.CheckAccountState();
1149 var response = await this.Api.ListsCreate(listName, description, isPrivate)
1150 .ConfigureAwait(false);
1152 var list = await response.LoadJsonAsync()
1153 .ConfigureAwait(false);
1155 TabInformations.GetInstance().SubscribableLists.Add(new ListElement(list, this));
1158 public async Task<bool> ContainsUserAtList(long listId, string user)
1160 this.CheckAccountState();
1164 await this.Api.ListsMembersShow(listId, user)
1165 .ConfigureAwait(false);
1169 catch (TwitterApiException ex)
1170 when (ex.Errors.Any(x => x.Code == TwitterErrorCode.NotFound))
1176 public async Task<TwitterApiStatus?> GetInfoApi()
1178 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return null;
1180 if (MyCommon.EndingFlag) return null;
1182 var limits = await this.Api.ApplicationRateLimitStatus()
1183 .ConfigureAwait(false);
1185 MyCommon.TwitterApiInfo.UpdateFromJson(limits);
1187 return MyCommon.TwitterApiInfo;
1191 /// ブロック中のユーザーを更新します
1193 /// <exception cref="WebApiException"/>
1194 public async Task RefreshBlockIds()
1196 if (MyCommon.EndingFlag) return;
1199 var newBlockIds = Enumerable.Empty<long>();
1202 var ret = await this.Api.BlocksIds(cursor)
1203 .ConfigureAwait(false);
1205 newBlockIds = newBlockIds.Concat(ret.Ids);
1206 cursor = ret.NextCursor;
1208 while (cursor != 0);
1210 var blockIdsSet = newBlockIds.ToHashSet();
1211 blockIdsSet.Remove(this.UserId); // 元のソースにあったので一応残しておく
1213 TabInformations.GetInstance().BlockIds = blockIdsSet;
1217 /// ミュート中のユーザーIDを更新します
1219 /// <exception cref="WebApiException"/>
1220 public async Task RefreshMuteUserIdsAsync()
1222 if (MyCommon.EndingFlag) return;
1224 var ids = await TwitterIds.GetAllItemsAsync(x => this.Api.MutesUsersIds(x))
1225 .ConfigureAwait(false);
1227 TabInformations.GetInstance().MuteUserIds = ids.ToHashSet();
1230 public string[] GetHashList()
1231 => this.postFactory.GetReceivedHashtags();
1233 public string AccessToken
1234 => ((TwitterApiConnection)this.Api.Connection).AccessToken;
1236 public string AccessTokenSecret
1237 => ((TwitterApiConnection)this.Api.Connection).AccessSecret;
1239 private void CheckAccountState()
1241 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
1242 throw new WebApiException("Auth error. Check your account");
1245 private void CheckAccessLevel(TwitterApiAccessLevel accessLevelFlags)
1247 if (!this.AccessLevel.HasFlag(accessLevelFlags))
1248 throw new WebApiException("Auth Err:try to re-authorization.");
1251 public int GetTextLengthRemain(string postText)
1253 var matchDm = Twitter.DMSendTextRegex.Match(postText);
1254 if (matchDm.Success)
1255 return this.GetTextLengthRemainDM(matchDm.Groups["body"].Value);
1257 return this.GetTextLengthRemainWeighted(postText);
1260 private int GetTextLengthRemainDM(string postText)
1265 while (pos < postText.Length)
1269 if (char.IsSurrogatePair(postText, pos))
1270 pos += 2; // サロゲートペアの場合は2文字分進める
1275 var urls = TweetExtractor.ExtractUrls(postText);
1276 foreach (var url in urls)
1278 var shortUrlLength = url.StartsWith("https://", StringComparison.OrdinalIgnoreCase)
1279 ? this.Configuration.ShortUrlLengthHttps
1280 : this.Configuration.ShortUrlLength;
1282 textLength += shortUrlLength - url.Length;
1285 return this.Configuration.DmTextCharacterLimit - textLength;
1288 private int GetTextLengthRemainWeighted(string postText)
1290 var config = this.TextConfiguration;
1291 var totalWeight = 0;
1293 int GetWeightFromCodepoint(int codepoint)
1295 foreach (var weightRange in config.Ranges)
1297 if (codepoint >= weightRange.Start && codepoint <= weightRange.End)
1298 return weightRange.Weight;
1301 return config.DefaultWeight;
1304 var urls = TweetExtractor.ExtractUrlEntities(postText).ToArray();
1305 var emojis = config.EmojiParsingEnabled
1306 ? TweetExtractor.ExtractEmojiEntities(postText).ToArray()
1307 : Array.Empty<TwitterEntityEmoji>();
1309 var codepoints = postText.ToCodepoints().ToArray();
1311 while (index < codepoints.Length)
1313 var urlEntity = urls.FirstOrDefault(x => x.Indices[0] == index);
1314 if (urlEntity != null)
1316 totalWeight += config.TransformedURLLength * config.Scale;
1317 index = urlEntity.Indices[1];
1321 var emojiEntity = emojis.FirstOrDefault(x => x.Indices[0] == index);
1322 if (emojiEntity != null)
1324 totalWeight += GetWeightFromCodepoint(codepoints[index]);
1325 index = emojiEntity.Indices[1];
1329 var codepoint = codepoints[index];
1330 totalWeight += GetWeightFromCodepoint(codepoint);
1335 var remainWeight = config.MaxWeightedTweetLength * config.Scale - totalWeight;
1337 return remainWeight / config.Scale;
1341 /// プロフィール画像のサイズを指定したURLを生成
1344 /// https://developer.twitter.com/en/docs/twitter-api/v1/accounts-and-users/user-profile-images-and-banners を参照
1346 public static string CreateProfileImageUrl(string normalUrl, string size)
1350 "original" => normalUrl.Replace("_normal.", "."),
1351 "normal" => normalUrl,
1352 "bigger" or "mini" => normalUrl.Replace("_normal.", $"_{size}."),
1353 _ => throw new ArgumentException($"Invalid size: ${size}", nameof(size)),
1357 public static string DecideProfileImageSize(int sizePx)
1359 return sizePx switch
1368 public bool IsDisposed { get; private set; } = false;
1370 protected virtual void Dispose(bool disposing)
1372 if (this.IsDisposed)
1380 this.IsDisposed = true;
1383 public void Dispose()
1386 GC.SuppressFinalize(this);