From 8b20e3fa89865ecb584c3d721454be6ff1d97cef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=E2=96=88=E2=96=88=E2=96=88=E2=96=88=E2=96=88?= Date: Mon, 21 Oct 2024 20:48:59 -0400 Subject: [PATCH] Swapping database references to interface nullinside-development-group/nullinside-api-twitch-bot#39 --- src/Nullinside.Api.TwitchBot/Bots/ABotRule.cs | 4 ++-- src/Nullinside.Api.TwitchBot/Bots/BanKnownBots.cs | 2 +- src/Nullinside.Api.TwitchBot/Bots/IBotRule.cs | 2 +- src/Nullinside.Api.TwitchBot/ChatRules/AChatRule.cs | 5 ++--- .../ChatRules/BestCheapViewers.cs | 2 +- src/Nullinside.Api.TwitchBot/ChatRules/Discord.cs | 2 +- src/Nullinside.Api.TwitchBot/ChatRules/Dogehype.cs | 2 +- src/Nullinside.Api.TwitchBot/ChatRules/HostHub.cs | 2 +- src/Nullinside.Api.TwitchBot/ChatRules/IChatRule.cs | 2 +- .../ChatRules/IfYouWantViewers.cs | 2 +- src/Nullinside.Api.TwitchBot/ChatRules/Naked.cs | 2 +- src/Nullinside.Api.TwitchBot/ChatRules/StreamRise.cs | 2 +- .../ChatRules/StreamViewers.cs | 2 +- .../ChatRules/TwitchChatMessageMonitorConsumer.cs | 4 ++-- .../Controllers/BotController.cs | 4 ++-- .../Controllers/LoginController.cs | 4 ++-- .../Model/NullinsideContextExtensions.cs | 6 +++--- src/Nullinside.Api.TwitchBot/Program.cs | 2 +- src/Nullinside.Api.TwitchBot/Services/MainService.cs | 12 ++++++------ src/nullinside-api | 2 +- 20 files changed, 32 insertions(+), 33 deletions(-) diff --git a/src/Nullinside.Api.TwitchBot/Bots/ABotRule.cs b/src/Nullinside.Api.TwitchBot/Bots/ABotRule.cs index f2fe6f0..f71c29d 100644 --- a/src/Nullinside.Api.TwitchBot/Bots/ABotRule.cs +++ b/src/Nullinside.Api.TwitchBot/Bots/ABotRule.cs @@ -31,7 +31,7 @@ public abstract class ABotRule : IBotRule { /// The cancellation token. /// An asynchronous task. public abstract Task Handle(User user, TwitchUserConfig config, TwitchApiProxy botProxy, - NullinsideContext db, CancellationToken stoppingToken = new()); + INullinsideContext db, CancellationToken stoppingToken = new()); /// /// Handles performing a ban only once, ever, on a user. Any future calls with the same user to be banned in the same @@ -44,7 +44,7 @@ public abstract Task Handle(User user, TwitchUserConfig config, TwitchApiProxy b /// The reason for the ban. /// The cancellation token. /// A collection of confirmed banned users. - protected virtual async Task?> BanOnce(TwitchApiProxy botProxy, NullinsideContext db, + protected virtual async Task?> BanOnce(TwitchApiProxy botProxy, INullinsideContext db, string channelId, IEnumerable<(string Id, string Username)> users, string reason, CancellationToken stoppingToken = new()) { // Get the list of everyone to ban diff --git a/src/Nullinside.Api.TwitchBot/Bots/BanKnownBots.cs b/src/Nullinside.Api.TwitchBot/Bots/BanKnownBots.cs index acd86a2..eaab64b 100644 --- a/src/Nullinside.Api.TwitchBot/Bots/BanKnownBots.cs +++ b/src/Nullinside.Api.TwitchBot/Bots/BanKnownBots.cs @@ -70,7 +70,7 @@ public override bool ShouldRun(TwitchUserConfig config) { /// The database. /// The cancellation token. public override async Task Handle(User user, TwitchUserConfig config, TwitchApiProxy botProxy, - NullinsideContext db, CancellationToken stoppingToken = new()) { + INullinsideContext db, CancellationToken stoppingToken = new()) { if (null == user.TwitchId) { return; } diff --git a/src/Nullinside.Api.TwitchBot/Bots/IBotRule.cs b/src/Nullinside.Api.TwitchBot/Bots/IBotRule.cs index c122776..a6b5716 100644 --- a/src/Nullinside.Api.TwitchBot/Bots/IBotRule.cs +++ b/src/Nullinside.Api.TwitchBot/Bots/IBotRule.cs @@ -26,5 +26,5 @@ public interface IBotRule { /// The cancellation token. /// An asynchronous task. public Task Handle(User user, TwitchUserConfig config, TwitchApiProxy botProxy, - NullinsideContext db, CancellationToken stoppingToken = new()); + INullinsideContext db, CancellationToken stoppingToken = new()); } \ No newline at end of file diff --git a/src/Nullinside.Api.TwitchBot/ChatRules/AChatRule.cs b/src/Nullinside.Api.TwitchBot/ChatRules/AChatRule.cs index 4f9ebd7..f4222b7 100644 --- a/src/Nullinside.Api.TwitchBot/ChatRules/AChatRule.cs +++ b/src/Nullinside.Api.TwitchBot/ChatRules/AChatRule.cs @@ -17,8 +17,7 @@ public abstract class AChatRule : IChatRule { /// public abstract Task Handle(string channelId, TwitchApiProxy botProxy, ChatMessage message, - NullinsideContext db, - CancellationToken stoppingToken = new()); + INullinsideContext db, CancellationToken stoppingToken = new()); /// /// Bans a user and logs the ban attempt to the database. @@ -30,7 +29,7 @@ public abstract Task Handle(string channelId, TwitchApiProxy botProxy, Cha /// The database. /// The cancellation token. public async Task BanAndLog(string channelId, TwitchApiProxy botProxy, - IEnumerable<(string Id, string Username)> users, string reason, NullinsideContext db, + IEnumerable<(string Id, string Username)> users, string reason, INullinsideContext db, CancellationToken stoppingToken = new()) { await botProxy.BanUsers(channelId, Constants.BotId, users, reason, stoppingToken); await db.SaveTwitchBans(channelId, users, reason, stoppingToken); diff --git a/src/Nullinside.Api.TwitchBot/ChatRules/BestCheapViewers.cs b/src/Nullinside.Api.TwitchBot/ChatRules/BestCheapViewers.cs index cc4d661..c3eef79 100644 --- a/src/Nullinside.Api.TwitchBot/ChatRules/BestCheapViewers.cs +++ b/src/Nullinside.Api.TwitchBot/ChatRules/BestCheapViewers.cs @@ -27,7 +27,7 @@ public override bool ShouldRun(TwitchUserConfig config) { /// public override async Task Handle(string channelId, TwitchApiProxy botProxy, ChatMessage message, - NullinsideContext db, CancellationToken stoppingToken = new()) { + INullinsideContext db, CancellationToken stoppingToken = new()) { if (!message.IsFirstMessage) { return true; } diff --git a/src/Nullinside.Api.TwitchBot/ChatRules/Discord.cs b/src/Nullinside.Api.TwitchBot/ChatRules/Discord.cs index 247c6e8..5da9979 100644 --- a/src/Nullinside.Api.TwitchBot/ChatRules/Discord.cs +++ b/src/Nullinside.Api.TwitchBot/ChatRules/Discord.cs @@ -23,7 +23,7 @@ public override bool ShouldRun(TwitchUserConfig config) { /// public override async Task Handle(string channelId, TwitchApiProxy botProxy, ChatMessage message, - NullinsideContext db, CancellationToken stoppingToken = new()) { + INullinsideContext db, CancellationToken stoppingToken = new()) { if (!message.IsFirstMessage) { return true; } diff --git a/src/Nullinside.Api.TwitchBot/ChatRules/Dogehype.cs b/src/Nullinside.Api.TwitchBot/ChatRules/Dogehype.cs index f9a65ce..5ba2bff 100644 --- a/src/Nullinside.Api.TwitchBot/ChatRules/Dogehype.cs +++ b/src/Nullinside.Api.TwitchBot/ChatRules/Dogehype.cs @@ -17,7 +17,7 @@ public override bool ShouldRun(TwitchUserConfig config) { /// public override async Task Handle(string channelId, TwitchApiProxy botProxy, ChatMessage message, - NullinsideContext db, CancellationToken stoppingToken = new()) { + INullinsideContext db, CancellationToken stoppingToken = new()) { // The number of spaces per message may chance, so normalize that and lowercase it for comparison. string normalized = string.Concat(message.Message.Split(" ").Where(s => !string.IsNullOrWhiteSpace(s))) .ToLowerInvariant(); diff --git a/src/Nullinside.Api.TwitchBot/ChatRules/HostHub.cs b/src/Nullinside.Api.TwitchBot/ChatRules/HostHub.cs index 4f1425c..0068949 100644 --- a/src/Nullinside.Api.TwitchBot/ChatRules/HostHub.cs +++ b/src/Nullinside.Api.TwitchBot/ChatRules/HostHub.cs @@ -17,7 +17,7 @@ public override bool ShouldRun(TwitchUserConfig config) { /// public override async Task Handle(string channelId, TwitchApiProxy botProxy, ChatMessage message, - NullinsideContext db, CancellationToken stoppingToken = new()) { + INullinsideContext db, CancellationToken stoppingToken = new()) { // The number of spaces per message may change, so normalize that and lowercase it for comparison. string normalized = string.Concat(message.Message.Split(" ").Where(s => !string.IsNullOrWhiteSpace(s))) .ToLowerInvariant(); diff --git a/src/Nullinside.Api.TwitchBot/ChatRules/IChatRule.cs b/src/Nullinside.Api.TwitchBot/ChatRules/IChatRule.cs index b12d034..d3cfda6 100644 --- a/src/Nullinside.Api.TwitchBot/ChatRules/IChatRule.cs +++ b/src/Nullinside.Api.TwitchBot/ChatRules/IChatRule.cs @@ -27,6 +27,6 @@ public interface IChatRule { /// The database. /// The cancellation token. /// An asynchronous task. - public Task Handle(string channelId, TwitchApiProxy botProxy, ChatMessage message, NullinsideContext db, + public Task Handle(string channelId, TwitchApiProxy botProxy, ChatMessage message, INullinsideContext db, CancellationToken stoppingToken = new()); } \ No newline at end of file diff --git a/src/Nullinside.Api.TwitchBot/ChatRules/IfYouWantViewers.cs b/src/Nullinside.Api.TwitchBot/ChatRules/IfYouWantViewers.cs index 1e01a71..ea965fa 100644 --- a/src/Nullinside.Api.TwitchBot/ChatRules/IfYouWantViewers.cs +++ b/src/Nullinside.Api.TwitchBot/ChatRules/IfYouWantViewers.cs @@ -24,7 +24,7 @@ public override bool ShouldRun(TwitchUserConfig config) { /// public override async Task Handle(string channelId, TwitchApiProxy botProxy, ChatMessage message, - NullinsideContext db, CancellationToken stoppingToken = new()) { + INullinsideContext db, CancellationToken stoppingToken = new()) { if (!message.IsFirstMessage) { return true; } diff --git a/src/Nullinside.Api.TwitchBot/ChatRules/Naked.cs b/src/Nullinside.Api.TwitchBot/ChatRules/Naked.cs index 937da89..18165f4 100644 --- a/src/Nullinside.Api.TwitchBot/ChatRules/Naked.cs +++ b/src/Nullinside.Api.TwitchBot/ChatRules/Naked.cs @@ -20,7 +20,7 @@ public override bool ShouldRun(TwitchUserConfig config) { /// public override async Task Handle(string channelId, TwitchApiProxy botProxy, ChatMessage message, - NullinsideContext db, CancellationToken stoppingToken = new()) { + INullinsideContext db, CancellationToken stoppingToken = new()) { if (message.IsFirstMessage && (message.Message.TrimStart().StartsWith(_spam, StringComparison.InvariantCultureIgnoreCase) || message.Message.TrimStart().StartsWith(_spam2, StringComparison.InvariantCultureIgnoreCase))) { diff --git a/src/Nullinside.Api.TwitchBot/ChatRules/StreamRise.cs b/src/Nullinside.Api.TwitchBot/ChatRules/StreamRise.cs index ef84d1e..661e78f 100644 --- a/src/Nullinside.Api.TwitchBot/ChatRules/StreamRise.cs +++ b/src/Nullinside.Api.TwitchBot/ChatRules/StreamRise.cs @@ -23,7 +23,7 @@ public override bool ShouldRun(TwitchUserConfig config) { /// public override async Task Handle(string channelId, TwitchApiProxy botProxy, ChatMessage message, - NullinsideContext db, CancellationToken stoppingToken = new()) { + INullinsideContext db, CancellationToken stoppingToken = new()) { if (message.IsFirstMessage && _spam.Equals(message.Message, StringComparison.InvariantCultureIgnoreCase)) { await BanAndLog(channelId, botProxy, new[] { (message.UserId, message.Username) }, "[Bot] Spam (StreamRise)", db, stoppingToken); diff --git a/src/Nullinside.Api.TwitchBot/ChatRules/StreamViewers.cs b/src/Nullinside.Api.TwitchBot/ChatRules/StreamViewers.cs index d208175..2035608 100644 --- a/src/Nullinside.Api.TwitchBot/ChatRules/StreamViewers.cs +++ b/src/Nullinside.Api.TwitchBot/ChatRules/StreamViewers.cs @@ -22,7 +22,7 @@ public override bool ShouldRun(TwitchUserConfig config) { /// public override async Task Handle(string channelId, TwitchApiProxy botProxy, ChatMessage message, - NullinsideContext db, CancellationToken stoppingToken = new()) { + INullinsideContext db, CancellationToken stoppingToken = new()) { if (!message.IsFirstMessage) { return true; } diff --git a/src/Nullinside.Api.TwitchBot/ChatRules/TwitchChatMessageMonitorConsumer.cs b/src/Nullinside.Api.TwitchBot/ChatRules/TwitchChatMessageMonitorConsumer.cs index 7ec69d8..daab287 100644 --- a/src/Nullinside.Api.TwitchBot/ChatRules/TwitchChatMessageMonitorConsumer.cs +++ b/src/Nullinside.Api.TwitchBot/ChatRules/TwitchChatMessageMonitorConsumer.cs @@ -36,7 +36,7 @@ public class TwitchChatMessageMonitorConsumer : IDisposable { /// /// The nullinside database. /// - private readonly NullinsideContext _db; + private readonly INullinsideContext _db; /// /// The non-priority queue to scan messages from. @@ -58,7 +58,7 @@ public class TwitchChatMessageMonitorConsumer : IDisposable { /// /// The database. /// The non-priority queue to scan messages from. - public TwitchChatMessageMonitorConsumer(NullinsideContext db, BlockingCollection queue) { + public TwitchChatMessageMonitorConsumer(INullinsideContext db, BlockingCollection queue) { _db = db; _queue = queue; diff --git a/src/Nullinside.Api.TwitchBot/Controllers/BotController.cs b/src/Nullinside.Api.TwitchBot/Controllers/BotController.cs index 52d5815..b35acc1 100644 --- a/src/Nullinside.Api.TwitchBot/Controllers/BotController.cs +++ b/src/Nullinside.Api.TwitchBot/Controllers/BotController.cs @@ -29,7 +29,7 @@ public class BotController : ControllerBase { /// /// The nullinside api database. /// - private readonly NullinsideContext _dbContext; + private readonly INullinsideContext _dbContext; /// /// The logger. @@ -41,7 +41,7 @@ public class BotController : ControllerBase { /// /// The nullinside database. /// The application's configuration. - public BotController(NullinsideContext dbContext, IConfiguration configuration) { + public BotController(INullinsideContext dbContext, IConfiguration configuration) { _dbContext = dbContext; _configuration = configuration; } diff --git a/src/Nullinside.Api.TwitchBot/Controllers/LoginController.cs b/src/Nullinside.Api.TwitchBot/Controllers/LoginController.cs index 07dc383..8157813 100644 --- a/src/Nullinside.Api.TwitchBot/Controllers/LoginController.cs +++ b/src/Nullinside.Api.TwitchBot/Controllers/LoginController.cs @@ -23,7 +23,7 @@ public class LoginController : ControllerBase { /// /// The nullinside api database. /// - private readonly NullinsideContext _dbContext; + private readonly INullinsideContext _dbContext; /// /// The logger. @@ -35,7 +35,7 @@ public class LoginController : ControllerBase { /// /// The nullinside database. /// The application's configuration. - public LoginController(NullinsideContext dbContext, IConfiguration configuration) { + public LoginController(INullinsideContext dbContext, IConfiguration configuration) { _dbContext = dbContext; _configuration = configuration; } diff --git a/src/Nullinside.Api.TwitchBot/Model/NullinsideContextExtensions.cs b/src/Nullinside.Api.TwitchBot/Model/NullinsideContextExtensions.cs index e5b18a5..f976bc0 100644 --- a/src/Nullinside.Api.TwitchBot/Model/NullinsideContextExtensions.cs +++ b/src/Nullinside.Api.TwitchBot/Model/NullinsideContextExtensions.cs @@ -30,7 +30,7 @@ public static TwitchApiProxy GetApi(this User user) { /// The user to configure the twitch api as. /// The stopping token. /// The twitch api. - public static async Task GetApiAndRefreshToken(this NullinsideContext db, User user, + public static async Task GetApiAndRefreshToken(this INullinsideContext db, User user, CancellationToken stoppingToken = new()) { // Get the API TwitchApiProxy api = GetApi(user); @@ -68,7 +68,7 @@ public static TwitchApiProxy GetApi(this User user) { /// The database. /// The stopping token. /// The twitch api. - public static async Task GetBotApiAndRefreshToken(this NullinsideContext db, + public static async Task GetBotApiAndRefreshToken(this INullinsideContext db, CancellationToken stoppingToken = new()) { // Get the bot user's information. User? botUser = await db.Users.AsNoTracking() @@ -88,7 +88,7 @@ public static TwitchApiProxy GetApi(this User user) { /// The users being banned. /// The reason for the bans. /// The stopping token. - public static async Task SaveTwitchBans(this NullinsideContext db, string channelId, + public static async Task SaveTwitchBans(this INullinsideContext db, string channelId, IEnumerable<(string Id, string Username)> bannedUsers, string reason, CancellationToken stoppingToken = new()) { List banUserIds = bannedUsers.Select(b => b.Id).ToList(); HashSet existingUsers = db.TwitchUser diff --git a/src/Nullinside.Api.TwitchBot/Program.cs b/src/Nullinside.Api.TwitchBot/Program.cs index 7a6e9ef..ca8ef0d 100644 --- a/src/Nullinside.Api.TwitchBot/Program.cs +++ b/src/Nullinside.Api.TwitchBot/Program.cs @@ -20,7 +20,7 @@ string? server = Environment.GetEnvironmentVariable("MYSQL_SERVER"); string? username = Environment.GetEnvironmentVariable("MYSQL_USERNAME"); string? password = Environment.GetEnvironmentVariable("MYSQL_PASSWORD"); -builder.Services.AddDbContext(optionsBuilder => +builder.Services.AddDbContext(optionsBuilder => optionsBuilder.UseMySQL( $"server={server};database=nullinside;user={username};password={password};AllowUserVariables=true;", builder => { diff --git a/src/Nullinside.Api.TwitchBot/Services/MainService.cs b/src/Nullinside.Api.TwitchBot/Services/MainService.cs index a41d15e..7d632c4 100644 --- a/src/Nullinside.Api.TwitchBot/Services/MainService.cs +++ b/src/Nullinside.Api.TwitchBot/Services/MainService.cs @@ -39,7 +39,7 @@ public class MainService : BackgroundService { /// /// The database. /// - private readonly NullinsideContext _db; + private readonly INullinsideContext _db; /// /// The logger. @@ -86,7 +86,7 @@ public class MainService : BackgroundService { public MainService(IServiceScopeFactory serviceScopeFactory) { _serviceScopeFactory = serviceScopeFactory; _scope = _serviceScopeFactory.CreateScope(); - _db = _scope.ServiceProvider.GetRequiredService(); + _db = _scope.ServiceProvider.GetRequiredService(); _chatMessageConsumer = new TwitchChatMessageMonitorConsumer(_db, _receivedMessageProcessingQueue); } @@ -131,7 +131,7 @@ private async Task Main(CancellationToken stoppingToken) { try { while (!stoppingToken.IsCancellationRequested) { using (IServiceScope scope = _serviceScopeFactory.CreateAsyncScope()) { - await using (var db = scope.ServiceProvider.GetRequiredService()) { + await using (var db = scope.ServiceProvider.GetRequiredService()) { // Send logs to database DumpLogsToDatabase(db); @@ -198,7 +198,7 @@ private async Task Main(CancellationToken stoppingToken) { /// Dumps a record of the current batch of twitch bans and twitch messages into the database. /// /// The database. - private void DumpLogsToDatabase(NullinsideContext db) { + private void DumpLogsToDatabase(INullinsideContext db) { lock (_receivedBans) { db.TwitchUserBannedOutsideOfBotLogs.AddRange(_receivedBans.Select(b => new TwitchUserBannedOutsideOfBotLogs { Channel = b.UserBan.Channel, @@ -254,7 +254,7 @@ private void OnTwitchMessageReceived(OnMessageReceivedArgs e) { /// The database. /// The stopping token. /// The list of users with the bot enabled. - private async Task?> GetUsersWithBotEnabled(NullinsideContext db, CancellationToken stoppingToken) { + private async Task?> GetUsersWithBotEnabled(INullinsideContext db, CancellationToken stoppingToken) { return await (from user in db.Users orderby user.TwitchLastScanned @@ -282,7 +282,7 @@ private async Task DoScan(User user, User botUser, CancellationToken stoppingTok // Since each scan will happen on a separate thread, we need an individual scope and database reference // per invocation, allowing them to release each loop. using (IServiceScope scope = _serviceScopeFactory.CreateAsyncScope()) { - await using (var db = scope.ServiceProvider.GetRequiredService()) { + await using (var db = scope.ServiceProvider.GetRequiredService()) { // Get the API TwitchApiProxy? botApi = await db.GetApiAndRefreshToken(botUser, stoppingToken); if (null == _botRules || null == user.TwitchConfig || null == botApi) { diff --git a/src/nullinside-api b/src/nullinside-api index 62cf982..7ce25f8 160000 --- a/src/nullinside-api +++ b/src/nullinside-api @@ -1 +1 @@ -Subproject commit 62cf982f538ae8e7ec8ba71c3af20d20f3a8cff4 +Subproject commit 7ce25f8099a8d04769f0bdfe49555dc5b0692bdd