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 { 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<LikeInfo> {
|
||||
const info = await 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;
|
||||
getLike(userId?: number, start = 0, limit = 10): Promise<LikeInfo> {
|
||||
return GetLike.invoke(this.ctx, { userId, start, limit });
|
||||
}
|
||||
|
||||
getUnidirectionalFriendList(): Promise<UnidirectionalFriendEntry[]> {
|
||||
|
||||
@@ -127,6 +127,7 @@ export interface MockBridge {
|
||||
nickname: string;
|
||||
findUidByUin: ReturnType<typeof vi.fn>;
|
||||
findUinByUid: ReturnType<typeof vi.fn>;
|
||||
resolveUin: ReturnType<typeof vi.fn>;
|
||||
findGroupMember: ReturnType<typeof vi.fn>;
|
||||
updateFriendRemark: ReturnType<typeof vi.fn>;
|
||||
updateGroupRemark: ReturnType<typeof vi.fn>;
|
||||
@@ -161,6 +162,7 @@ export function mockBridge(overrides: Partial<MockBridge> = {}): 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),
|
||||
|
||||
@@ -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<OidbBase<Oidb0x7edResp>>({
|
||||
@@ -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 () => {
|
||||
|
||||
Reference in new issue
Block a user