@@ -15,7 +15,8 @@ import {
1515 getPhotoFromCache ,
1616 getPhotoInvalidationTime ,
1717 photosCacheEnabled ,
18- storePhotoInCache
18+ storePhotoInCache ,
19+ CachePhoto
1920} from './graph.photos' ;
2021import { IDynamicPerson } from './types' ;
2122
@@ -114,6 +115,10 @@ export async function getUserWithPhoto(graph: IGraph, userId?: string): Promise<
114115 let cache : CacheStore < CacheUser > ;
115116 let photo = null ;
116117 let user : IDynamicPerson ;
118+ let cachedPhoto : CachePhoto ;
119+ const resource = userId ? `users/${ userId } ` : 'me' ;
120+ const scopes = userId ? [ 'user.readbasic.all' ] : [ 'user.read' ] ;
121+
117122 // attempt to get user and photo from cache if enabled
118123 if ( usersCacheEnabled ( ) ) {
119124 cache = CacheService . getCache < CacheUser > ( cacheSchema , userStore ) ;
@@ -123,11 +128,21 @@ export async function getUserWithPhoto(graph: IGraph, userId?: string): Promise<
123128 }
124129 }
125130 if ( photosCacheEnabled ( ) ) {
126- const cachedPhoto = await getPhotoFromCache ( userId || 'me' , 'users' ) ;
131+ cachedPhoto = await getPhotoFromCache ( userId || 'me' , 'users' ) ;
127132 if ( cachedPhoto && getPhotoInvalidationTime ( ) > Date . now ( ) - cachedPhoto . timeCached ) {
128133 photo = cachedPhoto . photo ;
134+ } else if ( cachedPhoto ) {
135+ try {
136+ const response = await graph . api ( `${ resource } /photo` ) . get ( ) ;
137+ if ( response && response [ '@odata.mediaEtag' ] && response [ '@odata.mediaEtag' ] === cachedPhoto . eTag ) {
138+ // put current image into the cache to update the timestamp since etag is the same
139+ storePhotoInCache ( userId || 'me' , 'users' , cachedPhoto ) ;
140+ photo = cachedPhoto . photo ;
141+ }
142+ } catch ( e ) { }
129143 }
130144 }
145+
131146 if ( ! photo && ! user ) {
132147 // batch calls
133148 const batch = graph . createBatch ( ) ;
@@ -139,8 +154,17 @@ export async function getUserWithPhoto(graph: IGraph, userId?: string): Promise<
139154 batch . get ( 'photo' , 'me/photo/$value' , [ 'user.read' ] ) ;
140155 }
141156 const response = await batch . executeAll ( ) ;
157+ const eTag = response . get ( 'photo' ) . headers [ 'ETag' ] ;
142158 photo = response . get ( 'photo' ) . content ;
143159 user = response . get ( 'user' ) . content ;
160+
161+ // store user & photo in their respective cache
162+ if ( usersCacheEnabled ( ) ) {
163+ cache . putValue ( userId || 'me' , { user : JSON . stringify ( user ) } ) ;
164+ }
165+ if ( photosCacheEnabled ( ) ) {
166+ storePhotoInCache ( userId || 'me' , 'users' , { eTag, photo : photo } ) ;
167+ }
144168 } else if ( ! photo ) {
145169 // get photo from graph
146170 const resource = userId ? `users/${ userId } ` : 'me' ;
@@ -165,9 +189,9 @@ export async function getUserWithPhoto(graph: IGraph, userId?: string): Promise<
165189 . get ( ) ;
166190 if ( response ) {
167191 if ( usersCacheEnabled ( ) ) {
168- cache . putValue ( userId || 'me' , { user : JSON . stringify ( response . content ) } ) ;
192+ cache . putValue ( userId || 'me' , { user : JSON . stringify ( response ) } ) ;
169193 }
170- user = response . content ;
194+ user = response ;
171195 }
172196 }
173197 person = user ;
0 commit comments