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.
30 using System.Diagnostics;
34 using System.Net.Http;
35 using System.Runtime.CompilerServices;
37 using System.Text.RegularExpressions;
38 using System.Threading;
39 using System.Threading.Tasks;
41 using System.Reflection;
42 using System.Collections.Generic;
43 using System.Windows.Forms;
45 using OpenTween.Api.DataModel;
46 using OpenTween.Connection;
47 using OpenTween.Models;
48 using OpenTween.Setting;
49 using System.Globalization;
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 LATIN_ACCENTS = @"\u00c0-\u00d6\u00d8-\u00f6\u00f8-\u00ff\u0100-\u024f\u0253\u0254\u0256\u0257\u0259\u025b\u0263\u0268\u026f\u0272\u0289\u028b\u02bb\u1e00-\u1eff";
76 private const string NON_LATIN_HASHTAG_CHARS = @"\u0400-\u04ff\u0500-\u0527\u1100-\u11ff\u3130-\u3185\uA960-\uA97F\uAC00-\uD7AF\uD7B0-\uD7FF";
77 private const string CJ_HASHTAG_CHARACTERS = @"\u30A1-\u30FA\u30FC\u3005\uFF66-\uFF9F\uFF10-\uFF19\uFF21-\uFF3A\uFF41-\uFF5A\u3041-\u309A\u3400-\u4DBF\p{IsCJKUnifiedIdeographs}";
78 private const string HASHTAG_BOUNDARY = @"^|$|\s|「|」|。|\.|!";
79 private const string HASHTAG_ALPHA = "[A-Za-z_" + LATIN_ACCENTS + NON_LATIN_HASHTAG_CHARS + CJ_HASHTAG_CHARACTERS + "]";
80 private const string HASHTAG_ALPHANUMERIC = "[A-Za-z0-9_" + LATIN_ACCENTS + NON_LATIN_HASHTAG_CHARS + CJ_HASHTAG_CHARACTERS + "]";
81 private const string HASHTAG_TERMINATOR = "[^A-Za-z0-9_" + LATIN_ACCENTS + NON_LATIN_HASHTAG_CHARS + CJ_HASHTAG_CHARACTERS + "]";
82 public const string HASHTAG = "(" + HASHTAG_BOUNDARY + ")(#|#)(" + HASHTAG_ALPHANUMERIC + "*" + HASHTAG_ALPHA + HASHTAG_ALPHANUMERIC + "*)(?=" + HASHTAG_TERMINATOR + "|" + HASHTAG_BOUNDARY + ")";
84 private const string url_valid_preceding_chars = @"(?:[^A-Za-z0-9@@$##\ufffe\ufeff\uffff\u202a-\u202e]|^)";
85 public const string url_invalid_without_protocol_preceding_chars = @"[-_./]$";
86 private const string url_invalid_domain_chars = @"\!'#%&'\(\)*\+,\\\-\.\/:;<=>\?@\[\]\^_{|}~\$\u2000-\u200a\u0009-\u000d\u0020\u0085\u00a0\u1680\u180e\u2028\u2029\u202f\u205f\u3000\ufffe\ufeff\uffff\u202a-\u202e";
87 private const string url_valid_domain_chars = @"[^" + url_invalid_domain_chars + "]";
88 private const string url_valid_subdomain = @"(?:(?:" + url_valid_domain_chars + @"(?:[_-]|" + url_valid_domain_chars + @")*)?" + url_valid_domain_chars + @"\.)";
89 private const string url_valid_domain_name = @"(?:(?:" + url_valid_domain_chars + @"(?:-|" + url_valid_domain_chars + @")*)?" + url_valid_domain_chars + @"\.)";
90 private const string url_valid_GTLD = @"(?:(?:aero|asia|biz|cat|com|coop|edu|gov|info|int|jobs|mil|mobi|museum|name|net|org|pro|tel|travel|xxx)(?=[^0-9a-zA-Z]|$))";
91 private const string url_valid_CCTLD = @"(?:(?:ac|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|cr|cs|cu|cv|cx|cy|cz|dd|de|dj|dk|dm|do|dz|ec|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|io|iq|ir|is|it|je|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|mv|mw|mx|my|mz|na|nc|ne|nf|ng|ni|nl|no|np|nr|nu|nz|om|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|ps|pt|pw|py|qa|re|ro|rs|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|ss|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tl|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|za|zm|zw)(?=[^0-9a-zA-Z]|$))";
92 private const string url_valid_punycode = @"(?:xn--[0-9a-z]+)";
93 private const string url_valid_domain = @"(?<domain>" + url_valid_subdomain + "*" + url_valid_domain_name + "(?:" + url_valid_GTLD + "|" + url_valid_CCTLD + ")|" + url_valid_punycode + ")";
94 public const string url_valid_ascii_domain = @"(?:(?:[a-z0-9" + LATIN_ACCENTS + @"]+)\.)+(?:" + url_valid_GTLD + "|" + url_valid_CCTLD + "|" + url_valid_punycode + ")";
95 public const string url_invalid_short_domain = "^" + url_valid_domain_name + url_valid_CCTLD + "$";
96 private const string url_valid_port_number = @"[0-9]+";
98 private const string url_valid_general_path_chars = @"[a-z0-9!*';:=+,.$/%#\[\]\-_~|&" + LATIN_ACCENTS + "]";
99 private const string url_balance_parens = @"(?:\(" + url_valid_general_path_chars + @"+\))";
100 private const string url_valid_path_ending_chars = @"(?:[+\-a-z0-9=_#/" + LATIN_ACCENTS + "]|" + url_balance_parens + ")";
101 private const string pth = "(?:" +
103 url_valid_general_path_chars + "*" +
104 "(?:" + url_balance_parens + url_valid_general_path_chars + "*)*" +
105 url_valid_path_ending_chars +
106 ")|(?:@" + url_valid_general_path_chars + "+/)" +
108 private const string qry = @"(?<query>\?[a-z0-9!?*'();:&=+$/%#\[\]\-_.,~|]*[a-z0-9_&=#/])?";
109 public const string rgUrl = @"(?<before>" + url_valid_preceding_chars + ")" +
110 "(?<url>(?<protocol>https?://)?" +
111 "(?<domain>" + url_valid_domain + ")" +
112 "(?::" + url_valid_port_number + ")?" +
113 "(?<path>/" + pth + "*)?" +
120 /// Twitter API のステータスページのURL
122 public const string ServiceAvailabilityStatusUrl = "https://status.io.watchmouse.com/7617";
125 /// ツイートへのパーマリンクURLを判定する正規表現
127 public static readonly Regex StatusUrlRegex = new Regex(@"https?://([^.]+\.)?twitter\.com/(#!/)?(?<ScreenName>[a-zA-Z0-9_]+)/status(es)?/(?<StatusId>[0-9]+)(/photo)?", RegexOptions.IgnoreCase);
130 /// attachment_url に指定可能な URL を判定する正規表現
132 public static readonly Regex AttachmentUrlRegex = new Regex(
134 twitter\.com/[0-9A-Za-z_]+/status/[0-9]+
135 | mobile\.twitter\.com/[0-9A-Za-z_]+/status/[0-9]+
136 | twitter\.com/messages/compose\?recipient_id=[0-9]+(&.+)?
138 RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
141 /// FavstarやaclogなどTwitter関連サービスのパーマリンクURLからステータスIDを抽出する正規表現
143 public static readonly Regex ThirdPartyStatusUrlRegex = new Regex(
144 @"https?://(?:[^.]+\.)?(?:
145 favstar\.fm/users/[a-zA-Z0-9_]+/status/ # Favstar
146 | favstar\.fm/t/ # Favstar (short)
147 | aclog\.koba789\.com/i/ # aclog
148 | frtrt\.net/solo_status\.php\?status= # RtRT
149 )(?<StatusId>[0-9]+)",
150 RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
153 /// DM送信かどうかを判定する正規表現
155 public static readonly Regex DMSendTextRegex = new Regex(@"^DM? +(?<id>[a-zA-Z0-9_]+) +(?<body>.*)", RegexOptions.IgnoreCase | RegexOptions.Singleline);
157 public TwitterApi Api { get; }
158 public TwitterConfiguration Configuration { get; private set; }
159 public TwitterTextConfiguration TextConfiguration { get; private set; }
161 public bool GetFollowersSuccess { get; private set; } = false;
162 public bool GetNoRetweetSuccess { get; private set; } = false;
164 delegate void GetIconImageDelegate(PostClass post);
165 private readonly object LockObj = new object();
166 private ISet<long> followerId = new HashSet<long>();
167 private long[] noRTId = Array.Empty<long>();
169 // プロパティからアクセスされる共通情報
170 private readonly List<string> _hashList = new List<string>();
172 private string? nextCursorDirectMessage = null;
174 private long previousStatusId = -1L;
176 public Twitter(TwitterApi api)
179 this.Configuration = TwitterConfiguration.DefaultConfiguration();
180 this.TextConfiguration = TwitterTextConfiguration.DefaultConfiguration();
183 public TwitterApiAccessLevel AccessLevel
184 => MyCommon.TwitterApiInfo.AccessLevel;
186 protected void ResetApiStatus()
187 => MyCommon.TwitterApiInfo.Reset();
189 public void ClearAuthInfo()
191 Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
192 this.ResetApiStatus();
195 public void VerifyCredentials()
199 this.VerifyCredentialsAsync().Wait();
201 catch (AggregateException ex) when (ex.InnerException is WebApiException)
203 throw new WebApiException(ex.InnerException.Message, ex);
207 public async Task VerifyCredentialsAsync()
209 var user = await this.Api.AccountVerifyCredentials()
210 .ConfigureAwait(false);
212 this.UpdateUserStats(user);
215 public void Initialize(string token, string tokenSecret, string username, long userId)
218 if (MyCommon.IsNullOrEmpty(token) || MyCommon.IsNullOrEmpty(tokenSecret) || MyCommon.IsNullOrEmpty(username))
220 Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
222 this.ResetApiStatus();
223 this.Api.Initialize(token, tokenSecret, userId, username);
226 internal static string PreProcessUrl(string orgData)
230 var href = "<a href=\"";
234 if (orgData.IndexOf(href, posl2, StringComparison.Ordinal) > -1)
237 posl1 = orgData.IndexOf(href, posl2, StringComparison.Ordinal);
238 posl1 += href.Length;
239 posl2 = orgData.IndexOf("\"", posl1, StringComparison.Ordinal);
240 var urlStr = orgData.Substring(posl1, posl2 - posl1);
242 if (!urlStr.StartsWith("http://", StringComparison.Ordinal)
243 && !urlStr.StartsWith("https://", StringComparison.Ordinal)
244 && !urlStr.StartsWith("ftp://", StringComparison.Ordinal))
249 var replacedUrl = MyCommon.IDNEncode(urlStr);
250 if (replacedUrl == null) continue;
251 if (replacedUrl == urlStr) continue;
253 orgData = orgData.Replace("<a href=\"" + urlStr, "<a href=\"" + replacedUrl);
264 public async Task<PostClass?> PostStatus(PostStatusParams param)
266 this.CheckAccountState();
268 if (Twitter.DMSendTextRegex.IsMatch(param.Text))
270 var mediaId = param.MediaIds != null && param.MediaIds.Any() ? param.MediaIds[0] : (long?)null;
272 await this.SendDirectMessage(param.Text, mediaId)
273 .ConfigureAwait(false);
277 var response = await this.Api.StatusesUpdate(
279 param.InReplyToStatusId,
281 param.AutoPopulateReplyMetadata,
282 param.ExcludeReplyUserIds,
285 .ConfigureAwait(false);
287 var status = await response.LoadJsonAsync()
288 .ConfigureAwait(false);
290 this.UpdateUserStats(status.User);
292 if (status.Id == this.previousStatusId)
293 throw new WebApiException("OK:Delaying?");
295 this.previousStatusId = status.Id;
298 var post = this.CreatePostsFromStatusData(status);
299 if (this.ReadOwnPost) post.IsRead = true;
303 public async Task<long> UploadMedia(IMediaItem item, string? mediaCategory = null)
305 this.CheckAccountState();
307 var mediaType = item.Extension switch
309 ".png" => "image/png",
310 ".jpg" => "image/jpeg",
311 ".jpeg" => "image/jpeg",
312 ".gif" => "image/gif",
313 _ => "application/octet-stream",
316 var initResponse = await this.Api.MediaUploadInit(item.Size, mediaType, mediaCategory)
317 .ConfigureAwait(false);
319 var initMedia = await initResponse.LoadJsonAsync()
320 .ConfigureAwait(false);
322 var mediaId = initMedia.MediaId;
324 await this.Api.MediaUploadAppend(mediaId, 0, item)
325 .ConfigureAwait(false);
327 var response = await this.Api.MediaUploadFinalize(mediaId)
328 .ConfigureAwait(false);
330 var media = await response.LoadJsonAsync()
331 .ConfigureAwait(false);
333 while (media.ProcessingInfo is TwitterUploadMediaResult.MediaProcessingInfo processingInfo)
335 switch (processingInfo.State)
344 throw new WebApiException($"Err:Upload failed ({processingInfo.Error?.Name})");
346 throw new WebApiException($"Err:Invalid state ({processingInfo.State})");
349 await Task.Delay(TimeSpan.FromSeconds(processingInfo.CheckAfterSecs ?? 5))
350 .ConfigureAwait(false);
352 media = await this.Api.MediaUploadStatus(mediaId)
353 .ConfigureAwait(false);
357 return media.MediaId;
360 public async Task SendDirectMessage(string postStr, long? mediaId = null)
362 this.CheckAccountState();
363 this.CheckAccessLevel(TwitterApiAccessLevel.ReadWriteAndDirectMessage);
365 var mc = Twitter.DMSendTextRegex.Match(postStr);
367 var body = mc.Groups["body"].Value;
368 var recipientName = mc.Groups["id"].Value;
370 var recipient = await this.Api.UsersShow(recipientName)
371 .ConfigureAwait(false);
373 var response = await this.Api.DirectMessagesEventsNew(recipient.Id, body, mediaId)
374 .ConfigureAwait(false);
376 var messageEventSingle = await response.LoadJsonAsync()
377 .ConfigureAwait(false);
379 await this.CreateDirectMessagesEventFromJson(messageEventSingle, read: true)
380 .ConfigureAwait(false);
383 public async Task<PostClass?> PostRetweet(long id, bool read)
385 this.CheckAccountState();
388 var post = TabInformations.GetInstance()[id];
390 throw new WebApiException("Err:Target isn't found.");
392 var target = post.RetweetedId ?? id; // 再RTの場合は元発言をRT
394 var response = await this.Api.StatusesRetweet(target)
395 .ConfigureAwait(false);
397 var status = await response.LoadJsonAsync()
398 .ConfigureAwait(false);
403 if (TabInformations.GetInstance().ContainsKey(status.Id))
408 if (status.RetweetedStatus == null)
409 throw new WebApiException("Invalid Json!");
412 post = this.CreatePostsFromStatusData(status);
419 if (this.ReadOwnPost) post.IsRead = true;
425 public string Username
426 => this.Api.CurrentScreenName;
429 => this.Api.CurrentUserId;
431 public static MyCommon.ACCOUNT_STATE AccountState { get; set; } = MyCommon.ACCOUNT_STATE.Valid;
432 public bool RestrictFavCheck { get; set; }
433 public bool ReadOwnPost { get; set; }
435 public int FollowersCount { get; private set; }
436 public int FriendsCount { get; private set; }
437 public int StatusesCount { get; private set; }
438 public string Location { get; private set; } = "";
439 public string Bio { get; private set; } = "";
441 /// <summary>ユーザーのフォロワー数などの情報を更新します</summary>
442 private void UpdateUserStats(TwitterUser self)
444 this.FollowersCount = self.FollowersCount;
445 this.FriendsCount = self.FriendsCount;
446 this.StatusesCount = self.StatusesCount;
447 this.Location = self.Location ?? "";
448 this.Bio = self.Description ?? "";
452 /// 渡された取得件数がWORKERTYPEに応じた取得可能範囲に収まっているか検証する
454 public static bool VerifyApiResultCount(MyCommon.WORKERTYPE type, int count)
455 => count >= 20 && count <= GetMaxApiResultCount(type);
458 /// 渡された取得件数が更新時の取得可能範囲に収まっているか検証する
460 public static bool VerifyMoreApiResultCount(int count)
461 => count >= 20 && count <= 200;
464 /// 渡された取得件数が起動時の取得可能範囲に収まっているか検証する
466 public static bool VerifyFirstApiResultCount(int count)
467 => count >= 20 && count <= 200;
470 /// WORKERTYPEに応じた取得可能な最大件数を取得する
472 public static int GetMaxApiResultCount(MyCommon.WORKERTYPE type)
474 // 参照: REST APIs - 各endpointのcountパラメータ
475 // https://dev.twitter.com/rest/public
478 MyCommon.WORKERTYPE.Timeline => 200,
479 MyCommon.WORKERTYPE.Reply => 200,
480 MyCommon.WORKERTYPE.UserTimeline => 200,
481 MyCommon.WORKERTYPE.Favorites => 200,
482 MyCommon.WORKERTYPE.List => 200, // 不明
483 MyCommon.WORKERTYPE.PublicSearch => 100,
484 _ => throw new InvalidOperationException("Invalid type: " + type),
489 /// WORKERTYPEに応じた取得件数を取得する
491 public static int GetApiResultCount(MyCommon.WORKERTYPE type, bool more, bool startup)
493 if (SettingManager.Common.UseAdditionalCount)
497 case MyCommon.WORKERTYPE.Favorites:
498 if (SettingManager.Common.FavoritesCountApi != 0)
499 return SettingManager.Common.FavoritesCountApi;
501 case MyCommon.WORKERTYPE.List:
502 if (SettingManager.Common.ListCountApi != 0)
503 return SettingManager.Common.ListCountApi;
505 case MyCommon.WORKERTYPE.PublicSearch:
506 if (SettingManager.Common.SearchCountApi != 0)
507 return SettingManager.Common.SearchCountApi;
509 case MyCommon.WORKERTYPE.UserTimeline:
510 if (SettingManager.Common.UserTimelineCountApi != 0)
511 return SettingManager.Common.UserTimelineCountApi;
514 if (more && SettingManager.Common.MoreCountApi != 0)
516 return Math.Min(SettingManager.Common.MoreCountApi, GetMaxApiResultCount(type));
518 if (startup && SettingManager.Common.FirstCountApi != 0 && type != MyCommon.WORKERTYPE.Reply)
520 return Math.Min(SettingManager.Common.FirstCountApi, GetMaxApiResultCount(type));
525 var count = SettingManager.Common.CountApi;
527 if (type == MyCommon.WORKERTYPE.Reply)
528 count = SettingManager.Common.CountApiReply;
530 return Math.Min(count, GetMaxApiResultCount(type));
533 public async Task GetHomeTimelineApi(bool read, HomeTabModel tab, bool more, bool startup)
535 this.CheckAccountState();
537 var count = GetApiResultCount(MyCommon.WORKERTYPE.Timeline, more, startup);
539 TwitterStatus[] statuses;
542 statuses = await this.Api.StatusesHomeTimeline(count, maxId: tab.OldestId)
543 .ConfigureAwait(false);
547 statuses = await this.Api.StatusesHomeTimeline(count)
548 .ConfigureAwait(false);
551 var minimumId = this.CreatePostsFromJson(statuses, MyCommon.WORKERTYPE.Timeline, tab, read);
552 if (minimumId != null)
553 tab.OldestId = minimumId.Value;
556 public async Task GetMentionsTimelineApi(bool read, MentionsTabModel tab, bool more, bool startup)
558 this.CheckAccountState();
560 var count = GetApiResultCount(MyCommon.WORKERTYPE.Reply, more, startup);
562 TwitterStatus[] statuses;
565 statuses = await this.Api.StatusesMentionsTimeline(count, maxId: tab.OldestId)
566 .ConfigureAwait(false);
570 statuses = await this.Api.StatusesMentionsTimeline(count)
571 .ConfigureAwait(false);
574 var minimumId = this.CreatePostsFromJson(statuses, MyCommon.WORKERTYPE.Reply, tab, read);
575 if (minimumId != null)
576 tab.OldestId = minimumId.Value;
579 public async Task GetUserTimelineApi(bool read, string userName, UserTimelineTabModel tab, bool more)
581 this.CheckAccountState();
583 var count = GetApiResultCount(MyCommon.WORKERTYPE.UserTimeline, more, false);
585 TwitterStatus[] statuses;
586 if (MyCommon.IsNullOrEmpty(userName))
588 var target = tab.ScreenName;
589 if (MyCommon.IsNullOrEmpty(target)) return;
591 statuses = await this.Api.StatusesUserTimeline(userName, count)
592 .ConfigureAwait(false);
598 statuses = await this.Api.StatusesUserTimeline(userName, count, maxId: tab.OldestId)
599 .ConfigureAwait(false);
603 statuses = await this.Api.StatusesUserTimeline(userName, count)
604 .ConfigureAwait(false);
608 var minimumId = this.CreatePostsFromJson(statuses, MyCommon.WORKERTYPE.UserTimeline, tab, read);
610 if (minimumId != null)
611 tab.OldestId = minimumId.Value;
614 public async Task<PostClass> GetStatusApi(bool read, long id)
616 this.CheckAccountState();
618 var status = await this.Api.StatusesShow(id)
619 .ConfigureAwait(false);
621 var item = this.CreatePostsFromStatusData(status);
624 if (item.IsMe && !read && this.ReadOwnPost) item.IsRead = true;
629 public async Task GetStatusApi(bool read, long id, TabModel tab)
631 var post = await this.GetStatusApi(read, id)
632 .ConfigureAwait(false);
634 // 非同期アイコン取得&StatusDictionaryに追加
635 if (tab != null && tab.IsInnerStorageTabType)
636 tab.AddPostQueue(post);
638 TabInformations.GetInstance().AddPost(post);
641 private PostClass CreatePostsFromStatusData(TwitterStatus status)
642 => this.CreatePostsFromStatusData(status, false);
644 private PostClass CreatePostsFromStatusData(TwitterStatus status, bool favTweet)
646 var post = new PostClass();
647 TwitterEntities entities;
650 post.StatusId = status.Id;
651 if (status.RetweetedStatus != null)
653 var retweeted = status.RetweetedStatus;
655 post.CreatedAt = MyCommon.DateTimeParse(retweeted.CreatedAt);
658 post.RetweetedId = retweeted.Id;
660 post.TextFromApi = retweeted.FullText;
661 entities = retweeted.MergedEntities;
662 sourceHtml = retweeted.Source;
664 post.InReplyToStatusId = retweeted.InReplyToStatusId;
665 post.InReplyToUser = retweeted.InReplyToScreenName;
666 post.InReplyToUserId = status.InReplyToUserId;
675 var tc = TabInformations.GetInstance().FavoriteTab;
676 post.IsFav = tc.Contains(retweeted.Id);
679 if (retweeted.Coordinates != null)
680 post.PostGeo = new PostClass.StatusGeo(retweeted.Coordinates.Coordinates[0], retweeted.Coordinates.Coordinates[1]);
683 var user = retweeted.User;
686 post.UserId = user.Id;
687 post.ScreenName = user.ScreenName;
688 post.Nickname = user.Name.Trim();
689 post.ImageUrl = user.ProfileImageUrlHttps;
690 post.IsProtect = user.Protected;
695 post.ScreenName = "?????";
696 post.Nickname = "Unknown User";
700 if (status.User != null)
702 post.RetweetedBy = status.User.ScreenName;
703 post.RetweetedByUserId = status.User.Id;
704 post.IsMe = post.RetweetedByUserId == this.UserId;
708 post.RetweetedBy = "?????";
709 post.RetweetedByUserId = 0L;
714 post.CreatedAt = MyCommon.DateTimeParse(status.CreatedAt);
716 post.TextFromApi = status.FullText;
717 entities = status.MergedEntities;
718 sourceHtml = status.Source;
719 post.InReplyToStatusId = status.InReplyToStatusId;
720 post.InReplyToUser = status.InReplyToScreenName;
721 post.InReplyToUserId = status.InReplyToUserId;
730 var tc = TabInformations.GetInstance().FavoriteTab;
731 post.IsFav = tc.Posts.TryGetValue(post.StatusId, out var tabinfoPost) && tabinfoPost.IsFav;
734 if (status.Coordinates != null)
735 post.PostGeo = new PostClass.StatusGeo(status.Coordinates.Coordinates[0], status.Coordinates.Coordinates[1]);
738 var user = status.User;
741 post.UserId = user.Id;
742 post.ScreenName = user.ScreenName;
743 post.Nickname = user.Name.Trim();
744 post.ImageUrl = user.ProfileImageUrlHttps;
745 post.IsProtect = user.Protected;
746 post.IsMe = post.UserId == this.UserId;
751 post.ScreenName = "?????";
752 post.Nickname = "Unknown User";
756 var textFromApi = post.TextFromApi;
758 var quotedStatusLink = (status.RetweetedStatus ?? status).QuotedStatusPermalink;
760 if (quotedStatusLink != null && entities.Urls.Any(x => x.ExpandedUrl == quotedStatusLink.Expanded))
761 quotedStatusLink = null; // 移行期は entities.urls と quoted_status_permalink の両方に含まれる場合がある
763 post.Text = CreateHtmlAnchor(textFromApi, entities, quotedStatusLink);
764 post.TextFromApi = textFromApi;
765 post.TextFromApi = this.ReplaceTextFromApi(post.TextFromApi, entities, quotedStatusLink);
766 post.TextFromApi = WebUtility.HtmlDecode(post.TextFromApi);
767 post.TextFromApi = post.TextFromApi.Replace("<3", "\u2661");
768 post.AccessibleText = CreateAccessibleText(textFromApi, entities, (status.RetweetedStatus ?? status).QuotedStatus, quotedStatusLink);
769 post.AccessibleText = WebUtility.HtmlDecode(post.AccessibleText);
770 post.AccessibleText = post.AccessibleText.Replace("<3", "\u2661");
772 this.ExtractEntities(entities, post.ReplyToList, post.Media);
774 post.QuoteStatusIds = GetQuoteTweetStatusIds(entities, quotedStatusLink)
775 .Where(x => x != post.StatusId && x != post.RetweetedId)
776 .Distinct().ToArray();
778 post.ExpandedUrls = entities.OfType<TwitterEntityUrl>()
779 .Select(x => new PostClass.ExpandedUrlInfo(x.Url, x.ExpandedUrl))
782 // メモリ使用量削減 (同一のテキストであれば同一の string インスタンスを参照させる)
783 if (post.Text == post.TextFromApi)
784 post.Text = post.TextFromApi;
785 if (post.AccessibleText == post.TextFromApi)
786 post.AccessibleText = post.TextFromApi;
788 // 他の発言と重複しやすい (共通化できる) 文字列は string.Intern を通す
789 post.ScreenName = string.Intern(post.ScreenName);
790 post.Nickname = string.Intern(post.Nickname);
791 post.ImageUrl = string.Intern(post.ImageUrl);
792 post.RetweetedBy = post.RetweetedBy != null ? string.Intern(post.RetweetedBy) : null;
795 var (sourceText, sourceUri) = ParseSource(sourceHtml);
796 post.Source = string.Intern(sourceText);
797 post.SourceUri = sourceUri;
799 post.IsReply = post.RetweetedId == null && post.ReplyToList.Any(x => x.UserId == this.UserId);
800 post.IsExcludeReply = false;
808 if (this.followerId.Count > 0) post.IsOwl = !this.followerId.Contains(post.UserId);
816 /// ツイートに含まれる引用ツイートのURLからステータスIDを抽出
818 public static IEnumerable<long> GetQuoteTweetStatusIds(IEnumerable<TwitterEntity>? entities, TwitterQuotedStatusPermalink? quotedStatusLink)
820 entities ??= Enumerable.Empty<TwitterEntity>();
822 var urls = entities.OfType<TwitterEntityUrl>().Select(x => x.ExpandedUrl);
824 if (quotedStatusLink != null)
825 urls = urls.Append(quotedStatusLink.Expanded);
827 return GetQuoteTweetStatusIds(urls);
830 public static IEnumerable<long> GetQuoteTweetStatusIds(IEnumerable<string> urls)
832 foreach (var url in urls)
834 var match = Twitter.StatusUrlRegex.Match(url);
837 if (long.TryParse(match.Groups["StatusId"].Value, out var statusId))
838 yield return statusId;
843 private long? CreatePostsFromJson(TwitterStatus[] items, MyCommon.WORKERTYPE gType, TabModel? tab, bool read)
845 long? minimumId = null;
847 foreach (var status in items)
849 if (minimumId == null || minimumId.Value > status.Id)
850 minimumId = status.Id;
857 if (TabInformations.GetInstance().ContainsKey(status.Id)) continue;
861 if (tab.Contains(status.Id)) continue;
866 if (gType != MyCommon.WORKERTYPE.UserTimeline &&
867 status.RetweetedStatus != null && this.noRTId.Contains(status.User.Id)) continue;
869 var post = this.CreatePostsFromStatusData(status);
872 if (post.IsMe && !read && this.ReadOwnPost) post.IsRead = true;
874 if (tab != null && tab.IsInnerStorageTabType)
875 tab.AddPostQueue(post);
877 TabInformations.GetInstance().AddPost(post);
883 private long? CreatePostsFromSearchJson(TwitterSearchResult items, PublicSearchTabModel tab, bool read, bool more)
885 long? minimumId = null;
887 foreach (var status in items.Statuses)
889 if (minimumId == null || minimumId.Value > status.Id)
890 minimumId = status.Id;
892 if (!more && status.Id > tab.SinceId) tab.SinceId = status.Id;
896 if (tab.Contains(status.Id)) continue;
899 var post = this.CreatePostsFromStatusData(status);
902 if ((post.IsMe && !read) && this.ReadOwnPost) post.IsRead = true;
904 tab.AddPostQueue(post);
910 private long? CreateFavoritePostsFromJson(TwitterStatus[] items, bool read)
912 var favTab = TabInformations.GetInstance().FavoriteTab;
913 long? minimumId = null;
915 foreach (var status in items)
917 if (minimumId == null || minimumId.Value > status.Id)
918 minimumId = status.Id;
923 if (favTab.Contains(status.Id)) continue;
926 var post = this.CreatePostsFromStatusData(status, true);
930 TabInformations.GetInstance().AddPost(post);
936 public async Task GetListStatus(bool read, ListTimelineTabModel tab, bool more, bool startup)
938 var count = GetApiResultCount(MyCommon.WORKERTYPE.List, more, startup);
940 TwitterStatus[] statuses;
943 statuses = await this.Api.ListsStatuses(tab.ListInfo.Id, count, maxId: tab.OldestId, includeRTs: SettingManager.Common.IsListsIncludeRts)
944 .ConfigureAwait(false);
948 statuses = await this.Api.ListsStatuses(tab.ListInfo.Id, count, includeRTs: SettingManager.Common.IsListsIncludeRts)
949 .ConfigureAwait(false);
952 var minimumId = this.CreatePostsFromJson(statuses, MyCommon.WORKERTYPE.List, tab, read);
954 if (minimumId != null)
955 tab.OldestId = minimumId.Value;
959 /// startStatusId からリプライ先の発言を辿る。発言は posts 以外からは検索しない。
961 /// <returns>posts の中から検索されたリプライチェインの末端</returns>
962 internal static PostClass FindTopOfReplyChain(IDictionary<long, PostClass> posts, long startStatusId)
964 if (!posts.ContainsKey(startStatusId))
965 throw new ArgumentException("startStatusId (" + startStatusId + ") が posts の中から見つかりませんでした。", nameof(startStatusId));
967 var nextPost = posts[startStatusId];
968 while (nextPost.InReplyToStatusId != null)
970 if (!posts.ContainsKey(nextPost.InReplyToStatusId.Value))
972 nextPost = posts[nextPost.InReplyToStatusId.Value];
978 public async Task GetRelatedResult(bool read, RelatedPostsTabModel tab)
980 var targetPost = tab.TargetPost;
981 var relPosts = new Dictionary<long, PostClass>();
982 if (targetPost.TextFromApi.Contains("@") && targetPost.InReplyToStatusId == null)
985 var p = TabInformations.GetInstance()[targetPost.StatusId];
986 if (p != null && p.InReplyToStatusId != null)
992 p = await this.GetStatusApi(read, targetPost.StatusId)
993 .ConfigureAwait(false);
997 relPosts.Add(targetPost.StatusId, targetPost);
999 Exception? lastException = null;
1001 // in_reply_to_status_id を使用してリプライチェインを辿る
1002 var nextPost = FindTopOfReplyChain(relPosts, targetPost.StatusId);
1004 while (nextPost.InReplyToStatusId != null && loopCount++ <= 20)
1006 var inReplyToId = nextPost.InReplyToStatusId.Value;
1008 var inReplyToPost = TabInformations.GetInstance()[inReplyToId];
1009 if (inReplyToPost == null)
1013 inReplyToPost = await this.GetStatusApi(read, inReplyToId)
1014 .ConfigureAwait(false);
1016 catch (WebApiException ex)
1023 relPosts.Add(inReplyToPost.StatusId, inReplyToPost);
1025 nextPost = FindTopOfReplyChain(relPosts, nextPost.StatusId);
1028 // MRTとかに対応のためツイート内にあるツイートを指すURLを取り込む
1029 var text = targetPost.Text;
1030 var ma = Twitter.StatusUrlRegex.Matches(text).Cast<Match>()
1031 .Concat(Twitter.ThirdPartyStatusUrlRegex.Matches(text).Cast<Match>());
1032 foreach (var _match in ma)
1034 if (long.TryParse(_match.Groups["StatusId"].Value, out var _statusId))
1036 if (relPosts.ContainsKey(_statusId))
1039 var p = TabInformations.GetInstance()[_statusId];
1044 p = await this.GetStatusApi(read, _statusId)
1045 .ConfigureAwait(false);
1047 catch (WebApiException ex)
1055 relPosts.Add(p.StatusId, p);
1059 relPosts.Values.ToList().ForEach(p =>
1061 if (p.IsMe && !read && this.ReadOwnPost)
1066 tab.AddPostQueue(p);
1069 if (lastException != null)
1070 throw new WebApiException(lastException.Message, lastException);
1073 public async Task GetSearch(bool read, PublicSearchTabModel tab, bool more)
1075 var count = GetApiResultCount(MyCommon.WORKERTYPE.PublicSearch, more, false);
1078 long? sinceId = null;
1081 maxId = tab.OldestId - 1;
1085 sinceId = tab.SinceId;
1088 var searchResult = await this.Api.SearchTweets(tab.SearchWords, tab.SearchLang, count, maxId, sinceId)
1089 .ConfigureAwait(false);
1091 if (!TabInformations.GetInstance().ContainsTab(tab))
1094 var minimumId = this.CreatePostsFromSearchJson(searchResult, tab, read, more);
1096 if (minimumId != null)
1097 tab.OldestId = minimumId.Value;
1100 public async Task GetDirectMessageEvents(bool read, bool backward)
1102 this.CheckAccountState();
1103 this.CheckAccessLevel(TwitterApiAccessLevel.ReadWriteAndDirectMessage);
1107 TwitterMessageEventList eventList;
1110 eventList = await this.Api.DirectMessagesEventsList(count, this.nextCursorDirectMessage)
1111 .ConfigureAwait(false);
1115 eventList = await this.Api.DirectMessagesEventsList(count)
1116 .ConfigureAwait(false);
1119 this.nextCursorDirectMessage = eventList.NextCursor;
1121 await this.CreateDirectMessagesEventFromJson(eventList, read)
1122 .ConfigureAwait(false);
1125 private async Task CreateDirectMessagesEventFromJson(TwitterMessageEventSingle eventSingle, bool read)
1127 var eventList = new TwitterMessageEventList
1129 Apps = new Dictionary<string, TwitterMessageEventList.App>(),
1130 Events = new[] { eventSingle.Event },
1133 await this.CreateDirectMessagesEventFromJson(eventList, read)
1134 .ConfigureAwait(false);
1137 private async Task CreateDirectMessagesEventFromJson(TwitterMessageEventList eventList, bool read)
1139 var events = eventList.Events
1140 .Where(x => x.Type == "message_create")
1143 if (events.Length == 0)
1146 var userIds = Enumerable.Concat(
1147 events.Select(x => x.MessageCreate.SenderId),
1148 events.Select(x => x.MessageCreate.Target.RecipientId)
1149 ).Distinct().ToArray();
1151 var users = (await this.Api.UsersLookup(userIds).ConfigureAwait(false))
1152 .ToDictionary(x => x.IdStr);
1154 var apps = eventList.Apps ?? new Dictionary<string, TwitterMessageEventList.App>();
1156 this.CreateDirectMessagesEventFromJson(events, users, apps, read);
1159 private void CreateDirectMessagesEventFromJson(
1160 IEnumerable<TwitterMessageEvent> events,
1161 IReadOnlyDictionary<string, TwitterUser> users,
1162 IReadOnlyDictionary<string, TwitterMessageEventList.App> apps,
1165 foreach (var eventItem in events)
1167 var post = new PostClass();
1168 post.StatusId = long.Parse(eventItem.Id);
1170 var timestamp = long.Parse(eventItem.CreatedTimestamp);
1171 post.CreatedAt = DateTimeUtc.UnixEpoch + TimeSpan.FromTicks(timestamp * TimeSpan.TicksPerMillisecond);
1173 var textFromApi = eventItem.MessageCreate.MessageData.Text;
1175 var entities = eventItem.MessageCreate.MessageData.Entities;
1176 var mediaEntity = eventItem.MessageCreate.MessageData.Attachment?.Media;
1178 if (mediaEntity != null)
1179 entities.Media = new[] { mediaEntity };
1182 post.Text = CreateHtmlAnchor(textFromApi, entities, quotedStatusLink: null);
1183 post.TextFromApi = this.ReplaceTextFromApi(textFromApi, entities, quotedStatusLink: null);
1184 post.TextFromApi = WebUtility.HtmlDecode(post.TextFromApi);
1185 post.TextFromApi = post.TextFromApi.Replace("<3", "\u2661");
1186 post.AccessibleText = CreateAccessibleText(textFromApi, entities, quotedStatus: null, quotedStatusLink: null);
1187 post.AccessibleText = WebUtility.HtmlDecode(post.AccessibleText);
1188 post.AccessibleText = post.AccessibleText.Replace("<3", "\u2661");
1191 this.ExtractEntities(entities, post.ReplyToList, post.Media);
1193 post.QuoteStatusIds = GetQuoteTweetStatusIds(entities, quotedStatusLink: null)
1194 .Distinct().ToArray();
1196 post.ExpandedUrls = entities.OfType<TwitterEntityUrl>()
1197 .Select(x => new PostClass.ExpandedUrlInfo(x.Url, x.ExpandedUrl))
1202 if (eventItem.MessageCreate.SenderId != this.Api.CurrentUserId.ToString(CultureInfo.InvariantCulture))
1204 userId = eventItem.MessageCreate.SenderId;
1210 userId = eventItem.MessageCreate.Target.RecipientId;
1215 if (!users.TryGetValue(userId, out var user))
1218 post.UserId = user.Id;
1219 post.ScreenName = user.ScreenName;
1220 post.Nickname = user.Name.Trim();
1221 post.ImageUrl = user.ProfileImageUrlHttps;
1222 post.IsProtect = user.Protected;
1224 // メモリ使用量削減 (同一のテキストであれば同一の string インスタンスを参照させる)
1225 if (post.Text == post.TextFromApi)
1226 post.Text = post.TextFromApi;
1227 if (post.AccessibleText == post.TextFromApi)
1228 post.AccessibleText = post.TextFromApi;
1230 // 他の発言と重複しやすい (共通化できる) 文字列は string.Intern を通す
1231 post.ScreenName = string.Intern(post.ScreenName);
1232 post.Nickname = string.Intern(post.Nickname);
1233 post.ImageUrl = string.Intern(post.ImageUrl);
1235 var appId = eventItem.MessageCreate.SourceAppId;
1236 if (appId != null && apps.TryGetValue(appId, out var app))
1238 post.Source = string.Intern(app.Name);
1242 post.SourceUri = new Uri(SourceUriBase, app.Url);
1244 catch (UriFormatException) { }
1248 if (post.IsMe && !read && this.ReadOwnPost)
1250 post.IsReply = false;
1251 post.IsExcludeReply = false;
1254 var dmTab = TabInformations.GetInstance().DirectMessageTab;
1255 dmTab.AddPostQueue(post);
1259 public async Task GetFavoritesApi(bool read, FavoritesTabModel tab, bool backward)
1261 this.CheckAccountState();
1263 var count = GetApiResultCount(MyCommon.WORKERTYPE.Favorites, backward, false);
1265 TwitterStatus[] statuses;
1268 statuses = await this.Api.FavoritesList(count, maxId: tab.OldestId)
1269 .ConfigureAwait(false);
1273 statuses = await this.Api.FavoritesList(count)
1274 .ConfigureAwait(false);
1277 var minimumId = this.CreateFavoritePostsFromJson(statuses, read);
1279 if (minimumId != null)
1280 tab.OldestId = minimumId.Value;
1283 private string ReplaceTextFromApi(string text, TwitterEntities? entities, TwitterQuotedStatusPermalink? quotedStatusLink)
1285 if (entities != null)
1287 if (entities.Urls != null)
1289 foreach (var m in entities.Urls)
1291 if (!MyCommon.IsNullOrEmpty(m.DisplayUrl)) text = text.Replace(m.Url, m.DisplayUrl);
1294 if (entities.Media != null)
1296 foreach (var m in entities.Media)
1298 if (!MyCommon.IsNullOrEmpty(m.DisplayUrl)) text = text.Replace(m.Url, m.DisplayUrl);
1303 if (quotedStatusLink != null)
1304 text += " " + quotedStatusLink.Display;
1309 internal static string CreateAccessibleText(string text, TwitterEntities? entities, TwitterStatus? quotedStatus, TwitterQuotedStatusPermalink? quotedStatusLink)
1311 if (entities == null)
1314 if (entities.Urls != null)
1316 foreach (var entity in entities.Urls)
1318 if (quotedStatus != null)
1320 var matchStatusUrl = Twitter.StatusUrlRegex.Match(entity.ExpandedUrl);
1321 if (matchStatusUrl.Success && matchStatusUrl.Groups["StatusId"].Value == quotedStatus.IdStr)
1323 var quotedText = CreateAccessibleText(quotedStatus.FullText, quotedStatus.MergedEntities, quotedStatus: null, quotedStatusLink: null);
1324 text = text.Replace(entity.Url, string.Format(Properties.Resources.QuoteStatus_AccessibleText, quotedStatus.User.ScreenName, quotedText));
1329 if (!MyCommon.IsNullOrEmpty(entity.DisplayUrl))
1330 text = text.Replace(entity.Url, entity.DisplayUrl);
1334 if (entities.Media != null)
1336 foreach (var entity in entities.Media)
1338 if (!MyCommon.IsNullOrEmpty(entity.AltText))
1340 text = text.Replace(entity.Url, string.Format(Properties.Resources.ImageAltText, entity.AltText));
1342 else if (!MyCommon.IsNullOrEmpty(entity.DisplayUrl))
1344 text = text.Replace(entity.Url, entity.DisplayUrl);
1349 if (quotedStatus != null && quotedStatusLink != null)
1351 var quoteText = CreateAccessibleText(quotedStatus.FullText, quotedStatus.MergedEntities, quotedStatus: null, quotedStatusLink: null);
1352 text += " " + string.Format(Properties.Resources.QuoteStatus_AccessibleText, quotedStatus.User.ScreenName, quoteText);
1361 /// <exception cref="WebApiException"/>
1362 public async Task RefreshFollowerIds()
1364 if (MyCommon._endingFlag) return;
1367 var newFollowerIds = Enumerable.Empty<long>();
1370 var ret = await this.Api.FollowersIds(cursor)
1371 .ConfigureAwait(false);
1373 if (ret.Ids == null)
1374 throw new WebApiException("ret.ids == null");
1376 newFollowerIds = newFollowerIds.Concat(ret.Ids);
1377 cursor = ret.NextCursor;
1378 } while (cursor != 0);
1380 this.followerId = newFollowerIds.ToHashSet();
1381 TabInformations.GetInstance().RefreshOwl(this.followerId);
1383 this.GetFollowersSuccess = true;
1387 /// RT 非表示ユーザーを更新します
1389 /// <exception cref="WebApiException"/>
1390 public async Task RefreshNoRetweetIds()
1392 if (MyCommon._endingFlag) return;
1394 this.noRTId = await this.Api.NoRetweetIds()
1395 .ConfigureAwait(false);
1397 this.GetNoRetweetSuccess = true;
1401 /// t.co の文字列長などの設定情報を更新します
1403 /// <exception cref="WebApiException"/>
1404 public async Task RefreshConfiguration()
1406 this.Configuration = await this.Api.Configuration()
1407 .ConfigureAwait(false);
1409 // TextConfiguration 相当の JSON を得る API が存在しないため、TransformedURLLength のみ help/configuration.json に合わせて更新する
1410 this.TextConfiguration.TransformedURLLength = this.Configuration.ShortUrlLengthHttps;
1413 public async Task GetListsApi()
1415 this.CheckAccountState();
1417 var ownedLists = await TwitterLists.GetAllItemsAsync(x =>
1418 this.Api.ListsOwnerships(this.Username, cursor: x, count: 1000))
1419 .ConfigureAwait(false);
1421 var subscribedLists = await TwitterLists.GetAllItemsAsync(x =>
1422 this.Api.ListsSubscriptions(this.Username, cursor: x, count: 1000))
1423 .ConfigureAwait(false);
1425 TabInformations.GetInstance().SubscribableLists = Enumerable.Concat(ownedLists, subscribedLists)
1426 .Select(x => new ListElement(x, this))
1430 public async Task DeleteList(long listId)
1432 await this.Api.ListsDestroy(listId)
1434 .ConfigureAwait(false);
1436 var tabinfo = TabInformations.GetInstance();
1438 tabinfo.SubscribableLists = tabinfo.SubscribableLists
1439 .Where(x => x.Id != listId)
1443 public async Task<ListElement> EditList(long listId, string new_name, bool isPrivate, string description)
1445 var response = await this.Api.ListsUpdate(listId, new_name, description, isPrivate)
1446 .ConfigureAwait(false);
1448 var list = await response.LoadJsonAsync()
1449 .ConfigureAwait(false);
1451 return new ListElement(list, this);
1454 public async Task<long> GetListMembers(long listId, List<UserInfo> lists, long cursor)
1456 this.CheckAccountState();
1458 var users = await this.Api.ListsMembers(listId, cursor)
1459 .ConfigureAwait(false);
1461 Array.ForEach(users.Users, u => lists.Add(new UserInfo(u)));
1463 return users.NextCursor;
1466 public async Task CreateListApi(string listName, bool isPrivate, string description)
1468 this.CheckAccountState();
1470 var response = await this.Api.ListsCreate(listName, description, isPrivate)
1471 .ConfigureAwait(false);
1473 var list = await response.LoadJsonAsync()
1474 .ConfigureAwait(false);
1476 TabInformations.GetInstance().SubscribableLists.Add(new ListElement(list, this));
1479 public async Task<bool> ContainsUserAtList(long listId, string user)
1481 this.CheckAccountState();
1485 await this.Api.ListsMembersShow(listId, user)
1486 .ConfigureAwait(false);
1490 catch (TwitterApiException ex)
1491 when (ex.Errors.Any(x => x.Code == TwitterErrorCode.NotFound))
1497 private void ExtractEntities(TwitterEntities? entities, List<(long UserId, string ScreenName)> AtList, List<MediaInfo> media)
1499 if (entities != null)
1501 if (entities.Hashtags != null)
1505 this._hashList.AddRange(entities.Hashtags.Select(x => "#" + x.Text));
1508 if (entities.UserMentions != null)
1510 foreach (var ent in entities.UserMentions)
1512 AtList.Add((ent.Id, ent.ScreenName));
1515 if (entities.Media != null)
1519 foreach (var ent in entities.Media)
1521 if (!media.Any(x => x.Url == ent.MediaUrlHttps))
1523 if (ent.VideoInfo != null &&
1524 ent.Type == "animated_gif" || ent.Type == "video")
1526 media.Add(new MediaInfo(ent.MediaUrlHttps, ent.AltText, ent.ExpandedUrl));
1529 media.Add(new MediaInfo(ent.MediaUrlHttps, ent.AltText, videoUrl: null));
1537 internal static string CreateHtmlAnchor(string text, TwitterEntities? entities, TwitterQuotedStatusPermalink? quotedStatusLink)
1539 var mergedEntities = entities.Concat(TweetExtractor.ExtractEmojiEntities(text));
1541 // PostClass.ExpandedUrlInfo を使用して非同期に URL 展開を行うためここでは expanded_url を使用しない
1542 text = TweetFormatter.AutoLinkHtml(text, mergedEntities, keepTco: true);
1544 text = Regex.Replace(text, "(^|[^a-zA-Z0-9_/&##@@>=.~])(sm|nm)([0-9]{1,10})", "$1<a href=\"https://www.nicovideo.jp/watch/$2$3\">$2$3</a>");
1545 text = PreProcessUrl(text); // IDN置換
1547 if (quotedStatusLink != null)
1549 text += string.Format(" <a href=\"{0}\" title=\"{0}\">{1}</a>",
1550 WebUtility.HtmlEncode(quotedStatusLink.Url),
1551 WebUtility.HtmlEncode(quotedStatusLink.Display));
1557 private static readonly Uri SourceUriBase = new Uri("https://twitter.com/");
1560 /// Twitter APIから得たHTML形式のsource文字列を分析し、source名とURLに分離します
1562 internal static (string SourceText, Uri? SourceUri) ParseSource(string? sourceHtml)
1564 if (MyCommon.IsNullOrEmpty(sourceHtml))
1570 // sourceHtmlの例: <a href="http://twitter.com" rel="nofollow">Twitter Web Client</a>
1572 var match = Regex.Match(sourceHtml, "^<a href=\"(?<uri>.+?)\".*?>(?<text>.+)</a>$", RegexOptions.IgnoreCase);
1575 sourceText = WebUtility.HtmlDecode(match.Groups["text"].Value);
1578 var uriStr = WebUtility.HtmlDecode(match.Groups["uri"].Value);
1579 sourceUri = new Uri(SourceUriBase, uriStr);
1581 catch (UriFormatException)
1588 sourceText = WebUtility.HtmlDecode(sourceHtml);
1592 return (sourceText, sourceUri);
1595 public async Task<TwitterApiStatus?> GetInfoApi()
1597 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return null;
1599 if (MyCommon._endingFlag) return null;
1601 var limits = await this.Api.ApplicationRateLimitStatus()
1602 .ConfigureAwait(false);
1604 MyCommon.TwitterApiInfo.UpdateFromJson(limits);
1606 return MyCommon.TwitterApiInfo;
1610 /// ブロック中のユーザーを更新します
1612 /// <exception cref="WebApiException"/>
1613 public async Task RefreshBlockIds()
1615 if (MyCommon._endingFlag) return;
1618 var newBlockIds = Enumerable.Empty<long>();
1621 var ret = await this.Api.BlocksIds(cursor)
1622 .ConfigureAwait(false);
1624 newBlockIds = newBlockIds.Concat(ret.Ids);
1625 cursor = ret.NextCursor;
1626 } while (cursor != 0);
1628 var blockIdsSet = newBlockIds.ToHashSet();
1629 blockIdsSet.Remove(this.UserId); // 元のソースにあったので一応残しておく
1631 TabInformations.GetInstance().BlockIds = blockIdsSet;
1635 /// ミュート中のユーザーIDを更新します
1637 /// <exception cref="WebApiException"/>
1638 public async Task RefreshMuteUserIdsAsync()
1640 if (MyCommon._endingFlag) return;
1642 var ids = await TwitterIds.GetAllItemsAsync(x => this.Api.MutesUsersIds(x))
1643 .ConfigureAwait(false);
1645 TabInformations.GetInstance().MuteUserIds = ids.ToHashSet();
1648 public string[] GetHashList()
1653 hashArray = this._hashList.ToArray();
1654 this._hashList.Clear();
1659 public string AccessToken
1660 => ((TwitterApiConnection)this.Api.Connection).AccessToken;
1662 public string AccessTokenSecret
1663 => ((TwitterApiConnection)this.Api.Connection).AccessSecret;
1665 private void CheckAccountState()
1667 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
1668 throw new WebApiException("Auth error. Check your account");
1671 private void CheckAccessLevel(TwitterApiAccessLevel accessLevelFlags)
1673 if (!this.AccessLevel.HasFlag(accessLevelFlags))
1674 throw new WebApiException("Auth Err:try to re-authorization.");
1677 public int GetTextLengthRemain(string postText)
1679 var matchDm = Twitter.DMSendTextRegex.Match(postText);
1680 if (matchDm.Success)
1681 return this.GetTextLengthRemainDM(matchDm.Groups["body"].Value);
1683 return this.GetTextLengthRemainWeighted(postText);
1686 private int GetTextLengthRemainDM(string postText)
1691 while (pos < postText.Length)
1695 if (char.IsSurrogatePair(postText, pos))
1696 pos += 2; // サロゲートペアの場合は2文字分進める
1701 var urls = TweetExtractor.ExtractUrls(postText);
1702 foreach (var url in urls)
1704 var shortUrlLength = url.StartsWith("https://", StringComparison.OrdinalIgnoreCase)
1705 ? this.Configuration.ShortUrlLengthHttps
1706 : this.Configuration.ShortUrlLength;
1708 textLength += shortUrlLength - url.Length;
1711 return this.Configuration.DmTextCharacterLimit - textLength;
1714 private int GetTextLengthRemainWeighted(string postText)
1716 var config = this.TextConfiguration;
1717 var totalWeight = 0;
1719 int GetWeightFromCodepoint(int codepoint)
1721 foreach (var weightRange in config.Ranges)
1723 if (codepoint >= weightRange.Start && codepoint <= weightRange.End)
1724 return weightRange.Weight;
1727 return config.DefaultWeight;
1730 var urls = TweetExtractor.ExtractUrlEntities(postText).ToArray();
1731 var emojis = config.EmojiParsingEnabled
1732 ? TweetExtractor.ExtractEmojiEntities(postText).ToArray()
1733 : Array.Empty<TwitterEntityEmoji>();
1735 var codepoints = postText.ToCodepoints().ToArray();
1737 while (index < codepoints.Length)
1739 var urlEntity = urls.FirstOrDefault(x => x.Indices[0] == index);
1740 if (urlEntity != null)
1742 totalWeight += config.TransformedURLLength * config.Scale;
1743 index = urlEntity.Indices[1];
1747 var emojiEntity = emojis.FirstOrDefault(x => x.Indices[0] == index);
1748 if (emojiEntity != null)
1750 totalWeight += GetWeightFromCodepoint(codepoints[index]);
1751 index = emojiEntity.Indices[1];
1755 var codepoint = codepoints[index];
1756 totalWeight += GetWeightFromCodepoint(codepoint);
1761 var remainWeight = config.MaxWeightedTweetLength * config.Scale - totalWeight;
1763 return remainWeight / config.Scale;
1766 public bool IsDisposed { get; private set; } = false;
1768 protected virtual void Dispose(bool disposing)
1770 if (this.IsDisposed)
1778 this.IsDisposed = true;
1781 public void Dispose()
1784 GC.SuppressFinalize(this);