Example #1
0
        internal static CommunicationIdentityAccessTokenResult DeserializeCommunicationIdentityAccessTokenResult(JsonElement element)
        {
            CommunicationIdentity             identity    = default;
            Optional <CommunicationUserToken> accessToken = default;

            foreach (var property in element.EnumerateObject())
            {
                if (property.NameEquals("identity"))
                {
                    identity = CommunicationIdentity.DeserializeCommunicationIdentity(property.Value);
                    continue;
                }
                if (property.NameEquals("accessToken"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        property.ThrowNonNullablePropertyIsNull();
                        continue;
                    }
                    accessToken = CommunicationUserToken.DeserializeCommunicationUserToken(property.Value);
                    continue;
                }
            }
            return(new CommunicationIdentityAccessTokenResult(identity, accessToken.Value));
        }
        public void SendGetUpdateDeleteMessagesSendNotificationReadReceipts()
        {
            CommunicationIdentityClient  communicationIdentityClient = new CommunicationIdentityClient(TestEnvironment.ConnectionString);
            Response <CommunicationUser> threadMember           = communicationIdentityClient.CreateUser();
            CommunicationUserToken       communicationUserToken = communicationIdentityClient.IssueToken(threadMember.Value, new[] { CommunicationTokenScope.Chat });
            string userToken            = communicationUserToken.Token;
            string endpoint             = TestEnvironment.ChatApiUrl();
            string theadCreatorMemberId = communicationUserToken.User.Id;

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

            var chatThreadMember = new ChatThreadMember(new CommunicationUser(theadCreatorMemberId))
            {
                DisplayName      = "UserDisplayName",
                ShareHistoryTime = DateTime.MinValue
            };
            ChatThreadClient chatThreadClient = chatClient.CreateChatThread(topic: "Hello world!", members: new[] { chatThreadMember });
            string           threadId         = chatThreadClient.Id;

            var content           = "hello world";
            var priority          = ChatMessagePriority.Normal;
            var senderDisplayName = "sender name";
            SendChatMessageResult sendMessageResult = chatThreadClient.SendMessage(content, priority, senderDisplayName);

            var                    messageId   = sendMessageResult.Id;
            ChatMessage            chatMessage = chatThreadClient.GetMessage(messageId);
            Pageable <ChatMessage> allMessages = chatThreadClient.GetMessages();

            foreach (ChatMessage message in allMessages)
            {
                Console.WriteLine($"{message.Id}:{message.Sender.Id}:{message.Content}");
            }

            chatThreadClient.UpdateMessage(messageId, "updated message content");
            chatThreadClient.SendReadReceipt(messageId);
            Pageable <ReadReceipt> allReadReceipts = chatThreadClient.GetReadReceipts();

            foreach (ReadReceipt readReceipt in allReadReceipts)
            {
                Console.WriteLine($"{readReceipt.ChatMessageId}:{readReceipt.Sender.Id}:{readReceipt.ReadOn}");
            }
            chatThreadClient.DeleteMessage(messageId);
            chatThreadClient.SendTypingNotification();
            chatClient.DeleteChatThread(threadId);
        }
Example #3
0
        public void CreateGetUpdateDeleteThread()
        {
            CommunicationIdentityClient  communicationIdentityClient = new CommunicationIdentityClient(TestEnvironment.ConnectionString);
            Response <CommunicationUser> threadCreator          = communicationIdentityClient.CreateUser();
            CommunicationUserToken       communicationUserToken = communicationIdentityClient.IssueToken(threadCreator.Value, new[] { CommunicationTokenScope.Chat });
            string userToken       = communicationUserToken.Token;
            string endpoint        = TestEnvironment.ChatApiUrl();
            string threadCreatorId = communicationUserToken.User.Id;

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

            var chatThreadMember = new ChatThreadMember(threadCreator)
            {
                DisplayName = "UserDisplayName"
            };
            ChatThreadClient chatThreadClient = chatClient.CreateChatThread(topic: "Hello world!", members: new[] { chatThreadMember });
            string           threadId         = chatThreadClient.Id;
            ChatThread       chatThread       = chatClient.GetChatThread(threadId);

            Pageable <ChatThreadInfo> chatThreadsInfo = chatClient.GetChatThreadsInfo();

            foreach (ChatThreadInfo chatThreadInfo in chatThreadsInfo)
            {
                Console.WriteLine($"{ chatThreadInfo.Id}");
            }
            var topic = "new topic";

            chatThreadClient.UpdateThread(topic);
            chatClient.DeleteChatThread(threadId);
            try
            {
                chatThreadMember = new ChatThreadMember(new CommunicationUser("invalid user"));
                ChatThreadClient chatThreadClient_ = chatClient.CreateChatThread(topic: "Hello world!", members: new[] { chatThreadMember });
                Assert.Fail("CreateChatThread did not fail");
            }
            catch (RequestFailedException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (Exception ex)
            {
                Assert.Fail($"Unexpected error: {ex}");
            }
        }
Example #4
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);
        }
