fix(protocol): support image replies in existing temp sessions

Allow image replies in existing group temporary conversations while preserving passive-only restrictions. Missing, expired, or mismatched sessions remain rejected before media processing, and sending never creates or refreshes a session.

Fixes #471
This commit is contained in:
DrSmoothl committed 2026-09-26 16:30:54 +08:00
1 parent ad360c8eba
commit 1914880df4
11 files changed
+210 -30

No files matched your search

@@ -1111,6 +1111,22 @@ describe('buildApiContext send and forward dispatch', () => {
expect(dispatchEvent).not.toHaveBeenCalled();
});
it('replies with an image only through the recorded temp session without refreshing it', async () => {
const { api, ref } = makeRef();
ref.tempSessions.record(PEER_ID, GROUP_ID);
const record = vi.spyOn(ref.tempSessions, 'record');
const source = 'base64://iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+jFZkAAAAASUVORK5CYII=';
await api.sendPrivateMessage(PEER_ID, [
{ type: 'image', data: { file: source } },
], false, GROUP_ID);
expect(ref.bridge.apis.message.sendGroupTempMessage).toHaveBeenCalledWith(
PEER_ID, GROUP_ID, [expect.objectContaining({ type: 'image', url: source })],
);
expect(ref.bridge.apis.message.sendPrivate).not.toHaveBeenCalled();
expect(record).not.toHaveBeenCalled();
});
it('sends a group message without a self-sent dispatch callback', async () => {
const { api, dispatchEvent, ref } = makeRef();
@@ -1,6 +1,6 @@
import { describe, expect, it, vi } from 'vitest';
import { sendPrivateMessage } from '../src/modules/message-actions';
import { TempSessionStore } from '../src/temp-session-store';
import { TempSessionStore, TEMP_SESSION_TTL_MS } from '../src/temp-session-store';
import type { OneBotInstanceContext } from '../src/instance-context';
// The passive gate lives at the very top of sendPrivateMessage, before any
@@ -21,6 +21,25 @@ function refWith(store: TempSessionStore): OneBotInstanceContext {
}
describe('sendPrivateMessage temp-session gate (passive-only)', () => {
it.each(['missing', 'other-user', 'other-group', 'expired'])(
'refuses an image before parsing or uploading when the session is %s', async (state) => {
const store = new TempSessionStore();
if (state === 'other-user') store.record(1002, 700);
if (state === 'other-group') store.record(1001, 701);
if (state === 'expired') store.record(1001, 700, Date.now() - TEMP_SESSION_TTL_MS);
const record = vi.spyOn(store, 'record');
const ref = refWith(store);
await expect(sendPrivateMessage(ref, 1001, [
{ type: 'image', data: { file: 'https://must-not-fetch.invalid/image.png' } },
], false, 700)).rejects.toThrow('no such temp session');
expect(ref.bridge.apis.message.sendGroupTempMessage).not.toHaveBeenCalled();
expect(ref.bridge.apis.message.sendPrivate).not.toHaveBeenCalled();
expect(record).not.toHaveBeenCalled();
},
);
it('refuses a temp reply to an unrecorded (user, group) and never sends', async () => {
const store = new TempSessionStore();
const ref = refWith(store);