예제 #1
0
        public async Task <bool> SendPrivateMessage(string recipientName, string message)
        {
            string identityName = Context.User.Identity.Name;

            if (recipientName != identityName && !String.IsNullOrEmpty(recipientName))
            {
                if (message.Length > _settings.MaxMessageLength)
                {
                    throw new HubException(String.Format("Message is too long", _settings.MaxMessageLength));
                }

                if (message.Length <= 0)
                {
                    throw new HubException(String.Format("Message is too low", _settings.MaxMessageLength));
                }


                User currentUser = _chatRepo.GetUserByName(identityName);
                User recipient   = _chatRepo.GetUserByName(recipientName);

                if (recipient != null)
                {
                    Dialog dialog = _chatRepo.HasDialog(identityName, recipientName);

                    if (dialog == null)
                    {
                        dialog = await _chatService.CreateDialog(currentUser, recipient, message);
                    }


                    if (currentUser.Status != UserStatus.Online)
                    {
                        await SendStatus(UserStatus.Online);
                    }


                    await Clients.User(recipientName).SendAsync("RecieveMessage", Context.User.Identity.FullName(), identityName, message);

                    await _chatService.AddMessage(currentUser, dialog, message);

                    return(true);
                }
                return(false);
            }
            return(false);
        }