Example #5
0
        public async Task SendGetUpdateDeleteMessagesSendNotificationReadReceiptsAsync()
        {
            CommunicationIdentityClient            communicationIdentityClient = new CommunicationIdentityClient(TestEnvironment.ConnectionString);
            Response <CommunicationUserIdentifier> threadMember = await communicationIdentityClient.CreateUserAsync();

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

            string userToken            = communicationUserToken.Token;
            string endpoint             = TestEnvironment.ChatApiUrl();
            string theadCreatorMemberId = communicationUserToken.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_SendMessage
            var content           = "hello world";
            var priority          = ChatMessagePriority.Normal;
            var senderDisplayName = "sender name";
            SendChatMessageResult sendMessageResult = await chatThreadClient.SendMessageAsync(content, priority, senderDisplayName);

            #endregion Snippet:Azure_Communication_Chat_Tests_SendMessage

            var messageId = sendMessageResult.Id;
            #region Snippet:Azure_Communication_Chat_Tests_Samples_GetMessage
            ChatMessage chatMessage = await chatThreadClient.GetMessageAsync(messageId);

            #endregion Snippet:Azure_Communication_Chat_Tests_Samples_GetMessage

            #region Snippet:Azure_Communication_Chat_Tests_Samples_GetMessages
            AsyncPageable <ChatMessage> allMessages = chatThreadClient.GetMessagesAsync();
            await foreach (ChatMessage message in allMessages)
            {
                Console.WriteLine($"{message.Id}:{message.Sender.Id}:{message.Content}");
            }
            #endregion Snippet:Azure_Communication_Chat_Tests_Samples_GetMessages

            #region Snippet:Azure_Communication_Chat_Tests_Samples_UpdateMessage
            await chatThreadClient.UpdateMessageAsync(messageId, "updated message content");

            #endregion Snippet:Azure_Communication_Chat_Tests_Samples_UpdateMessage

            //Note : Due to the async nature of the storage, moving coverage to CI tests
            #region Snippet:Azure_Communication_Chat_Tests_Samples_SendReadReceipt
            //@@await chatThreadClient.SendReadReceiptAsync(messageId);
            #endregion Snippet:Azure_Communication_Chat_Tests_Samples_SendReadReceipt

            #region Snippet:Azure_Communication_Chat_Tests_Samples_GetReadReceipts
            AsyncPageable <ReadReceipt> allReadReceipts = chatThreadClient.GetReadReceiptsAsync();
            await foreach (ReadReceipt readReceipt in allReadReceipts)
            {
                Console.WriteLine($"{readReceipt.ChatMessageId}:{readReceipt.Sender.Id}:{readReceipt.ReadOn}");
            }
            #endregion Snippet:Azure_Communication_Chat_Tests_Samples_GetReadReceipts

            #region Snippet:Azure_Communication_Chat_Tests_Samples_DeleteMessage
            await chatThreadClient.DeleteMessageAsync(messageId);

            #endregion Snippet:Azure_Communication_Chat_Tests_Samples_DeleteMessage

            #region Snippet:Azure_Communication_Chat_Tests_Samples_SendTypingNotification
            await chatThreadClient.SendTypingNotificationAsync();

            #endregion Snippet:Azure_Communication_Chat_Tests_Samples_SendTypingNotification

            await chatClient.DeleteChatThreadAsync(threadId);
        }
