feat(onebot): reply into group temp sessions (临时会话) (#212)
send_private_msg / send_msg can now reply to a group temp-session message by passing the source group_id. Inbound temp messages surface their source group on sender.group_id; a reply is only made into a temp session the bot has an inbound record of. Group temp replies route through RoutingHead.grpTmp (field 3).
This commit is contained in:
19 files changed
+311
-15
No files matched your search
@@ -109,10 +109,13 @@ describe('convertEvent — message kinds', () => {
|
||||
expect(out!.post_type).toBe('message_sent');
|
||||
});
|
||||
|
||||
it('temp_message peer → post_type "message", sub_type "group"', async () => {
|
||||
it('temp_message peer → post_type "message", sub_type "group", sender.group_id set', async () => {
|
||||
const out = await convertEvent(bareCtx(), makeTempMessage(PEER_UIN));
|
||||
expect(out!.post_type).toBe('message');
|
||||
expect(out!.sub_type).toBe('group');
|
||||
// Source group is surfaced on sender.group_id so a client can reply via
|
||||
// send_private_msg(user_id, group_id=...).
|
||||
expect((out!.sender as { group_id?: number }).group_id).toBe(99999);
|
||||
});
|
||||
|
||||
it('temp_message self → post_type "message_sent"', async () => {
|
||||
|
||||
@@ -12,6 +12,7 @@ vi.mock('../src/event-converter', async () => {
|
||||
import { convertEvent, type ConverterContext } from '../src/event-converter';
|
||||
import { registerEventPipeline } from '../src/event-pipeline';
|
||||
import type { OneBotInstanceContext } from '../src/instance-context';
|
||||
import { TempSessionStore } from '../src/temp-session-store';
|
||||
import type {
|
||||
FriendMessage,
|
||||
GroupMessage,
|
||||
@@ -115,6 +116,7 @@ function makeContext(extra: Partial<OneBotInstanceContext> = {}): {
|
||||
summarizeMessage: () => [],
|
||||
close: () => {},
|
||||
} as never,
|
||||
tempSessions: new TempSessionStore(),
|
||||
converterCtx,
|
||||
config: { networks: { httpServers: [], httpClients: [], wsServers: [], wsClients: [] } } as never,
|
||||
cacheMessageMeta: (id, meta) => { metaCalls.push({ id, meta }); },
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
import { sendPrivateMessage } from '../src/modules/message-actions';
|
||||
import { TempSessionStore } from '../src/temp-session-store';
|
||||
import type { OneBotInstanceContext } from '../src/instance-context';
|
||||
|
||||
// The passive gate lives at the very top of sendPrivateMessage, before any
|
||||
// message parsing or network call. These tests exercise that gate directly:
|
||||
// a temp reply (tempGroupId set) into a session the peer never opened MUST be
|
||||
// refused, without ever reaching the send path. This is the structural
|
||||
// guarantee that the public build cannot initiate a temp session.
|
||||
|
||||
function refWith(store: TempSessionStore): OneBotInstanceContext {
|
||||
const sendGroupTempMessage = vi.fn();
|
||||
const sendPrivate = vi.fn();
|
||||
return {
|
||||
tempSessions: store,
|
||||
// If the gate ever let a send through, these would be called — the tests
|
||||
// assert they are NOT for the refused case.
|
||||
bridge: { apis: { message: { sendGroupTempMessage, sendPrivate } } },
|
||||
} as unknown as OneBotInstanceContext;
|
||||
}
|
||||
|
||||
describe('sendPrivateMessage temp-session gate (passive-only)', () => {
|
||||
it('refuses a temp reply to an unrecorded (user, group) and never sends', async () => {
|
||||
const store = new TempSessionStore();
|
||||
const ref = refWith(store);
|
||||
await expect(
|
||||
sendPrivateMessage(ref, 1001, [{ type: 'text', data: { text: 'hi' } }], false, /* tempGroupId */ 700),
|
||||
).rejects.toThrow(/passive-only|temp session/i);
|
||||
// The gate threw before any wire call.
|
||||
expect((ref.bridge.apis.message as { sendGroupTempMessage: ReturnType<typeof vi.fn> }).sendGroupTempMessage)
|
||||
.not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('refuses when a DIFFERENT group is recorded (exact (user, group) match required)', async () => {
|
||||
const store = new TempSessionStore();
|
||||
store.record(1001, 701); // user opened a session from group 701, not 700
|
||||
const ref = refWith(store);
|
||||
await expect(
|
||||
sendPrivateMessage(ref, 1001, [{ type: 'text', data: { text: 'hi' } }], false, 700),
|
||||
).rejects.toThrow(/passive-only|temp session/i);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,57 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { TempSessionStore, TEMP_SESSION_TTL_MS } from '../src/temp-session-store';
|
||||
|
||||
const T0 = 1_700_000_000_000;
|
||||
|
||||
describe('TempSessionStore (passive temp-session gate, #212-temp)', () => {
|
||||
it('a recorded session is authorized; an unrecorded one is NOT (the core gate)', () => {
|
||||
const s = new TempSessionStore();
|
||||
s.record(1001, 700, T0);
|
||||
// Recorded (user, group) → allowed.
|
||||
expect(s.has(1001, 700, T0)).toBe(true);
|
||||
// Never recorded → refused. This is what makes send passive-only: the bot
|
||||
// cannot reply to a (user, group) that never messaged it first.
|
||||
expect(s.has(1001, 701, T0)).toBe(false); // same user, different group
|
||||
expect(s.has(1002, 700, T0)).toBe(false); // different user, same group
|
||||
expect(s.has(9999, 9999, T0)).toBe(false); // wholly unknown
|
||||
});
|
||||
|
||||
it('sessions are keyed per (user, group) — one user, many groups', () => {
|
||||
const s = new TempSessionStore();
|
||||
s.record(1001, 700, T0);
|
||||
s.record(1001, 800, T0);
|
||||
expect(s.has(1001, 700, T0)).toBe(true);
|
||||
expect(s.has(1001, 800, T0)).toBe(true);
|
||||
expect(s.has(1001, 900, T0)).toBe(false);
|
||||
});
|
||||
|
||||
it('expires exactly at TTL and prunes on read', () => {
|
||||
const s = new TempSessionStore();
|
||||
s.record(1001, 700, T0);
|
||||
// Just before expiry: still valid.
|
||||
expect(s.has(1001, 700, T0 + TEMP_SESSION_TTL_MS - 1)).toBe(true);
|
||||
// At/after expiry: gone.
|
||||
expect(s.has(1001, 700, T0 + TEMP_SESSION_TTL_MS)).toBe(false);
|
||||
// A later inbound message refreshes the window.
|
||||
s.record(1001, 700, T0 + TEMP_SESSION_TTL_MS);
|
||||
expect(s.has(1001, 700, T0 + TEMP_SESSION_TTL_MS + 1)).toBe(true);
|
||||
});
|
||||
|
||||
it('ignores invalid records (no group / no user) — stays receive-only', () => {
|
||||
const s = new TempSessionStore();
|
||||
s.record(1001, 0, T0); // no source group
|
||||
s.record(0, 700, T0); // no user
|
||||
s.record(-5, -5, T0);
|
||||
expect(s.has(1001, 0, T0)).toBe(false);
|
||||
expect(s.has(0, 700, T0)).toBe(false);
|
||||
});
|
||||
|
||||
it('sweep drops expired entries', () => {
|
||||
const s = new TempSessionStore();
|
||||
s.record(1001, 700, T0);
|
||||
s.record(1002, 700, T0 + TEMP_SESSION_TTL_MS); // expires later
|
||||
s.sweep(T0 + TEMP_SESSION_TTL_MS + 1);
|
||||
expect(s.has(1001, 700, T0 + TEMP_SESSION_TTL_MS + 1)).toBe(false);
|
||||
expect(s.has(1002, 700, T0 + TEMP_SESSION_TTL_MS + 1)).toBe(true);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user