refactor(identity): absorb QQInfo into IdentityService

QQInfo's in-memory state (self identity, friends, group rosters,
profile cache) and read methods (findFriend / findGroup /
findGroupMember / findUserProfile / nickname / uin / selfProfile) now
live directly on IdentityService. The QQInfo class is gone; its
interfaces stay in `qq-info.ts` as plain types shared across the
remember*/find* surface.

This eliminates a long-standing duplication: every observation used to
maintain both QQInfo's in-memory structures and IdentityService's
SQLite mirror in parallel, with the uidByUin/uinByUid Maps tracked
separately. With one Module owning all three layers, every remember*
hydrates one set of structures and `loadSnapshot` repopulates the
in-memory uid↔uin index from SQLite on startup — closing the cold-cache
gap that previously forced findUidByUin to fall through to a SQLite
query after every restart.

Knock-on changes:
  - `Bridge` no longer holds a QQInfo; constructor takes an
    `IdentityService`. The CmdParser signature on the cmd dispatch
    table now passes `IdentityService` to parsers.
  - `msg-push-handler.ts` drops its private `resolveUidToUin(qqInfo, …)`
    helper that bypassed IdentityService's cascade — the parser hot
    path now reads through `identity.findUinByUid(uid, groupId)` and
    benefits from the O(1) `uinByUid` map even on freshly-loaded
    sessions.
  - `BridgeManager`'s session-started callback drops the QQInfo
    parameter; OneBotInstance no longer carries a `qqInfo` field and
    exposes a `nickname` getter for the WebUI listing.
  - `contact-actions` and the per-theme bridge action files stop
    importing QQInfo; they read everything via `bridge.identity.*`.
This commit is contained in:
DrSmoothl committed 2026-05-15 00:44:01 +08:00
1 parent 030336a25c
commit ac10c663f1
27 files changed
+363 -428

No files matched your search

