feat(onebot): support choosing animated system faces
Send available face animations by default and accept large=false for the small representation. Preserve the sticker business type when encoding animated faces. Fixes #483
This commit is contained in:
8 files changed
+64
-22
No files matched your search
@@ -155,7 +155,17 @@ export const ELEMENT_CODECS = {
|
||||
async fromSegment(data) {
|
||||
const id = intOr(data.id, -1);
|
||||
if (id < 0) return null;
|
||||
return { type: 'face', faceId: id };
|
||||
const element: MessageElementOf<'face'> = { type: 'face', faceId: id };
|
||||
if (data.large !== undefined) {
|
||||
if (data.large === true || data.large === 'true' || data.large === 1 || data.large === '1') {
|
||||
element.large = true;
|
||||
} else if (data.large === false || data.large === 'false' || data.large === 0 || data.large === '0') {
|
||||
element.large = false;
|
||||
} else {
|
||||
throw new MessageElementValidationError('INVALID_FIELD', 'face.large must be a boolean');
|
||||
}
|
||||
}
|
||||
return element;
|
||||
},
|
||||
},
|
||||
|
||||
|
||||
@@ -43,6 +43,18 @@ describe('parseMessage', () => {
|
||||
expect(result[0].faceId).toBe(123);
|
||||
});
|
||||
|
||||
it.each([true, false, 'true', 'false', 1, 0, '1', '0'])('preserves face animation choice %s', async (large) => {
|
||||
const result = await parseMessage([{ type: 'face', data: { id: '424', large } }], false);
|
||||
expect(result).toEqual([{ type: 'face', faceId: 424, large: [true, 'true', 1, '1'].includes(large) }]);
|
||||
});
|
||||
|
||||
it('accepts the CQ animation switch and rejects invalid booleans', async () => {
|
||||
expect(await parseMessage('[CQ:face,id=424,large=false]', false))
|
||||
.toEqual([{ type: 'face', faceId: 424, large: false }]);
|
||||
await expect(parseMessage('[CQ:face,id=424,large=maybe]', false))
|
||||
.rejects.toMatchObject({ code: 'INVALID_FIELD' });
|
||||
});
|
||||
|
||||
it('parses at CQ code', async () => {
|
||||
const result = await parseMessage('[CQ:at,qq=12345]', false);
|
||||
expect(result).toHaveLength(1);
|
||||
|
||||
@@ -7,7 +7,7 @@ import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
|
||||
import { randomUUID } from 'crypto';
|
||||
import { deflateSync } from 'zlib';
|
||||
import type { BridgeContext } from './bridge-context';
|
||||
import { sysFaceStore } from './sys-face-store';
|
||||
import { sysFaceStore, type FaceWire } from './sys-face-store';
|
||||
import type { MessageElement } from './events';
|
||||
import {
|
||||
assertVideoSendPolicy,
|
||||
@@ -53,15 +53,16 @@ function makeTextElem(text: string): ProtoElem {
|
||||
// renders classic small faces; newer "super" / animated faces sent that way are
|
||||
// silently remapped by the server (e.g. 424→168, issue #168). The split is
|
||||
// data-driven off the system-face catalog (0x9154_1, see sys-face-store):
|
||||
// super (animated, aniSticker not pack (1,1)) → CommonElem 37 + QFaceExtra
|
||||
// animated (unless large=false) → CommonElem 37 + QFaceExtra
|
||||
// other id ≥ 260 → CommonElem 33 + QSmallFaceExtra
|
||||
// classic id < 260 → legacy FaceElem
|
||||
async function makeFaceElem(faceId: number, ctx?: SendContext): Promise<ProtoElem> {
|
||||
async function makeFaceElem(faceId: number, large: boolean, ctx?: SendContext): Promise<ProtoElem> {
|
||||
// With a live bridge, wait for the authoritative catalog. Login normally
|
||||
// preloads it, while this await closes the reconnect / first-send race.
|
||||
const wire = ctx
|
||||
const resolved = ctx
|
||||
? await sysFaceStore.resolveWire(ctx.bridge, faceId)
|
||||
: sysFaceStore.classify(faceId);
|
||||
const wire: FaceWire = large ? resolved : { kind: faceId < 260 ? 'classic' : 'small' };
|
||||
if (wire.kind === 'super') {
|
||||
return {
|
||||
commonElem: {
|
||||
@@ -74,7 +75,7 @@ async function makeFaceElem(faceId: number, ctx?: SendContext): Promise<ProtoEle
|
||||
stickerType: wire.stickerType,
|
||||
randomType: 1,
|
||||
}),
|
||||
businessType: 1,
|
||||
businessType: wire.stickerType < 4 ? wire.stickerType : 1,
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -567,7 +568,7 @@ export async function buildSendElems(elements: MessageElement[], ctx?: SendConte
|
||||
break;
|
||||
|
||||
case 'face':
|
||||
result.push(await makeFaceElem(elem.faceId, ctx));
|
||||
result.push(await makeFaceElem(elem.faceId, elem.large ?? true, ctx));
|
||||
break;
|
||||
|
||||
case 'poke':
|
||||
|
||||
@@ -141,7 +141,7 @@ export const ELEMENT_MANIFEST = {
|
||||
},
|
||||
face: {
|
||||
directions: { D: 'yes', S: 'yes', P: 'yes', W: 'yes' },
|
||||
fields: fieldsFor<'face'>()(['faceId']),
|
||||
fields: fieldsFor<'face'>()(['faceId', 'large']),
|
||||
requiredFields: ['faceId'],
|
||||
},
|
||||
reply: {
|
||||
@@ -258,7 +258,7 @@ const NUMBER_FIELDS: ReadonlySet<string> = new Set([
|
||||
'width', 'height', 'emojiPackageId', 'sceneType', 'forwardTSum',
|
||||
'picFormat', 'videoFormat', 'voiceFormat',
|
||||
]);
|
||||
const BOOLEAN_FIELDS: ReadonlySet<string> = new Set(['flash', 'noByteFallback']);
|
||||
const BOOLEAN_FIELDS: ReadonlySet<string> = new Set(['flash', 'noByteFallback', 'large']);
|
||||
|
||||
function throwValidation(
|
||||
code: MessageElementValidationCode,
|
||||
|
||||
@@ -39,6 +39,8 @@ export interface AtElement {
|
||||
export interface FaceElement {
|
||||
type: 'face';
|
||||
faceId: number;
|
||||
/** Send the catalog animation when available (default true). */
|
||||
large?: boolean;
|
||||
}
|
||||
|
||||
export interface ReplyElement {
|
||||
|
||||
@@ -172,10 +172,9 @@ export function findFaceEntity(packs: SysFacePackEntry[], faceId: number): SysFa
|
||||
return null;
|
||||
}
|
||||
|
||||
/** True when the catalog metadata selects the existing super-face send path. */
|
||||
/** True when the catalog declares an animated sticker representation. */
|
||||
export function isSuperFaceEntry(emoji: SysFaceEntry): boolean {
|
||||
if (emoji.aniStickerType == null || emoji.aniStickerPackId == null) return false;
|
||||
return !(emoji.aniStickerType === 1 && emoji.aniStickerPackId === 1);
|
||||
return emoji.aniStickerType != null && emoji.aniStickerType > 0;
|
||||
}
|
||||
|
||||
export function isSuperFaceId(packs: SysFacePackEntry[], faceId: number): boolean {
|
||||
|
||||
@@ -59,7 +59,7 @@ describe('element-manifest 对账(protocol 侧:D 收·解 / W 发·打包)
|
||||
if (field === 'mediaNode') return {};
|
||||
if (field === 'emojiId' || field === 'md5Hex') return 'ab'.repeat(16);
|
||||
if (field === 'sha1Hex') return 'cd'.repeat(20);
|
||||
if (field === 'flash' || field === 'noByteFallback') return true;
|
||||
if (field === 'flash' || field === 'noByteFallback' || field === 'large') return true;
|
||||
if (field === 'botAppid') return '1';
|
||||
if (field === 'targetUin' || field === 'faceId' || field === 'fileSize'
|
||||
|| field.startsWith('reply') || field === 'subType' || field === 'duration'
|
||||
|
||||
@@ -38,12 +38,12 @@ function entry(
|
||||
};
|
||||
}
|
||||
|
||||
// 392 is a real non-(1,1) super face; 424 is a real (1,1) "new small" face.
|
||||
// Animated faces include ordinary type-1 stickers as well as special types.
|
||||
const CATALOG: SysFacePackEntry[] = [
|
||||
{ packName: '经典', emojis: [entry('14', null, null, null, '/微笑', ['smile'])] },
|
||||
{ packName: '超级表情', emojis: [
|
||||
entry('392', 3, 2, 38, '/龙年快乐'), // super → CommonElem 37
|
||||
entry('424', 1, 1, 52), // (1,1) → small → CommonElem 33
|
||||
entry('424', 1, 1, 52),
|
||||
entry('358', 2, 1, 33), // super (packId 1 but type 2) → CommonElem 37
|
||||
] },
|
||||
];
|
||||
@@ -62,18 +62,19 @@ class MemoryCatalogStorage implements SysFaceCatalogStorage {
|
||||
}
|
||||
}
|
||||
|
||||
describe('isSuperFaceEntry / isSuperFaceId — the (1,1) rule', () => {
|
||||
it('treats only non-(1,1) aniSticker faces as super', () => {
|
||||
describe('isSuperFaceEntry / isSuperFaceId', () => {
|
||||
it('recognizes every animated sticker type', () => {
|
||||
expect(isSuperFaceEntry(entry('x', 3, 2, 1))).toBe(true);
|
||||
expect(isSuperFaceEntry(entry('x', 2, 1, 1))).toBe(true); // type≠1
|
||||
expect(isSuperFaceEntry(entry('x', 1, 2, 1))).toBe(true); // pack≠1
|
||||
expect(isSuperFaceEntry(entry('x', 1, 1, 52))).toBe(false); // the (1,1) pack
|
||||
expect(isSuperFaceEntry(entry('x', 1, 1, 52))).toBe(true);
|
||||
expect(isSuperFaceEntry(entry('x', 0, 0, 0))).toBe(false);
|
||||
expect(isSuperFaceEntry(entry('x', null, null, null))).toBe(false); // not aniSticker
|
||||
});
|
||||
|
||||
it('isSuperFaceId walks the packs by id', () => {
|
||||
expect(isSuperFaceId(CATALOG, 392)).toBe(true);
|
||||
expect(isSuperFaceId(CATALOG, 424)).toBe(false);
|
||||
expect(isSuperFaceId(CATALOG, 424)).toBe(true);
|
||||
expect(isSuperFaceId(CATALOG, 14)).toBe(false);
|
||||
expect(isSuperFaceId(CATALOG, 99999)).toBe(false); // unknown
|
||||
});
|
||||
@@ -94,8 +95,10 @@ describe('faceWireFor — classification', () => {
|
||||
expect(() => faceWireFor(entry('392', 3, 2, null), 392))
|
||||
.toThrow(/incomplete super-face metadata/);
|
||||
});
|
||||
it('(1,1) face → small/classic by id range, not super', () => {
|
||||
expect(faceWireFor(entry('424', 1, 1, 52), 424)).toEqual({ kind: 'small' });
|
||||
it('uses the animation metadata for type-1 stickers too', () => {
|
||||
expect(faceWireFor(entry('424', 1, 1, 52), 424)).toEqual({
|
||||
kind: 'super', packId: '1', stickerId: '52', stickerType: 1,
|
||||
});
|
||||
});
|
||||
it('rejects an unknown id instead of guessing a wire shape', () => {
|
||||
expect(() => faceWireFor(null, 14)).toThrow(/absent from the current catalog/);
|
||||
@@ -499,8 +502,13 @@ describe('makeFaceElem (via buildSendElems) — three-way wire encoding', () =>
|
||||
expect(big.qsid).toBe(392);
|
||||
expect(big.packId).toBe('2');
|
||||
expect(big.stickerId).toBe('38');
|
||||
expect(superElem.commonElem?.businessType).toBe(3);
|
||||
|
||||
const [smallElem] = await buildSendElems([{ type: 'face', faceId: 424 }], ctx);
|
||||
const [animated] = await buildSendElems([{ type: 'face', faceId: 424 }], ctx);
|
||||
expect(animated.commonElem?.serviceType).toBe(37);
|
||||
expect(protobuf_decode<QFaceExtra>(animated.commonElem!.pbElem!).stickerId).toBe('52');
|
||||
|
||||
const [smallElem] = await buildSendElems([{ type: 'face', faceId: 424, large: false }], ctx);
|
||||
expect(smallElem.commonElem?.serviceType).toBe(33);
|
||||
const small = protobuf_decode<QSmallFaceExtra>(smallElem.commonElem!.pbElem!);
|
||||
expect(small.faceId).toBe(424);
|
||||
@@ -509,6 +517,16 @@ describe('makeFaceElem (via buildSendElems) — three-way wire encoding', () =>
|
||||
expect(classicElem.face?.index).toBe(14);
|
||||
});
|
||||
|
||||
it('allows special animated faces to be sent small and normalizes newer business types', async () => {
|
||||
sysFaceStore.load([{ packName: 'special', emojis: [entry('504', 4, 1, 90)] }]);
|
||||
const [large] = await buildSendElems([{ type: 'face', faceId: 504, large: true }]);
|
||||
expect(large.commonElem?.businessType).toBe(1);
|
||||
expect(protobuf_decode<QFaceExtra>(large.commonElem!.pbElem!).stickerType).toBe(4);
|
||||
const [small] = await buildSendElems([{ type: 'face', faceId: 504, large: false }]);
|
||||
expect(small.commonElem?.serviceType).toBe(33);
|
||||
expect(protobuf_decode<QSmallFaceExtra>(small.commonElem!.pbElem!).faceId).toBe(504);
|
||||
});
|
||||
|
||||
it('rejects an unknown id even when building without a live send context', async () => {
|
||||
sysFaceStore.load(CATALOG);
|
||||
|
||||
|
||||
Reference in new issue
Block a user