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

ISocialAccount.AccountStateを追加 #345

Merged
merged 2 commits into from
May 23, 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
2 changes: 2 additions & 0 deletions OpenTween/SocialProtocol/ISocialAccount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public interface ISocialAccount : IDisposable

public ISocialProtocolClient Client { get; }

public ISocialAccountState AccountState { get; }

public bool IsDisposed { get; }

public void Initialize(UserAccount accountSettings, SettingCommon settingCommon);
Expand Down
50 changes: 50 additions & 0 deletions OpenTween/SocialProtocol/ISocialAccountState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// OpenTween - Client of Twitter
// Copyright (c) 2024 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
// All rights reserved.
//
// This file is part of OpenTween.
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 3 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program. If not, see <http://www.gnu.org/licenses/>, or write to
// the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
// Boston, MA 02110-1301, USA.

#nullable enable

using System.Collections.Generic;
using OpenTween.Api;
using OpenTween.Models;

namespace OpenTween.SocialProtocol
{
public interface ISocialAccountState
{
public PersonId UserId { get; }

public string UserName { get; }

public int? FollowersCount { get; }

public int? FriendsCount { get; }

public int? StatusesCount { get; }

public ISet<PersonId> FollowerIds { get; }

public ISet<PersonId> BlockedUserIds { get; }

public RateLimitCollection RateLimits { get; }

public bool HasUnrecoverableError { get; set; }
}
}
34 changes: 32 additions & 2 deletions OpenTween/SocialProtocol/InvalidAccount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
#nullable enable

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using OpenTween.Api;
using OpenTween.Connection;
using OpenTween.Models;

Expand All @@ -36,15 +38,17 @@ public string AccountType
public Guid UniqueKey { get; }

public PersonId UserId
=> new TwitterUserId("0");
=> this.AccountState.UserId;

public string UserName
=> "(Unknown account)";
=> this.AccountState.UserName;

public IApiConnection Connection { get; } = new InvalidAccountConnection();

public ISocialProtocolClient Client { get; } = new InvalidAccountClient();

public ISocialAccountState AccountState { get; } = new InvalidAccountState();

public bool IsDisposed { get; private set; }

public InvalidAccount(Guid uniqueKey)
Expand Down Expand Up @@ -114,5 +118,31 @@ public Task RefreshConfiguration()
private WebApiException CreateException()
=> new("Invalid account");
}

private class InvalidAccountState : ISocialAccountState
{
public PersonId UserId
=> new TwitterUserId("0");

public string UserName
=> "(Unknown account)";

public int? FollowersCount
=> null;

public int? FriendsCount
=> null;

public int? StatusesCount
=> null;

public ISet<PersonId> FollowerIds { get; } = new HashSet<PersonId>();

public ISet<PersonId> BlockedUserIds { get; } = new HashSet<PersonId>();

public RateLimitCollection RateLimits { get; } = new();

public bool HasUnrecoverableError { get; set; } = true;
}
}
}
3 changes: 3 additions & 0 deletions OpenTween/SocialProtocol/Twitter/TwitterAccount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public string AccountType

public TwitterAccountState AccountState { get; private set; } = new();

ISocialAccountState ISocialAccount.AccountState
=> this.AccountState;

public OpenTween.Twitter Legacy
=> this.twLegacy;

Expand Down
9 changes: 8 additions & 1 deletion OpenTween/SocialProtocol/Twitter/TwitterAccountState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,19 @@
#nullable enable

using System.Collections.Generic;
using OpenTween.Api;
using OpenTween.Api.DataModel;
using OpenTween.Models;

