Skip to content

Commit

Permalink
TwitterApiクラスからTwitterAccountStateへの依存を除去
Browse files Browse the repository at this point in the history
  • Loading branch information
upsilon committed May 10, 2024
1 parent dbad296 commit 6b45ef0
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 41 deletions.
13 changes: 4 additions & 9 deletions OpenTween.Tests/Api/TwitterApiTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
using Moq;
using OpenTween.Api.DataModel;
using OpenTween.Connection;
using OpenTween.SocialProtocol.Twitter;
using Xunit;

namespace OpenTween.Api
Expand All @@ -58,21 +57,17 @@ public void Initialize_Test()
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)
Expand Down
12 changes: 1 addition & 11 deletions OpenTween/Api/TwitterApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,34 +32,24 @@
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;

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)
Expand Down
2 changes: 1 addition & 1 deletion OpenTween/AppendSettingDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion OpenTween/SendErrorReportForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions OpenTween/Tween.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
36 changes: 20 additions & 16 deletions OpenTween/Twitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down Expand Up @@ -195,7 +197,7 @@ protected void ResetApiStatus()

public void ClearAuthInfo()
{
this.Api.AccountState.HasUnrecoverableError = true;
this.AccountState.HasUnrecoverableError = true;
this.ResetApiStatus();
}

Expand All @@ -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<PostClass?> PostStatus(PostStatusParams param)
Expand Down Expand Up @@ -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?");
Expand Down Expand Up @@ -389,21 +393,21 @@ public async Task<TwitterUser> 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;

/// <summary>
/// 渡された取得件数がWORKERTYPEに応じた取得可能範囲に収まっているか検証する
Expand Down Expand Up @@ -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;
Expand All @@ -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)
{
Expand Down Expand Up @@ -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;
}
Expand All @@ -1100,7 +1104,7 @@ public async Task RefreshNoRetweetIds()
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;
}

Expand Down Expand Up @@ -1203,7 +1207,7 @@ await this.Api.ListsMembersShow(listId, user)

public async Task<TwitterApiStatus?> GetInfoApi()
{
if (this.Api.AccountState.HasUnrecoverableError)
if (this.AccountState.HasUnrecoverableError)
return null;

if (MyCommon.EndingFlag) return null;
Expand Down Expand Up @@ -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");
}

Expand Down

0 comments on commit 6b45ef0

Please sign in to comment.