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:
6 files changed
+105
-6
No files matched your search
@@ -71,10 +71,12 @@ function makeGroupIncreasePacket(
|
|||||||
operatorUid = '',
|
operatorUid = '',
|
||||||
fromUin = GROUP_ID,
|
fromUin = GROUP_ID,
|
||||||
operatorBytes: Uint8Array = Buffer.from(operatorUid, 'utf8'),
|
operatorBytes: Uint8Array = Buffer.from(operatorUid, 'utf8'),
|
||||||
|
operationType = 130,
|
||||||
): PacketInfo {
|
): PacketInfo {
|
||||||
const content = protobuf_encode<GroupChange>({
|
const content = protobuf_encode<GroupChange>({
|
||||||
groupUin: GROUP_ID,
|
groupUin: GROUP_ID,
|
||||||
memberUid,
|
memberUid,
|
||||||
|
decreaseType: operationType,
|
||||||
operatorBytes,
|
operatorBytes,
|
||||||
});
|
});
|
||||||
const body = protobuf_encode<PushMsg>({
|
const body = protobuf_encode<PushMsg>({
|
||||||
@@ -154,6 +156,36 @@ describe('parseMsgPush group member increase', () => {
|
|||||||
expect(event.operatorUid).toBe(operator.uid);
|
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', () => {
|
it('reports malformed raw operator bytes with bounded context', () => {
|
||||||
const captured: string[] = [];
|
const captured: string[] = [];
|
||||||
const unsubscribe = subscribeLogs((entry) => {
|
const unsubscribe = subscribeLogs((entry) => {
|
||||||
@@ -177,6 +209,23 @@ describe('parseMsgPush group member increase', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('parseMsgPush group member decrease', () => {
|
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', () => {
|
it('decodes a raw operator uid for ordinary member removals', () => {
|
||||||
const member = makeGroupMember(22222, 'u_member');
|
const member = makeGroupMember(22222, 'u_member');
|
||||||
const operator = makeGroupMember(33333, 'u_operator');
|
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 {
|
export function convertGroupMemberJoin(ctx: ConverterContext, event: GroupMemberJoin): JsonObject {
|
||||||
return notice(ctx, event, {
|
return notice(ctx, event, {
|
||||||
notice_type: 'group_increase',
|
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,
|
group_id: event.groupId,
|
||||||
operator_id: event.operatorUin,
|
operator_id: event.operatorUin,
|
||||||
user_id: event.userUin,
|
user_id: event.userUin,
|
||||||
|
|||||||
@@ -132,13 +132,31 @@ describe('convertEvent — message kinds', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('convertEvent — notice 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(), {
|
const approve = await convertEvent(bareCtx(), {
|
||||||
kind: 'group_member_join',
|
kind: 'group_member_join',
|
||||||
time: 1, selfUin: SELF_ID, groupId: GROUP_ID,
|
time: 1, selfUin: SELF_ID, groupId: GROUP_ID,
|
||||||
userUin: PEER_UIN, operatorUin: PEER_UIN,
|
userUin: PEER_UIN, operatorUin: PEER_UIN,
|
||||||
} as QQEventVariant);
|
} as QQEventVariant);
|
||||||
expect(approve!.notice_type).toBe('group_increase');
|
|
||||||
expect(approve!.sub_type).toBe('approve');
|
expect(approve!.sub_type).toBe('approve');
|
||||||
|
|
||||||
const invite = await convertEvent(bareCtx(), {
|
const invite = await convertEvent(bareCtx(), {
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ export interface GroupChange {
|
|||||||
groupUin?: pb<1, uint_32>;
|
groupUin?: pb<1, uint_32>;
|
||||||
flag?: pb<2, uint_32>;
|
flag?: pb<2, uint_32>;
|
||||||
memberUid?: pb<3, string>;
|
memberUid?: pb<3, string>;
|
||||||
|
/** Shared member-change operation code; used by both increase and decrease pushes. */
|
||||||
decreaseType?: pb<4, uint_32>;
|
decreaseType?: pb<4, uint_32>;
|
||||||
operatorBytes?: pb<5, bytes>;
|
operatorBytes?: pb<5, bytes>;
|
||||||
increaseType?: pb<6, uint_32>;
|
increaseType?: pb<6, uint_32>;
|
||||||
|
|||||||
@@ -270,6 +270,12 @@ export interface GroupMemberJoin extends QQEvent {
|
|||||||
operatorUin: number;
|
operatorUin: number;
|
||||||
userUid?: string;
|
userUid?: string;
|
||||||
operatorUid?: 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 {
|
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 { decodeNestedOperatorUid, decodeRawOperatorUid, resolveUidToUin } from '../helpers';
|
||||||
import type { MsgPushDecoder } from '../registry';
|
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) => {
|
export const decodeGroupMemberJoin: MsgPushDecoder = (ctx) => {
|
||||||
const change = protobuf_decode<GroupChange>(ctx.content);
|
const change = protobuf_decode<GroupChange>(ctx.content);
|
||||||
if (!change) return [];
|
if (!change) return [];
|
||||||
@@ -22,6 +30,7 @@ export const decodeGroupMemberJoin: MsgPushDecoder = (ctx) => {
|
|||||||
operatorUin: resolveUidToUin(ctx.identity, groupId, operatorUid, 0),
|
operatorUin: resolveUidToUin(ctx.identity, groupId, operatorUid, 0),
|
||||||
userUid,
|
userUid,
|
||||||
operatorUid,
|
operatorUid,
|
||||||
|
joinType: joinTypeFromOperationType(change.decreaseType ?? 0),
|
||||||
};
|
};
|
||||||
return [ev];
|
return [ev];
|
||||||
};
|
};
|
||||||
@@ -52,19 +61,32 @@ export const decodeGroupMemberLeave: MsgPushDecoder = (ctx) => {
|
|||||||
const dt = change.decreaseType ?? 0;
|
const dt = change.decreaseType ?? 0;
|
||||||
const groupId = change.groupUin ?? 0;
|
const groupId = change.groupUin ?? 0;
|
||||||
const userUid = change.memberUid ?? '';
|
const userUid = change.memberUid ?? '';
|
||||||
|
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);
|
const operatorBytes = change.operatorBytes ?? new Uint8Array(0);
|
||||||
// QQ sends the operator as a raw UTF-8 UID for normal member changes.
|
// 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.
|
// Only the bot-self-kicked variant (decreaseType=3) wraps it in OperatorInfo.
|
||||||
const operatorUid = dt === 3
|
operatorUid = dt === 3
|
||||||
? decodeNestedOperatorUid(operatorBytes, 'group member decrease type=3')
|
? decodeNestedOperatorUid(operatorBytes, 'group member decrease type=3')
|
||||||
: decodeRawOperatorUid(operatorBytes, `group member decrease type=${dt}`);
|
: decodeRawOperatorUid(operatorBytes, `group member decrease type=${dt}`);
|
||||||
|
operatorUin = resolveUidToUin(ctx.identity, groupId, operatorUid, 0);
|
||||||
|
}
|
||||||
const ev: GroupMemberLeave = {
|
const ev: GroupMemberLeave = {
|
||||||
kind: 'group_member_leave',
|
kind: 'group_member_leave',
|
||||||
time: ctx.head.timestamp,
|
time: ctx.head.timestamp,
|
||||||
selfUin: ctx.selfUin,
|
selfUin: ctx.selfUin,
|
||||||
groupId,
|
groupId,
|
||||||
userUin: resolveUidToUin(ctx.identity, groupId, userUid, 0),
|
userUin,
|
||||||
operatorUin: resolveUidToUin(ctx.identity, groupId, operatorUid, 0),
|
operatorUin,
|
||||||
userUid,
|
userUid,
|
||||||
operatorUid,
|
operatorUid,
|
||||||
leaveType: leaveTypeFromDecreaseType(dt),
|
leaveType: leaveTypeFromDecreaseType(dt),
|
||||||
|
|||||||
Reference in new issue
Block a user