From 04aca30ccbb27ab559328a5c681d58c0a199bc96 Mon Sep 17 00:00:00 2001 From: Kimura Youichi Date: Sat, 11 May 2024 03:09:54 +0900 Subject: [PATCH 1/5] =?UTF-8?q?TweenMain.CheckAccountValid=E3=82=92?= =?UTF-8?q?=E3=82=A4=E3=83=B3=E3=82=B9=E3=82=BF=E3=83=B3=E3=82=B9=E3=83=A1?= =?UTF-8?q?=E3=82=BD=E3=83=83=E3=83=89=E3=81=AB=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- OpenTween/Tween.cs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/OpenTween/Tween.cs b/OpenTween/Tween.cs index 98ae2a8cd..45a1e6596 100644 --- a/OpenTween/Tween.cs +++ b/OpenTween/Tween.cs @@ -1259,22 +1259,22 @@ private void NotifyIcon1_BalloonTipClicked(object sender, EventArgs e) this.BringToFront(); } - private static int errorCount = 0; + private int errorCount = 0; - private static bool CheckAccountValid() + private bool CheckAccountValid() { if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) { - errorCount += 1; - if (errorCount > 5) + this.errorCount += 1; + if (this.errorCount > 5) { - errorCount = 0; + this.errorCount = 0; Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid; return true; } return false; } - errorCount = 0; + this.errorCount = 0; return true; } @@ -1366,7 +1366,7 @@ private async Task FavAddAsyncInternal(IProgress p, CancellationToken ct if (ct.IsCancellationRequested) return; - if (!CheckAccountValid()) + if (!this.CheckAccountValid()) throw new WebApiException("Auth error. Check your account"); if (!tab.Posts.TryGetValue(statusId, out var post)) @@ -1480,7 +1480,7 @@ private async Task FavRemoveAsyncInternal(IProgress p, CancellationToken if (ct.IsCancellationRequested) return; - if (!CheckAccountValid()) + if (!this.CheckAccountValid()) throw new WebApiException("Auth error. Check your account"); var successIds = new List(); @@ -1598,7 +1598,7 @@ private async Task PostMessageAsyncInternal( if (ct.IsCancellationRequested) return; - if (!CheckAccountValid()) + if (!this.CheckAccountValid()) throw new WebApiException("Auth error. Check your account"); p.Report("Posting..."); @@ -1739,7 +1739,7 @@ private async Task RetweetAsyncInternal(IProgress p, CancellationToken c if (ct.IsCancellationRequested) return; - if (!CheckAccountValid()) + if (!this.CheckAccountValid()) throw new WebApiException("Auth error. Check your account"); p.Report("Posting..."); From d5769d8bee669b95bf1f8b7738165348600b1f95 Mon Sep 17 00:00:00 2001 From: Kimura Youichi Date: Sat, 11 May 2024 03:11:28 +0900 Subject: [PATCH 2/5] =?UTF-8?q?Twitter.AccountState=E3=82=92TwitterAccount?= =?UTF-8?q?State=E3=81=AE=E3=83=97=E3=83=AD=E3=83=91=E3=83=86=E3=82=A3?= =?UTF-8?q?=E3=81=AB=E7=A7=BB=E5=8B=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- OpenTween/Api/TwitterApi.cs | 4 +-- OpenTween/Connection/TwitterApiConnection.cs | 25 +++++++++++++------ OpenTween/MyCommon.cs | 6 ----- OpenTween/SendErrorReportForm.cs | 2 +- .../Twitter/TwitterAccountState.cs | 2 ++ OpenTween/Tween.cs | 6 ++--- OpenTween/Twitter.cs | 11 ++++---- 7 files changed, 30 insertions(+), 26 deletions(-) diff --git a/OpenTween/Api/TwitterApi.cs b/OpenTween/Api/TwitterApi.cs index 3ce2ddd6c..2c6d40081 100644 --- a/OpenTween/Api/TwitterApi.cs +++ b/OpenTween/Api/TwitterApi.cs @@ -53,13 +53,13 @@ public string CurrentScreenName public TwitterAccountState AccountState { get; private set; } = new(); public TwitterApi() - => this.ApiConnection = new TwitterApiConnection(new TwitterCredentialNone()); + => this.ApiConnection = new TwitterApiConnection(); public void Initialize(ITwitterCredential credential, TwitterAccountState accountState) { this.AuthType = credential.AuthType; - var newInstance = new TwitterApiConnection(credential); + var newInstance = new TwitterApiConnection(credential, accountState); var oldInstance = Interlocked.Exchange(ref this.ApiConnection, newInstance); oldInstance?.Dispose(); diff --git a/OpenTween/Connection/TwitterApiConnection.cs b/OpenTween/Connection/TwitterApiConnection.cs index 64833db4f..60c6ec4cf 100644 --- a/OpenTween/Connection/TwitterApiConnection.cs +++ b/OpenTween/Connection/TwitterApiConnection.cs @@ -36,6 +36,7 @@ using System.Web; using OpenTween.Api; using OpenTween.Api.DataModel; +using OpenTween.SocialProtocol.Twitter; namespace OpenTween.Connection { @@ -56,14 +57,17 @@ public static string RestApiHost internal ITwitterCredential Credential { get; } + private readonly TwitterAccountState accountState; + public TwitterApiConnection() - : this(new TwitterCredentialNone()) + : this(new TwitterCredentialNone(), new()) { } - public TwitterApiConnection(ITwitterCredential credential) + public TwitterApiConnection(ITwitterCredential credential, TwitterAccountState accountState) { this.Credential = credential; + this.accountState = accountState; this.InitializeHttpClients(); Networking.WebProxyChanged += this.Networking_WebProxyChanged; @@ -99,7 +103,7 @@ public async Task SendAsync(IHttpRequest request) if (endpointName != null) MyCommon.TwitterApiInfo.UpdateFromHeader(responseMessage.Headers, endpointName); - await TwitterApiConnection.CheckStatusCode(responseMessage) + await TwitterApiConnection.CheckStatusCode(responseMessage, this.accountState) .ConfigureAwait(false); var response = new ApiResponse(responseMessage); @@ -165,13 +169,14 @@ public static async Task HandleTimeout(Func> fu return await task; } - protected static async Task CheckStatusCode(HttpResponseMessage response) + protected static async Task CheckStatusCode(HttpResponseMessage response, TwitterAccountState? accountState) { var statusCode = response.StatusCode; if ((int)statusCode >= 200 && (int)statusCode <= 299) { - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid; + if (accountState != null) + accountState.HasUnrecoverableError = false; return; } @@ -185,7 +190,10 @@ protected static async Task CheckStatusCode(HttpResponseMessage response) if (string.IsNullOrWhiteSpace(responseText)) { if (statusCode == HttpStatusCode.Unauthorized) - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid; + { + if (accountState != null) + accountState.HasUnrecoverableError = true; + } throw new TwitterApiException(statusCode, responseText); } @@ -200,7 +208,8 @@ protected static async Task CheckStatusCode(HttpResponseMessage response) var errorCodes = error.Errors.Select(x => x.Code); if (errorCodes.Any(x => x == TwitterErrorCode.InternalError || x == TwitterErrorCode.SuspendedAccount)) { - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid; + if (accountState != null) + accountState.HasUnrecoverableError = true; } throw new TwitterApiException(statusCode, error, responseText); @@ -324,7 +333,7 @@ TwitterCredentialOAuth1 credential var responseText = await content.ReadAsStringAsync() .ConfigureAwait(false); - await TwitterApiConnection.CheckStatusCode(response) + await TwitterApiConnection.CheckStatusCode(response, accountState: null) .ConfigureAwait(false); var responseParams = HttpUtility.ParseQueryString(responseText); diff --git a/OpenTween/MyCommon.cs b/OpenTween/MyCommon.cs index 0732c2e2b..64b0fae69 100644 --- a/OpenTween/MyCommon.cs +++ b/OpenTween/MyCommon.cs @@ -191,12 +191,6 @@ public static class DEFAULTTAB public static bool DebugBuild = false; #endif - public enum ACCOUNT_STATE - { - Valid, - Invalid, - } - public enum REPLY_ICONSTATE { None, diff --git a/OpenTween/SendErrorReportForm.cs b/OpenTween/SendErrorReportForm.cs index c08f947e2..06d63ebdf 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 (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) + if (this.tw.Api.AccountState.HasUnrecoverableError) return false; return true; diff --git a/OpenTween/SocialProtocol/Twitter/TwitterAccountState.cs b/OpenTween/SocialProtocol/Twitter/TwitterAccountState.cs index c9bbb26fd..95dd391bf 100644 --- a/OpenTween/SocialProtocol/Twitter/TwitterAccountState.cs +++ b/OpenTween/SocialProtocol/Twitter/TwitterAccountState.cs @@ -42,6 +42,8 @@ public class TwitterAccountState public ISet NoRetweetUserIds { get; set; } = new HashSet(); + public bool HasUnrecoverableError { get; set; } = true; + public TwitterAccountState() : this(0L, "") { diff --git a/OpenTween/Tween.cs b/OpenTween/Tween.cs index 45a1e6596..00a8052ff 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 (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) + if (this.tw.Api.AccountState.HasUnrecoverableError) { this.errorCount += 1; if (this.errorCount > 5) { this.errorCount = 0; - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid; + this.tw.Api.AccountState.HasUnrecoverableError = false; return true; } return false; @@ -2678,7 +2678,7 @@ private async void SettingStripMenuItem_Click(object sender, EventArgs e) } } - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid; + this.tw.Api.AccountState.HasUnrecoverableError = false; this.TopMost = this.settings.Common.AlwaysTop; this.SaveConfigsAll(false); diff --git a/OpenTween/Twitter.cs b/OpenTween/Twitter.cs index 4dfdb24c8..e8cfb616d 100644 --- a/OpenTween/Twitter.cs +++ b/OpenTween/Twitter.cs @@ -195,7 +195,7 @@ protected void ResetApiStatus() public void ClearAuthInfo() { - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid; + this.Api.AccountState.HasUnrecoverableError = true; this.ResetApiStatus(); } @@ -223,7 +223,7 @@ public void Initialize(ITwitterCredential credential, TwitterAccountState accoun { // OAuth認証 if (credential is TwitterCredentialNone) - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid; + this.Api.AccountState.HasUnrecoverableError = true; this.ResetApiStatus(); this.Api.Initialize(credential, accountState); @@ -398,8 +398,6 @@ public string Username public long UserId => this.Api.CurrentUserId; - public static MyCommon.ACCOUNT_STATE AccountState { get; set; } = MyCommon.ACCOUNT_STATE.Valid; - public bool RestrictFavCheck { get; set; } public int? FollowersCount @@ -1209,7 +1207,8 @@ await this.Api.ListsMembersShow(listId, user) public async Task GetInfoApi() { - if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return null; + if (this.Api.AccountState.HasUnrecoverableError) + return null; if (MyCommon.EndingFlag) return null; @@ -1266,7 +1265,7 @@ public string[] GetHashList() internal void CheckAccountState() { - if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) + if (this.Api.AccountState.HasUnrecoverableError) throw new WebApiException("Auth error. Check your account"); } From 4e8eec65f2c429c8e3949cf1ea39688224a7932b Mon Sep 17 00:00:00 2001 From: Kimura Youichi Date: Sat, 11 May 2024 03:37:39 +0900 Subject: [PATCH 3/5] =?UTF-8?q?TwitterApiConnection=E3=82=A4=E3=83=B3?= =?UTF-8?q?=E3=82=B9=E3=82=BF=E3=83=B3=E3=82=B9=E3=81=AE=E7=94=9F=E6=88=90?= =?UTF-8?q?=E3=82=92TwitterAccount=E3=81=A7=E8=A1=8C=E3=81=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- OpenTween.Tests/Api/TwitterApiTest.cs | 19 ++++++++----------- OpenTween/Api/TwitterApi.cs | 12 ++++-------- OpenTween/AppendSettingDialog.cs | 3 ++- .../SocialProtocol/Twitter/TwitterAccount.cs | 16 +++++++++++++--- .../Twitter/TwitterGraphqlMutation.cs | 2 +- OpenTween/Twitter.cs | 8 ++------ 6 files changed, 30 insertions(+), 30 deletions(-) diff --git a/OpenTween.Tests/Api/TwitterApiTest.cs b/OpenTween.Tests/Api/TwitterApiTest.cs index 0ab1aad41..f960eebe4 100644 --- a/OpenTween.Tests/Api/TwitterApiTest.cs +++ b/OpenTween.Tests/Api/TwitterApiTest.cs @@ -54,27 +54,24 @@ private void MyCommonSetup() public void Initialize_Test() { using var twitterApi = new TwitterApi(); - var apiConnection = Assert.IsType(twitterApi.Connection); - Assert.IsType(apiConnection.Credential); + var apiConnectionNone = Assert.IsType(twitterApi.Connection); + Assert.IsType(apiConnectionNone.Credential); var credential = new TwitterCredentialOAuth1(TwitterAppToken.GetDefault(), "*** AccessToken ***", "*** AccessSecret ***"); var accountState = new TwitterAccountState(100L, "hogehoge"); - twitterApi.Initialize(credential, accountState); + using var apiConnection = new TwitterApiConnection(credential, accountState); + twitterApi.Initialize(apiConnection, accountState); - apiConnection = Assert.IsType(twitterApi.Connection); - Assert.Same(credential, apiConnection.Credential); + 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"); - twitterApi.Initialize(credential2, accountState2); + using var apiConnection2 = new TwitterApiConnection(credential2, accountState2); + twitterApi.Initialize(apiConnection2, accountState2); - var oldApiConnection = apiConnection; - Assert.True(oldApiConnection.IsDisposed); - - apiConnection = Assert.IsType(twitterApi.Connection); - Assert.Same(credential2, apiConnection.Credential); + Assert.Same(apiConnection2, twitterApi.Connection); Assert.Same(accountState2, twitterApi.AccountState); } diff --git a/OpenTween/Api/TwitterApi.cs b/OpenTween/Api/TwitterApi.cs index 2c6d40081..940e75459 100644 --- a/OpenTween/Api/TwitterApi.cs +++ b/OpenTween/Api/TwitterApi.cs @@ -48,21 +48,17 @@ public string CurrentScreenName internal IApiConnection ApiConnection; - public APIAuthType AuthType { get; private set; } = APIAuthType.None; + public APIAuthType AuthType + => ((TwitterApiConnection)this.ApiConnection).Credential.AuthType; public TwitterAccountState AccountState { get; private set; } = new(); public TwitterApi() => this.ApiConnection = new TwitterApiConnection(); - public void Initialize(ITwitterCredential credential, TwitterAccountState accountState) + public void Initialize(IApiConnection apiConnection, TwitterAccountState accountState) { - this.AuthType = credential.AuthType; - - var newInstance = new TwitterApiConnection(credential, accountState); - var oldInstance = Interlocked.Exchange(ref this.ApiConnection, newInstance); - oldInstance?.Dispose(); - + this.ApiConnection = apiConnection; this.AccountState = accountState; } diff --git a/OpenTween/AppendSettingDialog.cs b/OpenTween/AppendSettingDialog.cs index 8ce7e7c8d..2d7c72b1b 100644 --- a/OpenTween/AppendSettingDialog.cs +++ b/OpenTween/AppendSettingDialog.cs @@ -188,7 +188,8 @@ private async void AddAccountButton_Click(object sender, EventArgs e) }; using var twitterApi = new TwitterApi(); - twitterApi.Initialize(new TwitterCredentialCookie(appToken), new()); + using var apiConnection = new TwitterApiConnection(new TwitterCredentialCookie(appToken), new()); + twitterApi.Initialize(apiConnection, new()); var twitterUser = await twitterApi.AccountVerifyCredentials(); newAccount.UserId = twitterUser.Id; newAccount.Username = twitterUser.ScreenName; diff --git a/OpenTween/SocialProtocol/Twitter/TwitterAccount.cs b/OpenTween/SocialProtocol/Twitter/TwitterAccount.cs index 6de15b790..e0a5f3261 100644 --- a/OpenTween/SocialProtocol/Twitter/TwitterAccount.cs +++ b/OpenTween/SocialProtocol/Twitter/TwitterAccount.cs @@ -30,6 +30,7 @@ namespace OpenTween.SocialProtocol.Twitter public class TwitterAccount : ISocialAccount { private readonly OpenTween.Twitter twLegacy = new(new()); + private TwitterApiConnection apiConnection = new(); public Guid UniqueKey { get; } @@ -54,7 +55,7 @@ public APIAuthType AuthType => this.Legacy.Api.AuthType; public IApiConnection Connection - => this.Legacy.Api.Connection; + => this.apiConnection; public TwitterAccount(Guid uniqueKey) { @@ -68,11 +69,20 @@ public void Initialize(UserAccount accountSettings, SettingCommon settingCommon) Debug.Assert(accountSettings.UniqueKey == this.UniqueKey, "UniqueKey must be same as current value."); var credential = accountSettings.GetTwitterCredential(); - this.AccountState = new TwitterAccountState(accountSettings.UserId, accountSettings.Username); + + this.AccountState = new TwitterAccountState(accountSettings.UserId, accountSettings.Username) + { + HasUnrecoverableError = credential is TwitterCredentialNone, + }; + + var newConnection = new TwitterApiConnection(credential, this.AccountState); + (this.apiConnection, var oldConnection) = (newConnection, this.apiConnection); + oldConnection.Dispose(); + this.Query = this.CreateQueryInstance(credential.AuthType); this.Mutation = this.CreateMutationInstance(credential.AuthType); - this.twLegacy.Initialize(credential, this.AccountState); + this.twLegacy.Initialize(newConnection, this.AccountState); this.twLegacy.RestrictFavCheck = settingCommon.RestrictFavCheck; } diff --git a/OpenTween/SocialProtocol/Twitter/TwitterGraphqlMutation.cs b/OpenTween/SocialProtocol/Twitter/TwitterGraphqlMutation.cs index 7c550547e..1c88834b6 100644 --- a/OpenTween/SocialProtocol/Twitter/TwitterGraphqlMutation.cs +++ b/OpenTween/SocialProtocol/Twitter/TwitterGraphqlMutation.cs @@ -50,7 +50,7 @@ public async Task DeletePost(PostId postId) TweetId = statusId, }; - await request.Send(this.account.Legacy.Api.Connection) + await request.Send(this.account.Connection) .ConfigureAwait(false); } diff --git a/OpenTween/Twitter.cs b/OpenTween/Twitter.cs index e8cfb616d..b8a148239 100644 --- a/OpenTween/Twitter.cs +++ b/OpenTween/Twitter.cs @@ -219,14 +219,10 @@ public async Task VerifyCredentialsAsync() this.Api.AccountState.UpdateFromUser(user); } - public void Initialize(ITwitterCredential credential, TwitterAccountState accountState) + public void Initialize(TwitterApiConnection apiConnection, TwitterAccountState accountState) { - // OAuth認証 - if (credential is TwitterCredentialNone) - this.Api.AccountState.HasUnrecoverableError = true; - this.ResetApiStatus(); - this.Api.Initialize(credential, accountState); + this.Api.Initialize(apiConnection, accountState); } public async Task PostStatus(PostStatusParams param) From 8f1b82497a2be4fdca709c9a5fbba5367c31e175 Mon Sep 17 00:00:00 2001 From: Kimura Youichi Date: Sat, 11 May 2024 04:03:14 +0900 Subject: [PATCH 4/5] =?UTF-8?q?MyLists,UserInfoDialog=E3=81=ABTwitterApi?= =?UTF-8?q?=E3=82=92=E7=9B=B4=E6=8E=A5=E6=B8=A1=E3=81=95=E3=81=9ATwitter?= =?UTF-8?q?=E3=82=AF=E3=83=A9=E3=82=B9=E3=82=92=E4=BD=BF=E7=94=A8=E3=81=99?= =?UTF-8?q?=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- OpenTween/MyLists.cs | 14 +++++++------- OpenTween/Tween.cs | 4 ++-- OpenTween/UserInfoDialog.cs | 30 +++++++++++++++--------------- 3 files changed, 24 insertions(+), 24 deletions(-) diff --git a/OpenTween/MyLists.cs b/OpenTween/MyLists.cs index 80fba4739..c0c88306c 100644 --- a/OpenTween/MyLists.cs +++ b/OpenTween/MyLists.cs @@ -43,7 +43,7 @@ namespace OpenTween { public partial class MyLists : OTBaseForm { - private readonly TwitterApi twitterApi = null!; + private readonly Twitter twitter = null!; private readonly string contextScreenName = null!; /// 自分が所有しているリスト @@ -55,11 +55,11 @@ public partial class MyLists : OTBaseForm public MyLists() => this.InitializeComponent(); - public MyLists(string screenName, TwitterApi twitterApi) + public MyLists(string screenName, Twitter twitter) { this.InitializeComponent(); - this.twitterApi = twitterApi; + this.twitter = twitter; this.contextScreenName = screenName; this.Text = screenName + Properties.Resources.MyLists1; @@ -92,13 +92,13 @@ private async Task RefreshListBox() private async Task FetchMembershipListIds() { var ownedListData = await TwitterLists.GetAllItemsAsync(x => - this.twitterApi.ListsOwnerships(this.twitterApi.CurrentScreenName, cursor: x, count: 1000)) + this.twitter.Api.ListsOwnerships(this.twitter.Username, cursor: x, count: 1000)) .ConfigureAwait(false); this.ownedLists = ownedListData.Select(x => new ListElement(x, null!)).ToArray(); var listsUserAddedTo = await TwitterLists.GetAllItemsAsync(x => - this.twitterApi.ListsMemberships(this.contextScreenName, cursor: x, count: 1000, filterToOwnedLists: true)) + this.twitter.Api.ListsMemberships(this.contextScreenName, cursor: x, count: 1000, filterToOwnedLists: true)) .ConfigureAwait(false); this.addedListIds = listsUserAddedTo.Select(x => x.Id).ToArray(); @@ -108,7 +108,7 @@ private async Task AddToList(ListElement list) { try { - await this.twitterApi.ListsMembersCreate(list.Id, this.contextScreenName) + await this.twitter.Api.ListsMembersCreate(list.Id, this.contextScreenName) .IgnoreResponse(); var index = this.ListsCheckedListBox.Items.IndexOf(list); @@ -124,7 +124,7 @@ private async Task RemoveFromList(ListElement list) { try { - await this.twitterApi.ListsMembersDestroy(list.Id, this.contextScreenName) + await this.twitter.Api.ListsMembersDestroy(list.Id, this.contextScreenName) .IgnoreResponse(); var index = this.ListsCheckedListBox.Items.IndexOf(list); diff --git a/OpenTween/Tween.cs b/OpenTween/Tween.cs index 00a8052ff..f8f3398a8 100644 --- a/OpenTween/Tween.cs +++ b/OpenTween/Tween.cs @@ -8538,7 +8538,7 @@ private void RetweetedByListManageMenuItem_Click(object sender, EventArgs e) public void ListManageUserContext(string screenName) { - using var listSelectForm = new MyLists(screenName, this.tw.Api); + using var listSelectForm = new MyLists(screenName, this.tw); listSelectForm.ShowDialog(this); } @@ -8817,7 +8817,7 @@ private async Task DoShowUserStatus(string id, bool showInputDialog) private async Task DoShowUserStatus(TwitterUser user) { - using var userDialog = new UserInfoDialog(this, this.tw.Api, this.detailsHtmlBuilder); + using var userDialog = new UserInfoDialog(this, this.tw, this.detailsHtmlBuilder); var showUserTask = userDialog.ShowUserAsync(user); userDialog.ShowDialog(this); diff --git a/OpenTween/UserInfoDialog.cs b/OpenTween/UserInfoDialog.cs index e8c06e9cd..ff27d2a65 100644 --- a/OpenTween/UserInfoDialog.cs +++ b/OpenTween/UserInfoDialog.cs @@ -54,13 +54,13 @@ public partial class UserInfoDialog : OTBaseForm private CancellationTokenSource? cancellationTokenSource = null; private readonly TweenMain mainForm; - private readonly TwitterApi twitterApi; + private readonly Twitter twitter; private readonly DetailsHtmlBuilder detailsHtmlBuilder; - public UserInfoDialog(TweenMain mainForm, TwitterApi twitterApi, DetailsHtmlBuilder detailsHtmlBuilder) + public UserInfoDialog(TweenMain mainForm, Twitter twitter, DetailsHtmlBuilder detailsHtmlBuilder) { this.mainForm = mainForm; - this.twitterApi = twitterApi; + this.twitter = twitter; this.detailsHtmlBuilder = detailsHtmlBuilder; this.InitializeComponent(); @@ -138,7 +138,7 @@ public async Task ShowUserAsync(TwitterUser user) this.LinkLabelTweet.Tag = profileUrl; this.ToolTip1.SetToolTip(this.LinkLabelTweet, profileUrl); - if (this.twitterApi.CurrentUserId == user.Id) + if (this.twitter.UserId == user.Id) { this.ButtonEdit.Enabled = true; this.ChangeIconToolStripMenuItem.Enabled = true; @@ -288,13 +288,13 @@ private async Task LoadFriendshipAsync(string screenName, CancellationToken canc this.ButtonFollow.Enabled = false; this.ButtonUnFollow.Enabled = false; - if (this.twitterApi.CurrentScreenName == screenName) + if (this.twitter.Username == screenName) return; TwitterFriendship friendship; try { - friendship = await this.twitterApi.FriendshipsShow(this.twitterApi.CurrentScreenName, screenName); + friendship = await this.twitter.Api.FriendshipsShow(this.twitter.Username, screenName); } catch (WebApiException) { @@ -363,7 +363,7 @@ private async void ButtonFollow_Click(object sender, EventArgs e) { try { - await this.twitterApi.FriendshipsCreate(this.displayUser.ScreenName) + await this.twitter.Api.FriendshipsCreate(this.displayUser.ScreenName) .IgnoreResponse(); } catch (WebApiException ex) @@ -392,7 +392,7 @@ private async void ButtonUnFollow_Click(object sender, EventArgs e) { try { - await this.twitterApi.FriendshipsDestroy(this.displayUser.ScreenName) + await this.twitter.Api.FriendshipsDestroy(this.displayUser.ScreenName) .IgnoreResponse(); } catch (WebApiException ex) @@ -484,7 +484,7 @@ private async void UserPicture_Click(object sender, EventArgs e) private async void ButtonEdit_Click(object sender, EventArgs e) { // 自分以外のプロフィールは変更できない - if (this.twitterApi.CurrentUserId != this.displayUser.Id) + if (this.twitter.UserId != this.displayUser.Id) return; using (ControlTransaction.Disabled(this.ButtonEdit)) @@ -530,7 +530,7 @@ private async void ButtonEdit_Click(object sender, EventArgs e) { try { - using var response = await this.twitterApi.AccountUpdateProfile( + using var response = await this.twitter.Api.AccountUpdateProfile( this.TextBoxName.Text, this.TextBoxWeb.Text, this.TextBoxLocation.Text, @@ -578,7 +578,7 @@ private async Task DoChangeIcon(string filename) { var mediaItem = new FileMediaItem(filename); - await this.twitterApi.AccountUpdateProfileImage(mediaItem) + await this.twitter.Api.AccountUpdateProfileImage(mediaItem) .IgnoreResponse(); } catch (WebApiException ex) @@ -591,7 +591,7 @@ await this.twitterApi.AccountUpdateProfileImage(mediaItem) try { - var user = await this.twitterApi.UsersShow(this.displayUser.ScreenName); + var user = await this.twitter.Api.UsersShow(this.displayUser.ScreenName); if (user != null) await this.ShowUserAsync(user); @@ -639,7 +639,7 @@ private async void ButtonBlock_Click(object sender, EventArgs e) { try { - await this.twitterApi.BlocksCreate(this.displayUser.ScreenName) + await this.twitter.Api.BlocksCreate(this.displayUser.ScreenName) .IgnoreResponse(); } catch (WebApiException ex) @@ -666,7 +666,7 @@ private async void ButtonReportSpam_Click(object sender, EventArgs e) { try { - await this.twitterApi.UsersReportSpam(this.displayUser.ScreenName) + await this.twitter.Api.UsersReportSpam(this.displayUser.ScreenName) .IgnoreResponse(); } catch (WebApiException ex) @@ -693,7 +693,7 @@ private async void ButtonBlockDestroy_Click(object sender, EventArgs e) { try { - await this.twitterApi.BlocksDestroy(this.displayUser.ScreenName) + await this.twitter.Api.BlocksDestroy(this.displayUser.ScreenName) .IgnoreResponse(); } catch (WebApiException ex) From 2028aacf5859df0232404a28e9adda601babc396 Mon Sep 17 00:00:00 2001 From: Kimura Youichi Date: Sat, 11 May 2024 04:05:49 +0900 Subject: [PATCH 5/5] =?UTF-8?q?TwitterApi=E3=82=AF=E3=83=A9=E3=82=B9?= =?UTF-8?q?=E3=81=8B=E3=82=89TwitterAccountState=E3=81=B8=E3=81=AE?= =?UTF-8?q?=E4=BE=9D=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"); }