@@ -111,20 +111,23 @@ export async function getMe(graph: IGraph): Promise<User> {
111111 * @memberof Graph
112112 */
113113export async function getUserWithPhoto ( graph : IGraph , userId ?: string ) : Promise < IDynamicPerson > {
114- let person = null as IDynamicPerson ;
115- let cache : CacheStore < CacheUser > ;
116114 let photo = null ;
117- let user : IDynamicPerson ;
115+ let user : IDynamicPerson = null ;
116+
118117 let cachedPhoto : CachePhoto ;
118+ let cachedUser : CacheUser ;
119+
119120 const resource = userId ? `users/${ userId } ` : 'me' ;
120121 const scopes = userId ? [ 'user.readbasic.all' ] : [ 'user.read' ] ;
121122
122123 // attempt to get user and photo from cache if enabled
123124 if ( usersCacheEnabled ( ) ) {
124- cache = CacheService . getCache < CacheUser > ( cacheSchema , userStore ) ;
125- const cachedUser = await cache . getValue ( userId || 'me' ) ;
125+ let cache = CacheService . getCache < CacheUser > ( cacheSchema , userStore ) ;
126+ cachedUser = await cache . getValue ( userId || 'me' ) ;
126127 if ( cachedUser && getUserInvalidationTime ( ) > Date . now ( ) - cachedUser . timeCached ) {
127- user = JSON . parse ( cachedUser . user ) ;
128+ user = cachedUser . user ? JSON . parse ( cachedUser . user ) : null ;
129+ } else {
130+ cachedUser = null ;
128131 }
129132 }
130133 if ( photosCacheEnabled ( ) ) {
@@ -138,12 +141,15 @@ export async function getUserWithPhoto(graph: IGraph, userId?: string): Promise<
138141 // put current image into the cache to update the timestamp since etag is the same
139142 storePhotoInCache ( userId || 'me' , 'users' , cachedPhoto ) ;
140143 photo = cachedPhoto . photo ;
144+ } else {
145+ cachedPhoto = null ;
141146 }
142147 } catch ( e ) { }
143148 }
144149 }
145150
146- if ( ! photo && ! user ) {
151+ // if both are not in the cache, batch get them
152+ if ( ! cachedPhoto && ! cachedUser ) {
147153 let eTag : string ;
148154
149155 // batch calls
@@ -170,45 +176,41 @@ export async function getUserWithPhoto(graph: IGraph, userId?: string): Promise<
170176
171177 // store user & photo in their respective cache
172178 if ( usersCacheEnabled ( ) ) {
179+ let cache = CacheService . getCache < CacheUser > ( cacheSchema , userStore ) ;
173180 cache . putValue ( userId || 'me' , { user : JSON . stringify ( user ) } ) ;
174181 }
175182 if ( photosCacheEnabled ( ) ) {
176183 storePhotoInCache ( userId || 'me' , 'users' , { eTag, photo : photo } ) ;
177184 }
178- } else if ( ! photo ) {
179- // get photo from graph
180- const resource = userId ? `users/${ userId } ` : 'me' ;
181- const scopes = userId ? [ 'user.readbasic.all' ] : [ 'user.read' ] ;
185+ } else if ( ! cachedPhoto ) {
186+ // if only photo or user is not cached, get it individually
182187 const response = await getPhotoForResource ( graph , resource , scopes ) ;
183188 if ( response ) {
184189 if ( photosCacheEnabled ( ) ) {
185190 storePhotoInCache ( userId || 'me' , 'users' , { eTag : response . eTag , photo : response . photo } ) ;
186191 }
187192 photo = response . photo ;
188193 }
189- } else if ( ! user ) {
194+ } else if ( ! cachedUser ) {
190195 // get user from graph
191- const response = userId
192- ? await graph
193- . api ( `/users/${ userId } ` )
194- . middlewareOptions ( prepScopes ( 'user.readbasic.all' ) )
195- . get ( )
196- : await graph
197- . api ( 'me' )
198- . middlewareOptions ( prepScopes ( 'user.read' ) )
199- . get ( ) ;
196+ const response = await graph
197+ . api ( resource )
198+ . middlewareOptions ( prepScopes ( ...scopes ) )
199+ . get ( ) ;
200+
200201 if ( response ) {
201202 if ( usersCacheEnabled ( ) ) {
203+ let cache = CacheService . getCache < CacheUser > ( cacheSchema , userStore ) ;
202204 cache . putValue ( userId || 'me' , { user : JSON . stringify ( response ) } ) ;
203205 }
204206 user = response ;
205207 }
206208 }
209+
207210 if ( user ) {
208- person = user ;
209- person . personImage = photo ;
211+ user . personImage = photo ;
210212 }
211- return person ;
213+ return user ;
212214}
213215
214216/**
@@ -229,7 +231,7 @@ export async function getUser(graph: IGraph, userPrincipleName: string): Promise
229231 // is it stored and is timestamp good?
230232 if ( user && getUserInvalidationTime ( ) > Date . now ( ) - user . timeCached ) {
231233 // return without any worries
232- return JSON . parse ( user . user ) ;
234+ return user . user ? JSON . parse ( user . user ) : null ;
233235 }
234236 }
235237 // else we must grab it
@@ -271,7 +273,7 @@ export async function getUsersForUserIds(graph: IGraph, userIds: string[]): Prom
271273 user = await cache . getValue ( id ) ;
272274 }
273275 if ( user && getUserInvalidationTime ( ) > Date . now ( ) - user . timeCached ) {
274- peopleDict [ id ] = JSON . parse ( user . user ) ;
276+ peopleDict [ id ] = user . user ? JSON . parse ( user . user ) : null ;
275277 } else if ( id !== '' ) {
276278 batch . get ( id , `/users/${ id } ` , [ 'user.readbasic.all' ] ) ;
277279 notInCache . push ( id ) ;
@@ -399,14 +401,18 @@ export async function findUsers(graph: IGraph, query: string, top: number = 10):
399401 }
400402 }
401403
402- const graphResult = await graph
403- . api ( 'users' )
404- . filter (
405- `startswith(displayName,'${ query } ') or startswith(givenName,'${ query } ') or startswith(surname,'${ query } ') or startswith(mail,'${ query } ') or startswith(userPrincipalName,'${ query } ')`
406- )
407- . top ( top )
408- . middlewareOptions ( prepScopes ( scopes ) )
409- . get ( ) ;
404+ let graphResult ;
405+
406+ try {
407+ graphResult = await graph
408+ . api ( 'users' )
409+ . filter (
410+ `startswith(displayName,'${ query } ') or startswith(givenName,'${ query } ') or startswith(surname,'${ query } ') or startswith(mail,'${ query } ') or startswith(userPrincipalName,'${ query } ')`
411+ )
412+ . top ( top )
413+ . middlewareOptions ( prepScopes ( scopes ) )
414+ . get ( ) ;
415+ } catch { }
410416
411417 if ( usersCacheEnabled ( ) && graphResult ) {
412418 item . results = graphResult . value . map ( userStr => JSON . stringify ( userStr ) ) ;
0 commit comments