This commit is contained in:
2026-03-04 23:13:32 +01:00
parent 2fa0f3cb8b
commit 4e6b5da71b
6 changed files with 160 additions and 10 deletions

View File

@@ -1,6 +1,9 @@
using AipsCore.Application.Abstract.MessageBroking; using AipsCore.Application.Abstract;
using AipsCore.Application.Common.Message.MoveShape;
using AipsCore.Application.Models.Shape.Command.MoveShape; using AipsCore.Application.Models.Shape.Command.MoveShape;
using AipsCore.Application.Models.Whiteboard.Command.AcceptUserRequestToJoin;
using AipsCore.Application.Models.Whiteboard.Command.RejectUserRequestToJoin;
using AipsCore.Application.Models.Whiteboard.Query.GetMembershipStatus;
using AipsCore.Domain.Models.WhiteboardMembership.Enums;
using AipsRT.Model.Whiteboard; using AipsRT.Model.Whiteboard;
using AipsRT.Model.Whiteboard.Shapes; using AipsRT.Model.Whiteboard.Shapes;
using AipsRT.Model.Whiteboard.Structs; using AipsRT.Model.Whiteboard.Structs;
@@ -15,27 +18,95 @@ public class WhiteboardHub : Hub
{ {
private readonly WhiteboardManager _whiteboardManager; private readonly WhiteboardManager _whiteboardManager;
private readonly IMessagingService _messagingService; private readonly IMessagingService _messagingService;
private readonly IDispatcher _dispatcher;
public WhiteboardHub(WhiteboardManager whiteboardManager, IMessagingService messagingService) public WhiteboardHub(WhiteboardManager whiteboardManager, IMessagingService messagingService, IDispatcher dispatcher)
{ {
_whiteboardManager = whiteboardManager; _whiteboardManager = whiteboardManager;
_messagingService = messagingService; _messagingService = messagingService;
_dispatcher = dispatcher;
} }
public async Task JoinWhiteboard(Guid whiteboardId) public async Task JoinWhiteboard(Guid whiteboardId)
{ {
if (!_whiteboardManager.WhiteboardExists(whiteboardId)) if (!_whiteboardManager.WhiteboardExists(whiteboardId))
{
await _whiteboardManager.AddWhiteboard(whiteboardId); await _whiteboardManager.AddWhiteboard(whiteboardId);
}
await Groups.AddToGroupAsync(Context.ConnectionId, whiteboardId.ToString()); await Groups.AddToGroupAsync(Context.ConnectionId, whiteboardId.ToString());
var whiteboard = _whiteboardManager.GetWhiteboard(whiteboardId)!;
var userId = CurrentUserId;
var ownerId = whiteboard.OwnerId;
WhiteboardMembershipStatus status;
if (userId == ownerId)
{
status = WhiteboardMembershipStatus.Accepted;
}
else
{
status = await _dispatcher.Execute(new GetMembershipStatusQuery(whiteboardId.ToString(), userId.ToString()));
}
if (status == WhiteboardMembershipStatus.Accepted)
{
_whiteboardManager.AddUserToWhiteboard(userId, whiteboardId);
whiteboard.AcceptUser(userId);
var state = _whiteboardManager.GetWhiteboard(whiteboardId)!; var state = _whiteboardManager.GetWhiteboard(whiteboardId)!;
_whiteboardManager.AddUserToWhiteboard(Guid.Parse(Context.UserIdentifier!), whiteboardId);
await Clients.Caller.SendAsync("InitWhiteboard", state); await Clients.Caller.SendAsync("InitWhiteboard", state);
await Clients.GroupExcept(whiteboardId.ToString(), Context.ConnectionId)
.SendAsync("Joined", Context.UserIdentifier!); await Clients.GroupExcept(whiteboardId.ToString(), Context.ConnectionId).SendAsync("Joined", Context.UserIdentifier!);
}
else
{
_whiteboardManager.AddPendingUser(userId, whiteboardId);
await Clients.Caller.SendAsync("WaitingForApproval", userId.ToString());
await Clients.User(ownerId.ToString()).SendAsync("UserWaitingForApproval", userId.ToString());
}
}
public async Task AcceptUser(Guid targetUserId)
{
var whiteboard = CurrentWhiteboard;
await _messagingService.AcceptedUser(new AcceptUserRequestToJoinCommand(whiteboard.WhiteboardId.ToString(), targetUserId.ToString()));
_whiteboardManager.MovePendingToAccepted(targetUserId, whiteboard.WhiteboardId);
await Clients.User(targetUserId.ToString()).SendAsync("Accepted");
await Clients.User(targetUserId.ToString()).SendAsync("InitWhiteboard", whiteboard);
}
public async Task RejectUser(Guid targetUserId)
{
var whiteboard = CurrentWhiteboard;
await _messagingService.RejectedUser(new RejectUserRequestToJoinCommand(whiteboard.WhiteboardId.ToString(), targetUserId.ToString()));
_whiteboardManager.RemovePendingUser(targetUserId, whiteboard.WhiteboardId);
await Clients.User(targetUserId.ToString()).SendAsync("Rejected");
}
public async Task CancelJoinRequest()
{
var userId = CurrentUserId;
var whiteboard = _whiteboardManager.GetWhiteboardForUser(userId);
if (whiteboard != null)
{
_whiteboardManager.RemovePendingUser(userId, whiteboard.WhiteboardId);
await Clients.User(whiteboard.OwnerId.ToString())
.SendAsync("UserCanceledJoinRequest", userId.ToString());
}
} }
public async Task LeaveWhiteboard(Guid whiteboardId) public async Task LeaveWhiteboard(Guid whiteboardId)
@@ -60,6 +131,8 @@ public class WhiteboardHub : Hub
public async Task AddRectangle(Rectangle rectangle) public async Task AddRectangle(Rectangle rectangle)
{ {
if (!_whiteboardManager.IsAccepted(CurrentUserId)) return;
rectangle.OwnerId = CurrentUserId; rectangle.OwnerId = CurrentUserId;
var whiteboard = CurrentWhiteboard; var whiteboard = CurrentWhiteboard;
@@ -72,6 +145,8 @@ public class WhiteboardHub : Hub
public async Task AddArrow(Arrow arrow) public async Task AddArrow(Arrow arrow)
{ {
if (!_whiteboardManager.IsAccepted(CurrentUserId)) return;
arrow.OwnerId = CurrentUserId; arrow.OwnerId = CurrentUserId;
var whiteboard = CurrentWhiteboard; var whiteboard = CurrentWhiteboard;
@@ -84,6 +159,8 @@ public class WhiteboardHub : Hub
public async Task AddLine(Line line) public async Task AddLine(Line line)
{ {
if (!_whiteboardManager.IsAccepted(CurrentUserId)) return;
line.OwnerId = CurrentUserId; line.OwnerId = CurrentUserId;
var whiteboard = CurrentWhiteboard; var whiteboard = CurrentWhiteboard;
@@ -96,6 +173,8 @@ public class WhiteboardHub : Hub
public async Task AddTextShape(TextShape textShape) public async Task AddTextShape(TextShape textShape)
{ {
if (!_whiteboardManager.IsAccepted(CurrentUserId)) return;
textShape.OwnerId = CurrentUserId; textShape.OwnerId = CurrentUserId;
var whiteboard = CurrentWhiteboard; var whiteboard = CurrentWhiteboard;
@@ -108,6 +187,8 @@ public class WhiteboardHub : Hub
public async Task MoveShape(MoveShapeCommand moveShape) public async Task MoveShape(MoveShapeCommand moveShape)
{ {
if (!_whiteboardManager.IsAccepted(CurrentUserId)) return;
var whiteboard = CurrentWhiteboard; var whiteboard = CurrentWhiteboard;
var shape = whiteboard.Shapes.Find(s => s.Id.ToString() == moveShape.ShapeId); var shape = whiteboard.Shapes.Find(s => s.Id.ToString() == moveShape.ShapeId);
@@ -125,6 +206,8 @@ public class WhiteboardHub : Hub
public async Task PlaceShape(MoveShapeCommand moveShape) public async Task PlaceShape(MoveShapeCommand moveShape)
{ {
if (!_whiteboardManager.IsAccepted(CurrentUserId)) return;
await MoveShape(moveShape); await MoveShape(moveShape);
await _messagingService.MoveShape(moveShape); await _messagingService.MoveShape(moveShape);

View File

@@ -1,6 +1,8 @@
using AipsCore.Application.Abstract; using AipsCore.Application.Abstract;
using AipsCore.Application.Models.Whiteboard.Query.GetMembershipStatus;
using AipsCore.Application.Models.Whiteboard.Query.GetWhiteboardInfoRT; using AipsCore.Application.Models.Whiteboard.Query.GetWhiteboardInfoRT;
using AipsCore.Domain.Models.Shape.Enums; using AipsCore.Domain.Models.Shape.Enums;
using AipsCore.Domain.Models.WhiteboardMembership.Enums;
using AipsRT.Model.Whiteboard.Shapes.Map; using AipsRT.Model.Whiteboard.Shapes.Map;
namespace AipsRT.Model.Whiteboard; namespace AipsRT.Model.Whiteboard;

View File

@@ -8,6 +8,9 @@ public class Whiteboard
public Guid OwnerId { get; set; } public Guid OwnerId { get; set; }
public HashSet<Guid> AcceptedUsers { get; } = new();
public HashSet<Guid> PendingUsers { get; } = new();
public List<Shape> Shapes { get; } = []; public List<Shape> Shapes { get; } = [];
public List<Rectangle> Rectangles { get; } = []; public List<Rectangle> Rectangles { get; } = [];
@@ -38,4 +41,16 @@ public class Whiteboard
Shapes.Add(shape); Shapes.Add(shape);
TextShapes.Add(shape); TextShapes.Add(shape);
} }
public void AddPendingUser(Guid userId) => PendingUsers.Add(userId);
public void AcceptUser(Guid userId)
{
PendingUsers.Remove(userId);
AcceptedUsers.Add(userId);
}
public void RejectUser(Guid userId) => PendingUsers.Remove(userId);
public bool IsAccepted(Guid userId) => AcceptedUsers.Contains(userId);
} }

View File

@@ -47,13 +47,42 @@ public class WhiteboardManager
return _userInWhiteboards[userId]; return _userInWhiteboards[userId];
} }
public void RemoveUserFromWhiteboard(Guid userId, Guid whiteboardId) public void RemoveUserFromWhiteboard(Guid userId)
{ {
_userInWhiteboards.TryRemove(whiteboardId, out _); _userInWhiteboards.TryRemove(userId, out _);
} }
public Whiteboard? GetWhiteboardForUser(Guid userId) public Whiteboard? GetWhiteboardForUser(Guid userId)
{ {
return GetWhiteboard(GetUserWhiteboard(userId)); return GetWhiteboard(GetUserWhiteboard(userId));
} }
public void AddPendingUser(Guid userId, Guid whiteboardId)
{
var wb = GetWhiteboard(whiteboardId)!;
wb.AddPendingUser(userId);
_userInWhiteboards[userId] = whiteboardId;
}
public void MovePendingToAccepted(Guid userId, Guid whiteboardId)
{
var wb = GetWhiteboard(whiteboardId)!;
wb.AcceptUser(userId);
}
public void RemovePendingUser(Guid userId, Guid whiteboardId)
{
var whiteboard = GetWhiteboard(whiteboardId)!;
whiteboard.RejectUser(userId);
_userInWhiteboards.TryRemove(userId, out _);
}
public bool IsAccepted(Guid userId)
{
if (!_userInWhiteboards.TryGetValue(userId, out var wbId))
return false;
var whiteboard = GetWhiteboard(wbId);
return whiteboard?.IsAccepted(userId) ?? false;
}
} }

View File

@@ -1,5 +1,7 @@
using AipsCore.Application.Models.Shape.Command.CreateTextShape; using AipsCore.Application.Models.Shape.Command.CreateTextShape;
using AipsCore.Application.Models.Shape.Command.MoveShape; using AipsCore.Application.Models.Shape.Command.MoveShape;
using AipsCore.Application.Models.Whiteboard.Command.AcceptUserRequestToJoin;
using AipsCore.Application.Models.Whiteboard.Command.RejectUserRequestToJoin;
using AipsRT.Model.Whiteboard.Shapes; using AipsRT.Model.Whiteboard.Shapes;
namespace AipsRT.Services.Interfaces; namespace AipsRT.Services.Interfaces;
@@ -12,4 +14,7 @@ public interface IMessagingService
Task CreateTextShape(Guid whiteboardId, TextShape textShape); Task CreateTextShape(Guid whiteboardId, TextShape textShape);
Task MoveShape(MoveShapeCommand moveShape); Task MoveShape(MoveShapeCommand moveShape);
Task AcceptedUser(AcceptUserRequestToJoinCommand command);
Task RejectedUser(RejectUserRequestToJoinCommand command);
} }

View File

@@ -1,14 +1,18 @@
using AipsCore.Application.Abstract.MessageBroking; using AipsCore.Application.Abstract.MessageBroking;
using AipsCore.Application.Common.Message.AcceptUserRequestToJoin;
using AipsCore.Application.Common.Message.AddArrow; using AipsCore.Application.Common.Message.AddArrow;
using AipsCore.Application.Common.Message.AddLine; using AipsCore.Application.Common.Message.AddLine;
using AipsCore.Application.Common.Message.AddRectangle; using AipsCore.Application.Common.Message.AddRectangle;
using AipsCore.Application.Common.Message.AddTextShape; using AipsCore.Application.Common.Message.AddTextShape;
using AipsCore.Application.Common.Message.MoveShape; using AipsCore.Application.Common.Message.MoveShape;
using AipsCore.Application.Common.Message.RejectUserRequestToJoin;
using AipsCore.Application.Models.Shape.Command.CreateArrow; using AipsCore.Application.Models.Shape.Command.CreateArrow;
using AipsCore.Application.Models.Shape.Command.CreateLine; using AipsCore.Application.Models.Shape.Command.CreateLine;
using AipsCore.Application.Models.Shape.Command.CreateRectangle; using AipsCore.Application.Models.Shape.Command.CreateRectangle;
using AipsCore.Application.Models.Shape.Command.CreateTextShape; using AipsCore.Application.Models.Shape.Command.CreateTextShape;
using AipsCore.Application.Models.Shape.Command.MoveShape; using AipsCore.Application.Models.Shape.Command.MoveShape;
using AipsCore.Application.Models.Whiteboard.Command.AcceptUserRequestToJoin;
using AipsCore.Application.Models.Whiteboard.Command.RejectUserRequestToJoin;
using AipsRT.Model.Whiteboard.Shapes; using AipsRT.Model.Whiteboard.Shapes;
using AipsRT.Services.Interfaces; using AipsRT.Services.Interfaces;
@@ -100,4 +104,16 @@ public class MessagingService : IMessagingService
var message = new MoveShapeMessage(moveShape); var message = new MoveShapeMessage(moveShape);
await _messagePublisher.PublishAsync(message); await _messagePublisher.PublishAsync(message);
} }
public async Task AcceptedUser(AcceptUserRequestToJoinCommand command)
{
var message = new AcceptUserRequestToJoinMessage(command);
await _messagePublisher.PublishAsync(message);
}
public async Task RejectedUser(RejectUserRequestToJoinCommand command)
{
var message = new RejectUserRequestToJoinMessage(command);
await _messagePublisher.PublishAsync(message);
}
} }