Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
33 changes: 28 additions & 5 deletions CommunityToolkit.Graph.Uwp/Controls/LoginButton/LoginButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Threading.Tasks;
using CommunityToolkit.Authentication;
using CommunityToolkit.Graph.Extensions;
using Microsoft.Graph;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
Expand Down Expand Up @@ -212,7 +213,13 @@ private async void OnProviderStateChanged(object sender, ProviderStateChangedEve
{
case ProviderState.SignedIn:
IsLoading = true;
await SetUserDetails();
if (!await TrySetUserDetailsAsync())
{
// Failed to retrieve user details, force signout.
await SignOutAsync();
return;
}

IsLoading = false;
LoginCompleted?.Invoke(this, new EventArgs());
break;
Expand Down Expand Up @@ -284,20 +291,36 @@ private async void LoadData()
}
}

private async Task SetUserDetails()
private async Task<bool> TrySetUserDetailsAsync()
{
User userDetails = null;

var provider = ProviderManager.Instance.GlobalProvider;
if (provider != null)
{
try
{
UserDetails = await provider.GetClient().GetMeAsync();
userDetails = await provider.GetClient().GetMeAsync();
}
catch
catch (Microsoft.Graph.ServiceException e)
{
// TODO: Handle if UserDetails is null.
if (e.StatusCode == System.Net.HttpStatusCode.Forbidden)
{
// Insufficient privileges.
// Incremental consent must not be supported by the current provider.
// TODO: Log or handle the lack of sufficient privileges.
}
else
{
// Something unexpected happened.
throw;
}
}
}

UserDetails = userDetails;

return UserDetails != null;
}

private void ClearUserDetails()
Expand Down
36 changes: 29 additions & 7 deletions CommunityToolkit.Graph.Uwp/Controls/PeoplePicker/PeoplePicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,25 @@ private async Task UpdateResultsAsync(string text)
IGraphServiceUsersCollectionPage usersCollection = null;
try
{
// Returns an empty collection if no results found.
usersCollection = await graph.FindUserAsync(text);
}
catch
catch (Microsoft.Graph.ServiceException e)
{
// No users found.
if (e.StatusCode == System.Net.HttpStatusCode.Forbidden)
{
// Insufficient privileges.
// Incremental consent must not be supported by the current provider.
// TODO: Log or handle the lack of sufficient privileges.
}
else
{
// Something unexpected happened.
throw;
}
}

if (usersCollection != null)
if (usersCollection != null && usersCollection.Count > 0)
{
foreach (var user in usersCollection.CurrentPage)
{
Expand All @@ -127,14 +138,25 @@ private async Task UpdateResultsAsync(string text)
IUserPeopleCollectionPage peopleCollection = null;
try
{
// Returns an empty collection if no results found.
peopleCollection = await graph.FindPersonAsync(text);
}
catch
catch (Microsoft.Graph.ServiceException e)
{
// No people found.
if (e.StatusCode == System.Net.HttpStatusCode.Forbidden)
{
// Insufficient privileges.
// Incremental consent must not be supported by the current provider.
// TODO: Log or handle the lack of sufficient privileges.
}
else
{
// Something unexpected happened.
throw;
}
}

if (peopleCollection != null)
if (peopleCollection != null && peopleCollection.Count > 0)
{
// Grab ids of current suggestions
var ids = list.Cast<object>().Select(person => (person as Person).Id);
Expand All @@ -152,4 +174,4 @@ private async Task UpdateResultsAsync(string text)
}
}
}
}
}
123 changes: 81 additions & 42 deletions CommunityToolkit.Graph.Uwp/Controls/PersonView/PersonView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,11 @@ private async void LoadData()

if (provider?.State == ProviderState.SignedIn)
{
await TryLoadPersonDetailsAsync();
if (!await TryLoadPersonDetailsAsync())
{
// TODO: Handle failure to load the PersonDetails.
}

UpdateVisual();
}
else
Expand All @@ -132,6 +136,12 @@ private async void LoadData()

private async void UpdateVisual()
{
if (PersonDetails == null)
{
LoadDefaultImage();
return;
}

if (PersonAvatarType is PersonAvatarType.Initials)
{
var initialsLoaded = TryLoadInitials();
Expand All @@ -144,31 +154,24 @@ private async void UpdateVisual()
LoadDefaultImage();
}
}
else if (PersonDetails != null)
else if (PersonDetails.Id != _photoId)
{
if (PersonDetails.Id != _photoId)
var photoLoaded = await TryLoadUserPhotoAsync();
if (photoLoaded)
{
LoadDefaultImage();
UpdateImageSize();
}
else
{
ClearUserPhoto();

var photoLoaded = await TryLoadUserPhotoAsync();
if (photoLoaded)
{
UpdateImageSize();
}
else
var initialsLoaded = TryLoadInitials();
if (!initialsLoaded)
{
var initialsLoaded = TryLoadInitials();
if (initialsLoaded)
{
ClearUserPhoto();
}
LoadDefaultImage();
}
}
}
else
{
LoadDefaultImage();
}
}

