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()); }
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()); }