Files
DrSmoothl c3675aef7c feat(emoji-like): back fetch_emoji_like / get_emoji_likes with a local cache
Two rounds of production wire-level probing established that the OIDB
SSO path cannot surface the reactor user list for an emoji:

  - 0x9083_1                  → 4-byte ack `18 01 20 01` (no list)
  - 0x9084_1                  → per-emoji summary (count + timestamp,
                                still no user IDs)
  - 0x9082_3..5, 0x9083_0/2/3, 0x9084_2..5, 0x9085_1/2, 0x9087_1
                              → "no privilege" — the cmds exist but
                                aren't in NTQQ's client capability
                                bitmap, so the server blocks anything
                                not registered at login. NTQQ itself
                                serves getMsgEmojiLikesList from a
                                wrapper-internal cache, not SSO.

Mirror that wrapper-cache approach on the OneBot side. SnowLuma
already receives GroupMsgEmojiLikeEvent push events for every
reaction add/remove (with operator uin + uid + emoji + msgSeq); we
just weren't persisting them. Now we do.

Changes:

* proto-defs: add Oidb0x9084 Req/Resp schema for the summary call.
  RespEntry's lastReactionTime/count are optional because the
  "available reactions catalog" tail entries omit both — only the
  "used" subset carries metadata.

* core InteractionApi: revert the 9-candidate probe loop, restore a
  single 0x9083_1 call as a legacy stub (returns empty list, with a
  debug-level hex dump on empty responses), and add
  fetchReactionSummary() that calls 0x9084_1 and yields the per-emoji
  summary (count + last-reaction timestamp).

* onebot ReactionStore: new SQLite-backed cache, schema
  reactions(group_id, msg_seq, emoji_id, emoji_type, operator_uin,
  operator_uid, set_at), PK on (group_id, msg_seq, emoji_id,
  operator_uin). Mirrors MessageStore design — prepared statements at
  construction, WAL journal.

* onebot event-pipeline: subscribe group_msg_emoji_like, call
  reactionStore.recordAdd / recordRemove before convertAndDispatch so
  a fetch_emoji_like racing with the push still sees the new row.

* onebot instance / instance-context: own the ReactionStore, expose
  it on OneBotInstanceContext, close it on dispose. New
  fetchEmojiLikeUsers helper on ApiActionContext combines local-cache
  reads with a 0x9084_1 cross-check so callers can detect cache gaps
  (events missed before bot boot, etc).

* onebot actions: get_emoji_likes and fetch_emoji_like now read from
  the cache. fetch_emoji_like's NapCat-style cookie becomes a numeric
  offset for cache pagination (no real cursor needed against local
  SQLite).

* tests: 7 new ReactionStore unit tests (CRUD + pagination +
  cross-key isolation + summarizeMessage). Existing event-pipeline
  test harness picks up a no-op ReactionStore stub.
2026-05-22 22:08:45 +08:00

90 lines
3.8 KiB
TypeScript

import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import fs from 'fs';
import path from 'path';
import { ReactionStore } from '../src/reaction-store';
describe('ReactionStore', () => {
const testDbPath = path.join('data', 'test', 'reactions-test.db');
let store: ReactionStore;
beforeEach(() => {
try { fs.unlinkSync(testDbPath); } catch { /* ignore */ }
try { fs.unlinkSync(testDbPath + '-wal'); } catch { /* ignore */ }
try { fs.unlinkSync(testDbPath + '-shm'); } catch { /* ignore */ }
store = new ReactionStore(testDbPath);
});
afterEach(() => {
store.close();
try { fs.unlinkSync(testDbPath); } catch { /* ignore */ }
try { fs.unlinkSync(testDbPath + '-wal'); } catch { /* ignore */ }
try { fs.unlinkSync(testDbPath + '-shm'); } catch { /* ignore */ }
});
it('records an add and exposes it via listUsers / countUsers', () => {
store.recordAdd(100, 4001, '124', 1, 12345, 'u_abc', 1700000000);
const users = store.listUsers(100, 4001, '124');
expect(users).toEqual([{ operatorUin: 12345, operatorUid: 'u_abc', setAt: 1700000000 }]);
expect(store.countUsers(100, 4001, '124')).toBe(1);
});
it('upserts on duplicate (group, msg, emoji, operator)', () => {
store.recordAdd(100, 4001, '124', 1, 12345, 'u_old', 1700000000);
store.recordAdd(100, 4001, '124', 1, 12345, 'u_new', 1700000500);
const users = store.listUsers(100, 4001, '124');
expect(users).toHaveLength(1);
expect(users[0]?.operatorUid).toBe('u_new');
expect(users[0]?.setAt).toBe(1700000500);
});
it('removes the right row only', () => {
store.recordAdd(100, 4001, '124', 1, 12345, 'u_a', 1700000000);
store.recordAdd(100, 4001, '124', 1, 67890, 'u_b', 1700000100);
store.recordRemove(100, 4001, '124', 12345);
const users = store.listUsers(100, 4001, '124');
expect(users).toHaveLength(1);
expect(users[0]?.operatorUin).toBe(67890);
});
it('isolates by group / msg / emoji', () => {
store.recordAdd(100, 4001, '124', 1, 1, 'u', 1);
store.recordAdd(100, 4001, '76', 1, 1, 'u', 1);
store.recordAdd(100, 4002, '124', 1, 1, 'u', 1);
store.recordAdd(200, 4001, '124', 1, 1, 'u', 1);
expect(store.countUsers(100, 4001, '124')).toBe(1);
expect(store.countUsers(100, 4001, '76')).toBe(1);
expect(store.countUsers(100, 4002, '124')).toBe(1);
expect(store.countUsers(200, 4001, '124')).toBe(1);
expect(store.countUsers(100, 4001, '999')).toBe(0);
});
it('paginates listUsers via limit + offset, ordering by set_at ASC', () => {
for (let i = 0; i < 5; i++) {
store.recordAdd(100, 4001, '124', 1, 1000 + i, `u_${i}`, 1700000000 + i);
}
const page1 = store.listUsers(100, 4001, '124', 2, 0);
const page2 = store.listUsers(100, 4001, '124', 2, 2);
expect(page1.map(u => u.operatorUin)).toEqual([1000, 1001]);
expect(page2.map(u => u.operatorUin)).toEqual([1002, 1003]);
});
it('summarizeMessage groups by emoji_id', () => {
store.recordAdd(100, 4001, '124', 1, 1, 'u1', 1);
store.recordAdd(100, 4001, '124', 1, 2, 'u2', 2);
store.recordAdd(100, 4001, '76', 1, 3, 'u3', 10);
const summary = store.summarizeMessage(100, 4001).sort((a, b) => a.emojiId.localeCompare(b.emojiId));
expect(summary).toEqual([
{ emojiId: '124', emojiType: 1, count: 2, lastSetAt: 2 },
{ emojiId: '76', emojiType: 1, count: 1, lastSetAt: 10 },
]);
});
it('ignores incomplete records (no-op instead of throwing)', () => {
store.recordAdd(0, 4001, '124', 1, 1, 'u', 1); // group 0
store.recordAdd(100, 0, '124', 1, 1, 'u', 1); // msg 0
store.recordAdd(100, 4001, '', 1, 1, 'u', 1); // empty emoji
store.recordAdd(100, 4001, '124', 1, 0, 'u', 1); // uin 0
expect(store.countUsers(100, 4001, '124')).toBe(0);
});
});