namespace OpenTween.SocialProtocol.Twitter
{
public class TwitterAccountState
public class TwitterAccountState : ISocialAccountState
{
public TwitterUserId UserId { get; private set; }

PersonId ISocialAccountState.UserId
=> this.UserId;

public string UserName { get; private set; }

public int? FollowersCount { get; private set; }
Expand All @@ -53,6 +57,9 @@ public class TwitterAccountState

public TwitterRateLimitCollection RateLimits { get; } = new();

RateLimitCollection ISocialAccountState.RateLimits
=> this.RateLimits;

public bool HasUnrecoverableError { get; set; } = true;

public TwitterAccountState()
Expand Down
53 changes: 21 additions & 32 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.AccountState.HasUnrecoverableError)
if (this.CurrentTabAccount.AccountState.HasUnrecoverableError)
{
this.errorCount += 1;
if (this.errorCount > 5)
{
this.errorCount = 0;
this.tw.AccountState.HasUnrecoverableError = false;
this.CurrentTabAccount.AccountState.HasUnrecoverableError = false;
return true;
}
return false;
Expand Down Expand Up @@ -1805,21 +1805,18 @@ from account in this.accounts.Items
await Task.WhenAll(loadTasks);

var primaryAccount = this.accounts.Primary;
this.statuses.RefreshOwl(primaryAccount.UniqueKey, primaryAccount.AccountState.FollowerIds, isPrimary: true);

if (primaryAccount is TwitterAccount twAccount)
{
this.statuses.RefreshOwl(twAccount.UniqueKey, twAccount.AccountState.FollowerIds, isPrimary: true);

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

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

this.listCache?.PurgeCache();
this.CurrentListView.Refresh();
Expand Down Expand Up @@ -2608,7 +2605,7 @@ private async void SettingStripMenuItem_Click(object sender, EventArgs e)
}
}

this.tw.AccountState.HasUnrecoverableError = false;
this.PrimaryAccount.AccountState.HasUnrecoverableError = false;

this.TopMost = this.settings.Common.AlwaysTop;
this.SaveConfigsAll(false);
Expand Down Expand Up @@ -3059,7 +3056,7 @@ private void ListTab_SelectedIndexChanged(object sender, EventArgs e)
{
this.SetMainWindowTitle();
this.SetStatusLabelUrl();
this.SetApiStatusLabel(null);
this.SetApiStatusLabel(this.CurrentTabAccount.AccountState.RateLimits);
if (this.ListTab.Focused || ((Control)this.CurrentTabPage.Tag).Focused)
this.Tag = this.ListTab.Tag;
this.TabMenuControl(this.CurrentTabName);
Expand Down Expand Up @@ -6746,13 +6743,14 @@ private void SetMainWindowTitle()
ttl.AppendFormat(Properties.Resources.SetMainWindowTitleText4, ur, al);
break;
case MyCommon.DispTitleEnum.OwnStatus:
if (followers == 0 && this.tw.FollowersCount != null) followers = this.tw.FollowersCount.Value;
var accountState = this.CurrentTabAccount.AccountState;
if (followers == 0 && accountState.FollowersCount != null) followers = accountState.FollowersCount.Value;
ttl.AppendFormat(
Properties.Resources.OwnStatusTitle,
this.tw.StatusesCount?.ToString() ?? "-",
this.tw.FriendsCount?.ToString() ?? "-",
this.tw.FollowersCount?.ToString() ?? "-",
this.tw.FollowersCount != null ? this.tw.FollowersCount.Value - followers : "-"
accountState.StatusesCount?.ToString() ?? "-",
accountState.FriendsCount?.ToString() ?? "-",
accountState.FollowersCount?.ToString() ?? "-",
accountState.FollowersCount != null ? accountState.FollowersCount.Value - followers : "-"
);
break;
}
Expand Down Expand Up @@ -6821,14 +6819,12 @@ private string GetStatusLabelText()
private void SubscribePrimaryAccountRatelimit()
{
this.unsubscribeRateLimitUpdate?.Dispose();
if (this.accounts.Primary is TwitterAccount twAccount)
{
var rateLimits = twAccount.AccountState.RateLimits;
this.unsubscribeRateLimitUpdate = rateLimits.SubscribeAccessLimitUpdated(this.TwitterApiStatus_AccessLimitUpdated);

// アカウントの切替を反映するため初回だけ空の更新通知を送る
this.TwitterApiStatus_AccessLimitUpdated(rateLimits, new(null));
}
var rateLimits = this.CurrentTabAccount.AccountState.RateLimits;
this.unsubscribeRateLimitUpdate = rateLimits.SubscribeAccessLimitUpdated(this.TwitterApiStatus_AccessLimitUpdated);

// アカウントの切替を反映するため初回だけ空の更新通知を送る
this.TwitterApiStatus_AccessLimitUpdated(rateLimits, new(null));
}

private async void TwitterApiStatus_AccessLimitUpdated(RateLimitCollection sender, RateLimitCollection.AccessLimitUpdatedEventArgs e)
Expand All @@ -6854,15 +6850,8 @@ private async void TwitterApiStatus_AccessLimitUpdated(RateLimitCollection sende
}
}

private void SetApiStatusLabel(RateLimitCollection? rateLimits, string? endpointName = null)
private void SetApiStatusLabel(RateLimitCollection rateLimits, string? endpointName = null)
{
if (rateLimits == null)
{
this.toolStripApiGauge.ApiLimit = null;
this.toolStripApiGauge.ApiEndpoint = endpointName;
return;
}

var tabType = this.CurrentTab.TabType;

if (endpointName == null)
Expand Down Expand Up @@ -8077,7 +8066,7 @@ private async void ApiUsageInfoMenuItem_Click(object sender, EventArgs e)
}

using var apiDlg = new ApiInfoDialog();
apiDlg.RateLimits = this.CurrentTabAccount is TwitterAccount twAccount ? twAccount.AccountState.RateLimits : null;
apiDlg.RateLimits = this.CurrentTabAccount.AccountState.RateLimits;
apiDlg.ShowDialog(this);
}

Expand Down
7 changes: 2 additions & 5 deletions OpenTween/TweetDetailsView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -372,11 +372,8 @@ private async Task<string> CreateQuoteTweetHtml(PostId statusId, bool isReply)
return FormatQuoteTweetHtml(statusId, WebUtility.HtmlEncode($"Err:{ex.Message}(GetStatus)"), isReply);
}

if (this.Owner.CurrentTabAccount is TwitterAccount twAccount)
{
if (twAccount.AccountState.BlockedUserIds.Contains(post.UserId))
return FormatQuoteTweetHtml(statusId, "This Tweet is unavailable.", isReply);
}
if (this.Owner.CurrentTabAccount.AccountState.BlockedUserIds.Contains(post.UserId))
return FormatQuoteTweetHtml(statusId, "This Tweet is unavailable.", isReply);

if (!TabInformations.GetInstance().AddQuoteTweet(post))
return FormatQuoteTweetHtml(statusId, "This Tweet is unavailable.", isReply);
Expand Down
Loading