1- import { getGlobalImagePacks } from '$plugins/custom-emoji/utils' ;
1+ import { getGlobalImagePacks , getRoomImagePacks } from '$plugins/custom-emoji/utils' ;
22import type { ImagePack } from '$plugins/custom-emoji/ImagePack' ;
33import type { MSC4459ImagePackReference } from '$types/matrix/common' ;
44import { SerializableMap } from '$types/wrapper/SerializableMap' ;
5- import type { MatrixClient } from 'matrix-js-sdk' ;
5+ import type { MatrixClient , Room } from 'matrix-js-sdk' ;
66import type { ImageUsage } from '$plugins/custom-emoji' ;
77import { SerializableSet } from '$types/wrapper/SerializableSet' ;
88import { getViaServers } from '$plugins/via-servers' ;
@@ -12,26 +12,31 @@ import { isRoomPrivate } from './roomVisibility';
1212export function getImagePackReferencesForMxcWrappedInMap (
1313 mxcUrl : string ,
1414 matrixClient : MatrixClient ,
15- imageUsage : ImageUsage
15+ imageUsage : ImageUsage ,
16+ room : Room
1617) : SerializableMap < string , MSC4459ImagePackReference > {
1718 const retMap = new SerializableMap < string , MSC4459ImagePackReference > ( ) ;
18- retMap . set ( mxcUrl , getImagePackReferencesForMxc ( mxcUrl , matrixClient , imageUsage ) ) ;
19+ if ( ! mxcUrl . startsWith ( 'mxc' ) ) return retMap ;
20+ const result = getImagePackReferencesForMxc ( mxcUrl , matrixClient , imageUsage , room ) ;
21+ // if the result is undefined return the empty map, to not produce invalid entries
22+ if ( ! result ?. room_id ) return retMap ;
23+ retMap . set ( mxcUrl , result ) ;
1924 return retMap ;
2025}
2126
22- export function getImagePackReferencesForMxc (
27+ function getImagePackReferencesForMxcInternal (
2328 mxcUrl : string ,
2429 matrixClient : MatrixClient ,
25- imageUsage : ImageUsage
26- ) : MSC4459ImagePackReference {
27- const globalImgPacks : ImagePack [ ] = getGlobalImagePacks ( matrixClient ) ;
28- if ( ! mxcUrl . startsWith ( 'mxc' ) ) return { } ;
29- const imgPkRef = globalImgPacks
30+ packs : ImagePack [ ] ,
31+ imageUsage : ImageUsage ,
32+ bypassPrivateFilter = false
33+ ) {
34+ return packs
3035 . filter ( ( val ) => val . getImages ( imageUsage ) . find ( ( img ) => img . url === mxcUrl ) )
3136 . map ( ( pack ) => {
3237 const img = pack . getImages ( imageUsage ) . find ( ( val ) => val . url === mxcUrl ) ;
3338 const room = matrixClient . getRoom ( pack . address ?. roomId ) ;
34- if ( ! room || isRoomPrivate ( matrixClient , room ) ) return ;
39+ if ( ! room || ( isRoomPrivate ( matrixClient , room ) && ! bypassPrivateFilter ) ) return ;
3540 const viaServers = new SerializableSet < string > ( ) ;
3641 if ( room )
3742 getViaServers ( room ) . forEach ( ( via ) => {
@@ -47,6 +52,34 @@ export function getImagePackReferencesForMxc(
4752 shortcode : img ?. shortcode ,
4853 } satisfies MSC4459ImagePackReference ;
4954 } )
50- . at ( 0 ) ;
51- return imgPkRef ?? { } ;
55+ . find ( ( val ) => val != undefined ) ;
56+ }
57+
58+ export function getImagePackReferencesForMxc (
59+ mxcUrl : string ,
60+ matrixClient : MatrixClient ,
61+ imageUsage : ImageUsage ,
62+ room : Room
63+ ) : MSC4459ImagePackReference {
64+ if ( ! mxcUrl . startsWith ( 'mxc' ) ) return { } ;
65+ const globalImgPacks : ImagePack [ ] = getGlobalImagePacks ( matrixClient ) ;
66+ const roomLocalImgPacks : ImagePack [ ] = getRoomImagePacks ( room ) ;
67+ const roomLocalMatch = getImagePackReferencesForMxcInternal (
68+ mxcUrl ,
69+ matrixClient ,
70+ roomLocalImgPacks ,
71+ imageUsage ,
72+ true
73+ ) ;
74+ // prefer room local match as they're probably often more relevant
75+ if ( roomLocalMatch ) return roomLocalMatch ;
76+ const globalMatch = getImagePackReferencesForMxcInternal (
77+ mxcUrl ,
78+ matrixClient ,
79+ globalImgPacks ,
80+ imageUsage ,
81+ false
82+ ) ;
83+
84+ return globalMatch ?? { } ;
5285}
0 commit comments