Esempio n. 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());
        }
        /// <summary>
        /// AddUserToChatThread() - add a new member to an existing chat thread
        /// </summary>
        /// <returns></returns>
        private static async Task AddUserToChatThread()
        {
            Console.WriteLine("# Adding a user to a chat thread");

            Console.Write("Enter thread id: ");
            var threadId = Console.ReadLine();

            Console.Write("Enter user id: ");
            var userToAdd = Console.ReadLine();

            Console.Write("Enter display name: ");
            var userDisplayname = Console.ReadLine();

            Uri endpoint = new Uri("https://ACS-INSTANCE.communication.azure.com/");

            CommunicationUserCredential communicationUserCredential = new CommunicationUserCredential(token);
            ChatClient chatClient = new ChatClient(endpoint, communicationUserCredential);

            var chatThreadMember = new ChatThreadMember(new CommunicationUser(userToAdd))
            {
                DisplayName = userDisplayname
            };

            ChatThreadClient chatThreadClient = chatClient.GetChatThreadClient(threadId);

            await chatThreadClient.AddMembersAsync(members : new[] { chatThreadMember });

            Console.WriteLine($"User {userDisplayname} added to thread");
        }
Esempio n. 3
0
        public async Task GetAddRemoveMembersAsync()
        {
            CommunicationIdentityClient            communicationIdentityClient = new CommunicationIdentityClient(TestEnvironment.ConnectionString);
            Response <CommunicationUserIdentifier> threadMember1 = await communicationIdentityClient.CreateUserAsync();

            Response <CommunicationUserIdentifier> threadMember2 = await communicationIdentityClient.CreateUserAsync();

            Response <CommunicationUserIdentifier> threadMember3 = await communicationIdentityClient.CreateUserAsync();

            CommunicationUserToken communicationUserToken1 = await communicationIdentityClient.IssueTokenAsync(threadMember1.Value, new[] { CommunicationTokenScope.Chat });

            CommunicationUserToken communicationUserToken2 = await communicationIdentityClient.IssueTokenAsync(threadMember2.Value, new[] { CommunicationTokenScope.Chat });

            CommunicationUserToken communicationUserToken3 = await communicationIdentityClient.IssueTokenAsync(threadMember3.Value, new[] { CommunicationTokenScope.Chat });

            string userToken            = communicationUserToken1.Token;
            string endpoint             = TestEnvironment.ChatApiUrl();
            string theadCreatorMemberId = communicationUserToken1.User.Id;

            ChatClient chatClient = new ChatClient(
                new Uri(endpoint),
                new CommunicationTokenCredential(userToken));

            var chatThreadMember = new ChatThreadMember(new CommunicationUserIdentifier(theadCreatorMemberId))
            {
                DisplayName      = "UserDisplayName",
                ShareHistoryTime = DateTime.MinValue
            };
            ChatThreadClient chatThreadClient = await chatClient.CreateChatThreadAsync(topic : "Hello world!", members : new[] { chatThreadMember });

            string threadId = chatThreadClient.Id;

            #region Snippet:Azure_Communication_Chat_Tests_Samples_GetMembers
            AsyncPageable <ChatThreadMember> allMembers = chatThreadClient.GetMembersAsync();
            await foreach (ChatThreadMember member in allMembers)
            {
                Console.WriteLine($"{member.User.Id}:{member.DisplayName}:{member.ShareHistoryTime}");
            }
            #endregion Snippet:Azure_Communication_Chat_Tests_GetMembers

            var memberId1 = theadCreatorMemberId;
            var memberId2 = communicationUserToken2.User.Id;
            var memberId3 = communicationUserToken3.User.Id;

            #region Snippet:Azure_Communication_Chat_Tests_Samples_AddMembers
            var members = new[]
            {
                new ChatThreadMember(new CommunicationUserIdentifier(memberId1))
                {
                    DisplayName = "display name member 1"
                },
                new ChatThreadMember(new CommunicationUserIdentifier(memberId2))
                {
                    DisplayName = "display name member 2"
                },
                new ChatThreadMember(new CommunicationUserIdentifier(memberId3))
                {
                    DisplayName = "display name member 3"
                }
            };
            await chatThreadClient.AddMembersAsync(members);

            #endregion Snippet:Azure_Communication_Chat_Tests_Samples_AddMembers

            var memberId = memberId2;
            #region Snippet:Azure_Communication_Chat_Tests_Samples_RemoveMember
            await chatThreadClient.RemoveMemberAsync(new CommunicationUserIdentifier(memberId));

            #endregion Snippet:Azure_Communication_Chat_Tests_Samples_RemoveMember

            await chatClient.DeleteChatThreadAsync(threadId);
        }
