adjust DTOs, add call to push character data to visible clients

This commit is contained in:
Stanley Dimant
2022-06-30 17:19:58 +02:00
parent 3e00bc4efd
commit d56923d553
13 changed files with 82 additions and 72 deletions

View File

@@ -1,12 +1,6 @@
using System; namespace MareSynchronos.API
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MareSynchronos.API
{ {
public class BannedUserDto public record BannedUserDto
{ {
public string CharacterHash { get; set; } public string CharacterHash { get; set; }
public string Reason { get; set; } public string Reason { get; set; }

View File

@@ -1,13 +1,8 @@
using System; using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace MareSynchronos.API namespace MareSynchronos.API
{ {
public class CharacterCacheDto public record CharacterCacheDto
{ {
public List<FileReplacementDto> FileReplacements { get; set; } = new(); public List<FileReplacementDto> FileReplacements { get; set; } = new();
public string GlamourerData { get; set; } public string GlamourerData { get; set; }
@@ -15,10 +10,4 @@ namespace MareSynchronos.API
public string Hash { get; set; } public string Hash { get; set; }
public int JobId { get; set; } public int JobId { get; set; }
} }
public class FileReplacementDto
{
public string[] GamePaths { get; set; } = Array.Empty<string>();
public string Hash { get; set; }
}
} }

View File

@@ -1,6 +1,6 @@
namespace MareSynchronos.API namespace MareSynchronos.API
{ {
public class ClientPairDto public record ClientPairDto
{ {
public string OtherUID { get; set; } public string OtherUID { get; set; }
public bool IsPaused { get; set; } public bool IsPaused { get; set; }

View File

@@ -1,12 +1,6 @@
using System; namespace MareSynchronos.API
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MareSynchronos.API
{ {
public class DownloadFileDto public record DownloadFileDto : ITransferFileDto
{ {
public bool FileExists { get; set; } = true; public bool FileExists { get; set; } = true;
public string Hash { get; set; } = string.Empty; public string Hash { get; set; } = string.Empty;

View File

@@ -0,0 +1,10 @@
using System;
namespace MareSynchronos.API
{
public record FileReplacementDto
{
public string[] GamePaths { get; set; } = Array.Empty<string>();
public string Hash { get; set; }
}
}

View File

@@ -1,12 +1,6 @@
using System; namespace MareSynchronos.API
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MareSynchronos.API
{ {
public class ForbiddenFileDto public record ForbiddenFileDto
{ {
public string Hash { get; set; } public string Hash { get; set; }
public string ForbiddenBy { get; set; } public string ForbiddenBy { get; set; }

View File

@@ -0,0 +1,9 @@
namespace MareSynchronos.API
{
public interface ITransferFileDto
{
string Hash { get; set; }
bool IsForbidden { get; set; }
string ForbiddenBy { get; set; }
}
}

View File

@@ -1,12 +1,6 @@
using System; namespace MareSynchronos.API
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MareSynchronos.API
{ {
public class LoggedInUserDto public record LoggedInUserDto
{ {
public bool IsAdmin { get; set; } public bool IsAdmin { get; set; }
public bool IsModerator { get; set; } public bool IsModerator { get; set; }

View File

@@ -1,12 +1,6 @@
using System; namespace MareSynchronos.API
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MareSynchronos.API
{ {
public class OnlineUserDto public record OnlineUserDto
{ {
public string UID { get; set; } public string UID { get; set; }
public string CharacterNameHash { get; set; } public string CharacterNameHash { get; set; }

View File

@@ -1,12 +1,6 @@
using System; namespace MareSynchronos.API
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MareSynchronos.API
{ {
public class UploadFileDto public record UploadFileDto : ITransferFileDto
{ {
public string Hash { get; set; } = string.Empty; public string Hash { get; set; } = string.Empty;
public bool IsForbidden { get; set; } = false; public bool IsForbidden { get; set; } = false;

View File

@@ -45,9 +45,16 @@ namespace MareSynchronosServer.Authentication
return AuthenticateResult.Fail("Failed Authorization"); return AuthenticateResult.Fail("Failed Authorization");
} }
user.CharacterIdentification = charNameHeader; if (user.CharacterIdentification != charNameHeader)
_mareDbContext.Users.Update(user); {
await _mareDbContext.SaveChangesAsync(); try
{
user.CharacterIdentification = charNameHeader;
_mareDbContext.Users.Update(user);
await _mareDbContext.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException) { }
}
var claims = new List<Claim> { var claims = new List<Claim> {
new Claim(ClaimTypes.Name, user.CharacterIdentification), new Claim(ClaimTypes.Name, user.CharacterIdentification),

View File

@@ -17,10 +17,11 @@ namespace MareSynchronosServer.Hubs
public async Task<LoggedInUserDto> Heartbeat() public async Task<LoggedInUserDto> Heartbeat()
{ {
var userId = Context.User!.Claims.SingleOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value; var userId = Context.User!.Claims.SingleOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value;
Logger.LogInformation("Heartbeat from " + (userId ?? "Unknown user"));
if (userId != null) if (userId != null)
{ {
Logger.LogInformation("Heartbeat from " + userId);
var user = (await DbContext.Users.SingleAsync(u => u.UID == userId)); var user = (await DbContext.Users.SingleAsync(u => u.UID == userId));
return new LoggedInUserDto return new LoggedInUserDto
{ {

View File

@@ -45,6 +45,13 @@ namespace MareSynchronosServer.Hubs
user.UID = uid; user.UID = uid;
hasValidUid = true; hasValidUid = true;
} }
// make the first registered user on the service to admin
if (!await DbContext.Users.AnyAsync())
{
user.IsAdmin = true;
}
DbContext.Users.Add(user); DbContext.Users.Add(user);
Logger.LogInformation("User registered: " + user.UID); Logger.LogInformation("User registered: " + user.UID);
@@ -89,6 +96,28 @@ namespace MareSynchronosServer.Hubs
} }
} }
[Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)]
public async Task PushCharacterDataToVisibleClients(CharacterCacheDto characterCache, List<string> visibleCharacterIds)
{
Logger.LogInformation("User " + AuthenticatedUserId + " pushing character data to visible clients");
var uid = AuthenticatedUserId;
var entriesHavingThisUser = DbContext.ClientPairs
.Include(w => w.User)
.Include(w => w.OtherUser)
.Where(w => w.OtherUser.UID == uid && !w.IsPaused
&& visibleCharacterIds.Contains(w.User.CharacterIdentification)).ToList();
foreach (var pair in entriesHavingThisUser)
{
var ownEntry = DbContext.ClientPairs.SingleOrDefault(w =>
w.User.UID == uid && w.OtherUser.UID == pair.User.UID);
if (ownEntry == null || ownEntry.IsPaused) continue;
await Clients.User(pair.User.UID).SendAsync("ReceiveCharacterData", characterCache,
pair.OtherUser.CharacterIdentification);
}
}
[Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)] [Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)]
public async Task PushCharacterData(CharacterCacheDto characterCache, List<string> visibleCharacterIds) public async Task PushCharacterData(CharacterCacheDto characterCache, List<string> visibleCharacterIds)
{ {
@@ -152,7 +181,7 @@ namespace MareSynchronosServer.Hubs
await Clients.Users(otherEntries.Select(e => e.User.UID)).SendAsync("AddOnlinePairedPlayer", ownUser.CharacterIdentification); await Clients.Users(otherEntries.Select(e => e.User.UID)).SendAsync("AddOnlinePairedPlayer", ownUser.CharacterIdentification);
await Clients.All.SendAsync("UsersOnline", await Clients.All.SendAsync("UsersOnline",
await DbContext.Users.CountAsync(u => !string.IsNullOrEmpty(u.CharacterIdentification))); await DbContext.Users.CountAsync(u => !string.IsNullOrEmpty(u.CharacterIdentification)));
return otherEntries.Select(e => e.User.CharacterIdentification).ToList(); return otherEntries.Select(e => e.User.CharacterIdentification).Distinct().ToList();
} }
[Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)] [Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)]
@@ -340,11 +369,6 @@ namespace MareSynchronosServer.Hubs
{ {
Logger.LogInformation("Disconnect from " + AuthenticatedUserId); Logger.LogInformation("Disconnect from " + AuthenticatedUserId);
var outdatedCharacterData = DbContext.CharacterData.Where(v => v.UserId == user.UID);
DbContext.RemoveRange(outdatedCharacterData);
user.CharacterIdentification = null;
await DbContext.SaveChangesAsync();
var otherUsers = DbContext.ClientPairs var otherUsers = DbContext.ClientPairs
.Include(u => u.User) .Include(u => u.User)
.Include(u => u.OtherUser) .Include(u => u.OtherUser)
@@ -354,6 +378,12 @@ namespace MareSynchronosServer.Hubs
var otherEntries = DbContext.ClientPairs.Include(u => u.User) var otherEntries = DbContext.ClientPairs.Include(u => u.User)
.Where(u => otherUsers.Any(e => e == u.User) && u.OtherUser == user && !u.IsPaused).ToList(); .Where(u => otherUsers.Any(e => e == u.User) && u.OtherUser == user && !u.IsPaused).ToList();
await Clients.Users(otherEntries.Select(e => e.User.UID)).SendAsync("RemoveOnlinePairedPlayer", user.CharacterIdentification); await Clients.Users(otherEntries.Select(e => e.User.UID)).SendAsync("RemoveOnlinePairedPlayer", user.CharacterIdentification);
var outdatedCharacterData = DbContext.CharacterData.Where(v => v.UserId == user.UID);
DbContext.RemoveRange(outdatedCharacterData);
user.CharacterIdentification = null;
await DbContext.SaveChangesAsync();
await Clients.All.SendAsync("UsersOnline", await Clients.All.SendAsync("UsersOnline",
await DbContext.Users.CountAsync(u => !string.IsNullOrEmpty(u.CharacterIdentification))); await DbContext.Users.CountAsync(u => !string.IsNullOrEmpty(u.CharacterIdentification)));
} }