Skip to content

Commit

Permalink
Rename and move some database stuff, also prepare for rework of Updater
Browse files Browse the repository at this point in the history
  • Loading branch information
Sella-GH committed Jun 10, 2024
1 parent e6241c7 commit 50a2c93
Show file tree
Hide file tree
Showing 16 changed files with 311 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ public async ValueTask<IReadOnlyDictionary<string, object>> AutoCompleteAsync(Au
if (stationId == 0)
return new Dictionary<string, object>();

List<AzuraCastMountEntity> mountsInDb = await _dbActions.GetAzuraCastMountsAsync(context.Guild.Id, stationId);
List<AzuraCastStationMountEntity> mountsInDb = await _dbActions.GetAzuraCastStationMountsAsync(context.Guild.Id, stationId);

Dictionary<string, object> results = [];
foreach (AzuraCastMountEntity mount in mountsInDb)
foreach (AzuraCastStationMountEntity mount in mountsInDb)
{
results.Add(Crypto.Decrypt(mount.Name), mount.Id);
}
Expand Down
40 changes: 29 additions & 11 deletions AzzyBot-Next/Commands/ConfigCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ public async ValueTask AddAzuraCastAsync
[Description("Set the base Url, an example: https://demo.azuracast.com/")] Uri url,
[Description("Add an administrator api key. It's enough when it has the permission to access system information.")] string apiKey,
[Description("Select a channel to get general notifications about your azuracast installation."), ChannelTypes(DiscordChannelType.Text)] DiscordChannel notificationChannel,
[Description("Select a channel to get notifications when your azuracast installation is down."), ChannelTypes(DiscordChannelType.Text)] DiscordChannel outagesChannel
[Description("Select a channel to get notifications when your azuracast installation is down."), ChannelTypes(DiscordChannelType.Text)] DiscordChannel outagesChannel,
[Description("Enable or disable the automatic check if the AzuraCast instance of your server is down.")] bool serverStatus,
[Description("Enable or disable the automatic check for AzuraCast updates.")] bool updates,
[Description("Enable or disable the addition of the changelog to the posted AzuraCast updates.")] bool updatesChangelog
)
{
ArgumentNullException.ThrowIfNull(context, nameof(context));
Expand Down Expand Up @@ -62,7 +65,7 @@ public async ValueTask AddAzuraCastAsync
return;
}

await _db.AddAzuraCastAsync(guildId, url, apiKey, notificationChannel.Id, outagesChannel.Id);
await _db.AddAzuraCastAsync(guildId, url, apiKey, notificationChannel.Id, outagesChannel.Id, serverStatus, updates, updatesChangelog);

await context.DeleteResponseAsync();
await context.FollowupAsync("Your AzuraCast installation was added successfully and your data has been encrypted.");
Expand All @@ -84,7 +87,7 @@ public async ValueTask AddAzuraCastMountAsync
await context.DeferResponseAsync();

ulong guildId = context.Guild?.Id ?? throw new InvalidOperationException("Guild is null");
await _db.AddAzuraCastMountPointAsync(guildId, station, mountName, mount);
await _db.AddAzuraCastStationMountPointAsync(guildId, station, mountName, mount);

await context.EditResponseAsync("Your mount point was added successfully.");
}
Expand All @@ -99,9 +102,6 @@ public async ValueTask AddAzuraCastStationAsync
[Description("Enable or disable the preference of HLS streams if you add an able mount point.")] bool hls,
[Description("Enable or disable the showing of the playlist in the nowplaying embed.")] bool showPlaylist,
[Description("Enable or disable the automatic check if files have been changed.")] bool fileChanges,
[Description("Enable or disable the automatic check if the AzuraCast instance of your server is down.")] bool serverStatus,
[Description("Enable or disable the automatic check for AzuraCast updates.")] bool updates,
[Description("Enable or disable the addition of the changelog to the posted AzuraCast updates.")] bool updatesChangelog,
[Description("Enter the api key of the new station. This is optional if the admin one has the permission.")] string? apiKey = null
)
{
Expand All @@ -118,7 +118,7 @@ public async ValueTask AddAzuraCastStationAsync
await context.DeferResponseAsync();

ulong guildId = context.Guild?.Id ?? throw new InvalidOperationException("Guild is null");
await _db.AddAzuraCastStationAsync(guildId, station, stationName, requestsChannel.Id, hls, showPlaylist, fileChanges, serverStatus, updates, updatesChangelog, apiKey);
await _db.AddAzuraCastStationAsync(guildId, station, stationName, requestsChannel.Id, hls, showPlaylist, fileChanges, apiKey);

await context.DeleteResponseAsync();
await context.FollowupAsync("Your station was added successfully. Your station name and api key have been encrypted. Your request was also deleted for security reasons.");
Expand Down Expand Up @@ -201,12 +201,10 @@ public async ValueTask UpdateAzuraCastAsync
await context.FollowupAsync("Your AzuraCast settings were saved successfully and have been encrypted.");
}

[Command("modify-azuracast-checks"), Description("Configure the automatic checks inside a station.")]
[Command("modify-azuracast-checks"), Description("Configure the automatic checks for your AzuraCast installation.")]
public async ValueTask UpdateAzuraCastChecksAsync
(
CommandContext context,
[Description("Choose the station you want to modify the checks."), SlashAutoCompleteProvider<AzuraCastStationsAutocomplete>] int station,
[Description("Enable or disable the automatic check if files have been changed.")] bool? fileChanges = null,
[Description("Enable or disable the automatic check if the AzuraCast instance of your server is down.")] bool? serverStatus = null,
[Description("Enable or disable the automatic check for AzuraCast updates.")] bool? updates = null,
[Description("Enable or disable the addition of the changelog to the posted AzuraCast updates.")] bool? updatesChangelog = null
Expand All @@ -219,7 +217,7 @@ public async ValueTask UpdateAzuraCastChecksAsync
await context.DeferResponseAsync();

ulong guildId = context.Guild?.Id ?? throw new InvalidOperationException("Guild is null");
await _db.UpdateAzuraCastChecksAsync(guildId, station, fileChanges, serverStatus, updates, updatesChangelog);
await _db.UpdateAzuraCastChecksAsync(guildId, serverStatus, updates, updatesChangelog);

await context.EditResponseAsync("Your settings were saved successfully.");
}
Expand Down Expand Up @@ -250,6 +248,26 @@ public async ValueTask UpdateAzuraCastStationAsync
await context.FollowupAsync("Your settings were saved successfully. Your station name and api key have been encrypted. Your request was also deleted for security reasons.");
}

[Command("modify-azuracast-station-checks"), Description("Configure the automatic checks inside a station.")]
public async ValueTask UpdateAzuraCastChecksAsync
(
CommandContext context,
[Description("Choose the station you want to modify the checks."), SlashAutoCompleteProvider<AzuraCastStationsAutocomplete>] int station,
[Description("Enable or disable the automatic check if files have been changed.")] bool? fileChanges = null
)
{
ArgumentNullException.ThrowIfNull(context, nameof(context));

_logger.CommandRequested(nameof(UpdateAzuraCastChecksAsync), context.User.GlobalName);

await context.DeferResponseAsync();

ulong guildId = context.Guild?.Id ?? throw new InvalidOperationException("Guild is null");
await _db.UpdateAzuraCastStationChecksAsync(guildId, station, fileChanges);

await context.EditResponseAsync("Your settings were saved successfully.");
}

