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.
28 using System.Diagnostics;
32 using System.Net.Http;
33 using System.Runtime.CompilerServices;
34 using System.Runtime.Serialization;
35 using System.Runtime.Serialization.Json;
37 using System.Text.RegularExpressions;
38 using System.Threading;
39 using System.Threading.Tasks;
42 using System.Xml.Linq;
43 using System.Xml.XPath;
45 using System.Reflection;
46 using System.Collections.Generic;
47 using System.Windows.Forms;
49 using OpenTween.Api.DataModel;
50 using OpenTween.Connection;
51 using OpenTween.Models;
52 using OpenTween.Setting;
56 public class Twitter : IDisposable
58 #region Regexp from twitter-text-js
60 // The code in this region code block incorporates works covered by
61 // the following copyright and permission notices:
63 // Copyright 2011 Twitter, Inc.
65 // Licensed under the Apache License, Version 2.0 (the "License"); you
66 // may not use this work except in compliance with the License. You
67 // may obtain a copy of the License in the LICENSE file, or at:
69 // http://www.apache.org/licenses/LICENSE-2.0
71 // Unless required by applicable law or agreed to in writing, software
72 // distributed under the License is distributed on an "AS IS" BASIS,
73 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
74 // implied. See the License for the specific language governing
75 // permissions and limitations under the License.
78 private const string LATIN_ACCENTS = @"\u00c0-\u00d6\u00d8-\u00f6\u00f8-\u00ff\u0100-\u024f\u0253\u0254\u0256\u0257\u0259\u025b\u0263\u0268\u026f\u0272\u0289\u028b\u02bb\u1e00-\u1eff";
79 private const string NON_LATIN_HASHTAG_CHARS = @"\u0400-\u04ff\u0500-\u0527\u1100-\u11ff\u3130-\u3185\uA960-\uA97F\uAC00-\uD7AF\uD7B0-\uD7FF";
80 //private const string CJ_HASHTAG_CHARACTERS = @"\u30A1-\u30FA\uFF66-\uFF9F\uFF10-\uFF19\uFF21-\uFF3A\uFF41-\uFF5A\u3041-\u3096\u3400-\u4DBF\u4E00-\u9FFF\u20000-\u2A6DF\u2A700-\u2B73F\u2B740-\u2B81F\u2F800-\u2FA1F";
81 private const string CJ_HASHTAG_CHARACTERS = @"\u30A1-\u30FA\u30FC\u3005\uFF66-\uFF9F\uFF10-\uFF19\uFF21-\uFF3A\uFF41-\uFF5A\u3041-\u309A\u3400-\u4DBF\p{IsCJKUnifiedIdeographs}";
82 private const string HASHTAG_BOUNDARY = @"^|$|\s|「|」|。|\.|!";
83 private const string HASHTAG_ALPHA = "[a-z_" + LATIN_ACCENTS + NON_LATIN_HASHTAG_CHARS + CJ_HASHTAG_CHARACTERS + "]";
84 private const string HASHTAG_ALPHANUMERIC = "[a-z0-9_" + LATIN_ACCENTS + NON_LATIN_HASHTAG_CHARS + CJ_HASHTAG_CHARACTERS + "]";
85 private const string HASHTAG_TERMINATOR = "[^a-z0-9_" + LATIN_ACCENTS + NON_LATIN_HASHTAG_CHARS + CJ_HASHTAG_CHARACTERS + "]";
86 public const string HASHTAG = "(" + HASHTAG_BOUNDARY + ")(#|#)(" + HASHTAG_ALPHANUMERIC + "*" + HASHTAG_ALPHA + HASHTAG_ALPHANUMERIC + "*)(?=" + HASHTAG_TERMINATOR + "|" + HASHTAG_BOUNDARY + ")";
88 private const string url_valid_preceding_chars = @"(?:[^A-Za-z0-9@@$##\ufffe\ufeff\uffff\u202a-\u202e]|^)";
89 public const string url_invalid_without_protocol_preceding_chars = @"[-_./]$";
90 private const string url_invalid_domain_chars = @"\!'#%&'\(\)*\+,\\\-\.\/:;<=>\?@\[\]\^_{|}~\$\u2000-\u200a\u0009-\u000d\u0020\u0085\u00a0\u1680\u180e\u2028\u2029\u202f\u205f\u3000\ufffe\ufeff\uffff\u202a-\u202e";
91 private const string url_valid_domain_chars = @"[^" + url_invalid_domain_chars + "]";
92 private const string url_valid_subdomain = @"(?:(?:" + url_valid_domain_chars + @"(?:[_-]|" + url_valid_domain_chars + @")*)?" + url_valid_domain_chars + @"\.)";
93 private const string url_valid_domain_name = @"(?:(?:" + url_valid_domain_chars + @"(?:-|" + url_valid_domain_chars + @")*)?" + url_valid_domain_chars + @"\.)";
94 private const string url_valid_GTLD = @"(?:(?:aero|asia|biz|cat|com|coop|edu|gov|info|int|jobs|mil|mobi|museum|name|net|org|pro|tel|travel|xxx)(?=[^0-9a-zA-Z]|$))";
95 private const string url_valid_CCTLD = @"(?:(?:ac|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|cr|cs|cu|cv|cx|cy|cz|dd|de|dj|dk|dm|do|dz|ec|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|io|iq|ir|is|it|je|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|mv|mw|mx|my|mz|na|nc|ne|nf|ng|ni|nl|no|np|nr|nu|nz|om|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|ps|pt|pw|py|qa|re|ro|rs|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|ss|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tl|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|za|zm|zw)(?=[^0-9a-zA-Z]|$))";
96 private const string url_valid_punycode = @"(?:xn--[0-9a-z]+)";
97 private const string url_valid_domain = @"(?<domain>" + url_valid_subdomain + "*" + url_valid_domain_name + "(?:" + url_valid_GTLD + "|" + url_valid_CCTLD + ")|" + url_valid_punycode + ")";
98 public const string url_valid_ascii_domain = @"(?:(?:[a-z0-9" + LATIN_ACCENTS + @"]+)\.)+(?:" + url_valid_GTLD + "|" + url_valid_CCTLD + "|" + url_valid_punycode + ")";
99 public const string url_invalid_short_domain = "^" + url_valid_domain_name + url_valid_CCTLD + "$";
100 private const string url_valid_port_number = @"[0-9]+";
102 private const string url_valid_general_path_chars = @"[a-z0-9!*';:=+,.$/%#\[\]\-_~|&" + LATIN_ACCENTS + "]";
103 private const string url_balance_parens = @"(?:\(" + url_valid_general_path_chars + @"+\))";
104 private const string url_valid_path_ending_chars = @"(?:[+\-a-z0-9=_#/" + LATIN_ACCENTS + "]|" + url_balance_parens + ")";
105 private const string pth = "(?:" +
107 url_valid_general_path_chars + "*" +
108 "(?:" + url_balance_parens + url_valid_general_path_chars + "*)*" +
109 url_valid_path_ending_chars +
110 ")|(?:@" + url_valid_general_path_chars + "+/)" +
112 private const string qry = @"(?<query>\?[a-z0-9!?*'();:&=+$/%#\[\]\-_.,~|]*[a-z0-9_&=#/])?";
113 public const string rgUrl = @"(?<before>" + url_valid_preceding_chars + ")" +
114 "(?<url>(?<protocol>https?://)?" +
115 "(?<domain>" + url_valid_domain + ")" +
116 "(?::" + url_valid_port_number + ")?" +
117 "(?<path>/" + pth + "*)?" +
124 /// Twitter API のステータスページのURL
126 public const string ServiceAvailabilityStatusUrl = "https://status.io.watchmouse.com/7617";
129 /// ツイートへのパーマリンクURLを判定する正規表現
131 public static readonly Regex StatusUrlRegex = new Regex(@"https?://([^.]+\.)?twitter\.com/(#!/)?(?<ScreenName>[a-zA-Z0-9_]+)/status(es)?/(?<StatusId>[0-9]+)(/photo)?", RegexOptions.IgnoreCase);
134 /// attachment_url に指定可能な URL を判定する正規表現
136 public static readonly Regex AttachmentUrlRegex = new Regex(@"https?://(
137 twitter\.com/[0-9A-Za-z_]+/status/[0-9]+
138 | mobile\.twitter\.com/[0-9A-Za-z_]+/status/[0-9]+
139 | twitter\.com/messages/compose\?recipient_id=[0-9]+(&.+)?
140 )$", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
143 /// FavstarやaclogなどTwitter関連サービスのパーマリンクURLからステータスIDを抽出する正規表現
145 public static readonly Regex ThirdPartyStatusUrlRegex = new Regex(@"https?://(?:[^.]+\.)?(?:
146 favstar\.fm/users/[a-zA-Z0-9_]+/status/ # Favstar
147 | favstar\.fm/t/ # Favstar (short)
148 | aclog\.koba789\.com/i/ # aclog
149 | frtrt\.net/solo_status\.php\?status= # RtRT
150 )(?<StatusId>[0-9]+)", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
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 = new long[0];
170 private List<string> _hashList = new List<string>();
172 //max_idで古い発言を取得するために保持(lists分は個別タブで管理)
173 private long minDirectmessage = long.MaxValue;
174 private long minDirectmessageSent = long.MaxValue;
176 private long previousStatusId = -1L;
178 //private FavoriteQueue favQueue;
180 //private List<PostClass> _deletemessages = new List<PostClass>();
182 public Twitter() : this(new TwitterApi())
186 public Twitter(TwitterApi api)
189 this.Configuration = TwitterConfiguration.DefaultConfiguration();
190 this.TextConfiguration = TwitterTextConfiguration.DefaultConfiguration();
193 public TwitterApiAccessLevel AccessLevel
194 => MyCommon.TwitterApiInfo.AccessLevel;
196 protected void ResetApiStatus()
197 => MyCommon.TwitterApiInfo.Reset();
199 public void ClearAuthInfo()
201 Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
202 this.ResetApiStatus();
206 public void VerifyCredentials()
210 this.VerifyCredentialsAsync().Wait();
212 catch (AggregateException ex) when (ex.InnerException is WebApiException)
214 throw new WebApiException(ex.InnerException.Message, ex);
218 public async Task VerifyCredentialsAsync()
220 var user = await this.Api.AccountVerifyCredentials()
221 .ConfigureAwait(false);
223 this.UpdateUserStats(user);
226 public void Initialize(string token, string tokenSecret, string username, long userId)
229 if (string.IsNullOrEmpty(token) || string.IsNullOrEmpty(tokenSecret) || string.IsNullOrEmpty(username))
231 Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
233 this.ResetApiStatus();
234 this.Api.Initialize(token, tokenSecret, userId, username);
235 if (SettingManager.Common.UserstreamStartup) this.ReconnectUserStream();
238 internal static string PreProcessUrl(string orgData)
242 //var IDNConveter = new IdnMapping();
243 var href = "<a href=\"";
247 if (orgData.IndexOf(href, posl2, StringComparison.Ordinal) > -1)
251 posl1 = orgData.IndexOf(href, posl2, StringComparison.Ordinal);
252 posl1 += href.Length;
253 posl2 = orgData.IndexOf("\"", posl1, StringComparison.Ordinal);
254 urlStr = orgData.Substring(posl1, posl2 - posl1);
256 if (!urlStr.StartsWith("http://", StringComparison.Ordinal)
257 && !urlStr.StartsWith("https://", StringComparison.Ordinal)
258 && !urlStr.StartsWith("ftp://", StringComparison.Ordinal))
263 var replacedUrl = MyCommon.IDNEncode(urlStr);
264 if (replacedUrl == null) continue;
265 if (replacedUrl == urlStr) continue;
267 orgData = orgData.Replace("<a href=\"" + urlStr, "<a href=\"" + replacedUrl);
278 public async Task PostStatus(PostStatusParams param)
280 this.CheckAccountState();
282 if (Twitter.DMSendTextRegex.IsMatch(param.Text))
284 await this.SendDirectMessage(param.Text)
285 .ConfigureAwait(false);
289 var response = await this.Api.StatusesUpdate(param.Text, param.InReplyToStatusId, param.MediaIds,
290 param.AutoPopulateReplyMetadata, param.ExcludeReplyUserIds, param.AttachmentUrl)
291 .ConfigureAwait(false);
293 var status = await response.LoadJsonAsync()
294 .ConfigureAwait(false);
296 this.UpdateUserStats(status.User);
298 if (status.Id == this.previousStatusId)
299 throw new WebApiException("OK:Delaying?");
301 this.previousStatusId = status.Id;
304 public async Task<long> UploadMedia(IMediaItem item)
306 this.CheckAccountState();
308 var response = await this.Api.MediaUpload(item)
309 .ConfigureAwait(false);
311 var media = await response.LoadJsonAsync()
312 .ConfigureAwait(false);
314 return media.MediaId;
317 public async Task SendDirectMessage(string postStr)
319 this.CheckAccountState();
320 this.CheckAccessLevel(TwitterApiAccessLevel.ReadWriteAndDirectMessage);
322 var mc = Twitter.DMSendTextRegex.Match(postStr);
324 var body = mc.Groups["body"].Value;
325 var recipientName = mc.Groups["id"].Value;
327 var recipient = await this.Api.UsersShow(recipientName)
328 .ConfigureAwait(false);
330 await this.Api.DirectMessagesEventsNew(recipient.Id, body)
331 .ConfigureAwait(false);
334 public async Task PostRetweet(long id, bool read)
336 this.CheckAccountState();
339 var post = TabInformations.GetInstance()[id];
341 throw new WebApiException("Err:Target isn't found.");
343 var target = post.RetweetedId ?? id; //再RTの場合は元発言をRT
345 var response = await this.Api.StatusesRetweet(target)
346 .ConfigureAwait(false);
348 var status = await response.LoadJsonAsync()
349 .ConfigureAwait(false);
354 if (TabInformations.GetInstance().ContainsKey(status.Id))
359 if (status.RetweetedStatus == null)
360 throw new WebApiException("Invalid Json!");
363 post = CreatePostsFromStatusData(status);
370 if (this.ReadOwnPost) post.IsRead = true;
373 TabInformations.GetInstance().AddPost(post);
376 public string Username
377 => this.Api.CurrentScreenName;
380 => this.Api.CurrentUserId;
382 public static MyCommon.ACCOUNT_STATE AccountState { get; set; } = MyCommon.ACCOUNT_STATE.Valid;
383 public bool RestrictFavCheck { get; set; }
384 public bool ReadOwnPost { get; set; }
386 public int FollowersCount { get; private set; }
387 public int FriendsCount { get; private set; }
388 public int StatusesCount { get; private set; }
389 public string Location { get; private set; } = "";
390 public string Bio { get; private set; } = "";
392 /// <summary>ユーザーのフォロワー数などの情報を更新します</summary>
393 private void UpdateUserStats(TwitterUser self)
395 this.FollowersCount = self.FollowersCount;
396 this.FriendsCount = self.FriendsCount;
397 this.StatusesCount = self.StatusesCount;
398 this.Location = self.Location;
399 this.Bio = self.Description;
403 /// 渡された取得件数がWORKERTYPEに応じた取得可能範囲に収まっているか検証する
405 public static bool VerifyApiResultCount(MyCommon.WORKERTYPE type, int count)
406 => count >= 20 && count <= GetMaxApiResultCount(type);
409 /// 渡された取得件数が更新時の取得可能範囲に収まっているか検証する
411 public static bool VerifyMoreApiResultCount(int count)
412 => count >= 20 && count <= 200;
415 /// 渡された取得件数が起動時の取得可能範囲に収まっているか検証する
417 public static bool VerifyFirstApiResultCount(int count)
418 => count >= 20 && count <= 200;
421 /// WORKERTYPEに応じた取得可能な最大件数を取得する
423 public static int GetMaxApiResultCount(MyCommon.WORKERTYPE type)
425 // 参照: REST APIs - 各endpointのcountパラメータ
426 // https://dev.twitter.com/rest/public
429 case MyCommon.WORKERTYPE.Timeline:
430 case MyCommon.WORKERTYPE.Reply:
431 case MyCommon.WORKERTYPE.UserTimeline:
432 case MyCommon.WORKERTYPE.Favorites:
433 case MyCommon.WORKERTYPE.DirectMessegeRcv:
434 case MyCommon.WORKERTYPE.DirectMessegeSnt:
435 case MyCommon.WORKERTYPE.List: // 不明
438 case MyCommon.WORKERTYPE.PublicSearch:
442 throw new InvalidOperationException("Invalid type: " + type);
447 /// WORKERTYPEに応じた取得件数を取得する
449 public static int GetApiResultCount(MyCommon.WORKERTYPE type, bool more, bool startup)
451 if (type == MyCommon.WORKERTYPE.DirectMessegeRcv ||
452 type == MyCommon.WORKERTYPE.DirectMessegeSnt)
457 if (SettingManager.Common.UseAdditionalCount)
461 case MyCommon.WORKERTYPE.Favorites:
462 if (SettingManager.Common.FavoritesCountApi != 0)
463 return SettingManager.Common.FavoritesCountApi;
465 case MyCommon.WORKERTYPE.List:
466 if (SettingManager.Common.ListCountApi != 0)
467 return SettingManager.Common.ListCountApi;
469 case MyCommon.WORKERTYPE.PublicSearch:
470 if (SettingManager.Common.SearchCountApi != 0)
471 return SettingManager.Common.SearchCountApi;
473 case MyCommon.WORKERTYPE.UserTimeline:
474 if (SettingManager.Common.UserTimelineCountApi != 0)
475 return SettingManager.Common.UserTimelineCountApi;
478 if (more && SettingManager.Common.MoreCountApi != 0)
480 return Math.Min(SettingManager.Common.MoreCountApi, GetMaxApiResultCount(type));
482 if (startup && SettingManager.Common.FirstCountApi != 0 && type != MyCommon.WORKERTYPE.Reply)
484 return Math.Min(SettingManager.Common.FirstCountApi, GetMaxApiResultCount(type));
489 var count = SettingManager.Common.CountApi;
491 if (type == MyCommon.WORKERTYPE.Reply)
492 count = SettingManager.Common.CountApiReply;
494 return Math.Min(count, GetMaxApiResultCount(type));
497 public async Task GetHomeTimelineApi(bool read, HomeTabModel tab, bool more, bool startup)
499 this.CheckAccountState();
501 var count = GetApiResultCount(MyCommon.WORKERTYPE.Timeline, more, startup);
503 TwitterStatus[] statuses;
506 statuses = await this.Api.StatusesHomeTimeline(count, maxId: tab.OldestId)
507 .ConfigureAwait(false);
511 statuses = await this.Api.StatusesHomeTimeline(count)
512 .ConfigureAwait(false);
515 var minimumId = this.CreatePostsFromJson(statuses, MyCommon.WORKERTYPE.Timeline, tab, read);
516 if (minimumId != null)
517 tab.OldestId = minimumId.Value;
520 public async Task GetMentionsTimelineApi(bool read, MentionsTabModel tab, bool more, bool startup)
522 this.CheckAccountState();
524 var count = GetApiResultCount(MyCommon.WORKERTYPE.Reply, more, startup);
526 TwitterStatus[] statuses;
529 statuses = await this.Api.StatusesMentionsTimeline(count, maxId: tab.OldestId)
530 .ConfigureAwait(false);
534 statuses = await this.Api.StatusesMentionsTimeline(count)
535 .ConfigureAwait(false);
538 var minimumId = this.CreatePostsFromJson(statuses, MyCommon.WORKERTYPE.Reply, tab, read);
539 if (minimumId != null)
540 tab.OldestId = minimumId.Value;
543 public async Task GetUserTimelineApi(bool read, string userName, UserTimelineTabModel tab, bool more)
545 this.CheckAccountState();
547 var count = GetApiResultCount(MyCommon.WORKERTYPE.UserTimeline, more, false);
549 TwitterStatus[] statuses;
550 if (string.IsNullOrEmpty(userName))
552 var target = tab.ScreenName;
553 if (string.IsNullOrEmpty(target)) return;
555 statuses = await this.Api.StatusesUserTimeline(userName, count)
556 .ConfigureAwait(false);
562 statuses = await this.Api.StatusesUserTimeline(userName, count, maxId: tab.OldestId)
563 .ConfigureAwait(false);
567 statuses = await this.Api.StatusesUserTimeline(userName, count)
568 .ConfigureAwait(false);
572 var minimumId = CreatePostsFromJson(statuses, MyCommon.WORKERTYPE.UserTimeline, tab, read);
574 if (minimumId != null)
575 tab.OldestId = minimumId.Value;
578 public async Task<PostClass> GetStatusApi(bool read, long id)
580 this.CheckAccountState();
582 var status = await this.Api.StatusesShow(id)
583 .ConfigureAwait(false);
585 var item = CreatePostsFromStatusData(status);
588 if (item.IsMe && !read && this.ReadOwnPost) item.IsRead = true;
593 public async Task GetStatusApi(bool read, long id, TabModel tab)
595 var post = await this.GetStatusApi(read, id)
596 .ConfigureAwait(false);
598 //非同期アイコン取得&StatusDictionaryに追加
599 if (tab != null && tab.IsInnerStorageTabType)
600 tab.AddPostQueue(post);
602 TabInformations.GetInstance().AddPost(post);
605 private PostClass CreatePostsFromStatusData(TwitterStatus status)
606 => this.CreatePostsFromStatusData(status, false);
608 private PostClass CreatePostsFromStatusData(TwitterStatus status, bool favTweet)
610 var post = new PostClass();
611 TwitterEntities entities;
614 post.StatusId = status.Id;
615 if (status.RetweetedStatus != null)
617 var retweeted = status.RetweetedStatus;
619 post.CreatedAt = MyCommon.DateTimeParse(retweeted.CreatedAt);
622 post.RetweetedId = retweeted.Id;
624 post.TextFromApi = retweeted.FullText;
625 entities = retweeted.MergedEntities;
626 sourceHtml = retweeted.Source;
628 post.InReplyToStatusId = retweeted.InReplyToStatusId;
629 post.InReplyToUser = retweeted.InReplyToScreenName;
630 post.InReplyToUserId = status.InReplyToUserId;
639 var tc = TabInformations.GetInstance().GetTabByType(MyCommon.TabUsageType.Favorites);
640 post.IsFav = tc.Contains(retweeted.Id);
643 if (retweeted.Coordinates != null)
644 post.PostGeo = new PostClass.StatusGeo(retweeted.Coordinates.Coordinates[0], retweeted.Coordinates.Coordinates[1]);
647 var user = retweeted.User;
650 post.UserId = user.Id;
651 post.ScreenName = user.ScreenName;
652 post.Nickname = user.Name.Trim();
653 post.ImageUrl = user.ProfileImageUrlHttps;
654 post.IsProtect = user.Protected;
659 post.ScreenName = "?????";
660 post.Nickname = "Unknown User";
664 if (status.User != null)
666 post.RetweetedBy = status.User.ScreenName;
667 post.RetweetedByUserId = status.User.Id;
668 post.IsMe = post.RetweetedByUserId == this.UserId;
672 post.RetweetedBy = "?????";
673 post.RetweetedByUserId = 0L;
678 post.CreatedAt = MyCommon.DateTimeParse(status.CreatedAt);
680 post.TextFromApi = status.FullText;
681 entities = status.MergedEntities;
682 sourceHtml = status.Source;
683 post.InReplyToStatusId = status.InReplyToStatusId;
684 post.InReplyToUser = status.InReplyToScreenName;
685 post.InReplyToUserId = status.InReplyToUserId;
694 var tc = TabInformations.GetInstance().GetTabByType(MyCommon.TabUsageType.Favorites);
695 post.IsFav = tc.Contains(post.StatusId) && TabInformations.GetInstance()[post.StatusId].IsFav;
698 if (status.Coordinates != null)
699 post.PostGeo = new PostClass.StatusGeo(status.Coordinates.Coordinates[0], status.Coordinates.Coordinates[1]);
702 var user = status.User;
705 post.UserId = user.Id;
706 post.ScreenName = user.ScreenName;
707 post.Nickname = user.Name.Trim();
708 post.ImageUrl = user.ProfileImageUrlHttps;
709 post.IsProtect = user.Protected;
710 post.IsMe = post.UserId == this.UserId;
715 post.ScreenName = "?????";
716 post.Nickname = "Unknown User";
720 string textFromApi = post.TextFromApi;
722 var quotedStatusLink = (status.RetweetedStatus ?? status).QuotedStatusPermalink;
724 if (quotedStatusLink != null && entities.Urls.Any(x => x.ExpandedUrl == quotedStatusLink.Expanded))
725 quotedStatusLink = null; // 移行期は entities.urls と quoted_status_permalink の両方に含まれる場合がある
727 post.Text = CreateHtmlAnchor(textFromApi, entities, quotedStatusLink);
728 post.TextFromApi = textFromApi;
729 post.TextFromApi = this.ReplaceTextFromApi(post.TextFromApi, entities, quotedStatusLink);
730 post.TextFromApi = WebUtility.HtmlDecode(post.TextFromApi);
731 post.TextFromApi = post.TextFromApi.Replace("<3", "\u2661");
732 post.AccessibleText = CreateAccessibleText(textFromApi, entities, (status.RetweetedStatus ?? status).QuotedStatus, quotedStatusLink);
733 post.AccessibleText = WebUtility.HtmlDecode(post.AccessibleText);
734 post.AccessibleText = post.AccessibleText.Replace("<3", "\u2661");
736 this.ExtractEntities(entities, post.ReplyToList, post.Media);
738 post.QuoteStatusIds = GetQuoteTweetStatusIds(entities, quotedStatusLink)
739 .Where(x => x != post.StatusId && x != post.RetweetedId)
740 .Distinct().ToArray();
742 post.ExpandedUrls = entities.OfType<TwitterEntityUrl>()
743 .Select(x => new PostClass.ExpandedUrlInfo(x.Url, x.ExpandedUrl))
746 // メモリ使用量削減 (同一のテキストであれば同一の string インスタンスを参照させる)
747 if (post.Text == post.TextFromApi)
748 post.Text = post.TextFromApi;
749 if (post.AccessibleText == post.TextFromApi)
750 post.AccessibleText = post.TextFromApi;
752 // 他の発言と重複しやすい (共通化できる) 文字列は string.Intern を通す
753 post.ScreenName = string.Intern(post.ScreenName);
754 post.Nickname = string.Intern(post.Nickname);
755 post.ImageUrl = string.Intern(post.ImageUrl);
756 post.RetweetedBy = post.RetweetedBy != null ? string.Intern(post.RetweetedBy) : null;
759 var (sourceText, sourceUri) = ParseSource(sourceHtml);
760 post.Source = string.Intern(sourceText);
761 post.SourceUri = sourceUri;
763 post.IsReply = post.RetweetedId == null && post.ReplyToList.Any(x => x.Item1 == this.UserId);
764 post.IsExcludeReply = false;
772 if (followerId.Count > 0) post.IsOwl = !followerId.Contains(post.UserId);
780 /// ツイートに含まれる引用ツイートのURLからステータスIDを抽出
782 public static IEnumerable<long> GetQuoteTweetStatusIds(IEnumerable<TwitterEntity> entities, TwitterQuotedStatusPermalink quotedStatusLink)
784 var urls = entities.OfType<TwitterEntityUrl>().Select(x => x.ExpandedUrl);
786 if (quotedStatusLink != null)
787 urls = urls.Concat(new[] { quotedStatusLink.Expanded });
789 return GetQuoteTweetStatusIds(urls);
792 public static IEnumerable<long> GetQuoteTweetStatusIds(IEnumerable<string> urls)
794 foreach (var url in urls)
796 var match = Twitter.StatusUrlRegex.Match(url);
799 if (long.TryParse(match.Groups["StatusId"].Value, out var statusId))
800 yield return statusId;
805 private long? CreatePostsFromJson(TwitterStatus[] items, MyCommon.WORKERTYPE gType, TabModel tab, bool read)
807 long? minimumId = null;
809 foreach (var status in items)
811 if (minimumId == null || minimumId.Value > status.Id)
812 minimumId = status.Id;
819 if (TabInformations.GetInstance().ContainsKey(status.Id)) continue;
823 if (tab.Contains(status.Id)) continue;
828 if (gType != MyCommon.WORKERTYPE.UserTimeline &&
829 status.RetweetedStatus != null && this.noRTId.Contains(status.User.Id)) continue;
831 var post = CreatePostsFromStatusData(status);
834 if (post.IsMe && !read && this.ReadOwnPost) post.IsRead = true;
836 if (tab != null && tab.IsInnerStorageTabType)
837 tab.AddPostQueue(post);
839 TabInformations.GetInstance().AddPost(post);
845 private long? CreatePostsFromSearchJson(TwitterSearchResult items, PublicSearchTabModel tab, bool read, bool more)
847 long? minimumId = null;
849 foreach (var status in items.Statuses)
851 if (minimumId == null || minimumId.Value > status.Id)
852 minimumId = status.Id;
854 if (!more && status.Id > tab.SinceId) tab.SinceId = status.Id;
858 if (tab.Contains(status.Id)) continue;
861 var post = CreatePostsFromStatusData(status);
864 if ((post.IsMe && !read) && this.ReadOwnPost) post.IsRead = true;
866 tab.AddPostQueue(post);
872 private long? CreateFavoritePostsFromJson(TwitterStatus[] items, bool read)
874 var favTab = TabInformations.GetInstance().GetTabByType(MyCommon.TabUsageType.Favorites);
875 long? minimumId = null;
877 foreach (var status in items)
879 if (minimumId == null || minimumId.Value > status.Id)
880 minimumId = status.Id;
885 if (favTab.Contains(status.Id)) continue;
888 var post = CreatePostsFromStatusData(status, true);
892 TabInformations.GetInstance().AddPost(post);
898 public async Task GetListStatus(bool read, ListTimelineTabModel tab, bool more, bool startup)
900 var count = GetApiResultCount(MyCommon.WORKERTYPE.List, more, startup);
902 TwitterStatus[] statuses;
905 statuses = await this.Api.ListsStatuses(tab.ListInfo.Id, count, maxId: tab.OldestId, includeRTs: SettingManager.Common.IsListsIncludeRts)
906 .ConfigureAwait(false);
910 statuses = await this.Api.ListsStatuses(tab.ListInfo.Id, count, includeRTs: SettingManager.Common.IsListsIncludeRts)
911 .ConfigureAwait(false);
914 var minimumId = CreatePostsFromJson(statuses, MyCommon.WORKERTYPE.List, tab, read);
916 if (minimumId != null)
917 tab.OldestId = minimumId.Value;
921 /// startStatusId からリプライ先の発言を辿る。発言は posts 以外からは検索しない。
923 /// <returns>posts の中から検索されたリプライチェインの末端</returns>
924 internal static PostClass FindTopOfReplyChain(IDictionary<Int64, PostClass> posts, Int64 startStatusId)
926 if (!posts.ContainsKey(startStatusId))
927 throw new ArgumentException("startStatusId (" + startStatusId + ") が posts の中から見つかりませんでした。", nameof(startStatusId));
929 var nextPost = posts[startStatusId];
930 while (nextPost.InReplyToStatusId != null)
932 if (!posts.ContainsKey(nextPost.InReplyToStatusId.Value))
934 nextPost = posts[nextPost.InReplyToStatusId.Value];
940 public async Task GetRelatedResult(bool read, RelatedPostsTabModel tab)
942 var targetPost = tab.TargetPost;
943 var relPosts = new Dictionary<Int64, PostClass>();
944 if (targetPost.TextFromApi.Contains("@") && targetPost.InReplyToStatusId == null)
947 var p = TabInformations.GetInstance()[targetPost.StatusId];
948 if (p != null && p.InReplyToStatusId != null)
954 p = await this.GetStatusApi(read, targetPost.StatusId)
955 .ConfigureAwait(false);
959 relPosts.Add(targetPost.StatusId, targetPost);
961 Exception lastException = null;
963 // in_reply_to_status_id を使用してリプライチェインを辿る
964 var nextPost = FindTopOfReplyChain(relPosts, targetPost.StatusId);
966 while (nextPost.InReplyToStatusId != null && loopCount++ <= 20)
968 var inReplyToId = nextPost.InReplyToStatusId.Value;
970 var inReplyToPost = TabInformations.GetInstance()[inReplyToId];
971 if (inReplyToPost == null)
975 inReplyToPost = await this.GetStatusApi(read, inReplyToId)
976 .ConfigureAwait(false);
978 catch (WebApiException ex)
985 relPosts.Add(inReplyToPost.StatusId, inReplyToPost);
987 nextPost = FindTopOfReplyChain(relPosts, nextPost.StatusId);
990 //MRTとかに対応のためツイート内にあるツイートを指すURLを取り込む
991 var text = targetPost.Text;
992 var ma = Twitter.StatusUrlRegex.Matches(text).Cast<Match>()
993 .Concat(Twitter.ThirdPartyStatusUrlRegex.Matches(text).Cast<Match>());
994 foreach (var _match in ma)
996 if (Int64.TryParse(_match.Groups["StatusId"].Value, out var _statusId))
998 if (relPosts.ContainsKey(_statusId))
1001 var p = TabInformations.GetInstance()[_statusId];
1006 p = await this.GetStatusApi(read, _statusId)
1007 .ConfigureAwait(false);
1009 catch (WebApiException ex)
1017 relPosts.Add(p.StatusId, p);
1021 relPosts.Values.ToList().ForEach(p =>
1023 if (p.IsMe && !read && this.ReadOwnPost)
1028 tab.AddPostQueue(p);
1031 if (lastException != null)
1032 throw new WebApiException(lastException.Message, lastException);
1035 public async Task GetSearch(bool read, PublicSearchTabModel tab, bool more)
1037 var count = GetApiResultCount(MyCommon.WORKERTYPE.PublicSearch, more, false);
1040 long? sinceId = null;
1043 maxId = tab.OldestId - 1;
1047 sinceId = tab.SinceId;
1050 var searchResult = await this.Api.SearchTweets(tab.SearchWords, tab.SearchLang, count, maxId, sinceId)
1051 .ConfigureAwait(false);
1053 if (!TabInformations.GetInstance().ContainsTab(tab))
1056 var minimumId = this.CreatePostsFromSearchJson(searchResult, tab, read, more);
1058 if (minimumId != null)
1059 tab.OldestId = minimumId.Value;
1062 private void CreateDirectMessagesFromJson(TwitterDirectMessage[] item, MyCommon.WORKERTYPE gType, bool read)
1064 foreach (var message in item)
1066 var post = new PostClass();
1069 post.StatusId = message.Id;
1070 if (gType != MyCommon.WORKERTYPE.UserStream)
1072 if (gType == MyCommon.WORKERTYPE.DirectMessegeRcv)
1074 if (minDirectmessage > post.StatusId) minDirectmessage = post.StatusId;
1078 if (minDirectmessageSent > post.StatusId) minDirectmessageSent = post.StatusId;
1085 if (TabInformations.GetInstance().GetTabByType(MyCommon.TabUsageType.DirectMessage).Contains(post.StatusId)) continue;
1089 post.CreatedAt = MyCommon.DateTimeParse(message.CreatedAt);
1091 var textFromApi = message.Text;
1093 post.Text = CreateHtmlAnchor(textFromApi, message.Entities, quotedStatusLink: null);
1094 post.TextFromApi = this.ReplaceTextFromApi(textFromApi, message.Entities, quotedStatusLink: null);
1095 post.TextFromApi = WebUtility.HtmlDecode(post.TextFromApi);
1096 post.TextFromApi = post.TextFromApi.Replace("<3", "\u2661");
1097 post.AccessibleText = CreateAccessibleText(textFromApi, message.Entities, quotedStatus: null, quotedStatusLink: null);
1098 post.AccessibleText = WebUtility.HtmlDecode(post.AccessibleText);
1099 post.AccessibleText = post.AccessibleText.Replace("<3", "\u2661");
1102 this.ExtractEntities(message.Entities, post.ReplyToList, post.Media);
1104 post.QuoteStatusIds = GetQuoteTweetStatusIds(message.Entities, quotedStatusLink: null)
1105 .Distinct().ToArray();
1107 post.ExpandedUrls = message.Entities.OfType<TwitterEntityUrl>()
1108 .Select(x => new PostClass.ExpandedUrlInfo(x.Url, x.ExpandedUrl))
1113 if (gType == MyCommon.WORKERTYPE.UserStream)
1115 if (this.Api.CurrentUserId == message.Recipient.Id)
1117 user = message.Sender;
1123 user = message.Recipient;
1130 if (gType == MyCommon.WORKERTYPE.DirectMessegeRcv)
1132 user = message.Sender;
1138 user = message.Recipient;
1144 post.UserId = user.Id;
1145 post.ScreenName = user.ScreenName;
1146 post.Nickname = user.Name.Trim();
1147 post.ImageUrl = user.ProfileImageUrlHttps;
1148 post.IsProtect = user.Protected;
1150 // メモリ使用量削減 (同一のテキストであれば同一の string インスタンスを参照させる)
1151 if (post.Text == post.TextFromApi)
1152 post.Text = post.TextFromApi;
1153 if (post.AccessibleText == post.TextFromApi)
1154 post.AccessibleText = post.TextFromApi;
1156 // 他の発言と重複しやすい (共通化できる) 文字列は string.Intern を通す
1157 post.ScreenName = string.Intern(post.ScreenName);
1158 post.Nickname = string.Intern(post.Nickname);
1159 post.ImageUrl = string.Intern(post.ImageUrl);
1163 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name);
1164 MessageBox.Show("Parse Error(CreateDirectMessagesFromJson)");
1169 if (post.IsMe && !read && this.ReadOwnPost) post.IsRead = true;
1170 post.IsReply = false;
1171 post.IsExcludeReply = false;
1174 var dmTab = TabInformations.GetInstance().GetTabByType(MyCommon.TabUsageType.DirectMessage);
1175 dmTab.AddPostQueue(post);
1179 public async Task GetDirectMessageApi(bool read, MyCommon.WORKERTYPE gType, bool more)
1181 this.CheckAccountState();
1182 this.CheckAccessLevel(TwitterApiAccessLevel.ReadWriteAndDirectMessage);
1184 var count = GetApiResultCount(gType, more, false);
1186 TwitterDirectMessage[] messages;
1187 if (gType == MyCommon.WORKERTYPE.DirectMessegeRcv)
1191 messages = await this.Api.DirectMessagesRecv(count, maxId: this.minDirectmessage)
1192 .ConfigureAwait(false);
1196 messages = await this.Api.DirectMessagesRecv(count)
1197 .ConfigureAwait(false);
1204 messages = await this.Api.DirectMessagesSent(count, maxId: this.minDirectmessageSent)
1205 .ConfigureAwait(false);
1209 messages = await this.Api.DirectMessagesSent(count)
1210 .ConfigureAwait(false);
1214 CreateDirectMessagesFromJson(messages, gType, read);
1217 public async Task GetFavoritesApi(bool read, FavoritesTabModel tab, bool backward)
1219 this.CheckAccountState();
1221 var count = GetApiResultCount(MyCommon.WORKERTYPE.Favorites, backward, false);
1223 TwitterStatus[] statuses;
1226 statuses = await this.Api.FavoritesList(count, maxId: tab.OldestId)
1227 .ConfigureAwait(false);
1231 statuses = await this.Api.FavoritesList(count)
1232 .ConfigureAwait(false);
1235 var minimumId = this.CreateFavoritePostsFromJson(statuses, read);
1237 if (minimumId != null)
1238 tab.OldestId = minimumId.Value;
1241 private string ReplaceTextFromApi(string text, TwitterEntities entities, TwitterQuotedStatusPermalink quotedStatusLink)
1243 if (entities != null)
1245 if (entities.Urls != null)
1247 foreach (var m in entities.Urls)
1249 if (!string.IsNullOrEmpty(m.DisplayUrl)) text = text.Replace(m.Url, m.DisplayUrl);
1252 if (entities.Media != null)
1254 foreach (var m in entities.Media)
1256 if (!string.IsNullOrEmpty(m.DisplayUrl)) text = text.Replace(m.Url, m.DisplayUrl);
1261 if (quotedStatusLink != null)
1262 text += " " + quotedStatusLink.Display;
1267 internal static string CreateAccessibleText(string text, TwitterEntities entities, TwitterStatus quotedStatus, TwitterQuotedStatusPermalink quotedStatusLink)
1269 if (entities == null)
1272 if (entities.Urls != null)
1274 foreach (var entity in entities.Urls)
1276 if (quotedStatus != null)
1278 var matchStatusUrl = Twitter.StatusUrlRegex.Match(entity.ExpandedUrl);
1279 if (matchStatusUrl.Success && matchStatusUrl.Groups["StatusId"].Value == quotedStatus.IdStr)
1281 var quotedText = CreateAccessibleText(quotedStatus.FullText, quotedStatus.MergedEntities, quotedStatus: null, quotedStatusLink: null);
1282 text = text.Replace(entity.Url, string.Format(Properties.Resources.QuoteStatus_AccessibleText, quotedStatus.User.ScreenName, quotedText));
1287 if (!string.IsNullOrEmpty(entity.DisplayUrl))
1288 text = text.Replace(entity.Url, entity.DisplayUrl);
1292 if (entities.Media != null)
1294 foreach (var entity in entities.Media)
1296 if (!string.IsNullOrEmpty(entity.AltText))
1298 text = text.Replace(entity.Url, string.Format(Properties.Resources.ImageAltText, entity.AltText));
1300 else if (!string.IsNullOrEmpty(entity.DisplayUrl))
1302 text = text.Replace(entity.Url, entity.DisplayUrl);
1307 if (quotedStatusLink != null)
1309 var quoteText = CreateAccessibleText(quotedStatus.FullText, quotedStatus.MergedEntities, quotedStatus: null, quotedStatusLink: null);
1310 text += " " + string.Format(Properties.Resources.QuoteStatus_AccessibleText, quotedStatus.User.ScreenName, quoteText);
1319 /// <exception cref="WebApiException"/>
1320 public async Task RefreshFollowerIds()
1322 if (MyCommon._endingFlag) return;
1325 var newFollowerIds = new HashSet<long>();
1328 var ret = await this.Api.FollowersIds(cursor)
1329 .ConfigureAwait(false);
1331 if (ret.Ids == null)
1332 throw new WebApiException("ret.ids == null");
1334 newFollowerIds.UnionWith(ret.Ids);
1335 cursor = ret.NextCursor;
1336 } while (cursor != 0);
1338 this.followerId = newFollowerIds;
1339 TabInformations.GetInstance().RefreshOwl(this.followerId);
1341 this.GetFollowersSuccess = true;
1345 /// RT 非表示ユーザーを更新します
1347 /// <exception cref="WebApiException"/>
1348 public async Task RefreshNoRetweetIds()
1350 if (MyCommon._endingFlag) return;
1352 this.noRTId = await this.Api.NoRetweetIds()
1353 .ConfigureAwait(false);
1355 this.GetNoRetweetSuccess = true;
1359 /// t.co の文字列長などの設定情報を更新します
1361 /// <exception cref="WebApiException"/>
1362 public async Task RefreshConfiguration()
1364 this.Configuration = await this.Api.Configuration()
1365 .ConfigureAwait(false);
1367 // TextConfiguration 相当の JSON を得る API が存在しないため、TransformedURLLength のみ help/configuration.json に合わせて更新する
1368 this.TextConfiguration.TransformedURLLength = this.Configuration.ShortUrlLengthHttps;
1371 public async Task GetListsApi()
1373 this.CheckAccountState();
1375 var ownedLists = await TwitterLists.GetAllItemsAsync(x =>
1376 this.Api.ListsOwnerships(this.Username, cursor: x, count: 1000))
1377 .ConfigureAwait(false);
1379 var subscribedLists = await TwitterLists.GetAllItemsAsync(x =>
1380 this.Api.ListsSubscriptions(this.Username, cursor: x, count: 1000))
1381 .ConfigureAwait(false);
1383 TabInformations.GetInstance().SubscribableLists = Enumerable.Concat(ownedLists, subscribedLists)
1384 .Select(x => new ListElement(x, this))
1388 public async Task DeleteList(long listId)
1390 await this.Api.ListsDestroy(listId)
1392 .ConfigureAwait(false);
1394 var tabinfo = TabInformations.GetInstance();
1396 tabinfo.SubscribableLists = tabinfo.SubscribableLists
1397 .Where(x => x.Id != listId)
1401 public async Task<ListElement> EditList(long listId, string new_name, bool isPrivate, string description)
1403 var response = await this.Api.ListsUpdate(listId, new_name, description, isPrivate)
1404 .ConfigureAwait(false);
1406 var list = await response.LoadJsonAsync()
1407 .ConfigureAwait(false);
1409 return new ListElement(list, this);
1412 public async Task<long> GetListMembers(long listId, List<UserInfo> lists, long cursor)
1414 this.CheckAccountState();
1416 var users = await this.Api.ListsMembers(listId, cursor)
1417 .ConfigureAwait(false);
1419 Array.ForEach(users.Users, u => lists.Add(new UserInfo(u)));
1421 return users.NextCursor;
1424 public async Task CreateListApi(string listName, bool isPrivate, string description)
1426 this.CheckAccountState();
1428 var response = await this.Api.ListsCreate(listName, description, isPrivate)
1429 .ConfigureAwait(false);
1431 var list = await response.LoadJsonAsync()
1432 .ConfigureAwait(false);
1434 TabInformations.GetInstance().SubscribableLists.Add(new ListElement(list, this));
1437 public async Task<bool> ContainsUserAtList(long listId, string user)
1439 this.CheckAccountState();
1443 await this.Api.ListsMembersShow(listId, user)
1444 .ConfigureAwait(false);
1448 catch (TwitterApiException ex)
1449 when (ex.ErrorResponse.Errors.Any(x => x.Code == TwitterErrorCode.NotFound))
1455 private void ExtractEntities(TwitterEntities entities, List<Tuple<long, string>> AtList, List<MediaInfo> media)
1457 if (entities != null)
1459 if (entities.Hashtags != null)
1463 this._hashList.AddRange(entities.Hashtags.Select(x => "#" + x.Text));
1466 if (entities.UserMentions != null)
1468 foreach (var ent in entities.UserMentions)
1470 AtList.Add(Tuple.Create(ent.Id, ent.ScreenName));
1473 if (entities.Media != null)
1477 foreach (var ent in entities.Media)
1479 if (!media.Any(x => x.Url == ent.MediaUrlHttps))
1481 if (ent.VideoInfo != null &&
1482 ent.Type == "animated_gif" || ent.Type == "video")
1484 //var videoUrl = ent.VideoInfo.Variants
1485 // .Where(v => v.ContentType == "video/mp4")
1486 // .OrderByDescending(v => v.Bitrate)
1487 // .Select(v => v.Url).FirstOrDefault();
1488 media.Add(new MediaInfo(ent.MediaUrlHttps, ent.AltText, ent.ExpandedUrl));
1491 media.Add(new MediaInfo(ent.MediaUrlHttps, ent.AltText, videoUrl: null));
1499 internal static string CreateHtmlAnchor(string text, TwitterEntities entities, TwitterQuotedStatusPermalink quotedStatusLink)
1501 // PostClass.ExpandedUrlInfo を使用して非同期に URL 展開を行うためここでは expanded_url を使用しない
1502 text = TweetFormatter.AutoLinkHtml(text, entities, keepTco: true);
1504 text = Regex.Replace(text, "(^|[^a-zA-Z0-9_/&##@@>=.~])(sm|nm)([0-9]{1,10})", "$1<a href=\"http://www.nicovideo.jp/watch/$2$3\">$2$3</a>");
1505 text = PreProcessUrl(text); //IDN置換
1507 if (quotedStatusLink != null)
1509 text += string.Format(" <a href=\"{0}\" title=\"{0}\">{1}</a>",
1510 WebUtility.HtmlEncode(quotedStatusLink.Url),
1511 WebUtility.HtmlEncode(quotedStatusLink.Display));
1517 private static readonly Uri SourceUriBase = new Uri("https://twitter.com/");
1520 /// Twitter APIから得たHTML形式のsource文字列を分析し、source名とURLに分離します
1522 internal static (string SourceText, Uri SourceUri) ParseSource(string sourceHtml)
1524 if (string.IsNullOrEmpty(sourceHtml))
1530 // sourceHtmlの例: <a href="http://twitter.com" rel="nofollow">Twitter Web Client</a>
1532 var match = Regex.Match(sourceHtml, "^<a href=\"(?<uri>.+?)\".*?>(?<text>.+)</a>$", RegexOptions.IgnoreCase);
1535 sourceText = WebUtility.HtmlDecode(match.Groups["text"].Value);
1538 var uriStr = WebUtility.HtmlDecode(match.Groups["uri"].Value);
1539 sourceUri = new Uri(SourceUriBase, uriStr);
1541 catch (UriFormatException)
1548 sourceText = WebUtility.HtmlDecode(sourceHtml);
1552 return (sourceText, sourceUri);
1555 public async Task<TwitterApiStatus> GetInfoApi()
1557 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return null;
1559 if (MyCommon._endingFlag) return null;
1561 var limits = await this.Api.ApplicationRateLimitStatus()
1562 .ConfigureAwait(false);
1564 MyCommon.TwitterApiInfo.UpdateFromJson(limits);
1566 return MyCommon.TwitterApiInfo;
1570 /// ブロック中のユーザーを更新します
1572 /// <exception cref="WebApiException"/>
1573 public async Task RefreshBlockIds()
1575 if (MyCommon._endingFlag) return;
1578 var newBlockIds = new HashSet<long>();
1581 var ret = await this.Api.BlocksIds(cursor)
1582 .ConfigureAwait(false);
1584 newBlockIds.UnionWith(ret.Ids);
1585 cursor = ret.NextCursor;
1586 } while (cursor != 0);
1588 newBlockIds.Remove(this.UserId); // 元のソースにあったので一応残しておく
1590 TabInformations.GetInstance().BlockIds = newBlockIds;
1594 /// ミュート中のユーザーIDを更新します
1596 /// <exception cref="WebApiException"/>
1597 public async Task RefreshMuteUserIdsAsync()
1599 if (MyCommon._endingFlag) return;
1601 var ids = await TwitterIds.GetAllItemsAsync(x => this.Api.MutesUsersIds(x))
1602 .ConfigureAwait(false);
1604 TabInformations.GetInstance().MuteUserIds = new HashSet<long>(ids);
1607 public string[] GetHashList()
1612 hashArray = _hashList.ToArray();
1618 public string AccessToken
1619 => ((TwitterApiConnection)this.Api.Connection).AccessToken;
1621 public string AccessTokenSecret
1622 => ((TwitterApiConnection)this.Api.Connection).AccessSecret;
1624 private void CheckAccountState()
1626 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
1627 throw new WebApiException("Auth error. Check your account");
1630 private void CheckAccessLevel(TwitterApiAccessLevel accessLevelFlags)
1632 if (!this.AccessLevel.HasFlag(accessLevelFlags))
1633 throw new WebApiException("Auth Err:try to re-authorization.");
1636 public int GetTextLengthRemain(string postText)
1638 var matchDm = Twitter.DMSendTextRegex.Match(postText);
1639 if (matchDm.Success)
1640 return this.GetTextLengthRemainDM(matchDm.Groups["body"].Value);
1642 return this.GetTextLengthRemainWeighted(postText);
1645 private int GetTextLengthRemainDM(string postText)
1650 while (pos < postText.Length)
1654 if (char.IsSurrogatePair(postText, pos))
1655 pos += 2; // サロゲートペアの場合は2文字分進める
1660 var urls = TweetExtractor.ExtractUrls(postText);
1661 foreach (var url in urls)
1663 var shortUrlLength = url.StartsWith("https://", StringComparison.OrdinalIgnoreCase)
1664 ? this.Configuration.ShortUrlLengthHttps
1665 : this.Configuration.ShortUrlLength;
1667 textLength += shortUrlLength - url.Length;
1670 return this.Configuration.DmTextCharacterLimit - textLength;
1673 private int GetTextLengthRemainWeighted(string postText)
1675 var config = this.TextConfiguration;
1676 var totalWeight = 0;
1678 var urls = TweetExtractor.ExtractUrlEntities(postText).ToArray();
1681 while (pos < postText.Length)
1683 var urlEntity = urls.FirstOrDefault(x => x.Indices[0] == pos);
1684 if (urlEntity != null)
1686 totalWeight += config.TransformedURLLength * config.Scale;
1688 var urlLength = urlEntity.Indices[1] - urlEntity.Indices[0];
1694 var codepoint = postText.GetCodepointAtSafe(pos);
1695 var weight = config.DefaultWeight;
1697 foreach (var weightRange in config.Ranges)
1699 if (codepoint >= weightRange.Start && codepoint <= weightRange.End)
1701 weight = weightRange.Weight;
1706 totalWeight += weight;
1708 var isSurrogatePair = codepoint > 0xffff;
1709 if (isSurrogatePair)
1710 pos += 2; // サロゲートペアの場合は2文字分進める
1715 var remainWeight = config.MaxWeightedTweetLength * config.Scale - totalWeight;
1717 return remainWeight / config.Scale;
1721 #region "UserStream"
1722 public string TrackWord { get; set; } = "";
1723 public bool AllAtReply { get; set; } = false;
1725 public event EventHandler NewPostFromStream;
1726 public event EventHandler UserStreamStarted;
1727 public event EventHandler UserStreamStopped;
1728 public event EventHandler<PostDeletedEventArgs> PostDeleted;
1729 public event EventHandler<UserStreamEventReceivedEventArgs> UserStreamEventReceived;
1730 private DateTimeUtc _lastUserstreamDataReceived;
1731 private TwitterUserstream userStream;
1733 public class FormattedEvent
1735 public MyCommon.EVENTTYPE Eventtype { get; set; }
1736 public DateTimeUtc CreatedAt { get; set; }
1737 public string Event { get; set; }
1738 public string Username { get; set; }
1739 public string Target { get; set; }
1740 public Int64 Id { get; set; }
1741 public bool IsMe { get; set; }
1744 public List<FormattedEvent> StoredEvent { get; } = new List<FormattedEvent>();
1746 private readonly IReadOnlyDictionary<string, MyCommon.EVENTTYPE> eventTable = new Dictionary<string, MyCommon.EVENTTYPE>
1748 ["favorite"] = MyCommon.EVENTTYPE.Favorite,
1749 ["unfavorite"] = MyCommon.EVENTTYPE.Unfavorite,
1750 ["follow"] = MyCommon.EVENTTYPE.Follow,
1751 ["list_member_added"] = MyCommon.EVENTTYPE.ListMemberAdded,
1752 ["list_member_removed"] = MyCommon.EVENTTYPE.ListMemberRemoved,
1753 ["block"] = MyCommon.EVENTTYPE.Block,
1754 ["unblock"] = MyCommon.EVENTTYPE.Unblock,
1755 ["user_update"] = MyCommon.EVENTTYPE.UserUpdate,
1756 ["deleted"] = MyCommon.EVENTTYPE.Deleted,
1757 ["list_created"] = MyCommon.EVENTTYPE.ListCreated,
1758 ["list_destroyed"] = MyCommon.EVENTTYPE.ListDestroyed,
1759 ["list_updated"] = MyCommon.EVENTTYPE.ListUpdated,
1760 ["unfollow"] = MyCommon.EVENTTYPE.Unfollow,
1761 ["list_user_subscribed"] = MyCommon.EVENTTYPE.ListUserSubscribed,
1762 ["list_user_unsubscribed"] = MyCommon.EVENTTYPE.ListUserUnsubscribed,
1763 ["mute"] = MyCommon.EVENTTYPE.Mute,
1764 ["unmute"] = MyCommon.EVENTTYPE.Unmute,
1765 ["quoted_tweet"] = MyCommon.EVENTTYPE.QuotedTweet,
1768 public bool IsUserstreamDataReceived
1769 => (DateTimeUtc.Now - this._lastUserstreamDataReceived).TotalSeconds < 31;
1771 private void userStream_StatusArrived(string line)
1773 this._lastUserstreamDataReceived = DateTimeUtc.Now;
1774 if (string.IsNullOrEmpty(line)) return;
1776 if (line.First() != '{' || line.Last() != '}')
1778 MyCommon.TraceOut("Invalid JSON (StatusArrived):" + Environment.NewLine + line);
1786 using (var jsonReader = JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(line), XmlDictionaryReaderQuotas.Max))
1788 var xElm = XElement.Load(jsonReader);
1789 if (xElm.Element("friends") != null)
1791 Debug.WriteLine("friends");
1794 else if (xElm.Element("delete") != null)
1796 Debug.WriteLine("delete");
1799 if ((idElm = xElm.Element("delete").Element("direct_message")?.Element("id")) != null)
1802 long.TryParse(idElm.Value, out id);
1804 this.PostDeleted?.Invoke(this, new PostDeletedEventArgs(id));
1806 else if ((idElm = xElm.Element("delete").Element("status")?.Element("id")) != null)
1809 long.TryParse(idElm.Value, out id);
1811 this.PostDeleted?.Invoke(this, new PostDeletedEventArgs(id));
1815 MyCommon.TraceOut("delete:" + line);
1818 for (int i = this.StoredEvent.Count - 1; i >= 0; i--)
1820 var sEvt = this.StoredEvent[i];
1821 if (sEvt.Id == id && (sEvt.Event == "favorite" || sEvt.Event == "unfavorite"))
1823 this.StoredEvent.RemoveAt(i);
1828 else if (xElm.Element("limit") != null)
1830 Debug.WriteLine(line);
1833 else if (xElm.Element("event") != null)
1835 Debug.WriteLine("event: " + xElm.Element("event").Value);
1836 CreateEventFromJson(line);
1839 else if (xElm.Element("direct_message") != null)
1841 Debug.WriteLine("direct_message");
1844 else if (xElm.Element("retweeted_status") != null)
1846 var sourceUserId = xElm.XPathSelectElement("/user/id_str").Value;
1847 var targetUserId = xElm.XPathSelectElement("/retweeted_status/user/id_str").Value;
1849 // 自分に関係しないリツイートの場合は無視する
1850 var selfUserId = this.UserId.ToString();
1851 if (sourceUserId == selfUserId || targetUserId == selfUserId)
1853 // 公式 RT をイベントとしても扱う
1854 var evt = CreateEventFromRetweet(xElm);
1857 this.StoredEvent.Insert(0, evt);
1859 this.UserStreamEventReceived?.Invoke(this, new UserStreamEventReceivedEventArgs(evt));
1863 // 従来通り公式 RT の表示も行うため return しない
1865 else if (xElm.Element("scrub_geo") != null)
1869 TabInformations.GetInstance().ScrubGeoReserve(long.Parse(xElm.Element("scrub_geo").Element("user_id").Value),
1870 long.Parse(xElm.Element("scrub_geo").Element("up_to_status_id").Value));
1874 MyCommon.TraceOut("scrub_geo:" + line);
1884 var message = TwitterStreamEventDirectMessage.ParseJson(line).DirectMessage;
1885 this.CreateDirectMessagesFromJson(new[] { message }, MyCommon.WORKERTYPE.UserStream, false);
1887 catch (SerializationException ex)
1889 throw TwitterApiException.CreateFromException(ex, line);
1896 var status = TwitterStatusCompat.ParseJson(line);
1897 this.CreatePostsFromJson(new[] { status.Normalize() }, MyCommon.WORKERTYPE.UserStream, null, false);
1899 catch (SerializationException ex)
1901 throw TwitterApiException.CreateFromException(ex, line);
1905 catch (WebApiException ex)
1907 MyCommon.TraceOut(ex);
1910 catch (XmlException)
1912 MyCommon.TraceOut("XmlException (StatusArrived): " + line);
1914 catch(NullReferenceException)
1916 MyCommon.TraceOut("NullRef StatusArrived: " + line);
1919 this.NewPostFromStream?.Invoke(this, EventArgs.Empty);
1923 /// UserStreamsから受信した公式RTをイベントに変換します
1925 private FormattedEvent CreateEventFromRetweet(XElement xElm)
1927 return new FormattedEvent
1929 Eventtype = MyCommon.EVENTTYPE.Retweet,
1931 CreatedAt = MyCommon.DateTimeParse(xElm.XPathSelectElement("/created_at").Value),
1932 IsMe = xElm.XPathSelectElement("/user/id_str").Value == this.UserId.ToString(),
1933 Username = xElm.XPathSelectElement("/user/screen_name").Value,
1934 Target = string.Format("@{0}:{1}", new[]
1936 xElm.XPathSelectElement("/retweeted_status/user/screen_name").Value,
1937 WebUtility.HtmlDecode(xElm.XPathSelectElement("/retweeted_status/text").Value),
1939 Id = long.Parse(xElm.XPathSelectElement("/retweeted_status/id_str").Value),
1943 private void CreateEventFromJson(string content)
1945 TwitterStreamEvent eventData = null;
1948 eventData = TwitterStreamEvent.ParseJson(content);
1950 catch(SerializationException ex)
1952 MyCommon.TraceOut(ex, "Event Serialize Exception!" + Environment.NewLine + content);
1956 MyCommon.TraceOut(ex, "Event Exception!" + Environment.NewLine + content);
1959 var evt = new FormattedEvent
1961 CreatedAt = MyCommon.DateTimeParse(eventData.CreatedAt),
1962 Event = eventData.Event,
1963 Username = eventData.Source.ScreenName,
1964 IsMe = eventData.Source.Id == this.UserId,
1965 Eventtype = eventTable.TryGetValue(eventData.Event, out var eventType) ? eventType : MyCommon.EVENTTYPE.None,
1968 TwitterStreamEvent<TwitterStatusCompat> tweetEvent;
1969 TwitterStatus tweet;
1971 switch (eventData.Event)
1973 case "access_revoked":
1974 case "access_unrevoked":
1976 case "user_suspend":
1979 if (eventData.Target.Id == this.UserId)
1981 if (!this.followerId.Contains(eventData.Source.Id)) this.followerId.Add(eventData.Source.Id);
1985 return; //Block後のUndoをすると、SourceとTargetが逆転したfollowイベントが帰ってくるため。
1990 evt.Target = "@" + eventData.Target.ScreenName;
1992 case "favorited_retweet":
1993 case "retweeted_retweet":
1997 tweetEvent = TwitterStreamEvent<TwitterStatusCompat>.ParseJson(content);
1998 tweet = tweetEvent.TargetObject.Normalize();
1999 evt.Target = "@" + tweet.User.ScreenName + ":" + WebUtility.HtmlDecode(tweet.FullText);
2002 if (SettingManager.Common.IsRemoveSameEvent)
2004 if (this.StoredEvent.Any(ev => ev.Username == evt.Username && ev.Eventtype == evt.Eventtype && ev.Target == evt.Target))
2008 var tabinfo = TabInformations.GetInstance();
2010 var statusId = tweet.Id;
2011 if (!tabinfo.Posts.TryGetValue(statusId, out var post))
2014 if (eventData.Event == "favorite")
2016 var favTab = tabinfo.GetTabByType(MyCommon.TabUsageType.Favorites);
2017 favTab.AddPostQueue(post);
2019 if (tweetEvent.Source.Id == this.UserId)
2023 else if (tweetEvent.Target.Id == this.UserId)
2025 post.FavoritedCount++;
2027 if (SettingManager.Common.FavEventUnread)
2028 tabinfo.SetReadAllTab(post.StatusId, read: false);
2033 if (tweetEvent.Source.Id == this.UserId)
2037 else if (tweetEvent.Target.Id == this.UserId)
2039 post.FavoritedCount = Math.Max(0, post.FavoritedCount - 1);
2043 case "quoted_tweet":
2044 if (evt.IsMe) return;
2046 tweetEvent = TwitterStreamEvent<TwitterStatusCompat>.ParseJson(content);
2047 tweet = tweetEvent.TargetObject.Normalize();
2048 evt.Target = "@" + tweet.User.ScreenName + ":" + WebUtility.HtmlDecode(tweet.FullText);
2051 if (SettingManager.Common.IsRemoveSameEvent)
2053 if (this.StoredEvent.Any(ev => ev.Username == evt.Username && ev.Eventtype == evt.Eventtype && ev.Target == evt.Target))
2057 case "list_member_added":
2058 case "list_member_removed":
2059 case "list_created":
2060 case "list_destroyed":
2061 case "list_updated":
2062 case "list_user_subscribed":
2063 case "list_user_unsubscribed":
2064 var listEvent = TwitterStreamEvent<TwitterList>.ParseJson(content);
2065 evt.Target = listEvent.TargetObject.FullName;
2068 if (!TabInformations.GetInstance().BlockIds.Contains(eventData.Target.Id)) TabInformations.GetInstance().BlockIds.Add(eventData.Target.Id);
2072 if (TabInformations.GetInstance().BlockIds.Contains(eventData.Target.Id)) TabInformations.GetInstance().BlockIds.Remove(eventData.Target.Id);
2081 evt.Target = "@" + eventData.Target.ScreenName;
2082 if (!TabInformations.GetInstance().MuteUserIds.Contains(eventData.Target.Id))
2084 TabInformations.GetInstance().MuteUserIds.Add(eventData.Target.Id);
2088 evt.Target = "@" + eventData.Target.ScreenName;
2089 if (TabInformations.GetInstance().MuteUserIds.Contains(eventData.Target.Id))
2091 TabInformations.GetInstance().MuteUserIds.Remove(eventData.Target.Id);
2096 MyCommon.TraceOut("Unknown Event:" + evt.Event + Environment.NewLine + content);
2099 this.StoredEvent.Insert(0, evt);
2101 this.UserStreamEventReceived?.Invoke(this, new UserStreamEventReceivedEventArgs(evt));
2104 private void userStream_Started()
2105 => this.UserStreamStarted?.Invoke(this, EventArgs.Empty);
2107 private void userStream_Stopped()
2108 => this.UserStreamStopped?.Invoke(this, EventArgs.Empty);
2110 public bool UserStreamActive
2111 => this.userStream != null && this.userStream.IsStreamActive;
2113 public void StartUserStream()
2115 var newStream = new TwitterUserstream(this.Api);
2117 newStream.StatusArrived += userStream_StatusArrived;
2118 newStream.Started += userStream_Started;
2119 newStream.Stopped += userStream_Stopped;
2121 newStream.Start(this.AllAtReply, this.TrackWord);
2123 var oldStream = Interlocked.Exchange(ref this.userStream, newStream);
2124 oldStream?.Dispose();
2127 public void StopUserStream()
2129 var oldStream = Interlocked.Exchange(ref this.userStream, null);
2130 oldStream?.Dispose();
2133 public void ReconnectUserStream()
2135 if (this.userStream != null)
2137 this.StartUserStream();
2141 private class TwitterUserstream : IDisposable
2143 public bool AllAtReplies { get; private set; }
2144 public string TrackWords { get; private set; }
2146 public bool IsStreamActive { get; private set; }
2148 public event Action<string> StatusArrived;
2149 public event Action Stopped;
2150 public event Action Started;
2152 private TwitterApi twitterApi;
2154 private Task streamTask;
2155 private CancellationTokenSource streamCts;
2157 public TwitterUserstream(TwitterApi twitterApi)
2158 => this.twitterApi = twitterApi;
2160 public void Start(bool allAtReplies, string trackwords)
2162 this.AllAtReplies = allAtReplies;
2163 this.TrackWords = trackwords;
2165 var cts = new CancellationTokenSource();
2167 this.streamCts = cts;
2168 this.streamTask = Task.Run(async () =>
2172 await this.UserStreamLoop(cts.Token)
2173 .ConfigureAwait(false);
2175 catch (OperationCanceledException) { }
2181 this.streamCts?.Cancel();
2183 // streamTask の完了を待たずに IsStreamActive を false にセットする
2184 this.IsStreamActive = false;
2185 this.Stopped?.Invoke();
2188 private async Task UserStreamLoop(CancellationToken cancellationToken)
2190 TimeSpan sleep = TimeSpan.Zero;
2193 if (sleep != TimeSpan.Zero)
2195 await Task.Delay(sleep, cancellationToken)
2196 .ConfigureAwait(false);
2197 sleep = TimeSpan.Zero;
2200 if (!MyCommon.IsNetworkAvailable())
2202 sleep = TimeSpan.FromSeconds(30);
2206 this.IsStreamActive = true;
2207 this.Started?.Invoke();
2211 var replies = this.AllAtReplies ? "all" : null;
2213 using (var stream = await this.twitterApi.UserStreams(replies, this.TrackWords)
2214 .ConfigureAwait(false))
2215 using (var reader = new StreamReader(stream))
2217 while (!reader.EndOfStream)
2219 var line = await reader.ReadLineAsync()
2220 .ConfigureAwait(false);
2222 cancellationToken.ThrowIfCancellationRequested();
2224 this.StatusArrived?.Invoke(line);
2228 // キャンセルされていないのにストリームが終了した場合
2229 sleep = TimeSpan.FromSeconds(30);
2231 catch (TwitterApiException) { sleep = TimeSpan.FromSeconds(30); }
2232 catch (IOException) { sleep = TimeSpan.FromSeconds(30); }
2233 catch (OperationCanceledException)
2235 if (cancellationToken.IsCancellationRequested)
2238 // cancellationToken によるキャンセルではない(=タイムアウトエラー)
2239 sleep = TimeSpan.FromSeconds(30);
2241 catch (Exception ex)
2243 MyCommon.ExceptionOut(ex);
2244 sleep = TimeSpan.FromSeconds(30);
2248 this.IsStreamActive = false;
2249 this.Stopped?.Invoke();
2254 private bool disposed = false;
2256 public void Dispose()
2261 this.disposed = true;
2265 this.Started = null;
2266 this.Stopped = null;
2267 this.StatusArrived = null;
2272 #region "IDisposable Support"
2273 private bool disposedValue; // 重複する呼び出しを検出するには
2276 protected virtual void Dispose(bool disposing)
2278 if (!this.disposedValue)
2282 this.StopUserStream();
2285 this.disposedValue = true;
2288 //protected Overrides void Finalize()
2290 // // このコードを変更しないでください。クリーンアップ コードを上の Dispose(bool disposing) に記述します。
2292 // MyBase.Finalize()
2295 // このコードは、破棄可能なパターンを正しく実装できるように Visual Basic によって追加されました。
2296 public void Dispose()
2298 // このコードを変更しないでください。クリーンアップ コードを上の Dispose(bool disposing) に記述します。
2300 GC.SuppressFinalize(this);
2305 public class PostDeletedEventArgs : EventArgs
2307 public long StatusId { get; }
2309 public PostDeletedEventArgs(long statusId)
2310 => this.StatusId = statusId;
2313 public class UserStreamEventReceivedEventArgs : EventArgs
2315 public Twitter.FormattedEvent EventData { get; }
2317 public UserStreamEventReceivedEventArgs(Twitter.FormattedEvent eventData)
2318 => this.EventData = eventData;