Skip to content

Commit 6f2d44e

Browse files
authored
Merge pull request #753 from microsoftgraph/nmetulev/useContactApis
Added useContactsApis global config to person and person card
2 parents 96ed37b + b0ab2a6 commit 6f2d44e

File tree

3 files changed

+75
-15
lines changed

3 files changed

+75
-15
lines changed

packages/mgt/src/components/mgt-person-card/mgt-person-card.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,22 @@ import { MgtPersonCardMessages } from './sections/mgt-person-card-messages/mgt-p
2626
import { MgtPersonCardOrganization } from './sections/mgt-person-card-organization/mgt-person-card-organization';
2727
import { MgtPersonCardProfile } from './sections/mgt-person-card-profile/mgt-person-card-profile';
2828

29+
/**
30+
* Configuration object for the Person Card component
31+
*
32+
* @export
33+
* @interface MgtPersonConfig
34+
*/
35+
export interface MgtPersonCardConfig {
36+
/**
37+
* Sets or gets whether the person card component can use Contacts APIs to
38+
* find contacts and their images
39+
*
40+
* @type {boolean}
41+
*/
42+
useContactApis: boolean;
43+
}
44+
2945
/**
3046
* Web Component used to show detailed data for a person in the Microsoft Graph
3147
*
@@ -55,6 +71,18 @@ export class MgtPersonCard extends MgtTemplatedComponent {
5571
return styles;
5672
}
5773

74+
/**
75+
* Global configuration object for
76+
* all person card components
77+
*
78+
* @static
79+
* @type {MgtPersonCardConfig}
80+
* @memberof MgtPersonCard
81+
*/
82+
public static config: MgtPersonCardConfig = {
83+
useContactApis: true
84+
};
85+
5886
/**
5987
* Set the person details to render
6088
*
@@ -587,7 +615,7 @@ export class MgtPersonCard extends MgtTemplatedComponent {
587615
((this.fetchImage && !this.personImage) || this.personImage === '@')
588616
) {
589617
// in some cases we might only have name or email, but need to find the image
590-
const image = await getPersonImage(graph, this.personDetails);
618+
const image = await getPersonImage(graph, this.personDetails, MgtPersonCard.config.useContactApis);
591619
if (image) {
592620
this.personDetails.personImage = image;
593621
this.personImage = image;
@@ -605,7 +633,7 @@ export class MgtPersonCard extends MgtTemplatedComponent {
605633

606634
if (people && people.length) {
607635
this.personDetails = people[0];
608-
const image = await getPersonImage(graph, this.personDetails);
636+
const image = await getPersonImage(graph, this.personDetails, MgtPersonCard.config.useContactApis);
609637
if (image) {
610638
this.personDetails.personImage = image;
611639
this.personImage = image;

packages/mgt/src/components/mgt-person/mgt-person.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,22 @@ export enum PersonViewType {
5353
threelines = 5
5454
}
5555

56+
/**
57+
* Configuration object for the Person component
58+
*
59+
* @export
60+
* @interface MgtPersonConfig
61+
*/
62+
export interface MgtPersonConfig {
63+
/**
64+
* Sets or gets whether the person component can use Contacts APIs to
65+
* find contacts and their images
66+
*
67+
* @type {boolean}
68+
*/
69+
useContactApis: boolean;
70+
}
71+
5672
/**
5773
* The person component is used to display a person or contact by using their photo, name, and/or email address.
5874
*
@@ -92,6 +108,18 @@ export class MgtPerson extends MgtTemplatedComponent {
92108
return styles;
93109
}
94110

111+
/**
112+
* Global Configuration object for all
113+
* person components
114+
*
115+
* @static
116+
* @type {MgtPersonConfig}
117+
* @memberof MgtPerson
118+
*/
119+
public static config: MgtPersonConfig = {
120+
useContactApis: true
121+
};
122+
95123
/**
96124
* allows developer to define name of person for component
97125
* @type {string}
@@ -746,7 +774,7 @@ export class MgtPerson extends MgtTemplatedComponent {
746774
!this.personDetails.personImage &&
747775
((this.fetchImage && !this.personImage && !this._fetchedImage) || this.personImage === '@')
748776
) {
749-
const image = await getPersonImage(graph, this.personDetails);
777+
const image = await getPersonImage(graph, this.personDetails, MgtPerson.config.useContactApis);
750778
if (image) {
751779
this.personDetails.personImage = image;
752780
this._fetchedImage = image;
@@ -764,7 +792,7 @@ export class MgtPerson extends MgtTemplatedComponent {
764792

765793
if (people && people.length) {
766794
this.personDetails = people[0];
767-
const image = await getPersonImage(graph, people[0]);
795+
const image = await getPersonImage(graph, people[0], MgtPerson.config.useContactApis);
768796

769797
if (image) {
770798
this.personDetails.personImage = image;

packages/mgt/src/graph/graph.photos.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -201,14 +201,16 @@ export async function myPhoto(graph: IGraph): Promise<string> {
201201
*
202202
* @export
203203
*/
204-
export async function getPersonImage(graph: IGraph, person: IDynamicPerson) {
204+
export async function getPersonImage(graph: IGraph, person: IDynamicPerson, useContactsApis: boolean = true) {
205205
// handle if contact
206206
if ('personType' in person && (person as any).personType.subclass === 'PersonalContact') {
207-
// if person is a contact, look for them and their photo in contact api
208-
let email = getEmailFromGraphEntity(person);
209-
const contact = await findContactsByEmail(graph, email);
210-
if (contact && contact.length && contact[0].id) {
211-
return await getContactPhoto(graph, contact[0].id);
207+
if (useContactsApis) {
208+
// if person is a contact, look for them and their photo in contact api
209+
const email = getEmailFromGraphEntity(person);
210+
const contact = await findContactsByEmail(graph, email);
211+
if (contact && contact.length && contact[0].id) {
212+
return await getContactPhoto(graph, contact[0].id);
213+
}
212214
}
213215

214216
return null;
@@ -223,14 +225,14 @@ export async function getPersonImage(graph: IGraph, person: IDynamicPerson) {
223225

224226
// else assume id is for user and try to get photo
225227
if (person.id) {
226-
let image = await getUserPhoto(graph, person.id);
228+
const image = await getUserPhoto(graph, person.id);
227229
if (image) {
228230
return image;
229231
}
230232
}
231233

232234
// let's try to find a person by the email
233-
let email = getEmailFromGraphEntity(person);
235+
const email = getEmailFromGraphEntity(person);
234236

235237
if (email) {
236238
// try to find user
@@ -240,9 +242,11 @@ export async function getPersonImage(graph: IGraph, person: IDynamicPerson) {
240242
}
241243

242244
// if no user, try to find a contact
243-
const contacts = await findContactsByEmail(graph, email);
244-
if (contacts && contacts.length) {
245-
return await getContactPhoto(graph, contacts[0].id);
245+
if (useContactsApis) {
246+
const contacts = await findContactsByEmail(graph, email);
247+
if (contacts && contacts.length) {
248+
return await getContactPhoto(graph, contacts[0].id);
249+
}
246250
}
247251
}
248252

0 commit comments

Comments
 (0)