[Command("modify-core")]
public async ValueTask UpdateCoreAsync(CommandContext context, [Description("Select a channel to get notifications when the bot runs into an issue."), ChannelTypes(DiscordChannelType.Text)] DiscordChannel? errorChannel = null)
{
Expand Down
6 changes: 4 additions & 2 deletions AzzyBot-Next/Database/AzzyDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@ public AzzyDbContext(DbContextOptions<AzzyDbContext> options) : base(options)

public DbSet<GuildsEntity> Guilds { get; set; }
public DbSet<AzuraCastEntity> AzuraCast { get; set; }
public DbSet<AzuraCastStationEntity> AzuraCastStations { get; set; }
public DbSet<AzuraCastChecksEntity> AzuraCastChecks { get; set; }
public DbSet<AzuraCastMountEntity> AzuraCastMounts { get; set; }
public DbSet<AzuraCastStationEntity> AzuraCastStations { get; set; }
public DbSet<AzuraCastStationChecksEntity> AzuraCastStationChecks { get; set; }
public DbSet<AzuraCastStationMountEntity> AzuraCastMounts { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
ArgumentNullException.ThrowIfNull(modelBuilder, nameof(modelBuilder));

modelBuilder.Entity<GuildsEntity>().Navigation(g => g.AzuraCast).AutoInclude();
modelBuilder.Entity<AzuraCastEntity>().Navigation(a => a.Stations).AutoInclude();
modelBuilder.Entity<AzuraCastEntity>().Navigation(a => a.Checks).AutoInclude();
modelBuilder.Entity<AzuraCastStationEntity>().Navigation(s => s.Checks).AutoInclude();
modelBuilder.Entity<AzuraCastStationEntity>().Navigation(s => s.Mounts).AutoInclude();

Expand Down
80 changes: 51 additions & 29 deletions AzzyBot-Next/Database/DbActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private async Task<bool> ExecuteDbActionAsync(Func<AzzyDbContext, Task> action)
}
}

public Task<bool> AddAzuraCastAsync(ulong guildId, Uri baseUrl, string apiKey, ulong notificationId, ulong outagesId)
public Task<bool> AddAzuraCastAsync(ulong guildId, Uri baseUrl, string apiKey, ulong notificationId, ulong outagesId, bool serverStatus, bool updates, bool changelog)
{
ArgumentNullException.ThrowIfNull(baseUrl, nameof(baseUrl));

Expand All @@ -58,14 +58,22 @@ public Task<bool> AddAzuraCastAsync(ulong guildId, Uri baseUrl, string apiKey, u
GuildId = guild.Id
};

azuraCast.Checks = new()
{
ServerStatus = serverStatus,
Updates = updates,
UpdatesShowChangelog = changelog,
AzuraCastId = azuraCast.Id
};

guild.AzuraCastSet = true;

await context.AzuraCast.AddAsync(azuraCast);
context.Guilds.Update(guild);
});
}

