Exemplo n.º 1
0
        public async Task <ActionResult> TryAddUserToThread(string threadId, ContosoMemberModel user)
        {
            var moderatorId = _store.Store[threadId];

            AccessToken moderatorToken = await _userTokenManager.GenerateTokenAsync(_resourceConnectionString, moderatorId);

            ChatClient chatClient = new ChatClient(
                new Uri(_chatGatewayUrl),
                new CommunicationTokenCredential(moderatorToken.Token));

            ChatThreadClient chatThreadClient = chatClient.GetChatThreadClient(threadId);

            var threadProperties = await chatThreadClient.GetPropertiesAsync();

            var chatParticipant = new ChatParticipant(new CommunicationUserIdentifier(user.Id));

            chatParticipant.DisplayName      = user.DisplayName;
            chatParticipant.ShareHistoryTime = threadProperties.Value.CreatedOn;

            try
            {
                Response response = await chatThreadClient.AddParticipantAsync(chatParticipant);

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

            return(Ok());
        }
Exemplo n.º 2
0
        public async Task AddParticipantShouldSucceed()
        {
            //arrange
            var              id = "8:acs:1b5cc06b-f352-4571-b1e6-d9b259b7c776_00000007-0464-274b-b274-5a3a0d000101";
            ChatParticipant  chatParticipant  = new ChatParticipant(new CommunicationUserIdentifier(id));
            ChatThreadClient chatThreadClient = CreateMockChatThreadClient(201, AddParticipantApiResponsePayload);

            //act
            Response AddParticipantResponse = await chatThreadClient.AddParticipantAsync(chatParticipant);

            //assert
            Assert.AreEqual(201, AddParticipantResponse.Status);
        }
Exemplo n.º 3
0
        public async Task AddParticipantFailureShouldThrowError()
        {
            //arrange
            var              id = "8:acs:1b5cc06b-f352-4571-b1e6-d9b259b7c776_00000007-0464-274b-b274-5a3a0d000101";
            ChatParticipant  chatParticipant  = new ChatParticipant(new CommunicationUserIdentifier(id));
            ChatThreadClient chatThreadClient = CreateMockChatThreadClient(401);

            try
            {
                //act
                await chatThreadClient.AddParticipantAsync(chatParticipant);
            }
            catch (RequestFailedException requestFailedException)
            {
                //assert
                Assert.AreEqual(401, requestFailedException.Status);
            }
        }
        public async Task ThreadCGUD_ParticipantAUR_MessageGSU_NotificationT_Async()
        {
            //arr
            Console.WriteLine($"ThreadCGUD_ParticipantAUR_MessageGSU_NotificationT_Async Running on RecordedTestMode : {Mode}");
            CommunicationIdentityClient communicationIdentityClient = CreateInstrumentedCommunicationIdentityClient();

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

            (CommunicationUserIdentifier user2, _) = await CreateUserAndTokenAsync(communicationIdentityClient);

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

            (CommunicationUserIdentifier user4, _) = await CreateUserAndTokenAsync(communicationIdentityClient);

            (CommunicationUserIdentifier user5, _) = await CreateUserAndTokenAsync(communicationIdentityClient);

            string repeatabilityRequestId1 = "contoso-C0A747F1-6245-4307-8267-B974340677DC";
            string repeatabilityRequestId2 = "contoso-D0A747F1-6245-4307-8267-B974340677DD";

            var topic = "Thread async from C# sdk";
            var displayNameMessage = "DisplayName sender message 1";
            var participants       = new List <ChatParticipant>
            {
                new ChatParticipant(user1)
                {
                    DisplayName = "user1"
                },
                new ChatParticipant(user2)
                {
                    DisplayName = "user2"
                },
                new ChatParticipant(user3)
                {
                    DisplayName = "user3"
                }
            };
            ChatClient chatClient  = CreateInstrumentedChatClient(token1);
            ChatClient chatClient3 = CreateInstrumentedChatClient(token3);

            //act
            CreateChatThreadResult createChatThreadResult = await chatClient.CreateChatThreadAsync(topic, participants, repeatabilityRequestId1);

            ChatThreadClient chatThreadClient = GetInstrumentedChatThreadClient(chatClient, createChatThreadResult.ChatThread.Id);
            var threadId = chatThreadClient.Id;
            CreateChatThreadResult createChatThreadResult2 = await chatClient.CreateChatThreadAsync(topic, participants, repeatabilityRequestId2);

            ChatThreadClient chatThreadClient2 = GetInstrumentedChatThreadClient(chatClient, createChatThreadResult2.ChatThread.Id);
            ChatThreadClient chatThreadClient3 = GetInstrumentedChatThreadClient(chatClient3, threadId);

            AsyncPageable <ChatParticipant> chatParticipantsOnCreation = chatThreadClient.GetParticipantsAsync();
            var chatParticipantsOnCreationCount = chatParticipantsOnCreation.ToEnumerableAsync().Result.Count;

            var updatedTopic = "Updated topic - C# sdk";
            await chatThreadClient.UpdateTopicAsync(updatedTopic);

            ChatThread chatThread = await chatClient.GetChatThreadAsync(threadId);

            string messageContent = "Content for message 1";
            string messageId      = await chatThreadClient.SendMessageAsync(messageContent, ChatMessageType.Text, displayNameMessage);

            string messageContent2 = "Content for message 2";
            string messageId2      = await chatThreadClient2.SendMessageAsync(messageContent2, ChatMessageType.Html, displayNameMessage);

            string messageContent3 = "Content for message 3";
            string messageId3      = await chatThreadClient3.SendMessageAsync(messageContent3, ChatMessageType.Text, displayNameMessage);

            string messageContent4 = "Content for message 4";
            string messageId4      = await chatThreadClient3.SendMessageAsync(messageContent4, ChatMessageType.Html, displayNameMessage);

            string messageContent5 = "Content for message 5";
            string messageId5      = await chatThreadClient3.SendMessageAsync(messageContent5, ChatMessageType.Text, displayNameMessage);

            string messageContent6 = "Content for message 6";
            string messageId6      = await chatThreadClient3.SendMessageAsync(messageContent6, ChatMessageType.Html, displayNameMessage);

            ChatMessage message = await chatThreadClient.GetMessageAsync(messageId);

            ChatMessage message2 = await chatThreadClient2.GetMessageAsync(messageId2);

            ChatMessage message3 = await chatThreadClient3.GetMessageAsync(messageId3);

            ChatMessage message4 = await chatThreadClient3.GetMessageAsync(messageId4);

            ChatMessage message5 = await chatThreadClient3.GetMessageAsync(messageId5);

            ChatMessage message6 = await chatThreadClient3.GetMessageAsync(messageId6);

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

            #region Messages Pagination assertions
            AsyncPageable <ChatMessage> messagesPaginationTest = chatThreadClient.GetMessagesAsync();
            await PageableTester <ChatMessage> .AssertPaginationAsync(enumerableResource : messagesPaginationTest, expectedPageSize : 2, expectedTotalResources : 8);

            #endregion

            var updatedMessageContent = "This is message 1 content updated";
            await chatThreadClient.UpdateMessageAsync(messageId, updatedMessageContent);

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

            await chatThreadClient.DeleteMessageAsync(messageId);

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

            #region Participants Pagination assertions
            AsyncPageable <ChatParticipant> chatParticipants = chatThreadClient.GetParticipantsAsync();
            var chatParticipantsCount = chatParticipants.ToEnumerableAsync().Result.Count;
            #endregion

            var newParticipant = new ChatParticipant(user4)
            {
                DisplayName = "user4"
            };
            var newParticipant2 = new ChatParticipant(user5)
            {
                DisplayName = "user5"
            };
            AddChatParticipantsResult addChatParticipantsResult = await chatThreadClient.AddParticipantsAsync(participants : new[] { newParticipant });

            AddChatParticipantsResult addChatParticipantsResult2 = await chatThreadClient.AddParticipantAsync(newParticipant2);

            AsyncPageable <ChatParticipant> chatParticipantsAfterTwoOneAdded = chatThreadClient.GetParticipantsAsync();
            await PageableTester <ChatParticipant> .AssertPaginationAsync(enumerableResource : chatParticipantsAfterTwoOneAdded, expectedPageSize : 2, expectedTotalResources : 5);

            var chatParticipantsAfterTwoOneAddedCount = chatParticipantsAfterTwoOneAdded.ToEnumerableAsync().Result.Count;

            List <ChatMessage> messagesAfterParticipantsAdded = chatThreadClient.GetMessagesAsync().ToEnumerableAsync().Result;
            ChatMessage        participantAddedMessage        = messagesAfterParticipantsAdded.First(x => x.Type == ChatMessageType.ParticipantAdded);

            CommunicationUserIdentifier participantToBeRemoved = user4;
            await chatThreadClient.RemoveParticipantAsync(identifier : participantToBeRemoved);

            AsyncPageable <ChatParticipant> chatParticipantAfterOneDeleted = chatThreadClient.GetParticipantsAsync();
            var chatParticipantAfterOneDeletedCount = chatParticipantAfterOneDeleted.ToEnumerableAsync().Result.Count;

            List <ChatMessage> messagesAfterParticipantRemoved = chatThreadClient.GetMessagesAsync().ToEnumerableAsync().Result;
            ChatMessage        participantRemovedMessage       = messagesAfterParticipantRemoved.First(x => x.Type == ChatMessageType.ParticipantRemoved);

            ChatMessage participantRemovedMessage2 = await chatThreadClient.GetMessageAsync(participantRemovedMessage.Id);

            Response typingNotificationResponse = await chatThreadClient.SendTypingNotificationAsync();

            await chatThreadClient.SendTypingNotificationAsync();

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

            await chatClient.DeleteChatThreadAsync(threadId);

            //assert
            Assert.AreEqual(updatedTopic, chatThread.Topic);
            Assert.AreEqual(3, chatParticipantsOnCreationCount);
            Assert.AreEqual(messageContent, message.Content.Message);
            Assert.AreEqual(displayNameMessage, message.SenderDisplayName);
            Assert.AreEqual(updatedMessageContent, actualUpdateMessage.Value.Content.Message);
            Assert.AreEqual(ChatMessageType.Text, message.Type);

            Assert.AreEqual(messageContent2, message2.Content.Message);
            Assert.AreEqual(messageContent3, message3.Content.Message);
            Assert.AreEqual(messageContent4, message4.Content.Message);
            Assert.AreEqual(messageContent5, message5.Content.Message);
            Assert.AreEqual(messageContent6, message6.Content.Message);

            Assert.AreEqual(2, threadsCount);
            Assert.AreEqual(8, getMessagesCount);  //Including all types : 5 text message, 3 control messages
            Assert.AreEqual(3, getMessagesCount2); //Including all types : 1 text message, 2 control messages

            Assert.AreEqual(1, participantAddedMessage.Content.Participants.Count);
            Assert.AreEqual(user5.Id, CommunicationIdentifierSerializer.Serialize(participantAddedMessage.Content.Participants[0].User).CommunicationUser.Id);
            Assert.AreEqual("user5", participantAddedMessage.Content.Participants[0].DisplayName);

            Assert.AreEqual(1, participantRemovedMessage.Content.Participants.Count);
            Assert.AreEqual(user4.Id, CommunicationIdentifierSerializer.Serialize(participantRemovedMessage.Content.Participants[0].User).CommunicationUser.Id);
            Assert.AreEqual("user4", participantRemovedMessage.Content.Participants[0].DisplayName);

            Assert.IsTrue(deletedChatMessage.DeletedOn.HasValue);
            Assert.AreEqual(3, chatParticipantsCount);
            Assert.AreEqual(5, chatParticipantsAfterTwoOneAddedCount);
            Assert.AreEqual(4, chatParticipantAfterOneDeletedCount);
            Assert.IsNull(addChatParticipantsResult.Errors);
            Assert.IsNull(addChatParticipantsResult2.Errors);
            Assert.AreEqual((int)HttpStatusCode.OK, typingNotificationResponse.Status);
        }