Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SourceAccountIdによるタブ単位のアカウントの切り替えに対応 #317

Merged
merged 4 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions OpenTween.Tests/Models/TabInformationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ public void AddTab_DuplicateTest()
Assert.False(ret);
}

[Fact]
public void AddTab_FirstTabTest()
{
var tabinfo = new TabInformations();
var tab = new PublicSearchTabModel("hoge");

tabinfo.AddTab(tab);

Assert.Equal("hoge", tabinfo.SelectedTabName);
Assert.Same(tab, tabinfo.SelectedTab);
}

[Fact]
public void RemoveTab_InnerStorageTabTest()
{
Expand Down
61 changes: 61 additions & 0 deletions OpenTween.Tests/SocialProtocol/AccountCollectionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
// Boston, MA 02110-1301, USA.

using System;
using OpenTween.Models;
using OpenTween.SocialProtocol.Twitter;
using Xunit;

Expand Down Expand Up @@ -139,5 +140,65 @@ public void LoadFromSettings_ReconfigureTest()
);
Assert.False(accountItem2.IsDisposed);
}

[Fact]
public void GetAccountForTab_DefaultTest()
{
using var accounts = new AccountCollection();
accounts.LoadFromSettings(new()
{
UserAccounts = new()
{
this.CreateAccountSetting("00000000-0000-4000-8000-000000000000"),
},
SelectedAccountKey = new("00000000-0000-4000-8000-000000000000"),
});

var tabWithoutAccountId = new PublicSearchTabModel("hoge");

// SourceAccountId が null のタブに対しては Primary のアカウントを返す
var actual = accounts.GetAccountForTab(tabWithoutAccountId);
Assert.Equal(new("00000000-0000-4000-8000-000000000000"), actual?.UniqueKey);
}

[Fact]
public void GetAccountForTab_SpecifiedAccountTest()
{
using var accounts = new AccountCollection();
accounts.LoadFromSettings(new()
{
UserAccounts = new()
{
this.CreateAccountSetting("00000000-0000-4000-8000-000000000000"),
this.CreateAccountSetting("00000000-0000-4000-8000-111111111111"),
},
SelectedAccountKey = new("00000000-0000-4000-8000-000000000000"),
});

var tabWithAccountId = new RelatedPostsTabModel("hoge", new("00000000-0000-4000-8000-111111111111"), new());

// SourceAccountId が設定されているタブに対しては対応するアカウントを返す
var actual = accounts.GetAccountForTab(tabWithAccountId);
Assert.Equal(new("00000000-0000-4000-8000-111111111111"), actual?.UniqueKey);
}

[Fact]
public void GetAccountForTab_NotExistsTest()
{
using var accounts = new AccountCollection();
accounts.LoadFromSettings(new()
{
UserAccounts = new()
{
this.CreateAccountSetting("00000000-0000-4000-8000-000000000000"),
},
SelectedAccountKey = new("00000000-0000-4000-8000-000000000000"),
});

var tabWithAccountId = new RelatedPostsTabModel("hoge", new("00000000-0000-4000-8000-999999999999"), new());

// SourceAccountId に存在しない ID が設定されていた場合は null を返す
Assert.Null(accounts.GetAccountForTab(tabWithAccountId));
}
}
}
1 change: 1 addition & 0 deletions OpenTween/ApplicationEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#nullable enable

using System;
using System.Linq;
using System.Windows.Forms;
using OpenTween.Connection;
using OpenTween.Setting;
Expand Down
9 changes: 7 additions & 2 deletions OpenTween/Models/DirectMessagesTabModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
using System.Text;
using System.Threading.Tasks;
using OpenTween.Setting;
using OpenTween.SocialProtocol;
using OpenTween.SocialProtocol.Twitter;

