fix(onebot): use canonical group request flags
This commit is contained in:
11 files changed
+468
-60
No files matched your search
@@ -20,6 +20,7 @@ export const actions = [
|
||||
name: 'set_group_add_request',
|
||||
summary: '处理加群请求',
|
||||
params: {
|
||||
// Opaque handle from the original request event; callers must echo it.
|
||||
flag: f.string({ allowEmpty: false }),
|
||||
// sub_type/type pass through with a nested fallback (sub_type → type → 'add');
|
||||
// kept raw so the original asString resolution is preserved verbatim.
|
||||
@@ -35,4 +36,3 @@ export const actions = [
|
||||
},
|
||||
}),
|
||||
];
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import type { BridgeInterface } from '@snowluma/core/bridge-interface';
|
||||
import type { IdentityService } from '@snowluma/protocol/identity-service';
|
||||
import { formatGroupRequestFlag } from '@snowluma/protocol/qq-info';
|
||||
import type { OneBotInstanceContext } from '../instance-context';
|
||||
import type { JsonObject } from '../types';
|
||||
|
||||
@@ -239,7 +240,7 @@ export async function getGroupSystemMessages(bridge: BridgeInterface): Promise<J
|
||||
requester_uin: r.targetUin,
|
||||
requester_nick: r.targetName,
|
||||
message: r.comment,
|
||||
flag: `${r.eventType}:${r.groupId}:${r.targetUid}`,
|
||||
flag: formatGroupRequestFlag(r),
|
||||
}));
|
||||
} catch {
|
||||
return [];
|
||||
|
||||
@@ -1,4 +1,96 @@
|
||||
import { createLogger } from '@snowluma/common/logger';
|
||||
import type { BridgeInterface } from '@snowluma/core/bridge-interface';
|
||||
import type { GroupRequestHandle, GroupRequestInfo } from '@snowluma/protocol/qq-info';
|
||||
|
||||
const log = createLogger('OneBot.Request');
|
||||
|
||||
type ParsedGroupRequestFlag =
|
||||
| ({ kind: 'canonical' } & GroupRequestHandle)
|
||||
| { kind: 'sequence'; sequence: number }
|
||||
| { kind: 'legacy'; requestType: 'add' | 'invite'; groupId: number; targetUid: string };
|
||||
|
||||
function positiveSafeInteger(value: string, field: string): number {
|
||||
if (!/^\d+$/.test(value)) throw new Error(`invalid ${field} in group request flag`);
|
||||
const parsed = Number(value);
|
||||
if (!Number.isSafeInteger(parsed) || parsed <= 0) {
|
||||
throw new Error(`invalid ${field} in group request flag`);
|
||||
}
|
||||
return parsed;
|
||||
}
|
||||
|
||||
export function parseGroupRequestFlag(flag: string): ParsedGroupRequestFlag {
|
||||
if (/^\d+$/.test(flag)) {
|
||||
return { kind: 'sequence', sequence: positiveSafeInteger(flag, 'sequence') };
|
||||
}
|
||||
|
||||
const parts = flag.split(':');
|
||||
if (parts[0] === 'slreq') {
|
||||
if (parts.length !== 6 || parts[1] !== '1') {
|
||||
throw new Error('unsupported canonical group request flag');
|
||||
}
|
||||
if (parts[5] !== '0' && parts[5] !== '1') {
|
||||
throw new Error('invalid filtered value in group request flag');
|
||||
}
|
||||
return {
|
||||
kind: 'canonical',
|
||||
sequence: positiveSafeInteger(parts[2], 'sequence'),
|
||||
groupId: positiveSafeInteger(parts[3], 'group_id'),
|
||||
eventType: positiveSafeInteger(parts[4], 'event_type'),
|
||||
filtered: parts[5] === '1',
|
||||
};
|
||||
}
|
||||
|
||||
const requestType = parts[0];
|
||||
if ((requestType !== 'add' && requestType !== 'invite') || parts.length < 3) {
|
||||
throw new Error('invalid group request flag');
|
||||
}
|
||||
const groupId = positiveSafeInteger(parts[1], 'group_id');
|
||||
const targetUid = parts.slice(2).join(':');
|
||||
if (!targetUid) throw new Error('invalid request target in flag');
|
||||
return { kind: 'legacy', requestType, groupId, targetUid };
|
||||
}
|
||||
|
||||
async function fetchRequestInboxes(bridge: BridgeInterface): Promise<GroupRequestInfo[]> {
|
||||
const [main, filtered] = await Promise.allSettled([
|
||||
bridge.apis.contacts.fetchGroupRequests(false),
|
||||
bridge.apis.contacts.fetchGroupRequests(true),
|
||||
]);
|
||||
|
||||
if (main.status === 'rejected') {
|
||||
log.warn('failed to fetch main group-request inbox: %s',
|
||||
main.reason instanceof Error ? main.reason.message : String(main.reason));
|
||||
}
|
||||
if (filtered.status === 'rejected') {
|
||||
log.warn('failed to fetch filtered group-request inbox: %s',
|
||||
filtered.reason instanceof Error ? filtered.reason.message : String(filtered.reason));
|
||||
}
|
||||
if (main.status === 'rejected' && filtered.status === 'rejected') {
|
||||
throw new Error('failed to fetch group requests from both inboxes');
|
||||
}
|
||||
|
||||
return [
|
||||
...(main.status === 'fulfilled' ? main.value : []),
|
||||
...(filtered.status === 'fulfilled' ? filtered.value : []),
|
||||
];
|
||||
}
|
||||
|
||||
async function applyGroupRequest(
|
||||
bridge: BridgeInterface,
|
||||
handle: GroupRequestHandle,
|
||||
approve: boolean,
|
||||
reason: string,
|
||||
): Promise<void> {
|
||||
log.debug('handling group request: group=%d sequence=%d eventType=%d filtered=%s approve=%s',
|
||||
handle.groupId, handle.sequence, handle.eventType, handle.filtered, approve);
|
||||
await bridge.apis.groupAdmin.setAddRequest(
|
||||
handle.groupId,
|
||||
handle.sequence,
|
||||
handle.eventType,
|
||||
approve,
|
||||
reason,
|
||||
handle.filtered,
|
||||
);
|
||||
}
|
||||
|
||||
export async function handleGroupAddRequest(
|
||||
bridge: BridgeInterface,
|
||||
@@ -6,55 +98,58 @@ export async function handleGroupAddRequest(
|
||||
approve: boolean,
|
||||
reason: string,
|
||||
): Promise<void> {
|
||||
// flag format: "add:groupId:uid" or "invite:groupId:uid"
|
||||
const parts = flag.split(':');
|
||||
if (parts.length < 3) throw new Error('invalid group request flag');
|
||||
const requestType = parts[0];
|
||||
const groupId = parseInt(parts[1], 10);
|
||||
const targetUid = parts.slice(2).join(':');
|
||||
if (!groupId) throw new Error('invalid group_id in flag');
|
||||
if (!targetUid) throw new Error('invalid request target in flag');
|
||||
const parsed = parseGroupRequestFlag(flag);
|
||||
|
||||
// Bot self-invited via a private "qun.invite" card: the only sequence the
|
||||
// server accepts at 0x10c8_1 is that card's jumpUrl msgseq, with eventType=2
|
||||
// / filtered=false. The fetchGroupRequests tuple below fails this case with
|
||||
// 120161001 ("handle async message fail"). See issue #125.
|
||||
if (requestType === 'invite') {
|
||||
const cardSequence = bridge.apis.contacts.getGroupInviteCardSequence(groupId);
|
||||
if (parsed.kind === 'canonical') {
|
||||
await applyGroupRequest(bridge, parsed, approve, reason);
|
||||
return;
|
||||
}
|
||||
|
||||
if (parsed.kind === 'sequence') {
|
||||
// Private "qun.invite" cards use their Ark msgseq and never appear under
|
||||
// that sequence in 0x10C0. Resolve the reverse card cache before inboxes.
|
||||
const cardGroupId = bridge.apis.contacts.findGroupInviteCardGroupBySequence(parsed.sequence);
|
||||
if (cardGroupId) {
|
||||
await applyGroupRequest(bridge, {
|
||||
groupId: cardGroupId,
|
||||
sequence: parsed.sequence,
|
||||
eventType: 2,
|
||||
filtered: false,
|
||||
}, approve, reason);
|
||||
return;
|
||||
}
|
||||
|
||||
const matching = (await fetchRequestInboxes(bridge))
|
||||
.find((request) => request.sequence === parsed.sequence);
|
||||
if (!matching) throw new Error(`group request sequence ${parsed.sequence} not found`);
|
||||
await applyGroupRequest(bridge, matching, approve, reason);
|
||||
return;
|
||||
}
|
||||
|
||||
// Legacy SnowLuma flags remain accepted for events emitted by older builds.
|
||||
// A private invite card needs its msgseq rather than the 0x10C0 tuple (#125).
|
||||
if (parsed.requestType === 'invite') {
|
||||
const cardSequence = bridge.apis.contacts.getGroupInviteCardSequence(parsed.groupId);
|
||||
if (cardSequence) {
|
||||
await bridge.apis.groupAdmin.setAddRequest(groupId, cardSequence, 2, approve, reason, false);
|
||||
await applyGroupRequest(bridge, {
|
||||
groupId: parsed.groupId,
|
||||
sequence: cardSequence,
|
||||
eventType: 2,
|
||||
filtered: false,
|
||||
}, approve, reason);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// A join/invite request lands in either the main inbox (0x10c8 subCommand 1)
|
||||
// or the spam-filtered one (subCommand 2). The old code only fetched the main
|
||||
// inbox, so a filtered request that SnowLuma had already reported to the
|
||||
// client couldn't be approved through its own flag — "matching group request
|
||||
// not found" (#197). Search both; each request keeps the `filtered` flag its
|
||||
// inbox needs for the approval subCommand. One inbox failing degrades to the
|
||||
// other rather than aborting.
|
||||
const [mainInbox, filteredInbox] = await Promise.all([
|
||||
bridge.apis.contacts.fetchGroupRequests(false).catch(() => []),
|
||||
bridge.apis.contacts.fetchGroupRequests(true).catch(() => []),
|
||||
]);
|
||||
const requests = [...mainInbox, ...filteredInbox];
|
||||
const matching = requests.find((r) => {
|
||||
if (r.groupId !== groupId) return false;
|
||||
if (requestType === 'add') return r.targetUid === targetUid;
|
||||
if (requestType === 'invite') return r.invitorUid === targetUid;
|
||||
return false;
|
||||
}) || requests.find(r => r.groupId === groupId);
|
||||
const matching = (await fetchRequestInboxes(bridge)).find((request) => {
|
||||
if (request.groupId !== parsed.groupId) return false;
|
||||
return parsed.requestType === 'add'
|
||||
? request.targetUid === parsed.targetUid
|
||||
: request.invitorUid === parsed.targetUid;
|
||||
});
|
||||
|
||||
if (!matching) {
|
||||
throw new Error('matching group request not found');
|
||||
}
|
||||
await bridge.apis.groupAdmin.setAddRequest(
|
||||
groupId,
|
||||
matching.sequence,
|
||||
matching.eventType,
|
||||
approve,
|
||||
reason,
|
||||
matching.filtered,
|
||||
);
|
||||
// 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) throw new Error('matching group request not found');
|
||||
await applyGroupRequest(bridge, matching, approve, reason);
|
||||
}
|
||||
@@ -6,7 +6,7 @@
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
import type { BridgeInterface } from '../../src/bridge/bridge-interface';
|
||||
import type {
|
||||
FriendInfo, GroupMemberInfo, QQGroupInfo, UserProfileInfo,
|
||||
FriendInfo, GroupMemberInfo, GroupRequestInfo, QQGroupInfo, UserProfileInfo,
|
||||
} from '@snowluma/protocol/qq-info';
|
||||
import {
|
||||
getFriendList,
|
||||
@@ -14,6 +14,7 @@ import {
|
||||
getGroupList,
|
||||
getGroupMemberInfo,
|
||||
getGroupMemberList,
|
||||
getGroupSystemMessages,
|
||||
getLoginInfo,
|
||||
getStrangerInfo,
|
||||
} from '../src/modules/contact-actions';
|
||||
@@ -93,6 +94,28 @@ function makeProfile(uin: number, nickname: string, sex: 'male' | 'female' | 'un
|
||||
};
|
||||
}
|
||||
|
||||
function makeGroupRequest(overrides: Partial<GroupRequestInfo> = {}): GroupRequestInfo {
|
||||
return {
|
||||
groupId: 999,
|
||||
groupName: 'group',
|
||||
targetUid: 'u_target',
|
||||
targetUin: 123,
|
||||
targetName: 'target',
|
||||
invitorUid: 'u_inviter',
|
||||
invitorUin: 456,
|
||||
invitorName: 'inviter',
|
||||
operatorUid: 'u_operator',
|
||||
operatorUin: 789,
|
||||
operatorName: 'operator',
|
||||
sequence: 123456,
|
||||
state: 1,
|
||||
eventType: 22,
|
||||
comment: 'please',
|
||||
filtered: false,
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
// ─── Tests ───
|
||||
|
||||
describe('onebot/contact-actions / getLoginInfo', () => {
|
||||
@@ -355,3 +378,22 @@ describe('onebot/contact-actions / getStrangerInfo', () => {
|
||||
expect(await getStrangerInfo(bridge, 99999)).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('onebot/contact-actions / getGroupSystemMessages', () => {
|
||||
it('returns the same canonical flag accepted by set_group_add_request', async () => {
|
||||
const bridge = fakeBridge({
|
||||
fetchGroupRequests: vi.fn(async () => [
|
||||
makeGroupRequest({ sequence: 123456, groupId: 999, eventType: 22, filtered: true }),
|
||||
]),
|
||||
});
|
||||
|
||||
const result = await getGroupSystemMessages(bridge);
|
||||
|
||||
expect(result).toHaveLength(1);
|
||||
expect(result[0]).toMatchObject({
|
||||
request_id: 123456,
|
||||
group_id: 999,
|
||||
flag: 'slreq:1:123456:999:22:1',
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -8,7 +8,7 @@ const APIS_ROUTING: Record<string, string> = {
|
||||
fetchFriendList: 'contacts', fetchGroupList: 'contacts',
|
||||
fetchGroupMemberList: 'contacts', fetchUserProfile: 'contacts',
|
||||
fetchGroupRequests: 'contacts', fetchDownloadRKeys: 'contacts',
|
||||
getGroupInviteCardSequence: 'contacts',
|
||||
getGroupInviteCardSequence: 'contacts', findGroupInviteCardGroupBySequence: 'contacts',
|
||||
};
|
||||
|
||||
function fakeBridge(overrides: Record<string, any> = {}): BridgeInterface {
|
||||
@@ -52,6 +52,63 @@ 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 () => {
|
||||
const setAddRequest = vi.fn(async () => {});
|
||||
const fetchGroupRequests = vi.fn(async () => []);
|
||||
const bridge = fakeBridge({
|
||||
fetchGroupRequests,
|
||||
apis: { groupAdmin: { setAddRequest } } as any,
|
||||
});
|
||||
|
||||
await handleGroupAddRequest(bridge, 'slreq:1:123456:999:22:1', true, 'ok');
|
||||
|
||||
expect(fetchGroupRequests).not.toHaveBeenCalled();
|
||||
expect(setAddRequest).toHaveBeenCalledWith(999, 123456, 22, true, 'ok', true);
|
||||
});
|
||||
|
||||
it('accepts a NapCat numeric sequence and resolves the exact main-inbox request', async () => {
|
||||
const setAddRequest = vi.fn(async () => {});
|
||||
const bridge = fakeBridge({
|
||||
findGroupInviteCardGroupBySequence: vi.fn(() => undefined),
|
||||
fetchGroupRequests: vi.fn(async (filtered: boolean) => filtered ? [] : [
|
||||
fakeRequest({ groupId: 999, sequence: 261237407, eventType: 7, filtered: false }),
|
||||
]),
|
||||
apis: { groupAdmin: { setAddRequest } } as any,
|
||||
});
|
||||
|
||||
await handleGroupAddRequest(bridge, '261237407', false, 'no');
|
||||
|
||||
expect(setAddRequest).toHaveBeenCalledWith(999, 261237407, 7, false, 'no', false);
|
||||
});
|
||||
|
||||
it('accepts a NapCat numeric sequence from the filtered inbox', async () => {
|
||||
const setAddRequest = vi.fn(async () => {});
|
||||
const bridge = fakeBridge({
|
||||
findGroupInviteCardGroupBySequence: vi.fn(() => undefined),
|
||||
fetchGroupRequests: vi.fn(async (filtered: boolean) => filtered ? [
|
||||
fakeRequest({ groupId: 999, sequence: 55, eventType: 2, filtered: true }),
|
||||
] : []),
|
||||
apis: { groupAdmin: { setAddRequest } } as any,
|
||||
});
|
||||
|
||||
await handleGroupAddRequest(bridge, '55', true, 'ok');
|
||||
|
||||
expect(setAddRequest).toHaveBeenCalledWith(999, 55, 2, true, 'ok', true);
|
||||
});
|
||||
|
||||
it('resolves a private invite-card msgseq by its cached group', async () => {
|
||||
const setAddRequest = vi.fn(async () => {});
|
||||
const bridge = fakeBridge({
|
||||
findGroupInviteCardGroupBySequence: vi.fn(() => 999),
|
||||
fetchGroupRequests: vi.fn(async () => []),
|
||||
apis: { groupAdmin: { setAddRequest } } as any,
|
||||
});
|
||||
|
||||
await handleGroupAddRequest(bridge, '778899', true, 'ok');
|
||||
|
||||
expect(setAddRequest).toHaveBeenCalledWith(999, 778899, 2, true, 'ok', false);
|
||||
});
|
||||
|
||||
it('matches add requests by groupId and targetUid', async () => {
|
||||
const setAddRequest = vi.fn(async () => {});
|
||||
const bridge = fakeBridge({
|
||||
@@ -111,4 +168,60 @@ describe('onebot/modules/request-actions / handleGroupAddRequest', () => {
|
||||
await expect(handleGroupAddRequest(bridge, 'invite:999:u_i', true, 'ok'))
|
||||
.rejects.toThrow(/matching group request not found/);
|
||||
});
|
||||
|
||||
it('does not treat an inviter QQ number as a request sequence (#213)', async () => {
|
||||
const setAddRequest = vi.fn(async () => {});
|
||||
const bridge = fakeBridge({
|
||||
findGroupInviteCardGroupBySequence: vi.fn(() => undefined),
|
||||
fetchGroupRequests: vi.fn(async () => [
|
||||
fakeRequest({ groupId: 888, invitorUin: 261237407, sequence: 123456 }),
|
||||
]),
|
||||
apis: { groupAdmin: { setAddRequest } } as any,
|
||||
});
|
||||
|
||||
await expect(handleGroupAddRequest(bridge, '261237407', true, 'ok'))
|
||||
.rejects.toThrow(/sequence 261237407 not found/);
|
||||
expect(setAddRequest).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('does not fall back to another request from the same group', async () => {
|
||||
const setAddRequest = vi.fn(async () => {});
|
||||
const bridge = fakeBridge({
|
||||
fetchGroupRequests: vi.fn(async () => [
|
||||
fakeRequest({ groupId: 999, targetUid: 'u_someone_else' }),
|
||||
]),
|
||||
apis: { groupAdmin: { setAddRequest } } as any,
|
||||
});
|
||||
|
||||
await expect(handleGroupAddRequest(bridge, 'add:999:u_missing', true, 'ok'))
|
||||
.rejects.toThrow(/matching group request not found/);
|
||||
expect(setAddRequest).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('uses the surviving inbox when the other request lookup fails', async () => {
|
||||
const setAddRequest = vi.fn(async () => {});
|
||||
const bridge = fakeBridge({
|
||||
findGroupInviteCardGroupBySequence: vi.fn(() => undefined),
|
||||
fetchGroupRequests: vi.fn(async (filtered: boolean) => {
|
||||
if (!filtered) throw new Error('main inbox down');
|
||||
return [fakeRequest({ groupId: 999, sequence: 55, eventType: 2, filtered: true })];
|
||||
}),
|
||||
apis: { groupAdmin: { setAddRequest } } as any,
|
||||
});
|
||||
|
||||
await handleGroupAddRequest(bridge, '55', true, 'ok');
|
||||
|
||||
expect(setAddRequest).toHaveBeenCalledWith(999, 55, 2, true, 'ok', true);
|
||||
});
|
||||
|
||||
it('distinguishes request-queue failure from a genuine missing request', async () => {
|
||||
const bridge = fakeBridge({
|
||||
findGroupInviteCardGroupBySequence: vi.fn(() => undefined),
|
||||
fetchGroupRequests: vi.fn(async () => { throw new Error('OIDB unavailable'); }),
|
||||
apis: { groupAdmin: { setAddRequest: vi.fn(async () => {}) } } as any,
|
||||
});
|
||||
|
||||
await expect(handleGroupAddRequest(bridge, '55', true, 'ok'))
|
||||
.rejects.toThrow(/failed to fetch group requests/);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user