Update Notifier (#464)

# Added
- Added: Ability to check for updates (stable-only) and be notified with a changelog. This is a first pass implementation. 
- Added: Ability to use SignalR within Kavita (websockets)
=====================================

* (some debug code present). Implemented the ability to check and log if the server is up to date or not.

* Fixed a bug for dark mode where anchor buttons wouldn't have the correct font color.

Suppress filter/sort button if there is no filters to show.

Debug: Active indicators for users currently on your server.

Refactored code to send update notification only to admins. Admins now get a popup where they can open the Github release (docker users can just close).

* Fixed an issue where getLibraryNames on first load would call for as many cards there was on the screen. Now we call it much earlier and the data is cached faster.

* Fixed a dark mode bug from previous commit

* Release notes is now rendered markdown

* Implemented the ability to check for an update ad-hoc. Response will come via websocket to all admins.

* Fixed a missing padding

* Cleanup, added some temp code to carousel

* Cleaned up old stat stuff from dev config and added debug only flow for checking for update

* Misc cleanup

* Added readonly to one variable

* Fixed In Progress not showing for all series due to pagination bug

* Fixed the In progress API returning back series that had another users progress on them. Added SplitQuery which speeds up query significantly.

* SplitQuery in GetRecentlyAdded for a speed increase on API.

Fixed the logic on VersionUpdaterService to properly send on non-dev systems.

Disable the check button once it's triggered once since the API does a task, so it can't return anything.

* Cleaned up the admin actions to be more friendly on mobile.

* Cleaned up the message as we wait for SingalR to notify the user

* more textual changes

* Code smells
This commit is contained in:
Joseph Milazzo 2021-08-09 08:52:24 -05:00 committed by GitHub
parent 867b8e5c8a
commit 2809233de0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
51 changed files with 753 additions and 100 deletions

View file

@ -33,14 +33,18 @@
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.1.1" />
<PackageReference Include="Docnet.Core" Version="2.3.1" />
<PackageReference Include="ExCSS" Version="4.1.0" />
<PackageReference Include="Flurl" Version="3.0.2" />
<PackageReference Include="Flurl.Http" Version="3.2.0" />
<PackageReference Include="Hangfire" Version="1.7.20" />
<PackageReference Include="Hangfire.AspNetCore" Version="1.7.20" />
<PackageReference Include="Hangfire.MaximumConcurrentExecutions" Version="1.1.0" />
<PackageReference Include="Hangfire.MemoryStorage.Core" Version="1.4.0" />
<PackageReference Include="HtmlAgilityPack" Version="1.11.32" />
<PackageReference Include="MarkdownDeep.NET.Core" Version="1.5.0.4" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.4" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="5.0.4" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="5.0.4" />
<PackageReference Include="Microsoft.AspNetCore.SignalR" Version="1.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

View file

@ -168,7 +168,7 @@ namespace API.Controllers
[HttpPost("in-progress")]
public async Task<ActionResult<IEnumerable<SeriesDto>>> GetInProgress(FilterDto filterDto, [FromQuery] UserParams userParams, [FromQuery] int libraryId = 0)
{
// NOTE: This has to be done manually like this due to the DisinctBy requirement
// NOTE: This has to be done manually like this due to the DistinctBy requirement
var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername());
var results = await _unitOfWork.SeriesRepository.GetInProgress(user.Id, libraryId, userParams, filterDto);

View file

@ -3,6 +3,7 @@ using System.IO;
using System.Threading.Tasks;
using API.DTOs.Stats;
using API.Extensions;
using API.Interfaces;
using API.Interfaces.Services;
using API.Services.Tasks;
using Kavita.Common;
@ -23,9 +24,10 @@ namespace API.Controllers
private readonly IBackupService _backupService;
private readonly IArchiveService _archiveService;
private readonly ICacheService _cacheService;
private readonly ITaskScheduler _taskScheduler;
public ServerController(IHostApplicationLifetime applicationLifetime, ILogger<ServerController> logger, IConfiguration config,
IBackupService backupService, IArchiveService archiveService, ICacheService cacheService)
IBackupService backupService, IArchiveService archiveService, ICacheService cacheService, ITaskScheduler taskScheduler)
{
_applicationLifetime = applicationLifetime;
_logger = logger;
@ -33,6 +35,7 @@ namespace API.Controllers
_backupService = backupService;
_archiveService = archiveService;
_cacheService = cacheService;
_taskScheduler = taskScheduler;
}
/// <summary>
@ -99,7 +102,11 @@ namespace API.Controllers
}
}
[HttpPost("check-update")]
public ActionResult CheckForUpdates()
{
_taskScheduler.CheckForUpdate();
return Ok();
}
}
}

