Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Corrected GetConversationMembers to use TeamsInfo helper class rather… #5874

Merged
merged 3 commits into from
Sep 3, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
// Copyright (c) Microsoft Corporation. All rights reserved.

using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using AdaptiveExpressions.Properties;
using Microsoft.Bot.Connector;
using Microsoft.Bot.Schema;
using Newtonsoft.Json;

namespace Microsoft.Bot.Builder.Dialogs.Adaptive.Actions
Expand Down Expand Up @@ -83,12 +86,6 @@ public GetActivityMembers([CallerFilePath] string callerPath = "", [CallerLineNu
return await dc.EndDialogAsync(cancellationToken: cancellationToken).ConfigureAwait(false);
}

var bfAdapter = dc.Context.Adapter as BotFrameworkAdapter;
if (bfAdapter == null)
{
throw new InvalidOperationException("GetActivityMembers() only works with BotFrameworkAdapter");
}

string id = dc.Context.Activity.Id;
if (this.ActivityId != null)
{
Expand All @@ -101,7 +98,7 @@ public GetActivityMembers([CallerFilePath] string callerPath = "", [CallerLineNu
id = value as string;
}

var result = await bfAdapter.GetActivityMembersAsync(dc.Context, id, cancellationToken).ConfigureAwait(false);
var result = await GetActivityMembersAsync(dc.Context, id, cancellationToken).ConfigureAwait(false);

dc.State.SetValue(this.Property.GetValue(dc.State), result);

Expand All @@ -113,5 +110,31 @@ protected override string OnComputeId()
{
return $"{GetType().Name}[{this.ActivityId?.ToString() ?? string.Empty},{this.Property?.ToString() ?? string.Empty}]";
}

private async Task<IList<ChannelAccount>> GetActivityMembersAsync(ITurnContext turnContext, string activityId, CancellationToken cancellationToken)
{
// If no activity was passed in, use the current activity.
if (activityId == null)
{
activityId = turnContext.Activity.Id;
}

if (turnContext.Activity.Conversation == null)
{
throw new ArgumentException($"{nameof(GetActivityMembers)}.{nameof(GetActivityMembersAsync)}(): missing conversation");
}

if (string.IsNullOrWhiteSpace(turnContext.Activity.Conversation.Id))
{
throw new ArgumentException($"{nameof(GetActivityMembers)}.{nameof(GetActivityMembersAsync)}(): missing conversation.id");
}

var connectorClient = turnContext.TurnState.Get<IConnectorClient>();
EricDahlvang marked this conversation as resolved.
Show resolved Hide resolved
var conversationId = turnContext.Activity.Conversation.Id;

var accounts = await connectorClient.Conversations.GetActivityMembersAsync(conversationId, activityId, cancellationToken).ConfigureAwait(false);

return accounts;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
// Copyright (c) Microsoft Corporation. All rights reserved.

using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using AdaptiveExpressions.Properties;
using Microsoft.Bot.Connector;
using Microsoft.Bot.Schema;
using Newtonsoft.Json;

namespace Microsoft.Bot.Builder.Dialogs.Adaptive.Actions
Expand Down Expand Up @@ -43,7 +46,7 @@ public GetConversationMembers([CallerFilePath] string callerPath = "", [CallerLi
/// A boolean expression.
/// </value>
[JsonProperty("disabled")]
public BoolExpression Disabled { get; set; }
public BoolExpression Disabled { get; set; }

/// <summary>
/// Gets or sets property path to put the value in.
Expand Down Expand Up @@ -74,13 +77,8 @@ public GetConversationMembers([CallerFilePath] string callerPath = "", [CallerLi
return await dc.EndDialogAsync(cancellationToken: cancellationToken).ConfigureAwait(false);
}

var bfAdapter = dc.Context.Adapter as BotFrameworkAdapter;
if (bfAdapter == null)
{
throw new InvalidOperationException("GetConversationMembersAsync() only works with BotFrameworkAdapter");
}

var result = await bfAdapter.GetConversationMembersAsync(dc.Context, cancellationToken).ConfigureAwait(false);
var conversationId = dc.Context.Activity?.Conversation?.Id;
var result = await GetMembersAsync(GetConnectorClient(dc.Context), conversationId, cancellationToken).ConfigureAwait(false);

if (this.Property != null)
{
Expand All @@ -95,5 +93,21 @@ protected override string OnComputeId()
{
return $"{GetType().Name}[{this.Property?.ToString() ?? string.Empty}]";
}

private static async Task<IEnumerable<ChannelAccount>> GetMembersAsync(IConnectorClient connectorClient, string conversationId, CancellationToken cancellationToken)
Copy link
Contributor

@mrivera-ms mrivera-ms Sep 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IConnectorClient connectorClient, string conversationId

Can we pass ITurnContext here instead of connectorClient and conversationId? #Resolved

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I've pushed a change to simplify this area of the code. Thanks for the suggestion.

{
if (conversationId == null)
{
throw new InvalidOperationException("The GetMembersAsync operation needs a valid conversation Id.");
}

var teamMembers = await connectorClient.Conversations.GetConversationMembersAsync(conversationId, cancellationToken).ConfigureAwait(false);
return teamMembers;
}

private static IConnectorClient GetConnectorClient(ITurnContext turnContext)
{
return turnContext.TurnState.Get<IConnectorClient>() ?? throw new InvalidOperationException("This method requires a connector client.");
}
}
}