fix(protocol): correct group membership notices

Derive group increase approval and invitation subtypes from the member-change operation code instead of actor identity.

Treat voluntary leave field 5 as structured metadata and attribute the operation to the departing member so valid notices are not discarded.

Add protocol and OneBot regression coverage for issue #237.
This commit is contained in:
DrSmoothl
2026-07-16 19:41:48 +08:00
parent 0503af2fcc
commit 0e3bbc6906
6 changed files with 110 additions and 11 deletions
@@ -71,10 +71,12 @@ function makeGroupIncreasePacket(
operatorUid = '',
fromUin = GROUP_ID,
operatorBytes: Uint8Array = Buffer.from(operatorUid, 'utf8'),
operationType = 130,
): PacketInfo {
const content = protobuf_encode<GroupChange>({
groupUin: GROUP_ID,
memberUid,
decreaseType: operationType,
operatorBytes,
});
const body = protobuf_encode<PushMsg>({
@@ -154,6 +156,36 @@ describe('parseMsgPush group member increase', () => {
expect(event.operatorUid).toBe(operator.uid);
});
it('derives approve vs invite from the member-change operation code (#237)', () => {
const member = makeGroupMember(22222, 'u_member');
const operator = makeGroupMember(33333, 'u_operator');
const identity = makeIdentity([member, operator]);
const [approved] = parseMsgPush(
makeGroupIncreasePacket(
member.uid,
operator.uid,
GROUP_ID,
Buffer.from(operator.uid, 'utf8'),
130,
),
identity,
) as GroupMemberJoin[];
const [invited] = parseMsgPush(
makeGroupIncreasePacket(
member.uid,
operator.uid,
GROUP_ID,
Buffer.from(operator.uid, 'utf8'),
131,
),
identity,
) as GroupMemberJoin[];
expect(approved.joinType).toBe('approve');
expect(invited.joinType).toBe('invite');
});
it('reports malformed raw operator bytes with bounded context', () => {
const captured: string[] = [];
const unsubscribe = subscribeLogs((entry) => {
@@ -177,6 +209,23 @@ describe('parseMsgPush group member increase', () => {
});
describe('parseMsgPush group member decrease', () => {
it('ignores structured field 5 metadata for a voluntary leave (#237)', () => {
const member = makeGroupMember(22222, 'u_member');
const [event] = parseMsgPush(
makeGroupDecreasePacket(
member.uid,
130,
new Uint8Array([0x12, 0x02, 0x08, 0x00]),
),
makeIdentity([member]),
) as Extract<QQEventVariant, { kind: 'group_member_leave' }>[];
expect(event.leaveType).toBe('leave');
expect(event.userUin).toBe(member.uin);
expect(event.operatorUin).toBe(member.uin);
expect(event.operatorUid).toBe(member.uid);
});
it('decodes a raw operator uid for ordinary member removals', () => {
const member = makeGroupMember(22222, 'u_member');
const operator = makeGroupMember(33333, 'u_operator');
@@ -27,7 +27,10 @@ type BotOffline = Extract<QQEventVariant, { kind: 'bot_offline' }>;
export function convertGroupMemberJoin(ctx: ConverterContext, event: GroupMemberJoin): JsonObject {
return notice(ctx, event, {
notice_type: 'group_increase',
sub_type: isSameActor(event.operatorUin, event.operatorUid, event.userUin, event.userUid) ? 'approve' : 'invite',
sub_type: event.joinType
?? (isSameActor(event.operatorUin, event.operatorUid, event.userUin, event.userUid)
? 'approve'
: 'invite'),
group_id: event.groupId,
operator_id: event.operatorUin,
user_id: event.userUin,
+20 -2
View File
@@ -132,13 +132,31 @@ describe('convertEvent — message kinds', () => {
});
describe('convertEvent — notice kinds', () => {
it('group_member_join: same-actor approve, else invite', async () => {
it('group_member_join: protocol admission type overrides actor identity (#237)', async () => {
const approve = await convertEvent(bareCtx(), {
kind: 'group_member_join',
time: 1, selfUin: SELF_ID, groupId: GROUP_ID,
userUin: PEER_UIN, operatorUin: 33333,
joinType: 'approve',
} as QQEventVariant);
expect(approve!.notice_type).toBe('group_increase');
expect(approve!.sub_type).toBe('approve');
const invite = await convertEvent(bareCtx(), {
kind: 'group_member_join',
time: 1, selfUin: SELF_ID, groupId: GROUP_ID,
userUin: PEER_UIN, operatorUin: PEER_UIN,
joinType: 'invite',
} as QQEventVariant);
expect(invite!.sub_type).toBe('invite');
});
it('group_member_join: falls back to actor identity when admission type is absent', async () => {
const approve = await convertEvent(bareCtx(), {
kind: 'group_member_join',
time: 1, selfUin: SELF_ID, groupId: GROUP_ID,
userUin: PEER_UIN, operatorUin: PEER_UIN,
} as QQEventVariant);
expect(approve!.notice_type).toBe('group_increase');
expect(approve!.sub_type).toBe('approve');
const invite = await convertEvent(bareCtx(), {
+1
View File
@@ -16,6 +16,7 @@ export interface GroupChange {
groupUin?: pb<1, uint_32>;
flag?: pb<2, uint_32>;
memberUid?: pb<3, string>;
/** Shared member-change operation code; used by both increase and decrease pushes. */
decreaseType?: pb<4, uint_32>;
operatorBytes?: pb<5, bytes>;
increaseType?: pb<6, uint_32>;
+6
View File
@@ -270,6 +270,12 @@ export interface GroupMemberJoin extends QQEvent {
operatorUin: number;
userUid?: string;
operatorUid?: string;
/**
* Admission mode derived from GroupChange field 4. This is deliberately
* separate from QQ's nested invitation-source enum, which has different
* wire semantics. It is absent on the separate self-join push path.
*/
joinType?: 'approve' | 'invite';
}
export interface GroupMemberLeave extends QQEvent {
@@ -4,6 +4,14 @@ import type { GroupChange, SelfJoinInGroup } from '@snowluma/proto-defs/notify';
import { decodeNestedOperatorUid, decodeRawOperatorUid, resolveUidToUin } from '../helpers';
import type { MsgPushDecoder } from '../registry';
function joinTypeFromOperationType(raw: number): NonNullable<GroupMemberJoin['joinType']> {
// QQ sets the high bit on the PkgType=33 operation code. After normalizing
// it, code 3 means an invitation; code 2 (and unknown/absent values) means
// approval. Do not use the nested `join_type`: that is an invitation-source
// enum rather than OneBot's approve/invite distinction.
return (raw & 0x7f) === 3 ? 'invite' : 'approve';
}
export const decodeGroupMemberJoin: MsgPushDecoder = (ctx) => {
const change = protobuf_decode<GroupChange>(ctx.content);
if (!change) return [];
@@ -22,6 +30,7 @@ export const decodeGroupMemberJoin: MsgPushDecoder = (ctx) => {
operatorUin: resolveUidToUin(ctx.identity, groupId, operatorUid, 0),
userUid,
operatorUid,
joinType: joinTypeFromOperationType(change.decreaseType ?? 0),
};
return [ev];
};
@@ -52,19 +61,32 @@ export const decodeGroupMemberLeave: MsgPushDecoder = (ctx) => {
const dt = change.decreaseType ?? 0;
const groupId = change.groupUin ?? 0;
const userUid = change.memberUid ?? '';
const operatorBytes = change.operatorBytes ?? new Uint8Array(0);
// QQ sends the operator as a raw UTF-8 UID for normal member changes.
// Only the bot-self-kicked variant (decreaseType=3) wraps it in OperatorInfo.
const operatorUid = dt === 3
? decodeNestedOperatorUid(operatorBytes, 'group member decrease type=3')
: decodeRawOperatorUid(operatorBytes, `group member decrease type=${dt}`);
const userUin = resolveUidToUin(ctx.identity, groupId, userUid, 0);
let operatorUid: string;
let operatorUin: number;
if (dt === 130) {
// A voluntary leave has no independent operator. QQ may place structured
// metadata in field 5, so it must not be decoded as a UID. OneBot models
// the leaving member as the operator for this event.
operatorUid = userUid;
operatorUin = userUin;
} else {
const operatorBytes = change.operatorBytes ?? new Uint8Array(0);
// QQ sends the operator as a raw UTF-8 UID for normal member changes.
// Only the bot-self-kicked variant (decreaseType=3) wraps it in OperatorInfo.
operatorUid = dt === 3
? decodeNestedOperatorUid(operatorBytes, 'group member decrease type=3')
: decodeRawOperatorUid(operatorBytes, `group member decrease type=${dt}`);
operatorUin = resolveUidToUin(ctx.identity, groupId, operatorUid, 0);
}
const ev: GroupMemberLeave = {
kind: 'group_member_leave',
time: ctx.head.timestamp,
selfUin: ctx.selfUin,
groupId,
userUin: resolveUidToUin(ctx.identity, groupId, userUid, 0),
operatorUin: resolveUidToUin(ctx.identity, groupId, operatorUid, 0),
userUin,
operatorUin,
userUid,
operatorUid,
leaveType: leaveTypeFromDecreaseType(dt),