View file

@ -29,12 +29,11 @@ namespace API.Controllers
return Ok();
}
catch (Exception e)
catch (Exception ex)
{
_logger.LogError(e, "Error updating the usage statistics");
Console.WriteLine(e);
_logger.LogError(ex, "Error updating the usage statistics");
throw;
}
}
}
}
}

View file

@ -334,6 +334,7 @@ namespace API.Data
.Where(s => s.LibraryId == libraryId && formats.Contains(s.Format))
.OrderByDescending(s => s.Created)
.ProjectTo<SeriesDto>(_mapper.ConfigurationProvider)
.AsSplitQuery()
.AsNoTracking();
return await PagedList<SeriesDto>.CreateAsync(query, userParams.PageNumber, userParams.PageSize);

View file

@ -4,6 +4,7 @@ using API.Interfaces;
using API.Interfaces.Services;
using API.Services;
using API.Services.Tasks;
using API.SignalR.Presence;
using Kavita.Common;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
@ -32,9 +33,13 @@ namespace API.Extensions
services.AddScoped<ICleanupService, CleanupService>();
services.AddScoped<IBookService, BookService>();
services.AddScoped<IImageService, ImageService>();
services.AddScoped<IVersionUpdaterService, VersionUpdaterService>();
services.AddScoped<IPresenceTracker, PresenceTracker>();
services.AddSqLite(config, env);
services.AddLogging(config);
services.AddSignalR();
}
private static void AddSqLite(this IServiceCollection services, IConfiguration config,

View file

@ -1,4 +1,5 @@
using System.Text;
using System.Threading.Tasks;
using API.Constants;
using API.Data;
using API.Entities;
@ -24,7 +25,7 @@ namespace API.Extensions
.AddSignInManager<SignInManager<AppUser>>()
.AddRoleValidator<RoleValidator<AppRole>>()
.AddEntityFrameworkStores<DataContext>();
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
@ -35,14 +36,30 @@ namespace API.Extensions
ValidateIssuer = false,
ValidateAudience = false
};
options.Events = new JwtBearerEvents()
{
OnMessageReceived = context =>
{
var accessToken = context.Request.Query["access_token"];
var path = context.HttpContext.Request.Path;
// Only use query string based token on SignalR hubs
if (!string.IsNullOrEmpty(accessToken) && path.StartsWithSegments("/hubs"))
{
context.Token = accessToken;
}
return Task.CompletedTask;
}
};
});
services.AddAuthorization(opt =>
{
opt.AddPolicy("RequireAdminRole", policy => policy.RequireRole(PolicyConstants.AdminRole));
opt.AddPolicy("RequireDownloadRole", policy => policy.RequireRole(PolicyConstants.DownloadRole, PolicyConstants.AdminRole));
});
return services;
}
}
}
}

View file

@ -6,13 +6,15 @@
/// For use on Server startup
/// </summary>
void ScheduleTasks();
void ScheduleStatsTasks();
void ScheduleUpdaterTasks();
void ScanLibrary(int libraryId, bool forceUpdate = false);
void CleanupChapters(int[] chapterIds);
void RefreshMetadata(int libraryId, bool forceUpdate = true);
void CleanupTemp();
void RefreshSeriesMetadata(int libraryId, int seriesId);
void ScanSeries(int libraryId, int seriesId, bool forceUpdate = false);
void ScheduleStatsTasks();
void CancelStatsTasks();
void CheckForUpdate();
}
}

