From db2204258a9017f10874678790eeb9cbb8488ec1 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Sun, 26 May 2024 00:49:32 +0200 Subject: [PATCH] Add setup step in /info --- .../Models/DTO/ServerInfo.cs | 26 +++++++++++++++++++ .../Kyoo.Core/Controllers/MiscRepository.cs | 10 +++++++ .../Views/InfoApi.cs | 7 +++-- 3 files changed, 41 insertions(+), 2 deletions(-) rename back/src/{Kyoo.Authentication => Kyoo.Core}/Views/InfoApi.cs (87%) diff --git a/back/src/Kyoo.Authentication/Models/DTO/ServerInfo.cs b/back/src/Kyoo.Authentication/Models/DTO/ServerInfo.cs index 9a581ed9..6286cb7c 100644 --- a/back/src/Kyoo.Authentication/Models/DTO/ServerInfo.cs +++ b/back/src/Kyoo.Authentication/Models/DTO/ServerInfo.cs @@ -46,6 +46,11 @@ public class ServerInfo /// The list of permissions available for the guest account. /// public List GuestPermissions { get; set; } + + /// + /// Check if kyoo's setup is finished. + /// + public SetupStep SetupStatus { get; set; } } public class OidcInfo @@ -60,3 +65,24 @@ public class OidcInfo /// public string? LogoUrl { get; set; } } + +/// +/// Check if kyoo's setup is finished. +/// +public enum SetupStep +{ + /// + /// No admin account exists, create an account before exposing kyoo to the internet! + /// + MissingAdminAccount, + + /// + /// No video was registered on kyoo, have you configured the rigth library path? + /// + NoVideoFound, + + /// + /// Setup finished! + /// + Done, +} diff --git a/back/src/Kyoo.Core/Controllers/MiscRepository.cs b/back/src/Kyoo.Core/Controllers/MiscRepository.cs index 8b014840..48916ad8 100644 --- a/back/src/Kyoo.Core/Controllers/MiscRepository.cs +++ b/back/src/Kyoo.Core/Controllers/MiscRepository.cs @@ -25,6 +25,7 @@ using System.Threading.Tasks; using Dapper; using Kyoo.Abstractions.Controllers; using Kyoo.Abstractions.Models; +using Kyoo.Authentication.Models; using Kyoo.Postgresql; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; @@ -129,6 +130,15 @@ public class MiscRepository( .OrderBy(x => x.RefreshDate) .ToListAsync(); } + + public async Task GetSetupStep() + { + bool hasUser = await context.Users.AnyAsync(); + if (!hasUser) + return SetupStep.MissingAdminAccount; + bool hasItem = await context.Movies.AnyAsync() || await context.Shows.AnyAsync(); + return hasItem ? SetupStep.Done : SetupStep.NoVideoFound; + } } public class RefreshableItem diff --git a/back/src/Kyoo.Authentication/Views/InfoApi.cs b/back/src/Kyoo.Core/Views/InfoApi.cs similarity index 87% rename from back/src/Kyoo.Authentication/Views/InfoApi.cs rename to back/src/Kyoo.Core/Views/InfoApi.cs index ceb8ab4e..113e32f2 100644 --- a/back/src/Kyoo.Authentication/Views/InfoApi.cs +++ b/back/src/Kyoo.Core/Views/InfoApi.cs @@ -18,8 +18,10 @@ using System.Collections.Generic; using System.Linq; +using System.Threading.Tasks; using Kyoo.Abstractions.Models.Attributes; using Kyoo.Authentication.Models; +using Kyoo.Core.Controllers; using Microsoft.AspNetCore.Mvc; using static Kyoo.Abstractions.Models.Utils.Constants; @@ -31,9 +33,9 @@ namespace Kyoo.Authentication.Views; [ApiController] [Route("info")] [ApiDefinition("Info", Group = UsersGroup)] -public class InfoApi(PermissionOption options) : ControllerBase +public class InfoApi(PermissionOption options, MiscRepository info) : ControllerBase { - public ActionResult GetInfo() + public async Task> GetInfo() { return Ok( new ServerInfo() @@ -48,6 +50,7 @@ public class InfoApi(PermissionOption options) : ControllerBase new() { DisplayName = x.Value.DisplayName, LogoUrl = x.Value.LogoUrl, } )) .ToDictionary(x => x.Key, x => x.Value), + SetupStatus = await info.GetSetupStep() } ); }