Skip to content

Commit

Permalink
fix: make sure anidb events are in universal time
Browse files Browse the repository at this point in the history
- Make sure AniDB events in the SignalR streams are in universal time (so they have a time zone attached when stringified).
  • Loading branch information
revam committed Oct 10, 2024
1 parent 9edaf03 commit 4470e52
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 21 deletions.
23 changes: 6 additions & 17 deletions Shoko.Server/API/SignalR/Aggregate/AniDBEmitter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.SignalR;
using Shoko.Server.API.SignalR.Models;
using Shoko.Server.Providers.AniDB;
using Shoko.Server.Providers.AniDB.Interfaces;

Expand All @@ -27,32 +28,20 @@ public void Dispose()

private async void OnUDPStateUpdate(object sender, AniDBStateUpdate e)
{
await SendAsync("AniDBUDPStateUpdate", e);
await SendAsync("AniDBUDPStateUpdate", new AniDBStatusUpdateSignalRModel(e));
}

private async void OnHttpStateUpdate(object sender, AniDBStateUpdate e)
{
await SendAsync("AniDBHttpStateUpdate", e);
await SendAsync("AniDBHttpStateUpdate", new AniDBStatusUpdateSignalRModel(e));
}

public override object GetInitialMessage()
{
return new List<AniDBStateUpdate>
return new List<AniDBStatusUpdateSignalRModel>
{
new()
{
UpdateType = UpdateType.UDPBan,
UpdateTime = UDPHandler.BanTime ?? DateTime.Now,
Value = UDPHandler.IsBanned,
PauseTimeSecs = (int)TimeSpan.FromHours(UDPHandler.BanTimerResetLength).TotalSeconds
},
new()
{
UpdateType = UpdateType.HTTPBan,
UpdateTime = HttpHandler.BanTime ?? DateTime.Now,
Value = HttpHandler.IsBanned,
PauseTimeSecs = (int)TimeSpan.FromHours(HttpHandler.BanTimerResetLength).TotalSeconds
}
new(UpdateType.UDPBan, UDPHandler.IsBanned, UDPHandler.BanTime ?? DateTime.Now, (int)TimeSpan.FromHours(UDPHandler.BanTimerResetLength).TotalSeconds),
new(UpdateType.HTTPBan, HttpHandler.IsBanned, HttpHandler.BanTime ?? DateTime.Now, (int)TimeSpan.FromHours(HttpHandler.BanTimerResetLength).TotalSeconds),
};
}
}
9 changes: 5 additions & 4 deletions Shoko.Server/API/SignalR/Legacy/AniDBEmitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR;
using Shoko.Server.API.SignalR.Models;
using Shoko.Server.Providers.AniDB;
using Shoko.Server.Providers.AniDB.Interfaces;

Expand Down Expand Up @@ -33,21 +34,21 @@ public async Task OnConnectedAsync(IClientProxy caller)
await caller.SendAsync("AniDBState", new Dictionary<string, object>
{
{"UDPBanned", UDPHandler.IsBanned},
{"UDPBanTime", UDPHandler.BanTime},
{"UDPBanTime", UDPHandler.BanTime?.ToUniversalTime()},
{"UDPBanWaitPeriod", UDPHandler.BanTimerResetLength},
{"HttpBanned", HttpHandler.IsBanned},
{"HttpBanTime", HttpHandler.BanTime},
{"HttpBanTime", HttpHandler.BanTime?.ToUniversalTime()},
{"HttpBanWaitPeriod", HttpHandler.BanTimerResetLength},
});
}

private async void OnUDPStateUpdate(object sender, AniDBStateUpdate e)
{
await Hub.Clients.All.SendAsync("AniDBUDPStateUpdate", e);
await Hub.Clients.All.SendAsync("AniDBUDPStateUpdate", new AniDBStatusUpdateSignalRModel(e));
}

private async void OnHttpStateUpdate(object sender, AniDBStateUpdate e)
{
await Hub.Clients.All.SendAsync("AniDBHttpStateUpdate", e);
await Hub.Clients.All.SendAsync("AniDBHttpStateUpdate", new AniDBStatusUpdateSignalRModel(e));
}
}
51 changes: 51 additions & 0 deletions Shoko.Server/API/SignalR/Models/AniDBStatusUpdateSignalRModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using Shoko.Server.Providers.AniDB;

#nullable enable
namespace Shoko.Server.API.SignalR.Models;

public class AniDBStatusUpdateSignalRModel
{
/// <summary>
/// The value of the UpdateType, is the ban active, is it waiting on a response, etc
/// </summary>
public bool Value { get; set; }

/// <summary>
/// Auxiliary Message for some states
/// </summary>
public string Message { get; set; }

/// <summary>
/// Update type, Ban, Invalid Session, Waiting on Response, etc
/// </summary>
public UpdateType UpdateType { get; set; }

/// <summary>
/// When was it updated, usually Now, but may not be
/// </summary>
public DateTime UpdateTime { get; set; }

/// <summary>
/// If we are pausing the queue, then for how long(er)
/// </summary>
public int PauseTimeSecs { get; set; }

public AniDBStatusUpdateSignalRModel(UpdateType updateType, bool value, DateTime updateTime, int pauseTimeSecs, string message = "")
{
Value = value;
Message = message;
UpdateType = updateType;
UpdateTime = updateTime.ToUniversalTime();
PauseTimeSecs = pauseTimeSecs;
}

public AniDBStatusUpdateSignalRModel(AniDBStateUpdate update)
{
Value = update.Value;
Message = update.Message;
UpdateType = update.UpdateType;
UpdateTime = update.UpdateTime.ToUniversalTime();
PauseTimeSecs = update.PauseTimeSecs;
}
}

0 comments on commit 4470e52

Please sign in to comment.