feat(onebot): unify market faces to image segments + add send support
Market faces (商城表情) now decode to an OneBot `image` segment carrying
NapCat-style markers (emoji_id/emoji_package_id/key/summary) plus a gxh
gif URL, so clients that don't special-case `mface` still render the
sticker while aware clients can reproduce it.
Adds the previously-missing send path: a dedicated `mface` segment OR an
`image` segment carrying `emoji_id` rebuilds the wire `marketFace`
(faceId=hex(emoji_id), itemType 6 / subType 3 / 300×300 /
pbReserve.field8=1) — byte-matched against NapCat and Lagrange.Core.
- proto-defs: MarketFace.key bytes→string; +MarketFacePbReserve{field8@8}
- protocol: decode faceId→emojiId(hex)/tabId/key; makeMarketFaceElem +
buildSendElems 'mface' case; format preview keeps the sticker name
- onebot: to-segment mface→image; message-parser mface + image(emoji_id)
- tests: wire decode + full proton round-trip; parse→build; segment shape
Reply/forward round-trips preserved (element keeps type 'mface' + text).
This commit is contained in:
10 files changed
+247
-16
No files matched your search
@@ -127,12 +127,27 @@ async function elementToSegment(
|
||||
}
|
||||
|
||||
if (element.type === 'mface') {
|
||||
// Unify market faces (商城表情) to an `image` segment so OneBot clients
|
||||
// that don't special-case `mface` still render the sticker, while the
|
||||
// `emoji_id`/`emoji_package_id`/`key` markers let aware clients (and our
|
||||
// own send path) reproduce it as a real market face. Mirrors NapCat's
|
||||
// marketFaceElement → image conversion. The gxh URL is a self-contained
|
||||
// external link (no rkey), so we set it directly and skip mediaSegmentSink.
|
||||
const emojiId = element.emojiId ?? '';
|
||||
const dir = emojiId.slice(0, 2);
|
||||
const url = emojiId
|
||||
? `https://gxh.vip.qq.com/club/item/parcel/item/${dir}/${emojiId}/raw300.gif`
|
||||
: '';
|
||||
return {
|
||||
type: 'mface',
|
||||
type: 'image',
|
||||
data: {
|
||||
name: element.text ?? '',
|
||||
tab_id: element.faceId ?? 0,
|
||||
sub_type: element.subType ?? 0,
|
||||
file: emojiId ? `${dir}-${emojiId}.gif` : '',
|
||||
url,
|
||||
summary: element.text ?? '',
|
||||
sub_type: 0,
|
||||
emoji_id: emojiId,
|
||||
emoji_package_id: element.emojiPackageId ?? 0,
|
||||
key: element.emojiKey ?? '',
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -23,6 +23,20 @@ function intOr(value: unknown, fallback = 0): number {
|
||||
return Number.isFinite(n) ? n : fallback;
|
||||
}
|
||||
|
||||
/** Build a market-face (`mface`) element from an OneBot segment's data.
|
||||
* Shared by the dedicated `mface` segment and the `image`-with-`emoji_id`
|
||||
* round-trip path. `emojiId` is the hex GUID the wire builder converts back
|
||||
* to `MarketFace.faceId`. */
|
||||
function marketFaceElement(emojiId: string, data: Record<string, unknown>): MessageElement {
|
||||
return {
|
||||
type: 'mface',
|
||||
text: String(data.summary ?? data.name ?? ''),
|
||||
emojiId,
|
||||
emojiPackageId: intOr(data.emoji_package_id ?? data.tab_id, 0),
|
||||
emojiKey: String(data.key ?? ''),
|
||||
};
|
||||
}
|
||||
|
||||
export function parseCQParams(raw: string): Record<string, string> {
|
||||
const params: Record<string, string> = {};
|
||||
if (!raw) return params;
|
||||
@@ -114,6 +128,11 @@ export async function segmentToElement(type: string, data: Record<string, unknow
|
||||
return id > 0 ? { type: 'reply', replySeq: id } : null;
|
||||
}
|
||||
case 'image': {
|
||||
// A market face that was surfaced as an `image` (see to-segment) can be
|
||||
// echoed straight back: when `emoji_id` is present we rebuild the market
|
||||
// face instead of re-uploading the gif as a plain picture.
|
||||
const imgEmojiId = String(data.emoji_id ?? '').trim();
|
||||
if (imgEmojiId) return marketFaceElement(imgEmojiId, data);
|
||||
return {
|
||||
type: 'image',
|
||||
url: String(data.file ?? data.url ?? data.path ?? data.media ?? ''),
|
||||
@@ -122,6 +141,16 @@ export async function segmentToElement(type: string, data: Record<string, unknow
|
||||
summary: data.summary ? String(data.summary) : undefined,
|
||||
};
|
||||
}
|
||||
case 'mface': {
|
||||
// Market face (商城表情). emoji_id is the hex GUID; without it we can't
|
||||
// construct the wire element, so drop the segment.
|
||||
const emojiId = String(data.emoji_id ?? '').trim();
|
||||
if (!emojiId) {
|
||||
log.warn('[MsgParser] mface segment without emoji_id is unsupported');
|
||||
return null;
|
||||
}
|
||||
return marketFaceElement(emojiId, data);
|
||||
}
|
||||
case 'record': {
|
||||
const source = String(data.file ?? data.url ?? data.path ?? data.media ?? '');
|
||||
if (!source) return null;
|
||||
|
||||
@@ -453,9 +453,23 @@ describe('convertEvent — message elements (13 segment types)', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('mface: text + tab_id + sub_type', async () => {
|
||||
const seg = await segment({ type: 'mface', text: 'sticker', faceId: 7, subType: 2 });
|
||||
expect(seg).toEqual({ type: 'mface', data: { name: 'sticker', tab_id: 7, sub_type: 2 } });
|
||||
it('mface: unified to an image segment carrying market-face markers', async () => {
|
||||
const emojiId = '235a82d9c0acd2e2db6e0b94e1a1c4f3';
|
||||
const seg = await segment({
|
||||
type: 'mface', text: '可爱', emojiId, emojiPackageId: 12, emojiKey: 'abc',
|
||||
});
|
||||
expect(seg).toEqual({
|
||||
type: 'image',
|
||||
data: {
|
||||
file: `23-${emojiId}.gif`,
|
||||
url: `https://gxh.vip.qq.com/club/item/parcel/item/23/${emojiId}/raw300.gif`,
|
||||
summary: '可爱',
|
||||
sub_type: 0,
|
||||
emoji_id: emojiId,
|
||||
emoji_package_id: 12,
|
||||
key: 'abc',
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('poke: subType forwarded as data.type', async () => {
|
||||
|
||||
@@ -6,6 +6,7 @@ import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
|
||||
import { parseMessage } from '../src/message-parser';
|
||||
import { buildSendElems } from '@snowluma/protocol/element-builder';
|
||||
import type { MentionExtraSend } from '@snowluma/proto-defs/action';
|
||||
import type { MarketFacePbReserve } from '@snowluma/proto-defs/element';
|
||||
import type { OidbBase } from '@snowluma/proto-defs/oidb';
|
||||
import type { NTV2UploadRichMediaResp } from '@snowluma/proto-defs/highway';
|
||||
|
||||
@@ -313,6 +314,72 @@ describe('parseMessage', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('market face (mface)', () => {
|
||||
const EMOJI_ID = '235a82d9c0acd2e2db6e0b94e1a1c4f3';
|
||||
|
||||
it('parses an mface segment into a market-face element', async () => {
|
||||
const result = await parseMessage(
|
||||
[{ type: 'mface', data: { emoji_id: EMOJI_ID, emoji_package_id: 12, key: 'abc', summary: '可爱' } }] as any,
|
||||
false,
|
||||
);
|
||||
expect(result).toEqual([{
|
||||
type: 'mface', text: '可爱', emojiId: EMOJI_ID, emojiPackageId: 12, emojiKey: 'abc',
|
||||
}]);
|
||||
});
|
||||
|
||||
it('round-trips an image-with-emoji_id back into a market face (not a re-uploaded picture)', async () => {
|
||||
const result = await parseMessage(
|
||||
[{
|
||||
type: 'image',
|
||||
data: {
|
||||
file: `23-${EMOJI_ID}.gif`,
|
||||
url: `https://gxh.vip.qq.com/club/item/parcel/item/23/${EMOJI_ID}/raw300.gif`,
|
||||
emoji_id: EMOJI_ID, emoji_package_id: 12, key: 'abc', summary: '可爱',
|
||||
},
|
||||
}] as any,
|
||||
false,
|
||||
);
|
||||
expect(result).toEqual([{
|
||||
type: 'mface', text: '可爱', emojiId: EMOJI_ID, emojiPackageId: 12, emojiKey: 'abc',
|
||||
}]);
|
||||
});
|
||||
|
||||
it('leaves a plain image (no emoji_id) as an image element', async () => {
|
||||
const result = await parseMessage(
|
||||
[{ type: 'image', data: { file: 'https://example.com/a.png' } }] as any,
|
||||
false,
|
||||
);
|
||||
expect(result[0]!.type).toBe('image');
|
||||
expect((result[0] as any).emojiId).toBeUndefined();
|
||||
});
|
||||
|
||||
it('drops an mface segment without emoji_id', async () => {
|
||||
const result = await parseMessage(
|
||||
[{ type: 'mface', data: { emoji_package_id: 12, summary: 'x' } }] as any,
|
||||
false,
|
||||
);
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
|
||||
it('builds the wire marketFace element (faceId = hex(emoji_id), NapCat constants)', async () => {
|
||||
const elements = await parseMessage(
|
||||
[{ type: 'mface', data: { emoji_id: EMOJI_ID, emoji_package_id: 12, key: 'abc', summary: '可爱' } }] as any,
|
||||
false,
|
||||
);
|
||||
const protoElems = await buildSendElems(elements);
|
||||
expect(protoElems).toHaveLength(1);
|
||||
const mf = protoElems[0]!.marketFace;
|
||||
expect(mf).toBeDefined();
|
||||
expect(Buffer.from(mf!.faceId as Uint8Array).toString('hex')).toBe(EMOJI_ID);
|
||||
expect(mf).toMatchObject({
|
||||
faceName: '可爱', itemType: 6, faceInfo: 1, tabId: 12, subType: 3,
|
||||
key: 'abc', imageWidth: 300, imageHeight: 300,
|
||||
});
|
||||
const reserve = protobuf_decode<MarketFacePbReserve>(mf!.pbReserve as Uint8Array);
|
||||
expect(reserve).toMatchObject({ field8: 1 });
|
||||
});
|
||||
});
|
||||
|
||||
describe('special segments', () => {
|
||||
it('parses json segment', async () => {
|
||||
const result = await parseMessage(
|
||||
|
||||
Reference in new issue
Block a user