Пример #1
0
        public async Task <ActionResult> TryAddUserToThread(string threadId, [FromBody] ContosoMemberModel user)
        {
            try
            {
                var              moderator        = _store.Store[threadId];
                var              userCredential   = new CommunicationUserCredential(moderator.token);
                ChatClient       chatClient       = new ChatClient(new Uri(_chatGatewayUrl), userCredential);
                ChatThread       chatThread       = chatClient.GetChatThread(threadId);
                ChatThreadClient chatThreadClient = chatClient.GetChatThreadClient(threadId);

                var chatThreadMember = new ChatThreadMember(new CommunicationUser(user.Id));
                chatThreadMember.DisplayName      = user.DisplayName;
                chatThreadMember.ShareHistoryTime = chatThread.CreatedOn;
                List <ChatThreadMember> chatThreadMembers = new List <ChatThreadMember>
                {
                    chatThreadMember
                };
                try
                {
                    var response = await chatThreadClient.AddMembersAsync(chatThreadMembers);

                    return(StatusCode(response.Status));
                }
                catch (RequestFailedException e)
                {
                    Console.WriteLine($"Unexpected error occurred while adding user from thread: {e}");
                    return(StatusCode(e.Status));
                }
            }
            catch (Exception e)
            {
                Console.WriteLine($"Unexpected error occurred while adding user from thread: {e}");
            }
            return(Ok());
        }