diff --git a/packages/core/tests/event-format.test.ts b/packages/core/tests/event-format.test.ts index de188683..cd1d0caa 100644 --- a/packages/core/tests/event-format.test.ts +++ b/packages/core/tests/event-format.test.ts @@ -286,12 +286,12 @@ describe('formatEvent', () => { .toBe('群戳 [Grp(1)] | [Alice(100)] -> [Bob(200)]'); }); - it('renders friend_poke without group context', () => { + it('renders friend_poke with its private peer and both endpoints', () => { expect(formatEvent(id, { kind: 'friend_poke', - userUin: 500, targetUin: 600, + peerUin: 500, senderUin: 600, targetUin: 500, } as QQEventVariant)) - .toBe('戳一戳 [FriendA(500)] -> [FriendB(600)]'); + .toBe('私聊戳 [FriendA(500)] | [FriendB(600)] -> [FriendA(500)]'); }); it('degrades to numeric IDs when nothing is cached', () => { diff --git a/packages/onebot/src/event-converter/to-notice.ts b/packages/onebot/src/event-converter/to-notice.ts index 2ba3ab35..0a347689 100644 --- a/packages/onebot/src/event-converter/to-notice.ts +++ b/packages/onebot/src/event-converter/to-notice.ts @@ -111,7 +111,8 @@ export function convertFriendPoke(ctx: ConverterContext, event: FriendPoke): Jso return notice(ctx, event, { notice_type: 'notify', sub_type: 'poke', - user_id: event.userUin, + user_id: event.peerUin, + sender_id: event.senderUin, target_id: event.targetUin, action: event.action, suffix: event.suffix, diff --git a/packages/onebot/tests/event-converter.test.ts b/packages/onebot/tests/event-converter.test.ts index 9b9448f2..c5926708 100644 --- a/packages/onebot/tests/event-converter.test.ts +++ b/packages/onebot/tests/event-converter.test.ts @@ -253,16 +253,25 @@ describe('convertEvent — notice kinds', () => { expect(out!.message_id).toBe(99); }); - it('friend_poke: notice/notify/poke with target_id + action fields', async () => { + it('friend_poke: identifies the private peer, sender, and target independently', async () => { const out = await convertEvent(bareCtx(), { kind: 'friend_poke', time: 1, selfUin: SELF_ID, - userUin: PEER_UIN, targetUin: SELF_ID, + peerUin: PEER_UIN, senderUin: SELF_ID, targetUin: SELF_ID, action: '戳了戳', suffix: '一下', actionImgUrl: 'http://x', } as QQEventVariant); - expect(out!.notice_type).toBe('notify'); - expect(out!.sub_type).toBe('poke'); - expect(out!.target_id).toBe(SELF_ID); - expect(out!.action).toBe('戳了戳'); + expect(out).toEqual({ + time: 1, + self_id: SELF_ID, + post_type: 'notice', + notice_type: 'notify', + sub_type: 'poke', + user_id: PEER_UIN, + sender_id: SELF_ID, + target_id: SELF_ID, + action: '戳了戳', + suffix: '一下', + action_img_url: 'http://x', + }); }); it('group_poke: same shape as friend_poke + group_id', async () => { diff --git a/packages/protocol/src/events.ts b/packages/protocol/src/events.ts index e56d2ec9..35a8f66a 100644 --- a/packages/protocol/src/events.ts +++ b/packages/protocol/src/events.ts @@ -360,7 +360,10 @@ export interface GroupInviteEvent extends QQEvent { export interface FriendPokeEvent extends QQEvent { kind: 'friend_poke'; - userUin: number; + /** The other participant whose private conversation contains this notice. */ + peerUin: number; + /** The account that performed the poke, which may be the logged-in account. */ + senderUin: number; targetUin: number; action: string; suffix: string; diff --git a/packages/protocol/src/format.ts b/packages/protocol/src/format.ts index f1ed875e..bb442018 100644 --- a/packages/protocol/src/format.ts +++ b/packages/protocol/src/format.ts @@ -277,7 +277,7 @@ export function formatEvent(identity: IdentityService, event: QQEventVariant): s case 'group_admin': return `管理 ${formatGroup(identity, event.groupId)} | ${formatUser(identity, event.groupId, event.userUin)} ${event.set ? '+' : '-'}管理员`; case 'friend_poke': - return `戳一戳 ${formatUser(identity, undefined, event.userUin)} -> ${formatUser(identity, undefined, event.targetUin)}`; + return `私聊戳 ${formatUser(identity, undefined, event.peerUin)} | ${formatUser(identity, undefined, event.senderUin)} -> ${formatUser(identity, undefined, event.targetUin)}`; case 'group_poke': return `群戳 ${formatGroup(identity, event.groupId)} | ${formatUser(identity, event.groupId, event.userUin)} -> ${formatUser(identity, event.groupId, event.targetUin)}`; case 'friend_request': diff --git a/packages/protocol/src/msg-push/decoders/event-0x210.ts b/packages/protocol/src/msg-push/decoders/event-0x210.ts index 70755a51..f0b7f640 100644 --- a/packages/protocol/src/msg-push/decoders/event-0x210.ts +++ b/packages/protocol/src/msg-push/decoders/event-0x210.ts @@ -257,11 +257,32 @@ function decodeFriendPoke(ctx: MsgPushContext): QQEventVariant[] { const templates = buildTemplateMap(grayTip.msgTemplParam ?? []); const actor = findTemplateValue(templates, 'uin_str1'); const target = findTemplateValue(templates, 'uin_str2'); + const senderUin = resolveUidToUin(ctx.identity, 0, actor, parseU64OrZero(actor)); + const routeFromUin = resolveUidToUin(ctx.identity, 0, ctx.fromUid, ctx.fromUin); + // The gray-tip template only identifies the two poke endpoints. The C2C + // routing head owns the conversation peer: outbound notices use `to`, + // while inbound notices use `from`, just like ordinary friend messages. + const sentBySelf = routeFromUin > 0 && routeFromUin === ctx.selfUin; + const peerUin = sentBySelf + ? resolveUidToUin( + ctx.identity, + 0, + ctx.responseHead?.toUid ?? '', + ctx.responseHead?.toUin ?? 0, + ) + : routeFromUin; + if (peerUin <= 0) { + throw new Error( + 'friend poke conversation peer is missing ' + + `(fromUin=${ctx.fromUin} toUin=${ctx.responseHead?.toUin ?? 0} sentBySelf=${sentBySelf})`, + ); + } const ev: FriendPokeEvent = { kind: 'friend_poke', time: ctx.head.timestamp, selfUin: ctx.selfUin, - userUin: resolveUidToUin(ctx.identity, 0, actor, parseU64OrZero(actor)), + peerUin, + senderUin, targetUin: resolveUidToUin(ctx.identity, 0, target, parseU64OrZero(target)), action: findTemplateValue(templates, 'action_str', 'alt_str1'), suffix: findTemplateValue(templates, 'suffix_str'), diff --git a/packages/protocol/tests/msg-push/friend-poke.test.ts b/packages/protocol/tests/msg-push/friend-poke.test.ts new file mode 100644 index 00000000..e5d8ba4c --- /dev/null +++ b/packages/protocol/tests/msg-push/friend-poke.test.ts @@ -0,0 +1,79 @@ +import type { PacketInfo } from '@snowluma/common/protocol-types'; +import type { PushMsg } from '@snowluma/proto-defs/message'; +import type { GeneralGrayTipInfo } from '@snowluma/proto-defs/notify'; +import { protobuf_encode } from '@snowluma/proton'; +import { describe, expect, it } from 'vitest'; + +import type { IdentityService } from '../../src/identity-service'; +import { parseMsgPush } from '../../src/msg-push'; + +const SELF_UIN = 10001; +const FRIEND_A = 20001; +const FRIEND_B = 20002; + +const identity = { + findUinByUid: () => null, +} as unknown as IdentityService; + +function friendPokePacket( + peerUin: number, + senderUin: number, + targetUin: number, +): PacketInfo { + const sentBySelf = senderUin === SELF_UIN; + const grayTip = protobuf_encode({ + busiType: 12n, + msgTemplParam: [ + { name: 'uin_str1', value: String(senderUin) }, + { name: 'uin_str2', value: String(targetUin) }, + { name: 'action_str', value: '拍了拍' }, + { name: 'suffix_str', value: '一下' }, + ], + }); + const body = protobuf_encode({ + message: { + responseHead: sentBySelf + ? { fromUin: SELF_UIN, toUin: peerUin } + : { fromUin: peerUin, toUin: SELF_UIN }, + contentHead: { + msgType: 528, + subType: 290, + timestamp: 1710000000, + }, + body: { msgContent: grayTip }, + }, + }); + return { + pid: 1, + uin: String(SELF_UIN), + serviceCmd: 'trpc.msg.olpush.OlPushService.MsgPush', + seqId: 1, + retCode: 0, + fromClient: false, + body, + }; +} + +describe('parseMsgPush — private poke conversation identity (#285)', () => { + it('preserves the private peer independently of the poke sender and target', () => { + const events = [ + ...parseMsgPush(friendPokePacket(FRIEND_A, SELF_UIN, SELF_UIN), identity), + ...parseMsgPush(friendPokePacket(FRIEND_B, SELF_UIN, SELF_UIN), identity), + ...parseMsgPush(friendPokePacket(FRIEND_B, FRIEND_B, SELF_UIN), identity), + ]; + + expect(events.map((event) => { + const poke = event as unknown as Record; + return { + kind: poke.kind, + peerUin: poke.peerUin, + senderUin: poke.senderUin, + targetUin: poke.targetUin, + }; + })).toEqual([ + { kind: 'friend_poke', peerUin: FRIEND_A, senderUin: SELF_UIN, targetUin: SELF_UIN }, + { kind: 'friend_poke', peerUin: FRIEND_B, senderUin: SELF_UIN, targetUin: SELF_UIN }, + { kind: 'friend_poke', peerUin: FRIEND_B, senderUin: FRIEND_B, targetUin: SELF_UIN }, + ]); + }); +});