+25 -40
View File
@@ -2,7 +2,7 @@ import { afterEach, describe, expect, it, vi } from 'vitest';
import fs from 'fs';
import path from 'path';
import { IdentityService } from '../src/bridge/identity-service';
import { QQInfo, type GroupMemberInfo, type QQGroupInfo, type UserProfileInfo } from '../src/bridge/qq-info';
import type { GroupMemberInfo, QQGroupInfo, UserProfileInfo } from '../src/bridge/qq-info';
const SELF_UIN = '10001';
const GROUP_ID = 123456789;
@@ -60,8 +60,7 @@ describe('IdentityService', () => {
const dbPath = tempDbPath('persist');
{
const qqInfo = new QQInfo(SELF_UIN);
const identity = new IdentityService(qqInfo, dbPath);
const identity = new IdentityService(SELF_UIN, dbPath);
identity.rememberFriends([{ uin: 22222, uid: 'u_friend', nickname: 'friend', remark: 'remark' }]);
identity.rememberGroups([makeGroup()]);
identity.rememberGroupMembers(GROUP_ID, [makeMember(33333, 'u_member', 'card')]);
@@ -69,12 +68,11 @@ describe('IdentityService', () => {
}
{
const qqInfo = new QQInfo(SELF_UIN);
const identity = new IdentityService(qqInfo, dbPath);
const identity = new IdentityService(SELF_UIN, dbPath);
expect(qqInfo.findFriend(22222)?.uid).toBe('u_friend');
expect(qqInfo.findGroup(GROUP_ID)?.groupName).toBe('group');
expect(qqInfo.findGroupMember(GROUP_ID, 33333)?.card).toBe('card');
expect(identity.findFriend(22222)?.uid).toBe('u_friend');
expect(identity.findGroup(GROUP_ID)?.groupName).toBe('group');
expect(identity.findGroupMember(GROUP_ID, 33333)?.card).toBe('card');
expect(identity.findUidByUin(33333, GROUP_ID)).toBe('u_member');
expect(identity.findUinByUid('u_member', GROUP_ID)).toBe(33333);
@@ -88,8 +86,7 @@ describe('IdentityService', () => {
const second = makeMember(44444, 'u_second');
{
const qqInfo = new QQInfo(SELF_UIN);
const identity = new IdentityService(qqInfo, dbPath);
const identity = new IdentityService(SELF_UIN, dbPath);
identity.rememberGroups([makeGroup()]);
identity.rememberGroupMembers(GROUP_ID, [first, second]);
identity.rememberGroupMembers(GROUP_ID, [second]);
@@ -97,11 +94,10 @@ describe('IdentityService', () => {
}
{
const qqInfo = new QQInfo(SELF_UIN);
const identity = new IdentityService(qqInfo, dbPath);
const identity = new IdentityService(SELF_UIN, dbPath);
expect(qqInfo.findGroupMember(GROUP_ID, first.uin)).toBeNull();
expect(qqInfo.findGroupMember(GROUP_ID, second.uin)?.uid).toBe(second.uid);
expect(identity.findGroupMember(GROUP_ID, first.uin)).toBeNull();
expect(identity.findGroupMember(GROUP_ID, second.uin)?.uid).toBe(second.uid);
// Historical identity remains available for UID/UIN resolution.
expect(identity.findUidByUin(first.uin, GROUP_ID)).toBe(first.uid);
@@ -114,8 +110,7 @@ describe('IdentityService', () => {
const member = makeMember(33333, 'u_member');
{
const qqInfo = new QQInfo(SELF_UIN);
const identity = new IdentityService(qqInfo, dbPath);
const identity = new IdentityService(SELF_UIN, dbPath);
identity.rememberFriends([{ uin: 22222, uid: 'u_friend', nickname: 'friend', remark: '' }]);
identity.rememberGroups([makeGroup()]);
identity.rememberGroupMembers(GROUP_ID, [member]);
@@ -125,11 +120,10 @@ describe('IdentityService', () => {
}
{
const qqInfo = new QQInfo(SELF_UIN);
const identity = new IdentityService(qqInfo, dbPath);
const identity = new IdentityService(SELF_UIN, dbPath);
expect(qqInfo.findFriend(22222)).toBeNull();
expect(qqInfo.findGroup(GROUP_ID)).toBeNull();
expect(identity.findFriend(22222)).toBeNull();
expect(identity.findGroup(GROUP_ID)).toBeNull();
// Identity mappings remain useful for historical events/actions.
expect(identity.findUidByUin(22222)).toBe('u_friend');
expect(identity.findUidByUin(member.uin, GROUP_ID)).toBe(member.uid);
@@ -143,8 +137,7 @@ describe('IdentityService', () => {
const member = makeMember(33333, 'u_member');
{
const qqInfo = new QQInfo(SELF_UIN);
const identity = new IdentityService(qqInfo, dbPath);
const identity = new IdentityService(SELF_UIN, dbPath);
identity.rememberGroups([makeGroup()]);
identity.rememberGroupMembers(GROUP_ID, [member]);
identity.markGroupMemberInactive(GROUP_ID, { uid: member.uid, uin: member.uin });
@@ -152,10 +145,9 @@ describe('IdentityService', () => {
}
{
const qqInfo = new QQInfo(SELF_UIN);
const identity = new IdentityService(qqInfo, dbPath);
const identity = new IdentityService(SELF_UIN, dbPath);
expect(qqInfo.findGroupMember(GROUP_ID, member.uin)).toBeNull();
expect(identity.findGroupMember(GROUP_ID, member.uin)).toBeNull();
expect(identity.findUinByUid(member.uid, GROUP_ID)).toBe(member.uin);
identity.close();
@@ -166,8 +158,7 @@ describe('IdentityService', () => {
const dbPath = tempDbPath('request-events');
{
const qqInfo = new QQInfo(SELF_UIN);
const identity = new IdentityService(qqInfo, dbPath);
const identity = new IdentityService(SELF_UIN, dbPath);
identity.rememberRequestIdentity({
uid: 'u_friend_request',
uin: 55555,
@@ -183,12 +174,11 @@ describe('IdentityService', () => {
}
{
const qqInfo = new QQInfo(SELF_UIN);
const identity = new IdentityService(qqInfo, dbPath);
const identity = new IdentityService(SELF_UIN, dbPath);
expect(identity.findUidByUin(55555)).toBe('u_friend_request');
expect(identity.findUinByUid('u_friend_request')).toBe(55555);
expect(qqInfo.findGroup(GROUP_ID)?.groupId).toBe(GROUP_ID);
expect(identity.findGroup(GROUP_ID)?.groupId).toBe(GROUP_ID);
expect(identity.findUidByUin(66666)).toBe('u_group_request');
identity.close();
@@ -202,8 +192,7 @@ describe('IdentityService.resolveUid', () => {
}
it('returns the cached uid without invoking the fetcher', async () => {
const qqInfo = new QQInfo(SELF_UIN);
const identity = IdentityService.memory(qqInfo);
const identity = IdentityService.memory(SELF_UIN);
identity.rememberRequestIdentity({ uid: 'u_known', uin: 12345 });
const fetchProfile = vi.fn(async (uin: number) => makeProfile(uin, 'should-not-be-used'));
identity.setFetcher({ fetchProfile });
@@ -213,8 +202,7 @@ describe('IdentityService.resolveUid', () => {
});
it('falls back to fetchProfile on miss and returns the resolved uid', async () => {
const qqInfo = new QQInfo(SELF_UIN);
const identity = IdentityService.memory(qqInfo);
const identity = IdentityService.memory(SELF_UIN);
const fetchProfile = vi.fn(async (uin: number) => {
// Simulate the bridge writing the result back via rememberUserProfile.
identity.rememberUserProfile(makeProfile(uin, 'u_fetched'));
@@ -227,16 +215,14 @@ describe('IdentityService.resolveUid', () => {
});
it('throws when neither cache nor fetcher can produce a uid', async () => {
const qqInfo = new QQInfo(SELF_UIN);
const identity = IdentityService.memory(qqInfo);
const identity = IdentityService.memory(SELF_UIN);
identity.setFetcher({ fetchProfile: async (uin) => makeProfile(uin, '') });
await expect(identity.resolveUid(99999)).rejects.toThrow(/failed to resolve UID/);
});
it('tries fetchGroupMemberList before fetchProfile when groupId is provided', async () => {
const qqInfo = new QQInfo(SELF_UIN);
const identity = IdentityService.memory(qqInfo);
const identity = IdentityService.memory(SELF_UIN);
identity.rememberGroups([{
groupId: GROUP_ID, groupName: '', remark: '',
memberCount: 0, memberMax: 0, members: new Map(),
@@ -256,8 +242,7 @@ describe('IdentityService.resolveUid', () => {
});
it('throws on invalid (zero / negative) uin without calling fetcher', async () => {
const qqInfo = new QQInfo(SELF_UIN);
const identity = IdentityService.memory(qqInfo);
const identity = IdentityService.memory(SELF_UIN);
const fetchProfile = vi.fn();
identity.setFetcher({ fetchProfile });