Skip to content

Commit

Permalink
Fix for the issue - CC sends 1:1 messages to guest users from differe…
Browse files Browse the repository at this point in the history
…nt tenants (OfficeDev#420)

* Fixed issue - sending 1:1 messages to guest users.
  • Loading branch information
Mikhail2k15 authored Jun 9, 2021
1 parent 9f23314 commit ca2b9e5
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,18 @@ public async Task InstallAppForUserAsync(string appId, string userId)
},
};

await this.graphServiceClient.Users[userId]
// Skip Guest users.
var user = await this.graphServiceClient.Users[userId].Request().GetAsync();
if (!string.Equals(user.UserType, "Guest", StringComparison.OrdinalIgnoreCase)
&& !user.UserPrincipalName.ToLower().Contains("#ext#"))
{
await this.graphServiceClient.Users[userId]
.Teamwork
.InstalledApps
.Request()
.WithMaxRetry(GraphConstants.MaxRetry)
.AddAsync(userScopeTeamsAppInstallation);
}
}

/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,10 @@ private async Task<IEnumerable<TeamsChannelAccount>> GetMembersAsync(
continuationToken,
cancellationToken);
continuationToken = currentPage.ContinuationToken;
members.AddRange(currentPage.Members);

// Skip Guest users.
var membersWithoutGuests = currentPage.Members.Where(member => !member.UserPrincipalName.ToLower().Contains("#ext#"));
members.AddRange(membersWithoutGuests);
}
while (continuationToken != null && !cancellationToken.IsCancellationRequested);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,20 @@ private async Task<IEnumerable<SentNotificationDataEntity>> GetRecipientsAsync(s
var maxParallelism = Math.Min(100, users.Count());
await Task.WhenAll(users.ForEachAsync(maxParallelism, async user =>
{
var userEntity = await this.userDataRepository.GetAsync(UserDataTableNames.UserDataPartition, user.Id);
if (userEntity == null)
// Skip Guest users.
if (!user.UserPrincipalName.ToLower().Contains("#ext#"))
{
userEntity = new UserDataEntity()
var userEntity = await this.userDataRepository.GetAsync(UserDataTableNames.UserDataPartition, user.Id);
if (userEntity == null)
{
AadId = user.Id,
};
}
userEntity = new UserDataEntity()
{
AadId = user.Id,
};
}

recipients.Add(userEntity.CreateInitialSentNotificationDataEntity(partitionKey: notificationId));
recipients.Add(userEntity.CreateInitialSentNotificationDataEntity(partitionKey: notificationId));
}
}));

return recipients;
Expand Down
17 changes: 15 additions & 2 deletions Source/CompanyCommunicator/Bot/TeamsDataCapture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ namespace Microsoft.Teams.Apps.CompanyCommunicator.Bot
{
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Teams;
using Microsoft.Bot.Schema;
using Microsoft.Bot.Schema.Teams;
using Microsoft.Teams.Apps.CompanyCommunicator.Common.Repositories.TeamData;
using Microsoft.Teams.Apps.CompanyCommunicator.Common.Services;
using Microsoft.Teams.Apps.CompanyCommunicator.Common.Services.User;
Expand Down Expand Up @@ -45,9 +49,12 @@ public TeamsDataCapture(
/// <summary>
/// Add channel or personal data in Table Storage.
/// </summary>
/// <param name="turnContext">The context object for this turn.</param>
/// <param name="activity">Teams activity instance.</param>
/// <param name="cancellationToken">A cancellation token that can be used by other objects
/// or threads to receive notice of cancellation.</param>
/// <returns>A task that represents the work queued to execute.</returns>
public async Task OnBotAddedAsync(IConversationUpdateActivity activity)
public async Task OnBotAddedAsync(ITurnContext turnContext, IConversationUpdateActivity activity, CancellationToken cancellationToken)
{
// Take action if the event includes the bot being added.
var membersAdded = activity.MembersAdded;
Expand All @@ -62,7 +69,13 @@ public async Task OnBotAddedAsync(IConversationUpdateActivity activity)
await this.teamDataRepository.SaveTeamDataAsync(activity);
break;
case TeamsDataCapture.PersonalType:
await this.userDataService.SaveUserDataAsync(activity);
// Skip Guest users.
TeamsChannelAccount teamsUser = await TeamsInfo.GetMemberAsync(turnContext, activity.Recipient.Id, cancellationToken);
if (!teamsUser.UserPrincipalName.ToLower().Contains("#ext#"))
{
await this.userDataService.SaveUserDataAsync(activity);
}

break;
default: break;
}
Expand Down
2 changes: 1 addition & 1 deletion Source/CompanyCommunicator/Bot/UserTeamsActivityHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected override async Task OnConversationUpdateActivityAsync(

if (activity.MembersAdded != null)
{
await this.teamsDataCapture.OnBotAddedAsync(activity);
await this.teamsDataCapture.OnBotAddedAsync(turnContext, activity, cancellationToken);
}

if (activity.MembersRemoved != null)
Expand Down

0 comments on commit ca2b9e5

Please sign in to comment.