From 4a6c53703f4f7fe82f151601dc90caab8ea9fa21 Mon Sep 17 00:00:00 2001 From: Ajay Bura <32841439+ajbura@users.noreply.github.com> Date: Wed, 21 Jun 2023 20:59:02 +1000 Subject: [PATCH] fix global pack showing all room packs (#1303) --- src/app/plugins/custom-emoji.ts | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/src/app/plugins/custom-emoji.ts b/src/app/plugins/custom-emoji.ts index daceef4..ffb3794 100644 --- a/src/app/plugins/custom-emoji.ts +++ b/src/app/plugins/custom-emoji.ts @@ -1,4 +1,4 @@ -import { IImageInfo, MatrixClient, Room } from 'matrix-js-sdk'; +import { IImageInfo, MatrixClient, MatrixEvent, Room } from 'matrix-js-sdk'; import { AccountDataEvent } from '../../types/matrix/accountData'; import { getAccountData, getStateEvents } from '../utils/room'; import { StateEvent } from '../../types/matrix/room'; @@ -225,21 +225,24 @@ export class ImagePack { } } -export function getRoomImagePacks(room: Room): ImagePack[] { - const dataEvents = getStateEvents(room, StateEvent.PoniesRoomEmotes); - - return dataEvents.reduce((roomPacks, packEvent) => { +export function packEventsToImagePacks(packEvents: MatrixEvent[]): ImagePack[] { + return packEvents.reduce((imagePacks, packEvent) => { const packId = packEvent?.getId(); const content = packEvent?.getContent() as PackContent | undefined; - if (!packId || !content) return roomPacks; + if (!packId || !content) return imagePacks; const pack = ImagePack.parsePack(packId, content); if (pack) { - roomPacks.push(pack); + imagePacks.push(pack); } - return roomPacks; + return imagePacks; }, []); } +export function getRoomImagePacks(room: Room): ImagePack[] { + const dataEvents = getStateEvents(room, StateEvent.PoniesRoomEmotes); + return packEventsToImagePacks(dataEvents); +} + export function getGlobalImagePacks(mx: MatrixClient): ImagePack[] { const emoteRoomsContent = getAccountData(mx, AccountDataEvent.PoniesEmoteRooms)?.getContent() as | EmoteRoomsContent @@ -255,7 +258,14 @@ export function getGlobalImagePacks(mx: MatrixClient): ImagePack[] { if (typeof rooms[roomId] !== 'object') return []; const room = mx.getRoom(roomId); if (!room) return []; - return getRoomImagePacks(room); + const packEventIdToUnknown = rooms[roomId]; + const roomPacks = getStateEvents(room, StateEvent.PoniesRoomEmotes); + const globalPacks = roomPacks.filter((mE) => { + const packKey = mE.getStateKey(); + if (typeof packKey === 'string') return !!packEventIdToUnknown[packKey]; + return false; + }); + return packEventsToImagePacks(globalPacks); }); return packs;