public async Task <IActionResult> CreateConversation(CreateConversationRequest createConversationRequest) { if (!_authService.AuthorizeSelf(Request.Headers["Authorization"], createConversationRequest.SenderId)) { return(Unauthorized()); } var duplicateConversationId = await _conversationValidator.ValidateConversationDuplicate(createConversationRequest); if (duplicateConversationId != Guid.Empty) { return(Conflict(duplicateConversationId)); } var conversationId = await _conversationsService.CreateConversationWithParticipants( createConversationRequest.SenderId, createConversationRequest.ReceiverId); if (!string.IsNullOrEmpty(createConversationRequest.Content)) { await _messagesService.CreateMessage(new Message(createConversationRequest, conversationId)); } return(Ok(conversationId)); }
public async Task Send(int chatId, string message) { MessageDto msg = await _messagesService.CreateMessage(Context.User.GetUserId(), chatId, message); await Clients.Group(chatId.ToString()).SendAsync("newMessage", msg); if (userGroups.ContainsKey(chatId)) { await Clients.AllExcept(userGroups[chatId]).SendAsync("newUnread", chatId); } }
public async Task <IActionResult> CreateMessage([FromBody] CreateMessageRequest request) { var tkn = Request.Headers["Authorization"]; if (!Guid.TryParse(_authService.GetUserIdFromToken(tkn), out var userId) || !(_authService.AuthorizeSelf(tkn, request.SenderId)) || !await InConversation(userId, request.ConversationId)) { return(Unauthorized()); } await _messagesService.CreateMessage(new Message(request)); await _messageHub.Clients.All.SendAsync($"newmessage/{request.ConversationId}"); return(Ok()); }