fix(protocol): support cross-pack system face overlaps (#324)
This commit is contained in:
2 files changed
+143
-17
No files matched your search
@@ -42,7 +42,8 @@ export interface SysFaceStoreOptions {
|
|||||||
interface IndexedCatalog {
|
interface IndexedCatalog {
|
||||||
packs: SysFacePackEntry[];
|
packs: SysFacePackEntry[];
|
||||||
byId: Map<string, SysFaceEntry>;
|
byId: Map<string, SysFaceEntry>;
|
||||||
packNameById: Map<string, string>;
|
entryCount: number;
|
||||||
|
overlapCount: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Pure classification once an authoritative entry is known. */
|
/** Pure classification once an authoritative entry is known. */
|
||||||
@@ -108,16 +109,25 @@ export class SysFaceStore {
|
|||||||
if (!needle || !this.catalog) return [];
|
if (!needle || !this.catalog) return [];
|
||||||
|
|
||||||
const matches: SysFaceEntry[] = [];
|
const matches: SysFaceEntry[] = [];
|
||||||
for (const [id, face] of this.catalog.byId) {
|
const matchedIds = new Set<string>();
|
||||||
|
for (const pack of this.catalog.packs) {
|
||||||
|
for (const face of pack.emojis) {
|
||||||
|
if (matchedIds.has(face.qSid)) continue;
|
||||||
const haystacks = [
|
const haystacks = [
|
||||||
id,
|
face.qSid,
|
||||||
face.qDes,
|
face.qDes,
|
||||||
face.emCode,
|
face.emCode,
|
||||||
this.catalog.packNameById.get(id) ?? '',
|
pack.packName,
|
||||||
...face.emojiNameAlias,
|
...face.emojiNameAlias,
|
||||||
];
|
];
|
||||||
if (haystacks.some((value) => value.toLocaleLowerCase().includes(needle))) {
|
if (haystacks.some((value) => value.toLocaleLowerCase().includes(needle))) {
|
||||||
matches.push(face);
|
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;
|
return matches;
|
||||||
@@ -179,8 +189,10 @@ export class SysFaceStore {
|
|||||||
this.refreshedInProcess = true;
|
this.refreshedInProcess = true;
|
||||||
if (this.storage) await this.storage.save(snapshot);
|
if (this.storage) await this.storage.save(snapshot);
|
||||||
log.info(
|
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.byId.size,
|
||||||
|
this.catalog.overlapCount,
|
||||||
validated.length,
|
validated.length,
|
||||||
this.now() - startedAt,
|
this.now() - startedAt,
|
||||||
);
|
);
|
||||||
@@ -246,8 +258,11 @@ export class SysFaceStore {
|
|||||||
validateSnapshot(snapshot);
|
validateSnapshot(snapshot);
|
||||||
this.catalog = indexCatalog(snapshot.packs);
|
this.catalog = indexCatalog(snapshot.packs);
|
||||||
log.info(
|
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.byId.size,
|
||||||
|
this.catalog.overlapCount,
|
||||||
snapshot.packs.length,
|
snapshot.packs.length,
|
||||||
snapshot.fetchedAt,
|
snapshot.fetchedAt,
|
||||||
this.now() - startedAt,
|
this.now() - startedAt,
|
||||||
@@ -288,7 +303,12 @@ function validateCatalog(packs: SysFacePackEntry[]): SysFacePackEntry[] {
|
|||||||
throw new Error('system face catalog is empty');
|
throw new Error('system face catalog is empty');
|
||||||
}
|
}
|
||||||
|
|
||||||
const seen = new Set<string>();
|
const seen = new Map<string, {
|
||||||
|
face: SysFaceEntry;
|
||||||
|
packIndex: number;
|
||||||
|
faceIndex: number;
|
||||||
|
faceIndexByPack: Map<number, number>;
|
||||||
|
}>();
|
||||||
let faceCount = 0;
|
let faceCount = 0;
|
||||||
for (const [packIndex, pack] of packs.entries()) {
|
for (const [packIndex, pack] of packs.entries()) {
|
||||||
if (!pack || typeof pack.packName !== 'string' || !Array.isArray(pack.emojis)) {
|
if (!pack || typeof pack.packName !== 'string' || !Array.isArray(pack.emojis)) {
|
||||||
@@ -301,9 +321,6 @@ function validateCatalog(packs: SysFacePackEntry[]): SysFacePackEntry[] {
|
|||||||
+ `value=${JSON.stringify(face?.qSid)}`,
|
+ `value=${JSON.stringify(face?.qSid)}`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if (seen.has(face.qSid)) {
|
|
||||||
throw new Error(`duplicate system face id ${face.qSid}`);
|
|
||||||
}
|
|
||||||
if (!Array.isArray(face.emojiNameAlias)
|
if (!Array.isArray(face.emojiNameAlias)
|
||||||
|| face.emojiNameAlias.some((alias) => typeof alias !== 'string')) {
|
|| face.emojiNameAlias.some((alias) => typeof alias !== 'string')) {
|
||||||
throw new Error(`invalid aliases for system face id ${face.qSid}`);
|
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') {
|
if (face.url !== null && typeof face.url !== 'string') {
|
||||||
throw new Error(`invalid url for system face id ${face.qSid}`);
|
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;
|
faceCount += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -334,16 +375,38 @@ function validateCatalog(packs: SysFacePackEntry[]): SysFacePackEntry[] {
|
|||||||
return packs;
|
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 {
|
function indexCatalog(packs: SysFacePackEntry[]): IndexedCatalog {
|
||||||
const byId = new Map<string, SysFaceEntry>();
|
const byId = new Map<string, SysFaceEntry>();
|
||||||
const packNameById = new Map<string, string>();
|
let entryCount = 0;
|
||||||
for (const pack of packs) {
|
for (const pack of packs) {
|
||||||
for (const emoji of pack.emojis) {
|
for (const emoji of pack.emojis) {
|
||||||
byId.set(emoji.qSid, emoji);
|
entryCount += 1;
|
||||||
packNameById.set(emoji.qSid, pack.packName);
|
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). */
|
/** Shared, process-wide catalog (the face set is account-independent). */
|
||||||
|
|||||||
@@ -232,7 +232,44 @@ describe('SysFaceStore — persistent catalog and query seam', () => {
|
|||||||
expect(store.lookup(128522)).toBeNull();
|
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();
|
const store = new SysFaceStore();
|
||||||
|
|
||||||
expect(() => store.load([{ packName: 'emoji', emojis: [entry('', null, null, null)] }]))
|
expect(() => store.load([{ packName: 'emoji', emojis: [entry('', null, null, null)] }]))
|
||||||
@@ -241,6 +278,32 @@ describe('SysFaceStore — persistent catalog and query seam', () => {
|
|||||||
packName: 'emoji',
|
packName: 'emoji',
|
||||||
emojis: [entry('😊', null, null, null), entry('😊', null, null, null)],
|
emojis: [entry('😊', null, null, null), entry('😊', null, null, null)],
|
||||||
}])).toThrow(/duplicate system face id/);
|
}])).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 () => {
|
it('coalesces concurrent refreshes and persists the authoritative result once', async () => {
|
||||||
|
|||||||
Reference in new issue
Block a user