2021-09-01 17:35:40 +02:00
|
|
|
using System;
|
|
|
|
|
using System.Threading;
|
2021-04-06 18:17:22 +02:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using MediatR;
|
|
|
|
|
using Microsoft.AspNetCore.Components;
|
|
|
|
|
using WotBlitzStatisticsPro.Blazor.Messages;
|
2021-09-01 17:35:40 +02:00
|
|
|
using WotBlitzStatisticsPro.Blazor.Model;
|
2021-04-06 18:17:22 +02:00
|
|
|
|
|
|
|
|
namespace WotBlitzStatisticsPro.Blazor.Services
|
|
|
|
|
{
|
|
|
|
|
public class NavigationMessagesInterceptor: INotificationHandler<OpenPlayerInfoMessage>
|
|
|
|
|
{
|
|
|
|
|
private readonly NavigationManager _navigationManager;
|
2021-09-01 17:35:40 +02:00
|
|
|
private readonly IMediator _mediator;
|
|
|
|
|
private readonly ILocalStorageService _localStorageService;
|
2021-04-06 18:17:22 +02:00
|
|
|
|
2021-09-01 17:35:40 +02:00
|
|
|
public NavigationMessagesInterceptor(
|
|
|
|
|
NavigationManager navigationManager,
|
|
|
|
|
ILocalStorageService localStorageService,
|
|
|
|
|
IMediator mediator)
|
2021-04-06 18:17:22 +02:00
|
|
|
{
|
|
|
|
|
_navigationManager = navigationManager;
|
2021-09-01 17:35:40 +02:00
|
|
|
_localStorageService = localStorageService;
|
|
|
|
|
_mediator = mediator;
|
2021-04-06 18:17:22 +02:00
|
|
|
}
|
2021-09-01 17:35:40 +02:00
|
|
|
public async Task Handle(OpenPlayerInfoMessage notification, CancellationToken cancellationToken)
|
2021-04-06 18:17:22 +02:00
|
|
|
{
|
2021-09-01 17:35:40 +02:00
|
|
|
if(notification.IsLoggedIn)
|
|
|
|
|
{
|
|
|
|
|
await CheckExpirationDateAndProlongIfNeeded();
|
|
|
|
|
}
|
2021-10-20 19:06:34 +03:00
|
|
|
_navigationManager.NavigateTo($"/player/{notification.AccountId}", forceLoad: true);
|
2021-09-01 17:35:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task CheckExpirationDateAndProlongIfNeeded()
|
|
|
|
|
{
|
|
|
|
|
int daysBeforeProlong = 5;
|
|
|
|
|
var loggedInData = await _localStorageService.GetItemAsync<LoginInfo>(Constants.LoginInfoLocalStorageKey);
|
|
|
|
|
|
|
|
|
|
if(loggedInData != null)
|
|
|
|
|
{
|
|
|
|
|
var tokenExpiration = (new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc))
|
|
|
|
|
.AddSeconds(loggedInData.ExpiresAt).ToLocalTime();
|
|
|
|
|
var expirationDaysDifference = (tokenExpiration - DateTime.Now).TotalDays;
|
|
|
|
|
if(expirationDaysDifference <= 0)
|
|
|
|
|
{
|
|
|
|
|
await _mediator.Publish(new RedirectToWgLoginMessage(loggedInData.Realm));
|
|
|
|
|
}
|
|
|
|
|
if(expirationDaysDifference > 0 && expirationDaysDifference <= daysBeforeProlong)
|
|
|
|
|
{
|
|
|
|
|
await _mediator.Publish(new ProlongWargamingAccessToken());
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-06 18:17:22 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|