feat(msg-push): handle three friend-related events we used to drop on the floor

Close three coverage gaps surfaced by the cross-Lagrange-family
audit (Lagrange V2 / LagrangeGo / acidify / lagrange-python):

  - **0x210 subType 179 + 226 (NewFriend)** — mutual-accept friend
    notice. 179 fires when bot sent a request and the other side
    accepted; 226 fires for the opposite direction. Both decode
    through the same wire shape. LagrangeGo handles the pair via a
    `case 226: case 179:` fallthrough in `client/listener.go:248`.
    We surface them as `kind: 'friend_add'`, which the existing
    converter routes straight to OneBot `notice.friend_add` — bot
    frameworks that wait for "auto-add a friend after they accept"
    were previously stuck because nothing arrived.
  - **0x210 subType 139 (self-recall)** — bot recalled its own
    message sent to a friend. Same `FriendRecall` wire shape as 138
    but the peer is on `toUid` instead of `fromUid` — acidify's
    `parseFriendRecall:380` splits exactly this way. Without 139,
    self-recalls disappeared and downstream `notice.friend_recall`
    handlers thought the bot had never recalled anything.
  - **PkgType 85 (bot self-joined a group)** — fires on the bot's
    session once `set_group_add_request` is approved or an invite is
    accepted, ported from lagrange-python's `server_push/msg.py:108`.
    PkgType 33 (GroupMemberIncreaseNotice) only fires for *other*
    members joining, so without 85 the bot never received its own
    `notice.group_increase` until the first message arrived. We
    decode `SelfJoinInGroup` and emit `kind: 'group_member_join'`
    with `userUin = selfUin`; the existing converter picks
    `sub_type: 'approve' | 'invite'` from the operator/user equality
    so consumers see the standard OneBot shape.

While wiring 139, also fix a *pre-existing* prod bug spotted via
proton call-site analysis on the resulting bundle: `event-0x210.ts`
imported `FriendRecall` from both `proto/proton/notify` (as
`FriendRecallNotify`) *and* `events` (as the event interface).
Proton's import-resolver registers `importedTypeSources` keyed on
the *imported* name (last-write-wins), so the events.ts import
shadowed the proto one — `protobuf_decode<FriendRecall>(...)` lost
its codec, fell through to the runtime fallback throw, and was
silently swallowed by `MsgPushRegistry.decode`'s catch. Verified by
the substituted codec list in `dist/index.mjs`: pre-fix it had
`FriendRequest` / `GeneralGrayTipInfo` but no `FriendRecall`;
post-fix `FriendRecall` / `NewFriend` / `SelfJoinInGroup` are all
emitted as expected. Side-step the collision by importing the
proto types unaliased and re-deriving the colliding event type via
`Extract<QQEventVariant, { kind: 'friend_recall' }>`.

New proto interfaces in `proto/proton/notify.ts`:
  - `NewFriend` + `NewFriendInfo` (uid/time/message/nickName fields,
    fields 1-9; ported from
    `LagrangeGo/.../notify.proto:25 NewFriend`)
  - `SelfJoinInGroup` (groupUin / operatorUid + a few placeholder
    fields; ported from
    `lagrange-python/pb/status/group.py:170 PBSelfJoinInGroup`)

Regression coverage in `tests/msg-push-handler.test.ts`:
  - subType 179/226 both produce identical friend_add events
  - falls back to `routingHead.fromUin` when the new friend's uid
    isn't in the local cache (matches LagrangeGo's
    "emit-then-resolve" pattern)
  - subType 138 keeps fromUid as the peer (other recalled their
    own msg to us); subType 139 picks toUid (we recalled ours
    sent to them)
  - PkgType 85 emits group_member_join with userUin == selfUin,
    operator resolved through identity when known

core: 635/635 tests pass (+7), typecheck clean. Production bundle
now contains substituted codecs for FriendRecall, NewFriend,
SelfJoinInGroup (cross-checked via `grep protobuf_decode_X` on
`dist/index.mjs`).
This commit is contained in:
DrSmoothl committed 2026-05-19 14:51:48 +08:00
1 parent 604dbc3f64
commit 898898a507
6 files changed
+361 -15

No files matched your search

