public async Task <IActionResult> SendMessage([FromForm] MessageForAddDto model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } //List<string> ReceiverConnectionids = _connections.GetConnections(model.MessageToUserId.ToString()).ToList<string>(); _response = await _repo.SendMessage(model); var userToIds = new List <string>() { model.MessageToUserId.ToString() }; var response = await _repo.GetChatMessages(userToIds, true); if (_response.Success) { var lastMessageStr = JsonConvert.SerializeObject(response.Data); var lastMessage = JsonConvert.DeserializeObject <GroupMessageForListByTimeDto>(lastMessageStr); var ToReturn = new GroupSignalRMessageForListDto { Id = lastMessage.Messages[0].Id, Type = lastMessage.Messages[0].Type, DateTimeToDisplay = lastMessage.Messages[0].TimeToDisplay, TimeToDisplay = lastMessage.Messages[0].TimeToDisplay, Comment = lastMessage.Messages[0].Comment, MessageFromUserId = lastMessage.Messages[0].MessageFromUserId, MessageFromUser = lastMessage.Messages[0].MessageFromUser, MessageToUserIdsStr = lastMessage.Messages[0].MessageToUserIdsStr, GroupId = 0, Attachment = lastMessage.Messages[0].Attachment, FileName = lastMessage.Messages[0].FileName, FileType = lastMessage.Messages[0].FileType, //MessageToUser = lastMessage.Messages[0].MessageToUser, }; // List<MessageForListByTimeDto> collection = new List<MessageForListByTimeDto>((IEnumerable<MessageForListByTimeDto>)lastMessage.Data); await _hubContext.Clients.All.SendAsync("MessageNotificationAlert", ToReturn); //_hubContext.Clients.Clients(ReceiverConnectionids) } return(Ok(_response)); }
public async Task <ServiceResponse <object> > SendMessage(MessageForAddDto model) { var ToAdd = new Message { Comment = model.Comment, MessageToUserId = model.MessageToUserId, IsRead = false, CreatedDateTime = DateTime.UtcNow, MessageFromUserId = _LoggedIn_UserID, MessageReplyId = model.MessageReplyId, }; await _context.Messages.AddAsync(ToAdd); await _context.SaveChangesAsync(); if (model.files != null && model.files.Count() > 0) { for (int i = 0; i < model.files.Count(); i++) { var dbPath = _fileRepo.SaveFile(model.files[i]); var attachment = new ChatMessageAttachment { MessageId = ToAdd.Id, AttachmentPath = dbPath, FileName = model.files[i].FileName != "blob" ? model.files[i].FileName : "", FileType = model.files[i].FileName != "blob" ? "FileAttachment" : "" }; await _context.ChatMessageAttachments.AddAsync(attachment); await _context.SaveChangesAsync(); } } _serviceResponse.Success = true; _serviceResponse.Message = CustomMessage.Added; return(_serviceResponse); }