diff --git a/packages/core/src/bridge/apis/profile.ts b/packages/core/src/bridge/apis/profile.ts index 1ac48ca1..d848afb8 100644 --- a/packages/core/src/bridge/apis/profile.ts +++ b/packages/core/src/bridge/apis/profile.ts @@ -19,12 +19,9 @@ import { SetInputStatus } from '@snowluma/protocol/oidb-services/profile/set-inp import { SetProfile } from '@snowluma/protocol/oidb-services/profile/set-profile'; import { SetSelfLongNick } from '@snowluma/protocol/oidb-services/profile/set-self-long-nick'; import { protobuf_decode, protobuf_encode } from '@snowluma/proton'; -import { createLogger } from '@snowluma/common/logger'; import type { Bridge } from '../bridge'; import type { BridgeContext } from '../bridge-context'; -const log = createLogger('Bridge.Profile'); - function asBridge(ctx: BridgeContext): Bridge { return ctx as unknown as Bridge; } export interface CustomFaceDetail { @@ -136,37 +133,8 @@ export class ProfileApi { // ─────────────── queries on me / my contacts ─────────────── - async getLike(userId?: number, start = 0, limit = 10): Promise { - const info = await GetLike.invoke(this.ctx, { userId, start, limit }); - const users = [...info.favoriteInfo.userInfos, ...info.voteInfo.userInfos]; - const unresolved = new Map(); - - for (const user of users) { - if (user.uin > 0) continue; - const sameUid = unresolved.get(user.uid) ?? []; - sameUid.push(user); - unresolved.set(user.uid, sameUid); - } - - await Promise.all([...unresolved].map(async ([uid, sameUid]) => { - try { - const profile = await this.ctx.apis.contacts.fetchUserProfileByUid(uid); - if (profile.uin <= 0) { - log.warn('profile-like user resolution returned no uin: uid=%s', uid); - return; - } - for (const user of sameUid) user.uin = profile.uin; - } catch (error) { - log.error( - 'profile-like user resolution failed: uid=%s err=%s', - uid, - error instanceof Error ? error.message : String(error), - ); - throw error; - } - })); - - return info; + getLike(userId?: number, start = 0, limit = 10): Promise { + return GetLike.invoke(this.ctx, { userId, start, limit }); } getUnidirectionalFriendList(): Promise { diff --git a/packages/core/tests/actions/_helpers.ts b/packages/core/tests/actions/_helpers.ts index dbdd9a77..cb642552 100644 --- a/packages/core/tests/actions/_helpers.ts +++ b/packages/core/tests/actions/_helpers.ts @@ -127,6 +127,7 @@ export interface MockBridge { nickname: string; findUidByUin: ReturnType; findUinByUid: ReturnType; + resolveUin: ReturnType; findGroupMember: ReturnType; updateFriendRemark: ReturnType; updateGroupRemark: ReturnType; @@ -161,6 +162,7 @@ export function mockBridge(overrides: Partial = {}): MockBridge { nickname: 'self-nick', findUidByUin: vi.fn(() => 'cached-uid'), findUinByUid: vi.fn(() => 0), + resolveUin: vi.fn(async () => null), findGroupMember: vi.fn(() => null), updateFriendRemark: vi.fn(() => true), updateGroupRemark: vi.fn(() => true), diff --git a/packages/core/tests/actions/profile.test.ts b/packages/core/tests/actions/profile.test.ts index 598cc737..058ce950 100644 --- a/packages/core/tests/actions/profile.test.ts +++ b/packages/core/tests/actions/profile.test.ts @@ -213,14 +213,13 @@ describe('apis/profile', () => { await expect(new ProfileApi(bridge as any).getLike()).rejects.toThrow(/empty/); }); - it('getProfileLike resolves uncached liker uins from their profiles', async () => { + it('getProfileLike resolves uncached liker uins through Identity.resolveUin', async () => { const bridge = mockBridge(); bridge.identity.findUinByUid.mockReturnValue(null); - const fetchUserProfileByUid = vi.fn(async (uid: string) => ({ - uid, - uin: 30003, - nickname: '陌生点赞者', - })); + bridge.identity.resolveUin.mockImplementation(async (uid: string) => ( + uid === 'stranger-uid' ? 30003 : null + )); + const fetchUserProfileByUid = vi.fn(); (bridge.apis.contacts as any).fetchUserProfileByUid = fetchUserProfileByUid; bridge.sendRawPacket.mockResolvedValueOnce({ success: true, gotResponse: true, errorCode: 0, errorMessage: '', @@ -236,17 +235,20 @@ describe('apis/profile', () => { const out = await new ProfileApi(bridge as any).getLike(); - expect(fetchUserProfileByUid).toHaveBeenCalledOnce(); - expect(fetchUserProfileByUid).toHaveBeenCalledWith('stranger-uid'); + expect(bridge.identity.resolveUin).toHaveBeenCalledOnce(); + expect(bridge.identity.resolveUin).toHaveBeenCalledWith('stranger-uid'); + expect(fetchUserProfileByUid).not.toHaveBeenCalled(); expect(out.voteInfo.userInfos[0]?.uin).toBe(30003); }); - it('getProfileLike surfaces failures while resolving uncached liker uins', async () => { + it('getProfileLike keeps uin=0 when Identity.resolveUin misses', async () => { const bridge = mockBridge(); bridge.identity.findUinByUid.mockReturnValue(null); - (bridge.apis.contacts as any).fetchUserProfileByUid = vi.fn(async () => { + bridge.identity.resolveUin.mockResolvedValue(null); + const fetchUserProfileByUid = vi.fn(async () => { throw new Error('profile lookup failed'); }); + (bridge.apis.contacts as any).fetchUserProfileByUid = fetchUserProfileByUid; bridge.sendRawPacket.mockResolvedValueOnce({ success: true, gotResponse: true, errorCode: 0, errorMessage: '', responseData: Buffer.from(protobuf_encode>({ @@ -259,8 +261,10 @@ describe('apis/profile', () => { })), }); - await expect(new ProfileApi(bridge as any).getLike()) - .rejects.toThrow('profile lookup failed'); + const out = await new ProfileApi(bridge as any).getLike(); + + expect(out.voteInfo.userInfos[0]?.uin).toBe(0); + expect(fetchUserProfileByUid).not.toHaveBeenCalled(); }); it('getUnidirectionalFriendList parses the embedded JSON body', async () => { diff --git a/packages/protocol/src/oidb-services/profile/get-like.ts b/packages/protocol/src/oidb-services/profile/get-like.ts index 84ef4163..f5d8a1d8 100644 --- a/packages/protocol/src/oidb-services/profile/get-like.ts +++ b/packages/protocol/src/oidb-services/profile/get-like.ts @@ -146,6 +146,27 @@ export namespace GetLike { export const decode = (bytes: Uint8Array): OidbBase => protobuf_decode>(bytes); - export const invoke = (deps: Deps, params: Params): Promise => - invokeOidb(deps, GetLike, params); + export const invoke = async (deps: Deps, params: Params): Promise => { + const info = await invokeOidb(deps, GetLike, params); + await fillUnresolvedLikeUins(deps, info); + return info; + }; +} + +async function fillUnresolvedLikeUins(ctx: GetLike.Deps, info: LikeInfo): Promise { + const users = [...info.favoriteInfo.userInfos, ...info.voteInfo.userInfos]; + const unresolved = new Map(); + for (const user of users) { + if (user.uin > 0) continue; + const sameUid = unresolved.get(user.uid) ?? []; + sameUid.push(user); + unresolved.set(user.uid, sameUid); + } + if (unresolved.size === 0) return; + + await Promise.all([...unresolved].map(async ([uid, sameUid]) => { + const uin = await ctx.identity.resolveUin(uid); + if (uin === null || uin <= 0) return; + for (const user of sameUid) user.uin = uin; + })); } diff --git a/packages/protocol/tests/oidb-services/profile/get-like.test.ts b/packages/protocol/tests/oidb-services/profile/get-like.test.ts index d394d8f0..0fecfcad 100644 --- a/packages/protocol/tests/oidb-services/profile/get-like.test.ts +++ b/packages/protocol/tests/oidb-services/profile/get-like.test.ts @@ -10,6 +10,7 @@ function makeDeps(opts: { cachedSelfUid?: string | null; resolveUserUid?: (uin: number) => Promise; findUinByUid?: (uid: string) => number | null; + resolveUin?: (uid: string) => Promise; responseBody?: Oidb0x7edResp; } = {}) { const responseData = opts.responseBody !== undefined @@ -22,6 +23,7 @@ function makeDeps(opts: { uin: '10001', selfUid: opts.cachedSelfUid ?? null, findUinByUid: vi.fn(opts.findUinByUid ?? (() => null)), + resolveUin: vi.fn(opts.resolveUin ?? (async () => null)), } as any, resolveUserUid: vi.fn(opts.resolveUserUid ?? (async (uin: number) => `uid-of-${uin}`)), }; @@ -214,4 +216,48 @@ describe('GetLike namespace', () => { expect(out.voteInfo.total_count).toBe(0); }); }); + + describe('invoke (inbound UIN fill)', () => { + it('fills leftover uin=0 rows through Identity.resolveUin once per uid', async () => { + const deps = makeDeps({ + cachedSelfUid: 'self-uid', + findUinByUid: () => null, + resolveUin: async (uid) => uid === 'shared-uid' ? 30003 : null, + responseBody: { + userLikeInfos: [{ + uid: 'self-uid', + favoriteInfo: { userInfos: [{ uid: 'shared-uid', nick: '收藏' }] }, + voteInfo: { userInfos: [{ uid: 'shared-uid', nick: '点赞' }, { uid: 'miss-uid' }] }, + }], + }, + }); + + const out = await GetLike.invoke(deps, {}); + + expect(deps.identity.resolveUin).toHaveBeenCalledTimes(2); + expect(deps.identity.resolveUin).toHaveBeenCalledWith('shared-uid'); + expect(deps.identity.resolveUin).toHaveBeenCalledWith('miss-uid'); + expect(out.favoriteInfo.userInfos[0]?.uin).toBe(30003); + expect(out.voteInfo.userInfos[0]?.uin).toBe(30003); + expect(out.voteInfo.userInfos[1]?.uin).toBe(0); + }); + + it('does not call resolveUin when deserialize already filled the cache hit', async () => { + const deps = makeDeps({ + cachedSelfUid: 'self-uid', + findUinByUid: (uid) => uid === 'cached-uid' ? 20002 : null, + responseBody: { + userLikeInfos: [{ + uid: 'self-uid', + voteInfo: { userInfos: [{ uid: 'cached-uid', nick: '点赞者' }] }, + }], + }, + }); + + const out = await GetLike.invoke(deps, {}); + + expect(deps.identity.resolveUin).not.toHaveBeenCalled(); + expect(out.voteInfo.userInfos[0]?.uin).toBe(20002); + }); + }); });