refactor(protocol): resolve like-list UINs through Identity
GetLike already filled cached UINs in deserialize. ProfileApi then opened a second Contacts hop and threw on fetch failure. Misses now go through Identity.resolveUin after invokeOidb, coalesced by uid. ProfileApi.getLike is invoke-only. Unresolved rows stay uin=0 and no longer fail the action.
This commit is contained in:
5 files changed
+89
-48
No files matched your search
@@ -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 { SetProfile } from '@snowluma/protocol/oidb-services/profile/set-profile';
|
||||||
import { SetSelfLongNick } from '@snowluma/protocol/oidb-services/profile/set-self-long-nick';
|
import { SetSelfLongNick } from '@snowluma/protocol/oidb-services/profile/set-self-long-nick';
|
||||||
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
|
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
|
||||||
import { createLogger } from '@snowluma/common/logger';
|
|
||||||
import type { Bridge } from '../bridge';
|
import type { Bridge } from '../bridge';
|
||||||
import type { BridgeContext } from '../bridge-context';
|
import type { BridgeContext } from '../bridge-context';
|
||||||
|
|
||||||
const log = createLogger('Bridge.Profile');
|
|
||||||
|
|
||||||
function asBridge(ctx: BridgeContext): Bridge { return ctx as unknown as Bridge; }
|
function asBridge(ctx: BridgeContext): Bridge { return ctx as unknown as Bridge; }
|
||||||
|
|
||||||
export interface CustomFaceDetail {
|
export interface CustomFaceDetail {
|
||||||
@@ -136,37 +133,8 @@ export class ProfileApi {
|
|||||||
|
|
||||||
// ─────────────── queries on me / my contacts ───────────────
|
// ─────────────── queries on me / my contacts ───────────────
|
||||||
|
|
||||||
async getLike(userId?: number, start = 0, limit = 10): Promise<LikeInfo> {
|
getLike(userId?: number, start = 0, limit = 10): Promise<LikeInfo> {
|
||||||
const info = await GetLike.invoke(this.ctx, { userId, start, limit });
|
return GetLike.invoke(this.ctx, { userId, start, limit });
|
||||||
const users = [...info.favoriteInfo.userInfos, ...info.voteInfo.userInfos];
|
|
||||||
const unresolved = new Map<string, typeof users>();
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getUnidirectionalFriendList(): Promise<UnidirectionalFriendEntry[]> {
|
getUnidirectionalFriendList(): Promise<UnidirectionalFriendEntry[]> {
|
||||||
|
|||||||
@@ -127,6 +127,7 @@ export interface MockBridge {
|
|||||||
nickname: string;
|
nickname: string;
|
||||||
findUidByUin: ReturnType<typeof vi.fn>;
|
findUidByUin: ReturnType<typeof vi.fn>;
|
||||||
findUinByUid: ReturnType<typeof vi.fn>;
|
findUinByUid: ReturnType<typeof vi.fn>;
|
||||||
|
resolveUin: ReturnType<typeof vi.fn>;
|
||||||
findGroupMember: ReturnType<typeof vi.fn>;
|
findGroupMember: ReturnType<typeof vi.fn>;
|
||||||
updateFriendRemark: ReturnType<typeof vi.fn>;
|
updateFriendRemark: ReturnType<typeof vi.fn>;
|
||||||
updateGroupRemark: ReturnType<typeof vi.fn>;
|
updateGroupRemark: ReturnType<typeof vi.fn>;
|
||||||
@@ -161,6 +162,7 @@ export function mockBridge(overrides: Partial<MockBridge> = {}): MockBridge {
|
|||||||
nickname: 'self-nick',
|
nickname: 'self-nick',
|
||||||
findUidByUin: vi.fn(() => 'cached-uid'),
|
findUidByUin: vi.fn(() => 'cached-uid'),
|
||||||
findUinByUid: vi.fn(() => 0),
|
findUinByUid: vi.fn(() => 0),
|
||||||
|
resolveUin: vi.fn(async () => null),
|
||||||
findGroupMember: vi.fn(() => null),
|
findGroupMember: vi.fn(() => null),
|
||||||
updateFriendRemark: vi.fn(() => true),
|
updateFriendRemark: vi.fn(() => true),
|
||||||
updateGroupRemark: vi.fn(() => true),
|
updateGroupRemark: vi.fn(() => true),
|
||||||
|
|||||||
@@ -213,14 +213,13 @@ describe('apis/profile', () => {
|
|||||||
await expect(new ProfileApi(bridge as any).getLike()).rejects.toThrow(/empty/);
|
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();
|
const bridge = mockBridge();
|
||||||
bridge.identity.findUinByUid.mockReturnValue(null);
|
bridge.identity.findUinByUid.mockReturnValue(null);
|
||||||
const fetchUserProfileByUid = vi.fn(async (uid: string) => ({
|
bridge.identity.resolveUin.mockImplementation(async (uid: string) => (
|
||||||
uid,
|
uid === 'stranger-uid' ? 30003 : null
|
||||||
uin: 30003,
|
));
|
||||||
nickname: '陌生点赞者',
|
const fetchUserProfileByUid = vi.fn();
|
||||||
}));
|
|
||||||
(bridge.apis.contacts as any).fetchUserProfileByUid = fetchUserProfileByUid;
|
(bridge.apis.contacts as any).fetchUserProfileByUid = fetchUserProfileByUid;
|
||||||
bridge.sendRawPacket.mockResolvedValueOnce({
|
bridge.sendRawPacket.mockResolvedValueOnce({
|
||||||
success: true, gotResponse: true, errorCode: 0, errorMessage: '',
|
success: true, gotResponse: true, errorCode: 0, errorMessage: '',
|
||||||
@@ -236,17 +235,20 @@ describe('apis/profile', () => {
|
|||||||
|
|
||||||
const out = await new ProfileApi(bridge as any).getLike();
|
const out = await new ProfileApi(bridge as any).getLike();
|
||||||
|
|
||||||
expect(fetchUserProfileByUid).toHaveBeenCalledOnce();
|
expect(bridge.identity.resolveUin).toHaveBeenCalledOnce();
|
||||||
expect(fetchUserProfileByUid).toHaveBeenCalledWith('stranger-uid');
|
expect(bridge.identity.resolveUin).toHaveBeenCalledWith('stranger-uid');
|
||||||
|
expect(fetchUserProfileByUid).not.toHaveBeenCalled();
|
||||||
expect(out.voteInfo.userInfos[0]?.uin).toBe(30003);
|
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();
|
const bridge = mockBridge();
|
||||||
bridge.identity.findUinByUid.mockReturnValue(null);
|
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');
|
throw new Error('profile lookup failed');
|
||||||
});
|
});
|
||||||
|
(bridge.apis.contacts as any).fetchUserProfileByUid = fetchUserProfileByUid;
|
||||||
bridge.sendRawPacket.mockResolvedValueOnce({
|
bridge.sendRawPacket.mockResolvedValueOnce({
|
||||||
success: true, gotResponse: true, errorCode: 0, errorMessage: '',
|
success: true, gotResponse: true, errorCode: 0, errorMessage: '',
|
||||||
responseData: Buffer.from(protobuf_encode<OidbBase<Oidb0x7edResp>>({
|
responseData: Buffer.from(protobuf_encode<OidbBase<Oidb0x7edResp>>({
|
||||||
@@ -259,8 +261,10 @@ describe('apis/profile', () => {
|
|||||||
})),
|
})),
|
||||||
});
|
});
|
||||||
|
|
||||||
await expect(new ProfileApi(bridge as any).getLike())
|
const out = await new ProfileApi(bridge as any).getLike();
|
||||||
.rejects.toThrow('profile lookup failed');
|
|
||||||
|
expect(out.voteInfo.userInfos[0]?.uin).toBe(0);
|
||||||
|
expect(fetchUserProfileByUid).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('getUnidirectionalFriendList parses the embedded JSON body', async () => {
|
it('getUnidirectionalFriendList parses the embedded JSON body', async () => {
|
||||||
|
|||||||
@@ -146,6 +146,27 @@ export namespace GetLike {
|
|||||||
export const decode = (bytes: Uint8Array): OidbBase<Oidb0x7edResp> =>
|
export const decode = (bytes: Uint8Array): OidbBase<Oidb0x7edResp> =>
|
||||||
protobuf_decode<OidbBase<Oidb0x7edResp>>(bytes);
|
protobuf_decode<OidbBase<Oidb0x7edResp>>(bytes);
|
||||||
|
|
||||||
export const invoke = (deps: Deps, params: Params): Promise<LikeInfo> =>
|
export const invoke = async (deps: Deps, params: Params): Promise<LikeInfo> => {
|
||||||
invokeOidb(deps, GetLike, params);
|
const info = await invokeOidb(deps, GetLike, params);
|
||||||
|
await fillUnresolvedLikeUins(deps, info);
|
||||||
|
return info;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async function fillUnresolvedLikeUins(ctx: GetLike.Deps, info: LikeInfo): Promise<void> {
|
||||||
|
const users = [...info.favoriteInfo.userInfos, ...info.voteInfo.userInfos];
|
||||||
|
const unresolved = new Map<string, ProfileLikeUserInfo[]>();
|
||||||
|
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;
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
@@ -10,6 +10,7 @@ function makeDeps(opts: {
|
|||||||
cachedSelfUid?: string | null;
|
cachedSelfUid?: string | null;
|
||||||
resolveUserUid?: (uin: number) => Promise<string>;
|
resolveUserUid?: (uin: number) => Promise<string>;
|
||||||
findUinByUid?: (uid: string) => number | null;
|
findUinByUid?: (uid: string) => number | null;
|
||||||
|
resolveUin?: (uid: string) => Promise<number | null>;
|
||||||
responseBody?: Oidb0x7edResp;
|
responseBody?: Oidb0x7edResp;
|
||||||
} = {}) {
|
} = {}) {
|
||||||
const responseData = opts.responseBody !== undefined
|
const responseData = opts.responseBody !== undefined
|
||||||
@@ -22,6 +23,7 @@ function makeDeps(opts: {
|
|||||||
uin: '10001',
|
uin: '10001',
|
||||||
selfUid: opts.cachedSelfUid ?? null,
|
selfUid: opts.cachedSelfUid ?? null,
|
||||||
findUinByUid: vi.fn(opts.findUinByUid ?? (() => null)),
|
findUinByUid: vi.fn(opts.findUinByUid ?? (() => null)),
|
||||||
|
resolveUin: vi.fn(opts.resolveUin ?? (async () => null)),
|
||||||
} as any,
|
} as any,
|
||||||
resolveUserUid: vi.fn(opts.resolveUserUid ?? (async (uin: number) => `uid-of-${uin}`)),
|
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);
|
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);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
Reference in new issue
Block a user