feat(messages): support animated face results

Allow selecting animated face results and preserve received results in messages and history. Keep the default selection when no result is supplied.

Fixes #487
This commit is contained in:
DrSmoothl committed 2026-09-27 13:44:48 +08:00
1 parent 8cc5326a3a
commit 93b5c1b4bf
13 files changed
+185 -15

No files matched your search

+11 -2
View File
@@ -58,7 +58,7 @@ function makeTextElem(text: string): ProtoElem {
// animated (unless large=false) → CommonElem 37 + QFaceExtra
// other id ≥ 260 → CommonElem 33 + QSmallFaceExtra
// classic id < 260 → legacy FaceElem
async function makeFaceElem(faceId: number, large: boolean, ctx?: SendContext): Promise<ProtoElem> {
async function makeFaceElem(faceId: number, large: boolean, resultId: string | undefined, 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 resolved = ctx
@@ -75,12 +75,21 @@ async function makeFaceElem(faceId: number, large: boolean, ctx?: SendContext):
qsid: faceId,
sourceType: 1,
stickerType: wire.stickerType,
resultId,
randomType: 1,
}),
businessType: wire.stickerType < 4 ? wire.stickerType : 1,
},
};
}
if (resultId !== undefined) {
throw new MessageElementValidationError(
'INVALID_FIELD',
`face.resultId requires animation metadata for face ${faceId}`,
'face',
'resultId',
);
}
if (wire.kind === 'small') {
return {
commonElem: {
@@ -578,7 +587,7 @@ export async function buildSendElems(elements: MessageElement[], ctx?: SendConte
break;
case 'face':
result.push(await makeFaceElem(elem.faceId, elem.large ?? true, ctx));
result.push(await makeFaceElem(elem.faceId, elem.large ?? true, elem.resultId, ctx));
break;
case 'poke':
+10 -2
View File
@@ -141,7 +141,7 @@ export const ELEMENT_MANIFEST = {
},
face: {
directions: { D: 'yes', S: 'yes', P: 'yes', W: 'yes' },
fields: fieldsFor<'face'>()(['faceId', 'large']),
fields: fieldsFor<'face'>()(['faceId', 'large', 'resultId']),
requiredFields: ['faceId'],
},
reply: {
@@ -250,7 +250,7 @@ const STRING_FIELDS: ReadonlySet<string> = new Set([
'text', 'uid', 'imageUrl', 'fileId', 'fileName', 'fileHash', 'url',
'thumbUrl', 'summary', 'emojiId', 'emojiKey', 'resId', 'filesetId',
'forwardSource', 'forwardSummary', 'forwardPrompt', 'forwardUuid',
'md5Hex', 'sha1Hex', 'botAppid',
'md5Hex', 'sha1Hex', 'botAppid', 'resultId',
]);
const NUMBER_FIELDS: ReadonlySet<string> = new Set([
'faceId', 'targetUin', 'fileSize', 'replySeq', 'replyMessageId',
@@ -452,6 +452,14 @@ function validateSemantics(
if (!Number.isInteger(element.faceId) || element.faceId < 0) {
throwValidation('INVALID_FIELD', 'message element "face" field "faceId" must be a non-negative integer', element.type, 'faceId');
}
if (element.resultId !== undefined && (direction === 'P' || direction === 'W')) {
if (!element.resultId.trim()) {
throwValidation('INVALID_FIELD', 'face.resultId must not be empty', element.type, 'resultId');
}
if (element.large === false) {
throwValidation('INVALID_FIELD', 'face.resultId requires an animated face; large cannot be false', element.type, 'resultId');
}
}
return;
case 'reply':
if (!Number.isInteger(element.replySeq) || element.replySeq <= 0) {
+2
View File
@@ -41,6 +41,8 @@ export interface FaceElement {
faceId: number;
/** Send the catalog animation when available (default true). */
large?: boolean;
/** Selected animation result; omitted to keep the default animation choice. */
resultId?: string;
}
export interface ReplyElement {
@@ -921,7 +921,11 @@ function convertElements(elems: ElemDecoded[], isGroup: boolean): MessageElement
// this protobuf twice.
const extra = decodedBigFaces.get(elem);
if (isValidBigFace(extra)) {
result.push({ type: 'face', faceId: extra.qsid });
result.push({
type: 'face',
faceId: extra.qsid,
...(extra.resultId != null ? { resultId: extra.resultId } : {}),
});
}
} else if (svcType === 45 && ce.pbElem && ce.pbElem.length > 0) {
const markdown = decodedMarkdown.get(elem);
+27
View File
@@ -466,6 +466,33 @@ describe('SysFaceStore — persistent catalog and query seam', () => {
describe('makeFaceElem (via buildSendElems) — three-way wire encoding', () => {
const ctx = { bridge: {} as never };
it.each(['0', '2', 'variant'])('preserves an explicit animation result %s', async (resultId) => {
sysFaceStore.load(CATALOG);
const [element] = await buildSendElems([{ type: 'face', faceId: 392, resultId }], ctx);
expect(protobuf_decode<QFaceExtra>(element.commonElem!.pbElem!)).toMatchObject({
qsid: 392, resultId, randomType: 1,
});
});
it('keeps the default animation choice when no result is supplied', async () => {
sysFaceStore.load(CATALOG);
const [element] = await buildSendElems([{ type: 'face', faceId: 392 }], ctx);
const payload = protobuf_decode<QFaceExtra>(element.commonElem!.pbElem!);
expect(payload.resultId).toBeNull();
expect(payload.randomType).toBe(1);
});
it.each([14, 504])('rejects an animation result without animation metadata for %i', async (faceId) => {
sysFaceStore.load(CATALOG);
await expect(buildSendElems([{ type: 'face', faceId, resultId: '0' }]))
.rejects.toMatchObject({ code: 'INVALID_FIELD', elementType: 'face', field: 'resultId' });
});
it('rejects incompatible animation options before loading the catalog', async () => {
await expect(buildSendElems([{ type: 'face', faceId: 392, large: false, resultId: '0' }], ctx))
.rejects.toMatchObject({ code: 'INVALID_FIELD', field: 'resultId' });
});
it('awaits an authoritative refresh when the first send misses the persisted catalog', async () => {
sysFaceStore.load(CATALOG.slice(0, 1));
let requests = 0;