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
@@ -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 () => {
|
||||
|
||||
Reference in new issue
Block a user