@@ -9,6 +9,17 @@ import { getViaServers } from '$plugins/via-servers';
99import { getMxIdServer } from './mxIdHelper' ;
1010import { isRoomPrivate } from './roomVisibility' ;
1111
12+ /**
13+ * lookup table for global mxc => MSC4459ImagePackReference
14+ * TODO this is far from a perfect solution
15+ */
16+ const globalLookupTable = new Map < string , MSC4459ImagePackReference > ( ) ;
17+ /**
18+ * lookup table for room local mxc => MSC4459ImagePackReference
19+ * TODO this is far from a perfect solution
20+ */
21+ const roomLookupTable = new Map < string , Map < string , MSC4459ImagePackReference > > ( ) ;
22+
1223export function getImagePackReferencesForMxcWrappedInMap (
1324 mxcUrl : string ,
1425 matrixClient : MatrixClient ,
@@ -62,7 +73,8 @@ export function getImagePackReferencesForMxc(
6273 room : Room
6374) : MSC4459ImagePackReference {
6475 if ( ! mxcUrl . startsWith ( 'mxc' ) ) return { } ;
65- const globalImgPacks : ImagePack [ ] = getGlobalImagePacks ( matrixClient ) ;
76+ if ( roomLookupTable . get ( room . roomId ) ?. has ( mxcUrl ) )
77+ return roomLookupTable . get ( room . roomId ) ! . get ( mxcUrl ) ! ;
6678 const roomLocalImgPacks : ImagePack [ ] = getRoomImagePacks ( room ) ;
6779 const roomLocalMatch = getImagePackReferencesForMxcInternal (
6880 mxcUrl ,
@@ -71,15 +83,25 @@ export function getImagePackReferencesForMxc(
7183 imageUsage ,
7284 true
7385 ) ;
86+ if ( roomLocalMatch ) {
87+ const roomLookupTabRes =
88+ roomLookupTable . get ( room . roomId ) ?? new Map < string , MSC4459ImagePackReference > ( ) ;
89+ roomLookupTabRes . set ( mxcUrl , roomLocalMatch ) ;
90+ roomLookupTable . set ( room . roomId , roomLookupTabRes ) ;
91+ }
7492 // prefer room local match as they're probably often more relevant
7593 if ( roomLocalMatch ) return roomLocalMatch ;
94+ // simple caching
95+ if ( globalLookupTable . has ( mxcUrl ) ) return globalLookupTable . get ( mxcUrl ) ! ;
96+ const globalImgPacks : ImagePack [ ] = getGlobalImagePacks ( matrixClient ) ;
7697 const globalMatch = getImagePackReferencesForMxcInternal (
7798 mxcUrl ,
7899 matrixClient ,
79100 globalImgPacks ,
80101 imageUsage ,
81102 false
82103 ) ;
104+ if ( globalMatch ) globalLookupTable . set ( mxcUrl , globalMatch ) ;
83105
84106 return globalMatch ?? { } ;
85107}
0 commit comments