OSDN Git Service

Twitter.AccountStateをTwitterAccountStateのプロパティに移動
authorKimura Youichi <kim.upsilon@bucyou.net>
Fri, 10 May 2024 18:11:28 +0000 (03:11 +0900)
committerKimura Youichi <kim.upsilon@bucyou.net>
Fri, 10 May 2024 19:28:53 +0000 (04:28 +0900)
OpenTween/Api/TwitterApi.cs
OpenTween/Connection/TwitterApiConnection.cs
OpenTween/MyCommon.cs
OpenTween/SendErrorReportForm.cs
OpenTween/SocialProtocol/Twitter/TwitterAccountState.cs
OpenTween/Tween.cs
OpenTween/Twitter.cs

index 3ce2ddd..2c6d400 100644 (file)
@@ -53,13 +53,13 @@ namespace OpenTween.Api
         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();
 
index 64833db..60c6ec4 100644 (file)
@@ -36,6 +36,7 @@ using System.Threading.Tasks;
 using System.Web;
 using OpenTween.Api;
 using OpenTween.Api.DataModel;
+using OpenTween.SocialProtocol.Twitter;
 
 namespace OpenTween.Connection
 {
@@ -56,14 +57,17 @@ namespace OpenTween.Connection
 
         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 @@ namespace OpenTween.Connection
                 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 @@ namespace OpenTween.Connection
             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 @@ namespace OpenTween.Connection
             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 @@ namespace OpenTween.Connection
                 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 @@ namespace OpenTween.Connection
                 var responseText = await content.ReadAsStringAsync()
                     .ConfigureAwait(false);
 
-                await TwitterApiConnection.CheckStatusCode(response)
+                await TwitterApiConnection.CheckStatusCode(response, accountState: null)
                     .ConfigureAwait(false);
 
                 var responseParams = HttpUtility.ParseQueryString(responseText);
index 0732c2e..64b0fae 100644 (file)
@@ -191,12 +191,6 @@ namespace OpenTween
         public static bool DebugBuild = false;
 #endif
 
-        public enum ACCOUNT_STATE
-        {
-            Valid,
-            Invalid,
-        }
-
         public enum REPLY_ICONSTATE
         {
             None,
index c08f947..06d63eb 100644 (file)
@@ -204,7 +204,7 @@ namespace OpenTween
             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;
index c9bbb26..95dd391 100644 (file)
@@ -42,6 +42,8 @@ namespace OpenTween.SocialProtocol.Twitter
 
         public ISet<long> NoRetweetUserIds { get; set; } = new HashSet<long>();
 
+        public bool HasUnrecoverableError { get; set; } = true;
+
         public TwitterAccountState()
             : this(0L, "")
         {
index 45a1e65..00a8052 100644 (file)
@@ -1263,13 +1263,13 @@ namespace OpenTween
 
         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 @@ namespace OpenTween
                 }
             }
 
-            Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
+            this.tw.Api.AccountState.HasUnrecoverableError = false;
 
             this.TopMost = this.settings.Common.AlwaysTop;
             this.SaveConfigsAll(false);
index 4dfdb24..e8cfb61 100644 (file)
@@ -195,7 +195,7 @@ namespace OpenTween
 
         public void ClearAuthInfo()
         {
-            Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
+            this.Api.AccountState.HasUnrecoverableError = true;
             this.ResetApiStatus();
         }
 
@@ -223,7 +223,7 @@ namespace OpenTween
         {
             // 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 @@ namespace OpenTween
         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 @@ namespace OpenTween
 
         public async Task<TwitterApiStatus?> 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 @@ namespace OpenTween
 
         internal void CheckAccountState()
         {
-            if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
+            if (this.Api.AccountState.HasUnrecoverableError)
                 throw new WebApiException("Auth error. Check your account");
         }