async Task IChannelNotificationHub.OnJoinChannel(MemberSummary member, ChannelSummaryResponse channel) { var clientIds = await GetChannelClientsAsync(new ChannelRequest(member.SaasUserId, channel.Id)); // Tell the people in this room that you've joined await Clients.Clients(clientIds).SendAsync(HubEvents.MemberJoined, member, channel); }
public static ChannelSummaryResponse ToChannelSummaryResponse(this Domain.Channel.Channel channel, bool isMuted, Domain.Message.Message lastReadMessage, MemberSummary creator, CloudStorageConfiguration configuration) { var channelListResponse = new ChannelSummaryResponse(); if (channel != null) { channelListResponse.Id = channel.Id; channelListResponse.IsClosed = channel.IsClosed; channelListResponse.Created = channel.Created; channelListResponse.Updated = channel.Updated; channelListResponse.Name = channel.Name; channelListResponse.Description = channel.Description; channelListResponse.WelcomeMessage = channel.WelcomeMessage; channelListResponse.Type = channel.Type; channelListResponse.IsMuted = isMuted; channelListResponse.CreatorId = channel.CreatorId ?? creator?.Id; channelListResponse.Creator = channel.Creator?.ToMemberSummary(configuration) ?? creator; channelListResponse.CreatorSaasUserId = channel.Creator?.SaasUserId ?? creator?.SaasUserId; channelListResponse.LastMessage = channel.Messages?.FirstOrDefault()?.ToMessageResponse(lastReadMessage, configuration); channelListResponse.UnreadMessagesCount = lastReadMessage != null?channel.Messages?.Count(x => x.Created > lastReadMessage.Created) ?? 0 : channel.Messages?.Count ?? 0; } return(channelListResponse); }
public async Task Step4_ShouldCloseChannel() { // Arrange var channelRequest = new ChannelRequest { RequestId = "623AE57B-9917-4DED-BFFC-44F09C906F10", ChannelId = _testChannel.Id }; // Subscribe event ChannelSummaryResponse channelSummaryResponse = null; void OnChannelClosed(ChannelSummaryResponse response) { channelSummaryResponse = response; } _userSignalRClient.ChannelClosed += OnChannelClosed; // Act await _adminSignalRClient.CloseChannelAsync(channelRequest); // Unsubscribe events _userSignalRClient.ChannelClosed -= OnChannelClosed; // Assert channelSummaryResponse.IsClosed.Should().BeTrue(); }
public async Task OnCloseChannel(ChannelSummaryResponse channel) { var clientIds = await GetChannelClientConnectionIdsAsync(channel.Id); // Tell the people in this room that you've joined await HubContext.Clients.Clients(clientIds).SendAsync(HubEvents.ChannelClosed, channel); }
public async Task Step3_ShouldUpdateChannel() { // Arrange var updateChannelRequest = new UpdateChannelRequest { ChannelId = _testChannel.Id, Name = $"new_{_testChannel.Name}", Description = $"new_{_testChannel.Description}", RequestId = "EA701C57-477D-42E3-B660-E510F7F8C72F", WelcomeMessage = $"new_{_testChannel.WelcomeMessage}" }; // Subscribe event ChannelSummaryResponse channelSummaryResponse = null; void OnChannelUpdated(ChannelSummaryResponse response) { channelSummaryResponse = response; } _userSignalRClient.ChannelUpdated += OnChannelUpdated; // Act await _adminSignalRClient.UpdateChannelAsync(updateChannelRequest); // Unsubscribe events _userSignalRClient.ChannelUpdated -= OnChannelUpdated; // Assert channelSummaryResponse.Id.Should().Be(updateChannelRequest.ChannelId); channelSummaryResponse.Description.Should().BeEquivalentTo(updateChannelRequest.Description); channelSummaryResponse.Name.Should().BeEquivalentTo(updateChannelRequest.Name); channelSummaryResponse.WelcomeMessage.Should().BeEquivalentTo(updateChannelRequest.WelcomeMessage); }
public ChannelSummaryResponse MapToDirectChannelSummaryResponse(Channel channel, DomainModels.Member currentUser, DomainModels.Member directMember, Message lastReadMessage = null) { var response = new ChannelSummaryResponse(); if (channel != null) { response = _mapper.Map(channel, response); var lastMessage = channel.Messages.OrderBy(o => o.Created).LastOrDefault(); if (lastMessage != null) { response.LastMessage = MapToMessageResponse(lastMessage, lastReadMessage?.Created); } response.UnreadMessagesCount = lastReadMessage != null? channel.Messages.Count(x => x.Created > lastReadMessage.Created) : channel.Messages.Count; } if (currentUser != null) { response.CreatorId = currentUser.Id; response.Creator = MapToMemberSummaryResponse(currentUser); } if (directMember != null) { response.DirectMemberId = directMember.Id; response.DirectMember = MapToMemberSummaryResponse(directMember); } return(response); }
public ChannelSummaryResponse MapToChannelSummaryResponse(Channel channel, ChannelMember channelMember, Message lastReadMessage = null) { var response = new ChannelSummaryResponse(); if (channelMember != null) { response = _mapper.Map(channelMember, response); } if (channel != null) { response = _mapper.Map(channel, response); if (channel.Messages == null) { response.UnreadMessagesCount = 0; return(response); } var lastMessage = channel.Messages.OrderBy(o => o.Created).LastOrDefault(); if (lastMessage != null) { response.LastMessage = MapToMessageResponse(lastMessage, lastReadMessage?.Created); } response.UnreadMessagesCount = lastReadMessage != null? channel.Messages.Count(x => x.Created > lastReadMessage.Created) : channel.Messages.Count; } return(response); }
internal static ChatSummaryModel DtoToChatSummary(ChannelSummaryResponse dto) { if (dto == null) { return(null); } var lastMessage = DtoToChatMessage(dto.LastMessage); var member = dto.Members.First(); var directMember = dto.Members.FirstOrDefault(x => x.Id != member.Id); return(new ChatSummaryModel { Id = dto.Id.ToString(), Name = dto.Name, PhotoUrl = dto.PhotoUrl, LastMessage = lastMessage, IsMuted = dto.IsMuted, CreatedDate = dto.Created, UpdatedDate = dto.Updated, UnreadMessagesCount = dto.UnreadMessagesCount, Type = (Models.ChannelType)dto.Type, Member = DtoToChatUser(member), DirectMember = DtoToChatUser(directMember) }); }
public async Task OnJoinDirectChannel(MemberSummaryResponse member, ChannelSummaryResponse channel) { var clientIds = await GetChannelMemberClientConnectionIdsAsync(channel.Id, member.Id); // Tell the people in this room that you've joined await HubContext.Clients.Clients(clientIds).SendAsync(HubEvents.MemberJoined, member, channel); }
public async Task Step2_ShouldCreateChannel() { // Arrange var admin = await _adminSignalRClient.AddClientAsync(); var client = await _userSignalRClient.AddClientAsync(); // Subscribe event ChannelSummaryResponse createdChannel = null; void OnChannelCreated(ChannelSummaryResponse channelSummaryResponse) { createdChannel = channelSummaryResponse; } _adminSignalRClient.ChannelCreated += OnChannelCreated; // Subscribe event MemberSummaryResponse joinedMember = null; ChannelSummaryResponse joinedChannel = null; void OnMemberJoined(MemberSummaryResponse memberSummary, ChannelSummaryResponse channelSummaryResponse) { joinedMember = memberSummary; joinedChannel = channelSummaryResponse; } _adminSignalRClient.MemberJoined += OnMemberJoined; var createChannelRequest = new CreateChannelRequest { Name = "channel_name_without_spaces", Description = "channel description", WelcomeMessage = "welcome message", Type = ChannelType.Private, RequestId = "3433E3F8-E363-4A07-8CAA-8F759340F769", AllowedMembers = new List <string> { admin.MemberId.ToString(), client.MemberId.ToString() } }; // Act _testChannel = await _adminSignalRClient.CreateChannelAsync(createChannelRequest); // Unsubscribe events _adminSignalRClient.ChannelCreated -= OnChannelCreated; _adminSignalRClient.MemberJoined -= OnMemberJoined; // Assert createdChannel.Should().NotBeNull(); createdChannel.Should().BeEquivalentTo(_testChannel); joinedMember.Should().NotBeNull(); joinedChannel.Should().NotBeNull(); joinedChannel.Should().BeEquivalentTo(_testChannel); }
public async Task ShouldReturnChannelSummaryResponse() { // Arrange var allowedChannel = new Channel { Id = new Guid("4C13BEC1-2979-4822-9AAC-520B474214FD") }; var saasUserId = "2A70F115-9F55-4024-829B-6521FE18680C"; _channelRepositoryMock.Setup(x => x.GetChannelWithCreatorAsync(It.Is <Guid>(channel => channel.Equals(allowedChannel.Id)))) .ReturnsAsync(allowedChannel) .Verifiable(); var member = new MemberSummaryResponse(); _memberServiceMock.Setup(x => x.GetMemberBySaasUserIdAsync(It.IsAny <string>())) .ReturnsAsync(member) .Verifiable(); var allowedChannelMember = new ChannelMember(); _channelMemberRepositoryMock.Setup(x => x.GetChannelMemberAsync( It.Is <Guid>(memberId => memberId.Equals(member.Id)), It.Is <Guid>(channelId => channelId.Equals(allowedChannel.Id)))) .ReturnsAsync(allowedChannelMember) .Verifiable(); var lastReadMessage = new Message(); _messageRepositoryMock.Setup(x => x.GetLastReadMessageAsync( It.Is <Guid>(memberId => memberId.Equals(member.Id)), It.Is <Guid>(channelId => channelId.Equals(allowedChannel.Id)))) .ReturnsAsync(lastReadMessage) .Verifiable(); var channelSummaryResponse = new ChannelSummaryResponse(); _domainModelsMapperMock.Setup(x => x.MapToChannelSummaryResponse( It.Is <Channel>(channel => channel.Equals(allowedChannel)), It.Is <ChannelMember>(channelMember => channelMember.Equals(allowedChannelMember)), It.Is <Message>(message => message.Equals(lastReadMessage)))) .Returns(channelSummaryResponse) .Verifiable(); var messages = new List <Message>(); _messageRepositoryMock.Setup(x => x.GetAllChannelMessagesWithOwnersAsync(It.IsAny <Guid>())) .ReturnsAsync(messages) .Verifiable(); // Act var result = await _channelService.GetChannelSummaryAsync(saasUserId, allowedChannel.Id); // Assert VerifyMocks(); result.Should().BeEquivalentTo(channelSummaryResponse); }
async Task IChannelNotificationHub.OnLeaveChannel(MemberSummary member, ChannelSummaryResponse channel) { var clientIds = await GetChannelClientsAsync(new ChannelRequest(member.SaasUserId, channel.Id)); var senderClients = await _memberService.GetMemberClientsAsync(member.Id); clientIds.AddRange(senderClients.Select(x => x.ClientConnectionId)); // Tell the people in this room that you've leaved await Clients.Clients(clientIds).SendAsync(HubEvents.MemberLeft, member, channel?.Id); }
async Task IChannelNotificationHub.OnAddChannel(MemberSummary member, ChannelSummaryResponse channel, string clientConnectionId) { var getClientsExceptCallerRequest = new ChannelRequest(member.SaasUserId, channel.Id) { ClientConnectionId = clientConnectionId }; var clientIds = await GetChannelClientsExceptCallerAsync(getClientsExceptCallerRequest, clientConnectionId); // Tell the people in this room that you've joined await Clients.Clients(clientIds).SendAsync(HubEvents.ChannelCreated, channel); }
public async Task ShouldCreatePrivateChannelWithPhoto_AndShouldAddAllowedMembers() { // Arrange var allowedMember = new Member { SaasUserId = "1AB5626B-B311-4862-A0F1-AFD21D9F421B" }; var request = new CreateChannelRequest("864EB62D-D833-47FA-8A88-DDBFE76AE6A7", "channel name", ChannelType.Private) { AllowedMembers = new List <string> { allowedMember.SaasUserId }, Description = "Description", PhotoUrl = "PhotoUrl", WelcomeMessage = "WelcomeMessage" }; var member = new Member { Id = new Guid("85B1E28A-3E29-48C5-B85B-1563EEB60742") }; _memberRepositoryMock.SetupSequence(x => x.GetMemberBySaasUserIdAsync(It.IsAny <string>())) .ReturnsAsync(member) .ReturnsAsync(allowedMember); var cloudPhotoUrl = "cloudPhotoUrl"; _cloudImageProviderMock.Setup(x => x.CopyImageToDestinationContainerAsync(It.Is <string>(photoUrl => photoUrl.Equals(request.PhotoUrl)))) .ReturnsAsync(cloudPhotoUrl) .Verifiable(); var utcNow = DateTimeOffset.UtcNow; _dateTimeProviderMock.Setup(x => x.GetUtcNow()) .Returns(utcNow) .Verifiable(); Channel channelToAdd = null; _channelRepositoryMock.Setup(x => x.AddChannelAsync(It.IsAny <Channel>())) .Callback <Channel>(x => channelToAdd = x) .Returns(Task.CompletedTask) .Verifiable(); var channelMembersToAdd = new List <ChannelMember>(); _channelMemberRepositoryMock.Setup(x => x.AddChannelMemberAsync(It.IsAny <ChannelMember>())) .Callback <ChannelMember>(x => channelMembersToAdd.Add(x)) .Returns(Task.CompletedTask) .Verifiable(); _memberRepositoryMock.Setup(x => x.GetMemberByIdAsync(It.IsAny <Guid>())) .ReturnsAsync(allowedMember) .Verifiable(); _channelRepositoryMock.Setup(x => x.IncrementChannelMembersCountAsync(It.IsAny <Guid>())) .Returns(Task.CompletedTask) .Verifiable(); var addedChannel = new Channel(); _channelRepositoryMock.Setup(x => x.GetChannelWithCreatorAsync(It.IsAny <Guid>())) .ReturnsAsync(addedChannel) .Verifiable(); var channelSummaryResponse = new ChannelSummaryResponse(); _domainModelsMapperMock.Setup(x => x.MapToChannelSummaryResponse( It.Is <Channel>(channel => channel.Equals(addedChannel)), It.IsAny <ChannelMember>(), It.IsAny <Message>())) .Returns(channelSummaryResponse) .Verifiable(); // Act var result = await _channelService.CreateChannelAsync(request); // Assert VerifyMocks(); channelToAdd.Created.Should().Be(utcNow); channelToAdd.Name.Should().Be(request.Name); channelToAdd.Type.Should().Be(request.Type); channelToAdd.CreatorId.Should().Be(member.Id); channelToAdd.Creator.Should().Be(member); channelToAdd.PhotoUrl.Should().Be(cloudPhotoUrl); channelToAdd.Description.Should().Be(request.Description); channelToAdd.WelcomeMessage.Should().Be(request.WelcomeMessage); channelToAdd.Members.Should().BeEquivalentTo(channelMembersToAdd); _channelMemberRepositoryMock.Verify(prov => prov.AddChannelMemberAsync(It.IsAny <ChannelMember>()), Times.Exactly(2)); _channelRepositoryMock.Verify(prov => prov.IncrementChannelMembersCountAsync(It.Is <Guid>(channelId => channelId.Equals(channelToAdd.Id))), Times.Exactly(2)); _memberRepositoryMock.Verify(prov => prov.GetMemberBySaasUserIdAsync(It.IsAny <string>()), Times.Exactly(1)); result.Should().BeEquivalentTo(channelSummaryResponse); }
public ChannelIconChangedLocalizationVisitor(ChannelSummaryResponse channel) { _channel = channel; }
public async Task ShouldCreatePublicChannelWithoutPhoto_AndShouldNotAddAllowedMembers() { // Arrange var request = new CreateChannelRequest("864EB62D-D833-47FA-8A88-DDBFE76AE6A7", "channel name", ChannelType.Public) { AllowedMembers = new List <string> { "A53F4EA4-B5BD-494A-8D5F-4B4B172A25F8" } }; var member = new Member { Id = new Guid("85B1E28A-3E29-48C5-B85B-1563EEB60742") }; _memberRepositoryMock.Setup(x => x.GetMemberBySaasUserIdAsync(It.Is <string>(saasUserId => saasUserId.Equals(request.SaasUserId)))) .ReturnsAsync(member) .Verifiable(); _cloudImageProviderMock.Setup(x => x.CopyImageToDestinationContainerAsync(It.IsAny <string>())) .ReturnsAsync((string)null) .Verifiable(); var utcNow = DateTimeOffset.UtcNow; _dateTimeProviderMock.Setup(x => x.GetUtcNow()) .Returns(utcNow) .Verifiable(); Channel channelToAdd = null; _channelRepositoryMock.Setup(x => x.AddChannelAsync(It.IsAny <Channel>())) .Callback <Channel>(x => channelToAdd = x) .Returns(Task.CompletedTask) .Verifiable(); ChannelMember channelMemberToAdd = null; _channelMemberRepositoryMock.Setup(x => x.AddChannelMemberAsync(It.IsAny <ChannelMember>())) .Callback <ChannelMember>(x => channelMemberToAdd = x) .Returns(Task.CompletedTask) .Verifiable(); _channelRepositoryMock.Setup(x => x.IncrementChannelMembersCountAsync(It.IsAny <Guid>())) .Returns(Task.CompletedTask) .Verifiable(); var addedChannel = new Channel(); _channelRepositoryMock.Setup(x => x.GetChannelWithCreatorAsync(It.IsAny <Guid>())) .ReturnsAsync(addedChannel) .Verifiable(); var channelSummaryResponse = new ChannelSummaryResponse(); _domainModelsMapperMock.Setup(x => x.MapToChannelSummaryResponse( It.Is <Channel>(channel => channel.Equals(addedChannel)), It.IsAny <ChannelMember>(), It.IsAny <Message>())) .Returns(channelSummaryResponse) .Verifiable(); // Act var result = await _channelService.CreateChannelAsync(request); // Assert VerifyMocks(); channelToAdd.Created.Should().Be(utcNow); channelToAdd.Name.Should().Be(request.Name); channelToAdd.Type.Should().Be(request.Type); channelToAdd.CreatorId.Should().Be(member.Id); channelToAdd.Creator.Should().Be(member); channelToAdd.PhotoUrl.Should().Be(null); channelToAdd.Members.Should().BeEquivalentTo(new List <ChannelMember> { channelMemberToAdd }); _channelMemberRepositoryMock.Verify(prov => prov.AddChannelMemberAsync(It.IsAny <ChannelMember>()), Times.Once); _channelRepositoryMock.Verify(prov => prov.IncrementChannelMembersCountAsync(It.Is <Guid>(channelId => channelId.Equals(channelToAdd.Id))), Times.Once); result.Should().BeEquivalentTo(channelSummaryResponse); }
public async Task Step3_ShouldAddUpdateDeleteMessage() { // Add message // Arrange var addMessageRequest = new AddMessageRequest { ChannelId = _testChannel.Id, Body = "test_body", ImageUrl = string.Empty, RequestId = "82EEC70D-D808-492C-98E3-6A5B47276990", Type = MessageType.Default }; // Subscribe event MessageResponse addedMessageResponse = null; void OnMessageAdded(MessageResponse response) { addedMessageResponse = response; } _userSignalRClient.MessageAdded += OnMessageAdded; // Act var messageResponse = await _adminSignalRClient.AddMessageAsync(addMessageRequest); // Unsubscribe events _userSignalRClient.MessageAdded -= OnMessageAdded; // Assert addedMessageResponse.Should().NotBeNull(); addedMessageResponse.Should().BeEquivalentTo(messageResponse); // Update message // Arrange var updatedMessageRequest = new UpdateMessageRequest { Body = $"new_{messageResponse.Body}", RequestId = "8992BCD7-630E-4F22-92BF-1A3AA4B7D11E", MessageId = messageResponse.Id }; // Subscribe event void OnMessageUpdated(MessageResponse response) { messageResponse = response; } _userSignalRClient.MessageUpdated += OnMessageUpdated; // Act await _adminSignalRClient.UpdateMessageAsync(updatedMessageRequest); // Unsubscribe events _userSignalRClient.MessageUpdated -= OnMessageUpdated; // Assert messageResponse.Should().NotBeNull(); messageResponse.Body.Should().BeEquivalentTo(updatedMessageRequest.Body); // Delete message // Arrange var deleteMessageRequest = new DeleteMessageRequest { RequestId = "47AF87B2-C39F-4814-AD62-3BC078E2A9BA", MessageId = messageResponse.Id }; // Subscribe event ChannelSummaryResponse channelSummaryResponse = null; Guid messageId = new Guid(); void OnMessageDeleted(Guid id, ChannelSummaryResponse response) { messageId = id; channelSummaryResponse = response; } _userSignalRClient.MessageDeleted += OnMessageDeleted; // Act await _adminSignalRClient.DeleteMessageAsync(deleteMessageRequest); // Unsubscribe events _userSignalRClient.MessageDeleted -= OnMessageDeleted; // Assert messageId.Should().Be(messageResponse.Id); channelSummaryResponse.UnreadMessagesCount.Should().Be(0); }
public async Task OnDeleteMessage(ChannelSummaryResponse channelSummary, MessageResponse message) { var clientIds = await GetChannelClientConnectionIdsAsync(message.ChannelId); await HubContext.Clients.Clients(clientIds).SendAsync(HubEvents.MessageDeleted, message.Id, channelSummary); }