diff --git a/dotnet/AipsRT/Hubs/WhiteboardHub.cs b/dotnet/AipsRT/Hubs/WhiteboardHub.cs index a94589c..519c915 100644 --- a/dotnet/AipsRT/Hubs/WhiteboardHub.cs +++ b/dotnet/AipsRT/Hubs/WhiteboardHub.cs @@ -21,12 +21,14 @@ public class WhiteboardHub : Hub private readonly WhiteboardManager _whiteboardManager; private readonly IMessagingService _messagingService; private readonly MembershipService _membershipService; + private readonly GetUserService _getUserService; - public WhiteboardHub(WhiteboardManager whiteboardManager, IMessagingService messagingService, MembershipService membershipService) + public WhiteboardHub(WhiteboardManager whiteboardManager, IMessagingService messagingService, MembershipService membershipService, GetUserService getUserService) { _whiteboardManager = whiteboardManager; _messagingService = messagingService; _membershipService = membershipService; + _getUserService = getUserService; } public override async Task OnDisconnectedAsync(Exception? exception) @@ -71,9 +73,10 @@ public class WhiteboardHub : Hub status = await _membershipService.GetMembershipStatus(whiteboardId, userId); } + _whiteboardManager.AddUserToWhiteboard(userId, whiteboardId); + if (status == WhiteboardMembershipStatus.Accepted) { - _whiteboardManager.AddUserToWhiteboard(userId, whiteboardId); var joiningUser = whiteboard.Users.FirstOrDefault(u => u.UserId == userId); if (joiningUser == null) @@ -84,8 +87,7 @@ public class WhiteboardHub : Hub } else { - joiningUser = new User(userId, Context.User?.Identity?.Name ?? "Unknown", - ""); + joiningUser = new User(userId, Context.User?.Identity?.Name ?? "Unknown", ""); whiteboard.AddUser(joiningUser); } } @@ -101,7 +103,12 @@ public class WhiteboardHub : Hub { await Clients.Caller.SendAsync("WaitingForApproval", userId.ToString()); - var user = whiteboard.Users.First(u => u.UserId == userId); + var user = whiteboard.Users.FirstOrDefault(u => u.UserId == userId); + + if (user == null) + { + user = await _getUserService.GetUser(userId); + } await Clients.User(ownerId.ToString()).SendAsync("UserWaitingForApproval", user); } @@ -113,8 +120,15 @@ public class WhiteboardHub : Hub await _messagingService.AcceptedUser(new AcceptUserRequestToJoinCommand(whiteboard.WhiteboardId.ToString(), targetUserId.ToString())); + var user = whiteboard.Users.FirstOrDefault(u => u.UserId == targetUserId); + + whiteboard.AddActiveUser(user!); + await Clients.User(targetUserId.ToString()).SendAsync("Accepted"); await Clients.User(targetUserId.ToString()).SendAsync("InitWhiteboard", whiteboard); + + await Clients.GroupExcept(whiteboard.WhiteboardId.ToString(), + Context.ConnectionId).SendAsync("Joined", user); } public async Task RejectUser(Guid targetUserId) diff --git a/dotnet/AipsRT/Model/Users/GetUserService.cs b/dotnet/AipsRT/Model/Users/GetUserService.cs new file mode 100644 index 0000000..988939e --- /dev/null +++ b/dotnet/AipsRT/Model/Users/GetUserService.cs @@ -0,0 +1,22 @@ +using AipsCore.Application.Abstract; +using AipsCore.Application.Models.User.Query.GetUser; + +namespace AipsRT.Model.Users; + +public class GetUserService +{ + private readonly IDispatcher _dispatcher; + + public GetUserService(IDispatcher dispatcher) + { + _dispatcher = dispatcher; + } + + public async Task GetUser(Guid userId) + { + var query = new GetUserQuery(userId.ToString()); + var userQueryDto = await _dispatcher.Execute(query); + + return new User(Guid.Parse(userQueryDto.Id), userQueryDto.UserName, userQueryDto.Email); + } +} \ No newline at end of file diff --git a/dotnet/AipsRT/Program.cs b/dotnet/AipsRT/Program.cs index 8d0d68e..8d8125e 100644 --- a/dotnet/AipsRT/Program.cs +++ b/dotnet/AipsRT/Program.cs @@ -2,6 +2,7 @@ using AipsCore.Application.Common.Message.ErrorMessage; using AipsCore.Infrastructure.DI; using AipsRT.Hubs; using AipsRT.Model.Memberships; +using AipsRT.Model.Users; using AipsRT.Model.Whiteboard; using AipsRT.Services; using AipsRT.Services.Interfaces; @@ -23,6 +24,7 @@ builder.Services.AddSingleton(); builder.Services.AddTransient(); +builder.Services.AddTransient(); builder.Services.AddScoped(); builder.Services.AddSingleton(); diff --git a/front/src/stores/whiteboard.ts b/front/src/stores/whiteboard.ts index 2fab846..c3eb8a4 100644 --- a/front/src/stores/whiteboard.ts +++ b/front/src/stores/whiteboard.ts @@ -51,11 +51,21 @@ export const useWhiteboardStore = defineStore('whiteboard', () => { } function registerHubEvents() { + whiteboardHubService.offAll() + whiteboardHubService.onInitWhiteboard((wb) => { console.log('InitWhiteboard payload:', JSON.stringify(wb, null, 2)) deselectShape() + whiteboard.value = wb - connectedUsers.value = wb.activeUsers ?? [] + + const uniqueUsers = new Map() + + for (const user of wb.activeUsers ?? []) { + uniqueUsers.set(user.userId, user) + } + + connectedUsers.value = Array.from(uniqueUsers.values()) isLoading.value = false }) @@ -95,7 +105,7 @@ export const useWhiteboardStore = defineStore('whiteboard', () => { }) whiteboardHubService.onUserWaitingForApproval((user) => { - if (!pendingUsers.value.includes(user)) { + if (!pendingUsers.value.some(u => u.userId === user.userId)) { pendingUsers.value.push(user) } }) diff --git a/front/src/stores/whiteboards.ts b/front/src/stores/whiteboards.ts index 9422eab..fbdb553 100644 --- a/front/src/stores/whiteboards.ts +++ b/front/src/stores/whiteboards.ts @@ -2,7 +2,7 @@ import { defineStore } from 'pinia' import { ref } from 'vue' import type {JoinResult, Whiteboard} from '@/types' import { whiteboardService } from '@/services/whiteboardService' -import type {WhiteboardJoinPolicy} from "@/enums"; +import type {WhiteboardJoinPolicy} from "@/enums"; export const useWhiteboardsStore = defineStore('whiteboards', () => { const ownedWhiteboards = ref([])