Esempio n. 4
0
        public async Task E2E_ThreadCreateUpdateGetDelete_MemberAddUpdateRemove_MessageGetSendUpdate_NotificationTyping_ReadReceiptGetSend_Async()
        {
            //arr
            CommunicationUser           user1, user2, user3;
            string                      token1, token2, token3;
            CommunicationIdentityClient communicationIdentityClient = CreateInstrumentedCommunicationIdentityClient();

            (user1, token1) = await CreateUserAndTokenAsync(communicationIdentityClient);

            (user2, token2) = await CreateUserAndTokenAsync(communicationIdentityClient);

            (user3, token3) = await CreateUserAndTokenAsync(communicationIdentityClient);

            var topic                 = "Thread Async from C# sdk";
            var contentMessage        = "This is message 1";
            var updatedMessageContent = "This is message 1 updated";
            var displayNameMessage    = "DisplayName sender message 1";
            var updatedTopic          = "Updated topic - C# sdk";
            var members               = new List <ChatThreadMember>
            {
                new ChatThreadMember(user1),
                new ChatThreadMember(user2)
            };
            ChatClient chatClient  = CreateInstrumentedChatClient(token1);
            ChatClient chatClient2 = CreateInstrumentedChatClient(token2);

            //act
            ChatThreadClient chatThreadClient = await CreateInstrumentedChatThreadClientAsync(chatClient, topic, members);

            ChatThreadClient chatThreadClient2 = await CreateInstrumentedChatThreadClientAsync(chatClient, topic, members);

            await chatThreadClient.UpdateThreadAsync(updatedTopic);

            ChatThread chatThread = await chatClient.GetChatThreadAsync(chatThreadClient.Id);

            AsyncPageable <ChatThreadInfo> threads = chatClient.GetChatThreadsInfoAsync();
            var threadsCount = threads.ToEnumerableAsync().Result.Count;

            SendChatMessageResult sendChatMessageResult = await chatThreadClient.SendMessageAsync(contentMessage, ChatMessagePriority.High, displayNameMessage);

            SendChatMessageResult sendChatMessageResult2 = await chatThreadClient.SendMessageAsync(contentMessage, ChatMessagePriority.High, displayNameMessage);

            ChatMessage message = await chatThreadClient.GetMessageAsync(sendChatMessageResult.Id);

            AsyncPageable <ChatMessage> messages = chatThreadClient.GetMessagesAsync();
            var getMessagesCount = messages.ToEnumerableAsync().Result.Count;

            var messageId = sendChatMessageResult.Id;
            await chatThreadClient.UpdateMessageAsync(messageId, updatedMessageContent);

            Response <ChatMessage> actualUpdateMessage = await chatThreadClient.GetMessageAsync(messageId);

            await chatThreadClient.DeleteMessageAsync(messageId);

            AsyncPageable <ChatMessage> messagesAfterOneDeleted = chatThreadClient.GetMessagesAsync();
            ChatMessage deletedChatMessage = messagesAfterOneDeleted.ToEnumerableAsync().Result.First(x => x.Id == messageId);

            AsyncPageable <ChatThreadMember> chatThreadMembers = chatThreadClient.GetMembersAsync();
            var chatThreadMembersCount = chatThreadMembers.ToEnumerableAsync().Result.Count;

            var newMember = new ChatThreadMember(user3);
            await chatThreadClient.AddMembersAsync(new List <ChatThreadMember> {
                newMember
            });

            AsyncPageable <ChatThreadMember> chatThreadMembersAfterOneAdded = chatThreadClient.GetMembersAsync();
            var chatThreadMembersAfterOneAddedCount = chatThreadMembersAfterOneAdded.ToEnumerableAsync().Result.Count();

            CommunicationUser userToBeRemoved = user3; //Better name for the snippet
            await chatThreadClient.RemoveMemberAsync(userToBeRemoved);

            AsyncPageable <ChatThreadMember> chatThreadMembersAfterOneDeleted = chatThreadClient.GetMembersAsync();
            var chatThreadMembersAfterOneDeletedCount = chatThreadMembersAfterOneDeleted.ToEnumerableAsync().Result.Count();

            Response typingNotificationResponse = await chatThreadClient.SendTypingNotificationAsync();

            await chatThreadClient.SendTypingNotificationAsync();

            await chatThreadClient.SendReadReceiptAsync(messageId);

            await chatThreadClient.SendReadReceiptAsync(sendChatMessageResult2.Id);

            AsyncPageable <ReadReceipt> readReceipts = chatThreadClient.GetReadReceiptsAsync();
            var readReceiptsCount = readReceipts.ToEnumerableAsync().Result.Count();

            await chatClient.DeleteChatThreadAsync(chatThreadClient.Id);

            //assert
            Assert.AreEqual(updatedTopic, chatThread.Topic);
            Assert.AreEqual(2, chatThread.Members.Count);
            Assert.AreEqual(contentMessage, message.Content);
            Assert.AreEqual(displayNameMessage, message.SenderDisplayName);
            Assert.AreEqual(ChatMessagePriority.High, message.Priority);
            Assert.AreEqual(2, threadsCount);
            Assert.AreEqual(5, getMessagesCount); //Including all types
            Assert.AreEqual(updatedMessageContent, actualUpdateMessage.Value.Content);
            Assert.IsTrue(deletedChatMessage.DeletedOn.HasValue);
            Assert.AreEqual(2, chatThreadMembersCount);
            Assert.AreEqual(3, chatThreadMembersAfterOneAddedCount);
            Assert.AreEqual(2, chatThreadMembersAfterOneDeletedCount);
            Assert.AreEqual((int)HttpStatusCode.OK, typingNotificationResponse.Status);
            //TODO: Commenting out the assert below for now as it is flakey due to server-side delay; currently in investigation
            // Assert.AreEqual(1, readReceiptsCount);
        }