View file

@ -0,0 +1,11 @@
using System;
using System.Threading.Tasks;
namespace API.Interfaces.Services
{
public interface IVersionUpdaterService
{
public Task CheckForUpdate();
}
}

View file

@ -39,16 +39,12 @@ namespace API.Services.Clients
response = responseContent
};
_logger.LogError(e, "The StatsServer did not respond successfully. {Content}", info);
Console.WriteLine(e);
_logger.LogError(e, "KavitaStats did not respond successfully. {Content}", info);
throw;
}
catch (Exception e)
{
_logger.LogError(e, "An error happened during the request to the Stats Server");
Console.WriteLine(e);
_logger.LogError(e, "An error happened during the request to KavitaStats");
throw;
}
}

View file

@ -23,6 +23,7 @@ namespace API.Services.HostedServices
var taskScheduler = scope.ServiceProvider.GetRequiredService<ITaskScheduler>();
taskScheduler.ScheduleTasks();
taskScheduler.ScheduleUpdaterTasks();
try
{
@ -51,4 +52,4 @@ namespace API.Services.HostedServices
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
}
}
}

View file

@ -21,13 +21,14 @@ namespace API.Services
private readonly ICleanupService _cleanupService;
private readonly IStatsService _statsService;
private readonly IVersionUpdaterService _versionUpdaterService;
public static BackgroundJobServer Client => new BackgroundJobServer();
public TaskScheduler(ICacheService cacheService, ILogger<TaskScheduler> logger, IScannerService scannerService,
IUnitOfWork unitOfWork, IMetadataService metadataService, IBackupService backupService,
ICleanupService cleanupService, IStatsService statsService)
ICleanupService cleanupService, IStatsService statsService, IVersionUpdaterService versionUpdaterService)
{
_cacheService = cacheService;
_logger = logger;
@ -37,6 +38,7 @@ namespace API.Services
_backupService = backupService;
_cleanupService = cleanupService;
_statsService = statsService;
_versionUpdaterService = versionUpdaterService;
}
public void ScheduleTasks()
@ -97,6 +99,16 @@ namespace API.Services
#endregion
#region UpdateTasks
public void ScheduleUpdaterTasks()
{
_logger.LogInformation("Scheduling Auto-Update tasks");
RecurringJob.AddOrUpdate("check-updates", () => _versionUpdaterService.CheckForUpdate(), Cron.Daily);
}
#endregion
public void ScanLibrary(int libraryId, bool forceUpdate = false)
{
_logger.LogInformation("Enqueuing library scan for: {LibraryId}", libraryId);
@ -138,5 +150,10 @@ namespace API.Services
{
BackgroundJob.Enqueue(() => _backupService.BackupDatabase());
}
public void CheckForUpdate()
{
BackgroundJob.Enqueue(() => _versionUpdaterService.CheckForUpdate());
}
}
}

View file

