fix(protocol): group reply+at — emit replied-sender mention after srcMsg (#129)

When a group message combined a reply with a user @ , the @ showed but
never notified (no blue link, no ping). On-target capture showed SnowLuma
sent [srcMsg, @target(mention), text] — the @ lands directly in the reply's
mention slot right after srcMsg, which the QQ NT group server does not honor.

Mirror Lagrange's ForwardEntity.PackElement: when a reply and an @ coexist
in a group message, emit the replied sender as a trailing mention element
right after srcMsg, so the user's @ is no longer the first post-srcMsg
element and is honored. Gated to the reply+@ case so plain replies are
unchanged.

Validated on-target (Docker, group send): baseline reply+at -> @ not
delivered; fixed -> @ delivered (blue link + notification).
This commit is contained in:
DrSmoothl committed 2026-06-22 20:37:19 +08:00
1 parent 839de51519
commit 45aae92af5
1 file changed
+27 -1
+27 -1
View File
@@ -105,6 +105,20 @@ function makeReplyElem(element: MessageElement): ProtoElem {
return { srcMsg };
}
// The replied sender, encoded as a mention element placed right after srcMsg.
// QQ NT's group reply wire shape expects this; without it a following
// user-supplied @ is silently not honored by the server (issue #129).
async function makeReplyMentionElem(ctx: SendContext, uin: number): Promise<ProtoElem | null> {
if (ctx.groupId === undefined || uin <= 0) return null;
const member = ctx.bridge.identity.findGroupMember(ctx.groupId, uin);
let uid = member?.uid ?? '';
if (!uid) uid = await ctx.bridge.identity.resolveUid(uin, ctx.groupId).catch(() => '');
if (!uid) return null;
const name = member?.card?.trim() || member?.nickname?.trim() || String(uin);
const extra = protobuf_encode<MentionExtraSend>({ type: 2, uin, field5: 0, uid });
return { text: { str: `@${name} `, pbReserve: extra } };
}
function makeDeflatedPayload(content: string): Uint8Array {
const deflated = deflateSync(Buffer.from(content, 'utf8'));
const payload = new Uint8Array(deflated.length + 1);
@@ -378,6 +392,12 @@ async function makeVideoElem(ctx: SendContext, element: MessageElement): Promise
*/
export async function buildSendElems(elements: MessageElement[], ctx?: SendContext): Promise<ProtoElem[]> {
const result: ProtoElem[] = [];
// A user-supplied @ that lands immediately after a reply's srcMsg isn't
// honored by the QQ NT group server (the @ shows but never notifies, #129).
// When a reply and an @ coexist, emit the replied sender as a trailing
// mention right after srcMsg (mirrors Lagrange ForwardEntity.PackElement),
// which restores the wire shape the server expects.
const hasUserAt = elements.some((e) => e.type === 'at');
for (const elem of elements) {
switch (elem.type) {
@@ -398,7 +418,13 @@ export async function buildSendElems(elements: MessageElement[], ctx?: SendConte
break;
case 'reply':
if (elem.replySeq) result.push(makeReplyElem(elem));
if (elem.replySeq) {
result.push(makeReplyElem(elem));
if (hasUserAt && ctx?.groupId !== undefined && elem.replySenderUin) {
const replyMention = await makeReplyMentionElem(ctx, elem.replySenderUin);
if (replyMention) result.push(replyMention);
}
}
break;
case 'json':