fix(onebot): return profile like user details

Restore the interaction user lists returned by get_profile_like and keep self and other-user lookups consistent with the current client behavior.

Resolve missing account identities when needed, surface lookup failures, and update the public action catalog with the completed response shape. Add regression coverage for request selection, response decoding, identity enrichment, and malformed results.

Fixes #349
This commit is contained in:
DrSmoothl committed 2026-08-11 23:46:46 +08:00
1 parent b38db6715f
commit 8ded1065d4
9 files changed
+717 -70

No files matched your search

+5 -4
View File
@@ -53,12 +53,13 @@ export interface OidbSender {
* `Pick<BridgeContext, ...>` 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<TCtx extends OidbSender, TReq, TResp, TParams, TResult> {
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<TCtx extends OidbSender, TReq, TResp, TParams,
timeoutMs?: number,
): Promise<TResult> {
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<TCtx extends OidbSender, TReq, TResp, TPa
params: TParams,
): Promise<{ wireName: string; bytes: Uint8Array }> {
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);
@@ -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<BridgeContext, 'identity' | 'resolveUserUid'>;
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<Oidb0x7edReq> => {
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)),
},
};
};
@@ -9,6 +9,7 @@ import { GetLike } from '../../../src/oidb-services/profile/get-like';
function makeDeps(opts: {
cachedSelfUid?: string | null;
resolveUserUid?: (uin: number) => Promise<string>;
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<OidbBase<Oidb0x7edReq>>(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<OidbBase<Oidb0x7edReq>>(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);
});