diff --git a/ProjectLighthouse.Servers.GameServer/Controllers/LoginController.cs b/ProjectLighthouse.Servers.GameServer/Controllers/LoginController.cs index dfc61e5d..2c797ee7 100644 --- a/ProjectLighthouse.Servers.GameServer/Controllers/LoginController.cs +++ b/ProjectLighthouse.Servers.GameServer/Controllers/LoginController.cs @@ -164,7 +164,7 @@ public class LoginController : ControllerBase } Logger.Info($"User's username has changed, old='{user.Username}', new='{npTicket.Username}', platform={npTicket.Platform}", LogArea.Login); user.Username = username; - this.database.PlatformLinkAttempts.RemoveWhere(p => p.UserId == user.UserId); + await this.database.PlatformLinkAttempts.RemoveWhere(p => p.UserId == user.UserId); // unlink other platforms because the names no longer match if (npTicket.Platform == Platform.RPCS3) user.LinkedPsnId = 0; diff --git a/ProjectLighthouse.Servers.GameServer/Controllers/LogoutController.cs b/ProjectLighthouse.Servers.GameServer/Controllers/LogoutController.cs index 342cb1c5..5ee0572c 100644 --- a/ProjectLighthouse.Servers.GameServer/Controllers/LogoutController.cs +++ b/ProjectLighthouse.Servers.GameServer/Controllers/LogoutController.cs @@ -32,9 +32,8 @@ public class LogoutController : ControllerBase user.LastLogout = TimeHelper.TimestampMillis; - this.database.GameTokens.RemoveWhere(t => t.TokenId == token.TokenId); - this.database.LastContacts.RemoveWhere(c => c.UserId == token.UserId); - await this.database.SaveChangesAsync(); + await this.database.GameTokens.RemoveWhere(t => t.TokenId == token.TokenId); + await this.database.LastContacts.RemoveWhere(c => c.UserId == token.UserId); return this.Ok(); } diff --git a/ProjectLighthouse.Servers.Website/Controllers/ExternalAuth/AuthenticationController.cs b/ProjectLighthouse.Servers.Website/Controllers/ExternalAuth/AuthenticationController.cs index 4de4709e..50b0a1a8 100644 --- a/ProjectLighthouse.Servers.Website/Controllers/ExternalAuth/AuthenticationController.cs +++ b/ProjectLighthouse.Servers.Website/Controllers/ExternalAuth/AuthenticationController.cs @@ -38,9 +38,7 @@ public class AuthenticationController : ControllerBase invalidTokens = new[] { Platform.RPCS3, }; } - this.database.GameTokens.RemoveWhere(t => t.UserId == user.UserId && invalidTokens.Contains(t.Platform)); - - await this.database.SaveChangesAsync(); + await this.database.GameTokens.RemoveWhere(t => t.UserId == user.UserId && invalidTokens.Contains(t.Platform)); return this.Redirect("~/authentication"); } diff --git a/ProjectLighthouse/Administration/Maintenance/RepeatingTasks/RemoveExpiredTokensTask.cs b/ProjectLighthouse/Administration/Maintenance/RepeatingTasks/RemoveExpiredTokensTask.cs index c8747b9d..c20dc040 100644 --- a/ProjectLighthouse/Administration/Maintenance/RepeatingTasks/RemoveExpiredTokensTask.cs +++ b/ProjectLighthouse/Administration/Maintenance/RepeatingTasks/RemoveExpiredTokensTask.cs @@ -11,5 +11,5 @@ public class RemoveExpiredTokensTask : IRepeatingTask public TimeSpan RepeatInterval => TimeSpan.FromHours(1); public DateTime LastRan { get; set; } - public Task Run(DatabaseContext database) => database.RemoveExpiredTokens(); + public async Task Run(DatabaseContext database) => await database.RemoveExpiredTokens(); } \ No newline at end of file diff --git a/ProjectLighthouse/Database/DatabaseContext.Utils.cs b/ProjectLighthouse/Database/DatabaseContext.Utils.cs index cfe8aa74..fd5317b3 100644 --- a/ProjectLighthouse/Database/DatabaseContext.Utils.cs +++ b/ProjectLighthouse/Database/DatabaseContext.Utils.cs @@ -170,9 +170,7 @@ public partial class DatabaseContext { if (userId == blockedUser.UserId) return; - this.BlockedProfiles.RemoveWhere(bp => bp.BlockedUserId == blockedUser.UserId && bp.UserId == userId); - - await this.SaveChangesAsync(); + await this.BlockedProfiles.RemoveWhere(bp => bp.BlockedUserId == blockedUser.UserId && bp.UserId == userId); } public async Task IsUserBlockedBy(int userId, int targetId) diff --git a/ProjectLighthouse/Database/DatabaseContext.WebTokens.cs b/ProjectLighthouse/Database/DatabaseContext.WebTokens.cs index 328f8106..e8d6150d 100644 --- a/ProjectLighthouse/Database/DatabaseContext.WebTokens.cs +++ b/ProjectLighthouse/Database/DatabaseContext.WebTokens.cs @@ -1,5 +1,6 @@ #nullable enable using System; +using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using LBPUnion.ProjectLighthouse.Extensions; @@ -91,18 +92,18 @@ public partial class DatabaseContext public async Task RemoveExpiredTokens() { - foreach (GameTokenEntity token in await this.GameTokens.Where(t => DateTime.Now > t.ExpiresAt).ToListAsync()) + List expiredTokens = await this.GameTokens.Where(t => DateTime.Now > t.ExpiresAt).ToListAsync(); + foreach (GameTokenEntity token in expiredTokens) { UserEntity? user = await this.Users.FirstOrDefaultAsync(u => u.UserId == token.UserId); if (user != null) user.LastLogout = TimeHelper.TimestampMillis; - this.GameTokens.Remove(token); } - - this.WebTokens.RemoveWhere(t => DateTime.Now > t.ExpiresAt); - this.EmailVerificationTokens.RemoveWhere(t => DateTime.Now > t.ExpiresAt); - this.EmailSetTokens.RemoveWhere(t => DateTime.Now > t.ExpiresAt); - await this.SaveChangesAsync(); + + await this.GameTokens.RemoveWhere(t => DateTime.Now > t.ExpiresAt); + await this.WebTokens.RemoveWhere(t => DateTime.Now > t.ExpiresAt); + await this.EmailVerificationTokens.RemoveWhere(t => DateTime.Now > t.ExpiresAt); + await this.EmailSetTokens.RemoveWhere(t => DateTime.Now > t.ExpiresAt); } public async Task RemoveRegistrationToken(string? tokenString) diff --git a/ProjectLighthouse/Extensions/DatabaseExtensions.cs b/ProjectLighthouse/Extensions/DatabaseExtensions.cs index 6afec267..ef990b85 100644 --- a/ProjectLighthouse/Extensions/DatabaseExtensions.cs +++ b/ProjectLighthouse/Extensions/DatabaseExtensions.cs @@ -55,6 +55,6 @@ public static class DatabaseExtensions public static async Task Has(this IQueryable queryable, Expression> predicate) => await queryable.FirstOrDefaultAsync(predicate) != null; - public static void RemoveWhere(this DbSet queryable, Expression> predicate) where T : class - => queryable.RemoveRange(queryable.Where(predicate)); + public static async Task RemoveWhere(this DbSet queryable, Expression> predicate) where T : class + => await queryable.Where(predicate).ExecuteDeleteAsync(); } \ No newline at end of file diff --git a/ProjectLighthouse/Helpers/EmailHelper.cs b/ProjectLighthouse/Helpers/EmailHelper.cs index 95b63cd1..094eb086 100644 --- a/ProjectLighthouse/Helpers/EmailHelper.cs +++ b/ProjectLighthouse/Helpers/EmailHelper.cs @@ -51,7 +51,7 @@ public partial class SMTPHelper string? existingToken = await database.EmailVerificationTokens.Where(v => v.UserId == user.UserId) .Select(v => v.EmailToken) .FirstOrDefaultAsync(); - if (existingToken != null) database.EmailVerificationTokens.RemoveWhere(t => t.EmailToken == existingToken); + if (existingToken != null) await database.EmailVerificationTokens.RemoveWhere(t => t.EmailToken == existingToken); EmailVerificationTokenEntity verifyToken = new() {