Beispiel #1
0
        public async Task SendMessage(CreateMessageDto dto)
        {
            var username = Context.User.GetUsername();
            var user     = await _userRepo.GetUserByUsername(username);

            var result = await _messageLogic.AddMessage(dto, username);

            var groupName = GetGroupName(username, dto.RecipientUsername);
            var group     = await _messageLogic.GetMessageGroup(groupName);

            if (group.Connections.Any(x => x.Username == dto.RecipientUsername))
            {
                result.MessageRead = DateTime.Now;
            }

            else
            {
                var connections = await _tracker.GetConnectionsForUser(dto.RecipientUsername);

                if (connections != null)
                {
                    await _presenceHub.Clients.Clients(connections).SendAsync("NewMessageReceived",
                                                                              new { userName = result.SenderUsername, knownAs = user.KnownAs });
                }
            }

            if (result != null)
            {
                await Clients.Group(groupName).SendAsync("NewMessage", result);
            }
            throw new HubException("The message was not sent. Please try again.");
        }
Beispiel #2
0
        public async Task SendMessage(CreateMessageDto createMessageDto)
        {
            var username = Context.User.GetUsername();

            if (username == createMessageDto.RecipientUsername.ToLower())
            {
                throw new HubException("You cannot send messages to yourself");
            }

            var sender = await _unitOfWork.UserRepository.GetUserByUsernameAsync(username);

            var recipient = await _unitOfWork.UserRepository.GetUserByUsernameAsync(createMessageDto.RecipientUsername);

            if (recipient == null)
            {
                throw new HubException("Not found user");
            }

            var message = new Message
            {
                Sender            = sender,
                Recipient         = recipient,
                SenderUsername    = sender.UserName,
                RecipientUsername = recipient.UserName,
                Content           = createMessageDto.Content
            };

            var groupName = GetGroupName(sender.UserName, recipient.UserName);

            var group = await _unitOfWork.MessageRepository.GetMessageGroup(groupName);

            if (group.Connections.Any(x => x.Username == recipient.UserName))
            {
                message.DateRead = DateTime.UtcNow;
            }
            else
            {
                var connections = await _tracker.GetConnectionsForUser(recipient.UserName);

                if (connections != null)
                {
                    await _presenceHub.Clients.Clients(connections).SendAsync("NewMessageReceived",
                                                                              new { username = sender.UserName, knownAs = sender.KnownAs });
                }
            }

            _unitOfWork.MessageRepository.AddMessage(message);

            if (await _unitOfWork.Complete())
            {
                await Clients.Group(groupName).SendAsync("NewMessage", _mapper.Map <MessageDto>(message));
            }
        }