fix(onebot): restore cached reply previews (#458)
Restore readable quote previews for cached messages while preserving reply targets and message content. Refs #458
This commit is contained in:
1 parent
1ef9a2c330
commit
7a07119ee9
6 files changed
+108
No files matched your search
@@ -78,6 +78,10 @@ pnpm test
|
||||
|
||||
贡献流程见 [`CONTRIBUTING.md`](CONTRIBUTING.md);模块边界与项目词汇见 [`CONTEXT.md`](CONTEXT.md);开发方向见 [`RoadMap.md`](RoadMap.md)。
|
||||
|
||||
## 引用消息预览
|
||||
|
||||
OneBot 发送 `reply` 段时,会将本地消息缓存中的原文附入 QQ 引用预览,保留文字、换行与引用关系。图片、语音等非文字段以类型提示显示,不重新上传媒体;原消息中的嵌套引用不递归展开。只有消息元数据而没有缓存正文时,不生成虚构的预览内容。
|
||||
|
||||
## 使用边界与许可
|
||||
|
||||
> [!IMPORTANT]
|
||||
|
||||
@@ -246,6 +246,7 @@ export const ELEMENT_CODECS = {
|
||||
element.replySenderUin = meta.senderUin;
|
||||
element.replyTime = meta.time;
|
||||
element.replyRandom = meta.random;
|
||||
element.replyElements = meta.elements;
|
||||
}
|
||||
|
||||
return element;
|
||||
|
||||
@@ -22,6 +22,7 @@ export interface ParseMessageOptions {
|
||||
senderUin: number;
|
||||
time: number;
|
||||
random: number;
|
||||
elements?: MessageElement[];
|
||||
sequenceAuthoritative?: boolean;
|
||||
} | null;
|
||||
resolveMentionUid?: (targetUin: number) => string | null | Promise<string | null>;
|
||||
|
||||
@@ -26,6 +26,27 @@ import { hasAuthoritativeSequence, type JsonArray, type JsonObject, type JsonVal
|
||||
|
||||
const log = createLogger('OneBot');
|
||||
|
||||
function replyPreviewElements(message: unknown): MessageElement[] {
|
||||
if (!Array.isArray(message)) return [];
|
||||
const labels: Record<string, string> = {
|
||||
image: '[图片]', record: '[语音]', video: '[视频]', file: '[文件]',
|
||||
face: '[表情]', mface: '[表情]', forward: '[聊天记录]',
|
||||
json: '[卡片]', xml: '[卡片]', markdown: '[Markdown]',
|
||||
};
|
||||
const parts: string[] = [];
|
||||
for (const segment of message) {
|
||||
if (!segment || typeof segment !== 'object') continue;
|
||||
const data = segment.data;
|
||||
if (!data || typeof data !== 'object') continue;
|
||||
if (segment.type === 'text' && typeof data.text === 'string') parts.push(data.text);
|
||||
else if (segment.type === 'at') parts.push(data.qq === 'all' ? '@全体成员' : `@${data.qq ?? ''}`);
|
||||
else if (typeof segment.type === 'string' && labels[segment.type]) parts.push(labels[segment.type]);
|
||||
}
|
||||
const text = parts.join('');
|
||||
return text ? [{ type: 'text', text }] : [];
|
||||
}
|
||||
|
||||
|
||||
// A video larger than QQ's Highway video ceiling can't be sent through the
|
||||
// element pipeline — it must fall back to a regular file upload. The fallback
|
||||
// is decided at the OneBot layer (not element-builder) because building a
|
||||
@@ -840,6 +861,7 @@ export async function sendPrivateMessage(
|
||||
senderUin,
|
||||
time,
|
||||
random: meta?.random ?? 0,
|
||||
elements: replyPreviewElements(event.message),
|
||||
sequenceAuthoritative: meta?.sequenceAuthoritative,
|
||||
};
|
||||
}
|
||||
@@ -1081,6 +1103,7 @@ export async function sendGroupMessage(
|
||||
? event.time
|
||||
: parseInt(String(event.time || '0'), 10),
|
||||
random: meta?.random ?? 0,
|
||||
elements: replyPreviewElements(event.message),
|
||||
sequenceAuthoritative: meta?.sequenceAuthoritative,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
|
||||
import type { Elem } from '@snowluma/proto-defs/element';
|
||||
import { buildSendElems } from '@snowluma/protocol/element-builder';
|
||||
import type { MessageElement } from '@snowluma/protocol/events';
|
||||
import type { OneBotInstanceContext } from '../src/instance-context';
|
||||
import type { JsonValue } from '../src/types';
|
||||
import { sendGroupMessage, sendPrivateMessage } from '../src/modules/message-actions';
|
||||
|
||||
const sender = 20002;
|
||||
const target = 30003;
|
||||
const time = 1700000000;
|
||||
const sequence = 42;
|
||||
|
||||
async function sendQuote(isGroup: boolean, original: JsonValue | null) {
|
||||
const packets: Partial<Elem>[] = [];
|
||||
const send = vi.fn(async (_target: number, elements: MessageElement[]) => {
|
||||
const encoded = await buildSendElems(elements, { bridge: {} as never, groupId: isGroup ? target : undefined });
|
||||
for (const element of encoded) {
|
||||
packets.push(protobuf_decode<Elem>(protobuf_encode<Elem>(element)));
|
||||
}
|
||||
return { messageId: 1, sequence: 100, clientSequence: 0, random: 1, timestamp: time + 1 };
|
||||
});
|
||||
const ref = {
|
||||
uin: '10001', selfId: 10001,
|
||||
bridge: { apis: { message: { sendGroup: send, sendPrivate: send } } },
|
||||
messageStore: {
|
||||
findEvent: () => original === null ? null : { user_id: sender, time, message: original },
|
||||
findMeta: () => ({ isGroup, targetId: target, timestamp: time, random: 7, sequenceAuthoritative: true }),
|
||||
resolveReplySequence: () => sequence,
|
||||
},
|
||||
cacheMessageMeta: vi.fn(), mediaStore: {}, musicSignUrl: '',
|
||||
} as unknown as OneBotInstanceContext;
|
||||
await (isGroup ? sendGroupMessage : sendPrivateMessage)(ref, target, [
|
||||
{ type: 'reply', data: { id: '-12345' } },
|
||||
{ type: 'text', data: { text: 'answer' } },
|
||||
], false);
|
||||
expect(send).toHaveBeenCalledOnce();
|
||||
expect(packets.find(p => p.text)?.text?.str).toBe('answer');
|
||||
return packets.find(p => p.srcMsg)?.srcMsg;
|
||||
}
|
||||
|
||||
function preview(src: Partial<Elem>['srcMsg']) {
|
||||
return src?.elemsRaw?.map(raw => protobuf_decode<Elem>(raw).text?.str).join('');
|
||||
}
|
||||
|
||||
describe.each([true, false])('outbound quote preview (group=%s)', (isGroup) => {
|
||||
it('carries cached Unicode text through OneBot parsing and wire encoding', async () => {
|
||||
const src = await sendQuote(isGroup, [
|
||||
{ type: 'text', data: { text: '第一段\n' } },
|
||||
{ type: 'text', data: { text: '第二段😀' } },
|
||||
]);
|
||||
expect(src?.origSeqs).toEqual([sequence]);
|
||||
expect(src?.senderUin).toBe(BigInt(sender));
|
||||
expect(src?.time).toBe(time);
|
||||
expect(preview(src)).toBe('第一段\n第二段😀');
|
||||
});
|
||||
|
||||
it('previews media without uploading it or recursively quoting earlier replies', async () => {
|
||||
const src = await sendQuote(isGroup, [
|
||||
{ type: 'reply', data: { id: '99' } },
|
||||
{ type: 'image', data: { file: 'unavailable.png' } },
|
||||
{ type: 'record', data: { file: 'unavailable.mp3' } },
|
||||
{ type: 'text', data: { text: '说明' } },
|
||||
]);
|
||||
expect(preview(src)).toBe('[图片][语音]说明');
|
||||
});
|
||||
|
||||
it('does not invent content when only message metadata is cached', async () => {
|
||||
const src = await sendQuote(isGroup, null);
|
||||
expect(src?.origSeqs).toEqual([sequence]);
|
||||
expect(src?.senderUin).toBe(10001n);
|
||||
expect(src?.elemsRaw?.length ?? 0).toBe(0);
|
||||
});
|
||||
});
|
||||
@@ -150,6 +150,10 @@ function makeReplyElem(element: MessageElement): ProtoElem {
|
||||
srcMsg.time = element.replyTime;
|
||||
}
|
||||
|
||||
const previews = (element.replyElements ?? []).filter(e => e.type === 'text' && e.text);
|
||||
if (previews.length) {
|
||||
srcMsg.elemsRaw = previews.map(e => protobuf_encode<Elem>({ text: { str: e.text! } }));
|
||||
}
|
||||
return { srcMsg };
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user