fix: #7
This commit is contained in:
1 parent
2a4ca46a99
commit
0d8e52ee77
3 files changed
+68
-2
No files matched your search
@@ -500,6 +500,7 @@ async function handleSendPrivate(
|
|||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
},
|
},
|
||||||
|
resolveMentionUid: (targetUin) => ref.bridge.resolveUserUid(targetUin),
|
||||||
musicSignUrl: ref.musicSignUrl,
|
musicSignUrl: ref.musicSignUrl,
|
||||||
});
|
});
|
||||||
if (elements.length === 0) throw new Error('message is empty');
|
if (elements.length === 0) throw new Error('message is empty');
|
||||||
@@ -545,6 +546,7 @@ async function handleSendGroup(
|
|||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
},
|
},
|
||||||
|
resolveMentionUid: (targetUin) => ref.bridge.resolveUserUid(targetUin, groupId),
|
||||||
musicSignUrl: ref.musicSignUrl,
|
musicSignUrl: ref.musicSignUrl,
|
||||||
});
|
});
|
||||||
if (elements.length === 0) throw new Error('message is empty');
|
if (elements.length === 0) throw new Error('message is empty');
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import type { JsonValue, JsonObject } from './types';
|
|||||||
export interface ParseMessageOptions {
|
export interface ParseMessageOptions {
|
||||||
resolveReplySequence?: (replyMessageId: number) => number | null;
|
resolveReplySequence?: (replyMessageId: number) => number | null;
|
||||||
resolveReplyMeta?: (replyMessageId: number) => { senderUin: number; time: number; random: number } | null;
|
resolveReplyMeta?: (replyMessageId: number) => { senderUin: number; time: number; random: number } | null;
|
||||||
|
resolveMentionUid?: (targetUin: number) => string | null | Promise<string | null>;
|
||||||
musicSignUrl?: string;
|
musicSignUrl?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -89,12 +90,25 @@ async function segmentToElement(type: string, data: Record<string, unknown>, opt
|
|||||||
return { type: 'face', faceId: id };
|
return { type: 'face', faceId: id };
|
||||||
}
|
}
|
||||||
case 'at': {
|
case 'at': {
|
||||||
const qq = String(data.qq ?? '');
|
const qq = String(data.qq ?? '').trim();
|
||||||
if (qq === 'all') {
|
if (qq === 'all') {
|
||||||
return { type: 'at', targetUin: 0, uid: 'all', text: '@全体成员 ' };
|
return { type: 'at', targetUin: 0, uid: 'all', text: '@全体成员 ' };
|
||||||
}
|
}
|
||||||
const uin = parseInt(qq, 10);
|
const uin = parseInt(qq, 10);
|
||||||
return uin > 0 ? { type: 'at', targetUin: uin, text: `@${uin} ` } : null;
|
if (uin <= 0) return null;
|
||||||
|
|
||||||
|
const name = String(data.name ?? data.nickname ?? data.card ?? '').trim();
|
||||||
|
let uid = String(data.uid ?? '').trim();
|
||||||
|
if (!uid && options?.resolveMentionUid) {
|
||||||
|
uid = (await options.resolveMentionUid(uin))?.trim() ?? '';
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
type: 'at',
|
||||||
|
targetUin: uin,
|
||||||
|
uid: uid || undefined,
|
||||||
|
text: name ? `@${name} ` : `@${uin} `,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
case 'reply': {
|
case 'reply': {
|
||||||
const id = parseInt(String(data.id ?? '0'), 10);
|
const id = parseInt(String(data.id ?? '0'), 10);
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
import { describe, it, expect } from 'vitest';
|
import { describe, it, expect } from 'vitest';
|
||||||
import { parseMessage } from '../src/onebot/message-parser';
|
import { parseMessage } from '../src/onebot/message-parser';
|
||||||
|
import { buildSendElems } from '../src/bridge/builders/element-builder';
|
||||||
|
import { MentionExtraSendSchema } from '../src/bridge/proto/action';
|
||||||
|
import { protoDecode } from '../src/protobuf/decode';
|
||||||
|
|
||||||
describe('parseMessage', () => {
|
describe('parseMessage', () => {
|
||||||
describe('plain text', () => {
|
describe('plain text', () => {
|
||||||
@@ -94,6 +97,53 @@ describe('parseMessage', () => {
|
|||||||
expect(result[0].targetUin).toBe(12345);
|
expect(result[0].targetUin).toBe(12345);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('uses at segment name for display text and preserves uid', async () => {
|
||||||
|
const result = await parseMessage(
|
||||||
|
[{ type: 'at', data: { qq: '123456', name: 'User', uid: 'u_test_uid' } }] as any,
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
expect(result).toHaveLength(1);
|
||||||
|
expect(result[0]).toMatchObject({
|
||||||
|
type: 'at',
|
||||||
|
targetUin: 123456,
|
||||||
|
uid: 'u_test_uid',
|
||||||
|
text: '@User ',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('resolves missing at uid through parse options', async () => {
|
||||||
|
const result = await parseMessage(
|
||||||
|
[{ type: 'at', data: { qq: '123456', name: 'User' } }] as any,
|
||||||
|
false,
|
||||||
|
{ resolveMentionUid: async (targetUin) => targetUin === 123456 ? 'u_resolved_uid' : null },
|
||||||
|
);
|
||||||
|
expect(result).toHaveLength(1);
|
||||||
|
expect(result[0]).toMatchObject({
|
||||||
|
type: 'at',
|
||||||
|
targetUin: 123456,
|
||||||
|
uid: 'u_resolved_uid',
|
||||||
|
text: '@User ',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('encodes mention extra with resolved uid for QQ notification', async () => {
|
||||||
|
const elements = await parseMessage(
|
||||||
|
[{ type: 'at', data: { qq: '123456', name: 'User' } }] as any,
|
||||||
|
false,
|
||||||
|
{ resolveMentionUid: () => 'u_resolved_uid' },
|
||||||
|
);
|
||||||
|
const protoElems = await buildSendElems(elements);
|
||||||
|
const reserve = protoElems[0].text?.pbReserve;
|
||||||
|
expect(reserve).toBeInstanceOf(Uint8Array);
|
||||||
|
const extra = protoDecode(reserve as Uint8Array, MentionExtraSendSchema);
|
||||||
|
expect(extra).toMatchObject({
|
||||||
|
type: 2,
|
||||||
|
uin: 123456,
|
||||||
|
uid: 'u_resolved_uid',
|
||||||
|
});
|
||||||
|
expect(protoElems[0].text?.str).toBe('@User ');
|
||||||
|
});
|
||||||
|
|
||||||
it('parses multiple segments', async () => {
|
it('parses multiple segments', async () => {
|
||||||
const result = await parseMessage(
|
const result = await parseMessage(
|
||||||
[
|
[
|
||||||
|
|||||||
Reference in new issue
Block a user