public async Task <IActionResult> SendChatMessage(PostMessageToChatDto messageData) { var userId = User.FindFirst("UserId")?.Value; if (string.IsNullOrEmpty(userId)) { return(BadRequest()); } messageData.UserId = userId; var response = await _messagesManager.ProcessChatMessage(messageData); if (!response.Success) { return(BadRequest(response.ErrorMessage)); } var groupName = messageData.ChatId.ToString(); var senderId = messageData.UserId; await _hub.Clients .GroupExcept(groupName, senderId) .SendAsync("receivemessage", response.Message); return(Ok(response.Message)); }
/// <summary> /// Process message /// to existing chat. /// /// </summary> /// <param name="messageData"></param> /// <returns></returns> public async Task <MessageResponse> ProcessChatMessage(PostMessageToChatDto messageData) { var messageOwner = await _userManager.FindByIdAsync(messageData.UserId); if (messageOwner == null) { return(MessageResponse.Unsuccessful("Error finding message owner.")); } messageData.ApplicationUser = messageOwner; var message = _mapper.Map <Message>(messageData); var messageSaved = await _repository.SaveMessage(message); if (!messageSaved) { return(MessageResponse.Unsuccessful("Error in saving message.")); } var chatResponse = await _chatsManager.RefreshChatActivity(messageData.ChatId); if (!chatResponse.Success) { return(MessageResponse.Unsuccessful(chatResponse.ErrorMessage)); } var messageDetailsDto = _mapper.Map <MessageDetailsDto>(message); messageDetailsDto.ApplicationUser = messageOwner.UserName; return(MessageResponse.Successfull(messageDetailsDto)); }