fix(protocol): decode nested group operators
Recognize both raw UTF-8 UIDs and nested OperatorInfo envelopes in GroupChange pushes while keeping self-kick and voluntary-leave semantics explicit.\n\nFixes #253.
This commit is contained in:
3 files changed
+56
-5
No files matched your search
@@ -156,6 +156,24 @@ describe('parseMsgPush group member increase', () => {
|
|||||||
expect(event.operatorUid).toBe(operator.uid);
|
expect(event.operatorUid).toBe(operator.uid);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('decodes nested operator info used by member-increase pushes', () => {
|
||||||
|
const member = makeGroupMember(22222, 'u_member');
|
||||||
|
const operator = makeGroupMember(33333, 'u_operator');
|
||||||
|
const operatorBytes = Buffer.from(
|
||||||
|
'0a180a0a755f6f70657261746f7210061a060102030405062001',
|
||||||
|
'hex',
|
||||||
|
);
|
||||||
|
const [event] = parseMsgPush(
|
||||||
|
makeGroupIncreasePacket(member.uid, '', GROUP_ID, operatorBytes),
|
||||||
|
makeIdentity([member, operator]),
|
||||||
|
) as GroupMemberJoin[];
|
||||||
|
|
||||||
|
expect(event.userUin).toBe(member.uin);
|
||||||
|
expect(event.operatorUin).toBe(operator.uin);
|
||||||
|
expect(event.userUid).toBe(member.uid);
|
||||||
|
expect(event.operatorUid).toBe(operator.uid);
|
||||||
|
});
|
||||||
|
|
||||||
it('derives approve vs invite from the member-change operation code (#237)', () => {
|
it('derives approve vs invite from the member-change operation code (#237)', () => {
|
||||||
const member = makeGroupMember(22222, 'u_member');
|
const member = makeGroupMember(22222, 'u_member');
|
||||||
const operator = makeGroupMember(33333, 'u_operator');
|
const operator = makeGroupMember(33333, 'u_operator');
|
||||||
@@ -251,6 +269,26 @@ describe('parseMsgPush group member decrease', () => {
|
|||||||
expect(event.leaveType).toBe('kick');
|
expect(event.leaveType).toBe('kick');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('decodes nested operator info for ordinary member removals (#253)', () => {
|
||||||
|
const member = makeGroupMember(22222, 'u_member');
|
||||||
|
const operator = makeGroupMember(33333, 'u_operator');
|
||||||
|
// GroupChange.operatorBytes is polymorphic on the wire. QQ may send an
|
||||||
|
// OperatorInfo envelope for type=131, including metadata after the UID.
|
||||||
|
const operatorBytes = Buffer.from(
|
||||||
|
'0a180a0a755f6f70657261746f7210061a060102030405062001',
|
||||||
|
'hex',
|
||||||
|
);
|
||||||
|
const [event] = parseMsgPush(
|
||||||
|
makeGroupDecreasePacket(member.uid, 131, operatorBytes),
|
||||||
|
makeIdentity([member, operator]),
|
||||||
|
) as Extract<QQEventVariant, { kind: 'group_member_leave' }>[];
|
||||||
|
|
||||||
|
expect(event.userUin).toBe(member.uin);
|
||||||
|
expect(event.operatorUin).toBe(operator.uin);
|
||||||
|
expect(event.operatorUid).toBe(operator.uid);
|
||||||
|
expect(event.leaveType).toBe('kick');
|
||||||
|
});
|
||||||
|
|
||||||
it('decodes nested operator info when the bot itself is kicked', () => {
|
it('decodes nested operator info when the bot itself is kicked', () => {
|
||||||
const operator = makeGroupMember(33333, 'u_operator');
|
const operator = makeGroupMember(33333, 'u_operator');
|
||||||
const operatorBytes = protobuf_encode<OperatorInfo>({
|
const operatorBytes = protobuf_encode<OperatorInfo>({
|
||||||
|
|||||||
@@ -1,7 +1,11 @@
|
|||||||
import { protobuf_decode } from '@snowluma/proton';
|
import { protobuf_decode } from '@snowluma/proton';
|
||||||
import type { GroupMemberJoin, GroupMemberLeave } from '../../events';
|
import type { GroupMemberJoin, GroupMemberLeave } from '../../events';
|
||||||
import type { GroupChange, SelfJoinInGroup } from '@snowluma/proto-defs/notify';
|
import type { GroupChange, SelfJoinInGroup } from '@snowluma/proto-defs/notify';
|
||||||
import { decodeNestedOperatorUid, decodeRawOperatorUid, resolveUidToUin } from '../helpers';
|
import {
|
||||||
|
decodeGroupChangeOperatorUid,
|
||||||
|
decodeNestedOperatorUid,
|
||||||
|
resolveUidToUin,
|
||||||
|
} from '../helpers';
|
||||||
import type { MsgPushDecoder } from '../registry';
|
import type { MsgPushDecoder } from '../registry';
|
||||||
|
|
||||||
function joinTypeFromOperationType(raw: number): NonNullable<GroupMemberJoin['joinType']> {
|
function joinTypeFromOperationType(raw: number): NonNullable<GroupMemberJoin['joinType']> {
|
||||||
@@ -18,7 +22,7 @@ export const decodeGroupMemberJoin: MsgPushDecoder = (ctx) => {
|
|||||||
if (!change) return [];
|
if (!change) return [];
|
||||||
const groupId = change.groupUin ?? 0;
|
const groupId = change.groupUin ?? 0;
|
||||||
const userUid = change.memberUid ?? '';
|
const userUid = change.memberUid ?? '';
|
||||||
const operatorUid = decodeRawOperatorUid(
|
const operatorUid = decodeGroupChangeOperatorUid(
|
||||||
change.operatorBytes ?? new Uint8Array(0),
|
change.operatorBytes ?? new Uint8Array(0),
|
||||||
'group member increase',
|
'group member increase',
|
||||||
);
|
);
|
||||||
@@ -74,11 +78,11 @@ export const decodeGroupMemberLeave: MsgPushDecoder = (ctx) => {
|
|||||||
operatorUin = userUin;
|
operatorUin = userUin;
|
||||||
} else {
|
} 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.
|
// Self-kick always uses OperatorInfo. Ordinary removals vary between a raw
|
||||||
// Only the bot-self-kicked variant (decreaseType=3) wraps it in OperatorInfo.
|
// UTF-8 UID and the same envelope, so discriminate by its field-1 wire tag.
|
||||||
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}`);
|
: decodeGroupChangeOperatorUid(operatorBytes, `group member decrease type=${dt}`);
|
||||||
operatorUin = resolveUidToUin(ctx.identity, groupId, operatorUid, 0);
|
operatorUin = resolveUidToUin(ctx.identity, groupId, operatorUid, 0);
|
||||||
}
|
}
|
||||||
const ev: GroupMemberLeave = {
|
const ev: GroupMemberLeave = {
|
||||||
|
|||||||
@@ -139,6 +139,15 @@ export function decodeNestedOperatorUid(bytes: Uint8Array, context: string): str
|
|||||||
return assertOperatorUid(uid, context, bytes);
|
return assertOperatorUid(uid, context, bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const OPERATOR_INFO_FIELD_TAG = 0x0a;
|
||||||
|
|
||||||
|
export function decodeGroupChangeOperatorUid(bytes: Uint8Array, context: string): string {
|
||||||
|
if (!bytes || bytes.length === 0) return '';
|
||||||
|
return bytes[0] === OPERATOR_INFO_FIELD_TAG
|
||||||
|
? decodeNestedOperatorUid(bytes, context)
|
||||||
|
: decodeRawOperatorUid(bytes, context);
|
||||||
|
}
|
||||||
|
|
||||||
export function buildTemplateMap(params: Array<{ name?: string; value?: string }>): Map<string, string> {
|
export function buildTemplateMap(params: Array<{ name?: string; value?: string }>): Map<string, string> {
|
||||||
const map = new Map<string, string>();
|
const map = new Map<string, string>();
|
||||||
for (const p of params) {
|
for (const p of params) {
|
||||||
|
|||||||
Reference in new issue
Block a user