namespace OpenTween.Models
{
Expand All @@ -53,13 +55,16 @@ public DirectMessagesTabModel(string tabName)
{
}

public override async Task RefreshAsync(Twitter tw, bool backward, IProgress<string> progress)
public override async Task RefreshAsync(ISocialAccount account, bool backward, IProgress<string> progress)
{
if (account is not TwitterAccount twAccount)
throw new ArgumentException($"Invalid account type: {account.GetType()}", nameof(account));

progress.Report(string.Format(Properties.Resources.GetTimelineWorker_RunWorkerCompletedText8, backward ? -1 : 1));

var firstLoad = !this.IsFirstLoadCompleted;

await tw.GetDirectMessageEvents(this, backward, firstLoad)
await twAccount.Legacy.GetDirectMessageEvents(this, backward, firstLoad)
.ConfigureAwait(false);

TabInformations.GetInstance().DistributePosts();
Expand Down
9 changes: 7 additions & 2 deletions OpenTween/Models/FavoritesTabModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
using System.Text;
using System.Threading.Tasks;
using OpenTween.Setting;
using OpenTween.SocialProtocol;
using OpenTween.SocialProtocol.Twitter;

namespace OpenTween.Models
{
Expand All @@ -57,13 +59,16 @@ public FavoritesTabModel(string tabName)
{
}

public override async Task RefreshAsync(Twitter tw, bool backward, IProgress<string> progress)
public override async Task RefreshAsync(ISocialAccount account, bool backward, IProgress<string> progress)
{
if (account is not TwitterAccount twAccount)
throw new ArgumentException($"Invalid account type: {account.GetType()}", nameof(account));

progress.Report(Properties.Resources.GetTimelineWorker_RunWorkerCompletedText19);

var firstLoad = !this.IsFirstLoadCompleted;

await tw.GetFavoritesApi(this, backward, firstLoad)
await twAccount.Legacy.GetFavoritesApi(this, backward, firstLoad)
.ConfigureAwait(false);

TabInformations.GetInstance().DistributePosts();
Expand Down
5 changes: 3 additions & 2 deletions OpenTween/Models/FilterTabModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenTween.SocialProtocol;

namespace OpenTween.Models
{
Expand Down Expand Up @@ -168,11 +169,11 @@ public PostFilterRule[] FilterArray
}
}

public override Task RefreshAsync(Twitter tw, bool backward, IProgress<string> progress)
public override Task RefreshAsync(ISocialAccount account, bool backward, IProgress<string> progress)
{
var homeTab = TabInformations.GetInstance().HomeTab;

return homeTab.RefreshAsync(tw, backward, progress);
return homeTab.RefreshAsync(account, backward, progress);
}
}
}
9 changes: 7 additions & 2 deletions OpenTween/Models/HomeTabModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
using System.Threading;
using System.Threading.Tasks;
using OpenTween.Setting;
using OpenTween.SocialProtocol;
using OpenTween.SocialProtocol.Twitter;

namespace OpenTween.Models
{
Expand Down Expand Up @@ -71,13 +73,16 @@ public override void AddPostQueue(PostClass post)
this.UpdateTimelineSpeed(post.CreatedAt);
}

public override async Task RefreshAsync(Twitter tw, bool backward, IProgress<string> progress)
public override async Task RefreshAsync(ISocialAccount account, bool backward, IProgress<string> progress)
{
if (account is not TwitterAccount twAccount)
throw new ArgumentException($"Invalid account type: {account.GetType()}", nameof(account));

progress.Report(string.Format(Properties.Resources.GetTimelineWorker_RunWorkerCompletedText5, backward ? -1 : 1));

var firstLoad = !this.IsFirstLoadCompleted;

await tw.GetHomeTimelineApi(this, backward, firstLoad)
await twAccount.Legacy.GetHomeTimelineApi(this, backward, firstLoad)
.ConfigureAwait(false);

// 新着時未読クリア
Expand Down
9 changes: 7 additions & 2 deletions OpenTween/Models/ListTimelineTabModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
using System.Text;
using System.Threading.Tasks;
using OpenTween.Setting;
using OpenTween.SocialProtocol;
using OpenTween.SocialProtocol.Twitter;

namespace OpenTween.Models
{
Expand All @@ -55,16 +57,19 @@ public ListTimelineTabModel(string tabName, ListElement list)
this.ListInfo = list;
}

public override async Task RefreshAsync(Twitter tw, bool backward, IProgress<string> progress)
public override async Task RefreshAsync(ISocialAccount account, bool backward, IProgress<string> progress)
{
if (account is not TwitterAccount twAccount)
throw new ArgumentException($"Invalid account type: {account.GetType()}", nameof(account));

if (this.ListInfo == null || this.ListInfo.Id == 0)
return;

progress.Report("List refreshing...");

var firstLoad = !this.IsFirstLoadCompleted;

await tw.GetListStatus(this, backward, firstLoad)
await twAccount.Legacy.GetListStatus(this, backward, firstLoad)
.ConfigureAwait(false);

TabInformations.GetInstance().DistributePosts();
Expand Down
3 changes: 2 additions & 1 deletion OpenTween/Models/LocalSearchTabModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenTween.SocialProtocol;

namespace OpenTween.Models
{
Expand All @@ -41,7 +42,7 @@ public LocalSearchTabModel(string tabName)
{
}

public override Task RefreshAsync(Twitter tw, bool backward, IProgress<string> progress)
public override Task RefreshAsync(ISocialAccount account, bool backward, IProgress<string> progress)
=> Task.CompletedTask; // 何もしない
}
}
9 changes: 7 additions & 2 deletions OpenTween/Models/MentionsTabModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
using System.Text;
using System.Threading.Tasks;
using OpenTween.Setting;
using OpenTween.SocialProtocol;
using OpenTween.SocialProtocol.Twitter;

namespace OpenTween.Models
{
Expand All @@ -57,13 +59,16 @@ public MentionsTabModel(string tabName)
{
}

public override async Task RefreshAsync(Twitter tw, bool backward, IProgress<string> progress)
public override async Task RefreshAsync(ISocialAccount account, bool backward, IProgress<string> progress)
{
if (account is not TwitterAccount twAccount)
throw new ArgumentException($"Invalid account type: {account.GetType()}", nameof(account));

progress.Report(string.Format(Properties.Resources.GetTimelineWorker_RunWorkerCompletedText4, backward ? -1 : 1));

var firstLoad = !this.IsFirstLoadCompleted;

await tw.GetMentionsTimelineApi(this, backward, firstLoad)
await twAccount.Legacy.GetMentionsTimelineApi(this, backward, firstLoad)
.ConfigureAwait(false);

TabInformations.GetInstance().DistributePosts();
Expand Down
3 changes: 2 additions & 1 deletion OpenTween/Models/MuteTabModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenTween.SocialProtocol;

namespace OpenTween.Models
{
Expand All @@ -48,7 +49,7 @@ public override void AddPostQueue(PostClass post)
{
}

public override Task RefreshAsync(Twitter tw, bool backward, IProgress<string> progress)
public override Task RefreshAsync(ISocialAccount account, bool backward, IProgress<string> progress)
=> Task.CompletedTask; // 何もしない
}
}
9 changes: 7 additions & 2 deletions OpenTween/Models/PublicSearchTabModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
using System.Text;
using System.Threading.Tasks;
using OpenTween.Setting;
using OpenTween.SocialProtocol;
using OpenTween.SocialProtocol.Twitter;

namespace OpenTween.Models
{
Expand Down Expand Up @@ -77,16 +79,19 @@ public PublicSearchTabModel(string tabName)
{
}

public override async Task RefreshAsync(Twitter tw, bool backward, IProgress<string> progress)
public override async Task RefreshAsync(ISocialAccount account, bool backward, IProgress<string> progress)
{
if (account is not TwitterAccount twAccount)
throw new ArgumentException($"Invalid account type: {account.GetType()}", nameof(account));

if (MyCommon.IsNullOrEmpty(this.SearchWords))
return;

progress.Report("Search refreshing...");

var firstLoad = !this.IsFirstLoadCompleted;

await tw.GetSearch(this, backward, firstLoad)
await twAccount.Legacy.GetSearch(this, backward, firstLoad)
.ConfigureAwait(false);

TabInformations.GetInstance().DistributePosts();
Expand Down
17 changes: 11 additions & 6 deletions OpenTween/Models/RelatedPostsTabModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
using System.Text;
using System.Threading.Tasks;
using OpenTween.Setting;
using OpenTween.SocialProtocol;
using OpenTween.SocialProtocol.Twitter;

namespace OpenTween.Models
{
Expand All @@ -43,26 +45,29 @@ public override MyCommon.TabUsageType TabType

public override bool IsPermanentTabType => false;

public override Guid? SourceAccountId { get; }

public PostClass TargetPost { get; }

public RelatedPostsTabModel(string tabName, PostClass targetPost)
public RelatedPostsTabModel(string tabName, Guid accountId, PostClass targetPost)
: base(tabName)
{
this.SourceAccountId = accountId;
this.TargetPost = targetPost;
}

public Task RefreshAsync(Twitter tw, IProgress<string> progress)
=> this.RefreshAsync(tw, false, progress);

public override async Task RefreshAsync(Twitter tw, bool backward, IProgress<string> progress)
public override async Task RefreshAsync(ISocialAccount account, bool backward, IProgress<string> progress)
{
if (account is not TwitterAccount twAccount)
throw new ArgumentException($"Invalid account type: {account.GetType()}", nameof(account));

try
{
progress.Report("Related refreshing...");

var firstLoad = !this.IsFirstLoadCompleted;

await tw.GetRelatedResult(this, firstLoad)
await twAccount.Legacy.GetRelatedResult(this, firstLoad)
.ConfigureAwait(false);

if (firstLoad)
Expand Down
5 changes: 5 additions & 0 deletions OpenTween/Models/TabInformations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,14 @@ public bool AddTab(TabModel tab)
if (this.Tabs.Contains(tab.TabName))
return false;

var isFirstTab = this.Tabs.Count == 0;

this.tabs.Add(tab);
tab.SetSortMode(this.SortMode, this.SortOrder);

if (isFirstTab)
this.SelectTab(tab.TabName);

return true;
}
}
Expand Down
Loading
Loading