OSDN Git Service

TwitterApiクラスからTwitterAccountStateへの依存を除去
authorKimura Youichi <kim.upsilon@bucyou.net>
Fri, 10 May 2024 19:05:49 +0000 (04:05 +0900)
committerKimura Youichi <kim.upsilon@bucyou.net>
Fri, 10 May 2024 19:28:59 +0000 (04:28 +0900)
OpenTween.Tests/Api/TwitterApiTest.cs
OpenTween/Api/TwitterApi.cs
OpenTween/AppendSettingDialog.cs
OpenTween/SendErrorReportForm.cs
OpenTween/Tween.cs
OpenTween/Twitter.cs

index f960eeb..96fb472 100644 (file)
@@ -32,7 +32,6 @@ using System.Threading.Tasks;
 using Moq;
 using OpenTween.Api.DataModel;
 using OpenTween.Connection;
-using OpenTween.SocialProtocol.Twitter;
 using Xunit;
 
 namespace OpenTween.Api
@@ -58,21 +57,17 @@ namespace OpenTween.Api
             Assert.IsType<TwitterCredentialNone>(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<IApiConnection> CreateApiConnectionMock<T>(Action<T> verifyRequest)
index 940e754..07f6714 100644 (file)
@@ -32,18 +32,11 @@ using System.Threading.Tasks;
 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 @@ namespace OpenTween.Api
         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<TwitterStatus[]> StatusesHomeTimeline(int? count = null, TwitterStatusId? maxId = null, TwitterStatusId? sinceId = null)
index 2d7c72b..aecd38f 100644 (file)
@@ -189,7 +189,7 @@ namespace OpenTween
 
                         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;
index 06d63eb..067d030 100644 (file)
@@ -204,7 +204,7 @@ namespace OpenTween
             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;
index f8f3398..f2004e2 100644 (file)
@@ -1263,13 +1263,13 @@ namespace OpenTween
 
         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 @@ namespace OpenTween
                 }
             }
 
-            this.tw.Api.AccountState.HasUnrecoverableError = false;
+            this.tw.AccountState.HasUnrecoverableError = false;
 
             this.TopMost = this.settings.Common.AlwaysTop;
             this.SaveConfigsAll(false);
index b8a1482..d1038d7 100644 (file)
@@ -160,6 +160,8 @@ namespace OpenTween
 
         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 @@ namespace OpenTween
 
         public void ClearAuthInfo()
         {
-            this.Api.AccountState.HasUnrecoverableError = true;
+            this.AccountState.HasUnrecoverableError = true;
             this.ResetApiStatus();
         }
 
@@ -216,13 +218,15 @@ namespace OpenTween
             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<PostClass?> PostStatus(PostStatusParams param)
@@ -270,7 +274,7 @@ namespace OpenTween
                     .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 @@ namespace OpenTween
         }
 
         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;
 
         /// <summary>
         /// 渡された取得件数がWORKERTYPEに応じた取得可能範囲に収まっているか検証する
@@ -622,7 +626,7 @@ namespace OpenTween
 
         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 @@ namespace OpenTween
         }
 
         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 @@ namespace OpenTween
             }
             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 @@ namespace OpenTween
             var noRetweetUserIds = await this.Api.NoRetweetIds()
                 .ConfigureAwait(false);
 
-            this.Api.AccountState.NoRetweetUserIds = new HashSet<long>(noRetweetUserIds);
+            this.AccountState.NoRetweetUserIds = new HashSet<long>(noRetweetUserIds);
             this.GetNoRetweetSuccess = true;
         }
 
@@ -1203,7 +1207,7 @@ namespace OpenTween
 
         public async Task<TwitterApiStatus?> GetInfoApi()
         {
-            if (this.Api.AccountState.HasUnrecoverableError)
+            if (this.AccountState.HasUnrecoverableError)
                 return null;
 
             if (MyCommon.EndingFlag) return null;
@@ -1261,7 +1265,7 @@ namespace OpenTween
 
         internal void CheckAccountState()
         {
-            if (this.Api.AccountState.HasUnrecoverableError)
+            if (this.AccountState.HasUnrecoverableError)
                 throw new WebApiException("Auth error. Check your account");
         }