move marehub to strong api usage (#14)
* move marehub to strong api usage * fix merge issues * latest api * further optimizations
This commit is contained in:
@@ -19,8 +19,7 @@ public partial class MareHub
|
||||
private List<string> OnlineAdmins => _dbContext.Users.Where(u => (u.IsModerator || u.IsAdmin)).Select(u => u.UID).ToList();
|
||||
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
[HubMethodName(Api.SendAdminChangeModeratorStatus)]
|
||||
public async Task ChangeModeratorStatus(string uid, bool isModerator)
|
||||
public async Task AdminChangeModeratorStatus(string uid, bool isModerator)
|
||||
{
|
||||
if (!IsAdmin) return;
|
||||
var user = await _dbContext.Users.SingleOrDefaultAsync(u => u.UID == uid).ConfigureAwait(false);
|
||||
@@ -30,12 +29,11 @@ public partial class MareHub
|
||||
user.IsModerator = isModerator;
|
||||
_dbContext.Update(user);
|
||||
await _dbContext.SaveChangesAsync().ConfigureAwait(false);
|
||||
await Clients.Users(user.UID).SendAsync(Api.OnAdminForcedReconnect).ConfigureAwait(false);
|
||||
await Clients.Users(user.UID).Client_AdminForcedReconnect().ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
[HubMethodName(Api.SendAdminDeleteBannedUser)]
|
||||
public async Task DeleteBannedUser(BannedUserDto dto)
|
||||
public async Task AdminDeleteBannedUser(BannedUserDto dto)
|
||||
{
|
||||
if (!IsModerator || string.IsNullOrEmpty(dto.CharacterHash)) return;
|
||||
|
||||
@@ -48,12 +46,11 @@ public partial class MareHub
|
||||
|
||||
_dbContext.Remove(existingUser);
|
||||
await _dbContext.SaveChangesAsync().ConfigureAwait(false);
|
||||
await Clients.Users(OnlineAdmins).SendAsync(Api.OnAdminDeleteBannedUser, dto).ConfigureAwait(false);
|
||||
await Clients.Users(OnlineAdmins).Client_AdminDeleteBannedUser(dto).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
[HubMethodName(Api.SendAdminDeleteForbiddenFile)]
|
||||
public async Task DeleteForbiddenFile(ForbiddenFileDto dto)
|
||||
public async Task AdminDeleteForbiddenFile(ForbiddenFileDto dto)
|
||||
{
|
||||
if (!IsAdmin || string.IsNullOrEmpty(dto.Hash)) return;
|
||||
|
||||
@@ -66,12 +63,11 @@ public partial class MareHub
|
||||
|
||||
_dbContext.Remove(existingFile);
|
||||
await _dbContext.SaveChangesAsync().ConfigureAwait(false);
|
||||
await Clients.Users(OnlineAdmins).SendAsync(Api.OnAdminDeleteForbiddenFile, dto).ConfigureAwait(false);
|
||||
await Clients.Users(OnlineAdmins).Client_AdminDeleteForbiddenFile(dto).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
[HubMethodName(Api.InvokeAdminGetBannedUsers)]
|
||||
public async Task<List<BannedUserDto>> GetBannedUsers()
|
||||
public async Task<List<BannedUserDto>> AdminGetBannedUsers()
|
||||
{
|
||||
if (!IsModerator) return null;
|
||||
|
||||
@@ -83,8 +79,7 @@ public partial class MareHub
|
||||
}
|
||||
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
[HubMethodName(Api.InvokeAdminGetForbiddenFiles)]
|
||||
public async Task<List<ForbiddenFileDto>> GetForbiddenFiles()
|
||||
public async Task<List<ForbiddenFileDto>> AdminGetForbiddenFiles()
|
||||
{
|
||||
if (!IsModerator) return null;
|
||||
|
||||
@@ -96,7 +91,6 @@ public partial class MareHub
|
||||
}
|
||||
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
[HubMethodName(Api.InvokeAdminGetOnlineUsers)]
|
||||
public async Task<List<OnlineUserDto>> AdminGetOnlineUsers()
|
||||
{
|
||||
if (!IsModerator) return null;
|
||||
@@ -112,8 +106,7 @@ public partial class MareHub
|
||||
}
|
||||
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
[HubMethodName(Api.SendAdminUpdateOrAddBannedUser)]
|
||||
public async Task UpdateOrAddBannedUser(BannedUserDto dto)
|
||||
public async Task AdminUpdateOrAddBannedUser(BannedUserDto dto)
|
||||
{
|
||||
if (!IsModerator || string.IsNullOrEmpty(dto.CharacterHash)) return;
|
||||
|
||||
@@ -134,17 +127,16 @@ public partial class MareHub
|
||||
}
|
||||
|
||||
await _dbContext.SaveChangesAsync().ConfigureAwait(false);
|
||||
await Clients.Users(OnlineAdmins).SendAsync(Api.OnAdminUpdateOrAddBannedUser, dto).ConfigureAwait(false);
|
||||
await Clients.Users(OnlineAdmins).Client_AdminUpdateOrAddBannedUser(dto).ConfigureAwait(false);
|
||||
var bannedUser = await _clientIdentService.GetUidForCharacterIdent(dto.CharacterHash).ConfigureAwait(false);
|
||||
if (!string.IsNullOrEmpty(bannedUser))
|
||||
{
|
||||
await Clients.User(bannedUser).SendAsync(Api.OnAdminForcedReconnect).ConfigureAwait(false);
|
||||
await Clients.User(bannedUser).Client_AdminForcedReconnect().ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
[HubMethodName(Api.SendAdminUpdateOrAddForbiddenFile)]
|
||||
public async Task UpdateOrAddForbiddenFile(ForbiddenFileDto dto)
|
||||
public async Task AdminUpdateOrAddForbiddenFile(ForbiddenFileDto dto)
|
||||
{
|
||||
if (!IsAdmin || string.IsNullOrEmpty(dto.Hash)) return;
|
||||
|
||||
@@ -166,6 +158,6 @@ public partial class MareHub
|
||||
|
||||
await _dbContext.SaveChangesAsync().ConfigureAwait(false);
|
||||
|
||||
await Clients.Users(OnlineAdmins).SendAsync(Api.OnAdminUpdateOrAddForbiddenFile, dto).ConfigureAwait(false);
|
||||
await Clients.Users(OnlineAdmins).Client_AdminUpdateOrAddForbiddenFile(dto).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
using MareSynchronos.API;
|
||||
using System.Threading.Tasks;
|
||||
using System;
|
||||
|
||||
namespace MareSynchronosServer.Hubs
|
||||
{
|
||||
public partial class MareHub
|
||||
{
|
||||
public Task Client_UserUpdateClientPairs(ClientPairDto clientPairDto)
|
||||
{
|
||||
throw new PlatformNotSupportedException("Calling clientside method on server not supported");
|
||||
}
|
||||
|
||||
public Task Client_UpdateSystemInfo(SystemInfoDto systemInfo)
|
||||
{
|
||||
throw new PlatformNotSupportedException("Calling clientside method on server not supported");
|
||||
}
|
||||
|
||||
public Task Client_UserReceiveCharacterData(CharacterCacheDto clientPairDto, string characterIdent)
|
||||
{
|
||||
throw new PlatformNotSupportedException("Calling clientside method on server not supported");
|
||||
}
|
||||
|
||||
public Task Client_UserChangePairedPlayer(string characterIdent, bool isOnline)
|
||||
{
|
||||
throw new PlatformNotSupportedException("Calling clientside method on server not supported");
|
||||
}
|
||||
|
||||
public Task Client_GroupChange(GroupDto groupDto)
|
||||
{
|
||||
throw new PlatformNotSupportedException("Calling clientside method on server not supported");
|
||||
}
|
||||
|
||||
public Task Client_GroupUserChange(GroupPairDto groupPairDto)
|
||||
{
|
||||
throw new PlatformNotSupportedException("Calling clientside method on server not supported");
|
||||
}
|
||||
|
||||
public Task Client_AdminForcedReconnect()
|
||||
{
|
||||
throw new PlatformNotSupportedException("Calling clientside method on server not supported");
|
||||
}
|
||||
|
||||
public Task Client_AdminDeleteBannedUser(BannedUserDto dto)
|
||||
{
|
||||
throw new PlatformNotSupportedException("Calling clientside method on server not supported");
|
||||
}
|
||||
|
||||
public Task Client_AdminDeleteForbiddenFile(ForbiddenFileDto dto)
|
||||
{
|
||||
throw new PlatformNotSupportedException("Calling clientside method on server not supported");
|
||||
}
|
||||
|
||||
public Task Client_AdminUpdateOrAddBannedUser(BannedUserDto dto)
|
||||
{
|
||||
throw new PlatformNotSupportedException("Calling clientside method on server not supported");
|
||||
}
|
||||
|
||||
public Task Client_AdminUpdateOrAddForbiddenFile(ForbiddenFileDto dto)
|
||||
{
|
||||
throw new PlatformNotSupportedException("Calling clientside method on server not supported");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ using System.Threading.Tasks;
|
||||
using Google.Protobuf;
|
||||
using Grpc.Core;
|
||||
using MareSynchronos.API;
|
||||
using MareSynchronosServer.Utils;
|
||||
using MareSynchronosShared.Authentication;
|
||||
using MareSynchronosShared.Models;
|
||||
using MareSynchronosShared.Protos;
|
||||
@@ -21,10 +22,9 @@ namespace MareSynchronosServer.Hubs;
|
||||
public partial class MareHub
|
||||
{
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
[HubMethodName(Api.SendFileAbortUpload)]
|
||||
public async Task AbortUpload()
|
||||
public async Task FilesAbortUpload()
|
||||
{
|
||||
_logger.LogCallInfo(Api.SendFileAbortUpload);
|
||||
_logger.LogCallInfo();
|
||||
var userId = AuthenticatedUserId;
|
||||
var notUploadedFiles = _dbContext.Files.Where(f => !f.Uploaded && f.Uploader.UID == userId).ToList();
|
||||
_dbContext.RemoveRange(notUploadedFiles);
|
||||
@@ -32,10 +32,9 @@ public partial class MareHub
|
||||
}
|
||||
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
[HubMethodName(Api.SendFileDeleteAllFiles)]
|
||||
public async Task DeleteAllFiles()
|
||||
public async Task FilesDeleteAll()
|
||||
{
|
||||
_logger.LogCallInfo(Api.SendFileDeleteAllFiles);
|
||||
_logger.LogCallInfo();
|
||||
|
||||
var ownFiles = await _dbContext.Files.Where(f => f.Uploaded && f.Uploader.UID == AuthenticatedUserId).ToListAsync().ConfigureAwait(false);
|
||||
var request = new DeleteFilesRequest();
|
||||
@@ -48,10 +47,9 @@ public partial class MareHub
|
||||
}
|
||||
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
[HubMethodName(Api.InvokeGetFilesSizes)]
|
||||
public async Task<List<DownloadFileDto>> GetFilesSizes(List<string> hashes)
|
||||
public async Task<List<DownloadFileDto>> FilesGetSizes(List<string> hashes)
|
||||
{
|
||||
_logger.LogCallInfo(Api.InvokeGetFilesSizes, hashes.Count.ToString());
|
||||
_logger.LogCallInfo(MareHubLogger.Args(hashes.Count.ToString()));
|
||||
|
||||
var allFiles = await _dbContext.Files.Where(f => hashes.Contains(f.Hash)).ToListAsync().ConfigureAwait(false);
|
||||
var forbiddenFiles = await _dbContext.ForbiddenUploadEntries.
|
||||
@@ -86,24 +84,22 @@ public partial class MareHub
|
||||
}
|
||||
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
[HubMethodName(Api.InvokeFileIsUploadFinished)]
|
||||
public async Task<bool> IsUploadFinished()
|
||||
public async Task<bool> FilesIsUploadFinished()
|
||||
{
|
||||
_logger.LogCallInfo(Api.InvokeFileIsUploadFinished);
|
||||
_logger.LogCallInfo();
|
||||
var userUid = AuthenticatedUserId;
|
||||
return await _dbContext.Files.AsNoTracking()
|
||||
.AnyAsync(f => f.Uploader.UID == userUid && !f.Uploaded).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
[HubMethodName(Api.InvokeFileSendFiles)]
|
||||
public async Task<List<UploadFileDto>> SendFiles(List<string> fileListHashes)
|
||||
public async Task<List<UploadFileDto>> FilesSend(List<string> fileListHashes)
|
||||
{
|
||||
var userSentHashes = new HashSet<string>(fileListHashes.Distinct(StringComparer.Ordinal), StringComparer.Ordinal);
|
||||
_logger.LogCallInfo(Api.InvokeFileSendFiles, userSentHashes.Count.ToString());
|
||||
_logger.LogCallInfo(MareHubLogger.Args(userSentHashes.Count.ToString()));
|
||||
var notCoveredFiles = new Dictionary<string, UploadFileDto>(StringComparer.Ordinal);
|
||||
var forbiddenFiles = await _dbContext.ForbiddenUploadEntries.AsNoTracking().Where(f => userSentHashes.Contains(f.Hash)).ToDictionaryAsync(f => f.Hash, f => f).ConfigureAwait(false);
|
||||
var existingFiles = await _dbContext.Files.AsNoTracking().Where(f => userSentHashes.Contains(f.Hash)).ToDictionaryAsync(f => f.Hash, f => f).ConfigureAwait(false);
|
||||
var forbiddenFiles = await _dbContext.ForbiddenUploadEntries.AsNoTracking().Where(f => userSentHashes.Contains(f.Hash)).AsNoTracking().ToDictionaryAsync(f => f.Hash, f => f).ConfigureAwait(false);
|
||||
var existingFiles = await _dbContext.Files.AsNoTracking().Where(f => userSentHashes.Contains(f.Hash)).AsNoTracking().ToDictionaryAsync(f => f.Hash, f => f).ConfigureAwait(false);
|
||||
var uploader = await _dbContext.Users.SingleAsync(u => u.UID == AuthenticatedUserId).ConfigureAwait(false);
|
||||
|
||||
List<FileCache> fileCachesToUpload = new();
|
||||
@@ -125,7 +121,7 @@ public partial class MareHub
|
||||
}
|
||||
if (existingFiles.ContainsKey(file)) { continue; }
|
||||
|
||||
_logger.LogCallInfo(Api.InvokeFileSendFiles, file, "Missing");
|
||||
_logger.LogCallInfo(MareHubLogger.Args(file, "Missing"));
|
||||
|
||||
var userId = AuthenticatedUserId;
|
||||
fileCachesToUpload.Add(new FileCache()
|
||||
@@ -147,10 +143,9 @@ public partial class MareHub
|
||||
}
|
||||
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
[HubMethodName(Api.SendFileUploadFileStreamAsync)]
|
||||
public async Task UploadFileStreamAsync(string hash, IAsyncEnumerable<byte[]> fileContent)
|
||||
public async Task FilesUploadStreamAsync(string hash, IAsyncEnumerable<byte[]> fileContent)
|
||||
{
|
||||
_logger.LogCallInfo(Api.SendFileUploadFileStreamAsync, hash);
|
||||
_logger.LogCallInfo(MareHubLogger.Args(hash));
|
||||
|
||||
var relatedFile = _dbContext.Files.SingleOrDefault(f => f.Hash == hash && f.Uploader.UID == AuthenticatedUserId && !f.Uploaded);
|
||||
if (relatedFile == null) return;
|
||||
@@ -192,7 +187,7 @@ public partial class MareHub
|
||||
return;
|
||||
}
|
||||
|
||||
_logger.LogCallInfo(Api.SendFileUploadFileStreamAsync, hash, "Uploaded");
|
||||
_logger.LogCallInfo(MareHubLogger.Args(hash, "Uploaded"));
|
||||
|
||||
try
|
||||
{
|
||||
@@ -203,7 +198,7 @@ public partial class MareHub
|
||||
var computedHashString = BitConverter.ToString(computedHash).Replace("-", "", StringComparison.Ordinal);
|
||||
if (!string.Equals(hash, computedHashString, StringComparison.Ordinal))
|
||||
{
|
||||
_logger.LogCallWarning(Api.SendFileUploadFileStreamAsync, hash, "Invalid", computedHashString);
|
||||
_logger.LogCallWarning(MareHubLogger.Args(hash, "Invalid", computedHashString));
|
||||
_dbContext.Remove(relatedFile);
|
||||
await _dbContext.SaveChangesAsync().ConfigureAwait(false);
|
||||
|
||||
@@ -232,11 +227,11 @@ public partial class MareHub
|
||||
tempFileStream.Close();
|
||||
await tempFileStream.DisposeAsync().ConfigureAwait(false);
|
||||
|
||||
_logger.LogCallInfo(Api.SendFileUploadFileStreamAsync, hash, "Pushed");
|
||||
_logger.LogCallInfo(MareHubLogger.Args(hash, "Pushed"));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogCallWarning(Api.SendFileUploadFileStreamAsync, "Failed", hash, ex.Message);
|
||||
_logger.LogCallWarning(MareHubLogger.Args("Failed", hash, ex.Message));
|
||||
_dbContext.Remove(relatedFile);
|
||||
await _dbContext.SaveChangesAsync().ConfigureAwait(false);
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ public partial class MareHub
|
||||
GID = Convert.ToString(otherGroupPair.GroupGID),
|
||||
PauseState = (userGroupPair.IsPaused || otherGroupPair.IsPaused)
|
||||
})
|
||||
).ToListAsync().ConfigureAwait(false);
|
||||
).AsNoTracking().ToListAsync().ConfigureAwait(false);
|
||||
|
||||
return query.GroupBy(g => g.UID, g => (g.GID, g.PauseState),
|
||||
(key, g) => new PausedEntry
|
||||
@@ -58,21 +58,24 @@ public partial class MareHub
|
||||
return ret.Where(k => !k.IsPaused).Select(k => k.UID).ToList();
|
||||
}
|
||||
|
||||
private async Task<List<string>> SendDataToAllPairedUsers(string apiMethod, object arg)
|
||||
private async Task<List<string>> SendOnlineToAllPairedUsers(string arg)
|
||||
{
|
||||
var usersToSendDataTo = await GetAllPairedUnpausedUsers().ConfigureAwait(false);
|
||||
await Clients.Users(usersToSendDataTo).SendAsync(apiMethod, arg).ConfigureAwait(false);
|
||||
await Clients.Users(usersToSendDataTo).Client_UserChangePairedPlayer(arg, true).ConfigureAwait(false);
|
||||
|
||||
return usersToSendDataTo;
|
||||
}
|
||||
|
||||
private async Task<List<string>> SendOfflineToAllPairedUsers(string arg)
|
||||
{
|
||||
var usersToSendDataTo = await GetAllPairedUnpausedUsers().ConfigureAwait(false);
|
||||
await Clients.Users(usersToSendDataTo).Client_UserChangePairedPlayer(arg, false).ConfigureAwait(false);
|
||||
|
||||
return usersToSendDataTo;
|
||||
}
|
||||
|
||||
public string AuthenticatedUserId => Context.User?.Claims?.SingleOrDefault(c => string.Equals(c.Type, ClaimTypes.NameIdentifier, StringComparison.Ordinal))?.Value ?? "Unknown";
|
||||
|
||||
protected async Task<User> GetAuthenticatedUserUntrackedAsync()
|
||||
{
|
||||
return await _dbContext.Users.AsNoTrackingWithIdentityResolution().SingleAsync(u => u.UID == AuthenticatedUserId).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private async Task UserGroupLeave(GroupPair groupUserPair, List<PausedEntry> allUserPairs, string userIdent, string? uid = null)
|
||||
{
|
||||
uid ??= AuthenticatedUserId;
|
||||
@@ -86,8 +89,8 @@ public partial class MareHub
|
||||
var groupUserIdent = await _clientIdentService.GetCharacterIdentForUid(groupUserPair.GroupUserUID).ConfigureAwait(false);
|
||||
if (!string.IsNullOrEmpty(groupUserIdent))
|
||||
{
|
||||
await Clients.User(uid).SendAsync(Api.OnUserRemoveOnlinePairedPlayer, groupUserIdent).ConfigureAwait(false);
|
||||
await Clients.User(groupUserPair.GroupUserUID).SendAsync(Api.OnUserRemoveOnlinePairedPlayer, userIdent).ConfigureAwait(false);
|
||||
await Clients.User(uid).Client_UserChangePairedPlayer(groupUserIdent, false).ConfigureAwait(false);
|
||||
await Clients.User(groupUserPair.GroupUserUID).Client_UserChangePairedPlayer(userIdent, false).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -17,12 +17,11 @@ namespace MareSynchronosServer.Hubs;
|
||||
public partial class MareHub
|
||||
{
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
[HubMethodName(Api.InvokeGroupCreate)]
|
||||
public async Task<GroupCreatedDto> CreateGroup()
|
||||
public async Task<GroupCreatedDto> GroupCreate()
|
||||
{
|
||||
_logger.LogCallInfo(Api.InvokeGroupCreate);
|
||||
var existingGroupsByUser = _dbContext.Groups.Count(u => u.OwnerUID == AuthenticatedUserId);
|
||||
var existingJoinedGroups = _dbContext.GroupPairs.Count(u => u.GroupUserUID == AuthenticatedUserId);
|
||||
_logger.LogCallInfo();
|
||||
var existingGroupsByUser = await _dbContext.Groups.CountAsync(u => u.OwnerUID == AuthenticatedUserId).ConfigureAwait(false);
|
||||
var existingJoinedGroups = await _dbContext.GroupPairs.CountAsync(u => u.GroupUserUID == AuthenticatedUserId).ConfigureAwait(false);
|
||||
if (existingGroupsByUser >= _maxExistingGroupsByUser || existingJoinedGroups >= _maxJoinedGroupsByUser)
|
||||
{
|
||||
throw new System.Exception($"Max groups for user is {_maxExistingGroupsByUser}, max joined groups is {_maxJoinedGroupsByUser}.");
|
||||
@@ -59,9 +58,9 @@ public partial class MareHub
|
||||
await _dbContext.GroupPairs.AddAsync(initialPair).ConfigureAwait(false);
|
||||
await _dbContext.SaveChangesAsync().ConfigureAwait(false);
|
||||
|
||||
var self = _dbContext.Users.Single(u => u.UID == AuthenticatedUserId);
|
||||
var self = await _dbContext.Users.SingleAsync(u => u.UID == AuthenticatedUserId).ConfigureAwait(false);
|
||||
|
||||
await Clients.User(AuthenticatedUserId).SendAsync(Api.OnGroupChange, new GroupDto()
|
||||
await Clients.User(AuthenticatedUserId).Client_GroupChange(new GroupDto()
|
||||
{
|
||||
GID = newGroup.GID,
|
||||
OwnedBy = string.IsNullOrEmpty(self.Alias) ? self.UID : self.Alias,
|
||||
@@ -70,7 +69,7 @@ public partial class MareHub
|
||||
InvitesEnabled = true
|
||||
}).ConfigureAwait(false);
|
||||
|
||||
_logger.LogCallInfo(Api.InvokeGroupCreate, gid);
|
||||
_logger.LogCallInfo(MareHubLogger.Args(gid));
|
||||
|
||||
return new GroupCreatedDto()
|
||||
{
|
||||
@@ -80,12 +79,11 @@ public partial class MareHub
|
||||
}
|
||||
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
[HubMethodName(Api.InvokeGroupGetGroups)]
|
||||
public async Task<List<GroupDto>> GetGroups()
|
||||
public async Task<List<GroupDto>> GroupsGetAll()
|
||||
{
|
||||
_logger.LogCallInfo(Api.InvokeGroupGetGroups);
|
||||
_logger.LogCallInfo();
|
||||
|
||||
var groups = await _dbContext.GroupPairs.Include(g => g.Group).Include(g => g.Group.Owner).Where(g => g.GroupUserUID == AuthenticatedUserId).ToListAsync().ConfigureAwait(false);
|
||||
var groups = await _dbContext.GroupPairs.Include(g => g.Group).Include(g => g.Group.Owner).Where(g => g.GroupUserUID == AuthenticatedUserId).AsNoTracking().ToListAsync().ConfigureAwait(false);
|
||||
|
||||
return groups.Select(g => new GroupDto()
|
||||
{
|
||||
@@ -99,15 +97,14 @@ public partial class MareHub
|
||||
}
|
||||
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
[HubMethodName(Api.InvokeGroupGetUsersInGroup)]
|
||||
public async Task<List<GroupPairDto>> GetUsersInGroup(string gid)
|
||||
public async Task<List<GroupPairDto>> GroupsGetUsersInGroup(string gid)
|
||||
{
|
||||
_logger.LogCallInfo(Api.InvokeGroupGetUsersInGroup, gid);
|
||||
_logger.LogCallInfo(MareHubLogger.Args(gid));
|
||||
|
||||
var (inGroup, _) = await TryValidateUserInGroup(gid).ConfigureAwait(false);
|
||||
if (!inGroup) return new List<GroupPairDto>();
|
||||
|
||||
var allPairs = await _dbContext.GroupPairs.Include(g => g.GroupUser).Where(g => g.GroupGID == gid && g.GroupUserUID != AuthenticatedUserId).ToListAsync().ConfigureAwait(false);
|
||||
var allPairs = await _dbContext.GroupPairs.Include(g => g.GroupUser).Where(g => g.GroupGID == gid && g.GroupUserUID != AuthenticatedUserId).AsNoTracking().ToListAsync().ConfigureAwait(false);
|
||||
return allPairs.Select(p => new GroupPairDto()
|
||||
{
|
||||
GroupGID = gid,
|
||||
@@ -121,10 +118,9 @@ public partial class MareHub
|
||||
}
|
||||
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
[HubMethodName(Api.SendGroupChangeInviteState)]
|
||||
public async Task GroupChangeInviteState(string gid, bool enabled)
|
||||
{
|
||||
_logger.LogCallInfo(Api.SendGroupChangeInviteState, gid, enabled.ToString());
|
||||
_logger.LogCallInfo(MareHubLogger.Args(gid, enabled.ToString()));
|
||||
|
||||
var (hasRights, group) = await TryValidateOwner(gid).ConfigureAwait(false);
|
||||
if (!hasRights) return;
|
||||
@@ -132,10 +128,10 @@ public partial class MareHub
|
||||
group.InvitesEnabled = enabled;
|
||||
await _dbContext.SaveChangesAsync().ConfigureAwait(false);
|
||||
|
||||
_logger.LogCallInfo(Api.SendGroupChangeInviteState, gid, enabled.ToString(), "Success");
|
||||
_logger.LogCallInfo(MareHubLogger.Args(gid, enabled.ToString(), "Success"));
|
||||
|
||||
var groupPairs = _dbContext.GroupPairs.Where(p => p.GroupGID == gid).Select(p => p.GroupUserUID).ToList();
|
||||
await Clients.Users(groupPairs).SendAsync(Api.OnGroupChange, new GroupDto()
|
||||
await Clients.Users(groupPairs).Client_GroupChange(new GroupDto()
|
||||
{
|
||||
GID = gid,
|
||||
InvitesEnabled = enabled,
|
||||
@@ -143,21 +139,20 @@ public partial class MareHub
|
||||
}
|
||||
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
[HubMethodName(Api.SendGroupDelete)]
|
||||
public async Task GroupDelete(string gid)
|
||||
{
|
||||
_logger.LogCallInfo(Api.SendGroupDelete, gid);
|
||||
_logger.LogCallInfo(MareHubLogger.Args(gid));
|
||||
|
||||
var (hasRights, group) = await TryValidateOwner(gid).ConfigureAwait(false);
|
||||
|
||||
_logger.LogCallInfo(Api.SendGroupDelete, gid, "Success");
|
||||
_logger.LogCallInfo(MareHubLogger.Args(gid, "Success"));
|
||||
|
||||
var groupPairs = await _dbContext.GroupPairs.Where(p => p.GroupGID == gid).ToListAsync().ConfigureAwait(false);
|
||||
_dbContext.RemoveRange(groupPairs);
|
||||
_dbContext.Remove(group);
|
||||
await _dbContext.SaveChangesAsync().ConfigureAwait(false);
|
||||
|
||||
await Clients.Users(groupPairs.Select(g => g.GroupUserUID)).SendAsync(Api.OnGroupChange, new GroupDto()
|
||||
await Clients.Users(groupPairs.Select(g => g.GroupUserUID)).Client_GroupChange(new GroupDto()
|
||||
{
|
||||
GID = group.GID,
|
||||
IsDeleted = true,
|
||||
@@ -168,15 +163,14 @@ public partial class MareHub
|
||||
}
|
||||
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
[HubMethodName(Api.InvokeGroupJoin)]
|
||||
public async Task<bool> GroupJoin(string gid, string password)
|
||||
{
|
||||
_logger.LogCallInfo(Api.InvokeGroupJoin, gid);
|
||||
_logger.LogCallInfo(MareHubLogger.Args(gid));
|
||||
|
||||
var group = await _dbContext.Groups.Include(g => g.Owner).SingleOrDefaultAsync(g => g.GID == gid || g.Alias == gid).ConfigureAwait(false);
|
||||
var existingPair = await _dbContext.GroupPairs.SingleOrDefaultAsync(g => g.GroupGID == gid && g.GroupUserUID == AuthenticatedUserId).ConfigureAwait(false);
|
||||
var group = await _dbContext.Groups.Include(g => g.Owner).AsNoTracking().SingleOrDefaultAsync(g => g.GID == gid || g.Alias == gid).ConfigureAwait(false);
|
||||
var existingPair = await _dbContext.GroupPairs.AsNoTracking().SingleOrDefaultAsync(g => g.GroupGID == gid && g.GroupUserUID == AuthenticatedUserId).ConfigureAwait(false);
|
||||
var hashedPw = StringUtils.Sha256String(password);
|
||||
var existingUserCount = await _dbContext.GroupPairs.CountAsync(g => g.GroupGID == gid).ConfigureAwait(false);
|
||||
var existingUserCount = await _dbContext.GroupPairs.AsNoTracking().CountAsync(g => g.GroupGID == gid).ConfigureAwait(false);
|
||||
var joinedGroups = await _dbContext.GroupPairs.CountAsync(g => g.GroupUserUID == AuthenticatedUserId).ConfigureAwait(false);
|
||||
var isBanned = await _dbContext.GroupBans.AnyAsync(g => g.GroupGID == gid && g.BannedUserUID == AuthenticatedUserId).ConfigureAwait(false);
|
||||
|
||||
@@ -198,9 +192,9 @@ public partial class MareHub
|
||||
await _dbContext.GroupPairs.AddAsync(newPair).ConfigureAwait(false);
|
||||
await _dbContext.SaveChangesAsync().ConfigureAwait(false);
|
||||
|
||||
_logger.LogCallInfo(Api.InvokeGroupJoin, gid, "Success");
|
||||
_logger.LogCallInfo(MareHubLogger.Args(gid, "Success"));
|
||||
|
||||
await Clients.User(AuthenticatedUserId).SendAsync(Api.OnGroupChange, new GroupDto()
|
||||
await Clients.User(AuthenticatedUserId).Client_GroupChange(new GroupDto()
|
||||
{
|
||||
GID = group.GID,
|
||||
OwnedBy = string.IsNullOrEmpty(group.Owner.Alias) ? group.Owner.UID : group.Owner.Alias,
|
||||
@@ -213,7 +207,7 @@ public partial class MareHub
|
||||
var self = _dbContext.Users.Single(u => u.UID == AuthenticatedUserId);
|
||||
|
||||
var groupPairs = await _dbContext.GroupPairs.Where(p => p.GroupGID == group.GID && p.GroupUserUID != AuthenticatedUserId).ToListAsync().ConfigureAwait(false);
|
||||
await Clients.Users(groupPairs.Select(p => p.GroupUserUID)).SendAsync(Api.OnGroupUserChange, new GroupPairDto()
|
||||
await Clients.Users(groupPairs.Select(p => p.GroupUserUID)).Client_GroupUserChange(new GroupPairDto()
|
||||
{
|
||||
GroupGID = group.GID,
|
||||
IsPaused = false,
|
||||
@@ -237,8 +231,8 @@ public partial class MareHub
|
||||
var groupUserIdent = await _clientIdentService.GetCharacterIdentForUid(groupUserPair.GroupUserUID).ConfigureAwait(false);
|
||||
if (!string.IsNullOrEmpty(groupUserIdent))
|
||||
{
|
||||
await Clients.User(AuthenticatedUserId).SendAsync(Api.OnUserAddOnlinePairedPlayer, groupUserIdent).ConfigureAwait(false);
|
||||
await Clients.User(groupUserPair.GroupUserUID).SendAsync(Api.OnUserAddOnlinePairedPlayer, userIdent).ConfigureAwait(false);
|
||||
await Clients.User(AuthenticatedUserId).Client_UserChangePairedPlayer(groupUserIdent, true).ConfigureAwait(false);
|
||||
await Clients.User(groupUserPair.GroupUserUID).Client_UserChangePairedPlayer(userIdent, true).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -246,10 +240,9 @@ public partial class MareHub
|
||||
}
|
||||
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
[HubMethodName(Api.SendGroupLeave)]
|
||||
public async Task GroupLeave(string gid)
|
||||
{
|
||||
_logger.LogCallInfo(Api.SendGroupLeave, gid);
|
||||
_logger.LogCallInfo(MareHubLogger.Args(gid));
|
||||
|
||||
var (exists, groupPair) = await TryValidateUserInGroup(gid).ConfigureAwait(false);
|
||||
if (!exists) return;
|
||||
@@ -262,7 +255,7 @@ public partial class MareHub
|
||||
_dbContext.GroupPairs.Remove(groupPair);
|
||||
await _dbContext.SaveChangesAsync().ConfigureAwait(false);
|
||||
|
||||
await Clients.User(AuthenticatedUserId).SendAsync(Api.OnGroupChange, new GroupDto()
|
||||
await Clients.User(AuthenticatedUserId).Client_GroupChange(new GroupDto()
|
||||
{
|
||||
GID = group.GID,
|
||||
IsDeleted = true
|
||||
@@ -273,7 +266,7 @@ public partial class MareHub
|
||||
{
|
||||
if (!groupPairsWithoutSelf.Any())
|
||||
{
|
||||
_logger.LogCallInfo(Api.SendGroupLeave, gid, "Deleted");
|
||||
_logger.LogCallInfo(MareHubLogger.Args(gid, "Deleted"));
|
||||
|
||||
_dbContext.Remove(group);
|
||||
}
|
||||
@@ -283,9 +276,9 @@ public partial class MareHub
|
||||
|
||||
if (groupHasMigrated.Item1)
|
||||
{
|
||||
_logger.LogCallInfo(Api.SendGroupLeave, gid, "Migrated", groupHasMigrated.Item2);
|
||||
_logger.LogCallInfo(MareHubLogger.Args(gid, "Migrated", groupHasMigrated.Item2));
|
||||
|
||||
await Clients.Users(groupPairsWithoutSelf.Select(p => p.GroupUserUID)).SendAsync(Api.OnGroupChange, new GroupDto()
|
||||
await Clients.Users(groupPairsWithoutSelf.Select(p => p.GroupUserUID)).Client_GroupChange(new GroupDto()
|
||||
{
|
||||
GID = group.GID,
|
||||
OwnedBy = groupHasMigrated.Item2,
|
||||
@@ -294,9 +287,9 @@ public partial class MareHub
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogCallInfo(Api.SendGroupLeave, gid, "Deleted");
|
||||
_logger.LogCallInfo(MareHubLogger.Args(gid, "Deleted"));
|
||||
|
||||
await Clients.Users(groupPairsWithoutSelf.Select(p => p.GroupUserUID)).SendAsync(Api.OnGroupChange, new GroupDto()
|
||||
await Clients.Users(groupPairsWithoutSelf.Select(p => p.GroupUserUID)).Client_GroupChange(new GroupDto()
|
||||
{
|
||||
GID = group.GID,
|
||||
IsDeleted = true
|
||||
@@ -311,9 +304,9 @@ public partial class MareHub
|
||||
|
||||
await _dbContext.SaveChangesAsync().ConfigureAwait(false);
|
||||
|
||||
_logger.LogCallInfo(Api.SendGroupLeave, gid, "Success");
|
||||
_logger.LogCallInfo(MareHubLogger.Args(gid, "Success"));
|
||||
|
||||
await Clients.Users(groupPairsWithoutSelf.Select(p => p.GroupUserUID)).SendAsync(Api.OnGroupUserChange, new GroupPairDto()
|
||||
await Clients.Users(groupPairsWithoutSelf.Select(p => p.GroupUserUID)).Client_GroupUserChange(new GroupPairDto()
|
||||
{
|
||||
GroupGID = group.GID,
|
||||
IsRemoved = true,
|
||||
@@ -330,10 +323,9 @@ public partial class MareHub
|
||||
}
|
||||
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
[HubMethodName(Api.SendGroupPause)]
|
||||
public async Task GroupChangePauseState(string gid, bool isPaused)
|
||||
{
|
||||
_logger.LogCallInfo(Api.SendGroupPause, gid, isPaused);
|
||||
_logger.LogCallInfo(MareHubLogger.Args(gid, isPaused));
|
||||
|
||||
var (exists, groupPair) = await TryValidateUserInGroup(gid).ConfigureAwait(false);
|
||||
if (!exists) return;
|
||||
@@ -341,17 +333,17 @@ public partial class MareHub
|
||||
groupPair.IsPaused = isPaused;
|
||||
await _dbContext.SaveChangesAsync().ConfigureAwait(false);
|
||||
|
||||
_logger.LogCallInfo(Api.SendGroupPause, gid, isPaused, "Success");
|
||||
_logger.LogCallInfo(MareHubLogger.Args(gid, isPaused, "Success"));
|
||||
|
||||
var groupPairs = await _dbContext.GroupPairs.Where(p => p.GroupGID == gid && p.GroupUserUID != AuthenticatedUserId).ToListAsync().ConfigureAwait(false);
|
||||
await Clients.Users(groupPairs.Select(p => p.GroupUserUID)).SendAsync(Api.OnGroupUserChange, new GroupPairDto()
|
||||
var groupPairs = await _dbContext.GroupPairs.Where(p => p.GroupGID == gid && p.GroupUserUID != AuthenticatedUserId).AsNoTracking().ToListAsync().ConfigureAwait(false);
|
||||
await Clients.Users(groupPairs.Select(p => p.GroupUserUID)).Client_GroupUserChange(new GroupPairDto()
|
||||
{
|
||||
GroupGID = gid,
|
||||
IsPaused = isPaused,
|
||||
UserUID = AuthenticatedUserId,
|
||||
}).ConfigureAwait(false);
|
||||
|
||||
await Clients.User(AuthenticatedUserId).SendAsync(Api.OnGroupChange, new GroupDto
|
||||
await Clients.User(AuthenticatedUserId).Client_GroupChange(new GroupDto
|
||||
{
|
||||
GID = gid,
|
||||
IsPaused = isPaused
|
||||
@@ -372,17 +364,16 @@ public partial class MareHub
|
||||
var groupUserIdent = await _clientIdentService.GetCharacterIdentForUid(groupUserPair.GroupUserUID).ConfigureAwait(false);
|
||||
if (!string.IsNullOrEmpty(groupUserIdent))
|
||||
{
|
||||
await Clients.User(AuthenticatedUserId).SendAsync(isPaused ? Api.OnUserRemoveOnlinePairedPlayer : Api.OnUserAddOnlinePairedPlayer, groupUserIdent).ConfigureAwait(false);
|
||||
await Clients.User(groupUserPair.GroupUserUID).SendAsync(isPaused ? Api.OnUserRemoveOnlinePairedPlayer : Api.OnUserAddOnlinePairedPlayer, userIdent).ConfigureAwait(false);
|
||||
await Clients.User(AuthenticatedUserId).Client_UserChangePairedPlayer(groupUserIdent, !isPaused).ConfigureAwait(false);
|
||||
await Clients.User(groupUserPair.GroupUserUID).Client_UserChangePairedPlayer(userIdent, !isPaused).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
[HubMethodName(Api.SendGroupRemoveUser)]
|
||||
public async Task GroupRemoveUser(string gid, string uid)
|
||||
{
|
||||
_logger.LogCallInfo(Api.SendGroupRemoveUser, gid, uid);
|
||||
_logger.LogCallInfo(MareHubLogger.Args(gid, uid));
|
||||
|
||||
var (hasRights, group) = await TryValidateGroupModeratorOrOwner(gid).ConfigureAwait(false);
|
||||
if (!hasRights) return;
|
||||
@@ -391,14 +382,13 @@ public partial class MareHub
|
||||
if (!userExists) return;
|
||||
|
||||
if (groupPair.IsModerator || string.Equals(group.OwnerUID, uid, StringComparison.Ordinal)) return;
|
||||
|
||||
_logger.LogCallInfo(Api.SendGroupRemoveUser, gid, uid, "Success");
|
||||
_logger.LogCallInfo(MareHubLogger.Args(gid, uid, "Success"));
|
||||
|
||||
_dbContext.GroupPairs.Remove(groupPair);
|
||||
await _dbContext.SaveChangesAsync().ConfigureAwait(false);
|
||||
|
||||
var groupPairs = _dbContext.GroupPairs.Where(p => p.GroupGID == group.GID).ToList();
|
||||
await Clients.Users(groupPairs.Select(p => p.GroupUserUID)).SendAsync(Api.OnGroupUserChange, new GroupPairDto()
|
||||
var groupPairs = _dbContext.GroupPairs.Where(p => p.GroupGID == group.GID).AsNoTracking().ToList();
|
||||
await Clients.Users(groupPairs.Select(p => p.GroupUserUID)).Client_GroupUserChange(new GroupPairDto()
|
||||
{
|
||||
GroupGID = group.GID,
|
||||
IsRemoved = true,
|
||||
@@ -408,7 +398,7 @@ public partial class MareHub
|
||||
var userIdent = await _clientIdentService.GetCharacterIdentForUid(uid).ConfigureAwait(false);
|
||||
if (userIdent == null) return;
|
||||
|
||||
await Clients.User(uid).SendAsync(Api.OnGroupChange, new GroupDto()
|
||||
await Clients.User(uid).Client_GroupChange(new GroupDto()
|
||||
{
|
||||
GID = gid,
|
||||
IsDeleted = true,
|
||||
@@ -423,10 +413,9 @@ public partial class MareHub
|
||||
}
|
||||
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
[HubMethodName(Api.SendBanUserFromGroup)]
|
||||
public async Task GroupBanUser(string gid, string uid, string reason)
|
||||
{
|
||||
_logger.LogCallInfo(Api.SendBanUserFromGroup, gid, uid);
|
||||
_logger.LogCallInfo(MareHubLogger.Args(gid, uid));
|
||||
|
||||
var (userHasRights, group) = await TryValidateGroupModeratorOrOwner(gid).ConfigureAwait(false);
|
||||
if (!userHasRights) return;
|
||||
@@ -451,14 +440,13 @@ public partial class MareHub
|
||||
|
||||
await GroupRemoveUser(gid, uid).ConfigureAwait(false);
|
||||
|
||||
_logger.LogCallInfo(Api.SendBanUserFromGroup, gid, uid, "Success");
|
||||
_logger.LogCallInfo(MareHubLogger.Args(gid, uid, "Success"));
|
||||
}
|
||||
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
[HubMethodName(Api.SendUnbanUserFromGroup)]
|
||||
public async Task GroupUnbanUser(string gid, string uid)
|
||||
{
|
||||
_logger.LogCallInfo(Api.SendUnbanUserFromGroup, gid, uid);
|
||||
_logger.LogCallInfo(MareHubLogger.Args(gid, uid));
|
||||
|
||||
var (userHasRights, _) = await TryValidateGroupModeratorOrOwner(gid).ConfigureAwait(false);
|
||||
if (!userHasRights) return;
|
||||
@@ -469,19 +457,18 @@ public partial class MareHub
|
||||
_dbContext.Remove(banEntry);
|
||||
await _dbContext.SaveChangesAsync().ConfigureAwait(false);
|
||||
|
||||
_logger.LogCallInfo(Api.SendUnbanUserFromGroup, gid, uid, "Success");
|
||||
_logger.LogCallInfo(MareHubLogger.Args(gid, uid, "Success"));
|
||||
}
|
||||
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
[HubMethodName(Api.InvokeGetBannedUsersForGroup)]
|
||||
public async Task<List<BannedGroupUserDto>> GetGroupBannedUsers(string gid)
|
||||
public async Task<List<BannedGroupUserDto>> GroupGetBannedUsers(string gid)
|
||||
{
|
||||
_logger.LogCallInfo(Api.InvokeGetBannedUsersForGroup, gid);
|
||||
_logger.LogCallInfo(MareHubLogger.Args(gid));
|
||||
|
||||
var (userHasRights, _) = await TryValidateGroupModeratorOrOwner(gid).ConfigureAwait(false);
|
||||
if (!userHasRights) return new List<BannedGroupUserDto>();
|
||||
|
||||
var banEntries = await _dbContext.GroupBans.Where(g => g.GroupGID == gid).ToListAsync().ConfigureAwait(false);
|
||||
var banEntries = await _dbContext.GroupBans.Where(g => g.GroupGID == gid).AsNoTracking().ToListAsync().ConfigureAwait(false);
|
||||
|
||||
List<BannedGroupUserDto> bannedGroupUsers = banEntries.Select(b => new BannedGroupUserDto()
|
||||
{
|
||||
@@ -492,16 +479,15 @@ public partial class MareHub
|
||||
|
||||
}).ToList();
|
||||
|
||||
_logger.LogCallInfo(Api.InvokeGetBannedUsersForGroup, gid, bannedGroupUsers.Count);
|
||||
_logger.LogCallInfo(MareHubLogger.Args(gid, bannedGroupUsers.Count));
|
||||
|
||||
return bannedGroupUsers;
|
||||
}
|
||||
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
[HubMethodName(Api.SendGroupSetModerator)]
|
||||
public async Task SetModerator(string gid, string uid, bool isGroupModerator)
|
||||
public async Task GroupSetModerator(string gid, string uid, bool isGroupModerator)
|
||||
{
|
||||
_logger.LogCallInfo(Api.SendGroupSetModerator, gid, uid, isGroupModerator);
|
||||
_logger.LogCallInfo(MareHubLogger.Args(gid, uid, isGroupModerator));
|
||||
|
||||
var (userHasRights, _) = await TryValidateOwner(gid).ConfigureAwait(false);
|
||||
if (!userHasRights) return;
|
||||
@@ -512,30 +498,29 @@ public partial class MareHub
|
||||
userPair.IsModerator = isGroupModerator;
|
||||
await _dbContext.SaveChangesAsync().ConfigureAwait(false);
|
||||
|
||||
var groupPairs = await _dbContext.GroupPairs.Where(g => g.GroupGID == gid).ToListAsync().ConfigureAwait(false);
|
||||
var groupPairs = await _dbContext.GroupPairs.Where(g => g.GroupGID == gid).AsNoTracking().ToListAsync().ConfigureAwait(false);
|
||||
|
||||
await Clients.User(uid).SendAsync(Api.OnGroupChange, new GroupDto()
|
||||
await Clients.User(uid).Client_GroupChange(new GroupDto()
|
||||
{
|
||||
GID = gid,
|
||||
IsModerator = isGroupModerator
|
||||
}).ConfigureAwait(false);
|
||||
|
||||
await Clients.Users(groupPairs.Where(p => !string.Equals(p.GroupUserUID, uid, StringComparison.Ordinal))
|
||||
.Select(g => g.GroupUserUID)).SendAsync(Api.OnGroupUserChange, new GroupPairDto()
|
||||
.Select(g => g.GroupUserUID)).Client_GroupUserChange(new GroupPairDto()
|
||||
{
|
||||
GroupGID = gid,
|
||||
IsModerator = isGroupModerator,
|
||||
UserUID = uid
|
||||
}).ConfigureAwait(false);
|
||||
|
||||
_logger.LogCallInfo(Api.SendGroupSetModerator, gid, uid, isGroupModerator, "Success");
|
||||
_logger.LogCallInfo(MareHubLogger.Args(gid, uid, isGroupModerator, "Success"));
|
||||
}
|
||||
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
[HubMethodName(Api.SendGroupChangeOwner)]
|
||||
public async Task ChangeOwnership(string gid, string uid)
|
||||
public async Task GroupChangeOwnership(string gid, string uid)
|
||||
{
|
||||
_logger.LogCallInfo(Api.SendGroupChangeOwner, gid, uid);
|
||||
_logger.LogCallInfo(MareHubLogger.Args(gid, uid));
|
||||
|
||||
var (isOwner, group) = await TryValidateOwner(gid).ConfigureAwait(false);
|
||||
if (!isOwner) return;
|
||||
@@ -554,25 +539,26 @@ public partial class MareHub
|
||||
newOwnerPair.IsModerator = false;
|
||||
await _dbContext.SaveChangesAsync().ConfigureAwait(false);
|
||||
|
||||
_logger.LogCallInfo(Api.SendGroupChangeOwner, gid, uid, "Success");
|
||||
_logger.LogCallInfo(MareHubLogger.Args(gid, uid, "Success"));
|
||||
|
||||
var groupPairs = await _dbContext.GroupPairs.Where(p => p.GroupGID == gid).Select(p => p.GroupUserUID).ToListAsync().ConfigureAwait(false);
|
||||
var groupPairs = await _dbContext.GroupPairs.Where(p => p.GroupGID == gid).Select(p => p.GroupUserUID).AsNoTracking().ToListAsync().ConfigureAwait(false);
|
||||
|
||||
await Clients.Users(uid).SendAsync(Api.OnGroupChange, new GroupDto()
|
||||
await Clients.Users(uid).Client_GroupChange(new GroupDto()
|
||||
{
|
||||
GID = gid,
|
||||
OwnedBy = string.IsNullOrEmpty(group.Owner.Alias) ? group.Owner.UID : group.Owner.Alias,
|
||||
IsModerator = false
|
||||
IsModerator = false,
|
||||
Alias = null
|
||||
}).ConfigureAwait(false);
|
||||
|
||||
await Clients.Users(groupPairs).SendAsync(Api.OnGroupChange, new GroupDto()
|
||||
await Clients.Users(groupPairs).Client_GroupChange(new GroupDto()
|
||||
{
|
||||
GID = gid,
|
||||
OwnedBy = string.IsNullOrEmpty(group.Owner.Alias) ? group.Owner.UID : group.Owner.Alias,
|
||||
Alias = null
|
||||
}).ConfigureAwait(false);
|
||||
|
||||
await Clients.Users(groupPairs.Where(p => !string.Equals(p, uid, StringComparison.Ordinal))).SendAsync(Api.OnGroupUserChange, new GroupPairDto()
|
||||
await Clients.Users(groupPairs.Where(p => !string.Equals(p, uid, StringComparison.Ordinal))).Client_GroupUserChange(new GroupPairDto()
|
||||
{
|
||||
GroupGID = gid,
|
||||
UserUID = uid,
|
||||
@@ -582,15 +568,14 @@ public partial class MareHub
|
||||
}
|
||||
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
[HubMethodName(Api.InvokeGroupChangePassword)]
|
||||
public async Task<bool> ChangeGroupPassword(string gid, string password)
|
||||
public async Task<bool> GroupChangePassword(string gid, string password)
|
||||
{
|
||||
_logger.LogCallInfo(Api.InvokeGroupChangePassword, gid);
|
||||
_logger.LogCallInfo(MareHubLogger.Args(gid));
|
||||
|
||||
var (isOwner, group) = await TryValidateOwner(gid).ConfigureAwait(false);
|
||||
if (!isOwner || password.Length < 10) return false;
|
||||
|
||||
_logger.LogCallInfo(Api.InvokeGroupChangePassword, gid, "Success");
|
||||
_logger.LogCallInfo(MareHubLogger.Args(gid, "Success"));
|
||||
|
||||
group.HashedPassword = StringUtils.Sha256String(password);
|
||||
await _dbContext.SaveChangesAsync().ConfigureAwait(false);
|
||||
@@ -599,10 +584,9 @@ public partial class MareHub
|
||||
}
|
||||
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
[HubMethodName(Api.SendGroupChangePinned)]
|
||||
public async Task ChangePinned(string gid, string uid, bool isPinned)
|
||||
public async Task GroupChangePinned(string gid, string uid, bool isPinned)
|
||||
{
|
||||
_logger.LogCallInfo(Api.SendGroupChangePinned, gid, uid, isPinned);
|
||||
_logger.LogCallInfo(MareHubLogger.Args(gid, uid, isPinned));
|
||||
|
||||
var (userHasRights, _) = await TryValidateGroupModeratorOrOwner(gid).ConfigureAwait(false);
|
||||
if (!userHasRights) return;
|
||||
@@ -613,11 +597,11 @@ public partial class MareHub
|
||||
groupPair.IsPinned = isPinned;
|
||||
await _dbContext.SaveChangesAsync().ConfigureAwait(false);
|
||||
|
||||
_logger.LogCallInfo(Api.InvokeGroupChangePassword, gid, uid, isPinned, "Success");
|
||||
_logger.LogCallInfo(MareHubLogger.Args(gid, uid, isPinned, "Success"));
|
||||
|
||||
var groupPairs = await _dbContext.GroupPairs.Where(p => p.GroupGID == gid).Select(p => p.GroupUserUID).ToListAsync().ConfigureAwait(false);
|
||||
var groupPairs = await _dbContext.GroupPairs.Where(p => p.GroupGID == gid).Select(p => p.GroupUserUID).AsNoTracking().ToListAsync().ConfigureAwait(false);
|
||||
|
||||
await Clients.Users(groupPairs.Where(p => !string.Equals(p, uid, StringComparison.Ordinal))).SendAsync(Api.OnGroupUserChange, new GroupPairDto()
|
||||
await Clients.Users(groupPairs.Where(p => !string.Equals(p, uid, StringComparison.Ordinal))).Client_GroupUserChange(new GroupPairDto()
|
||||
{
|
||||
GroupGID = gid,
|
||||
UserUID = uid,
|
||||
@@ -626,23 +610,22 @@ public partial class MareHub
|
||||
}
|
||||
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
[HubMethodName(Api.SendGroupClear)]
|
||||
public async Task ClearGroup(string gid)
|
||||
public async Task GroupClear(string gid)
|
||||
{
|
||||
_logger.LogCallInfo(Api.SendGroupClear, gid);
|
||||
_logger.LogCallInfo(MareHubLogger.Args(gid));
|
||||
|
||||
var (hasRights, group) = await TryValidateGroupModeratorOrOwner(gid).ConfigureAwait(false);
|
||||
if (!hasRights) return;
|
||||
|
||||
var groupPairs = await _dbContext.GroupPairs.Where(p => p.GroupGID == gid).ToListAsync().ConfigureAwait(false);
|
||||
|
||||
await Clients.Users(groupPairs.Where(p => !p.IsPinned && !p.IsModerator).Select(g => g.GroupUserUID)).SendAsync(Api.OnGroupChange, new GroupDto()
|
||||
await Clients.Users(groupPairs.Where(p => !p.IsPinned && !p.IsModerator).Select(g => g.GroupUserUID)).Client_GroupChange(new GroupDto()
|
||||
{
|
||||
GID = group.GID,
|
||||
IsDeleted = true,
|
||||
}).ConfigureAwait(false);
|
||||
|
||||
_logger.LogCallInfo(Api.SendGroupClear, gid, "Success");
|
||||
_logger.LogCallInfo(MareHubLogger.Args(gid, "Success"));
|
||||
|
||||
var notPinned = groupPairs.Where(g => !g.IsPinned).ToList();
|
||||
|
||||
@@ -651,7 +634,7 @@ public partial class MareHub
|
||||
|
||||
foreach (var pair in notPinned)
|
||||
{
|
||||
await Clients.Users(groupPairs.Where(p => p.IsPinned).Select(g => g.GroupUserUID)).SendAsync(Api.OnGroupUserChange, new GroupPairDto()
|
||||
await Clients.Users(groupPairs.Where(p => p.IsPinned).Select(g => g.GroupUserUID)).Client_GroupUserChange(new GroupPairDto()
|
||||
{
|
||||
GroupGID = pair.GroupGID,
|
||||
IsRemoved = true,
|
||||
|
||||
@@ -10,17 +10,15 @@ using MareSynchronosShared.Protos;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace MareSynchronosServer.Hubs;
|
||||
|
||||
public partial class MareHub
|
||||
{
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
[HubMethodName(Api.SendUserDeleteAccount)]
|
||||
public async Task DeleteAccount()
|
||||
public async Task UserDelete()
|
||||
{
|
||||
_logger.LogCallInfo(Api.SendUserDeleteAccount);
|
||||
_logger.LogCallInfo();
|
||||
|
||||
string userid = AuthenticatedUserId;
|
||||
var userEntry = await _dbContext.Users.SingleAsync(u => u.UID == userid).ConfigureAwait(false);
|
||||
@@ -45,15 +43,14 @@ public partial class MareHub
|
||||
_dbContext.RemoveRange(ownPairData);
|
||||
await _dbContext.SaveChangesAsync().ConfigureAwait(false);
|
||||
var otherPairData = await _dbContext.ClientPairs.Include(u => u.User)
|
||||
.Where(u => u.OtherUser.UID == userid).ToListAsync().ConfigureAwait(false);
|
||||
.Where(u => u.OtherUser.UID == userid).AsNoTracking().ToListAsync().ConfigureAwait(false);
|
||||
foreach (var pair in otherPairData)
|
||||
{
|
||||
await Clients.User(pair.User.UID)
|
||||
.SendAsync(Api.OnUserUpdateClientPairs, new ClientPairDto()
|
||||
{
|
||||
OtherUID = userid,
|
||||
IsRemoved = true
|
||||
}, charaIdent).ConfigureAwait(false);
|
||||
await Clients.User(pair.User.UID).Client_UserUpdateClientPairs(new ClientPairDto()
|
||||
{
|
||||
OtherUID = userid,
|
||||
IsRemoved = true
|
||||
}).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
foreach (var pair in groupPairs)
|
||||
@@ -70,22 +67,20 @@ public partial class MareHub
|
||||
}
|
||||
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
[HubMethodName(Api.InvokeUserGetOnlineCharacters)]
|
||||
public async Task<List<string>> GetOnlineCharacters()
|
||||
public async Task<List<string>> UserGetOnlineCharacters()
|
||||
{
|
||||
_logger.LogCallInfo(Api.InvokeUserGetOnlineCharacters);
|
||||
_logger.LogCallInfo();
|
||||
|
||||
var ownIdent = await _clientIdentService.GetCharacterIdentForUid(AuthenticatedUserId).ConfigureAwait(false);
|
||||
|
||||
var usersToSendOnlineTo = await SendDataToAllPairedUsers(Api.OnUserAddOnlinePairedPlayer, ownIdent).ConfigureAwait(false);
|
||||
var usersToSendOnlineTo = await SendOnlineToAllPairedUsers(ownIdent).ConfigureAwait(false);
|
||||
return usersToSendOnlineTo.Select(async e => await _clientIdentService.GetCharacterIdentForUid(e).ConfigureAwait(false)).Select(t => t.Result).Where(t => !string.IsNullOrEmpty(t)).Distinct(System.StringComparer.Ordinal).ToList();
|
||||
}
|
||||
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
[HubMethodName(Api.InvokeUserGetPairedClients)]
|
||||
public async Task<List<ClientPairDto>> GetPairedClients()
|
||||
public async Task<List<ClientPairDto>> UserGetPairedClients()
|
||||
{
|
||||
_logger.LogCallInfo(Api.InvokeUserGetPairedClients);
|
||||
_logger.LogCallInfo();
|
||||
|
||||
string userid = AuthenticatedUserId;
|
||||
var query =
|
||||
@@ -113,7 +108,7 @@ public partial class MareHub
|
||||
IsSynced = otherEntry != null
|
||||
};
|
||||
|
||||
return (await query.ToListAsync().ConfigureAwait(false)).Select(f => new ClientPairDto()
|
||||
return (await query.AsNoTracking().ToListAsync().ConfigureAwait(false)).Select(f => new ClientPairDto()
|
||||
{
|
||||
VanityUID = f.Alias,
|
||||
IsPaused = f.IsPaused,
|
||||
@@ -124,10 +119,9 @@ public partial class MareHub
|
||||
}
|
||||
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
[HubMethodName(Api.InvokeUserPushCharacterDataToVisibleClients)]
|
||||
public async Task PushCharacterDataToVisibleClients(CharacterCacheDto characterCache, List<string> visibleCharacterIds)
|
||||
public async Task UserPushData(CharacterCacheDto characterCache, List<string> visibleCharacterIds)
|
||||
{
|
||||
_logger.LogCallInfo(Api.InvokeUserPushCharacterDataToVisibleClients, visibleCharacterIds.Count);
|
||||
_logger.LogCallInfo(MareHubLogger.Args(visibleCharacterIds.Count));
|
||||
|
||||
var allPairedUsers = await GetAllPairedUnpausedUsers().ConfigureAwait(false);
|
||||
|
||||
@@ -136,19 +130,18 @@ public partial class MareHub
|
||||
|
||||
var ownIdent = await _clientIdentService.GetCharacterIdentForUid(AuthenticatedUserId).ConfigureAwait(false);
|
||||
|
||||
_logger.LogCallInfo(Api.InvokeUserPushCharacterDataToVisibleClients, visibleCharacterIds.Count, allPairedUsersDict.Count());
|
||||
_logger.LogCallInfo(MareHubLogger.Args(visibleCharacterIds.Count, allPairedUsersDict.Count()));
|
||||
|
||||
await Clients.Users(allPairedUsersDict.Select(f => f.Key)).SendAsync(Api.OnUserReceiveCharacterData, characterCache, ownIdent).ConfigureAwait(false);
|
||||
await Clients.Users(allPairedUsersDict.Select(f => f.Key)).Client_UserReceiveCharacterData(characterCache, ownIdent).ConfigureAwait(false);
|
||||
|
||||
_mareMetrics.IncCounter(MetricsAPI.CounterUserPushData);
|
||||
_mareMetrics.IncCounter(MetricsAPI.CounterUserPushDataTo, allPairedUsersDict.Count());
|
||||
}
|
||||
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
[HubMethodName(Api.SendUserPairedClientAddition)]
|
||||
public async Task SendPairedClientAddition(string uid)
|
||||
public async Task UserAddPair(string uid)
|
||||
{
|
||||
_logger.LogCallInfo(Api.SendUserPairedClientAddition, uid);
|
||||
_logger.LogCallInfo(MareHubLogger.Args(uid));
|
||||
|
||||
// don't allow adding yourself or nothing
|
||||
uid = uid.Trim();
|
||||
@@ -165,7 +158,7 @@ public partial class MareHub
|
||||
// grab self create new client pair and save
|
||||
var user = await _dbContext.Users.SingleAsync(u => u.UID == AuthenticatedUserId).ConfigureAwait(false);
|
||||
|
||||
_logger.LogCallInfo(Api.SendUserPairedClientAddition, uid, "Success");
|
||||
_logger.LogCallInfo(MareHubLogger.Args(uid, "Success"));
|
||||
|
||||
ClientPair wl = new ClientPair()
|
||||
{
|
||||
@@ -178,8 +171,8 @@ public partial class MareHub
|
||||
|
||||
// get the opposite entry of the client pair
|
||||
var otherEntry = OppositeEntry(otherUser.UID);
|
||||
await Clients.User(user.UID)
|
||||
.SendAsync(Api.OnUserUpdateClientPairs, new ClientPairDto()
|
||||
await Clients.User(user.UID).Client_UserUpdateClientPairs(
|
||||
new ClientPairDto()
|
||||
{
|
||||
VanityUID = otherUser.Alias,
|
||||
OtherUID = otherUser.UID,
|
||||
@@ -196,7 +189,7 @@ public partial class MareHub
|
||||
if (otherIdent == null) return;
|
||||
|
||||
// send push with update to other user if other user is online
|
||||
await Clients.User(otherUser.UID).SendAsync(Api.OnUserUpdateClientPairs,
|
||||
await Clients.User(otherUser.UID).Client_UserUpdateClientPairs(
|
||||
new ClientPairDto()
|
||||
{
|
||||
VanityUID = user.Alias,
|
||||
@@ -213,18 +206,15 @@ public partial class MareHub
|
||||
// if the other user has paused the main user and there was no previous group connection don't send anything
|
||||
if (!otherEntry.IsPaused && allUserPairs.Any(p => string.Equals(p.UID, uid, System.StringComparison.Ordinal) && p.IsPausedPerGroup is PauseInfo.Paused or PauseInfo.NoConnection))
|
||||
{
|
||||
await Clients.User(user.UID)
|
||||
.SendAsync(Api.OnUserAddOnlinePairedPlayer, otherIdent).ConfigureAwait(false);
|
||||
await Clients.User(otherUser.UID)
|
||||
.SendAsync(Api.OnUserAddOnlinePairedPlayer, userIdent).ConfigureAwait(false);
|
||||
await Clients.User(user.UID).Client_UserChangePairedPlayer(otherIdent, true).ConfigureAwait(false);
|
||||
await Clients.User(otherUser.UID).Client_UserChangePairedPlayer(userIdent, true).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
[HubMethodName(Api.SendUserPairedClientPauseChange)]
|
||||
public async Task SendPairedClientPauseChange(string otherUserUid, bool isPaused)
|
||||
public async Task UserChangePairPauseStatus(string otherUserUid, bool isPaused)
|
||||
{
|
||||
_logger.LogCallInfo(Api.SendUserPairedClientPauseChange, otherUserUid, isPaused);
|
||||
_logger.LogCallInfo(MareHubLogger.Args(otherUserUid, isPaused));
|
||||
|
||||
if (string.Equals(otherUserUid, AuthenticatedUserId, System.StringComparison.Ordinal)) return;
|
||||
ClientPair pair = await _dbContext.ClientPairs.SingleOrDefaultAsync(w => w.UserUID == AuthenticatedUserId && w.OtherUserUID == otherUserUid).ConfigureAwait(false);
|
||||
@@ -234,12 +224,12 @@ public partial class MareHub
|
||||
_dbContext.Update(pair);
|
||||
await _dbContext.SaveChangesAsync().ConfigureAwait(false);
|
||||
|
||||
_logger.LogCallInfo(Api.SendUserPairedClientPauseChange, otherUserUid, isPaused, "Success");
|
||||
_logger.LogCallInfo(MareHubLogger.Args(otherUserUid, isPaused, "Success"));
|
||||
|
||||
var otherEntry = OppositeEntry(otherUserUid);
|
||||
|
||||
await Clients.User(AuthenticatedUserId)
|
||||
.SendAsync(Api.OnUserUpdateClientPairs, new ClientPairDto()
|
||||
await Clients.User(AuthenticatedUserId).Client_UserUpdateClientPairs(
|
||||
new ClientPairDto()
|
||||
{
|
||||
OtherUID = otherUserUid,
|
||||
IsPaused = isPaused,
|
||||
@@ -248,7 +238,7 @@ public partial class MareHub
|
||||
}).ConfigureAwait(false);
|
||||
if (otherEntry != null)
|
||||
{
|
||||
await Clients.User(otherUserUid).SendAsync(Api.OnUserUpdateClientPairs, new ClientPairDto()
|
||||
await Clients.User(otherUserUid).Client_UserUpdateClientPairs(new ClientPairDto()
|
||||
{
|
||||
OtherUID = AuthenticatedUserId,
|
||||
IsPaused = otherEntry.IsPaused,
|
||||
@@ -261,24 +251,15 @@ public partial class MareHub
|
||||
|
||||
if (selfCharaIdent == null || otherCharaIdent == null || otherEntry.IsPaused) return;
|
||||
|
||||
if (isPaused)
|
||||
{
|
||||
await Clients.User(AuthenticatedUserId).SendAsync(Api.OnUserRemoveOnlinePairedPlayer, otherCharaIdent).ConfigureAwait(false);
|
||||
await Clients.User(otherUserUid).SendAsync(Api.OnUserRemoveOnlinePairedPlayer, selfCharaIdent).ConfigureAwait(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
await Clients.User(AuthenticatedUserId).SendAsync(Api.OnUserAddOnlinePairedPlayer, otherCharaIdent).ConfigureAwait(false);
|
||||
await Clients.User(otherUserUid).SendAsync(Api.OnUserAddOnlinePairedPlayer, selfCharaIdent).ConfigureAwait(false);
|
||||
}
|
||||
await Clients.User(AuthenticatedUserId).Client_UserChangePairedPlayer(otherCharaIdent, !isPaused).ConfigureAwait(false);
|
||||
await Clients.User(otherUserUid).Client_UserChangePairedPlayer(selfCharaIdent, !isPaused).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
[HubMethodName(Api.SendUserPairedClientRemoval)]
|
||||
public async Task SendPairedClientRemoval(string otherUserUid)
|
||||
public async Task UserRemovePair(string otherUserUid)
|
||||
{
|
||||
_logger.LogCallInfo(Api.SendUserPairedClientRemoval, otherUserUid);
|
||||
_logger.LogCallInfo(MareHubLogger.Args(otherUserUid));
|
||||
|
||||
if (string.Equals(otherUserUid, AuthenticatedUserId, System.StringComparison.Ordinal)) return;
|
||||
|
||||
@@ -292,10 +273,10 @@ public partial class MareHub
|
||||
_dbContext.ClientPairs.Remove(callerPair);
|
||||
await _dbContext.SaveChangesAsync().ConfigureAwait(false);
|
||||
|
||||
_logger.LogCallInfo(Api.SendUserPairedClientRemoval, otherUserUid, "Success");
|
||||
_logger.LogCallInfo(MareHubLogger.Args(otherUserUid, "Success"));
|
||||
|
||||
await Clients.User(AuthenticatedUserId)
|
||||
.SendAsync(Api.OnUserUpdateClientPairs, new ClientPairDto()
|
||||
.Client_UserUpdateClientPairs(new ClientPairDto()
|
||||
{
|
||||
OtherUID = otherUserUid,
|
||||
IsRemoved = true
|
||||
@@ -310,7 +291,7 @@ public partial class MareHub
|
||||
if (otherIdent == null) return;
|
||||
|
||||
// get own ident and
|
||||
await Clients.User(otherUserUid).SendAsync(Api.OnUserUpdateClientPairs,
|
||||
await Clients.User(otherUserUid).Client_UserUpdateClientPairs(
|
||||
new ClientPairDto()
|
||||
{
|
||||
OtherUID = AuthenticatedUserId,
|
||||
@@ -333,16 +314,16 @@ public partial class MareHub
|
||||
if (!callerHadPaused && !otherHadPaused && isPausedInGroup)
|
||||
{
|
||||
var userIdent = await _clientIdentService.GetCharacterIdentForUid(AuthenticatedUserId).ConfigureAwait(false);
|
||||
await Clients.User(AuthenticatedUserId).SendAsync(Api.OnUserRemoveOnlinePairedPlayer, otherIdent).ConfigureAwait(false);
|
||||
await Clients.User(otherUserUid).SendAsync(Api.OnUserRemoveOnlinePairedPlayer, userIdent).ConfigureAwait(false);
|
||||
await Clients.User(AuthenticatedUserId).Client_UserChangePairedPlayer(otherIdent, false).ConfigureAwait(false);
|
||||
await Clients.User(otherUserUid).Client_UserChangePairedPlayer(userIdent, false).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
// if the caller had paused other but not the other has paused the caller and they are in an unpaused group together, change state to online
|
||||
if (callerHadPaused && !otherHadPaused && !isPausedInGroup)
|
||||
{
|
||||
var userIdent = await _clientIdentService.GetCharacterIdentForUid(AuthenticatedUserId).ConfigureAwait(false);
|
||||
await Clients.User(AuthenticatedUserId).SendAsync(Api.OnUserAddOnlinePairedPlayer, otherIdent).ConfigureAwait(false);
|
||||
await Clients.User(otherUserUid).SendAsync(Api.OnUserAddOnlinePairedPlayer, userIdent).ConfigureAwait(false);
|
||||
await Clients.User(AuthenticatedUserId).Client_UserChangePairedPlayer(otherIdent, true).ConfigureAwait(false);
|
||||
await Clients.User(otherUserUid).Client_UserChangePairedPlayer(userIdent, true).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace MareSynchronosServer.Hubs;
|
||||
|
||||
public partial class MareHub : Hub
|
||||
public partial class MareHub : Hub<IMareHub>, IMareHub
|
||||
{
|
||||
private readonly MareMetrics _mareMetrics;
|
||||
private readonly AuthService.AuthServiceClient _authServiceClient;
|
||||
@@ -54,7 +54,6 @@ public partial class MareHub : Hub
|
||||
_dbContext = mareDbContext;
|
||||
}
|
||||
|
||||
[HubMethodName(Api.InvokeHeartbeat)]
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
public async Task<ConnectionDto> Heartbeat(string characterIdentification)
|
||||
{
|
||||
@@ -62,9 +61,9 @@ public partial class MareHub : Hub
|
||||
|
||||
var userId = Context.User!.Claims.SingleOrDefault(c => string.Equals(c.Type, ClaimTypes.NameIdentifier, StringComparison.Ordinal))?.Value;
|
||||
|
||||
_logger.LogCallInfo(Api.InvokeHeartbeat, characterIdentification);
|
||||
_logger.LogCallInfo(MareHubLogger.Args(characterIdentification));
|
||||
|
||||
await Clients.Caller.SendAsync(Api.OnUpdateSystemInfo, _systemInfoService.SystemInfoDto).ConfigureAwait(false);
|
||||
await Clients.Caller.Client_UpdateSystemInfo(_systemInfoService.SystemInfoDto).ConfigureAwait(false);
|
||||
|
||||
var isBanned = await _dbContext.BannedUsers.AsNoTracking().AnyAsync(u => u.CharacterIdentification == characterIdentification).ConfigureAwait(false);
|
||||
|
||||
@@ -74,11 +73,11 @@ public partial class MareHub : Hub
|
||||
var existingIdent = await _clientIdentService.GetCharacterIdentForUid(userId).ConfigureAwait(false);
|
||||
if (!string.IsNullOrEmpty(existingIdent) && !string.Equals(characterIdentification, existingIdent, StringComparison.Ordinal))
|
||||
{
|
||||
_logger.LogCallInfo(Api.InvokeHeartbeat, characterIdentification, "Failure", "LoggedIn");
|
||||
_logger.LogCallWarning(MareHubLogger.Args(characterIdentification, "Failure", "LoggedIn"));
|
||||
|
||||
return new ConnectionDto()
|
||||
{
|
||||
ServerVersion = Api.Version
|
||||
ServerVersion = IMareHub.ApiVersion
|
||||
};
|
||||
}
|
||||
|
||||
@@ -86,11 +85,11 @@ public partial class MareHub : Hub
|
||||
await _clientIdentService.MarkUserOnline(user.UID, characterIdentification).ConfigureAwait(false);
|
||||
await _dbContext.SaveChangesAsync().ConfigureAwait(false);
|
||||
|
||||
_logger.LogCallInfo(Api.InvokeHeartbeat, characterIdentification, "Success");
|
||||
_logger.LogCallInfo(MareHubLogger.Args(characterIdentification, "Success"));
|
||||
|
||||
return new ConnectionDto
|
||||
{
|
||||
ServerVersion = Api.Version,
|
||||
ServerVersion = IMareHub.ApiVersion,
|
||||
UID = string.IsNullOrEmpty(user.Alias) ? user.UID : user.Alias,
|
||||
IsModerator = user.IsModerator,
|
||||
IsAdmin = user.IsAdmin,
|
||||
@@ -104,15 +103,14 @@ public partial class MareHub : Hub
|
||||
};
|
||||
}
|
||||
|
||||
_logger.LogCallInfo(Api.InvokeHeartbeat, characterIdentification, "Failure");
|
||||
_logger.LogCallWarning(MareHubLogger.Args(characterIdentification, "Failure"));
|
||||
|
||||
return new ConnectionDto()
|
||||
{
|
||||
ServerVersion = Api.Version
|
||||
ServerVersion = IMareHub.ApiVersion
|
||||
};
|
||||
}
|
||||
|
||||
[HubMethodName(Api.InvokeCheckClientHealth)]
|
||||
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
|
||||
public async Task<bool> CheckClientHealth()
|
||||
{
|
||||
@@ -121,14 +119,14 @@ public partial class MareHub : Hub
|
||||
if (string.IsNullOrEmpty(serverId) || !string.Equals(serverId, _shardName, StringComparison.Ordinal))
|
||||
{
|
||||
needsReconnect = true;
|
||||
_logger.LogCallWarning(Api.InvokeCheckClientHealth, needsReconnect);
|
||||
_logger.LogCallWarning(MareHubLogger.Args(needsReconnect));
|
||||
}
|
||||
return needsReconnect;
|
||||
}
|
||||
|
||||
public override async Task OnConnectedAsync()
|
||||
{
|
||||
_logger.LogCallInfo("Connect", _contextAccessor.GetIpAddress());
|
||||
_logger.LogCallInfo(MareHubLogger.Args(_contextAccessor.GetIpAddress()));
|
||||
_mareMetrics.IncGauge(MetricsAPI.GaugeConnections);
|
||||
await base.OnConnectedAsync().ConfigureAwait(false);
|
||||
}
|
||||
@@ -143,9 +141,9 @@ public partial class MareHub : Hub
|
||||
{
|
||||
_mareMetrics.DecGauge(MetricsAPI.GaugeAuthorizedConnections);
|
||||
|
||||
_logger.LogCallInfo("Disconnect");
|
||||
_logger.LogCallInfo();
|
||||
|
||||
await SendDataToAllPairedUsers(Api.OnUserRemoveOnlinePairedPlayer, userCharaIdent).ConfigureAwait(false);
|
||||
await SendOfflineToAllPairedUsers(userCharaIdent).ConfigureAwait(false);
|
||||
|
||||
_dbContext.RemoveRange(_dbContext.Files.Where(f => !f.Uploaded && f.UploaderUID == AuthenticatedUserId));
|
||||
|
||||
|
||||
@@ -19,7 +19,10 @@ public class GrpcClientIdentificationService : IHostedService
|
||||
private readonly IdentificationService.IdentificationServiceClient _grpcIdentClient;
|
||||
private readonly MareMetrics _metrics;
|
||||
protected ConcurrentDictionary<string, string> OnlineClients = new(StringComparer.Ordinal);
|
||||
private ConcurrentDictionary<string, string> RemoteCachedIdents = new(StringComparer.Ordinal);
|
||||
private readonly TimeSpan InvalidateCachedIdent = TimeSpan.FromSeconds(30);
|
||||
private bool _grpcIsFaulty = false;
|
||||
private CancellationTokenSource _cacheVerificationCts = new();
|
||||
|
||||
public GrpcClientIdentificationService(ILogger<GrpcClientIdentificationService> logger, IdentificationService.IdentificationServiceClient gprcIdentClient, MareMetrics metrics, IConfiguration configuration)
|
||||
{
|
||||
@@ -30,6 +33,46 @@ public class GrpcClientIdentificationService : IHostedService
|
||||
_metrics = metrics;
|
||||
}
|
||||
|
||||
private async Task HandleCacheVerification(CancellationToken ct)
|
||||
{
|
||||
while (!ct.IsCancellationRequested)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (RemoteCachedIdents.Any())
|
||||
{
|
||||
MultiUidMessage req = new();
|
||||
req.Uids.AddRange(RemoteCachedIdents.Select(k => new UidMessage() { Uid = k.Key }));
|
||||
var cacheResponse = await InvokeOnGrpc(_grpcIdentClient.ValidateCachedIdentsAsync(req)).ConfigureAwait(false);
|
||||
|
||||
foreach (var entry in cacheResponse.UidWithIdent)
|
||||
{
|
||||
if (!RemoteCachedIdents.TryGetValue(entry.Uid.Uid, out var ident))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(entry.Ident.Ident))
|
||||
{
|
||||
RemoteCachedIdents.TryRemove(entry.Uid.Uid, out var _);
|
||||
continue;
|
||||
}
|
||||
|
||||
RemoteCachedIdents[entry.Uid.Uid] = entry.Ident.Ident;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "Error during cached idents verification");
|
||||
}
|
||||
finally
|
||||
{
|
||||
await Task.Delay(InvalidateCachedIdent, ct).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<string?> GetCharacterIdentForUid(string uid)
|
||||
{
|
||||
if (OnlineClients.TryGetValue(uid, out string ident))
|
||||
@@ -37,8 +80,14 @@ public class GrpcClientIdentificationService : IHostedService
|
||||
return ident;
|
||||
}
|
||||
|
||||
if (RemoteCachedIdents.TryGetValue(uid, out var cachedIdent))
|
||||
{
|
||||
return cachedIdent;
|
||||
}
|
||||
|
||||
var result = await InvokeOnGrpc(_grpcIdentClient.GetIdentForUidAsync(new UidMessage { Uid = uid })).ConfigureAwait(false);
|
||||
if (result == default(CharacterIdentMessage)) return null;
|
||||
RemoteCachedIdents[uid] = result.Ident;
|
||||
return result.Ident;
|
||||
}
|
||||
|
||||
@@ -91,10 +140,13 @@ public class GrpcClientIdentificationService : IHostedService
|
||||
public async Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
await ExecuteOnGrpc(_grpcIdentClient.ClearIdentsForServerAsync(new ServerMessage() { ServerId = _shardName })).ConfigureAwait(false);
|
||||
|
||||
_ = HandleCacheVerification(_cacheVerificationCts.Token);
|
||||
}
|
||||
|
||||
public async Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_cacheVerificationCts.Cancel();
|
||||
await ExecuteOnGrpc(_grpcIdentClient.ClearIdentsForServerAsync(new ServerMessage() { ServerId = _shardName })).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
@@ -156,4 +208,4 @@ public class GrpcClientIdentificationService : IHostedService
|
||||
_logger.LogWarning("GRPC connection is faulty");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ using MareSynchronosServer.Hubs;
|
||||
using MareSynchronosShared.Data;
|
||||
using MareSynchronosShared.Metrics;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
@@ -20,12 +21,11 @@ public class SystemInfoService : IHostedService, IDisposable
|
||||
private readonly IServiceProvider _services;
|
||||
private readonly GrpcClientIdentificationService _clientIdentService;
|
||||
private readonly ILogger<SystemInfoService> _logger;
|
||||
private readonly IHubContext<MareHub> _hubContext;
|
||||
private readonly IHubContext<MareHub, IMareHub> _hubContext;
|
||||
private Timer _timer;
|
||||
private string _shardName;
|
||||
public SystemInfoDto SystemInfoDto { get; private set; } = new();
|
||||
|
||||
public SystemInfoService(MareMetrics mareMetrics, IConfiguration configuration, IServiceProvider services, GrpcClientIdentificationService clientIdentService, ILogger<SystemInfoService> logger, IHubContext<MareHub> hubContext)
|
||||
public SystemInfoService(MareMetrics mareMetrics, IConfiguration configuration, IServiceProvider services, GrpcClientIdentificationService clientIdentService, ILogger<SystemInfoService> logger, IHubContext<MareHub, IMareHub> hubContext)
|
||||
{
|
||||
_mareMetrics = mareMetrics;
|
||||
_services = services;
|
||||
@@ -58,16 +58,16 @@ public class SystemInfoService : IHostedService, IDisposable
|
||||
OnlineUsers = (int)_clientIdentService.GetOnlineUsers().Result,
|
||||
};
|
||||
|
||||
_hubContext.Clients.All.SendAsync(Api.OnUpdateSystemInfo, SystemInfoDto);
|
||||
_hubContext.Clients.All.Client_UpdateSystemInfo(SystemInfoDto);
|
||||
|
||||
using var scope = _services.CreateScope();
|
||||
using var db = scope.ServiceProvider.GetService<MareDbContext>()!;
|
||||
|
||||
_mareMetrics.SetGaugeTo(MetricsAPI.GaugePairs, db.ClientPairs.Count());
|
||||
_mareMetrics.SetGaugeTo(MetricsAPI.GaugePairsPaused, db.ClientPairs.Count(p => p.IsPaused));
|
||||
_mareMetrics.SetGaugeTo(MetricsAPI.GaugeGroups, db.Groups.Count());
|
||||
_mareMetrics.SetGaugeTo(MetricsAPI.GaugeGroupPairs, db.GroupPairs.Count());
|
||||
_mareMetrics.SetGaugeTo(MetricsAPI.GaugeGroupPairsPaused, db.GroupPairs.Count(p => p.IsPaused));
|
||||
_mareMetrics.SetGaugeTo(MetricsAPI.GaugePairs, db.ClientPairs.AsNoTracking().Count());
|
||||
_mareMetrics.SetGaugeTo(MetricsAPI.GaugePairsPaused, db.ClientPairs.AsNoTracking().Count(p => p.IsPaused));
|
||||
_mareMetrics.SetGaugeTo(MetricsAPI.GaugeGroups, db.Groups.AsNoTracking().Count());
|
||||
_mareMetrics.SetGaugeTo(MetricsAPI.GaugeGroupPairs, db.GroupPairs.AsNoTracking().Count());
|
||||
_mareMetrics.SetGaugeTo(MetricsAPI.GaugeGroupPairsPaused, db.GroupPairs.AsNoTracking().Count(p => p.IsPaused));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -189,7 +189,7 @@ public class Startup
|
||||
|
||||
app.UseEndpoints(endpoints =>
|
||||
{
|
||||
endpoints.MapHub<MareHub>(Api.Path, options =>
|
||||
endpoints.MapHub<MareHub>(IMareHub.Path, options =>
|
||||
{
|
||||
options.ApplicationMaxBufferSize = 5242880;
|
||||
options.TransportMaxBufferSize = 5242880;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using MareSynchronosServer.Hubs;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace MareSynchronosServer.Utils;
|
||||
|
||||
@@ -13,16 +14,20 @@ public class MareHubLogger
|
||||
_hub = hub;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void LogCallInfo(string methodName, params object[] args)
|
||||
public static object[] Args(params object[] args)
|
||||
{
|
||||
string formattedArgs = args.Length != 0 ? "|" + string.Join(":", args) : string.Empty;
|
||||
return args;
|
||||
}
|
||||
|
||||
public void LogCallInfo(object[] args = null, [CallerMemberName] string methodName = "")
|
||||
{
|
||||
string formattedArgs = args != null && args.Length != 0 ? "|" + string.Join(":", args) : string.Empty;
|
||||
_logger.LogInformation("{uid}:{method}{args}", _hub.AuthenticatedUserId, methodName, formattedArgs);
|
||||
}
|
||||
|
||||
public void LogCallWarning(string methodName, params object[] args)
|
||||
public void LogCallWarning(object[] args = null, [CallerMemberName] string methodName = "")
|
||||
{
|
||||
string formattedArgs = args.Length != 0 ? "|" + string.Join(":", args) : string.Empty;
|
||||
string formattedArgs = args != null && args.Length != 0 ? "|" + string.Join(":", args) : string.Empty;
|
||||
_logger.LogWarning("{uid}:{method}{args}", _hub.AuthenticatedUserId, methodName, formattedArgs);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,4 +66,23 @@ internal class IdentityService : IdentificationService.IdentificationServiceBase
|
||||
}
|
||||
return Task.FromResult(new Empty());
|
||||
}
|
||||
|
||||
public override async Task<UidWithIdentMessage> ValidateCachedIdents(MultiUidMessage request, ServerCallContext context)
|
||||
{
|
||||
UidWithIdentMessage response = new UidWithIdentMessage();
|
||||
foreach (var msg in request.Uids)
|
||||
{
|
||||
UidWithIdent msgResp = new()
|
||||
{
|
||||
Uid = msg,
|
||||
Ident = new()
|
||||
};
|
||||
var ident = await _handler.GetIdentForuid(msg.Uid);
|
||||
msgResp.Ident.Ident = ident.CharacterIdent;
|
||||
msgResp.Ident.ServerId = ident.ServerId;
|
||||
response.UidWithIdent.Add(msgResp);
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
}
|
||||
@@ -24,14 +24,32 @@ service IdentificationService {
|
||||
rpc GetIdentForUid (UidMessage) returns (CharacterIdentMessage);
|
||||
rpc ClearIdentsForServer (ServerMessage) returns (Empty);
|
||||
rpc RecreateServerIdents (ServerIdentMessage) returns (Empty);
|
||||
rpc ValidateCachedIdents (MultiUidMessage) returns (UidWithIdentMessage);
|
||||
}
|
||||
|
||||
message Empty { }
|
||||
|
||||
message MultiUidMessage {
|
||||
repeated UidMessage uids = 1;
|
||||
}
|
||||
|
||||
message UidWithIdentMessage {
|
||||
repeated UidWithIdent uidWithIdent = 1;
|
||||
}
|
||||
|
||||
message UidWithIdent {
|
||||
UidMessage uid = 1;
|
||||
CharacterIdentMessage ident = 2;
|
||||
}
|
||||
|
||||
message ServerIdentMessage {
|
||||
repeated SetIdentMessage idents = 1;
|
||||
}
|
||||
|
||||
message UidMessage {
|
||||
string uid = 1;
|
||||
}
|
||||
|
||||
message ServerMessage {
|
||||
string server_id = 1;
|
||||
}
|
||||
@@ -56,10 +74,6 @@ message CharacterIdentMessage {
|
||||
string ident = 2;
|
||||
}
|
||||
|
||||
message UidMessage {
|
||||
string uid = 1;
|
||||
}
|
||||
|
||||
message UploadFileRequest {
|
||||
string hash = 1;
|
||||
string uploader = 2;
|
||||
|
||||
Reference in New Issue
Block a user