@ -0,0 +1,112 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using API.Interfaces.Services;
using API.SignalR;
using API.SignalR.Presence;
using Flurl.Http;
using Kavita.Common.EnvironmentInfo;
using MarkdownDeep;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace API.Services.Tasks
{
internal class GithubReleaseMetadata
{
/// <summary>
/// Name of the Tag
/// <example>v0.4.3</example>
/// </summary>
public string Tag_Name { get; init; }
/// <summary>
/// Name of the Release
/// </summary>
public string Name { get; init; }
/// <summary>
/// Body of the Release
/// </summary>
public string Body { get; init; }
/// <summary>
/// Url of the release on Github
/// </summary>
public string Html_Url { get; init; }
}
public class VersionUpdaterService : IVersionUpdaterService
{
private readonly ILogger<VersionUpdaterService> _logger;
private readonly IHubContext<MessageHub> _messageHub;
private readonly IPresenceTracker _tracker;
private readonly Markdown _markdown = new MarkdownDeep.Markdown();
public VersionUpdaterService(ILogger<VersionUpdaterService> logger, IHubContext<MessageHub> messageHub, IPresenceTracker tracker)
{
_logger = logger;
_messageHub = messageHub;
_tracker = tracker;
}
/// <summary>
/// Scheduled Task that checks if a newer version is available. If it is, will check if User is currently connected and push
/// a message.
/// </summary>
public async Task CheckForUpdate()
{
var update = await GetGithubRelease();
if (update == null || string.IsNullOrEmpty(update.Tag_Name)) return;
var admins = await _tracker.GetOnlineAdmins();
var version = update.Tag_Name.Replace("v", string.Empty);
var updateVersion = new Version(version);
if (BuildInfo.Version < updateVersion)
{
_logger.LogInformation("Server is out of date. Current: {CurrentVersion}. Available: {AvailableUpdate}", BuildInfo.Version, updateVersion);
await SendEvent(update, admins);
}
else if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == Environments.Development)
{
_logger.LogInformation("Server is up to date. Current: {CurrentVersion}", BuildInfo.Version);
await SendEvent(update, admins);
}
}
private async Task SendEvent(GithubReleaseMetadata update, IReadOnlyList<string> admins)
{
var version = update.Tag_Name.Replace("v", string.Empty);
var updateVersion = new Version(version);
var connections = new List<string>();
foreach (var admin in admins)
{
connections.AddRange(await _tracker.GetConnectionsForUser(admin));
}
await _messageHub.Clients.Users(admins).SendAsync("UpdateAvailable", new SignalRMessage
{
Name = "UpdateAvailable",
Body = new
{
CurrentVersion = version,
UpdateVersion = updateVersion.ToString(),
UpdateBody = _markdown.Transform(update.Body.Trim()),
UpdateTitle = update.Name,
UpdateUrl = update.Html_Url,
IsDocker = new OsInfo(Array.Empty<IOsVersionAdapter>()).IsDocker
}
});
}
private static async Task<GithubReleaseMetadata> GetGithubRelease()
{
var update = await "https://api.github.com/repos/Kareadita/Kavita/releases/latest"
.WithHeader("Accept", "application/json")
.WithHeader("User-Agent", "Kavita")
.GetJsonAsync<GithubReleaseMetadata>();
return update;
}
}
}

46
API/SignalR/MessageHub.cs Normal file
View file

@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.SignalR;
namespace API.SignalR
{
[Authorize]
public class MessageHub : Hub
{
private static readonly HashSet<string> _connections = new HashSet<string>();
public static bool IsConnected
{
get
{
lock (_connections)
{
return _connections.Count != 0;
}
}
}
public override async Task OnConnectedAsync()
{
lock (_connections)
{
_connections.Add(Context.ConnectionId);
}
await base.OnConnectedAsync();
}
public override async Task OnDisconnectedAsync(Exception exception)
{
lock (_connections)
{
_connections.Remove(Context.ConnectionId);
}
await base.OnDisconnectedAsync(exception);
}
}
}

View file

@ -0,0 +1,99 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using API.Interfaces;
namespace API.SignalR.Presence
{
public interface IPresenceTracker
{
Task UserConnected(string username, string connectionId);
Task UserDisconnected(string username, string connectionId);
Task<string[]> GetOnlineAdmins();
Task<List<string>> GetConnectionsForUser(string username);
}
/// <summary>
/// This is a singleton service for tracking what users have a SignalR connection and their difference connectionIds
/// </summary>
public class PresenceTracker : IPresenceTracker
{
private readonly IUnitOfWork _unitOfWork;
private static readonly Dictionary<string, List<string>> OnlineUsers = new Dictionary<string, List<string>>();
public PresenceTracker(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
public Task UserConnected(string username, string connectionId)
{
lock (OnlineUsers)
{
if (OnlineUsers.ContainsKey(username))
{
OnlineUsers[username].Add(connectionId);
}
else
{
OnlineUsers.Add(username, new List<string>() { connectionId });
}
}
return Task.CompletedTask;
}
public Task UserDisconnected(string username, string connectionId)
{
lock (OnlineUsers)
{
if (!OnlineUsers.ContainsKey(username)) return Task.CompletedTask;
OnlineUsers[username].Remove(connectionId);
if (OnlineUsers[username].Count == 0)
{
OnlineUsers.Remove(username);
}
}
return Task.CompletedTask;
}
public static Task<string[]> GetOnlineUsers()
{
string[] onlineUsers;
lock (OnlineUsers)
{
onlineUsers = OnlineUsers.OrderBy(k => k.Key).Select(k => k.Key).ToArray();
}
return Task.FromResult(onlineUsers);
}
public async Task<string[]> GetOnlineAdmins()
{
string[] onlineUsers;
lock (OnlineUsers)
{
onlineUsers = OnlineUsers.OrderBy(k => k.Key).Select(k => k.Key).ToArray();
}
var admins = await _unitOfWork.UserRepository.GetAdminUsersAsync();
var result = admins.Select(a => a.UserName).Intersect(onlineUsers).ToArray();
return result;
}
public Task<List<string>> GetConnectionsForUser(string username)
{
List<string> connectionIds;
lock (OnlineUsers)
{
connectionIds = OnlineUsers.GetValueOrDefault(username);
}
return Task.FromResult(connectionIds);
}
}
}

View file

@ -0,0 +1,38 @@
using System;
using System.Threading.Tasks;
using API.Extensions;
using API.SignalR.Presence;
using Microsoft.AspNetCore.SignalR;
namespace API.SignalR
{
public class PresenceHub : Hub
{
private readonly IPresenceTracker _tracker;
public PresenceHub(IPresenceTracker tracker)
{
_tracker = tracker;
}
public override async Task OnConnectedAsync()
{
await _tracker.UserConnected(Context.User.GetUsername(), Context.ConnectionId);
var currentUsers = await PresenceTracker.GetOnlineUsers();
await Clients.All.SendAsync("GetOnlineUsers", currentUsers);
}
public override async Task OnDisconnectedAsync(Exception exception)
{
await _tracker.UserDisconnected(Context.User.GetUsername(), Context.ConnectionId);
var currentUsers = await PresenceTracker.GetOnlineUsers();
await Clients.All.SendAsync("GetOnlineUsers", currentUsers);
await base.OnDisconnectedAsync(exception);
}
}
}

View file

@ -0,0 +1,11 @@
namespace API.SignalR
{
public class SignalRMessage
{
public object Body { get; set; }
public string Name { get; set; }
//[JsonIgnore]
//public ModelAction Action { get; set; } // This will be for when we add new flows
}
}

View file

@ -5,6 +5,7 @@ using API.Extensions;
using API.Middleware;
using API.Services;
using API.Services.HostedServices;
using API.SignalR;
using Hangfire;
using Hangfire.MemoryStorage;
using Kavita.Common.EnvironmentInfo;
@ -104,6 +105,7 @@ namespace API
app.UseCors(policy => policy
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials() // For SignalR token query param
.WithOrigins("http://localhost:4200")
.WithExposedHeaders("Content-Disposition", "Pagination"));
}
@ -138,6 +140,8 @@ namespace API
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapHub<MessageHub>("hubs/messages");
endpoints.MapHub<PresenceHub>("hubs/presence");
endpoints.MapHangfireDashboard();
endpoints.MapFallbackToController("Index", "Fallback");
});

View file

@ -3,11 +3,6 @@
"DefaultConnection": "Data source=kavita.db"
},
"TokenKey": "super secret unguessable key",
"StatsOptions": {
"ServerUrl": "http://localhost:5002",
"ServerSecret": "here's where the api key goes",
"SendDataAt": "23:50"
},
"Logging": {
"LogLevel": {
"Default": "Debug",