fix(onebot): approve pending group join requests that were rejected as gone
Closes #380. An empty reject reason and a stale request type could make the server treat a still-pending join as already handled.
This commit is contained in:
11 files changed
+136
-45
No files matched your search
@@ -389,6 +389,9 @@ export class ContactsApi {
|
||||
eventType: operationType ?? 0,
|
||||
comment: raw.comment ?? '',
|
||||
filtered,
|
||||
operateTransInfo: raw.operateTransInfo && raw.operateTransInfo.length > 0
|
||||
? raw.operateTransInfo
|
||||
: undefined,
|
||||
});
|
||||
}
|
||||
this.ctx.identity.rememberGroupRequests(requests);
|
||||
@@ -445,6 +448,9 @@ export class ContactsApi {
|
||||
eventType: operationType ?? 0,
|
||||
comment: raw.comment ?? '',
|
||||
filtered,
|
||||
operateTransInfo: raw.operateTransInfo && raw.operateTransInfo.length > 0
|
||||
? raw.operateTransInfo
|
||||
: undefined,
|
||||
});
|
||||
}
|
||||
this.ctx.identity.rememberGroupRequests(requests);
|
||||
|
||||
@@ -165,8 +165,11 @@ export class GroupAdminApi {
|
||||
setAddRequest(
|
||||
groupId: number, sequence: number, eventType: number,
|
||||
approve: boolean, reason = '', filtered = false,
|
||||
operateTransInfo?: Uint8Array,
|
||||
): Promise<void> {
|
||||
return SetAddRequest.invoke(this.ctx, { groupId, sequence, eventType, approve, reason, filtered });
|
||||
return SetAddRequest.invoke(this.ctx, {
|
||||
groupId, sequence, eventType, approve, reason, filtered, operateTransInfo,
|
||||
});
|
||||
}
|
||||
|
||||
kickMember(groupId: number, userId: number, reject: boolean, reason = ''): Promise<void> {
|
||||
|
||||
@@ -84,10 +84,14 @@ export class Bridge implements BridgeInterface {
|
||||
...(main.status === 'fulfilled' ? main.value : []),
|
||||
...(filtered.status === 'fulfilled' ? filtered.value : []),
|
||||
];
|
||||
return requests.find(r => {
|
||||
const matches = requests.filter((r) => {
|
||||
if (r.groupId !== groupId) return false;
|
||||
return subType === 'invite' ? r.invitorUid === uid : r.targetUid === uid;
|
||||
}) ?? null;
|
||||
});
|
||||
if (subType === 'invite') {
|
||||
return matches.find((r) => r.notifyType === 1) ?? matches[0] ?? null;
|
||||
}
|
||||
return matches.find((r) => r.notifyType === 7) ?? matches[0] ?? null;
|
||||
},
|
||||
resolveGroupInviteCardSequence: async (groupId) => {
|
||||
const deadline = Date.now() + 1_000;
|
||||
|
||||
@@ -99,21 +99,48 @@ function throwMissingGroupRequest(message: string, failures: readonly unknown[])
|
||||
throw new Error(message);
|
||||
}
|
||||
|
||||
async function resolveLiveGroupRequest(
|
||||
bridge: BridgeInterface,
|
||||
handle: GroupRequestHandle,
|
||||
): Promise<GroupRequestHandle & { operateTransInfo?: Uint8Array }> {
|
||||
const cardGroup = bridge.apis.contacts.findGroupInviteCardGroupBySequence?.(handle.sequence);
|
||||
// Private invite-card seq is absent from 0x10C0; keep the flag tuple (#125).
|
||||
if (cardGroup === handle.groupId) return handle;
|
||||
|
||||
const lookup = await fetchRequestInboxes(bridge, 'uin');
|
||||
const live = lookup.requests.find((request) => (
|
||||
request.groupId === handle.groupId
|
||||
&& request.sequence === handle.sequence
|
||||
&& request.eventType > 0
|
||||
));
|
||||
if (!live) return handle;
|
||||
return {
|
||||
sequence: live.sequence,
|
||||
groupId: live.groupId,
|
||||
eventType: live.eventType,
|
||||
filtered: live.filtered,
|
||||
operateTransInfo: live.operateTransInfo,
|
||||
};
|
||||
}
|
||||
|
||||
async function applyGroupRequest(
|
||||
bridge: BridgeInterface,
|
||||
handle: GroupRequestHandle,
|
||||
approve: boolean,
|
||||
reason: string,
|
||||
knownLive?: GroupRequestInfo,
|
||||
): Promise<void> {
|
||||
const live = knownLive ?? await resolveLiveGroupRequest(bridge, handle);
|
||||
log.debug('handling group request: group=%d sequence=%d eventType=%d filtered=%s approve=%s',
|
||||
handle.groupId, handle.sequence, handle.eventType, handle.filtered, approve);
|
||||
live.groupId, live.sequence, live.eventType, live.filtered, approve);
|
||||
await bridge.apis.groupAdmin.setAddRequest(
|
||||
handle.groupId,
|
||||
handle.sequence,
|
||||
handle.eventType,
|
||||
live.groupId,
|
||||
live.sequence,
|
||||
live.eventType,
|
||||
approve,
|
||||
reason,
|
||||
handle.filtered,
|
||||
live.filtered,
|
||||
live.operateTransInfo,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -152,7 +179,7 @@ export async function handleGroupAddRequest(
|
||||
lookup.failures,
|
||||
);
|
||||
}
|
||||
await applyGroupRequest(bridge, matching, approve, reason);
|
||||
await applyGroupRequest(bridge, matching, approve, reason, matching);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -182,5 +209,5 @@ export async function handleGroupAddRequest(
|
||||
// Never fall back to an arbitrary request from the same group: when a UID
|
||||
// does not match, approving another pending request is worse than failing.
|
||||
if (!matching) throwMissingGroupRequest('matching group request not found', lookup.failures);
|
||||
await applyGroupRequest(bridge, matching, approve, reason);
|
||||
await applyGroupRequest(bridge, matching, approve, reason, matching);
|
||||
}
|
||||
@@ -627,8 +627,8 @@ describe('buildApiContext handleGroupRequest', () => {
|
||||
|
||||
await api.handleGroupRequest('slreq:1:123456:710410:22:1', 'add', true, 'welcome');
|
||||
|
||||
expect(setAddRequest).toHaveBeenCalledWith(710410, 123456, 22, true, 'welcome', true);
|
||||
expect(fetchGroupRequests).not.toHaveBeenCalled();
|
||||
expect(setAddRequest).toHaveBeenCalledWith(710410, 123456, 22, true, 'welcome', true, undefined);
|
||||
expect(fetchGroupRequests).toHaveBeenCalled();
|
||||
expect(ref.bridge.apis.groupAdmin.setAddRequest).toHaveBeenCalledOnce();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -52,7 +52,7 @@ function fakeRequest(overrides: Partial<GroupRequestInfo> = {}): GroupRequestInf
|
||||
}
|
||||
|
||||
describe('onebot/modules/request-actions / handleGroupAddRequest', () => {
|
||||
it('uses a canonical self-contained flag without refetching the request queue', async () => {
|
||||
it('uses a canonical flag and keeps the tuple when the inbox has no match', async () => {
|
||||
const setAddRequest = vi.fn(async () => {});
|
||||
const fetchGroupRequests = vi.fn(async () => []);
|
||||
const bridge = fakeBridge({
|
||||
@@ -62,8 +62,31 @@ describe('onebot/modules/request-actions / handleGroupAddRequest', () => {
|
||||
|
||||
await handleGroupAddRequest(bridge, 'slreq:1:123456:999:22:1', true, 'ok');
|
||||
|
||||
expect(fetchGroupRequests).not.toHaveBeenCalled();
|
||||
expect(setAddRequest).toHaveBeenCalledWith(999, 123456, 22, true, 'ok', true);
|
||||
expect(fetchGroupRequests).toHaveBeenCalled();
|
||||
expect(setAddRequest).toHaveBeenCalledWith(999, 123456, 22, true, 'ok', true, undefined);
|
||||
});
|
||||
|
||||
it('corrects a stale canonical eventType from the live inbox', async () => {
|
||||
const setAddRequest = vi.fn(async () => {});
|
||||
const trans = Uint8Array.from([0xaa]);
|
||||
const bridge = fakeBridge({
|
||||
findGroupInviteCardGroupBySequence: vi.fn(() => undefined),
|
||||
fetchGroupRequests: vi.fn(async (filtered: boolean) => filtered ? [] : [
|
||||
fakeRequest({
|
||||
groupId: 999,
|
||||
sequence: 123456,
|
||||
notifyType: 7,
|
||||
eventType: 1,
|
||||
filtered: false,
|
||||
operateTransInfo: trans,
|
||||
}),
|
||||
]),
|
||||
apis: { groupAdmin: { setAddRequest } } as any,
|
||||
});
|
||||
|
||||
await handleGroupAddRequest(bridge, 'slreq:1:123456:999:2:0', true, '');
|
||||
|
||||
expect(setAddRequest).toHaveBeenCalledWith(999, 123456, 1, true, '', false, trans);
|
||||
});
|
||||
|
||||
it('accepts a NapCat numeric sequence and resolves the exact main-inbox request', async () => {
|
||||
@@ -78,7 +101,7 @@ describe('onebot/modules/request-actions / handleGroupAddRequest', () => {
|
||||
|
||||
await handleGroupAddRequest(bridge, '261237407', false, 'no');
|
||||
|
||||
expect(setAddRequest).toHaveBeenCalledWith(999, 261237407, 7, false, 'no', false);
|
||||
expect(setAddRequest).toHaveBeenCalledWith(999, 261237407, 7, false, 'no', false, undefined);
|
||||
});
|
||||
|
||||
it('accepts a NapCat numeric sequence from the filtered inbox', async () => {
|
||||
@@ -93,7 +116,7 @@ describe('onebot/modules/request-actions / handleGroupAddRequest', () => {
|
||||
|
||||
await handleGroupAddRequest(bridge, '55', true, 'ok');
|
||||
|
||||
expect(setAddRequest).toHaveBeenCalledWith(999, 55, 2, true, 'ok', true);
|
||||
expect(setAddRequest).toHaveBeenCalledWith(999, 55, 2, true, 'ok', true, undefined);
|
||||
});
|
||||
|
||||
it('resolves a private invite-card msgseq by its cached group', async () => {
|
||||
@@ -106,7 +129,7 @@ describe('onebot/modules/request-actions / handleGroupAddRequest', () => {
|
||||
|
||||
await handleGroupAddRequest(bridge, '778899', true, 'ok');
|
||||
|
||||
expect(setAddRequest).toHaveBeenCalledWith(999, 778899, 2, true, 'ok', false);
|
||||
expect(setAddRequest).toHaveBeenCalledWith(999, 778899, 2, true, 'ok', false, undefined);
|
||||
});
|
||||
|
||||
it('matches add requests by groupId and targetUid', async () => {
|
||||
@@ -122,7 +145,7 @@ describe('onebot/modules/request-actions / handleGroupAddRequest', () => {
|
||||
await handleGroupAddRequest(bridge, 'add:999:u_t', true, 'ok');
|
||||
|
||||
expect(setAddRequest).toHaveBeenCalledOnce();
|
||||
expect(setAddRequest).toHaveBeenCalledWith(999, 42, 7, true, 'ok', false);
|
||||
expect(setAddRequest).toHaveBeenCalledWith(999, 42, 7, true, 'ok', false, undefined);
|
||||
expect(fetchGroupRequestsByUid).toHaveBeenCalledWith(false, 100);
|
||||
expect(fetchGroupRequestsByUid).toHaveBeenCalledWith(true, 100);
|
||||
});
|
||||
@@ -140,7 +163,7 @@ describe('onebot/modules/request-actions / handleGroupAddRequest', () => {
|
||||
await handleGroupAddRequest(bridge, 'invite:999:u_i', false, 'no');
|
||||
|
||||
expect(setAddRequest).toHaveBeenCalledOnce();
|
||||
expect(setAddRequest).toHaveBeenCalledWith(999, 97, 8, false, 'no', false);
|
||||
expect(setAddRequest).toHaveBeenCalledWith(999, 97, 8, false, 'no', false, undefined);
|
||||
});
|
||||
|
||||
it('finds a request that only lives in the filtered inbox (#197)', async () => {
|
||||
@@ -159,7 +182,7 @@ describe('onebot/modules/request-actions / handleGroupAddRequest', () => {
|
||||
|
||||
// Approved through the filtered inbox (subCommand 2 → last arg true).
|
||||
expect(setAddRequest).toHaveBeenCalledOnce();
|
||||
expect(setAddRequest).toHaveBeenCalledWith(999, 55, 2, true, 'ok', true);
|
||||
expect(setAddRequest).toHaveBeenCalledWith(999, 55, 2, true, 'ok', true, undefined);
|
||||
});
|
||||
|
||||
it('surfaces "not found" only when neither inbox has the request', async () => {
|
||||
@@ -214,7 +237,7 @@ describe('onebot/modules/request-actions / handleGroupAddRequest', () => {
|
||||
|
||||
await handleGroupAddRequest(bridge, '55', true, 'ok');
|
||||
|
||||
expect(setAddRequest).toHaveBeenCalledWith(999, 55, 2, true, 'ok', true);
|
||||
expect(setAddRequest).toHaveBeenCalledWith(999, 55, 2, true, 'ok', true, undefined);
|
||||
});
|
||||
|
||||
it('does not report not-found when an unavailable inbox may contain the request', async () => {
|
||||
|
||||
@@ -139,10 +139,13 @@ export interface OidbDeleteFriend {
|
||||
field1?: pb<1, OidbDeleteFriendField1>;
|
||||
}
|
||||
export interface OidbGroupRequestBody {
|
||||
sequence?: pb<1, uint_64>;
|
||||
eventType?: pb<2, uint_32>;
|
||||
groupUin?: pb<3, uint_32>;
|
||||
message?: pb<4, string>;
|
||||
sequence?: pb<1, uint_64>;
|
||||
eventType?: pb<2, uint_32>;
|
||||
groupUin?: pb<3, uint_32>;
|
||||
// Encoder always writes this field. An omitted empty string is not the
|
||||
// same as a present empty/space value; callers must set it.
|
||||
message?: pb_optional<4, string>;
|
||||
operateTransInfo?: pb<7, bytes>;
|
||||
}
|
||||
export interface OidbGroupRequestAction {
|
||||
accept?: pb<1, uint_32>;
|
||||
|
||||
@@ -208,9 +208,10 @@ export interface OidbSvcTrpcTcp0x10C0ResponseRequest {
|
||||
group?: pb<4, OidbSvcTrpcTcp0x10C0ResponseGroup>;
|
||||
target?: pb<5, OidbSvcTrpcTcp0x10C0ResponseUser>;
|
||||
invitor?: pb<6, OidbSvcTrpcTcp0x10C0ResponseUser>;
|
||||
operatorUser?: pb<7, OidbSvcTrpcTcp0x10C0ResponseUser>;
|
||||
field9?: pb<9, string>;
|
||||
comment?: pb<10, string>;
|
||||
operatorUser?: pb<7, OidbSvcTrpcTcp0x10C0ResponseUser>;
|
||||
field9?: pb<9, string>;
|
||||
comment?: pb<10, string>;
|
||||
operateTransInfo?: pb<14, bytes>;
|
||||
}
|
||||
|
||||
export interface OidbSvcTrpcTcp0x10C0Response {
|
||||
@@ -238,9 +239,10 @@ export interface OidbSvcTrpcTcp0x10C0ResponseRequestByUin {
|
||||
group?: pb<4, OidbSvcTrpcTcp0x10C0ResponseGroup>;
|
||||
target?: pb<5, OidbSvcTrpcTcp0x10C0ResponseUserByUin>;
|
||||
invitor?: pb<6, OidbSvcTrpcTcp0x10C0ResponseUserByUin>;
|
||||
operatorUser?: pb<7, OidbSvcTrpcTcp0x10C0ResponseUserByUin>;
|
||||
field9?: pb<9, string>;
|
||||
comment?: pb<10, string>;
|
||||
operatorUser?: pb<7, OidbSvcTrpcTcp0x10C0ResponseUserByUin>;
|
||||
field9?: pb<9, string>;
|
||||
comment?: pb<10, string>;
|
||||
operateTransInfo?: pb<14, bytes>;
|
||||
}
|
||||
|
||||
export interface OidbSvcTrpcTcp0x10C0ResponseByUin {
|
||||
|
||||
@@ -20,21 +20,30 @@ export namespace SetAddRequest {
|
||||
approve: boolean;
|
||||
reason?: string;
|
||||
filtered?: boolean;
|
||||
operateTransInfo?: Uint8Array;
|
||||
}
|
||||
|
||||
export type Deps = OidbSender;
|
||||
|
||||
export const resolveSubCommand = (p: Params): number => p.filtered ? 2 : 1;
|
||||
|
||||
export const serialize = (_ctx: Deps, p: Params): OidbGroupRequestAction => ({
|
||||
accept: p.approve ? 1 : 2,
|
||||
body: {
|
||||
sequence: BigInt(p.sequence),
|
||||
eventType: p.eventType,
|
||||
groupUin: p.groupId,
|
||||
message: p.reason ?? '',
|
||||
},
|
||||
});
|
||||
export const serialize = (_ctx: Deps, p: Params): OidbGroupRequestAction => {
|
||||
const reason = p.reason ?? '';
|
||||
return {
|
||||
accept: p.approve ? 1 : 2,
|
||||
body: {
|
||||
sequence: BigInt(p.sequence),
|
||||
eventType: p.eventType,
|
||||
groupUin: p.groupId,
|
||||
// Native operate always writes field 4. An omitted empty string is
|
||||
// rejected; a present space is the native empty-reason form.
|
||||
message: reason.length > 0 ? reason : ' ',
|
||||
...(p.operateTransInfo && p.operateTransInfo.length > 0
|
||||
? { operateTransInfo: p.operateTransInfo }
|
||||
: {}),
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
export const deserialize = (_ctx: Deps, _: OidbEmpty): void => {};
|
||||
|
||||
|
||||
@@ -81,6 +81,7 @@ export interface GroupRequestInfo {
|
||||
eventType: number;
|
||||
comment: string;
|
||||
filtered: boolean;
|
||||
operateTransInfo?: Uint8Array;
|
||||
}
|
||||
|
||||
/** Approval tuple required by OIDB 0x10C8. The OneBot flag is opaque to
|
||||
|
||||
@@ -41,12 +41,25 @@ describe('SetAddRequest namespace', () => {
|
||||
expect(env.body?.accept).toBe(2);
|
||||
});
|
||||
|
||||
it('defaults reason to empty string', async () => {
|
||||
it('writes a space when the reason is empty so field 4 stays present', async () => {
|
||||
const deps = makeDeps();
|
||||
await SetAddRequest.invoke(deps, { groupId: 1, sequence: 1, eventType: 0, approve: true });
|
||||
await SetAddRequest.invoke(deps, { groupId: 1, sequence: 1, eventType: 1, approve: true });
|
||||
const [, bytes] = deps.sendRawPacket.mock.calls[0]!;
|
||||
const env = protobuf_decode<OidbBase<OidbGroupRequestAction>>(bytes);
|
||||
// proto3 empty string omitted on the wire.
|
||||
expect(env.body?.body?.message ?? '').toBe('');
|
||||
expect(env.body?.body?.message).toBe(' ');
|
||||
const arr = Array.from(bytes);
|
||||
const hasMessageField = arr.some((_, i) => arr[i] === 0x22 && arr[i + 1] === 0x01 && arr[i + 2] === 0x20);
|
||||
expect(hasMessageField).toBe(true);
|
||||
});
|
||||
|
||||
it('forwards operateTransInfo as body field 7', async () => {
|
||||
const deps = makeDeps();
|
||||
const operateTransInfo = Uint8Array.from([0x01, 0x02, 0x03]);
|
||||
await SetAddRequest.invoke(deps, {
|
||||
groupId: 1, sequence: 1, eventType: 1, approve: true, reason: 'ok', operateTransInfo,
|
||||
});
|
||||
const [, bytes] = deps.sendRawPacket.mock.calls[0]!;
|
||||
const env = protobuf_decode<OidbBase<OidbGroupRequestAction>>(bytes);
|
||||
expect(env.body?.body?.operateTransInfo).toEqual(operateTransInfo);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user