diff --git a/packages/core/src/bridge/apis/profile.ts b/packages/core/src/bridge/apis/profile.ts index d848afb8..1ac48ca1 100644 --- a/packages/core/src/bridge/apis/profile.ts +++ b/packages/core/src/bridge/apis/profile.ts @@ -19,9 +19,12 @@ 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 { @@ -133,8 +136,37 @@ export class ProfileApi { // ─────────────── queries on me / my contacts ─────────────── - getLike(userId?: number, start = 0, limit = 10): Promise { - return GetLike.invoke(this.ctx, { userId, start, limit }); + 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; } getUnidirectionalFriendList(): Promise { diff --git a/packages/core/tests/actions/profile.test.ts b/packages/core/tests/actions/profile.test.ts index fa95515e..598cc737 100644 --- a/packages/core/tests/actions/profile.test.ts +++ b/packages/core/tests/actions/profile.test.ts @@ -173,22 +173,35 @@ describe('apis/profile', () => { it('getProfileLike (self): resolves self UID, returns formatted favorite + vote info', async () => { const bridge = mockBridge(); + bridge.identity.findUinByUid.mockImplementation( + (uid: string) => uid === 'liker-uid' ? 20002 : 0, + ); bridge.sendRawPacket.mockResolvedValueOnce({ success: true, gotResponse: true, errorCode: 0, errorMessage: '', responseData: Buffer.from(protobuf_encode>({ body: { userLikeInfos: [{ uid: 'u', - time: 1700000000n, - favoriteInfo: { totalCount: 5, lastTime: 1n, newCount: 1 }, - voteInfo: { totalCount: 7, newCount: 2, lastTime: 2n }, + time: 1700000000, + favoriteInfo: { totalCount: 5, lastTime: 1, todayCount: 1 }, + voteInfo: { + totalCount: 7, + newCount: 2, + newNearbyCount: 1, + lastVisitTime: 2, + userInfos: [{ uid: 'liker-uid', nick: '点赞者', count: 3 }], + }, }], } as any, })), }); const out = await new ProfileApi(bridge as any).getLike(); + expect(bridge.sendRawPacket.mock.calls[0]![0]).toBe('OidbSvcTrpcTcp.0x7ed_13'); expect(out.favoriteInfo.total_count).toBe(5); expect(out.voteInfo.total_count).toBe(7); + expect(out.voteInfo.userInfos).toEqual([ + expect.objectContaining({ uid: 'liker-uid', uin: 20002, nick: '点赞者', count: 3 }), + ]); }); it('getProfileLike throws on empty result', async () => { @@ -200,6 +213,56 @@ describe('apis/profile', () => { await expect(new ProfileApi(bridge as any).getLike()).rejects.toThrow(/empty/); }); + it('getProfileLike resolves uncached liker uins from their profiles', async () => { + const bridge = mockBridge(); + bridge.identity.findUinByUid.mockReturnValue(null); + const fetchUserProfileByUid = vi.fn(async (uid: string) => ({ + uid, + uin: 30003, + nickname: '陌生点赞者', + })); + (bridge.apis.contacts as any).fetchUserProfileByUid = fetchUserProfileByUid; + bridge.sendRawPacket.mockResolvedValueOnce({ + success: true, gotResponse: true, errorCode: 0, errorMessage: '', + responseData: Buffer.from(protobuf_encode>({ + body: { + userLikeInfos: [{ + uid: 'self-uid', + voteInfo: { userInfos: [{ uid: 'stranger-uid', nick: '陌生点赞者' }] }, + }], + }, + })), + }); + + const out = await new ProfileApi(bridge as any).getLike(); + + expect(fetchUserProfileByUid).toHaveBeenCalledOnce(); + expect(fetchUserProfileByUid).toHaveBeenCalledWith('stranger-uid'); + expect(out.voteInfo.userInfos[0]?.uin).toBe(30003); + }); + + it('getProfileLike surfaces failures while resolving uncached liker uins', async () => { + const bridge = mockBridge(); + bridge.identity.findUinByUid.mockReturnValue(null); + (bridge.apis.contacts as any).fetchUserProfileByUid = vi.fn(async () => { + throw new Error('profile lookup failed'); + }); + bridge.sendRawPacket.mockResolvedValueOnce({ + success: true, gotResponse: true, errorCode: 0, errorMessage: '', + responseData: Buffer.from(protobuf_encode>({ + body: { + userLikeInfos: [{ + uid: 'self-uid', + voteInfo: { userInfos: [{ uid: 'stranger-uid' }] }, + }], + }, + })), + }); + + await expect(new ProfileApi(bridge as any).getLike()) + .rejects.toThrow('profile lookup failed'); + }); + it('getUnidirectionalFriendList parses the embedded JSON body', async () => { const bridge = mockBridge(); bridge.sendRawPacket.mockResolvedValueOnce({ diff --git a/packages/mcp/src/generated/catalog.json b/packages/mcp/src/generated/catalog.json index ac14206c..be46612f 100644 --- a/packages/mcp/src/generated/catalog.json +++ b/packages/mcp/src/generated/catalog.json @@ -6172,7 +6172,7 @@ "name": "get_profile_like", "aliases": [], "summary": "获取资料点赞", - "returns": "点赞资料:uid、最近点赞时间、收藏(favoriteInfo)与点赞(voteInfo)统计。", + "returns": "点赞资料:uid、最近点赞时间、收藏与点赞统计及用户明细。", "returnsSchema": { "type": "object", "properties": { @@ -6202,7 +6202,94 @@ }, "userInfos": { "type": "array", - "description": "用户列表(恒空)" + "description": "收藏用户列表", + "items": { + "type": "object", + "properties": { + "uid": { + "type": "string", + "description": "用户 uid" + }, + "uin": { + "type": "integer", + "description": "用户 QQ 号;资料中没有有效号码时为 0" + }, + "src": { + "type": "integer", + "description": "来源类型" + }, + "latestTime": { + "type": "integer", + "description": "最近互动时间戳" + }, + "count": { + "type": "integer", + "description": "互动次数" + }, + "giftCount": { + "type": "integer", + "description": "礼物数量" + }, + "customId": { + "type": "integer", + "description": "自定义标识" + }, + "lastCharged": { + "type": "integer", + "description": "最近充能时间" + }, + "bAvailableCnt": { + "type": "integer", + "description": "可用次数" + }, + "bTodayVotedCnt": { + "type": "integer", + "description": "今日已点赞次数" + }, + "nick": { + "type": "string", + "description": "昵称" + }, + "gender": { + "type": "integer", + "description": "性别" + }, + "age": { + "type": "integer", + "description": "年龄" + }, + "isFriend": { + "type": "boolean", + "description": "是否为好友" + }, + "isvip": { + "type": "boolean", + "description": "是否为会员" + }, + "isSvip": { + "type": "boolean", + "description": "是否为超级会员" + } + }, + "required": [ + "uid", + "uin", + "src", + "latestTime", + "count", + "giftCount", + "customId", + "lastCharged", + "bAvailableCnt", + "bTodayVotedCnt", + "nick", + "gender", + "age", + "isFriend", + "isvip", + "isSvip" + ] + } } } }, @@ -6228,7 +6315,94 @@ }, "userInfos": { "type": "array", - "description": "用户列表(恒空)" + "description": "点赞用户列表", + "items": { + "type": "object", + "properties": { + "uid": { + "type": "string", + "description": "用户 uid" + }, + "uin": { + "type": "integer", + "description": "用户 QQ 号;资料中没有有效号码时为 0" + }, + "src": { + "type": "integer", + "description": "来源类型" + }, + "latestTime": { + "type": "integer", + "description": "最近互动时间戳" + }, + "count": { + "type": "integer", + "description": "互动次数" + }, + "giftCount": { + "type": "integer", + "description": "礼物数量" + }, + "customId": { + "type": "integer", + "description": "自定义标识" + }, + "lastCharged": { + "type": "integer", + "description": "最近充能时间" + }, + "bAvailableCnt": { + "type": "integer", + "description": "可用次数" + }, + "bTodayVotedCnt": { + "type": "integer", + "description": "今日已点赞次数" + }, + "nick": { + "type": "string", + "description": "昵称" + }, + "gender": { + "type": "integer", + "description": "性别" + }, + "age": { + "type": "integer", + "description": "年龄" + }, + "isFriend": { + "type": "boolean", + "description": "是否为好友" + }, + "isvip": { + "type": "boolean", + "description": "是否为会员" + }, + "isSvip": { + "type": "boolean", + "description": "是否为超级会员" + } + }, + "required": [ + "uid", + "uin", + "src", + "latestTime", + "count", + "giftCount", + "customId", + "lastCharged", + "bAvailableCnt", + "bTodayVotedCnt", + "nick", + "gender", + "age", + "isFriend", + "isvip", + "isSvip" + ] + } } } } diff --git a/packages/mcp/src/generated/catalog.ts b/packages/mcp/src/generated/catalog.ts index c1bd818a..62e9ca0b 100644 --- a/packages/mcp/src/generated/catalog.ts +++ b/packages/mcp/src/generated/catalog.ts @@ -6175,7 +6175,7 @@ export const ACTIONS: CatalogAction[] = [ "name": "get_profile_like", "aliases": [], "summary": "获取资料点赞", - "returns": "点赞资料:uid、最近点赞时间、收藏(favoriteInfo)与点赞(voteInfo)统计。", + "returns": "点赞资料:uid、最近点赞时间、收藏与点赞统计及用户明细。", "returnsSchema": { "type": "object", "properties": { @@ -6205,7 +6205,94 @@ export const ACTIONS: CatalogAction[] = [ }, "userInfos": { "type": "array", - "description": "用户列表(恒空)" + "description": "收藏用户列表", + "items": { + "type": "object", + "properties": { + "uid": { + "type": "string", + "description": "用户 uid" + }, + "uin": { + "type": "integer", + "description": "用户 QQ 号;资料中没有有效号码时为 0" + }, + "src": { + "type": "integer", + "description": "来源类型" + }, + "latestTime": { + "type": "integer", + "description": "最近互动时间戳" + }, + "count": { + "type": "integer", + "description": "互动次数" + }, + "giftCount": { + "type": "integer", + "description": "礼物数量" + }, + "customId": { + "type": "integer", + "description": "自定义标识" + }, + "lastCharged": { + "type": "integer", + "description": "最近充能时间" + }, + "bAvailableCnt": { + "type": "integer", + "description": "可用次数" + }, + "bTodayVotedCnt": { + "type": "integer", + "description": "今日已点赞次数" + }, + "nick": { + "type": "string", + "description": "昵称" + }, + "gender": { + "type": "integer", + "description": "性别" + }, + "age": { + "type": "integer", + "description": "年龄" + }, + "isFriend": { + "type": "boolean", + "description": "是否为好友" + }, + "isvip": { + "type": "boolean", + "description": "是否为会员" + }, + "isSvip": { + "type": "boolean", + "description": "是否为超级会员" + } + }, + "required": [ + "uid", + "uin", + "src", + "latestTime", + "count", + "giftCount", + "customId", + "lastCharged", + "bAvailableCnt", + "bTodayVotedCnt", + "nick", + "gender", + "age", + "isFriend", + "isvip", + "isSvip" + ] + } } } }, @@ -6231,7 +6318,94 @@ export const ACTIONS: CatalogAction[] = [ }, "userInfos": { "type": "array", - "description": "用户列表(恒空)" + "description": "点赞用户列表", + "items": { + "type": "object", + "properties": { + "uid": { + "type": "string", + "description": "用户 uid" + }, + "uin": { + "type": "integer", + "description": "用户 QQ 号;资料中没有有效号码时为 0" + }, + "src": { + "type": "integer", + "description": "来源类型" + }, + "latestTime": { + "type": "integer", + "description": "最近互动时间戳" + }, + "count": { + "type": "integer", + "description": "互动次数" + }, + "giftCount": { + "type": "integer", + "description": "礼物数量" + }, + "customId": { + "type": "integer", + "description": "自定义标识" + }, + "lastCharged": { + "type": "integer", + "description": "最近充能时间" + }, + "bAvailableCnt": { + "type": "integer", + "description": "可用次数" + }, + "bTodayVotedCnt": { + "type": "integer", + "description": "今日已点赞次数" + }, + "nick": { + "type": "string", + "description": "昵称" + }, + "gender": { + "type": "integer", + "description": "性别" + }, + "age": { + "type": "integer", + "description": "年龄" + }, + "isFriend": { + "type": "boolean", + "description": "是否为好友" + }, + "isvip": { + "type": "boolean", + "description": "是否为会员" + }, + "isSvip": { + "type": "boolean", + "description": "是否为超级会员" + } + }, + "required": [ + "uid", + "uin", + "src", + "latestTime", + "count", + "giftCount", + "customId", + "lastCharged", + "bAvailableCnt", + "bTodayVotedCnt", + "nick", + "gender", + "age", + "isFriend", + "isvip", + "isSvip" + ] + } } } } diff --git a/packages/onebot/src/actions/extended.ts b/packages/onebot/src/actions/extended.ts index 823dda68..048a568e 100644 --- a/packages/onebot/src/actions/extended.ts +++ b/packages/onebot/src/actions/extended.ts @@ -23,6 +23,33 @@ import { GROUP_MESSAGE_EVENT, hashMessageIdInt32 } from '../message-id'; const DOWNLOAD_FILE_MAX_BYTES = 1024 * 1024 * 1024; // 1 GiB const DOWNLOAD_FILE_TIMEOUT_MS = 60_000; +const profileLikeUserSchema = { + type: 'object', + properties: { + uid: { type: 'string', description: '用户 uid' }, + uin: { type: 'integer', description: '用户 QQ 号;资料中没有有效号码时为 0' }, + src: { type: 'integer', description: '来源类型' }, + latestTime: { type: 'integer', description: '最近互动时间戳' }, + count: { type: 'integer', description: '互动次数' }, + giftCount: { type: 'integer', description: '礼物数量' }, + customId: { type: 'integer', description: '自定义标识' }, + lastCharged: { type: 'integer', description: '最近充能时间' }, + bAvailableCnt: { type: 'integer', description: '可用次数' }, + bTodayVotedCnt: { type: 'integer', description: '今日已点赞次数' }, + nick: { type: 'string', description: '昵称' }, + gender: { type: 'integer', description: '性别' }, + age: { type: 'integer', description: '年龄' }, + isFriend: { type: 'boolean', description: '是否为好友' }, + isvip: { type: 'boolean', description: '是否为会员' }, + isSvip: { type: 'boolean', description: '是否为超级会员' }, + }, + required: [ + 'uid', 'uin', 'src', 'latestTime', 'count', 'giftCount', 'customId', + 'lastCharged', 'bAvailableCnt', 'bTodayVotedCnt', 'nick', 'gender', + 'age', 'isFriend', 'isvip', 'isSvip', + ], +}; + function essenceNumber(value: unknown, field: string): number { if (typeof value !== 'number' && (typeof value !== 'string' || !/^-?\d+$/.test(value))) { @@ -1150,7 +1177,7 @@ export const actions = [ name: 'get_profile_like', summary: '获取资料点赞', readOnly: true, - returns: '点赞资料:uid、最近点赞时间、收藏(favoriteInfo)与点赞(voteInfo)统计。', + returns: '点赞资料:uid、最近点赞时间、收藏与点赞统计及用户明细。', returnsSchema: { type: 'object', properties: { @@ -1163,7 +1190,11 @@ export const actions = [ total_count: { type: 'integer', description: '收藏总数' }, last_time: { type: 'integer', description: '最近收藏时间戳' }, today_count: { type: 'integer', description: '今日收藏数' }, - userInfos: { type: 'array', description: '用户列表(恒空)' }, + userInfos: { + type: 'array', + description: '收藏用户列表', + items: profileLikeUserSchema, + }, }, }, voteInfo: { @@ -1174,7 +1205,11 @@ export const actions = [ new_count: { type: 'integer', description: '新增点赞数' }, new_nearby_count: { type: 'integer', description: '附近的人新增点赞数' }, last_visit_time: { type: 'integer', description: '最近访问时间戳' }, - userInfos: { type: 'array', description: '用户列表(恒空)' }, + userInfos: { + type: 'array', + description: '点赞用户列表', + items: profileLikeUserSchema, + }, }, }, }, diff --git a/packages/proto-defs/src/oidb-actions/base.ts b/packages/proto-defs/src/oidb-actions/base.ts index a0163c4f..2255f2eb 100644 --- a/packages/proto-defs/src/oidb-actions/base.ts +++ b/packages/proto-defs/src/oidb-actions/base.ts @@ -539,28 +539,55 @@ export interface OidbSetProfile { stringProfiles?: pb_repeated<2, OidbProfileStringItem>; intProfiles?: pb_repeated<3, OidbProfileIntItem>; } -export interface Oidb0x7edInteraction { +export interface Oidb0x7edUserInfo { + uid?: pb<1, string>; + src?: pb<2, uint_32>; + latestTime?: pb<3, uint_32>; + count?: pb<4, uint_32>; + giftCount?: pb<5, uint_32>; + customId?: pb<6, uint_32>; + lastCharged?: pb<8, uint_32>; + availableCount?: pb<21, uint_32>; + todayVotedCount?: pb<22, uint_32>; + nick?: pb<101, string>; + gender?: pb<102, uint_32>; + age?: pb<103, uint_32>; + isFriend?: pb<104, bool>; + isVip?: pb<105, bool>; + isSvip?: pb<106, bool>; +} +export interface Oidb0x7edFavoriteInfo { totalCount?: pb<1, uint_32>; - newCount?: pb<2, uint_32>; + lastTime?: pb<2, uint_32>; todayCount?: pb<3, uint_32>; - lastTime?: pb<4, uint_64>; + userInfos?: pb_repeated<4, Oidb0x7edUserInfo>; +} +export interface Oidb0x7edVoteInfo { + totalCount?: pb<1, uint_32>; + newCount?: pb<2, uint_32>; + newNearbyCount?: pb<3, uint_32>; + lastVisitTime?: pb<4, uint_32>; + userInfos?: pb_repeated<5, Oidb0x7edUserInfo>; } export interface Oidb0x7edUserLikeInfo { uid?: pb<1, string>; - time?: pb<2, uint_64>; - favoriteInfo?: pb<3, Oidb0x7edInteraction>; - voteInfo?: pb<4, Oidb0x7edInteraction>; + time?: pb<2, uint_32>; + favoriteInfo?: pb<3, Oidb0x7edFavoriteInfo>; + voteInfo?: pb<4, Oidb0x7edVoteInfo>; } export interface Oidb0x7edReq { - targetUid?: pb<1, string>; - basic?: pb<2, uint_32>; - vote?: pb<3, uint_32>; - favorite?: pb<4, uint_32>; - start?: pb<12, uint_32>; - limit?: pb<103, uint_32>; + targetUids?: pb_repeated<1, string>; + basic?: pb<2, uint_32>; + vote?: pb<3, uint_32>; + favorite?: pb<4, uint_32>; + userProfile?: pb<101, uint_32>; + start?: pb<102, uint_32>; + limit?: pb<103, uint_32>; } export interface Oidb0x7edResp { userLikeInfos?: pb_repeated<1, Oidb0x7edUserLikeInfo>; + friendMaxVotes?: pb<2, uint_32>; + start?: pb<101, int_32>; } export interface Oidb0x8a7Req { basic1?: pb<1, uint_32>; diff --git a/packages/protocol/src/oidb-service.ts b/packages/protocol/src/oidb-service.ts index 98667ce1..5a3f1de7 100644 --- a/packages/protocol/src/oidb-service.ts +++ b/packages/protocol/src/oidb-service.ts @@ -53,12 +53,13 @@ export interface OidbSender { * `Pick` slices the cmd actually needs. * * One of `subCommand` (static) or `resolveSubCommand` (dynamic from - * params) must be present. `resolveSubCommand` wins if both are given. + * params and context) must be present. `resolveSubCommand` wins if both + * are given. */ export interface OidbCallSpec { command: number; subCommand?: number; - resolveSubCommand?(params: TParams): number; + resolveSubCommand?(params: TParams, ctx: TCtx): number; /** Set OIDB envelope `reserved = 1` (UIN-form variant, see makeOidbEnvelope's isUid). */ uinForm?: boolean; /** Override the default `OidbSvcTrpcTcp.0xNNNN_N` wire name. A few @@ -107,7 +108,7 @@ export async function invokeOidb { const subCommand = spec.resolveSubCommand - ? spec.resolveSubCommand(params) + ? spec.resolveSubCommand(params, ctx) : spec.subCommand!; const reqBody = await spec.serialize(ctx, params); const env = makeOidbEnvelope(spec.command, subCommand, reqBody, spec.uinForm ?? false); @@ -163,7 +164,7 @@ export async function buildOidbRequest { const subCommand = spec.resolveSubCommand - ? spec.resolveSubCommand(params) + ? spec.resolveSubCommand(params, ctx) : spec.subCommand!; const reqBody = await spec.serialize(ctx, params); const env = makeOidbEnvelope(spec.command, subCommand, reqBody, spec.uinForm ?? false); diff --git a/packages/protocol/src/oidb-services/profile/get-like.ts b/packages/protocol/src/oidb-services/profile/get-like.ts index 9f273753..84ef4163 100644 --- a/packages/protocol/src/oidb-services/profile/get-like.ts +++ b/packages/protocol/src/oidb-services/profile/get-like.ts @@ -1,15 +1,40 @@ -// 0x7ED_12 — fetch QQ "thumbs up" (赞) summary for self or another user. +// 0x7ED_12 / 0x7ED_13 — fetch QQ "thumbs up" (赞) details for another +// user / self respectively. // // Self lookup needs the bot's own UID, hence the `identity` dependency. // For other users we use `resolveUserUid` to translate the uin first. import type { OidbBase } from '@snowluma/proto-defs/oidb'; -import type { Oidb0x7edReq, Oidb0x7edResp } from '@snowluma/proto-defs/oidb-actions/base'; +import type { + Oidb0x7edReq, + Oidb0x7edResp, + Oidb0x7edUserInfo, +} from '@snowluma/proto-defs/oidb-actions/base'; import { protobuf_decode, protobuf_encode } from '@snowluma/proton'; import type { BridgeContext } from '../../bridge-context'; import { invokeOidb, type OidbSender } from '../../oidb-service'; import { resolveSelfUid } from '../../self-uid'; +export interface ProfileLikeUserInfo { + [key: string]: import('@snowluma/common/json').JsonValue; + uid: string; + uin: number; + src: number; + latestTime: number; + count: number; + giftCount: number; + customId: number; + lastCharged: number; + bAvailableCnt: number; + bTodayVotedCnt: number; + nick: string; + gender: number; + age: number; + isFriend: boolean; + isvip: boolean; + isSvip: boolean; +} + export interface LikeInfo { [key: string]: import('@snowluma/common/json').JsonValue; uid: string; @@ -19,7 +44,7 @@ export interface LikeInfo { total_count: number; last_time: number; today_count: number; - userInfos: never[]; + userInfos: ProfileLikeUserInfo[]; }; voteInfo: { [key: string]: import('@snowluma/common/json').JsonValue; @@ -27,13 +52,38 @@ export interface LikeInfo { new_count: number; new_nearby_count: number; last_visit_time: number; - userInfos: never[]; + userInfos: ProfileLikeUserInfo[]; + }; +} + +function deserializeUserInfo( + ctx: GetLike.Deps, + data: Oidb0x7edUserInfo, +): ProfileLikeUserInfo { + const uid = data.uid; + if (!uid) throw new Error('get profile like user uid missing'); + return { + uid, + uin: ctx.identity.findUinByUid(uid) ?? 0, + src: data.src ?? 0, + latestTime: data.latestTime ?? 0, + count: data.count ?? 0, + giftCount: data.giftCount ?? 0, + customId: data.customId ?? 0, + lastCharged: data.lastCharged ?? 0, + bAvailableCnt: data.availableCount ?? 0, + bTodayVotedCnt: data.todayVotedCount ?? 0, + nick: data.nick ?? '', + gender: data.gender ?? 0, + age: data.age ?? 0, + isFriend: data.isFriend ?? false, + isvip: data.isVip ?? false, + isSvip: data.isSvip ?? false, }; } export namespace GetLike { export const command = 0x7ED; - export const subCommand = 12; export interface Params { /** Omit / 0 → query self. */ @@ -44,39 +94,48 @@ export namespace GetLike { export type Deps = OidbSender & Pick; + const isSelfQuery = (ctx: Deps, p: Params): boolean => + !p.userId || p.userId === Number(ctx.identity.uin); + + export const resolveSubCommand = (p: Params, ctx: Deps): number => + isSelfQuery(ctx, p) ? 13 : 12; + export const serialize = async (ctx: Deps, p: Params): Promise => { - const targetUid = p.userId - ? await ctx.resolveUserUid(p.userId) - : await resolveSelfUid(ctx); + const targetUid = isSelfQuery(ctx, p) + ? await resolveSelfUid(ctx) + : await ctx.resolveUserUid(p.userId!); if (!targetUid) throw new Error('target uid not found'); return { - targetUid, + targetUids: [targetUid], basic: 1, vote: 1, - favorite: 1, + favorite: 0, + userProfile: 1, start: p.start ?? 0, limit: p.limit ?? 10, }; }; - export const deserialize = (_ctx: Deps, body: Oidb0x7edResp): LikeInfo => { + export const deserialize = (ctx: Deps, body: Oidb0x7edResp): LikeInfo => { const data = body.userLikeInfos?.[0]; if (!data) throw new Error('get profile like info empty'); return { uid: data.uid ?? '', time: Number(data.time ?? 0), favoriteInfo: { - total_count: data.favoriteInfo?.totalCount || 0, - last_time: Number(data.favoriteInfo?.lastTime || 0), - today_count: data.favoriteInfo?.newCount || 0, - userInfos: [], + total_count: data.favoriteInfo?.totalCount ?? 0, + last_time: data.favoriteInfo?.lastTime ?? 0, + today_count: data.favoriteInfo?.todayCount ?? 0, + userInfos: (data.favoriteInfo?.userInfos ?? []) + .map(user => deserializeUserInfo(ctx, user)), }, voteInfo: { - total_count: data.voteInfo?.totalCount || 0, - new_count: data.voteInfo?.newCount || 0, - new_nearby_count: 0, - last_visit_time: Number(data.voteInfo?.lastTime || 0), - userInfos: [], + total_count: data.voteInfo?.totalCount ?? 0, + new_count: data.voteInfo?.newCount ?? 0, + new_nearby_count: data.voteInfo?.newNearbyCount ?? 0, + last_visit_time: data.voteInfo?.lastVisitTime ?? 0, + userInfos: (data.voteInfo?.userInfos ?? []) + .map(user => deserializeUserInfo(ctx, user)), }, }; }; 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 6d14c7f5..d394d8f0 100644 --- a/packages/protocol/tests/oidb-services/profile/get-like.test.ts +++ b/packages/protocol/tests/oidb-services/profile/get-like.test.ts @@ -9,6 +9,7 @@ import { GetLike } from '../../../src/oidb-services/profile/get-like'; function makeDeps(opts: { cachedSelfUid?: string | null; resolveUserUid?: (uin: number) => Promise; + findUinByUid?: (uid: string) => number | null; responseBody?: Oidb0x7edResp; } = {}) { const responseData = opts.responseBody !== undefined @@ -17,35 +18,44 @@ function makeDeps(opts: { const r: SendPacketResult = { success: true, gotResponse: true, errorCode: 0, errorMessage: '', responseData }; return { sendRawPacket: vi.fn(async () => r), - identity: { uin: '10001', selfUid: opts.cachedSelfUid ?? null } as any, + identity: { + uin: '10001', + selfUid: opts.cachedSelfUid ?? null, + findUinByUid: vi.fn(opts.findUinByUid ?? (() => null)), + } as any, resolveUserUid: vi.fn(opts.resolveUserUid ?? (async (uin: number) => `uid-of-${uin}`)), }; } describe('GetLike namespace', () => { - it('declares 0x7ED_12', () => { + it('uses the self and other-user subcommands selected by the native client', () => { + const deps = makeDeps({ cachedSelfUid: 'self-uid' }); expect(GetLike.command).toBe(0x7ED); - expect(GetLike.subCommand).toBe(12); + expect(GetLike.resolveSubCommand({}, deps)).toBe(13); + expect(GetLike.resolveSubCommand({ userId: 0 }, deps)).toBe(13); + expect(GetLike.resolveSubCommand({ userId: 10001 }, deps)).toBe(13); + expect(GetLike.resolveSubCommand({ userId: 20002 }, deps)).toBe(12); }); describe('invoke (target uid resolution)', () => { it('uses cached self uid when userId is omitted', async () => { const deps = makeDeps({ cachedSelfUid: 'cached-self', - responseBody: { userLikeInfos: [{ uid: 'cached-self', time: 0n, favoriteInfo: {}, voteInfo: {} }] }, + responseBody: { userLikeInfos: [{ uid: 'cached-self', time: 0, favoriteInfo: {}, voteInfo: {} }] }, }); await GetLike.invoke(deps, {}); expect(deps.resolveUserUid).not.toHaveBeenCalled(); - const [, bytes] = deps.sendRawPacket.mock.calls[0]!; + const [serviceCmd, bytes] = deps.sendRawPacket.mock.calls[0]!; + expect(serviceCmd).toBe('OidbSvcTrpcTcp.0x7ed_13'); const env = protobuf_decode>(bytes); - expect(env.body?.targetUid).toBe('cached-self'); + expect(env.body?.targetUids).toEqual(['cached-self']); }); it('falls back to resolveUserUid for self when cache is empty', async () => { const deps = makeDeps({ cachedSelfUid: null, resolveUserUid: vi.fn(async () => 'fresh-self') as any, - responseBody: { userLikeInfos: [{ uid: 'fresh-self', time: 0n, favoriteInfo: {}, voteInfo: {} }] }, + responseBody: { userLikeInfos: [{ uid: 'fresh-self', time: 0, favoriteInfo: {}, voteInfo: {} }] }, }); await GetLike.invoke(deps, {}); expect(deps.resolveUserUid).toHaveBeenCalledWith(10001); @@ -53,10 +63,21 @@ describe('GetLike namespace', () => { it('resolves other users via resolveUserUid', async () => { const deps = makeDeps({ - responseBody: { userLikeInfos: [{ uid: 'uid-of-99999', time: 0n, favoriteInfo: {}, voteInfo: {} }] }, + responseBody: { userLikeInfos: [{ uid: 'uid-of-99999', time: 0, favoriteInfo: {}, voteInfo: {} }] }, }); await GetLike.invoke(deps, { userId: 99999 }); expect(deps.resolveUserUid).toHaveBeenCalledWith(99999); + expect(deps.sendRawPacket.mock.calls[0]![0]).toBe('OidbSvcTrpcTcp.0x7ed_12'); + }); + + it('treats an explicit self uin as a self query', async () => { + const deps = makeDeps({ + cachedSelfUid: 'cached-self', + responseBody: { userLikeInfos: [{ uid: 'cached-self', time: 0 }] }, + }); + await GetLike.invoke(deps, { userId: 10001 }); + expect(deps.resolveUserUid).not.toHaveBeenCalled(); + expect(deps.sendRawPacket.mock.calls[0]![0]).toBe('OidbSvcTrpcTcp.0x7ed_13'); }); it('throws when self uin is invalid and cache is empty', async () => { @@ -67,21 +88,22 @@ describe('GetLike namespace', () => { }); describe('serialize', () => { - it('always sets basic=1, vote=1, favorite=1 (full breakdown query)', async () => { + it('requests the profile-backed vote list', async () => { const deps = makeDeps({ - responseBody: { userLikeInfos: [{ uid: 'u', time: 0n, favoriteInfo: {}, voteInfo: {} }] }, + responseBody: { userLikeInfos: [{ uid: 'u', time: 0, favoriteInfo: {}, voteInfo: {} }] }, }); await GetLike.invoke(deps, { userId: 1 }); const [, bytes] = deps.sendRawPacket.mock.calls[0]!; const env = protobuf_decode>(bytes); expect(env.body?.basic).toBe(1); expect(env.body?.vote).toBe(1); - expect(env.body?.favorite).toBe(1); + expect(env.body?.favorite ?? 0).toBe(0); + expect(env.body?.userProfile).toBe(1); }); it('threads start / limit through to the request', async () => { const deps = makeDeps({ - responseBody: { userLikeInfos: [{ uid: 'u', time: 0n, favoriteInfo: {}, voteInfo: {} }] }, + responseBody: { userLikeInfos: [{ uid: 'u', time: 0, favoriteInfo: {}, voteInfo: {} }] }, }); await GetLike.invoke(deps, { userId: 1, start: 5, limit: 50 }); const [, bytes] = deps.sendRawPacket.mock.calls[0]!; @@ -92,7 +114,7 @@ describe('GetLike namespace', () => { it('defaults start=0 / limit=10', async () => { const deps = makeDeps({ - responseBody: { userLikeInfos: [{ uid: 'u', time: 0n, favoriteInfo: {}, voteInfo: {} }] }, + responseBody: { userLikeInfos: [{ uid: 'u', time: 0, favoriteInfo: {}, voteInfo: {} }] }, }); await GetLike.invoke(deps, { userId: 1 }); const [, bytes] = deps.sendRawPacket.mock.calls[0]!; @@ -104,20 +126,74 @@ describe('GetLike namespace', () => { describe('deserialize', () => { it('shapes favorite + vote info with the expected key names', () => { - const out = GetLike.deserialize({} as any, { + const deps = makeDeps({ + findUinByUid: uid => uid === 'vote-user' ? 12345 : null, + }); + const out = GetLike.deserialize(deps, { userLikeInfos: [{ - uid: 'u', time: 1700000000n, - favoriteInfo: { totalCount: 5, lastTime: 1n, newCount: 1 }, - voteInfo: { totalCount: 7, newCount: 2, lastTime: 2n }, + uid: 'u', time: 1700000000, + favoriteInfo: { + totalCount: 5, + lastTime: 1, + todayCount: 1, + userInfos: [{ uid: 'favorite-user', nick: '收藏者', count: 3 }], + }, + voteInfo: { + totalCount: 7, + newCount: 2, + newNearbyCount: 1, + lastVisitTime: 2, + userInfos: [{ + uid: 'vote-user', + src: 4, + latestTime: 3, + count: 2, + giftCount: 1, + customId: 5, + lastCharged: 6, + availableCount: 7, + todayVotedCount: 8, + nick: '点赞者', + gender: 1, + age: 20, + isFriend: true, + isVip: true, + isSvip: false, + }], + }, }], }); expect(out.uid).toBe('u'); expect(out.time).toBe(1700000000); expect(out.favoriteInfo).toEqual({ - total_count: 5, last_time: 1, today_count: 1, userInfos: [], + total_count: 5, + last_time: 1, + today_count: 1, + userInfos: [expect.objectContaining({ uid: 'favorite-user', nick: '收藏者', count: 3 })], }); expect(out.voteInfo).toEqual({ - total_count: 7, new_count: 2, new_nearby_count: 0, last_visit_time: 2, userInfos: [], + total_count: 7, + new_count: 2, + new_nearby_count: 1, + last_visit_time: 2, + userInfos: [expect.objectContaining({ + uid: 'vote-user', + uin: 12345, + src: 4, + latestTime: 3, + count: 2, + giftCount: 1, + customId: 5, + lastCharged: 6, + bAvailableCnt: 7, + bTodayVotedCnt: 8, + nick: '点赞者', + gender: 1, + age: 20, + isFriend: true, + isvip: true, + isSvip: false, + })], }); }); @@ -126,8 +202,14 @@ describe('GetLike namespace', () => { expect(() => GetLike.deserialize({} as any, { userLikeInfos: [] })).toThrow('get profile like info empty'); }); + it('throws when a returned user item has no uid', () => { + expect(() => GetLike.deserialize(makeDeps(), { + userLikeInfos: [{ uid: 'u', voteInfo: { userInfos: [{ nick: 'broken' }] } }], + })).toThrow('get profile like user uid missing'); + }); + it('defaults all count fields to 0 when omitted', () => { - const out = GetLike.deserialize({} as any, { userLikeInfos: [{ uid: 'u', time: 0n }] }); + const out = GetLike.deserialize(makeDeps(), { userLikeInfos: [{ uid: 'u', time: 0 }] }); expect(out.favoriteInfo.total_count).toBe(0); expect(out.voteInfo.total_count).toBe(0); });