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:
13 files changed
+185
-15
No files matched your search
@@ -150,12 +150,27 @@ export const ELEMENT_CODECS = {
|
||||
|
||||
face: {
|
||||
async toSegment(element) {
|
||||
return { type: 'face', data: { id: String(element.faceId ?? 0) } };
|
||||
return {
|
||||
type: 'face',
|
||||
data: {
|
||||
id: String(element.faceId ?? 0),
|
||||
...(element.resultId !== undefined ? { resultId: element.resultId } : {}),
|
||||
},
|
||||
};
|
||||
},
|
||||
async fromSegment(data) {
|
||||
const id = intOr(data.id, -1);
|
||||
if (id < 0) return null;
|
||||
const element: MessageElementOf<'face'> = { type: 'face', faceId: id };
|
||||
if (data.resultId !== undefined) {
|
||||
if (typeof data.resultId === 'string') {
|
||||
element.resultId = data.resultId;
|
||||
} else if (typeof data.resultId === 'number' && Number.isSafeInteger(data.resultId) && data.resultId >= 0) {
|
||||
element.resultId = String(data.resultId);
|
||||
} else {
|
||||
throw new MessageElementValidationError('INVALID_FIELD', 'face.resultId must be a string or non-negative safe integer', 'face', 'resultId');
|
||||
}
|
||||
}
|
||||
if (data.large !== undefined) {
|
||||
if (data.large === true || data.large === 'true' || data.large === 1 || data.large === '1') {
|
||||
element.large = true;
|
||||
|
||||
@@ -10,7 +10,7 @@ export function segmentToCQ(seg: JsonObject): string {
|
||||
case 'text':
|
||||
return cqEscape(String(data.text ?? ''));
|
||||
case 'face':
|
||||
return `[CQ:face,id=${data.id ?? 0}]`;
|
||||
return `[CQ:face,id=${data.id ?? 0}${data.resultId !== undefined ? `,resultId=${cqEscape(String(data.resultId))}` : ''}]`;
|
||||
case 'image':
|
||||
return `[CQ:image,file=${cqEscape(String(data.file ?? ''))},url=${cqEscape(String(data.url ?? ''))}]`;
|
||||
case 'at':
|
||||
@@ -77,4 +77,3 @@ export async function parseFromCQString(message: string, options?: ParseMessageO
|
||||
|
||||
return elements;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
import fs from 'node:fs';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { decodeRichBody } from '@snowluma/protocol/msg-push/rich-body-decoder';
|
||||
import { convertEvent, type ConverterContext } from '../src/event-converter';
|
||||
import { MessageStore } from '../src/message-store';
|
||||
import { GROUP_MESSAGE_EVENT } from '../src/message-id';
|
||||
import { parseMessage } from '../src/message-parser';
|
||||
import { getGroupMsgHistory } from '../src/modules/message-actions';
|
||||
import { segmentsToRawMessage } from '../src/helper/cq';
|
||||
|
||||
const ctx: ConverterContext = {
|
||||
selfId: 10001,
|
||||
imageUrlResolver: null,
|
||||
mediaUrlResolver: null,
|
||||
messageIdResolver: null,
|
||||
mediaSegmentSink: null,
|
||||
};
|
||||
|
||||
describe('face animation results', () => {
|
||||
it('preserves zero from an incoming message through CQ, persistence and history', async () => {
|
||||
const elements = decodeRichBody({
|
||||
richText: { elems: [{ commonElem: {
|
||||
serviceType: 37,
|
||||
businessType: 2,
|
||||
pbElem: Buffer.from('1872320130', 'hex'),
|
||||
} }] },
|
||||
}, true);
|
||||
expect(elements).toEqual([{ type: 'face', faceId: 114, resultId: '0' }]);
|
||||
|
||||
const event = await convertEvent(ctx, {
|
||||
kind: 'group_message',
|
||||
groupId: 30001,
|
||||
groupName: 'group',
|
||||
senderUin: 20001,
|
||||
senderNick: 'sender',
|
||||
senderCard: '',
|
||||
senderRole: 'member',
|
||||
selfUin: 10001,
|
||||
time: 1_700_000_000,
|
||||
msgSeq: 42,
|
||||
msgId: 10042,
|
||||
elements,
|
||||
});
|
||||
if (!event) throw new Error('message conversion returned no event');
|
||||
const message = [{ type: 'face', data: { id: '114', resultId: '0' } }];
|
||||
expect(event.message).toEqual(message);
|
||||
expect(event.raw_message).toBe('[CQ:face,id=114,resultId=0]');
|
||||
await expect(parseMessage(String(event.raw_message), false)).resolves.toEqual(elements);
|
||||
|
||||
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'snowluma-face-animation-'));
|
||||
const dbPath = path.join(dir, 'messages.db');
|
||||
let store = new MessageStore(dbPath);
|
||||
try {
|
||||
store.storeEvent(Number(event.message_id), true, 30001, 42, GROUP_MESSAGE_EVENT, event);
|
||||
store.close();
|
||||
store = new MessageStore(dbPath);
|
||||
expect(store.findEvent(Number(event.message_id))?.message).toEqual(message);
|
||||
const history = await getGroupMsgHistory(store, 30001, undefined, 20);
|
||||
expect(history).toHaveLength(1);
|
||||
expect(history[0].message).toEqual(message);
|
||||
expect(history[0].raw_message).toBe(event.raw_message);
|
||||
} finally {
|
||||
store.close();
|
||||
fs.rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
it('escapes an opaque result identifier without changing its value', async () => {
|
||||
const resultId = 'a,b&[c]';
|
||||
const raw = segmentsToRawMessage([{ type: 'face', data: { id: '114', resultId } }]);
|
||||
await expect(parseMessage(raw, false)).resolves.toEqual([{ type: 'face', faceId: 114, resultId }]);
|
||||
});
|
||||
});
|
||||
@@ -36,6 +36,21 @@ describe('parseMessage', () => {
|
||||
});
|
||||
|
||||
describe('CQ code parsing', () => {
|
||||
it.each(['0', '2', 0, 2])('preserves an animation result %s', async (resultId) => {
|
||||
expect(await parseMessage([{ type: 'face', data: { id: 114, resultId } }], false))
|
||||
.toEqual([{ type: 'face', faceId: 114, resultId: String(resultId) }]);
|
||||
});
|
||||
|
||||
it.each([null, false, {}, [], '', ' ', -1, 1.5, Number.MAX_SAFE_INTEGER + 1])('rejects an invalid animation result %j', async (resultId) => {
|
||||
await expect(parseMessage([{ type: 'face', data: { id: 114, resultId } }], false))
|
||||
.rejects.toMatchObject({ code: 'INVALID_FIELD', field: 'resultId' });
|
||||
});
|
||||
|
||||
it('rejects a selected animation on a small face', async () => {
|
||||
await expect(parseMessage('[CQ:face,id=114,large=false,resultId=0]', false))
|
||||
.rejects.toMatchObject({ code: 'INVALID_FIELD', field: 'resultId' });
|
||||
});
|
||||
|
||||
it('parses face CQ code', async () => {
|
||||
const result = await parseMessage('[CQ:face,id=123]', false);
|
||||
expect(result).toHaveLength(1);
|
||||
|
||||
Reference in new issue
Block a user