private void LoadDefaultImage()
Expand Down Expand Up @@ -200,6 +203,7 @@ private void ClearUserPhoto()

private async Task<bool> TryLoadPersonDetailsAsync()
{
// TODO: Better guarding.
if (PersonDetails != null)
{
return true;
Expand All @@ -208,50 +212,61 @@ private async Task<bool> TryLoadPersonDetailsAsync()
var provider = ProviderManager.Instance.GlobalProvider;
if (provider?.State != ProviderState.SignedIn)
{
PersonDetails = null;
return false;
}

var graph = provider.GetClient();

try
{
if (!string.IsNullOrWhiteSpace(UserId))
{
var user = await provider.GetClient().GetUserAsync(UserId);
var user = await graph.GetUserAsync(UserId);
PersonDetails = user.ToPerson();
}
else if (PersonQuery?.ToLowerInvariant() == PersonQueryMe)
{
var user = await provider.GetClient().GetMeAsync();
var user = await graph.GetMeAsync();
PersonDetails = user.ToPerson();
}
else if (!string.IsNullOrWhiteSpace(PersonQuery))
{
var people = await provider.GetClient().FindPersonAsync(PersonQuery);
var people = await graph.FindPersonAsync(PersonQuery);
if (people != null && people.Count > 0)
{
var person = people.FirstOrDefault();
PersonDetails = person;
PersonDetails = people.FirstOrDefault();
}
}

return PersonDetails != null;
}
catch
catch (Microsoft.Graph.ServiceException e)
{
// TODO: Log exception
if (e.StatusCode == System.Net.HttpStatusCode.Forbidden)
{
// Insufficient privileges.
// Incremental consent must not be supported by the current provider.
// TODO: Log or handle the lack of sufficient privileges.
}
else
{
// Something unexpected happened.
throw;
}
}

return false;
return PersonDetails != null;
}

private async Task<bool> TryLoadUserPhotoAsync()
{
// TODO: Better guarding.
var person = PersonDetails;
if (person == null)
{
return false;
}

if (PersonDetails.Id == _photoId && UserPhoto != null && UserPhoto != _defaultImage)
if (person.Id == _photoId && UserPhoto != null && UserPhoto != _defaultImage)
{
return true;
}
Expand All @@ -264,27 +279,51 @@ private async Task<bool> TryLoadUserPhotoAsync()

Stream photoStream = null;

// TODO: Better guarding
try
{
var graph = ProviderManager.Instance.GlobalProvider?.GetBetaClient();
photoStream = await graph.GetUserPhoto(person.Id);
}
catch
catch (Microsoft.Graph.ServiceException e)
{
// TODO: Log exception
if (e.StatusCode == System.Net.HttpStatusCode.Forbidden)
{
// Insufficient privileges.
// Incremental consent must not be supported by the current provider.
// TODO: Log or handle the lack of sufficient privileges.
}
else if (e.StatusCode == System.Net.HttpStatusCode.BadRequest)
{
// This entity does not support profile photos.
}
else if (e.StatusCode == System.Net.HttpStatusCode.NotFound)
{
// Image not found.
}
else if ((int)e.StatusCode == 422)
{
// IncorrectRecipientTypeDetected:
// Incorrect recipient type detected in request url. Retry with Groups as the object type.
}
else
{
// Something unexpected happened.
throw;
}
}

if (photoStream != null)
if (photoStream == null)
{
var decodeResults = await TryDecodeStreamAsync(photoStream);
if (decodeResults.Success)
{
UserPhoto = decodeResults.Image;
_photoId = person.Id;
return false;
}

return true;
}
var decodeResults = await TryDecodeStreamAsync(photoStream);
if (decodeResults.Success)
{
UserPhoto = decodeResults.Image;
_photoId = person.Id;

return true;
}

return false;
Expand Down Expand Up @@ -341,4 +380,4 @@ private bool TryLoadInitials()
return (false, null);
}
}
}
}
22 changes: 7 additions & 15 deletions CommunityToolkit.Graph/Extensions/GraphExtensions.People.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,13 @@ public static partial class GraphExtensions
/// <returns><see cref="IUserPeopleCollectionPage"/> collection of <see cref="Person"/>.</returns>
public static async Task<IUserPeopleCollectionPage> FindPersonAsync(this GraphServiceClient graph, string query)
{
try
{
return await graph
.Me
.People
.Request()
.Search(query)
////.WithScopes(new string[] { "people.read" })
.GetAsync();
}
catch
{
}

return new UserPeopleCollectionPage();
return await graph
.Me
.People
.Request()
.Search(query)
.WithScopes(new string[] { "people.read" })
.GetAsync();
}
}
}