Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Nullinside.Api.TwitchBot/Bots/ABotRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public abstract class ABotRule : IBotRule {
/// <param name="stoppingToken">The cancellation token.</param>
/// <returns>An asynchronous task.</returns>
public abstract Task Handle(User user, TwitchUserConfig config, TwitchApiProxy botProxy,
NullinsideContext db, CancellationToken stoppingToken = new());
INullinsideContext db, CancellationToken stoppingToken = new());

/// <summary>
/// Handles performing a ban only once, ever, on a user. Any future calls with the same user to be banned in the same
Expand All @@ -44,7 +44,7 @@ public abstract Task Handle(User user, TwitchUserConfig config, TwitchApiProxy b
/// <param name="reason">The reason for the ban.</param>
/// <param name="stoppingToken">The cancellation token.</param>
/// <returns>A collection of confirmed banned users.</returns>
protected virtual async Task<IEnumerable<BannedUser>?> BanOnce(TwitchApiProxy botProxy, NullinsideContext db,
protected virtual async Task<IEnumerable<BannedUser>?> 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
Expand Down
2 changes: 1 addition & 1 deletion src/Nullinside.Api.TwitchBot/Bots/BanKnownBots.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public override bool ShouldRun(TwitchUserConfig config) {
/// <param name="db">The database.</param>
/// <param name="stoppingToken">The cancellation token.</param>
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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Nullinside.Api.TwitchBot/Bots/IBotRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ public interface IBotRule {
/// <param name="stoppingToken">The cancellation token.</param>
/// <returns>An asynchronous task.</returns>
public Task Handle(User user, TwitchUserConfig config, TwitchApiProxy botProxy,
NullinsideContext db, CancellationToken stoppingToken = new());
INullinsideContext db, CancellationToken stoppingToken = new());
}
5 changes: 2 additions & 3 deletions src/Nullinside.Api.TwitchBot/ChatRules/AChatRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ public abstract class AChatRule : IChatRule {

/// <inheritdoc />
public abstract Task<bool> Handle(string channelId, TwitchApiProxy botProxy, ChatMessage message,
NullinsideContext db,
CancellationToken stoppingToken = new());
INullinsideContext db, CancellationToken stoppingToken = new());

/// <summary>
/// Bans a user and logs the ban attempt to the database.
Expand All @@ -30,7 +29,7 @@ public abstract Task<bool> Handle(string channelId, TwitchApiProxy botProxy, Cha
/// <param name="db">The database.</param>
/// <param name="stoppingToken">The cancellation token.</param>
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public override bool ShouldRun(TwitchUserConfig config) {

/// <inheritdoc />
public override async Task<bool> Handle(string channelId, TwitchApiProxy botProxy, ChatMessage message,
NullinsideContext db, CancellationToken stoppingToken = new()) {
INullinsideContext db, CancellationToken stoppingToken = new()) {
if (!message.IsFirstMessage) {
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Nullinside.Api.TwitchBot/ChatRules/Discord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public override bool ShouldRun(TwitchUserConfig config) {

/// <inheritdoc />
public override async Task<bool> Handle(string channelId, TwitchApiProxy botProxy, ChatMessage message,
NullinsideContext db, CancellationToken stoppingToken = new()) {
INullinsideContext db, CancellationToken stoppingToken = new()) {
if (!message.IsFirstMessage) {
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Nullinside.Api.TwitchBot/ChatRules/Dogehype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public override bool ShouldRun(TwitchUserConfig config) {

/// <inheritdoc />
public override async Task<bool> 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();
Expand Down
2 changes: 1 addition & 1 deletion src/Nullinside.Api.TwitchBot/ChatRules/HostHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public override bool ShouldRun(TwitchUserConfig config) {

/// <inheritdoc />
public override async Task<bool> 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();
Expand Down
2 changes: 1 addition & 1 deletion src/Nullinside.Api.TwitchBot/ChatRules/IChatRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ public interface IChatRule {
/// <param name="db">The database.</param>
/// <param name="stoppingToken">The cancellation token.</param>
/// <returns>An asynchronous task.</returns>
public Task<bool> Handle(string channelId, TwitchApiProxy botProxy, ChatMessage message, NullinsideContext db,
public Task<bool> Handle(string channelId, TwitchApiProxy botProxy, ChatMessage message, INullinsideContext db,
CancellationToken stoppingToken = new());
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public override bool ShouldRun(TwitchUserConfig config) {

/// <inheritdoc />
public override async Task<bool> Handle(string channelId, TwitchApiProxy botProxy, ChatMessage message,
NullinsideContext db, CancellationToken stoppingToken = new()) {
INullinsideContext db, CancellationToken stoppingToken = new()) {
if (!message.IsFirstMessage) {
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Nullinside.Api.TwitchBot/ChatRules/Naked.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public override bool ShouldRun(TwitchUserConfig config) {

/// <inheritdoc />
public override async Task<bool> 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))) {
Expand Down
2 changes: 1 addition & 1 deletion src/Nullinside.Api.TwitchBot/ChatRules/StreamRise.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public override bool ShouldRun(TwitchUserConfig config) {

/// <inheritdoc />
public override async Task<bool> 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);
Expand Down
2 changes: 1 addition & 1 deletion src/Nullinside.Api.TwitchBot/ChatRules/StreamViewers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public override bool ShouldRun(TwitchUserConfig config) {

/// <inheritdoc />
public override async Task<bool> Handle(string channelId, TwitchApiProxy botProxy, ChatMessage message,
NullinsideContext db, CancellationToken stoppingToken = new()) {
INullinsideContext db, CancellationToken stoppingToken = new()) {
if (!message.IsFirstMessage) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class TwitchChatMessageMonitorConsumer : IDisposable {
/// <summary>
/// The nullinside database.
/// </summary>
private readonly NullinsideContext _db;
private readonly INullinsideContext _db;

/// <summary>
/// The non-priority queue to scan messages from.
Expand All @@ -58,7 +58,7 @@ public class TwitchChatMessageMonitorConsumer : IDisposable {
/// </summary>
/// <param name="db">The database.</param>
/// <param name="queue">The non-priority queue to scan messages from.</param>
public TwitchChatMessageMonitorConsumer(NullinsideContext db, BlockingCollection<ChatMessage> queue) {
public TwitchChatMessageMonitorConsumer(INullinsideContext db, BlockingCollection<ChatMessage> queue) {
_db = db;
_queue = queue;

Expand Down
4 changes: 2 additions & 2 deletions src/Nullinside.Api.TwitchBot/Controllers/BotController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class BotController : ControllerBase {
/// <summary>
/// The nullinside api database.
/// </summary>
private readonly NullinsideContext _dbContext;
private readonly INullinsideContext _dbContext;

/// <summary>
/// The logger.
Expand All @@ -41,7 +41,7 @@ public class BotController : ControllerBase {
/// </summary>
/// <param name="dbContext">The nullinside database.</param>
/// <param name="configuration">The application's configuration.</param>
public BotController(NullinsideContext dbContext, IConfiguration configuration) {
public BotController(INullinsideContext dbContext, IConfiguration configuration) {
_dbContext = dbContext;
_configuration = configuration;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Nullinside.Api.TwitchBot/Controllers/LoginController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class LoginController : ControllerBase {
/// <summary>
/// The nullinside api database.
/// </summary>
private readonly NullinsideContext _dbContext;
private readonly INullinsideContext _dbContext;

/// <summary>
/// The logger.
Expand All @@ -35,7 +35,7 @@ public class LoginController : ControllerBase {
/// </summary>
/// <param name="dbContext">The nullinside database.</param>
/// <param name="configuration">The application's configuration.</param>
public LoginController(NullinsideContext dbContext, IConfiguration configuration) {
public LoginController(INullinsideContext dbContext, IConfiguration configuration) {
_dbContext = dbContext;
_configuration = configuration;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static TwitchApiProxy GetApi(this User user) {
/// <param name="user">The user to configure the twitch api as.</param>
/// <param name="stoppingToken">The stopping token.</param>
/// <returns>The twitch api.</returns>
public static async Task<TwitchApiProxy?> GetApiAndRefreshToken(this NullinsideContext db, User user,
public static async Task<TwitchApiProxy?> GetApiAndRefreshToken(this INullinsideContext db, User user,
CancellationToken stoppingToken = new()) {
// Get the API
TwitchApiProxy api = GetApi(user);
Expand Down Expand Up @@ -68,7 +68,7 @@ public static TwitchApiProxy GetApi(this User user) {
/// <param name="db">The database.</param>
/// <param name="stoppingToken">The stopping token.</param>
/// <returns>The twitch api.</returns>
public static async Task<TwitchApiProxy?> GetBotApiAndRefreshToken(this NullinsideContext db,
public static async Task<TwitchApiProxy?> GetBotApiAndRefreshToken(this INullinsideContext db,
CancellationToken stoppingToken = new()) {
// Get the bot user's information.
User? botUser = await db.Users.AsNoTracking()
Expand All @@ -88,7 +88,7 @@ public static TwitchApiProxy GetApi(this User user) {
/// <param name="bannedUsers">The users being banned.</param>
/// <param name="reason">The reason for the bans.</param>
/// <param name="stoppingToken">The stopping token.</param>
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<string> banUserIds = bannedUsers.Select(b => b.Id).ToList();
HashSet<string?> existingUsers = db.TwitchUser
Expand Down
2 changes: 1 addition & 1 deletion src/Nullinside.Api.TwitchBot/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<NullinsideContext>(optionsBuilder =>
builder.Services.AddDbContext<INullinsideContext, NullinsideContext>(optionsBuilder =>
optionsBuilder.UseMySQL(
$"server={server};database=nullinside;user={username};password={password};AllowUserVariables=true;",
builder => {
Expand Down
12 changes: 6 additions & 6 deletions src/Nullinside.Api.TwitchBot/Services/MainService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class MainService : BackgroundService {
/// <summary>
/// The database.
/// </summary>
private readonly NullinsideContext _db;
private readonly INullinsideContext _db;

/// <summary>
/// The logger.
Expand Down Expand Up @@ -86,7 +86,7 @@ public class MainService : BackgroundService {
public MainService(IServiceScopeFactory serviceScopeFactory) {
_serviceScopeFactory = serviceScopeFactory;
_scope = _serviceScopeFactory.CreateScope();
_db = _scope.ServiceProvider.GetRequiredService<NullinsideContext>();
_db = _scope.ServiceProvider.GetRequiredService<INullinsideContext>();
_chatMessageConsumer = new TwitchChatMessageMonitorConsumer(_db, _receivedMessageProcessingQueue);
}

Expand Down Expand Up @@ -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<NullinsideContext>()) {
await using (var db = scope.ServiceProvider.GetRequiredService<INullinsideContext>()) {
// Send logs to database
DumpLogsToDatabase(db);

Expand Down Expand Up @@ -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.
/// </summary>
/// <param name="db">The database.</param>
private void DumpLogsToDatabase(NullinsideContext db) {
private void DumpLogsToDatabase(INullinsideContext db) {
lock (_receivedBans) {
db.TwitchUserBannedOutsideOfBotLogs.AddRange(_receivedBans.Select(b => new TwitchUserBannedOutsideOfBotLogs {
Channel = b.UserBan.Channel,
Expand Down Expand Up @@ -254,7 +254,7 @@ private void OnTwitchMessageReceived(OnMessageReceivedArgs e) {
/// <param name="db">The database.</param>
/// <param name="stoppingToken">The stopping token.</param>
/// <returns>The list of users with the bot enabled.</returns>
private async Task<List<User>?> GetUsersWithBotEnabled(NullinsideContext db, CancellationToken stoppingToken) {
private async Task<List<User>?> GetUsersWithBotEnabled(INullinsideContext db, CancellationToken stoppingToken) {
return await
(from user in db.Users
orderby user.TwitchLastScanned
Expand Down Expand Up @@ -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<NullinsideContext>()) {
await using (var db = scope.ServiceProvider.GetRequiredService<INullinsideContext>()) {
// Get the API
TwitchApiProxy? botApi = await db.GetApiAndRefreshToken(botUser, stoppingToken);
if (null == _botRules || null == user.TwitchConfig || null == botApi) {
Expand Down
Loading