Skip to content

Commit

Permalink
RefreshOwlをアカウント別に実行可能にする
Browse files Browse the repository at this point in the history
  • Loading branch information
upsilon committed May 21, 2024
1 parent 927e406 commit a4b10a0
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 26 deletions.
58 changes: 55 additions & 3 deletions OpenTween.Tests/Models/TabInformationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1199,6 +1199,7 @@ public void ClearTabIds_NotAffectToOtherTabs_Test()
[Fact]
public void RefreshOwl_HomeTabTest()
{
var accountId = Guid.NewGuid();
var post = new PostClass
{
StatusId = new TwitterStatusId("100"),
Expand All @@ -1211,14 +1212,15 @@ public void RefreshOwl_HomeTabTest()
this.tabinfo.SubmitUpdate();

var followerIds = new HashSet<PersonId> { new TwitterUserId("123") };
this.tabinfo.RefreshOwl(followerIds);
this.tabinfo.RefreshOwl(accountId, followerIds, isPrimary: true);

Assert.False(post.IsOwl);
}

[Fact]
public void RefreshOwl_InnerStoregeTabTest()
{
var accountId = Guid.NewGuid();
var tab = new PublicSearchTabModel("search");
this.tabinfo.AddTab(tab);

Expand All @@ -1234,14 +1236,15 @@ public void RefreshOwl_InnerStoregeTabTest()
this.tabinfo.SubmitUpdate();

var followerIds = new HashSet<PersonId> { new TwitterUserId("123") };
this.tabinfo.RefreshOwl(followerIds);
this.tabinfo.RefreshOwl(accountId, followerIds, isPrimary: true);

Assert.False(post.IsOwl);
}

[Fact]
public void RefreshOwl_UnfollowedTest()
{
var accountId = Guid.NewGuid();
var post = new PostClass
{
StatusId = new TwitterStatusId("100"),
Expand All @@ -1254,7 +1257,56 @@ public void RefreshOwl_UnfollowedTest()
this.tabinfo.SubmitUpdate();

var followerIds = new HashSet<PersonId> { new TwitterUserId("456") };
this.tabinfo.RefreshOwl(followerIds);
this.tabinfo.RefreshOwl(accountId, followerIds, isPrimary: true);

Assert.True(post.IsOwl);
}

[Fact]
public void RefreshOwl_SecondaryAccountTabTest()
{
var accountId = Guid.NewGuid();
var tab = new HomeSpecifiedAccountTabModel("secondary", accountId);
this.tabinfo.AddTab(tab);

var post = new PostClass
{
StatusId = new TwitterStatusId("100"),
ScreenName = "aaa",
UserId = new TwitterUserId("234"),
IsOwl = true,
};
tab.AddPostQueue(post);
this.tabinfo.DistributePosts();
this.tabinfo.SubmitUpdate();

var followerIds = new HashSet<PersonId> { new TwitterUserId("123") };
this.tabinfo.RefreshOwl(accountId, followerIds, isPrimary: false);

Assert.True(post.IsOwl);
}

[Fact]
public void RefreshOwl_SecondaryAccountTab_AccountNotMatchedTest()
{
var accountId = Guid.NewGuid();
var tab = new HomeSpecifiedAccountTabModel("secondary", accountId);
this.tabinfo.AddTab(tab);

var post = new PostClass
{
StatusId = new TwitterStatusId("100"),
ScreenName = "aaa",
UserId = new TwitterUserId("234"),
IsOwl = true,
};
tab.AddPostQueue(post);
this.tabinfo.DistributePosts();
this.tabinfo.SubmitUpdate();

var otherAccountId = Guid.NewGuid(); // 他アカウントの followerIds なので IsOwl は更新されない
var followerIds = new HashSet<PersonId> { new TwitterUserId("123") };
this.tabinfo.RefreshOwl(otherAccountId, followerIds, isPrimary: false);

Assert.True(post.IsOwl);
}
Expand Down
37 changes: 16 additions & 21 deletions OpenTween/Models/TabInformations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -866,34 +866,29 @@ public void ClearTabIds(string tabName)
}
}

public void RefreshOwl(ISet<PersonId> follower)
public void RefreshOwl(Guid accountId, ISet<PersonId> follower, bool isPrimary)
{
lock (this.lockObj)
{
var allPosts = this.GetTabsByType<InternalStorageTabModel>()
.SelectMany(x => x.Posts.Values)
.Concat(this.Posts.Values);
static bool DetermineOwl(PostClass post, ISet<PersonId> followers)
=> !post.IsMe && followers.Count > 0 && !followers.Contains(post.UserId);

if (follower.Count > 0)
static bool SourceAccountIdMatched(TabModel tab, Guid accountId, bool isPrimary)
=> tab.SourceAccountId == accountId || (isPrimary && tab.SourceAccountId == null);

if (isPrimary)
{
foreach (var post in allPosts)
{
if (post.IsMe)
{
post.IsOwl = false;
}
else
{
post.IsOwl = !follower.Contains(post.UserId);
}
}
foreach (var post in this.Posts.Values)
post.IsOwl = DetermineOwl(post, follower);
}
else

foreach (var tab in this.GetTabsByType<InternalStorageTabModel>())
{
foreach (var post in allPosts)
{
post.IsOwl = false;
}
if (!SourceAccountIdMatched(tab, accountId, isPrimary))
continue;

foreach (var post in tab.Posts.Values)
post.IsOwl = DetermineOwl(post, follower);
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion OpenTween/Tween.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1804,14 +1804,20 @@ from account in this.accounts.Items
var primaryAccount = this.accounts.Primary;
if (primaryAccount is TwitterAccount twAccount)
{
this.statuses.RefreshOwl(twAccount.AccountState.FollowerIds);
this.statuses.RefreshOwl(twAccount.UniqueKey, twAccount.AccountState.FollowerIds, isPrimary: true);

foreach (var (_, service) in this.ImageSelector.Model.MediaServices)
{
service.UpdateTwitterConfiguration(this.tw.Configuration);
}
}

foreach (var account in this.accounts.SecondaryAccounts)
{
if (account is TwitterAccount twAccountSecondary)
this.statuses.RefreshOwl(twAccountSecondary.UniqueKey, twAccountSecondary.AccountState.FollowerIds, isPrimary: false);
}

this.listCache?.PurgeCache();
this.CurrentListView.Refresh();

Expand Down
1 change: 0 additions & 1 deletion OpenTween/Twitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,6 @@ public async Task RefreshFollowerIds()
while (cursor != 0);

this.AccountState.FollowerIds = newFollowerIds.ToHashSet();
TabInformations.GetInstance().RefreshOwl(this.AccountState.FollowerIds);

this.GetFollowersSuccess = true;
}
Expand Down

0 comments on commit a4b10a0

Please sign in to comment.