Skip to content

Commit

Permalink
Push messages to staff members
Browse files Browse the repository at this point in the history
  • Loading branch information
oliver-scott committed Sep 5, 2024
1 parent 0e31f6f commit dff8e5a
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ await HubContext.Clients.Group(participant.Username.ToLowerInvariant())

await HubContext.Clients.Group(Hub.EventHub.VhOfficersGroupName)
.HearingCancelledMessage(conferenceId);

await HubContext.Clients.Group(Hub.EventHub.StaffMembersGroupName)
.NewConferenceAddedMessage(conferenceId);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ await HubContext.Clients.Group(participant.Username.ToLowerInvariant())

await HubContext.Clients.Group(Hub.EventHub.VhOfficersGroupName)
.HearingDetailsUpdatedMessage(conferenceId);

await HubContext.Clients.Group(Hub.EventHub.StaffMembersGroupName)
.HearingDetailsUpdatedMessage(conferenceId);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ await HubContext.Clients.Group(participant.Username.ToLowerInvariant())

await HubContext.Clients.Group(Hub.EventHub.VhOfficersGroupName)
.NewConferenceAddedMessage(conferenceId);

await HubContext.Clients.Group(Hub.EventHub.StaffMembersGroupName)
.NewConferenceAddedMessage(conferenceId);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,8 @@ await HubContext.Clients.Group(participant.UserName.ToLowerInvariant())

await HubContext.Clients.Group(Hub.EventHub.VhOfficersGroupName)
.ParticipantsUpdatedMessage(SourceConference.Id, updatedParticipants);

await HubContext.Clients.Group(Hub.EventHub.StaffMembersGroupName)
.ParticipantsUpdatedMessage(SourceConference.Id, updatedParticipants);
}
}
30 changes: 24 additions & 6 deletions VideoWeb/VideoWeb.EventHub/Hub/EventHubClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ public class EventHub(
{
public static string VhOfficersGroupName => "VhOfficers";
public static string DefaultAdminName => "Admin";
public static string StaffMembersGroupName => "StaffMembers";

public override async Task OnConnectedAsync()
{
var isAdmin = IsSenderAdmin();
var isStaffMember = IsSenderStaffMember();

await AddUserToUserGroup(isAdmin);
await AddUserToConferenceGroups(isAdmin);
await AddUserToUserGroup(isAdmin, isStaffMember);
await AddUserToConferenceGroups(isAdmin || isStaffMember);

await base.OnConnectedAsync();

Expand All @@ -59,12 +61,17 @@ public async Task AddToGroup(string conferenceId)
}
}

private async Task AddUserToUserGroup(bool isAdmin)
private async Task AddUserToUserGroup(bool isAdmin, bool isStaffMember)
{
if (isAdmin)
{
await Groups.AddToGroupAsync(Context.ConnectionId, VhOfficersGroupName);
}

if (isStaffMember)
{
await Groups.AddToGroupAsync(Context.ConnectionId, StaffMembersGroupName);
}

await Groups.AddToGroupAsync(Context.ConnectionId, Context.User.Identity!.Name!.ToLowerInvariant());
}
Expand All @@ -86,20 +93,26 @@ public override async Task OnDisconnectedAsync(Exception exception)
}

var isAdmin = IsSenderAdmin();
await RemoveUserFromUserGroup(isAdmin);
await RemoveUserFromConferenceGroups(isAdmin);
var isStaffMember = IsSenderStaffMember();
await RemoveUserFromUserGroup(isAdmin, isStaffMember);
await RemoveUserFromConferenceGroups(isAdmin || isStaffMember);
await userProfileService.ClearUserCache(username);
await appRoleService.ClearUserCache(username);

await base.OnDisconnectedAsync(exception);
}

private async Task RemoveUserFromUserGroup(bool isAdmin)
private async Task RemoveUserFromUserGroup(bool isAdmin, bool isStaffMember)
{
if (isAdmin)
{
await Groups.RemoveFromGroupAsync(Context.ConnectionId, VhOfficersGroupName);
}

if (isStaffMember)
{
await Groups.RemoveFromGroupAsync(Context.ConnectionId, StaffMembersGroupName);
}

await Groups.RemoveFromGroupAsync(Context.ConnectionId, Context.User.Identity.Name.ToLowerInvariant());
}
Expand Down Expand Up @@ -129,6 +142,11 @@ private bool IsSenderAdmin()
{
return Context.User.IsInRole(AppRoles.VhOfficerRole);
}

private bool IsSenderStaffMember()
{
return Context.User.IsInRole(AppRoles.StaffMember);
}

private string GetObfuscatedUsernameAsync(string username)
{
Expand Down
3 changes: 3 additions & 0 deletions VideoWeb/VideoWeb.UnitTests/Builders/EventComponentHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ public void RegisterUsersForHubContext(IEnumerable<Participant> participants)

EventHubContextMock.Setup(x => x.Clients.Group(EventHub.Hub.EventHub.VhOfficersGroupName))
.Returns(EventHubClientMock.Object);

EventHubContextMock.Setup(x => x.Clients.Group(EventHub.Hub.EventHub.StaffMembersGroupName))
.Returns(EventHubClientMock.Object);
}

public Conference BuildConferenceForTest()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public async Task should_publish_message()
await _eventHandler.HandleAsync(callbackEvent);

const int vhoCount = 1;
EventHubClientMock.Verify(x => x.HearingDetailsUpdatedMessage(callbackEvent.ConferenceId), Times.Exactly(TestConference.Participants.Count + vhoCount));
const int staffMemberCount = 1;
EventHubClientMock.Verify(x => x.HearingDetailsUpdatedMessage(callbackEvent.ConferenceId), Times.Exactly(TestConference.Participants.Count + vhoCount + staffMemberCount));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public async Task Publish_New_Conference_Added_Event()
await _eventHandler.HandleAsync(callbackEvent);

const int vhoCount = 1;
EventHubClientMock.Verify(x => x.NewConferenceAddedMessage(callbackEvent.ConferenceId), Times.Exactly(TestConference.Participants.Count + vhoCount));
const int staffMemberCount = 1;
EventHubClientMock.Verify(x => x.NewConferenceAddedMessage(callbackEvent.ConferenceId), Times.Exactly(TestConference.Participants.Count + vhoCount + staffMemberCount));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ public async Task Should_send_participants_updated_message_to_participants()

await _eventHandler.HandleAsync(callbackEvent);

const int vhoCount = 1;
const int staffMemberCount = 1;
EventHubClientMock.Verify(
x => x.ParticipantsUpdatedMessage(conference.Id, participants), Times.Exactly(participantCount + 1));
x => x.ParticipantsUpdatedMessage(conference.Id, participants), Times.Exactly(participantCount + vhoCount + staffMemberCount));
TestConference.Participants.Should().HaveCount(participantCount);
}
}
Expand Down

0 comments on commit dff8e5a

Please sign in to comment.