feat(onebot): report web-originated group requests

Report pending group invitations through the standard OneBot request event even when their origin does not provide a live notification.

Prevent duplicate delivery when the same invitation is observed more than once, stop outstanding work with the account lifecycle, and preserve previously supported invitation behavior.

Expand group-system message queries and surface incomplete lookups instead of reporting misleading empty or not-found results.
This commit is contained in:
DrSmoothl committed 2026-08-10 00:26:16 +08:00
1 parent fe44a2f5bc
commit 1996192a5e
14 files changed
+606 -49

No files matched your search

@@ -137,16 +137,18 @@ describe('compiled production Action registry', () => {
const response = await handler.handle('get_group_system_msg', {
group_id: '100000002',
only_pending: true,
count: '80',
});
expect(response.status).toBe('ok');
expect(handleGetGroupSystemMsg).toHaveBeenCalledWith({
groupId: 100000002,
onlyPending: true,
count: 80,
});
const doc = ACTION_REGISTRY.resolve('get_group_system_msg');
if (!doc || doc.kind === 'raw') throw new Error('get_group_system_msg action missing');
expect(doc.action.doc.params.map((param) => param.name)).toEqual(['group_id', 'only_pending']);
expect(doc.action.doc.params.map((param) => param.name)).toEqual(['group_id', 'only_pending', 'count']);
});
});
@@ -123,6 +123,7 @@ function makeGroupRequest(overrides: Partial<GroupRequestInfo> = {}): GroupReque
operatorName: 'operator',
sequence: 123456,
state: 1,
notifyType: 7,
eventType: 22,
comment: 'please',
filtered: false,
@@ -595,6 +596,19 @@ describe('onebot/contact-actions / getStrangerInfo', () => {
});
describe('onebot/contact-actions / getGroupSystemMessages', () => {
it('reads both request inboxes with the requested per-inbox count', async () => {
const fetchGroupRequests = vi.fn(async (filtered: boolean) => filtered
? [makeGroupRequest({ sequence: 2, filtered: true, targetUin: 202, targetUid: '' })]
: [makeGroupRequest({ sequence: 1, filtered: false, targetUin: 101, targetUid: '' })]);
const bridge = fakeBridge({ fetchGroupRequests });
const result = await getGroupSystemMessages(bridge, { count: 80 });
expect(fetchGroupRequests).toHaveBeenNthCalledWith(1, false, 80);
expect(fetchGroupRequests).toHaveBeenNthCalledWith(2, true, 80);
expect(result.map((item) => item.request_id)).toEqual([1, 2]);
});
it('returns the same canonical flag accepted by set_group_add_request', async () => {
const bridge = fakeBridge({
fetchGroupRequests: vi.fn(async () => [
@@ -613,6 +627,28 @@ describe('onebot/contact-actions / getGroupSystemMessages', () => {
});
});
it('reports the inviter as requester for group invitations', async () => {
const bridge = fakeBridge({
fetchGroupRequests: vi.fn(async (filtered: boolean) => filtered ? [] : [
makeGroupRequest({
notifyType: 1,
eventType: 2,
targetUin: 10_001,
targetName: 'bot',
invitorUin: 45_678,
invitorName: 'inviter',
}),
]),
});
const result = await getGroupSystemMessages(bridge);
expect(result).toEqual([expect.objectContaining({
requester_uin: 45_678,
requester_nick: 'inviter',
})]);
});
it('filters by group and pending state before resolving requester identities', async () => {
const fetchUserProfileByUid = vi.fn(async (uid: string) =>
makeProfile(uid === 'u_pending' ? 101 : 202, uid));
@@ -1059,16 +1059,17 @@ describe('extended-actions / get_group_ignored_notifies', () => {
actor: 8888,
invitor_uin: 7777,
invitor_nick: 'inviter',
flag: '7:999:u_t:filtered',
flag: 'slreq:1:42:999:7:1',
}]);
});
it('returns [] when the fetch throws', async () => {
it('surfaces filtered-inbox failures instead of returning a false empty result', async () => {
const bridge = fakeBridge({
fetchGroupRequests: (async () => { throw new Error('boom'); }) as any,
});
const res = await makeHandler(fakeCtx(bridge)).handle('get_group_ignored_notifies', {});
expect(res).toMatchObject({ status: 'ok', data: [] });
expect(res).toMatchObject({ status: 'failed' });
expect(res.wording).toContain('boom');
});
});
@@ -0,0 +1,192 @@
import { describe, expect, it, vi } from 'vitest';
import { BridgeEventBus } from '@snowluma/protocol/event-bus';
import type { GroupRequestInfo } from '@snowluma/protocol/qq-info';
import type { BridgeInterface } from '../../src/bridge/bridge-interface';
import { GroupRequestPoller } from '../src/group-request-poller';
function request(overrides: Partial<GroupRequestInfo> = {}): GroupRequestInfo {
return {
groupId: 999,
groupName: 'group',
targetUid: 'target_uid',
targetUin: 123,
targetName: 'requester',
invitorUid: 'inviter_uid',
invitorUin: 456,
invitorName: 'inviter',
operatorUid: '',
operatorUin: 0,
operatorName: '',
sequence: 42,
state: 1,
notifyType: 7,
eventType: 1,
comment: 'please',
filtered: false,
...overrides,
};
}
function setup(
fetchGroupRequests: (filtered: boolean, count: number) => Promise<GroupRequestInfo[]>,
getGroupInviteCardSequence?: (groupId: number) => number | undefined,
) {
const events = new BridgeEventBus({
onError: (_kind, error) => { throw error; },
});
const bridge = {
events,
apis: { contacts: { fetchGroupRequests, getGroupInviteCardSequence } },
} as unknown as BridgeInterface;
const poller = new GroupRequestPoller(bridge, 10_001, { intervalMs: 60_000 });
return { events, poller };
}
describe('GroupRequestPoller', () => {
it('emits a canonical invite request from a pending list-only notification once', async () => {
const fetchGroupRequests = vi.fn(async (filtered: boolean) => filtered ? [] : [request({
notifyType: 1,
eventType: 2,
})]);
const { events, poller } = setup(fetchGroupRequests);
const received: unknown[] = [];
events.on('group_invite', (event) => { received.push(event); });
await poller.pollOnce();
await poller.pollOnce();
expect(fetchGroupRequests).toHaveBeenCalledWith(false, 50);
expect(fetchGroupRequests).toHaveBeenCalledWith(true, 50);
expect(received).toEqual([expect.objectContaining({
kind: 'group_invite',
selfUin: 10_001,
groupId: 999,
fromUin: 456,
fromUid: 'inviter_uid',
subType: 'invite',
message: 'please',
flag: 'slreq:1:42:999:2:0',
})]);
});
it('uses the applicant identity and filtered handle for join applications', async () => {
const fetchGroupRequests = vi.fn(async (filtered: boolean) => filtered
? [request({ filtered: true, sequence: 43 })]
: []);
const { events, poller } = setup(fetchGroupRequests);
const received: unknown[] = [];
events.on('group_invite', (event) => { received.push(event); });
await poller.pollOnce();
expect(received).toEqual([expect.objectContaining({
fromUin: 123,
fromUid: 'target_uid',
subType: 'add',
flag: 'slreq:1:43:999:1:1',
})]);
});
it('preserves the private invite-card approval sequence when one is known', async () => {
const fetchGroupRequests = vi.fn(async (filtered: boolean) => filtered ? [] : [request({
notifyType: 1,
eventType: 2,
sequence: 42,
})]);
const { events, poller } = setup(fetchGroupRequests, () => 778_899);
const received: unknown[] = [];
events.on('group_invite', (event) => { received.push(event); });
await poller.pollOnce();
expect(received).toEqual([expect.objectContaining({
flag: 'slreq:1:778899:999:2:0',
})]);
});
it('does not emit processed or non-request notification types', async () => {
const fetchGroupRequests = vi.fn(async (filtered: boolean) => filtered ? [] : [
request({ sequence: 1, state: 2 }),
request({ sequence: 2, notifyType: 9, eventType: 6 }),
]);
const { events, poller } = setup(fetchGroupRequests);
const received = vi.fn();
events.on('group_invite', received);
await poller.pollOnce();
expect(received).not.toHaveBeenCalled();
});
it('fails the scan when either inbox cannot be read', async () => {
const fetchGroupRequests = vi.fn(async (filtered: boolean) => {
if (filtered) throw new Error('filtered inbox unavailable');
return [request()];
});
const { events, poller } = setup(fetchGroupRequests);
const received = vi.fn();
events.on('group_invite', received);
await expect(poller.pollOnce()).rejects.toThrow('filtered inbox unavailable');
expect(received).not.toHaveBeenCalled();
});
it('does not duplicate a request already delivered by the real-time path', async () => {
let releaseMain!: (value: GroupRequestInfo[]) => void;
const main = new Promise<GroupRequestInfo[]>((resolve) => { releaseMain = resolve; });
const fetchGroupRequests = vi.fn((filtered: boolean) => filtered
? Promise.resolve([])
: main);
const { events, poller } = setup(fetchGroupRequests);
const received: unknown[] = [];
events.on('group_invite', (event) => { received.push(event); });
poller.start();
await events.emit({
kind: 'group_invite',
time: 1,
selfUin: 10_001,
groupId: 999,
fromUin: 456,
fromUid: 'inviter_uid',
subType: 'invite',
message: 'please',
flag: 'invite:999:inviter_uid',
});
releaseMain([request({ notifyType: 1, eventType: 2 })]);
await poller.stop();
expect(received).toHaveLength(1);
});
it('shares concurrent scans instead of issuing overlapping requests', async () => {
let release!: (value: GroupRequestInfo[]) => void;
const pending = new Promise<GroupRequestInfo[]>((resolve) => { release = resolve; });
const fetchGroupRequests = vi.fn(() => pending);
const { poller } = setup(fetchGroupRequests);
const first = poller.pollOnce();
const second = poller.pollOnce();
expect(second).toBe(first);
expect(fetchGroupRequests).toHaveBeenCalledTimes(2);
release([]);
await first;
});
it('does not emit from an in-flight scheduled scan after shutdown starts', async () => {
let release!: (value: GroupRequestInfo[]) => void;
const pending = new Promise<GroupRequestInfo[]>((resolve) => { release = resolve; });
const fetchGroupRequests = vi.fn(() => pending);
const { events, poller } = setup(fetchGroupRequests);
const received = vi.fn();
events.on('group_invite', received);
poller.start();
const stopped = poller.stop();
release([request()]);
await stopped;
expect(received).not.toHaveBeenCalled();
});
});
@@ -18,6 +18,8 @@ function fabricatedInstance(results: NetworkShutdownResult[]) {
heartbeatTimer: null,
eventPipeline: null,
eventPipelineDrain: Promise.resolve(),
groupRequestPoller: { stop: vi.fn(() => Promise.resolve()) },
groupRequestPollerDrain: Promise.resolve(),
disposePromise: null,
disposeRequested: false,
acceptingActions: true,
@@ -23,6 +23,7 @@ function fakeInstance(
nickname: `retiring-${uin}`,
quiesce,
dispose,
startGroupRequestPolling: vi.fn(),
getConnectionStatuses: () => statuses,
} as unknown as OneBotInstance;
}
@@ -173,6 +174,9 @@ describe('OneBotManager database preparation', () => {
expect(beginMigration).toHaveBeenCalledOnce();
expect(manager.getInstance('10001')).not.toBeNull();
await vi.waitFor(() => {
expect(instance.startGroupRequestPolling).toHaveBeenCalledOnce();
});
} finally {
await manager.dispose();
process.chdir(originalCwd);
+25 -8
View File
@@ -7,7 +7,7 @@ import type { GroupRequestInfo } from '@snowluma/protocol/qq-info';
const APIS_ROUTING: Record<string, string> = {
fetchFriendList: 'contacts', fetchGroupList: 'contacts',
fetchGroupMemberList: 'contacts', fetchUserProfile: 'contacts',
fetchGroupRequests: 'contacts', fetchDownloadRKeys: 'contacts',
fetchGroupRequests: 'contacts', fetchGroupRequestsByUid: 'contacts', fetchDownloadRKeys: 'contacts',
getGroupInviteCardSequence: 'contacts', findGroupInviteCardGroupBySequence: 'contacts',
};
@@ -111,10 +111,11 @@ describe('onebot/modules/request-actions / handleGroupAddRequest', () => {
it('matches add requests by groupId and targetUid', async () => {
const setAddRequest = vi.fn(async () => {});
const fetchGroupRequestsByUid = vi.fn(async () => [
fakeRequest({ groupId: 999, targetUid: 'u_t', sequence: 42, eventType: 7, filtered: false }),
]);
const bridge = fakeBridge({
fetchGroupRequests: vi.fn(async () => [
fakeRequest({ groupId: 999, targetUid: 'u_t', sequence: 42, eventType: 7, filtered: false }),
]),
fetchGroupRequestsByUid,
apis: { groupAdmin: { setAddRequest } } as any,
});
@@ -122,12 +123,14 @@ describe('onebot/modules/request-actions / handleGroupAddRequest', () => {
expect(setAddRequest).toHaveBeenCalledOnce();
expect(setAddRequest).toHaveBeenCalledWith(999, 42, 7, true, 'ok', false);
expect(fetchGroupRequestsByUid).toHaveBeenCalledWith(false, 100);
expect(fetchGroupRequestsByUid).toHaveBeenCalledWith(true, 100);
});
it('matches invite requests by groupId and invitorUid', async () => {
const setAddRequest = vi.fn(async () => {});
const bridge = fakeBridge({
fetchGroupRequests: vi.fn(async () => [
fetchGroupRequestsByUid: vi.fn(async () => [
fakeRequest({ groupId: 999, invitorUid: 'u_i', sequence: 97, eventType: 8, filtered: false }),
]),
getGroupInviteCardSequence: vi.fn(() => null),
@@ -144,7 +147,7 @@ describe('onebot/modules/request-actions / handleGroupAddRequest', () => {
const setAddRequest = vi.fn(async () => {});
const bridge = fakeBridge({
// Main inbox empty; the invite sits only in the spam-filtered inbox.
fetchGroupRequests: vi.fn(async (filtered: boolean) =>
fetchGroupRequestsByUid: vi.fn(async (filtered: boolean) =>
filtered
? [fakeRequest({ groupId: 999, invitorUid: 'u_i', sequence: 55, eventType: 2, filtered: true })]
: []),
@@ -161,7 +164,7 @@ describe('onebot/modules/request-actions / handleGroupAddRequest', () => {
it('surfaces "not found" only when neither inbox has the request', async () => {
const bridge = fakeBridge({
fetchGroupRequests: vi.fn(async () => []),
fetchGroupRequestsByUid: vi.fn(async () => []),
getGroupInviteCardSequence: vi.fn(() => null),
apis: { groupAdmin: { setAddRequest: vi.fn(async () => {}) } } as any,
});
@@ -187,7 +190,7 @@ describe('onebot/modules/request-actions / handleGroupAddRequest', () => {
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 () => [
fetchGroupRequestsByUid: vi.fn(async () => [
fakeRequest({ groupId: 999, targetUid: 'u_someone_else' }),
]),
apis: { groupAdmin: { setAddRequest } } as any,
@@ -214,6 +217,20 @@ describe('onebot/modules/request-actions / handleGroupAddRequest', () => {
expect(setAddRequest).toHaveBeenCalledWith(999, 55, 2, true, 'ok', true);
});
it('does not report not-found when an unavailable inbox may contain the request', async () => {
const bridge = fakeBridge({
findGroupInviteCardGroupBySequence: vi.fn(() => undefined),
fetchGroupRequests: vi.fn(async (filtered: boolean) => {
if (filtered) throw new Error('filtered inbox down');
return [];
}),
apis: { groupAdmin: { setAddRequest: vi.fn(async () => {}) } } as any,
});
await expect(handleGroupAddRequest(bridge, '55', true, 'ok'))
.rejects.toThrow(/lookup incomplete/);
});
it('distinguishes request-queue failure from a genuine missing request', async () => {
const bridge = fakeBridge({
findGroupInviteCardGroupBySequence: vi.fn(() => undefined),