From 6b45ef02a3b3900a0d9e3b2a9954fb6475d7b7d5 Mon Sep 17 00:00:00 2001 From: Kimura Youichi Date: Sat, 11 May 2024 04:05:49 +0900 Subject: [PATCH] =?UTF-8?q?TwitterApi=E3=82=AF=E3=83=A9=E3=82=B9=E3=81=8B?= =?UTF-8?q?=E3=82=89TwitterAccountState=E3=81=B8=E3=81=AE=E4=BE=9D?= =?UTF-8?q?=E5=AD=98=E3=82=92=E9=99=A4=E5=8E=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- OpenTween.Tests/Api/TwitterApiTest.cs | 13 +++------- OpenTween/Api/TwitterApi.cs | 12 +-------- OpenTween/AppendSettingDialog.cs | 2 +- OpenTween/SendErrorReportForm.cs | 2 +- OpenTween/Tween.cs | 6 ++--- OpenTween/Twitter.cs | 36 +++++++++++++++------------ 6 files changed, 30 insertions(+), 41 deletions(-) diff --git a/OpenTween.Tests/Api/TwitterApiTest.cs b/OpenTween.Tests/Api/TwitterApiTest.cs index f960eebe4..96fb47274 100644 --- a/OpenTween.Tests/Api/TwitterApiTest.cs +++ b/OpenTween.Tests/Api/TwitterApiTest.cs @@ -32,7 +32,6 @@ using Moq; using OpenTween.Api.DataModel; using OpenTween.Connection; -using OpenTween.SocialProtocol.Twitter; using Xunit; namespace OpenTween.Api @@ -58,21 +57,17 @@ public void Initialize_Test() Assert.IsType(apiConnectionNone.Credential); var credential = new TwitterCredentialOAuth1(TwitterAppToken.GetDefault(), "*** AccessToken ***", "*** AccessSecret ***"); - var accountState = new TwitterAccountState(100L, "hogehoge"); - using var apiConnection = new TwitterApiConnection(credential, accountState); - twitterApi.Initialize(apiConnection, accountState); + using var apiConnection = new TwitterApiConnection(credential, new()); + twitterApi.Initialize(apiConnection); Assert.Same(apiConnection, twitterApi.Connection); - Assert.Same(accountState, twitterApi.AccountState); // 複数回 Initialize を実行した場合は新たに TwitterApiConnection が生成される var credential2 = new TwitterCredentialOAuth1(TwitterAppToken.GetDefault(), "*** AccessToken2 ***", "*** AccessSecret2 ***"); - var accountState2 = new TwitterAccountState(200L, "foobar"); - using var apiConnection2 = new TwitterApiConnection(credential2, accountState2); - twitterApi.Initialize(apiConnection2, accountState2); + using var apiConnection2 = new TwitterApiConnection(credential2, new()); + twitterApi.Initialize(apiConnection2); Assert.Same(apiConnection2, twitterApi.Connection); - Assert.Same(accountState2, twitterApi.AccountState); } private Mock CreateApiConnectionMock(Action verifyRequest) diff --git a/OpenTween/Api/TwitterApi.cs b/OpenTween/Api/TwitterApi.cs index 940e75459..07f6714a1 100644 --- a/OpenTween/Api/TwitterApi.cs +++ b/OpenTween/Api/TwitterApi.cs @@ -32,18 +32,11 @@ using OpenTween.Api.DataModel; using OpenTween.Connection; using OpenTween.Models; -using OpenTween.SocialProtocol.Twitter; namespace OpenTween.Api { public sealed class TwitterApi : IDisposable { - public long CurrentUserId - => this.AccountState.UserId; - - public string CurrentScreenName - => this.AccountState.UserName; - public IApiConnection Connection => this.ApiConnection; internal IApiConnection ApiConnection; @@ -51,15 +44,12 @@ public string CurrentScreenName public APIAuthType AuthType => ((TwitterApiConnection)this.ApiConnection).Credential.AuthType; - public TwitterAccountState AccountState { get; private set; } = new(); - public TwitterApi() => this.ApiConnection = new TwitterApiConnection(); - public void Initialize(IApiConnection apiConnection, TwitterAccountState accountState) + public void Initialize(IApiConnection apiConnection) { this.ApiConnection = apiConnection; - this.AccountState = accountState; } public async Task StatusesHomeTimeline(int? count = null, TwitterStatusId? maxId = null, TwitterStatusId? sinceId = null) diff --git a/OpenTween/AppendSettingDialog.cs b/OpenTween/AppendSettingDialog.cs index 2d7c72b1b..aecd38fac 100644 --- a/OpenTween/AppendSettingDialog.cs +++ b/OpenTween/AppendSettingDialog.cs @@ -189,7 +189,7 @@ private async void AddAccountButton_Click(object sender, EventArgs e) using var twitterApi = new TwitterApi(); using var apiConnection = new TwitterApiConnection(new TwitterCredentialCookie(appToken), new()); - twitterApi.Initialize(apiConnection, new()); + twitterApi.Initialize(apiConnection); var twitterUser = await twitterApi.AccountVerifyCredentials(); newAccount.UserId = twitterUser.Id; newAccount.Username = twitterUser.ScreenName; diff --git a/OpenTween/SendErrorReportForm.cs b/OpenTween/SendErrorReportForm.cs index 06d63ebdf..067d030b3 100644 --- a/OpenTween/SendErrorReportForm.cs +++ b/OpenTween/SendErrorReportForm.cs @@ -204,7 +204,7 @@ private bool CheckDmAvailable() if (this.tw == null || !this.tw.AccessLevel.HasFlag(TwitterApiAccessLevel.DirectMessage)) return false; - if (this.tw.Api.AccountState.HasUnrecoverableError) + if (this.tw.AccountState.HasUnrecoverableError) return false; return true; diff --git a/OpenTween/Tween.cs b/OpenTween/Tween.cs index f8f3398a8..f2004e2f0 100644 --- a/OpenTween/Tween.cs +++ b/OpenTween/Tween.cs @@ -1263,13 +1263,13 @@ private void NotifyIcon1_BalloonTipClicked(object sender, EventArgs e) private bool CheckAccountValid() { - if (this.tw.Api.AccountState.HasUnrecoverableError) + if (this.tw.AccountState.HasUnrecoverableError) { this.errorCount += 1; if (this.errorCount > 5) { this.errorCount = 0; - this.tw.Api.AccountState.HasUnrecoverableError = false; + this.tw.AccountState.HasUnrecoverableError = false; return true; } return false; @@ -2678,7 +2678,7 @@ private async void SettingStripMenuItem_Click(object sender, EventArgs e) } } - this.tw.Api.AccountState.HasUnrecoverableError = false; + this.tw.AccountState.HasUnrecoverableError = false; this.TopMost = this.settings.Common.AlwaysTop; this.SaveConfigsAll(false); diff --git a/OpenTween/Twitter.cs b/OpenTween/Twitter.cs index b8a148239..d1038d75b 100644 --- a/OpenTween/Twitter.cs +++ b/OpenTween/Twitter.cs @@ -160,6 +160,8 @@ public class Twitter : IDisposable public TwitterApi Api { get; } + public TwitterAccountState AccountState { get; private set; } = new(); + public TwitterConfiguration Configuration { get; private set; } public TwitterTextConfiguration TextConfiguration { get; private set; } @@ -195,7 +197,7 @@ protected void ResetApiStatus() public void ClearAuthInfo() { - this.Api.AccountState.HasUnrecoverableError = true; + this.AccountState.HasUnrecoverableError = true; this.ResetApiStatus(); } @@ -216,13 +218,15 @@ public async Task VerifyCredentialsAsync() var user = await this.Api.AccountVerifyCredentials() .ConfigureAwait(false); - this.Api.AccountState.UpdateFromUser(user); + this.AccountState.UpdateFromUser(user); } public void Initialize(TwitterApiConnection apiConnection, TwitterAccountState accountState) { + this.AccountState = accountState; + this.ResetApiStatus(); - this.Api.Initialize(apiConnection, accountState); + this.Api.Initialize(apiConnection); } public async Task PostStatus(PostStatusParams param) @@ -270,7 +274,7 @@ await this.SendDirectMessage(param.Text, mediaId) .ConfigureAwait(false); } - this.Api.AccountState.UpdateFromUser(status.User); + this.AccountState.UpdateFromUser(status.User); if (status.IdStr == this.previousStatusId) throw new WebApiException("OK:Delaying?"); @@ -389,21 +393,21 @@ public async Task GetUserInfo(string screenName) } public string Username - => this.Api.CurrentScreenName; + => this.AccountState.UserName; public long UserId - => this.Api.CurrentUserId; + => this.AccountState.UserId; public bool RestrictFavCheck { get; set; } public int? FollowersCount - => this.Api.AccountState.FollowersCount; + => this.AccountState.FollowersCount; public int? FriendsCount - => this.Api.AccountState.FriendsCount; + => this.AccountState.FriendsCount; public int? StatusesCount - => this.Api.AccountState.StatusesCount; + => this.AccountState.StatusesCount; /// /// 渡された取得件数がWORKERTYPEに応じた取得可能範囲に収まっているか検証する @@ -622,7 +626,7 @@ private PostClass CreatePostsFromStatusData(TwitterStatus status, bool firstLoad internal PostClass CreatePostsFromStatusData(TwitterStatus status, bool firstLoad, bool favTweet) { - var post = this.postFactory.CreateFromStatus(status, this.UserId, this.Api.AccountState.FollowerIds, firstLoad, favTweet); + var post = this.postFactory.CreateFromStatus(status, this.UserId, this.AccountState.FollowerIds, firstLoad, favTweet); _ = this.urlExpander.Expand(post); return post; @@ -638,7 +642,7 @@ internal PostClass[] CreatePostsFromJson(TwitterStatus[] statuses, bool firstLoa } internal PostClass[] FilterNoRetweetUserPosts(PostClass[] posts) - => posts.Where(x => x.RetweetedByUserId == null || !this.Api.AccountState.NoRetweetUserIds.Contains(x.RetweetedByUserId.Value)).ToArray(); + => posts.Where(x => x.RetweetedByUserId == null || !this.AccountState.NoRetweetUserIds.Contains(x.RetweetedByUserId.Value)).ToArray(); public async Task GetListStatus(ListTimelineTabModel tab, bool more, bool firstLoad) { @@ -1083,8 +1087,8 @@ public async Task RefreshFollowerIds() } while (cursor != 0); - this.Api.AccountState.FollowerIds = newFollowerIds.ToHashSet(); - TabInformations.GetInstance().RefreshOwl(this.Api.AccountState.FollowerIds); + this.AccountState.FollowerIds = newFollowerIds.ToHashSet(); + TabInformations.GetInstance().RefreshOwl(this.AccountState.FollowerIds); this.GetFollowersSuccess = true; } @@ -1100,7 +1104,7 @@ public async Task RefreshNoRetweetIds() var noRetweetUserIds = await this.Api.NoRetweetIds() .ConfigureAwait(false); - this.Api.AccountState.NoRetweetUserIds = new HashSet(noRetweetUserIds); + this.AccountState.NoRetweetUserIds = new HashSet(noRetweetUserIds); this.GetNoRetweetSuccess = true; } @@ -1203,7 +1207,7 @@ await this.Api.ListsMembersShow(listId, user) public async Task GetInfoApi() { - if (this.Api.AccountState.HasUnrecoverableError) + if (this.AccountState.HasUnrecoverableError) return null; if (MyCommon.EndingFlag) return null; @@ -1261,7 +1265,7 @@ public string[] GetHashList() internal void CheckAccountState() { - if (this.Api.AccountState.HasUnrecoverableError) + if (this.AccountState.HasUnrecoverableError) throw new WebApiException("Auth error. Check your account"); }