From c404974e75c22a84e292326ebc3e0e39b7d79d83 Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Sat, 8 Aug 2026 19:26:09 +0800 Subject: [PATCH] fix(protocol): support cross-pack system face overlaps (#324) --- packages/protocol/src/sys-face-store.ts | 107 +++++++++++++++++----- packages/protocol/tests/sys-faces.test.ts | 65 ++++++++++++- 2 files changed, 149 insertions(+), 23 deletions(-) diff --git a/packages/protocol/src/sys-face-store.ts b/packages/protocol/src/sys-face-store.ts index a0418fed..e6934759 100644 --- a/packages/protocol/src/sys-face-store.ts +++ b/packages/protocol/src/sys-face-store.ts @@ -42,7 +42,8 @@ export interface SysFaceStoreOptions { interface IndexedCatalog { packs: SysFacePackEntry[]; byId: Map; - packNameById: Map; + entryCount: number; + overlapCount: number; } /** Pure classification once an authoritative entry is known. */ @@ -108,16 +109,25 @@ export class SysFaceStore { if (!needle || !this.catalog) return []; const matches: SysFaceEntry[] = []; - for (const [id, face] of this.catalog.byId) { - const haystacks = [ - id, - face.qDes, - face.emCode, - this.catalog.packNameById.get(id) ?? '', - ...face.emojiNameAlias, - ]; - if (haystacks.some((value) => value.toLocaleLowerCase().includes(needle))) { - matches.push(face); + const matchedIds = new Set(); + for (const pack of this.catalog.packs) { + for (const face of pack.emojis) { + if (matchedIds.has(face.qSid)) continue; + const haystacks = [ + face.qSid, + face.qDes, + face.emCode, + pack.packName, + ...face.emojiNameAlias, + ]; + if (haystacks.some((value) => value.toLocaleLowerCase().includes(needle))) { + const canonical = this.catalog.byId.get(face.qSid); + if (!canonical) { + throw new Error(`system face index is missing catalog id ${face.qSid}`); + } + matches.push(canonical); + matchedIds.add(face.qSid); + } } } return matches; @@ -179,8 +189,10 @@ export class SysFaceStore { this.refreshedInProcess = true; if (this.storage) await this.storage.save(snapshot); log.info( - 'system face catalog refreshed: faces=%d packs=%d elapsedMs=%d', + 'system face catalog refreshed: entries=%d uniqueFaces=%d overlaps=%d packs=%d elapsedMs=%d', + this.catalog.entryCount, this.catalog.byId.size, + this.catalog.overlapCount, validated.length, this.now() - startedAt, ); @@ -246,8 +258,11 @@ export class SysFaceStore { validateSnapshot(snapshot); this.catalog = indexCatalog(snapshot.packs); log.info( - 'system face catalog restored: faces=%d packs=%d fetchedAt=%d elapsedMs=%d', + 'system face catalog restored: entries=%d uniqueFaces=%d overlaps=%d packs=%d ' + + 'fetchedAt=%d elapsedMs=%d', + this.catalog.entryCount, this.catalog.byId.size, + this.catalog.overlapCount, snapshot.packs.length, snapshot.fetchedAt, this.now() - startedAt, @@ -288,7 +303,12 @@ function validateCatalog(packs: SysFacePackEntry[]): SysFacePackEntry[] { throw new Error('system face catalog is empty'); } - const seen = new Set(); + const seen = new Map; + }>(); let faceCount = 0; for (const [packIndex, pack] of packs.entries()) { if (!pack || typeof pack.packName !== 'string' || !Array.isArray(pack.emojis)) { @@ -301,9 +321,6 @@ function validateCatalog(packs: SysFacePackEntry[]): SysFacePackEntry[] { + `value=${JSON.stringify(face?.qSid)}`, ); } - if (seen.has(face.qSid)) { - throw new Error(`duplicate system face id ${face.qSid}`); - } if (!Array.isArray(face.emojiNameAlias) || face.emojiNameAlias.some((alias) => typeof alias !== 'string')) { throw new Error(`invalid aliases for system face id ${face.qSid}`); @@ -326,7 +343,31 @@ function validateCatalog(packs: SysFacePackEntry[]): SysFacePackEntry[] { if (face.url !== null && typeof face.url !== 'string') { throw new Error(`invalid url for system face id ${face.qSid}`); } - seen.add(face.qSid); + const previous = seen.get(face.qSid); + if (previous) { + const previousFaceIndex = previous.faceIndexByPack.get(packIndex); + if (previousFaceIndex !== undefined) { + throw new Error( + `duplicate system face id ${face.qSid} within pack ${packIndex}: ` + + `faces ${previousFaceIndex} and ${faceIndex}`, + ); + } + if (!hasCompatibleSendMetadata(previous.face, face)) { + throw new Error( + `conflicting system face send metadata for id ${face.qSid}: ` + + `pack ${previous.packIndex}, face ${previous.faceIndex} vs ` + + `pack ${packIndex}, face ${faceIndex}`, + ); + } + previous.faceIndexByPack.set(packIndex, faceIndex); + } else { + seen.set(face.qSid, { + face, + packIndex, + faceIndex, + faceIndexByPack: new Map([[packIndex, faceIndex]]), + }); + } faceCount += 1; } } @@ -334,16 +375,38 @@ function validateCatalog(packs: SysFacePackEntry[]): SysFacePackEntry[] { return packs; } +function hasCompatibleSendMetadata(left: SysFaceEntry, right: SysFaceEntry): boolean { + if (!isAddressableFaceId(left.qSid)) return true; + const leftIsSuper = isSuperFaceEntry(left); + const rightIsSuper = isSuperFaceEntry(right); + if (leftIsSuper !== rightIsSuper) return false; + if (!leftIsSuper) return true; + return left.aniStickerType === right.aniStickerType + && left.aniStickerPackId === right.aniStickerPackId + && left.aniStickerId === right.aniStickerId; +} + +function isAddressableFaceId(id: string): boolean { + if (!/^(?:0|[1-9]\d*)$/.test(id)) return false; + const numeric = Number(id); + return Number.isSafeInteger(numeric) && numeric >= 0; +} + function indexCatalog(packs: SysFacePackEntry[]): IndexedCatalog { const byId = new Map(); - const packNameById = new Map(); + let entryCount = 0; for (const pack of packs) { for (const emoji of pack.emojis) { - byId.set(emoji.qSid, emoji); - packNameById.set(emoji.qSid, pack.packName); + entryCount += 1; + if (!byId.has(emoji.qSid)) byId.set(emoji.qSid, emoji); } } - return { packs, byId, packNameById }; + return { + packs, + byId, + entryCount, + overlapCount: entryCount - byId.size, + }; } /** Shared, process-wide catalog (the face set is account-independent). */ diff --git a/packages/protocol/tests/sys-faces.test.ts b/packages/protocol/tests/sys-faces.test.ts index 7d3da09a..d948373d 100644 --- a/packages/protocol/tests/sys-faces.test.ts +++ b/packages/protocol/tests/sys-faces.test.ts @@ -232,7 +232,44 @@ describe('SysFaceStore — persistent catalog and query seam', () => { expect(store.lookup(128522)).toBeNull(); }); - it('still rejects empty and duplicate catalog identifiers', () => { + it('preserves one face across multiple catalog packs', async () => { + const commonFace = entry('474', 1, 4, 76, '/给你一拳'); + commonFace.emCode = '10474'; + const specialFace: SysFaceEntry = { + ...commonFace, + qDes: '/给你一拳(黄脸)', + emojiNameAlias: ['punch'], + }; + const catalog: SysFacePackEntry[] = [ + { packName: '超级表情', emojis: [commonFace] }, + { packName: 'QQ黄脸', emojis: [specialFace] }, + ]; + const storage = new MemoryCatalogStorage(); + const store = new SysFaceStore({ + storage, + now: () => 456, + fetchCatalog: async () => catalog, + }); + + await expect(store.refresh(sender)).resolves.toEqual(catalog); + + expect(storage.saved[0]?.packs).toEqual(catalog); + await expect(store.getCatalog(sender)).resolves.toEqual(catalog); + expect(store.lookup(474)).toEqual(commonFace); + expect(store.search('超级表情').map((face) => face.qSid)).toEqual(['474']); + expect(store.search('QQ黄脸').map((face) => face.qSid)).toEqual(['474']); + expect(store.search('punch').map((face) => face.qSid)).toEqual(['474']); + + const restored = new SysFaceStore({ + storage: new MemoryCatalogStorage(storage.saved[0]!), + fetchCatalog: async () => { throw new Error('network must not be used'); }, + }); + await restored.ensureReady(sender); + await expect(restored.getCatalog(sender)).resolves.toEqual(catalog); + expect(restored.search('QQ黄脸').map((face) => face.qSid)).toEqual(['474']); + }); + + it('still rejects empty identifiers and duplicates within one pack', () => { const store = new SysFaceStore(); expect(() => store.load([{ packName: 'emoji', emojis: [entry('', null, null, null)] }])) @@ -241,6 +278,32 @@ describe('SysFaceStore — persistent catalog and query seam', () => { packName: 'emoji', emojis: [entry('😊', null, null, null), entry('😊', null, null, null)], }])).toThrow(/duplicate system face id/); + expect(() => store.load([ + { packName: '超级表情', emojis: [entry('474', 1, 4, 76)] }, + { + packName: 'QQ黄脸', + emojis: [entry('474', 1, 4, 76), entry('474', 1, 4, 76)], + }, + ])).toThrow(/duplicate system face id 474 within pack 1/); + }); + + it('rejects cross-pack copies with conflicting send metadata', () => { + const store = new SysFaceStore(); + + expect(() => store.load([ + { packName: '超级表情', emojis: [entry('474', 1, 4, 76)] }, + { packName: 'QQ黄脸', emojis: [entry('474', 3, 2, 38)] }, + ])).toThrow(/conflicting system face send metadata.*pack 0.*pack 1/); + }); + + it('allows non-addressable Unicode entries to carry panel-specific metadata', () => { + const store = new SysFaceStore(); + + expect(() => store.load([ + { packName: 'emoji', emojis: [entry('😊', null, null, null, '/微笑')] }, + { packName: '最近使用', emojis: [entry('😊', 3, 2, 38, '/最近使用')] }, + ])).not.toThrow(); + expect(store.search('最近使用').map((face) => face.qSid)).toEqual(['😊']); }); it('coalesces concurrent refreshes and persists the authoritative result once', async () => {