mirror of
https://github.com/Caraxi/mare.server.git
synced 2025-12-12 22:17:22 +01:00
performance improvements for queries
This commit is contained in:
parent
b9ac535836
commit
0e0a75a71b
3 changed files with 17 additions and 16 deletions
|
|
@ -32,15 +32,15 @@ namespace MareSynchronosServer.Authentication
|
||||||
|
|
||||||
using var sha256 = SHA256.Create();
|
using var sha256 = SHA256.Create();
|
||||||
var hashedHeader = BitConverter.ToString(sha256.ComputeHash(Encoding.UTF8.GetBytes(authHeader))).Replace("-", "");
|
var hashedHeader = BitConverter.ToString(sha256.ComputeHash(Encoding.UTF8.GetBytes(authHeader))).Replace("-", "");
|
||||||
var user = await _mareDbContext.Users.AsNoTracking().SingleOrDefaultAsync(m => m.SecretKey == hashedHeader);
|
var uid = (await _mareDbContext.Users.AsNoTracking().FirstOrDefaultAsync(m => m.SecretKey == hashedHeader))?.UID;
|
||||||
|
|
||||||
if (user == null)
|
if (uid == null)
|
||||||
{
|
{
|
||||||
return AuthenticateResult.Fail("Failed Authorization");
|
return AuthenticateResult.Fail("Failed Authorization");
|
||||||
}
|
}
|
||||||
|
|
||||||
var claims = new List<Claim> {
|
var claims = new List<Claim> {
|
||||||
new Claim(ClaimTypes.NameIdentifier, user.UID)
|
new Claim(ClaimTypes.NameIdentifier, uid)
|
||||||
};
|
};
|
||||||
|
|
||||||
var identity = new ClaimsIdentity(claims, nameof(SecretKeyAuthenticationHandler));
|
var identity = new ClaimsIdentity(claims, nameof(SecretKeyAuthenticationHandler));
|
||||||
|
|
|
||||||
|
|
@ -141,7 +141,7 @@ namespace MareSynchronosServer.Hubs
|
||||||
{
|
{
|
||||||
fileListHashes = fileListHashes.Where(f => !string.IsNullOrEmpty(f)).Distinct().ToList();
|
fileListHashes = fileListHashes.Where(f => !string.IsNullOrEmpty(f)).Distinct().ToList();
|
||||||
Logger.LogInformation("User " + AuthenticatedUserId + " sending files");
|
Logger.LogInformation("User " + AuthenticatedUserId + " sending files");
|
||||||
var forbiddenFiles = DbContext.ForbiddenUploadEntries.AsNoTracking().Where(f => fileListHashes.Contains(f.Hash));
|
var forbiddenFiles = await DbContext.ForbiddenUploadEntries.AsNoTracking().Where(f => fileListHashes.Contains(f.Hash)).ToListAsync();
|
||||||
var filesToUpload = new List<UploadFileDto>();
|
var filesToUpload = new List<UploadFileDto>();
|
||||||
filesToUpload.AddRange(forbiddenFiles.Select(f => new UploadFileDto()
|
filesToUpload.AddRange(forbiddenFiles.Select(f => new UploadFileDto()
|
||||||
{
|
{
|
||||||
|
|
@ -150,7 +150,7 @@ namespace MareSynchronosServer.Hubs
|
||||||
IsForbidden = true
|
IsForbidden = true
|
||||||
}));
|
}));
|
||||||
fileListHashes.RemoveAll(f => filesToUpload.Any(u => u.Hash == f));
|
fileListHashes.RemoveAll(f => filesToUpload.Any(u => u.Hash == f));
|
||||||
var existingFiles = DbContext.Files.Where(f => fileListHashes.Contains(f.Hash));
|
var existingFiles = await DbContext.Files.AsNoTracking().Where(f => fileListHashes.Contains(f.Hash)).ToListAsync();
|
||||||
foreach (var file in fileListHashes.Where(f => existingFiles.All(e => e.Hash != f) && filesToUpload.All(u => u.Hash != f)))
|
foreach (var file in fileListHashes.Where(f => existingFiles.All(e => e.Hash != f) && filesToUpload.All(u => u.Hash != f)))
|
||||||
{
|
{
|
||||||
Logger.LogInformation("User " + AuthenticatedUserId + " needs upload: " + file);
|
Logger.LogInformation("User " + AuthenticatedUserId + " needs upload: " + file);
|
||||||
|
|
|
||||||
|
|
@ -134,21 +134,22 @@ namespace MareSynchronosServer.Hubs
|
||||||
{
|
{
|
||||||
Logger.LogInformation("User " + AuthenticatedUserId + " pushing character data to " + visibleCharacterIds.Count + " visible clients");
|
Logger.LogInformation("User " + AuthenticatedUserId + " pushing character data to " + visibleCharacterIds.Count + " visible clients");
|
||||||
|
|
||||||
var uid = AuthenticatedUserId;
|
var user = GetAuthenticatedUserUntracked();
|
||||||
var entriesHavingThisUser = DbContext.ClientPairs.AsNoTracking()
|
var senderPairedUsers = DbContext.ClientPairs.AsNoTracking()
|
||||||
.Include(w => w.User)
|
.Include(w => w.User)
|
||||||
.Include(w => w.OtherUser)
|
.Include(w => w.OtherUser)
|
||||||
.Where(w => w.OtherUser.UID == uid && !w.IsPaused
|
.Where(w => w.User.UID == user.UID && !w.IsPaused
|
||||||
&& visibleCharacterIds.Contains(w.User.CharacterIdentification)).ToList();
|
&& visibleCharacterIds.Contains(w.OtherUser.CharacterIdentification))
|
||||||
|
.Select(u => u.OtherUser).ToList();
|
||||||
|
|
||||||
foreach (var pair in entriesHavingThisUser)
|
foreach (var pairedUser in senderPairedUsers)
|
||||||
{
|
{
|
||||||
var ownEntry = DbContext.ClientPairs.AsNoTracking()
|
var isPaused = DbContext.ClientPairs.AsNoTracking()
|
||||||
.SingleOrDefault(w =>
|
.FirstOrDefault(w =>
|
||||||
w.User.UID == uid && w.OtherUser.UID == pair.User.UID);
|
w.User.UID == pairedUser.UID && w.OtherUser.UID == user.UID)?.IsPaused ?? true;
|
||||||
if (ownEntry == null || ownEntry.IsPaused) continue;
|
if (isPaused) continue;
|
||||||
await Clients.User(pair.User.UID).SendAsync(UserHubAPI.OnReceiveCharacterData, characterCache,
|
await Clients.User(pairedUser.UID).SendAsync(UserHubAPI.OnReceiveCharacterData, characterCache,
|
||||||
pair.OtherUser.CharacterIdentification);
|
user.CharacterIdentification);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue