fix(protocol): accept Unicode system face identifiers (#324)

This commit is contained in:
DrSmoothl committed 2026-08-08 17:42:14 +08:00
1 parent 65683bb44d
commit 39d5a50d5f
6 files changed
+122 -26

No files matched your search

@@ -1,10 +1,8 @@
// 0x9154_1 — fetch the bot's system face / emoji catalog.
//
// Mirrors Lagrange.Core's `FetchFullSysFacesService`. One server round-trip
// returns the entire pack list — common faces, "big"/super faces (the giant
// animated emoji), and the magic-face pack. The send path uses each emoji's
// `aniSticker*` metadata to pick the right wire encoding for a face id (see
// element-builder's makeFaceElem + sys-face-store).
// QQ returns the common, special-big, and magic-face panels in one response.
// The send path uses the catalog metadata to select the corresponding face
// element encoding (see element-builder and sys-face-store).
import { createLogger } from '@snowluma/common/logger';
import {
@@ -22,8 +20,9 @@ import { invokeOidb, type OidbSender } from '../../oidb-service';
const log = createLogger('SysFace');
/** A single system face / emoji. Field names match Lagrange's `SysFaceEntry`;
* `qSid` is the numeric face ID as a string. */
/** A single system face / emoji. `qSid` is QQ's opaque catalog identifier:
* numeric system faces use decimal strings, while emoji groups may use a
* Unicode emoji string. */
export interface SysFaceEntry {
qSid: string;
qDes: string;
@@ -118,7 +117,7 @@ export namespace FetchSysFaces {
export const serialize = (_ctx: Deps, _p: Params): OidbFetchSysFacesReq => ({
field1: 0,
field2: 7,
field3: '0',
field3: 0,
});
export const deserialize = (_ctx: Deps, body: OidbFetchSysFacesResp): SysFacePackEntry[] => {
@@ -138,8 +137,8 @@ export namespace FetchSysFaces {
}
}
// Magic faces arrive as a single un-named bundle — Lagrange tags it
// "MagicFace" client-side, so we match for parity.
// QQ returns magic faces as a single unnamed bundle. Keep a stable local
// name so callers can identify the group across refreshes.
const magicEmojis = body.specialMagicFace?.field1?.emojiList ?? [];
if (magicEmojis.length > 0) {
const packIndex = packs.length;
@@ -173,10 +172,7 @@ export function findFaceEntity(packs: SysFacePackEntry[], faceId: number): SysFa
return null;
}
/** True when `faceId` is an animated "super face" — has both aniStickerType
* and aniStickerPackId set, and is NOT the (1,1) pack (the regular static
* emojis). Matches Lagrange's `GetUniqueSuperQSids` filter. Super faces ride
* CommonElem serviceType 37 (QFaceExtra); the (1,1) ones ride serviceType 33. */
/** True when the catalog metadata selects the existing super-face send path. */
export function isSuperFaceEntry(emoji: SysFaceEntry): boolean {
if (emoji.aniStickerType == null || emoji.aniStickerPackId == null) return false;
return !(emoji.aniStickerType === 1 && emoji.aniStickerPackId === 1);
+1 -2
View File
@@ -295,8 +295,7 @@ function validateCatalog(packs: SysFacePackEntry[]): SysFacePackEntry[] {
throw new Error(`invalid system face pack at index ${packIndex}`);
}
for (const [faceIndex, face] of pack.emojis.entries()) {
if (!face || typeof face.qSid !== 'string' || !/^(?:0|[1-9]\d*)$/.test(face.qSid)
|| !Number.isSafeInteger(Number(face.qSid)) || Number(face.qSid) < 0) {
if (!face || typeof face.qSid !== 'string' || face.qSid.length === 0) {
throw new Error(
`invalid system face id at pack ${packIndex}, face ${faceIndex}: `
+ `value=${JSON.stringify(face?.qSid)}`,
+77 -1
View File
@@ -2,7 +2,10 @@ import { describe, it, expect } from 'vitest';
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
import type { QFaceExtra, QSmallFaceExtra } from '@snowluma/proto-defs/element';
import type { OidbBase } from '@snowluma/proto-defs/oidb';
import type { OidbFetchSysFacesResp } from '@snowluma/proto-defs/oidb-actions/sys-faces';
import type {
OidbFetchSysFacesReq,
OidbFetchSysFacesResp,
} from '@snowluma/proto-defs/oidb-actions/sys-faces';
import {
FetchSysFaces,
findFaceEntity,
@@ -100,6 +103,27 @@ describe('faceWireFor — classification', () => {
});
});
describe('FetchSysFaces protocol contract', () => {
it('encodes the native 0x9154_1 request layout', () => {
const body = FetchSysFaces.serialize({} as never, {});
expect(body).toEqual({
field1: 0,
field2: 7,
field3: 0,
});
const decoded = protobuf_decode<OidbBase<OidbFetchSysFacesReq>>(
FetchSysFaces.encode({ body }),
);
expect(decoded.body).toEqual({
field1: null,
field2: 7,
field3: null,
field4: null,
});
});
});
describe('FetchSysFaces.deserialize', () => {
it('flattens common + big + magic packs', () => {
const packs = FetchSysFaces.deserialize({} as never, {
@@ -111,6 +135,27 @@ describe('FetchSysFaces.deserialize', () => {
expect(packs[1].emojis[0].aniStickerType).toBe(3);
});
it('preserves Unicode catalog identifiers returned by QQ', () => {
const packs = FetchSysFaces.deserialize({} as never, {
commonFace: {
emojiList: [{
emojiPackName: 'emoji',
emojiDetail: [{ qSid: '😊', qDes: '/嘿嘿', qCid: 128522, emCode: '400832' }],
}],
},
} as never);
expect(packs).toEqual([{
packName: 'emoji',
emojis: [expect.objectContaining({
qSid: '😊',
qDes: '/嘿嘿',
qCid: 128522,
emCode: '400832',
})],
}]);
});
it('fails closed when an id field uses an unsupported wire encoding', () => {
const driftedFace = { qDes: '/未知表情' };
Object.defineProperty(driftedFace, Symbol.for('snowluma.proton.unknownFields'), {
@@ -167,6 +212,37 @@ describe('SysFaceStore — persistent catalog and query seam', () => {
expect(store.search('经典').map((face) => face.qSid)).toEqual(['14']);
});
it('persists and searches Unicode catalog ids without exposing them as numeric face ids', async () => {
const unicodeFace = entry('😊', null, null, null, '/嘿嘿');
unicodeFace.qCid = 128522;
unicodeFace.emCode = '400832';
const catalog: SysFacePackEntry[] = [{ packName: 'emoji', emojis: [unicodeFace] }];
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);
expect(store.search('😊').map((face) => face.qSid)).toEqual(['😊']);
expect(store.search('嘿嘿').map((face) => face.qSid)).toEqual(['😊']);
expect(store.lookup(128522)).toBeNull();
});
it('still rejects empty and duplicate catalog identifiers', () => {
const store = new SysFaceStore();
expect(() => store.load([{ packName: 'emoji', emojis: [entry('', null, null, null)] }]))
.toThrow(/invalid system face id/);
expect(() => store.load([{
packName: 'emoji',
emojis: [entry('😊', null, null, null), entry('😊', null, null, null)],
}])).toThrow(/duplicate system face id/);
});
it('coalesces concurrent refreshes and persists the authoritative result once', async () => {
const storage = new MemoryCatalogStorage();
let fetches = 0;