public void ShouldThrowIfMemberIsNotMessageOwner()
        {
            // Arrange
            _messageRepositoryMock.Setup(x => x.GetMessageWithOwnerAndForwardMessageAsync(It.IsAny <Guid>()))
            .ReturnsAsync(new Message {
                OwnerId = new Guid("F19AD922-B0DB-4686-8CB4-F51902800CAE")
            })
            .Verifiable();
            _memberRepositoryMock.Setup(x => x.GetMemberBySaasUserIdAsync(It.IsAny <string>()))
            .ReturnsAsync(new Member {
                Id = new Guid("ABF2CA08-5374-4CED-BE87-6EA93A8B90DA")
            })
            .Verifiable();

            var request = new ArchiveMessageRequest("864EB62D-D833-47FA-8A88-DDBFE76AE6A7", new Guid("A455F139-09E6-4EF5-B55A-D4C94D05DFDE"));

            // Act
            Func <Task> act = async() => { await _messageService.ArchiveMessageAsync(request); };

            // Assert
            act.Should().Throw <NetKitChatAccessForbiddenException>()
            .And.Message.Should().Be($"Unable to delete message. Message {nameof(request.MessageId)}:{request.MessageId} owner required.");

            VerifyMocks();
        }
Example #2
0
        public async Task ArchiveMessageAsync(ArchiveMessageRequest request)
        {
            var message = await UnitOfWork.MessageRepository.GetMessageWithOwnerAndForwardMessageAsync(request.MessageId);

            if (message == null)
            {
                throw new NetKitChatNotFoundException($"Unable to delete message. Message {nameof(request.MessageId)}:{request.MessageId} is not found.");
            }

            var member = await UnitOfWork.MemberRepository.GetMemberBySaasUserIdAsync(request.SaasUserId);

            if (member == null)
            {
                throw new NetKitChatNotFoundException($"Unable to delete message. Member {nameof(request.SaasUserId)}:{request.SaasUserId} is not found.");
            }

            if (member.Id != message.OwnerId)
            {
                throw new NetKitChatAccessForbiddenException($"Unable to delete message. Message {nameof(request.MessageId)}:{request.MessageId} owner required.");
            }

            await UnitOfWork.ExecuteTransactionAsync(async() =>
            {
                //TODO: [ek] Get previous message for direct channel
                var previousMessage = await UnitOfWork.MessageRepository.GetPreviousMessageAsync(message.ChannelId, message.Created);
                //TODO: [ek]: Save last read message for direct members
                await UnitOfWork.ChannelMemberRepository.UpdateLastReadMessageAsync(message.Id, previousMessage?.Id);

                await UnitOfWork.MessageRepository.ArchiveMessageAsync(message.Id);
            });
        }
Example #3
0
 public async Task DeleteMessageAsync(SignalRRequest.Message.DeleteMessageRequest request)
 {
     await ValidateAndExecuteAsync(request, new DeleteMessageRequestValidator(), new TaskReference(async() =>
     {
         var archiveMessageRequest = new ArchiveMessageRequest(Context.GetSaasUserId(), request.MessageId);
         await _messageSocketService.ArchiveMessageAsync(archiveMessageRequest);
     }),
                                   request.RequestId);
 }
        public async Task ArchiveMessageAsync(ArchiveMessageRequest request)
        {
            var message = await _messageService.GetMessageByIdAsync(request.MessageId);

            await _messageService.ArchiveMessageAsync(request);

            var channelSummary = await _channelService.GetChannelSummaryAsync(request.SaasUserId, message.ChannelId);

            await _messageNotificationService.OnDeleteMessage(channelSummary, message);
        }
        public async Task ShouldDeleteForwardMessageWithoutAttachmentsAndWithoutPreviousMessage()
        {
            // Arrange
            var request = new ArchiveMessageRequest("864EB62D-D833-47FA-8A88-DDBFE76AE6A7", new Guid("A455F139-09E6-4EF5-B55A-D4C94D05DFDE"));

            var messageOwnerId = new Guid("F19AD922-B0DB-4686-8CB4-F51902800CAE");
            var message        = new Message
            {
                Id               = request.MessageId,
                OwnerId          = messageOwnerId,
                Type             = MessageType.Forward,
                ForwardMessageId = new Guid("C7EFF82F-56FF-4966-9040-E79C90F118A7"),
                ChannelId        = new Guid("FE0ED6EF-588A-4844-B754-0ADAC9D5A217"),
                Created          = DateTimeOffset.UtcNow
            };

            _messageRepositoryMock.Setup(x => x.GetMessageWithOwnerAndForwardMessageAsync(It.Is <Guid>(messageId => messageId.Equals(request.MessageId))))
            .ReturnsAsync(message)
            .Verifiable();


            var previousMessage = new Message();

            _messageRepositoryMock.Setup(x => x.GetPreviousMessageAsync(
                                             It.Is <Guid?>(channelId => channelId.Equals(message.ChannelId)),
                                             It.Is <DateTimeOffset>(d => d.Equals(message.Created))))
            .ReturnsAsync(previousMessage)
            .Verifiable();

            _messageRepositoryMock.Setup(x => x.ArchiveMessageAsync(It.Is <Guid>(messageId => messageId.Equals(request.MessageId))))
            .Returns(Task.CompletedTask)
            .Verifiable();

            _memberRepositoryMock.Setup(x => x.GetMemberBySaasUserIdAsync(It.Is <string>(saasUserId => saasUserId.Equals(request.SaasUserId))))
            .ReturnsAsync(new Member {
                Id = messageOwnerId
            })
            .Verifiable();

            _channelMemberRepositoryMock.Setup(x => x.UpdateLastReadMessageAsync(
                                                   It.Is <Guid>(old => old.Equals(message.Id)),
                                                   It.Is <Guid?>(current => current.Equals(previousMessage.Id))))
            .Returns(Task.CompletedTask)
            .Verifiable();

            // Act
            await _messageService.ArchiveMessageAsync(request);

            // Assert
            VerifyMocks();
        }
        public void ShouldThrowIfMessageDoesNotExist()
        {
            // Arrange
            _messageRepositoryMock.Setup(x => x.GetMessageWithOwnerAndForwardMessageAsync(It.IsAny <Guid>()))
            .ReturnsAsync((Message)null)
            .Verifiable();

            var request = new ArchiveMessageRequest("864EB62D-D833-47FA-8A88-DDBFE76AE6A7", new Guid("A455F139-09E6-4EF5-B55A-D4C94D05DFDE"));

            // Act
            Func <Task> act = async() => { await _messageService.ArchiveMessageAsync(request); };

            // Assert
            act.Should().Throw <NetKitChatNotFoundException>()
            .And.Message.Should().Be($"Unable to delete message. Message {nameof(request.MessageId)}:{request.MessageId} is not found.");

            VerifyMocks();
        }