public async Task CreateChatThreadShouldExposePartialErrors() { //arrange ChatClient chatClient = CreateMockChatClient(201, CreateChatThreadWithErrorsApiResponsePayload); var chatParticipant = new ChatParticipant(new CommunicationUserIdentifier("8:acs:46849534-eb08-4ab7-bde7-c36928cd1547_00000007-165c-9b10-b0b7-3a3a0d00076c")); //act CreateChatThreadResult createChatThreadResult = await chatClient.CreateChatThreadAsync("", new List <ChatParticipant>() { chatParticipant }); //assert AsssertParticipantError(createChatThreadResult.InvalidParticipants.First(x => x.Code == "401"), "Authentication failed", "8:acs:1b5cc06b-f352-4571-b1e6-d9b259b7c776_00000007-1234-1234-1234-223a12345678"); AsssertParticipantError(createChatThreadResult.InvalidParticipants.First(x => x.Code == "403"), "Permissions check failed", "8:acs:1b5cc06b-f352-4571-b1e6-d9b259b7c776_00000007-1234-1234-1234-223a12345679"); AsssertParticipantError(createChatThreadResult.InvalidParticipants.First(x => x.Code == "404"), "Not found", "8:acs:1b5cc06b-f352-4571-b1e6-d9b259b7c776_00000007-1234-1234-1234-223a12345677"); Assert.AreEqual(3, createChatThreadResult.InvalidParticipants.Count); Assert.AreEqual("8:acs:46849534-eb08-4ab7-bde7-c36928cd1547_00000007-165c-9b10-b0b7-3a3a0d00076c", CommunicationIdentifierSerializer.Serialize(createChatThreadResult.ChatThread.CreatedBy).RawId); Assert.AreEqual("Topic for testing errors", createChatThreadResult.ChatThread.Topic); Assert.AreEqual("19:[email protected]", createChatThreadResult.ChatThread.Id); }
public async Task <ActionResult> TryAddUserToThread(string threadId, ContosoMemberModel user) { string moderatorId; if (_chatThreadStore.Store.ContainsKey(threadId)) { moderatorId = _chatThreadStore.Store[threadId]; } else { return(NotFound()); } 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()); }
public async Task Insert(Chat entity) { var company = await _context.Companies.FirstOrDefaultAsync(x => x.CompanyId == entity.Company.CompanyId); var user = await _context.Users.FirstOrDefaultAsync(x => x.Id.Equals(entity.Owner.Id)); entity.Company = company; entity.Owner = user; await _context.Chats.AddAsync(entity); await _context.SaveChangesAsync(); var newParticipant = new ChatParticipant { User = user, Chat = entity }; await _context.ChatParticipants.AddAsync(newParticipant); }
private void UnInitialized(object message) { message.Match() .With <CreateChatCommand>(cmd => { List <GetUserByIdResult> users = new List <GetUserByIdResult>(); foreach (Guid cmdParticipantItem in cmd.Participants) { var envelop = new ShardEnvelope(cmdParticipantItem.ToString(), new GetUserById(cmdParticipantItem)); GetUserByIdResult contactUser = _userRegion.Ask <GetUserByIdResult>(envelop).Result; users.Add(contactUser); } List <ChatParticipant> chatParticipants = users.Select(x => new ChatParticipant(x.Id, x.Login, x.UserName)).ToList(); ChatParticipant creator = users.Where(x => x.Id == cmd.Creator) .Select(x => new ChatParticipant(x.Id, x.Login, x.UserName)).FirstOrDefault(); var @event = new ChatCreatedEvent(cmd.Id, creator, chatParticipants); Persist <ChatCreatedEvent>(@event, UpdateState); }); }
static async System.Threading.Tasks.Task Main(string[] args) { Uri endpoint = new Uri("https://<RESOURCE_NAME>.communication.azure.com"); CommunicationTokenCredential communicationTokenCredential = new CommunicationTokenCredential("<Access_Token>"); ChatClient chatClient = new ChatClient(endpoint, communicationTokenCredential); // <Start a chat thread> var chatParticipant = new ChatParticipant(identifier: new CommunicationUserIdentifier(id: "<Access_ID>")) { DisplayName = "UserDisplayName" }; CreateChatThreadResult createChatThreadResult = await chatClient.CreateChatThreadAsync(topic : "Hello world!", participants : new[] { chatParticipant }); // <Get a chat thread client> ChatThreadClient chatThreadClient = chatClient.GetChatThreadClient(threadId: createChatThreadResult.ChatThread.Id); string threadId = chatThreadClient.Id; // <List all chat threads> AsyncPageable <ChatThreadItem> chatThreadItems = chatClient.GetChatThreadsAsync(); await foreach (ChatThreadItem chatThreadItem in chatThreadItems) { Console.WriteLine($"{ chatThreadItem.Id}"); } // <Send a message to a chat thread> SendChatMessageResult sendChatMessageResult = await chatThreadClient.SendMessageAsync(content : "hello world", type : ChatMessageType.Text); string messageId = sendChatMessageResult.Id; // <Receive chat messages from a chat thread> AsyncPageable <ChatMessage> allMessages = chatThreadClient.GetMessagesAsync(); await foreach (ChatMessage message in allMessages) { Console.WriteLine($"{message.Id}:{message.Content.Message}"); } // <Add a user as a participant to the chat thread> var josh = new CommunicationUserIdentifier(id: "<Access_ID_For_Josh>"); var gloria = new CommunicationUserIdentifier(id: "<Access_ID_For_Gloria>"); var amy = new CommunicationUserIdentifier(id: "<Access_ID_For_Amy>"); var participants = new[] { new ChatParticipant(josh) { DisplayName = "Josh" }, new ChatParticipant(gloria) { DisplayName = "Gloria" }, new ChatParticipant(amy) { DisplayName = "Amy" } }; await chatThreadClient.AddParticipantsAsync(participants : participants); // <Get thread participants> AsyncPageable <ChatParticipant> allParticipants = chatThreadClient.GetParticipantsAsync(); await foreach (ChatParticipant participant in allParticipants) { Console.WriteLine($"{((CommunicationUserIdentifier)participant.User).Id}:{participant.DisplayName}:{participant.ShareHistoryTime}"); } // <Send read receipt> await chatThreadClient.SendReadReceiptAsync(messageId : messageId); }
public async Task GetAddRemoveMembersAsync() { CommunicationIdentityClient communicationIdentityClient = new CommunicationIdentityClient(TestEnvironment.LiveTestDynamicConnectionString); Response <CommunicationUserIdentifier> joshResponse = await communicationIdentityClient.CreateUserAsync(); CommunicationUserIdentifier josh = joshResponse.Value; Response <CommunicationUserIdentifier> gloriaResponse = await communicationIdentityClient.CreateUserAsync(); CommunicationUserIdentifier gloria = gloriaResponse.Value; Response <CommunicationUserIdentifier> amyResponse = await communicationIdentityClient.CreateUserAsync(); CommunicationUserIdentifier amy = amyResponse.Value; AccessToken joshTokenResponse = await communicationIdentityClient.GetTokenAsync(josh, new[] { CommunicationTokenScope.Chat }); ChatClient chatClient = new ChatClient( TestEnvironment.LiveTestDynamicEndpoint, new CommunicationTokenCredential(joshTokenResponse.Token)); var chatParticipant = new ChatParticipant(josh) { DisplayName = "Josh", ShareHistoryTime = DateTimeOffset.MinValue }; CreateChatThreadResult createChatThreadResult = await chatClient.CreateChatThreadAsync(topic : "Hello world!", participants : new[] { chatParticipant }); ChatThreadClient chatThreadClient = chatClient.GetChatThreadClient(createChatThreadResult.ChatThread.Id); #region Snippet:Azure_Communication_Chat_Tests_Samples_GetParticipants AsyncPageable <ChatParticipant> allParticipants = chatThreadClient.GetParticipantsAsync(); await foreach (ChatParticipant participant in allParticipants) { Console.WriteLine($"{((CommunicationUserIdentifier)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(chatThreadClient.Id); }
public ChatCreatedEvent(Guid id, ChatParticipant creator, List <ChatParticipant> participants) { Id = id; Creator = creator; Participants = participants; }
internal void ShowMessage(NetworkManager.ChatMessage e) { if (string.IsNullOrEmpty(e.message)) { return; } // Count the lines of the chat, trim if necessary int numlines = 1; foreach (char c in ChatTxt.text) { if (c == '\n') { numlines++; } } if (numlines > 11) { int pos = ChatTxt.text.Length - 1; numlines = 1; while (pos > 0 && numlines < 12) { if (ChatTxt.text[pos] == '\n') { numlines++; } pos--; } ChatTxt.text = ChatTxt.text.Substring(pos + 1); } string msg = ""; if (e.type == ChatType.ParticipantGone) { ChatParticipant goner = null; foreach (ChatParticipant cp in participants) { if (cp.id == e.senderid) { goner = cp; break; } } if (goner != null) { participants.Remove(goner); msg = (ChatTxt.text.Length > 0 ? "\n" : "") + e.message; } } else if (e.type == ChatType.Error) { msg = "<color=red>" + msg + "</color>"; } else { // Add the avatar and name of the sender if in the list ChatParticipant sender = null; foreach (ChatParticipant cp in participants) { if (cp.id == e.senderid) { sender = cp; break; } } if (sender != null) { msg = (ChatTxt.text.Length > 0 ? "\n" : "") + "<sprite=" + sender.avatar + "><b><color=#101000>" + sender.name + "</color></b> " + e.message; } else { msg = (ChatTxt.text.Length > 0 ? "\n" : "") + e.message; } // Merge the participants foreach (ChatParticipant pe in e.participants) { bool found = false; foreach (ChatParticipant pc in participants) { if (pe.id == pc.id) { found = true; break; } } if (!found) { participants.Add(pe); } } // Replace emojis msg = msg .Replace(":)", "<sprite=75>").Replace(":smile:", "<sprite=75>") .Replace(":D", "<sprite=76>").Replace(":happy:", "<sprite=76>") .Replace(":love:", "<sprite=77>") .Replace("8)", "<sprite=78>").Replace(":glasses:", "<sprite=78>") .Replace(":lol:", "<sprite=79>").Replace(":joy:", "<sprite=79>") .Replace(":p", "<sprite=80>").Replace(":P", "<sprite=80>").Replace(":tongue:", "<sprite=80>") .Replace(":cry:", "<sprite=81>").Replace(">(", "<sprite=81>").Replace(":sad:", "<sprite=81>") .Replace(":(", "<sprite=82>"); } // Add the message ChatTxt.text += msg; }
public void ThreadCGUD_ParticipantAUR_MessageGSU_NotificationT() { //arr Console.WriteLine($"ThreadCGUD_ParticipantAUR_MessageGSU_NotificationT Running on RecordedTestMode : {Mode}"); CommunicationIdentityClient communicationIdentityClient = CreateInstrumentedCommunicationIdentityClient(); (CommunicationUserIdentifier user1, string token1) = CreateUserAndToken(communicationIdentityClient); (CommunicationUserIdentifier user2, _) = CreateUserAndToken(communicationIdentityClient); (CommunicationUserIdentifier user3, string token3) = CreateUserAndToken(communicationIdentityClient); (CommunicationUserIdentifier user4, _) = CreateUserAndToken(communicationIdentityClient); (CommunicationUserIdentifier user5, _) = CreateUserAndToken(communicationIdentityClient); string repeatabilityRequestId1 = "contoso-F0A747F1-6245-4307-8267-B974340677D2"; string repeatabilityRequestId2 = "contoso-A0A747F1-6245-4307-8267-B974340677DA"; var topic = "Thread sync from C# sdk"; var displayNameMessage = "DisplayName sender message 1"; var participants = new[] { 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 = chatClient.CreateChatThread(topic, participants, repeatabilityRequestId1); ChatThreadClient chatThreadClient = GetInstrumentedChatThreadClient(chatClient, createChatThreadResult.ChatThread.Id); var threadId = chatThreadClient.Id; CreateChatThreadResult createChatThreadResult2 = chatClient.CreateChatThread(topic, participants, repeatabilityRequestId2); ChatThreadClient chatThreadClient2 = GetInstrumentedChatThreadClient(chatClient, createChatThreadResult2.ChatThread.Id); ChatThreadClient chatThreadClient3 = GetInstrumentedChatThreadClient(chatClient3, threadId); Pageable <ChatParticipant> chatParticipantsOnCreation = chatThreadClient.GetParticipants(); var chatParticipantsOnCreationCount = chatParticipantsOnCreation.Count(); string updatedTopic = "Launch meeting"; chatThreadClient.UpdateTopic(topic: "Launch meeting"); ChatThread chatThread = chatClient.GetChatThread(threadId); string messageContent = "Let's meet at 11am"; string messageId = chatThreadClient.SendMessage("Let's meet at 11am"); string messageContent2 = "Content for message 2"; string messageId2 = chatThreadClient2.SendMessage(messageContent2, ChatMessageType.Text, displayNameMessage); string messageContent3 = "Content for message 3"; string messageId3 = chatThreadClient3.SendMessage(messageContent3, ChatMessageType.Html, displayNameMessage); string messageContent4 = "Content for message 4"; string messageId4 = chatThreadClient3.SendMessage(messageContent4, ChatMessageType.Text, displayNameMessage); string messageContent5 = "Content for message 5"; string messageId5 = chatThreadClient3.SendMessage(messageContent5, ChatMessageType.Html, displayNameMessage); string messageContent6 = "Content for message 6"; string messageId6 = chatThreadClient3.SendMessage(messageContent6, ChatMessageType.Text, displayNameMessage); ChatMessage message = chatThreadClient.GetMessage(messageId); ChatMessage message2 = chatThreadClient2.GetMessage(messageId2); ChatMessage message3 = chatThreadClient3.GetMessage(messageId3); ChatMessage message4 = chatThreadClient3.GetMessage(messageId4); ChatMessage message5 = chatThreadClient3.GetMessage(messageId5); ChatMessage message6 = chatThreadClient3.GetMessage(messageId6); Pageable <ChatMessage> messages = chatThreadClient.GetMessages(); Pageable <ChatMessage> messages2 = chatThreadClient2.GetMessages(); var getMessagesCount = messages.Count(); var getMessagesCount2 = messages2.Count(); Pageable <ChatMessage> pageableMessages = chatThreadClient.GetMessages(); PageableTester <ChatMessage> .AssertPagination(enumerableResource : pageableMessages, expectedPageSize : 2, expectedTotalResources : 8); string updatedMessageContent = "Instead of 11am, let's meet at 2pm"; chatThreadClient.UpdateMessage(messageId, content: "Instead of 11am, let's meet at 2pm"); Response <ChatMessage> actualUpdateMessage = chatThreadClient.GetMessage(messageId); chatThreadClient.DeleteMessage(messageId); Pageable <ChatMessage> messagesAfterOneDeleted = chatThreadClient.GetMessages(); ChatMessage deletedChatMessage = messagesAfterOneDeleted.First(x => x.Id == messageId); Pageable <ChatParticipant> chatParticipants = chatThreadClient.GetParticipants(); var chatParticipantsCount = chatParticipants.Count(); var newParticipant = new ChatParticipant(user4) { DisplayName = "user4" }; var newParticipant2 = new ChatParticipant(user5) { DisplayName = "user5" }; chatThreadClient.AddParticipants(participants: new[] { newParticipant }); AddChatParticipantsResult addChatParticipantsResult = chatThreadClient.AddParticipant(newParticipant2); Pageable <ChatParticipant> chatParticipantsAfterTwoAdded = chatThreadClient.GetParticipants(); PageableTester <ChatParticipant> .AssertPagination(enumerableResource : chatParticipantsAfterTwoAdded, expectedPageSize : 2, expectedTotalResources : 5); var chatParticipantsAfterTwoAddedCount = chatParticipantsAfterTwoAdded.Count(); Pageable <ChatMessage> messagesAfterParticipantsAdded = chatThreadClient.GetMessages(); ChatMessage participantAddedMessage = messagesAfterParticipantsAdded.First(x => x.Type == ChatMessageType.ParticipantAdded); CommunicationUserIdentifier participantToBeRemoved = user4; chatThreadClient.RemoveParticipant(identifier: participantToBeRemoved); Pageable <ChatParticipant> chatParticipantAfterOneDeleted = chatThreadClient.GetParticipants(); var chatParticipantAfterOneDeletedCount = chatParticipantAfterOneDeleted.Count(); Pageable <ChatMessage> messagesAfterParticipantRemoved = chatThreadClient.GetMessages(); ChatMessage participantRemovedMessage = messagesAfterParticipantRemoved.First(x => x.Type == ChatMessageType.ParticipantRemoved); Response typingNotificationResponse = chatThreadClient.SendTypingNotification(); chatThreadClient.SendTypingNotification(); Pageable <ChatThreadInfo> threads = chatClient.GetChatThreadsInfo(); var threadsCount = threads.Count(); chatClient.DeleteChatThread(threadId); //assert Assert.AreEqual(updatedTopic, chatThread.Topic); Assert.AreEqual(3, chatParticipantsOnCreationCount); Assert.AreEqual(messageContent, message.Content.Message); Assert.AreEqual(updatedMessageContent, actualUpdateMessage.Value.Content.Message); Assert.AreEqual(ChatMessageType.Text, message.Type); Assert.AreEqual(messageContent2, message2.Content.Message); Assert.AreEqual(displayNameMessage, message2.SenderDisplayName); 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, chatParticipantsAfterTwoAddedCount); Assert.AreEqual(4, chatParticipantAfterOneDeletedCount); Assert.IsNull(addChatParticipantsResult.Errors); Assert.AreEqual((int)HttpStatusCode.OK, typingNotificationResponse.Status); }
public IChatParticipantTypeChangedEvent <ChatParticipant> BuildChatParticipantTypeChangedEvent(Guid initiatorUserId, Guid chatId, ChatParticipant chatParticipant, ChatParticipantStatus?previousChatParticipantStatus) { return(new ChatParticipantTypeChangedEvent(initiatorUserId, chatId, chatParticipant)); }
public async Task <ChatRoom> JoinChatRoom(ChatParticipant participant) { ChatRoom room = await GetChatRoomById(participant.RoomId); return(room); }
public async Task CreateGetUpdateDeleteThreadAsync() { CommunicationIdentityClient communicationIdentityClient = new CommunicationIdentityClient(TestEnvironment.ConnectionString); Response <CommunicationUser> threadMember = await communicationIdentityClient.CreateUserAsync(); CommunicationUserToken communicationUserToken = await communicationIdentityClient.IssueTokenAsync(threadMember.Value, new[] { CommunicationTokenScope.Chat }); string userToken = communicationUserToken.Token; string endpoint = TestEnvironment.ChatApiUrl(); string threadCreatorId = communicationUserToken.User.Id; #region Snippet:Azure_Communication_Chat_Tests_Samples_CreateChatClient ChatClient chatClient = new ChatClient( new Uri(endpoint), new CommunicationUserCredential(userToken)); #endregion Snippet:Azure_Communication_Chat_Tests_Samples_CreateChatClient var threadCreator = new CommunicationUser(threadCreatorId); #region Snippet:Azure_Communication_Chat_Tests_Samples_CreateThread var chatParticipant = new ChatParticipant(threadCreator) { DisplayName = "UserDisplayName" }; ChatThreadClient chatThreadClient = await chatClient.CreateChatThreadAsync(topic : "Hello world!", participants : new[] { chatParticipant }); 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.UpdateTopicAsync(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 var josh = new ChatParticipant(new CommunicationUser("invalid user")); #region Snippet:Azure_Communication_Chat_Tests_Samples_Troubleshooting try { ChatThreadClient chatThreadClient_ = await chatClient.CreateChatThreadAsync(topic : "Hello world!", participants : new[] { josh }); } catch (RequestFailedException ex) { Console.WriteLine(ex.Message); } #endregion Snippet:Azure_Communication_Chat_Tests_Samples_Troubleshooting catch (Exception ex) { Assert.Fail($"Unexpected error: {ex}"); } }
public void RemoveUser(ChatParticipant user) { users.Remove(user); }
public void AddUser(ChatParticipant user) { users.Add(user); }
public async Task SendGetUpdateDeleteMessagesSendNotificationReadReceiptsAsync() { CommunicationIdentityClient communicationIdentityClient = new CommunicationIdentityClient(TestEnvironment.ConnectionString); Response <CommunicationUserIdentifier> threadMember = await communicationIdentityClient.CreateUserAsync(); AccessToken communicationUserToken = await communicationIdentityClient.GetTokenAsync(threadMember.Value, new[] { CommunicationTokenScope.Chat }); string userToken = communicationUserToken.Token; string theadCreatorMemberId = threadMember.Value.Id; ChatClient chatClient = new ChatClient( TestEnvironment.Endpoint, new CommunicationTokenCredential(userToken)); var chatParticipant = new ChatParticipant(new CommunicationUserIdentifier(theadCreatorMemberId)) { DisplayName = "UserDisplayName", ShareHistoryTime = DateTimeOffset.MinValue }; CreateChatThreadResult createChatThreadResult = await chatClient.CreateChatThreadAsync(topic : "Hello world!", participants : new[] { chatParticipant }); ChatThreadClient chatThreadClient = chatClient.GetChatThreadClient(createChatThreadResult.ChatThread.Id); #region Snippet:Azure_Communication_Chat_Tests_Samples_SendMessage SendChatMessageResult sendChatMessageResult = await chatThreadClient.SendMessageAsync(content : "hello world"); var messageId = sendChatMessageResult.Id; #endregion Snippet:Azure_Communication_Chat_Tests_SendMessage #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.Content.Message}"); } #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 <ChatMessageReadReceipt> allReadReceipts = chatThreadClient.GetReadReceiptsAsync(); await foreach (ChatMessageReadReceipt readReceipt in allReadReceipts) { Console.WriteLine($"{readReceipt.ChatMessageId}:{((CommunicationUserIdentifier)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(chatThreadClient.Id); }
/// <summary> /// Used by the Chat Participant to register it with the Manager. /// </summary> public void AddParticipant(ChatParticipant participant) { mParticipants.Add(participant); }
public ParticipationResult BuildParticipationResult(ChatParticipant chatParticipant, ChatParticipantStatus?previousChatParticipantStatus) { return(new ParticipationResult(previousChatParticipantStatus, chatParticipant)); }
public async Task CreateGetUpdateDeleteThreadAsync() { CommunicationIdentityClient communicationIdentityClient = new CommunicationIdentityClient(TestEnvironment.LiveTestDynamicConnectionString); Response <CommunicationUserIdentifier> threadCreatorIdentifier = await communicationIdentityClient.CreateUserAsync(); CommunicationUserIdentifier kimberly = await communicationIdentityClient.CreateUserAsync(); AccessToken communicationUserToken = await communicationIdentityClient.GetTokenAsync(threadCreatorIdentifier.Value, new[] { CommunicationTokenScope.Chat }); string userToken = communicationUserToken.Token; var endpoint = TestEnvironment.LiveTestDynamicEndpoint; #region Snippet:Azure_Communication_Chat_Tests_Samples_CreateChatClient ChatClient chatClient = new ChatClient( endpoint, new CommunicationTokenCredential(userToken)); #endregion Snippet:Azure_Communication_Chat_Tests_Samples_CreateChatClient #region Snippet:Azure_Communication_Chat_Tests_Samples_CreateThread var chatParticipant = new ChatParticipant(identifier: kimberly) { DisplayName = "Kim" }; CreateChatThreadResult createChatThreadResult = await chatClient.CreateChatThreadAsync(topic : "Hello world!", participants : new[] { chatParticipant }); string threadId = createChatThreadResult.ChatThread.Id; ChatThreadClient chatThreadClient = chatClient.GetChatThreadClient(threadId); #endregion Snippet:Azure_Communication_Chat_Tests_Samples_CreateThread #region Snippet:Azure_Communication_Chat_Tests_Samples_GetThread ChatThreadProperties chatThread = await chatThreadClient.GetPropertiesAsync(); #endregion Snippet:Azure_Communication_Chat_Tests_Samples_GetThread #region Snippet:Azure_Communication_Chat_Tests_Samples_GetThreads AsyncPageable <ChatThreadItem> chatThreadItems = chatClient.GetChatThreadsAsync(); await foreach (ChatThreadItem chatThreadItem in chatThreadItems) { Console.WriteLine($"{ chatThreadItem.Id}"); } #endregion Snippet:Azure_Communication_Chat_Tests_Samples_GetThreads #region Snippet:Azure_Communication_Chat_Tests_Samples_UpdateThread await chatThreadClient.UpdateTopicAsync(topic : "new 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 var josh = new ChatParticipant(new CommunicationUserIdentifier("invalid user")); #region Snippet:Azure_Communication_Chat_Tests_Samples_Troubleshooting try { CreateChatThreadResult createChatThreadErrorResult = await chatClient.CreateChatThreadAsync(topic : "Hello world!", participants : new[] { josh }); } catch (RequestFailedException ex) { Console.WriteLine(ex.Message); } #endregion Snippet:Azure_Communication_Chat_Tests_Samples_Troubleshooting catch (Exception ex) { Assert.Fail($"Unexpected error: {ex}"); } }
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); }
public void Init() { instance = new ChatParticipant(); }
public ChatMessageAddedEvent(Guid messageId, Guid chatId, DateTime date, string message, ChatParticipant author) { MessageId = messageId; ChatId = chatId; Date = date; Message = message; Author = author; }