@@ -1,15 +1,29 @@
// Handles Event0x210 wrapper (528). Internal switch on subType dispatches
// to FriendRequest (35) / FriendRecall (138) / FriendPoke (290).
// to FriendRequest (35) / FriendRecall (138, 139) / NewFriend
// (179, 226) / FriendPoke (290).
import { protobuf_decode } from '@snowluma/proton';
// Proto types imported unaliased — proton's import-resolver registers
// `importedTypeSources` keyed on the *imported* name. If we also did
// `import type { FriendRecall } from '../../events'` it would shadow
// the proto-side mapping (last-write-wins), point the resolver at the
// event-side file which has no `pb<>` schema, and the codec would
// never get generated — `protobuf_decode<FriendRecall>(...)` then
// falls through to the runtime fallback and throws (silently caught
// by `MsgPushRegistry.decode`). The event-side colliding names are
// re-derived from `QQEventVariant` below via `Extract` instead.
import type {
FriendRequest as FriendRequestNotify,
FriendRecall as FriendRecallNotify,
FriendRequest,
FriendRecall,
GeneralGrayTipInfo,
NewFriend,
} from '../../proto/proton/notify';
import type {
FriendRequestEvent, FriendRecall, FriendPokeEvent, QQEventVariant,
FriendRequestEvent, FriendPokeEvent,
FriendAddEvent, QQEventVariant,
} from '../../events';
type FriendRecallEvent = Extract<QQEventVariant, { kind: 'friend_recall' }>;
import type { MsgPushDecoder } from '../registry';
import type { MsgPushContext } from '../context';
import { Event0x210SubType } from '../enums';
@@ -23,8 +37,17 @@ const unknownLog = createLogger('MsgPush.Unknown');
export const decodeEvent0x210: MsgPushDecoder = (ctx) => {
switch (ctx.head.subType as Event0x210SubType) {
case Event0x210SubType.FriendRequestNotice: return decodeFriendRequest(ctx);
case Event0x210SubType.FriendRecallNotice: return decodeFriendRecall(ctx);
// 138 (other-recalled) and 139 (self-recalled) share the same
// FriendRecall wire shape; the helper picks the right uid side
// based on the subType.
case Event0x210SubType.FriendRecallNotice:
case Event0x210SubType.FriendRecallSelfNotice:
return decodeFriendRecall(ctx);
case Event0x210SubType.FriendPokeNotice: return decodeFriendPoke(ctx);
// 179 + 226 both carry the NewFriend payload — see enum comment.
case Event0x210SubType.NewFriendNotice:
case Event0x210SubType.NewFriendNoticeAlt:
return decodeNewFriend(ctx);
case Event0x210SubType.GroupAppStatePush:
// QQ-client-internal troop shortcut bar / discussion app state
// push — no OneBot-level event. See enum comment for the
@@ -36,7 +59,7 @@ export const decodeEvent0x210: MsgPushDecoder = (ctx) => {
};
function decodeFriendRequest(ctx: MsgPushContext): QQEventVariant[] {
const request = protobuf_decode<FriendRequestNotify>(ctx.content);
const request = protobuf_decode<FriendRequest>(ctx.content);
if (!request?.info) return [];
const sourceUid = request.info.newSource || request.info.sourceUid || '';
const ev: FriendRequestEvent = {
@@ -52,18 +75,43 @@ function decodeFriendRequest(ctx: MsgPushContext): QQEventVariant[] {
}
function decodeFriendRecall(ctx: MsgPushContext): QQEventVariant[] {
const recall = protobuf_decode<FriendRecallNotify>(ctx.content);
const recall = protobuf_decode<FriendRecall>(ctx.content);
if (!recall?.info) return [];
const ev: FriendRecall = {
// 138 = friend recalled their own message sent to bot (peer = fromUid).
// 139 = bot recalled own message sent to friend (peer = toUid). Same
// ambiguity acidify handles in `parseFriendRecall:380`. Without this
// split, self-recall events surface `userUin` as the bot itself which
// confuses downstream OneBot consumers that key off `user_id`.
const isSelfRecall = ctx.head.subType === Event0x210SubType.FriendRecallSelfNotice;
const peerUid = (isSelfRecall ? recall.info.toUid : recall.info.fromUid) ?? '';
const ev: FriendRecallEvent = {
kind: 'friend_recall',
time: recall.info.time ?? ctx.head.timestamp,
selfUin: ctx.selfUin,
userUin: resolveUidToUin(ctx.identity, 0, recall.info.fromUid ?? '', ctx.fromUin),
userUin: resolveUidToUin(ctx.identity, 0, peerUid, ctx.fromUin),
msgSeq: recall.info.clientSequence ?? 0,
};
return [ev];
}
function decodeNewFriend(ctx: MsgPushContext): QQEventVariant[] {
const nf = protobuf_decode<NewFriend>(ctx.content);
if (!nf?.info) return [];
const newFriendUid = nf.info.uid ?? '';
const newFriendUin = resolveUidToUin(ctx.identity, 0, newFriendUid, ctx.fromUin);
if (newFriendUin <= 0) return []; // can't surface a `user_id`-less friend_add to OneBot
const ev: FriendAddEvent = {
kind: 'friend_add',
// proto field is `fixed32 Time` — already uint32 seconds epoch.
// Fall back to head.timestamp if the field is missing (some
// captures have it as 0).
time: nf.info.time && nf.info.time > 0 ? nf.info.time : ctx.head.timestamp,
selfUin: ctx.selfUin,
userUin: newFriendUin,
};
return [ev];
}
function decodeFriendPoke(ctx: MsgPushContext): QQEventVariant[] {
const grayTip = protobuf_decode<GeneralGrayTipInfo>(ctx.content);
if (!grayTip || (grayTip.busiType ?? 0n) !== 12n) return [];
@@ -1,9 +1,21 @@
// Handles GroupMemberIncreaseNotice (33) + GroupMemberDecreaseNotice (34).
// Both share GroupChangeSchema; the decreaseType field distinguishes kick vs
// voluntary leave (dt != 0 && dt != 130 means kicked).
// Handles GroupMemberIncreaseNotice (33) + GroupMemberDecreaseNotice (34) +
// GroupSelfJoinedNotice (85, bot-self admittance).
//
// 33/34 share GroupChangeSchema; the decreaseType field distinguishes
// kick vs voluntary leave (dt != 0 && dt != 130 means kicked).
//
// 85 carries a different wire shape (`SelfJoinInGroup`) — without it
// the bot has no signal that its own `set_group_add_request` was
// approved (33 fires only for *other* members joining). We surface
// 85 as a regular GroupMemberJoin with `userUin = selfUin` so the
// existing OneBot converter naturally produces
// `notice.group_increase` with `sub_type: 'approve' | 'invite'`
// (the converter compares operator == user to pick the sub_type;
// for self-join the operator is the admin/inviter, so 'invite' is
// emitted — matching go-cqhttp / NapCat semantics).
import { protobuf_decode } from '@snowluma/proton';
import type { GroupChange } from '../../proto/proton/notify';
import type { GroupChange, SelfJoinInGroup } from '../../proto/proton/notify';
import type { GroupMemberJoin, GroupMemberLeave } from '../../events';
import type { MsgPushDecoder } from '../registry';
import { decodeOperatorUid, resolveUidToUin } from '../helpers';
@@ -47,3 +59,29 @@ export const decodeGroupMemberLeave: MsgPushDecoder = (ctx) => {
};
return [ev];
};
export const decodeGroupSelfJoined: MsgPushDecoder = (ctx) => {
const joined = protobuf_decode<SelfJoinInGroup>(ctx.content);
if (!joined) return [];
// groupUin is uint_64 on the wire (bigint). Real group IDs fit in
// uint32 (≤ 10 digits in practice), so Number() conversion is safe
// — saturate to 0 on the unlikely overflow rather than letting
// BigInt propagate downstream where consumers expect a number.
const groupUinBig = joined.groupUin ?? 0n;
const groupId = groupUinBig > 0n && groupUinBig <= 0x7FFFFFFFn ? Number(groupUinBig) : 0;
const operatorUid = joined.operatorUid ?? '';
// We're the one joining — surface the bot's own identity as userUin
// so the existing converter's `notice.group_increase` shape works
// unchanged.
const ev: GroupMemberJoin = {
kind: 'group_member_join',
time: ctx.head.timestamp,
selfUin: ctx.selfUin,
groupId,
userUin: ctx.selfUin,
userUid: ctx.identity.selfUid ?? '',
operatorUin: resolveUidToUin(ctx.identity, groupId, operatorUid, 0),
operatorUid,
};
return [ev];
};
@@ -13,6 +13,17 @@ export enum PkgType {
PrivateFileMessage = 529,
GroupRequestInvitationNotice = 525,
GroupRequestJoinNotice = 84,
/**
* Self-join admittance — fires on the bot's session right after an
* admin approves its `set_group_add_request` or an invite-accept
* goes through. Ported from `lagrange-python`'s
* `server_push/msg.py:108 case 85`. Maps to OneBot11
* `notice.group_increase` with the bot itself as `user_id` —
* existing 33 (GroupMemberIncreaseNotice) doesn't fire for the
* bot's own join, so without this case the bot never knows it
* just entered a group until the first message arrives.
*/
GroupSelfJoinedNotice = 85,
GroupInviteNotice = 87,
GroupAdminChangedNotice = 44,
GroupMemberIncreaseNotice = 33,
@@ -29,8 +40,29 @@ export enum Event0x2DCSubType {
export enum Event0x210SubType {
FriendRequestNotice = 35,
/**
* Outgoing friend-message recall — bot recalled its own message that
* was sent to a friend. Same `FriendRecall` wire shape as 138; the
* difference is direction (138 = friend recalled their own message
* sent to bot, 139 = bot recalled own message sent to friend).
* Cross-referenced against Lagrange V2 (`FriendRecallMessageProcessor`
* registered for both 138 and 139) + acidify (`138, 139 ->
* parseFriendRecall`). The decoder uses the subType to figure out
* which UID side ends up as `userUin` on the emitted event.
*/
FriendRecallSelfNotice = 139,
FriendRecallNotice = 138,
FriendPokeNotice = 290,
/**
* Mutual-accept friend notice — 179 fires when bot sent a friend
* request and the other side accepted; 226 fires for the opposite
* direction. Both decode through the same `NewFriend` wire shape
* (LagrangeGo registers them via a `case 226: case 179:` fallthrough
* in `client/listener.go:248-258`). Maps to OneBot11
* `notice.friend_add`.
*/
NewFriendNotice = 179,
NewFriendNoticeAlt = 226,
/**
* Group-app state push (troop shortcut bar / discussion app).
*
+2 -1
View File
@@ -9,7 +9,7 @@ import { buildContext } from './context';
import { MsgPushRegistry } from './registry';
import { PkgType } from './enums';
import {
decodeGroupMemberJoin, decodeGroupMemberLeave,
decodeGroupMemberJoin, decodeGroupMemberLeave, decodeGroupSelfJoined,
} from './decoders/group-member-change';
import { decodeGroupAdmin } from './decoders/group-admin';
import {
@@ -26,6 +26,7 @@ export const MSG_PUSH_CMD = 'trpc.msg.olpush.OlPushService.MsgPush';
const registry = new MsgPushRegistry();
registry.register(PkgType.GroupMemberIncreaseNotice, decodeGroupMemberJoin);
registry.register(PkgType.GroupMemberDecreaseNotice, decodeGroupMemberLeave);
registry.register(PkgType.GroupSelfJoinedNotice, decodeGroupSelfJoined);
registry.register(PkgType.GroupAdminChangedNotice, decodeGroupAdmin);
registry.register(PkgType.GroupRequestJoinNotice, decodeGroupJoinRequest);
registry.register(PkgType.GroupRequestInvitationNotice, decodeGroupInvitation);
@@ -122,6 +122,45 @@ export interface FriendRecall {
reserved?: pb<5, bytes>;
}
// ── NewFriend (0x210 subType 179 + 226) ───────────────────────────
//
// Mutual-accept friend notice. Fires when:
// - bot sent a friend request and the other side accepted (179), or
// - the other side sent a request and bot accepted it (226).
// Both subTypes share this wire shape; field semantics follow
// `LagrangeDev/LagrangeGo` client/packets/pb/message/notify.proto.
export interface NewFriendInfo {
uid?: pb<1, string>;
field2?: pb<2, uint_32>;
time?: pb<3, uint_32>; // fixed32 on wire; uint_32 decoded value is the unix epoch
message?: pb<4, string>;
nickName?: pb<5, string>;
field6?: pb<6, uint_32>;
field7?: pb<7, uint_32>;
toUid?: pb<9, string>;
}
export interface NewFriend {
field1?: pb<1, uint_32>;
info?: pb<2, NewFriendInfo>;
}
// ── SelfJoinInGroup (PkgType 85) ──────────────────────────────────
//
// Fired when the bot itself was admitted into a group — typically the
// completion of an admin-approved join request or an accepted invite.
// Ported from `lagrange-python/pb/status/group.py:170 PBSelfJoinInGroup`.
export interface SelfJoinInGroup {
groupUin?: pb<1, uint_64>;
field2?: pb<2, uint_32>;
operatorUid?: pb<3, string>;
field4?: pb<4, uint_32>;
field6?: pb<6, uint_32>;
field7?: pb<7, string>;
}
export interface GroupMuteState {
targetUid?: pb<1, string>;
duration?: pb<2, uint_32>;