public async Task <IActionResult> SendMessage([FromForm] PostMessageViewModel postMessage) { try { var id = User.Claims.First(c => c.Type == "Id").Value; if (blackListService.HasUserInBlock(id, postMessage.ReceiverId)) { return(BadRequest("Unlock this user to send messages")); } if (blackListService.HasUserInBlock(postMessage.ReceiverId, id)) { return(BadRequest("This user reseieved access to sendind messages")); } if (postMessage.Attachment == null && postMessage.Text == null) { return(BadRequest("You cannot send the empty message!")); } if (postMessage.Attachment != null) { foreach (var file in postMessage.Attachment) { if (file.ContentType == "image/jpg" || file.ContentType == "image/jpeg" || file.ContentType == "audio/mp3" || file.ContentType == "video/mp4" || file.ContentType == "audio/mpeg") { if ((file.ContentType == "image/jpg" || file.ContentType == "image/jpeg") && file.Length >= 2097152) { return(BadRequest("Unsupported file length. JPG/JPEG must be before 2MB ")); } else if ((file.ContentType == "audio/mp3" || file.ContentType == "audio/mpeg") && file.Length >= 10484760) { return(BadRequest("Unsupported file length. Audio files must be before 10MB ")); } else if (file.ContentType == "video/mp4" && file.Length >= 31457280) { return(BadRequest("Unsupported file length. Video files must be before 10MB ")); } } else { return(BadRequest("Unsupported file type. Files must be only of 'jpg/jpeg', 'mp3', 'mp4' formats! ")); } } } UserIds receiver, caller; chatHub.FindCallerReceiverByIds(postMessage.ReceiverId, id, out caller, out receiver); bool dialogExists = await chatService.IsExistDialog(caller.userId, postMessage.ReceiverId); if (dialogExists) { var message = await chatService.AddNewMessage(caller.userId, postMessage, DateTime.Now); if (chatService.IsOnline(postMessage.ReceiverId)) { await hubContext.Clients.Client(receiver.connectionId).SendAsync("Send", message, caller.userId); await hubContext.Clients.Clients(caller.connectionId).SendAsync("SendMyself", message); } else { await hubContext.Clients.Clients(caller.connectionId).SendAsync("SendMyself", message); } return(Ok(message)); } else { await chatService.CreateNewDialogAsync(caller.userId, postMessage.ReceiverId); var message = await chatService.AddNewMessage(caller.userId, postMessage, DateTime.Now); if (receiver != null) { if (chatService.IsOnline(postMessage.ReceiverId)) { await hubContext.Clients.Client(receiver.connectionId).SendAsync("Send", message, caller.userId); await hubContext.Clients.Clients(caller.connectionId).SendAsync("SendMyself", message); } else { await hubContext.Clients.Clients(caller.connectionId).SendAsync("SendMyself", message); } } return(Ok(message)); } } catch (Exception ex) { throw ex; } }
public async Task SendMessage(string message, string chatRoom) { var chatMessage = _chatService.AddNewMessage("Channa", message, chatRoom); await Clients.All.SendAsync("ReceiveMessage", "Channa", message); }