public Task<bool> AddAzuraCastStationAsync(ulong guildId, int stationId, string name, ulong requestsId, bool hls, bool showPlaylist, bool fileChanges, bool serverStatus, bool updates, bool updatesChangelog, string? apiKey)
public Task<bool> AddAzuraCastStationAsync(ulong guildId, int stationId, string name, ulong requestsId, bool hls, bool showPlaylist, bool fileChanges, string? apiKey)
{
return ExecuteDbActionAsync(async context =>
{
Expand All @@ -85,23 +93,20 @@ public Task<bool> AddAzuraCastStationAsync(ulong guildId, int stationId, string
station.Checks = new()
{
FileChanges = fileChanges,
ServerStatus = serverStatus,
Updates = updates,
UpdatesShowChangelog = updatesChangelog,
StationId = station.Id
};

await context.AzuraCastStations.AddAsync(station);
});
}

public Task<bool> AddAzuraCastMountPointAsync(ulong guildId, int stationId, string mountName, string mount)
public Task<bool> AddAzuraCastStationMountPointAsync(ulong guildId, int stationId, string mountName, string mount)
{
return ExecuteDbActionAsync(async context =>
{
AzuraCastStationEntity station = await GetAzuraCastStationAsync(guildId, stationId);

AzuraCastMountEntity mountPoint = new()
AzuraCastStationMountEntity mountPoint = new()
{
Name = Crypto.Encrypt(mountName),
Mount = Crypto.Encrypt(mount),
Expand Down Expand Up @@ -147,7 +152,7 @@ public Task<bool> DeleteAzuraCastMountAsync(ulong guildId, int stationId, int mo
{
return ExecuteDbActionAsync(async context =>
{
AzuraCastMountEntity mount = await GetAzuraCastMountAsync(guildId, stationId, mountId);
AzuraCastStationMountEntity mount = await GetAzuraCastStationMountAsync(guildId, stationId, mountId);

context.AzuraCastMounts.Remove(mount);
});
Expand Down Expand Up @@ -183,39 +188,46 @@ public async Task<AzuraCastEntity> GetAzuraCastAsync(ulong guildId)
return guild.AzuraCast ?? throw new InvalidOperationException("AzuraCast settings not found in databse");
}

public async Task<AzuraCastChecksEntity> GetAzuraCastChecksAsync(ulong guildId, int stationId)
public async Task<AzuraCastChecksEntity> GetAzuraCastChecksAsync(ulong guildId)
{
AzuraCastStationEntity station = await GetAzuraCastStationAsync(guildId, stationId);
AzuraCastEntity azuraCast = await GetAzuraCastAsync(guildId);

return station.Checks;
return azuraCast.Checks;
}

public async Task<AzuraCastMountEntity> GetAzuraCastMountAsync(ulong guildId, int stationId, int mountId)
public async Task<AzuraCastStationEntity> GetAzuraCastStationAsync(ulong guildId, int stationId)
{
AzuraCastStationEntity station = await GetAzuraCastStationAsync(guildId, stationId);
AzuraCastEntity azuraCast = await GetAzuraCastAsync(guildId);

return station.Mounts.FirstOrDefault(m => m.Id == mountId) ?? throw new InvalidOperationException("Mount not found in database");
return azuraCast.Stations.FirstOrDefault(s => s.StationId == stationId) ?? throw new InvalidOperationException("Station not found in database");
}

public async Task<List<AzuraCastStationEntity>> GetAzuraCastStationsAsync(ulong guildId)
{
AzuraCastEntity azuraCast = await GetAzuraCastAsync(guildId);

return [.. azuraCast.Stations];
}

public async Task<List<AzuraCastMountEntity>> GetAzuraCastMountsAsync(ulong guildId, int stationId)
public async Task<AzuraCastStationChecksEntity> GetAzuraCastStationChecksAsync(ulong guildId, int stationId)
{
AzuraCastStationEntity station = await GetAzuraCastStationAsync(guildId, stationId);

return [.. station.Mounts];
return station.Checks;
}

public async Task<AzuraCastStationEntity> GetAzuraCastStationAsync(ulong guildId, int stationId)
public async Task<AzuraCastStationMountEntity> GetAzuraCastStationMountAsync(ulong guildId, int stationId, int mountId)
{
AzuraCastEntity azuraCast = await GetAzuraCastAsync(guildId);
AzuraCastStationEntity station = await GetAzuraCastStationAsync(guildId, stationId);

return azuraCast.Stations.FirstOrDefault(s => s.StationId == stationId) ?? throw new InvalidOperationException("Station not found in database");
return station.Mounts.FirstOrDefault(m => m.Id == mountId) ?? throw new InvalidOperationException("Mount not found in database");
}

public async Task<List<AzuraCastStationEntity>> GetAzuraCastStationsAsync(ulong guildId)
public async Task<List<AzuraCastStationMountEntity>> GetAzuraCastStationMountsAsync(ulong guildId, int stationId)
{
AzuraCastEntity azuraCast = await GetAzuraCastAsync(guildId);
AzuraCastStationEntity station = await GetAzuraCastStationAsync(guildId, stationId);

return [.. azuraCast.Stations];
return [.. station.Mounts];
}

public async Task<GuildsEntity> GetGuildAsync(ulong guildId)
Expand Down Expand Up @@ -263,23 +275,20 @@ public Task<bool> UpdateAzuraCastAsync(ulong guildId, Uri? baseUrl, string? apiK
});
}

public Task<bool> UpdateAzuraCastChecksAsync(ulong guildId, int stationId, bool? fileChanges, bool? serverStatus, bool? updates, bool? updatesChangelog)
public Task<bool> UpdateAzuraCastChecksAsync(ulong guildId, bool? serverStatus, bool? updates, bool? changelog)
{
return ExecuteDbActionAsync(async context =>
{
AzuraCastChecksEntity checks = await GetAzuraCastChecksAsync(guildId, stationId);

if (fileChanges.HasValue)
checks.FileChanges = fileChanges.Value;
AzuraCastChecksEntity checks = await GetAzuraCastChecksAsync(guildId);

if (serverStatus.HasValue)
checks.ServerStatus = serverStatus.Value;

if (updates.HasValue)
checks.Updates = updates.Value;

if (updatesChangelog.HasValue)
checks.UpdatesShowChangelog = updatesChangelog.Value;
if (changelog.HasValue)
checks.UpdatesShowChangelog = changelog.Value;

context.AzuraCastChecks.Update(checks);
});
Expand Down Expand Up @@ -313,6 +322,19 @@ public Task<bool> UpdateAzuraCastStationAsync(ulong guildId, int station, int? s
});
}

public Task<bool> UpdateAzuraCastStationChecksAsync(ulong guildId, int stationId, bool? fileChanges)
{
return ExecuteDbActionAsync(async context =>
{
AzuraCastStationChecksEntity checks = await GetAzuraCastStationChecksAsync(guildId, stationId);

if (fileChanges.HasValue)
checks.FileChanges = fileChanges.Value;

context.AzuraCastStationChecks.Update(checks);
});
}

public Task<bool> UpdateGuildAsync(ulong guildId, ulong? errorChannelId, bool? isDebug)
{
return ExecuteDbActionAsync(async context =>
Expand Down
5 changes: 2 additions & 3 deletions AzzyBot-Next/Database/Entities/AzuraCastChecksEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ public sealed class AzuraCastChecksEntity
{
public int Id { get; set; }

public bool FileChanges { get; set; }
public bool ServerStatus { get; set; }
public bool Updates { get; set; }
public bool UpdatesShowChangelog { get; set; }

public int StationId { get; set; }
public AzuraCastStationEntity Station { get; set; } = null!;
public int AzuraCastId { get; set; }
public AzuraCastEntity AzuraCast { get; set; } = null!;
}
1 change: 1 addition & 0 deletions AzzyBot-Next/Database/Entities/AzuraCastEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public sealed class AzuraCastEntity
public string AdminApiKey { get; set; } = string.Empty;
public ulong NotificationChannelId { get; set; }
public ulong OutagesChannelId { get; set; }
public AzuraCastChecksEntity Checks { get; set; } = new();
public ICollection<AzuraCastStationEntity> Stations { get; } = new List<AzuraCastStationEntity>();

public int? GuildId { get; set; }
Expand Down
15 changes: 15 additions & 0 deletions AzzyBot-Next/Database/Entities/AzuraCastStationChecksEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Diagnostics.CodeAnalysis;

namespace AzzyBot.Database.Entities;

[SuppressMessage("Roslynator", "RCS0036:Remove blank line between single-line declarations of same kind", Justification = "Better clarification on keys")]
public sealed class AzuraCastStationChecksEntity
{
public int Id { get; set; }

public bool FileChanges { get; set; }
public bool ServerStatus { get; set; }

public int StationId { get; set; }
public AzuraCastStationEntity Station { get; set; } = null!;
}
4 changes: 2 additions & 2 deletions AzzyBot-Next/Database/Entities/AzuraCastStationEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public sealed class AzuraCastStationEntity
public int StationId { get; set; }
public string Name { get; set; } = string.Empty;
public string ApiKey { get; set; } = string.Empty;
public AzuraCastChecksEntity Checks { get; set; } = new();
public ICollection<AzuraCastMountEntity> Mounts { get; } = new List<AzuraCastMountEntity>();
public AzuraCastStationChecksEntity Checks { get; set; } = new();
public ICollection<AzuraCastStationMountEntity> Mounts { get; } = new List<AzuraCastStationMountEntity>();
public ulong RequestsChannelId { get; set; }
public bool PreferHls { get; set; }
public bool ShowPlaylistInNowPlaying { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace AzzyBot.Database.Entities;

[SuppressMessage("Roslynator", "RCS0036:Remove blank line between single-line declarations of same kind", Justification = "Better clarification on keys")]
public sealed class AzuraCastMountEntity
public sealed class AzuraCastStationMountEntity
{
public int Id { get; set; }

Expand Down
Loading

0 comments on commit 50a2c93

Please sign in to comment.