Skip to content

Commit 952212e

Browse files
committed
fix(msc4459): fix room local image packs
1 parent 58a98b1 commit 952212e

4 files changed

Lines changed: 62 additions & 19 deletions

File tree

src/app/features/room/RoomInput.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,7 +1137,7 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
11371137
if (!imagePacksUsedRef.current.has(key))
11381138
imagePacksUsedRef.current.set(
11391139
key,
1140-
getImagePackReferencesForMxc(key, mx, ImageUsage.Emoticon)
1140+
getImagePackReferencesForMxc(key, mx, ImageUsage.Emoticon, room)
11411141
);
11421142
moveCursor(editor);
11431143
handleCloseAutocomplete();
@@ -1162,7 +1162,8 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
11621162
content[MATRIX_IMAGE_SOURCE_PACK_PROPERTY_NAME] = getImagePackReferencesForMxcWrappedInMap(
11631163
mxc,
11641164
mx,
1165-
ImageUsage.Sticker
1165+
ImageUsage.Sticker,
1166+
room
11661167
);
11671168

11681169
/**
@@ -1181,7 +1182,8 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
11811182
content[MATRIX_IMAGE_SOURCE_PACK_PROPERTY_NAME] = getImagePackReferencesForMxcWrappedInMap(
11821183
mxc,
11831184
mx,
1184-
ImageUsage.Sticker
1185+
ImageUsage.Sticker,
1186+
room
11851187
);
11861188

11871189
if (replyDraft) {

src/app/utils/matrix.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,12 @@ export const toggleReaction = (
420420
mx.sendEvent(
421421
room.roomId,
422422
EventType.Reaction as string as unknown as keyof TimelineEvents,
423-
getReactionContent(targetEventId, key, mx, rShortcode) as TimelineEvents[keyof TimelineEvents]
423+
getReactionContent(
424+
targetEventId,
425+
key,
426+
mx,
427+
room,
428+
rShortcode
429+
) as TimelineEvents[keyof TimelineEvents]
424430
);
425431
};

src/app/utils/messageReaction.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { MATRIX_IMAGE_SOURCE_PACK_PROPERTY_NAME } from '$types/matrix/common';
22
import type { MatrixReactionEvent } from '$types/matrix/common';
3-
import type { MatrixClient } from 'matrix-js-sdk';
3+
import type { MatrixClient, Room } from 'matrix-js-sdk';
44
import { ImageUsage } from '$plugins/custom-emoji';
55
import { getImagePackReferencesForMxcWrappedInMap } from './msc4459helper';
66

77
export const getReactionContent = (
88
eventId: string,
99
key: string,
1010
matrixClient: MatrixClient,
11+
room: Room,
1112
shortcode?: string
1213
): MatrixReactionEvent => ({
1314
'm.relates_to': {
@@ -20,6 +21,7 @@ export const getReactionContent = (
2021
[MATRIX_IMAGE_SOURCE_PACK_PROPERTY_NAME]: getImagePackReferencesForMxcWrappedInMap(
2122
key,
2223
matrixClient,
23-
ImageUsage.Emoticon
24+
ImageUsage.Emoticon,
25+
room
2426
),
2527
});

src/app/utils/msc4459helper.ts

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { getGlobalImagePacks } from '$plugins/custom-emoji/utils';
1+
import { getGlobalImagePacks, getRoomImagePacks } from '$plugins/custom-emoji/utils';
22
import type { ImagePack } from '$plugins/custom-emoji/ImagePack';
33
import type { MSC4459ImagePackReference } from '$types/matrix/common';
44
import { SerializableMap } from '$types/wrapper/SerializableMap';
5-
import type { MatrixClient } from 'matrix-js-sdk';
5+
import type { MatrixClient, Room } from 'matrix-js-sdk';
66
import type { ImageUsage } from '$plugins/custom-emoji';
77
import { SerializableSet } from '$types/wrapper/SerializableSet';
88
import { getViaServers } from '$plugins/via-servers';
@@ -12,26 +12,31 @@ import { isRoomPrivate } from './roomVisibility';
1212
export 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

Comments
 (0)