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."); }
public async Task <IActionResult> SendMessage([FromBody] CreateMessageDto dto) { var username = User.GetUsername(); var result = await _messageLogic.AddMessage(dto, username); if (result != null) { return(Ok(result)); } return(BadRequest("The message was not sent. Please try again.")); }
public OperationResultInfo AddMessage(MessageReceiveModel messageModel) { try { var callBackMessage = _messageLogic.AddMessage(messageModel); return(new OperationResultInfo() { ErrorInfo = string.Empty, OperationResult = OperationsResults.Successfully, ToListener = ListenerType.ChatsMessagesListener, JsonData = callBackMessage }); } catch (Exception ex) { return(new OperationResultInfo() { ErrorInfo = ex.Message, OperationResult = OperationsResults.Unsuccessfully, ToListener = ListenerType.ChatsMessagesListener }); } }