Example #6
0
        public async Task CreateGetUpdateDeleteThreadAsync()
        {
            CommunicationIdentityClient            communicationIdentityClient = new CommunicationIdentityClient(TestEnvironment.ConnectionString);
            Response <CommunicationUserIdentifier> threadMember = await communicationIdentityClient.CreateUserAsync();

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

            string userToken       = communicationUserToken.Token;
            string endpoint        = TestEnvironment.ChatApiUrl();
            string threadCreatorId = threadMember.Value.Id;

            #region Snippet:Azure_Communication_Chat_Tests_Samples_CreateChatClient
            ChatClient chatClient = new ChatClient(
                new Uri(endpoint),
                new CommunicationTokenCredential(userToken));
            #endregion Snippet:Azure_Communication_Chat_Tests_Samples_CreateChatClient

            #region Snippet:Azure_Communication_Chat_Tests_Samples_CreateThread
            var chatThreadMember = new ChatThreadMember(new CommunicationUserIdentifier(threadCreatorId))
            {
                DisplayName = "UserDisplayName"
            };
            ChatThreadClient chatThreadClient = await chatClient.CreateChatThreadAsync(topic : "Hello world!", members : new[] { chatThreadMember });

            string threadId = chatThreadClient.Id;
            #endregion Snippet:Azure_Communication_Chat_Tests_Samples_CreateThread

            #region Snippet:Azure_Communication_Chat_Tests_Samples_GetThread
            ChatThread chatThread = await chatClient.GetChatThreadAsync(threadId);

            #endregion Snippet:Azure_Communication_Chat_Tests_Samples_GetThread

            #region Snippet:Azure_Communication_Chat_Tests_Samples_GetThreads
            AsyncPageable <ChatThreadInfo> chatThreadsInfo = chatClient.GetChatThreadsInfoAsync();
            await foreach (ChatThreadInfo chatThreadInfo in chatThreadsInfo)
            {
                Console.WriteLine($"{ chatThreadInfo.Id}");
            }
            #endregion Snippet:Azure_Communication_Chat_Tests_Samples_GetThreads

            #region Snippet:Azure_Communication_Chat_Tests_Samples_UpdateThread
            var topic = "new topic";
            await chatThreadClient.UpdateThreadAsync(topic);

            #endregion Snippet:Azure_Communication_Chat_Tests_Samples_UpdateThread

            #region Snippet:Azure_Communication_Chat_Tests_Samples_DeleteThread
            await chatClient.DeleteChatThreadAsync(threadId);

            #endregion Snippet:Azure_Communication_Chat_Tests_Samples_DeleteThread

            #region Snippet:Azure_Communication_Chat_Tests_Samples_Troubleshooting
            try
            {
                /*@@*/ chatThreadMember = new ChatThreadMember(new CommunicationUserIdentifier("invalid user"));
                ChatThreadClient chatThreadClient_ = await chatClient.CreateChatThreadAsync(topic : "Hello world!", members : new[] { chatThreadMember });
            }
            catch (RequestFailedException ex)
            {
                Console.WriteLine(ex.Message);
            }
            #endregion Snippet:Azure_Communication_Chat_Tests_Samples_Troubleshooting
            catch (Exception ex)
            {
                Assert.Fail($"Unexpected error: {ex}");
            }
        }
Example #7
0
        public async Task GetAddRemoveMembersAsync()
        {
            CommunicationIdentityClient  communicationIdentityClient = new CommunicationIdentityClient(TestEnvironment.ConnectionString);
            Response <CommunicationUser> joshResponse = await communicationIdentityClient.CreateUserAsync();

            CommunicationUser            josh           = joshResponse.Value;
            Response <CommunicationUser> gloriaResponse = await communicationIdentityClient.CreateUserAsync();

            CommunicationUser            gloria      = gloriaResponse.Value;
            Response <CommunicationUser> amyResponse = await communicationIdentityClient.CreateUserAsync();

            CommunicationUser amy = amyResponse.Value;

            CommunicationUserToken joshTokenResponse = await communicationIdentityClient.IssueTokenAsync(josh, new[] { CommunicationTokenScope.Chat });

            ChatClient chatClient = new ChatClient(
                new Uri(TestEnvironment.ChatApiUrl()),
                new CommunicationUserCredential(joshTokenResponse.Token));

            var chatParticipant = new ChatParticipant(josh)
            {
                DisplayName      = "Josh",
                ShareHistoryTime = DateTime.MinValue
            };
            ChatThreadClient chatThreadClient = await chatClient.CreateChatThreadAsync(topic : "Hello world!", participants : new[] { chatParticipant });

            string threadId = chatThreadClient.Id;

            #region Snippet:Azure_Communication_Chat_Tests_Samples_GetParticipants
            AsyncPageable <ChatParticipant> allParticipants = chatThreadClient.GetParticipantsAsync();
            await foreach (ChatParticipant participant in allParticipants)
            {
                Console.WriteLine($"{participant.User.Id}:{participant.DisplayName}:{participant.ShareHistoryTime}");
            }
            #endregion Snippet:Azure_Communication_Chat_Tests_GetMembers

            #region Snippet:Azure_Communication_Chat_Tests_Samples_AddParticipants
            var participants = new[]
            {
                new ChatParticipant(josh)
                {
                    DisplayName = "Josh"
                },
                new ChatParticipant(gloria)
                {
                    DisplayName = "Gloria"
                },
                new ChatParticipant(amy)
                {
                    DisplayName = "Amy"
                }
            };

            await chatThreadClient.AddParticipantsAsync(participants);

            #endregion Snippet:Azure_Communication_Chat_Tests_Samples_AddParticipants

            #region Snippet:Azure_Communication_Chat_Tests_Samples_RemoveParticipant
            await chatThreadClient.RemoveParticipantAsync(gloria);

            #endregion Snippet:Azure_Communication_Chat_Tests_Samples_RemoveParticipant

            await chatClient.DeleteChatThreadAsync(threadId);
        }
Example #8
0
 internal CommunicationIdentityAccessTokenResult(CommunicationIdentity identity, CommunicationUserToken accessToken)
 {
     Identity    = identity;
     AccessToken = accessToken;
 }
 public User(string userId, CommunicationUserToken token)
     : this(userId, token.User)
 {
     CommunicationUserToken = token;
 }