refactor(identity): consolidate UID resolution behind IdentityService

Pull the cache→DB→network-fetch fallback cascade for UIN→UID translation
into a single async `resolveUid` on IdentityService, and drop the
duplicated `resolveUserUid` helper that used to live in bridge-oidb. Bridge
injects itself as the IdentityFetcher post-construction so the network
fallback runs through the same OIDB profile fetch the helper used.

Each action module's `resolveUserUid(bridge, uin, groupId?)` call becomes
`bridge.resolveUserUid(uin, groupId?)` — same signature on Bridge, but
internally it now goes through `identity.resolveUid` so future changes
(retry, coalescing, alternative resolvers) only need to land in one place.

This is part 1 of the IdentityService deepening (RoadMap item 1). The
QQInfo bypass in msg-push-handler and the broader QQInfo absorption land
in the follow-up commit.
This commit is contained in:
DrSmoothl committed 2026-05-15 00:32:59 +08:00
1 parent 2e4dd9b01e
commit 030336a25c
20 files changed
+157 -75

No files matched your search

+2
View File
@@ -24,6 +24,7 @@ export interface MockBridge {
fetchFriendList: ReturnType<typeof vi.fn>;
fetchGroupMemberList: ReturnType<typeof vi.fn>;
fetchUserProfile: ReturnType<typeof vi.fn>;
resolveUserUid: ReturnType<typeof vi.fn>;
}
export function mockBridge(overrides: Partial<MockBridge> = {}): MockBridge {
@@ -50,6 +51,7 @@ export function mockBridge(overrides: Partial<MockBridge> = {}): MockBridge {
fetchFriendList: vi.fn(async () => []),
fetchGroupMemberList: vi.fn(async () => []),
fetchUserProfile: vi.fn(async () => ({ uid: 'profile-uid' })),
resolveUserUid: vi.fn(async () => 'resolved-uid'),
...overrides,
};
}
@@ -3,7 +3,6 @@ import { describe, it, expect, vi } from 'vitest';
vi.mock('../../src/bridge/bridge-oidb', () => ({
sendOidbAndCheck: vi.fn(async () => undefined),
sendOidbAndDecode: vi.fn(async () => ({})),
resolveUserUid: vi.fn(async () => 'resolved-uid'),
makeOidbRequest: vi.fn(() => new Uint8Array(0)),
}));
+3 -5
View File
@@ -3,7 +3,6 @@ import { describe, it, expect, vi, beforeEach } from 'vitest';
vi.mock('../../src/bridge/bridge-oidb', () => ({
sendOidbAndCheck: vi.fn(async () => undefined),
sendOidbAndDecode: vi.fn(async () => ({})),
resolveUserUid: vi.fn(async () => 'resolved-uid'),
makeOidbRequest: vi.fn(() => new Uint8Array(0)),
}));
@@ -14,13 +13,12 @@ import { mockBridge } from './_helpers';
describe('actions/friend', () => {
beforeEach(() => {
vi.mocked(oidb.sendOidbAndCheck).mockClear();
vi.mocked(oidb.resolveUserUid).mockClear();
});
it('setFriendAddRequest: numeric input is treated as UIN and resolved', async () => {
const bridge = mockBridge();
await friend.setFriendAddRequest(bridge as any, '10001', true);
expect(oidb.resolveUserUid).toHaveBeenCalledWith(bridge, 10001);
expect(bridge.resolveUserUid).toHaveBeenCalledWith(10001);
const payload: any = vi.mocked(oidb.sendOidbAndCheck).mock.calls[0]![4];
expect(payload).toMatchObject({ accept: 3, targetUid: 'resolved-uid' });
});
@@ -28,7 +26,7 @@ describe('actions/friend', () => {
it('setFriendAddRequest: non-numeric flag is forwarded as-is', async () => {
const bridge = mockBridge();
await friend.setFriendAddRequest(bridge as any, 'flag-abc', false);
expect(oidb.resolveUserUid).not.toHaveBeenCalled();
expect(bridge.resolveUserUid).not.toHaveBeenCalled();
const payload: any = vi.mocked(oidb.sendOidbAndCheck).mock.calls[0]![4];
expect(payload).toMatchObject({ accept: 5, targetUid: 'flag-abc' });
});
@@ -36,7 +34,7 @@ describe('actions/friend', () => {
it('deleteFriend resolves UID, calls 0x126b_0, and triggers a friend-list refresh', async () => {
const bridge = mockBridge();
await friend.deleteFriend(bridge as any, 10001, true);
expect(oidb.resolveUserUid).toHaveBeenCalledWith(bridge, 10001);
expect(bridge.resolveUserUid).toHaveBeenCalledWith(10001);
const call = vi.mocked(oidb.sendOidbAndCheck).mock.calls[0]!;
expect(call[1]).toBe('OidbSvcTrpcTcp.0x126b_0');
expect((call[4] as any).field1.block).toBe(true);
@@ -5,7 +5,6 @@ import { describe, it, expect, vi, beforeEach } from 'vitest';
vi.mock('../../src/bridge/bridge-oidb', () => ({
sendOidbAndCheck: vi.fn(async () => undefined),
sendOidbAndDecode: vi.fn(async () => ({})),
resolveUserUid: vi.fn(async () => 'resolved-uid'),
makeOidbRequest: vi.fn(() => new Uint8Array(0)),
}));
@@ -17,13 +16,12 @@ describe('actions/group-admin', () => {
beforeEach(() => {
vi.mocked(oidb.sendOidbAndCheck).mockClear();
vi.mocked(oidb.sendOidbAndDecode).mockClear();
vi.mocked(oidb.resolveUserUid).mockClear();
});
it('muteGroupMember resolves UID and dispatches 0x1253_1', async () => {
const bridge = mockBridge();
await admin.muteGroupMember(bridge as any, 12345, 67890, 600);
expect(oidb.resolveUserUid).toHaveBeenCalledWith(bridge, 67890, 12345);
expect(bridge.resolveUserUid).toHaveBeenCalledWith(67890, 12345);
expect(oidb.sendOidbAndCheck).toHaveBeenCalledOnce();
const args = vi.mocked(oidb.sendOidbAndCheck).mock.calls[0]!;
expect(args[1]).toBe('OidbSvcTrpcTcp.0x1253_1');
@@ -53,7 +51,7 @@ describe('actions/group-admin', () => {
it('kickGroupMember resolves UID per-group and forwards reject + reason', async () => {
const bridge = mockBridge();
await admin.kickGroupMember(bridge as any, 12345, 67890, true, 'bad behaviour');
expect(oidb.resolveUserUid).toHaveBeenCalledWith(bridge, 67890, 12345);
expect(bridge.resolveUserUid).toHaveBeenCalledWith(67890, 12345);
expect(vi.mocked(oidb.sendOidbAndCheck).mock.calls[0]![4]).toMatchObject({
groupUin: 12345,
targetUid: 'resolved-uid',
@@ -64,12 +62,12 @@ describe('actions/group-admin', () => {
it('kickGroupMembers resolves each UID in parallel', async () => {
const bridge = mockBridge();
vi.mocked(oidb.resolveUserUid)
vi.mocked(bridge.resolveUserUid)
.mockResolvedValueOnce('uid-a')
.mockResolvedValueOnce('uid-b');
await admin.kickGroupMembers(bridge as any, 12345, [11, 22], false);
expect(oidb.resolveUserUid).toHaveBeenCalledTimes(2);
expect(bridge.resolveUserUid).toHaveBeenCalledTimes(2);
const payload: any = vi.mocked(oidb.sendOidbAndCheck).mock.calls[0]![4];
expect(payload.targetUids).toEqual(['uid-a', 'uid-b']);
expect(payload.rejectAddRequest).toBe(0);
@@ -3,7 +3,6 @@ import { describe, it, expect, vi, beforeEach } from 'vitest';
vi.mock('../../src/bridge/bridge-oidb', () => ({
sendOidbAndCheck: vi.fn(async () => undefined),
sendOidbAndDecode: vi.fn(async () => ({})),
resolveUserUid: vi.fn(async () => 'resolved-uid'),
makeOidbRequest: vi.fn(() => new Uint8Array(0)),
}));
@@ -30,7 +29,6 @@ describe('actions/group-file', () => {
beforeEach(() => {
vi.mocked(oidb.sendOidbAndCheck).mockClear();
vi.mocked(oidb.sendOidbAndDecode).mockClear();
vi.mocked(oidb.resolveUserUid).mockClear();
vi.mocked(highwayClient.fetchHighwaySession).mockClear();
vi.mocked(highwayClient.uploadHighwayHttp).mockClear();
});
@@ -104,7 +102,7 @@ describe('actions/group-file', () => {
const bridge = mockBridge({
qqInfo: { uin: '10001', selfUid: '', findGroupMember: vi.fn(() => null) },
});
vi.mocked(oidb.resolveUserUid)
vi.mocked(bridge.resolveUserUid)
.mockResolvedValueOnce('target-uid') // target user
.mockResolvedValueOnce('self-uid-resolved'); // self fallback
vi.mocked(oidb.sendOidbAndDecode).mockResolvedValueOnce({
@@ -112,7 +110,7 @@ describe('actions/group-file', () => {
});
const out = await groupFile.uploadPrivateFile(bridge as any, 67890, '/path/file');
expect(out).toEqual({ fileId: 'fid', fileHash: 'hash' });
expect(oidb.resolveUserUid).toHaveBeenCalledTimes(2);
expect(bridge.resolveUserUid).toHaveBeenCalledTimes(2);
});
it('fetchGroupFiles paginates files + folders out of OIDB items', async () => {
@@ -3,7 +3,6 @@ import { describe, it, expect, vi, beforeEach } from 'vitest';
vi.mock('../../src/bridge/bridge-oidb', () => ({
sendOidbAndCheck: vi.fn(async () => undefined),
sendOidbAndDecode: vi.fn(async () => ({})),
resolveUserUid: vi.fn(async () => 'resolved-uid'),
makeOidbRequest: vi.fn(() => new Uint8Array(0)),
}));
@@ -14,7 +13,6 @@ import { mockBridge } from './_helpers';
describe('actions/group-message', () => {
beforeEach(() => {
vi.mocked(oidb.sendOidbAndCheck).mockClear();
vi.mocked(oidb.resolveUserUid).mockClear();
});
it('recallGroupMessage sends to SsoGroupRecallMsg with the group sequence', async () => {
@@ -42,7 +40,7 @@ describe('actions/group-message', () => {
it('recallPrivateMessage resolves the target UID before dispatch', async () => {
const bridge = mockBridge();
await msg.recallPrivateMessage(bridge as any, 10001, 100, 200, 123, 1700000000);
expect(oidb.resolveUserUid).toHaveBeenCalledWith(bridge, 10001);
expect(bridge.resolveUserUid).toHaveBeenCalledWith(10001);
const [serviceCmd] = bridge.sendRawPacket.mock.calls[0]!;
expect(serviceCmd).toBe('trpc.msg.msg_svc.MsgService.SsoC2CRecallMsg');
});
@@ -3,7 +3,6 @@ import { describe, it, expect, vi, beforeEach } from 'vitest';
vi.mock('../../src/bridge/bridge-oidb', () => ({
sendOidbAndCheck: vi.fn(async () => undefined),
sendOidbAndDecode: vi.fn(async () => ({})),
resolveUserUid: vi.fn(async () => 'resolved-uid'),
makeOidbRequest: vi.fn(() => new Uint8Array(0)),
}));
-1
View File
@@ -3,7 +3,6 @@ import { describe, it, expect, vi, beforeEach } from 'vitest';
vi.mock('../../src/bridge/bridge-oidb', () => ({
sendOidbAndCheck: vi.fn(async () => undefined),
sendOidbAndDecode: vi.fn(async () => ({})),
resolveUserUid: vi.fn(async () => 'resolved-uid'),
makeOidbRequest: vi.fn(() => new Uint8Array(0)),
}));
+1 -3
View File
@@ -3,7 +3,6 @@ import { describe, it, expect, vi, beforeEach } from 'vitest';
vi.mock('../../src/bridge/bridge-oidb', () => ({
sendOidbAndCheck: vi.fn(async () => undefined),
sendOidbAndDecode: vi.fn(async () => ({})),
resolveUserUid: vi.fn(async () => 'resolved-uid'),
makeOidbRequest: vi.fn(() => new Uint8Array(0)),
}));
@@ -27,7 +26,6 @@ describe('actions/profile', () => {
beforeEach(() => {
vi.mocked(oidb.sendOidbAndCheck).mockClear();
vi.mocked(oidb.sendOidbAndDecode).mockClear();
vi.mocked(oidb.resolveUserUid).mockClear();
vi.mocked(highwayClient.fetchHighwaySession).mockClear();
vi.mocked(highwayClient.uploadHighwayHttp).mockClear();
});
@@ -63,7 +61,7 @@ describe('actions/profile', () => {
it('setInputStatus resolves UID first and sends 0xcd4_1', async () => {
const bridge = mockBridge();
await profile.setInputStatus(bridge as any, 10001, 1);
expect(oidb.resolveUserUid).toHaveBeenCalledWith(bridge, 10001);
expect(bridge.resolveUserUid).toHaveBeenCalledWith(10001);
const call = vi.mocked(oidb.sendOidbAndDecode).mock.calls[0]!;
expect(call[1]).toBe('OidbSvcTrpcTcp.0xcd4_1');
});