feat(onebot): configure group invite policy

Add validated group member invite policy updates with post-write verification.

Fixes #334
This commit is contained in:
DrSmoothl committed 2026-08-09 11:46:04 +08:00
1 parent 597a4c05f3
commit cd6fcadea4
11 files changed
+343 -6

No files matched your search

@@ -19,6 +19,7 @@ import { FetchGroupDetail } from '../../../src/oidb-services/contacts/fetch-grou
interface GroupDetailResultsFixture {
name?: pb<15, string>;
shutUpAllTimestamp?: pb<59, uint_32>;
privilegeFlag?: pb<99, uint_32>;
}
interface GroupDetailInfoFixture {
@@ -55,6 +56,7 @@ describe('FetchGroupDetail namespace', () => {
expect(out.config?.flags?.maxMemberCount).toBe(true);
expect((out.config?.flags as { shutUpAllTimestamp?: boolean })?.shutUpAllTimestamp)
.toBe(true);
expect(out.config?.flags?.privilegeFlag).toBe(true);
});
});
@@ -111,5 +113,27 @@ describe('FetchGroupDetail namespace', () => {
expect(out.groupInfo?.results?.shutUpAllTimestamp).toBe(4_294_967_295);
});
it('decodes the complete privilege flag used by masked group mutations', async () => {
const responseData = Buffer.from(protobuf_encode<OidbBase<GroupDetailResponseFixture>>({
body: {
groupInfo: {
uin: 601692726n,
results: { privilegeFlag: 0x82100001 },
},
},
}));
const sender = makeSender();
sender.sendRawPacket.mockResolvedValue({
success: true,
gotResponse: true,
errorCode: 0,
errorMessage: '',
responseData,
});
const out = await FetchGroupDetail.invoke(sender, { groupUin: 601692726 });
expect(out.groupInfo?.results?.privilegeFlag).toBe(0x82100001);
});
});
});
@@ -0,0 +1,81 @@
import { describe, expect, it, vi } from 'vitest';
import { protobuf_decode } from '@snowluma/proton';
import type { SendPacketResult } from '@snowluma/common/packet-sender';
import type { OidbBase } from '@snowluma/proto-defs/oidb';
import type { Oidb0x89a_0InvitePolicy } from '@snowluma/proto-defs/oidb-actions/base';
import {
MEMBER_INVITE_PRIVILEGE_MASK,
mergeMemberInvitePrivilegeFlag,
SetMemberInvitePolicy,
type GroupMemberInvitePolicy,
} from '../../../src/oidb-services/group-admin/set-member-invite-policy';
function makeSender() {
const r: SendPacketResult = {
success: true,
gotResponse: true,
errorCode: 0,
errorMessage: '',
responseData: Buffer.alloc(0),
};
return { sendRawPacket: vi.fn(async () => r) };
}
describe('SetMemberInvitePolicy namespace', () => {
it.each<[GroupMemberInvitePolicy, number, number]>([
['disabled', 0x04000000, 0],
['require_approval', 0, 1],
['no_approval', 0x00100000, 1],
['no_approval_under_100', 0x02000000, 1],
])('maps %s to the verified privilege bits', async (policy, policyBits, allowMemberInvite) => {
const sender = makeSender();
const unrelatedBits = 0x80000001;
await SetMemberInvitePolicy.invoke(sender, {
groupId: 12345,
currentPrivilegeFlag: unrelatedBits + 0x06100000,
policy,
});
const [cmd, bytes] = sender.sendRawPacket.mock.calls[0]!;
expect(cmd).toBe('OidbSvcTrpcTcp.0x89a_0');
const env = protobuf_decode<OidbBase<Oidb0x89a_0InvitePolicy>>(bytes);
expect(env.command).toBe(0x89A);
expect(env.body?.groupUin).toBe(12345n);
expect(env.body?.settings).toEqual({
appPrivilegeFlag: unrelatedBits + policyBits,
appPrivilegeMask: MEMBER_INVITE_PRIVILEGE_MASK,
allowMemberInvite,
});
});
it('preserves unrelated bits while replacing only the member-invite mask', () => {
expect(mergeMemberInvitePrivilegeFlag(0xE7100001, 'require_approval'))
.toBe(0xE1000001);
});
it('keeps explicit zero fields on the wire', async () => {
const sender = makeSender();
await SetMemberInvitePolicy.invoke(sender, {
groupId: 1,
currentPrivilegeFlag: MEMBER_INVITE_PRIVILEGE_MASK,
policy: 'require_approval',
});
const env = protobuf_decode<OidbBase<Oidb0x89a_0InvitePolicy>>(
sender.sendRawPacket.mock.calls[0]![1],
);
expect(env.body?.settings?.appPrivilegeFlag).toBe(0);
expect(env.body?.settings?.allowMemberInvite).toBe(1);
});
it('rejects an invalid current flag before sending a packet', async () => {
const sender = makeSender();
await expect(SetMemberInvitePolicy.invoke(sender, {
groupId: 1,
currentPrivilegeFlag: -1,
policy: 'disabled',
})).rejects.toThrow(/unsigned 32-bit/);
expect(sender.sendRawPacket).not.toHaveBeenCalled();
});
});