Пример #1
0
        public async Task CreateMessage(CreateChatMessageDTO chatMessageDTO)
        {
            string userEmail = Context.UserIdentifier;
            User   user      = await _userManager.FindByEmailAsync(userEmail);

            CreateChatMessage chatMessage = new CreateChatMessage
            {
                User          = user,
                Message       = chatMessageDTO.Message,
                ChatGroupUuid = chatMessageDTO.ChatGroupUuid
            };

            ChatMessageResponse chatMessageResponse = await _chatMessageService.CreateChatMessageAsync(chatMessage);

            await SendMessage(chatMessageResponse);
        }
Пример #2
0
        public async Task <ActionResult <ChatMessageResponse> > CreateChatMessage([FromBody] CreateChatMessageDTO chatMessageDTO)
        {
            string userEmail = User.FindFirstValue(ClaimTypes.Email);
            User   user      = await _userManager.FindByEmailAsync(userEmail);

            if (user == null)
            {
                return(NotFound(new ChatMessageResponse {
                    Success = false, ErrorMessage = "User does not exist"
                }));
            }

            ChatGroupUser chatGroupUser = await _chatGroupUserService.GetByUserAndChatGroupAsync(
                user.Id, chatMessageDTO.ChatGroupUuid);

            // Check if user is authorized to create chat messages for the chat group
            AuthorizationResult isAuthorized = await _authorizationService.AuthorizeAsync(
                User,
                chatGroupUser,
                Operations.Create);

            if (!isAuthorized.Succeeded)
            {
                return(Forbid());
            }

            CreateChatMessage chatMessage = new CreateChatMessage
            {
                User          = user,
                Message       = chatMessageDTO.Message,
                ChatGroupUuid = chatMessageDTO.ChatGroupUuid
            };
            ChatMessageResponse createRes = await _chatMessageService.CreateChatMessageAsync(chatMessage);

            // Broadcast message to only clients who are members of the chat group
            await _chatHubContext.Clients.Group(chatMessageDTO.ChatGroupUuid.ToString()).SendAsync("ReceiveMessage", createRes);

            return(createRes);
        }