From cd4d278ceb9f0770cbbe19d52f67a746c085d60a Mon Sep 17 00:00:00 2001 From: Marcel Baumgartner Date: Tue, 23 May 2023 21:03:09 +0200 Subject: [PATCH] Added external discord bot api --- .../Api/Moonlight/DiscordBotController.cs | 149 ++++++++++++++++++ .../DiscordBot/Requests/SetPowerSignal.cs | 8 + Moonlight/defaultstorage/configs/config.json | 4 + 3 files changed, 161 insertions(+) create mode 100644 Moonlight/App/Http/Controllers/Api/Moonlight/DiscordBotController.cs create mode 100644 Moonlight/App/Http/Requests/DiscordBot/Requests/SetPowerSignal.cs diff --git a/Moonlight/App/Http/Controllers/Api/Moonlight/DiscordBotController.cs b/Moonlight/App/Http/Controllers/Api/Moonlight/DiscordBotController.cs new file mode 100644 index 00000000..d2c4adb4 --- /dev/null +++ b/Moonlight/App/Http/Controllers/Api/Moonlight/DiscordBotController.cs @@ -0,0 +1,149 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using Moonlight.App.ApiClients.Wings; +using Moonlight.App.ApiClients.Wings.Resources; +using Moonlight.App.Database.Entities; +using Moonlight.App.Http.Requests.DiscordBot.Requests; +using Moonlight.App.Repositories; +using Moonlight.App.Services; + +namespace Moonlight.App.Http.Controllers.Api.Moonlight; + +[ApiController] +[Route("api/moonlight/discordbot")] +public class DiscordBotController : Controller +{ + private readonly Repository UserRepository; + private readonly Repository ServerRepository; + private readonly ServerService ServerService; + private readonly string Token = ""; + private readonly bool Enable; + + public DiscordBotController( + Repository userRepository, + Repository serverRepository, + ServerService serverService, + ConfigService configService) + { + UserRepository = userRepository; + ServerRepository = serverRepository; + ServerService = serverService; + + var config = configService + .GetSection("Moonlight") + .GetSection("DiscordBotApi"); + + Enable = config.GetValue("Enable"); + + if (Enable) + { + Token = config.GetValue("Token"); + } + } + + [HttpGet("{id}/link")] + public async Task GetLink(ulong id) + { + if (!await IsAuth(Request)) + return StatusCode(403); + + if (await GetUserFromDiscordId(id) == null) + { + return BadRequest(); + } + + return Ok(); + } + + [HttpGet("{id}/servers")] + public async Task> GetServers(ulong id) + { + if (!await IsAuth(Request)) + return StatusCode(403); + + var user = await GetUserFromDiscordId(id); + + if (user == null) + return BadRequest(); + + return ServerRepository + .Get() + .Include(x => x.Owner) + .Include(x => x.Image) + .Where(x => x.Owner.Id == user.Id) + .ToArray(); + } + + [HttpPost("{id}/servers/{uuid}")] + public async Task SetPowerState(ulong id, Guid uuid, [FromBody] SetPowerSignal signal) + { + if (!await IsAuth(Request)) + return StatusCode(403); + + var user = await GetUserFromDiscordId(id); + + if (user == null) + return BadRequest(); + + var server = ServerRepository + .Get() + .Include(x => x.Owner) + .FirstOrDefault(x => x.Owner.Id == user.Id && x.Uuid == uuid); + + if (server == null) + return NotFound(); + + if (Enum.TryParse(signal.Signal, true, out PowerSignal powerSignal)) + { + await ServerService.SetPowerState(server, powerSignal); + return Ok(); + } + else + return BadRequest(); + } + + [HttpGet("{id}/servers/{uuid}")] + public async Task> GetServerDetails(ulong id, Guid uuid) + { + if (!await IsAuth(Request)) + return StatusCode(403); + + var user = await GetUserFromDiscordId(id); + + if (user == null) + return BadRequest(); + + var server = ServerRepository + .Get() + .Include(x => x.Owner) + .FirstOrDefault(x => x.Owner.Id == user.Id && x.Uuid == uuid); + + if (server == null) + return NotFound(); + + return await ServerService.GetDetails(server); + } + + private Task GetUserFromDiscordId(ulong discordId) + { + var user = UserRepository + .Get() + .FirstOrDefault(x => x.DiscordId == discordId); + + return Task.FromResult(user); + } + + private Task IsAuth(HttpRequest request) + { + if (!Enable) + return Task.FromResult(false); + + if (string.IsNullOrEmpty(request.Headers.Authorization)) + return Task.FromResult(false); + + if(request.Headers.Authorization == Token) + return Task.FromResult(true); + + return Task.FromResult(false); + } +} \ No newline at end of file diff --git a/Moonlight/App/Http/Requests/DiscordBot/Requests/SetPowerSignal.cs b/Moonlight/App/Http/Requests/DiscordBot/Requests/SetPowerSignal.cs new file mode 100644 index 00000000..1d364053 --- /dev/null +++ b/Moonlight/App/Http/Requests/DiscordBot/Requests/SetPowerSignal.cs @@ -0,0 +1,8 @@ +using Moonlight.App.ApiClients.Wings; + +namespace Moonlight.App.Http.Requests.DiscordBot.Requests; + +public class SetPowerSignal +{ + public string Signal { get; set; } +} \ No newline at end of file diff --git a/Moonlight/defaultstorage/configs/config.json b/Moonlight/defaultstorage/configs/config.json index 19537828..bc1bc396 100644 --- a/Moonlight/defaultstorage/configs/config.json +++ b/Moonlight/defaultstorage/configs/config.json @@ -8,6 +8,10 @@ "Port": "10324", "Username": "user_name" }, + "DiscordBotApi": { + "Enable": false, + "Token": "you api key here" + }, "DiscordBot": { "Enable": false, "Token": "Discord.Token.Here",