public async Task <IActionResult> CreateMessage(int userId, [FromBody] MessageForCreationDto messageForCreationDto) { User sender = await _repo.GetUser(userId); if (sender.Id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } messageForCreationDto.SenderId = userId; User recipient = await _repo.GetUser(messageForCreationDto.RecipientId); if (recipient == null) { return(BadRequest("No se encontrĂ³ el usuario")); } Message message = _mapper.Map <Message>(messageForCreationDto); _repo.Add(message); if (await _repo.SaveAll()) { MessageToReturnDto messageToReturn = _mapper.Map <MessageToReturnDto>(message); return(CreatedAtRoute("GetMessage", new { userId, id = message.Id }, messageToReturn)); } throw new Exception("mensaje fallido al guardar"); }
public async Task BroadcastNewMessage(MessageToReturnDto data) { foreach (var connectionId in _connections.GetConnections(data.RecipientId.ToString())) { await Clients.Client(connectionId).SendAsync("newMessage", data); } }
public async Task <IActionResult> CreateMessage(int userId, MessageForCreationDto messageForCreationDto) { User sender = await _repo.GetUser(userId); if (sender.Id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } messageForCreationDto.SenderId = userId; User recipient = await _repo.GetUser(messageForCreationDto.RecipientId); if (recipient == null) { return(BadRequest("Could not find user")); } Message message = _mapper.Map <Message>(messageForCreationDto); _repo.Add(message); if (await _repo.SaveAll()) { MessageToReturnDto messageToReturn = _mapper.Map <MessageToReturnDto>(message); return(CreatedAtRoute("GetMessage", new { id = message.Id }, messageToReturn)); } throw new Exception("Creating the message failed on save"); }
public async Task <IActionResult> CreateMessage(int userId, MessageForCreationDto messageForCreationDto) { if (userId != Int32.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var recipientId = await _repo.GetUser(messageForCreationDto.RecipientId); if (recipientId == null) { return(BadRequest("could not find user")); } var sender = await _repo.GetUser(messageForCreationDto.SenderId = userId); //automapper automatically maps recipient and sender information // ,since they are in memory which we are getting using getUser function var message = _mapper.Map <Message>(messageForCreationDto); _repo.Add(message); if (await _repo.SaveAll()) { MessageToReturnDto messageToReturn = _mapper.Map <MessageToReturnDto>(message); return(CreatedAtRoute("GetMessage", new { userId = userId, id = message.Id }, messageToReturn)); } return(BadRequest("we are not able to deliver message")); }
public void SendPrivate(string message) { // message format: /private(receiverName) Lorem ipsum... string[] split = message.Split(')'); string receiver = split[0].Split('(')[1]; // Who is the sender; var sender = _Connections.Where(u => u.Id == Identity()).First(); message = Regex.Replace(message, @"\/private\(.*?\)", string.Empty).Trim(); var receiverUser = context.Users.FirstOrDefault(user => user.UserName == receiver); // Build the message TODO MessageToReturnDto messageViewModel = new MessageToReturnDto(); foreach (var cc in _connectionsMapping.GetConnections(receiverUser.Id)) { Clients.Client(cc).NewMessage(messageViewModel); } Clients.Caller.NewMessage(messageViewModel); }
public async Task SendMessageAsync(MessageToReturnDto messageToSend) { await _hubContext.Clients.All.ReceiveMessage(JsonConvert.SerializeObject(messageToSend)); }
public async Task SendMessage(MessageToReturnDto messageToSend, string id) { await _hubContext.Clients.User(id).ReceiveMessage(messageToSend); }
public async Task SendMessage(MessageToReturnDto message) { await _hubContext.Clients.All.ReceiveMessage(message); }
public void Kicked(MessageToReturnDto message) { }
public async Task Send(MessageToReturnDto msg) { await Clients.Others.SendAsync("new_message", msg); }
public async Task SendMessage(int id, MessageToReturnDto messageToReturnDto) { await Clients.All.SendAsync("Send", id, messageToReturnDto); }