Skip to content

Commit

Permalink
https://hut.ao/tasks/2
Browse files Browse the repository at this point in the history
  • Loading branch information
Lightczx committed Jan 3, 2025
1 parent f17cec4 commit cd29abe
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 22 deletions.
3 changes: 3 additions & 0 deletions src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.resx
Original file line number Diff line number Diff line change
Expand Up @@ -3296,6 +3296,9 @@
<data name="ViewPageSettingHutaoPassportRedeemCodeHeader" xml:space="preserve">
<value>使用兑换码</value>
</data>
<data name="ViewPageSettingHutaoPassportRefreshAction" xml:space="preserve">
<value>刷新</value>
</data>
<data name="ViewPageSettingHutaoPassportRegisterAction" xml:space="preserve">
<value>注册</value>
</data>
Expand Down
67 changes: 45 additions & 22 deletions src/Snap.Hutao/Snap.Hutao/Service/Hutao/HutaoUserOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public async ValueTask<bool> GetIsHutaoCdnAllowedAsync()
return IsHutaoCdnAllowed;
}

public async ValueTask<string?> GetAuthTokenAsync()
public async ValueTask<string?> GetAuthTokenAsync(CancellationToken token = default)
{
using (await operationLock.LockAsync(nameof(GetAuthTokenAsync)).ConfigureAwait(false))
{
Expand All @@ -74,7 +74,7 @@ public async ValueTask<bool> GetIsHutaoCdnAllowedAsync()
if (authTokenExpiration.ExpireAt < DateTimeOffset.UtcNow)
{
// Re-initialize to refresh the token
await InitializeAsync().ConfigureAwait(false);
await InitializeAsync(token).ConfigureAwait(false);
}

if (!IsLoggedIn)
Expand Down Expand Up @@ -112,6 +112,21 @@ public Task WaitUserInfoInitializationAsync()
return infoEvent.WaitAsync();
}

public async ValueTask RefreshUserInfoAsync(CancellationToken token = default)
{
using (await operationLock.LockAsync(nameof(RefreshUserInfoAsync)).ConfigureAwait(false))
{
// Wait previous Info Event
await infoEvent.WaitAsync().ConfigureAwait(false);
infoEvent.Reset();

if (await GetAuthTokenAsync(token).ConfigureAwait(false) is { } authToken)
{
await RefreshUserInfoCoreAsync(token).ConfigureAwait(false);
}
}
}

public async ValueTask LoginAsync(string username, string password, bool resuming = false, CancellationToken token = default)
{
using (await operationLock.LockAsync(nameof(LoginAsync)).ConfigureAwait(false))
Expand Down Expand Up @@ -245,6 +260,34 @@ private async ValueTask AcceptAuthTokenAsync(string? username, string? password,
IsLoggedIn = true;
loginEvent.Set();

await RefreshUserInfoCoreAsync(token).ConfigureAwait(false);
}
}

private async ValueTask LogoutOrUnregisterAsync()
{
using (await operationLock.LockAsync(nameof(LogoutOrUnregisterAsync)).ConfigureAwait(false))
{
LocalSetting.Set(SettingKeys.PassportUserName, string.Empty);
LocalSetting.Set(SettingKeys.PassportPassword, string.Empty);

await taskContext.SwitchToMainThreadAsync();
authTokenExpiration = default;
UserName = default;
IsLoggedIn = false;
IsDeveloper = false;
IsMaintainer = false;
IsHutaoCloudAllowed = false;
CloudExpireAt = default;
IsHutaoCdnAllowed = false;
CdnExpireAt = default;
}
}

private async ValueTask RefreshUserInfoCoreAsync(CancellationToken token = default)
{
using (await operationLock.LockAsync(nameof(RefreshUserInfoCoreAsync)).ConfigureAwait(false))
{
await taskContext.SwitchToBackgroundAsync();
HutaoPassportClient passportClient = serviceProvider.GetRequiredService<HutaoPassportClient>();
Response<UserInfo> userInfoResponse = await passportClient.GetUserInfoAsync(token).ConfigureAwait(false);
Expand Down Expand Up @@ -273,26 +316,6 @@ private async ValueTask AcceptAuthTokenAsync(string? username, string? password,
}
}

private async ValueTask LogoutOrUnregisterAsync()
{
using (await operationLock.LockAsync(nameof(LogoutOrUnregisterAsync)).ConfigureAwait(false))
{
LocalSetting.Set(SettingKeys.PassportUserName, string.Empty);
LocalSetting.Set(SettingKeys.PassportPassword, string.Empty);

await taskContext.SwitchToMainThreadAsync();
authTokenExpiration = default;
UserName = default;
IsLoggedIn = false;
IsDeveloper = false;
IsMaintainer = false;
IsHutaoCloudAllowed = false;
CloudExpireAt = default;
IsHutaoCdnAllowed = false;
CdnExpireAt = default;
}
}

private readonly struct AuthTokenExpiration
{
public readonly string Token;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
Content="{shuxm:ResourceString Name=ViewPageSettingHutaoPassportRegisterAction}"
Style="{ThemeResource SettingButtonStyle}"
Visibility="{Binding HutaoUserOptions.IsLoggedIn, Converter={StaticResource BoolToVisibilityRevertConverter}}"/>
<Button
Command="{Binding RefreshUserInfoCommand}"
Content="{shuxm:ResourceString Name=ViewPageSettingHutaoPassportRefreshAction}"
Style="{ThemeResource SettingButtonStyle}"
Visibility="{Binding HutaoUserOptions.IsLoggedIn, Converter={StaticResource BoolToVisibilityConverter}}"/>
<Button
Command="{Binding LogoutCommand}"
Content="{shuxm:ResourceString Name=ViewPageSettingHutaoPassportLogoutAction}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,10 @@ private async Task ResetPasswordAsync()

await HutaoUserOptions.ResetPasswordAsync(username, password, verifyCode).ConfigureAwait(false);
}

[Command("RefreshUserInfoCommand")]
private async Task RefreshUserInfoAsync()
{
await HutaoUserOptions.RefreshUserInfoAsync().ConfigureAwait(false);
}
}

0 comments on commit cd29abe

Please sign in to comment.