feat(onebot): set profile gender and return stranger online status

set_qq_profile accepts sex. get_stranger_info now returns the online
status, extended status, and custom status that QQ already provides
on the user profile.

Closes #397
Closes #398
This commit is contained in:
DrSmoothl committed 2026-08-19 21:46:03 +08:00
1 parent 8fdd41ea22
commit d8521d4345
11 files changed
+81 -13

No files matched your search

+2 -2
View File
@@ -82,8 +82,8 @@ export class ProfileApi {
} }
} }
setProfile(nickname?: string, personalNote?: string): Promise<void> { setProfile(nickname?: string, personalNote?: string, sex?: number): Promise<void> {
return SetProfile.invoke(this.ctx, { nickname, personalNote }); return SetProfile.invoke(this.ctx, { nickname, personalNote, sex });
} }
setSelfLongNick(longNick: string): Promise<void> { setSelfLongNick(longNick: string): Promise<void> {
+6
View File
@@ -44,6 +44,12 @@ export const actions = [
long_nick: { type: 'string', description: '个性签名' }, long_nick: { type: 'string', description: '个性签名' },
qq_level: { type: 'integer', description: 'QQ 等级(仅查到资料时返回)' }, qq_level: { type: 'integer', description: 'QQ 等级(仅查到资料时返回)' },
level: { type: 'integer', description: 'QQ 等级,同 qq_level(仅查到资料时返回)' }, level: { type: 'integer', description: 'QQ 等级,同 qq_level(仅查到资料时返回)' },
status: { type: 'integer', description: '在线状态码' },
extStatus: { type: 'integer', description: '扩展状态码' },
ext_status: { type: 'integer', description: '扩展状态码(同 extStatus)' },
batteryStatus: { type: 'integer', description: '电量状态' },
customStatus: { type: 'object', description: '自定义状态', nullable: true },
customStatusDescInfo: { type: 'string', description: '自定义状态说明' },
}, },
required: ['user_id', 'nickname', 'remark', 'sex', 'age', 'long_nick'], required: ['user_id', 'nickname', 'remark', 'sex', 'age', 'long_nick'],
}, },
+6 -2
View File
@@ -86,9 +86,9 @@ export interface Oidb0x89a_0HistoryVisibility {
settings?: pb<2, Oidb0x89a_0HistoryVisibilitySettings>; settings?: pb<2, Oidb0x89a_0HistoryVisibilitySettings>;
field12?: pb<12, uint_32>; field12?: pb<12, uint_32>;
} }
// 0x89A_8 — one masked group-member capability update. These are deny bits, // 0x89A_0 — one masked group-member capability update. These are deny bits,
// so callers clear the selected bit to allow a capability and set it to deny. // so callers clear the selected bit to allow a capability and set it to deny.
// Same settings tags as invite policy; the command variant is what differs. // Same settings tags as invite policy.
export interface Oidb0x89a_0MemberPermissionSettings { export interface Oidb0x89a_0MemberPermissionSettings {
appPrivilegeFlag?: pb_optional<23, uint_32>; appPrivilegeFlag?: pb_optional<23, uint_32>;
appPrivilegeMask?: pb_optional<24, uint_32>; appPrivilegeMask?: pb_optional<24, uint_32>;
@@ -266,6 +266,10 @@ export interface OidbUserInfoResponse {
export interface AvatarInfo { export interface AvatarInfo {
url?: pb<5, string>; url?: pb<5, string>;
} }
export interface OidbCustomStatus {
faceId?: pb<1, uint_32>;
msg?: pb<2, string>;
}
export interface OidbFriendListNumber { export interface OidbFriendListNumber {
numbers?: pb_repeated<1, uint_32>; numbers?: pb_repeated<1, uint_32>;
} }
@@ -22,6 +22,7 @@ import type {
OidbUserInfoResponse, OidbUserInfoResponse,
} from '@snowluma/proto-defs/oidb-actions/base'; } from '@snowluma/proto-defs/oidb-actions/base';
import type { UserProfileInfo } from '../../qq-info'; import type { UserProfileInfo } from '../../qq-info';
import { applyOnlineStatus } from './fetch-user-profile';
import { invokeOidb, type OidbSender } from '../../oidb-service'; import { invokeOidb, type OidbSender } from '../../oidb-service';
const REQUESTED_KEYS = [ const REQUESTED_KEYS = [
@@ -83,6 +84,7 @@ export namespace FetchUserProfileByUid {
info.sex = sexNum === 1 ? 'male' : sexNum === 2 ? 'female' : 'unknown'; info.sex = sexNum === 1 ? 'male' : sexNum === 2 ? 'female' : 'unknown';
info.age = numMap.get(20037) ?? 0; info.age = numMap.get(20037) ?? 0;
info.level = numMap.get(105) ?? 0; info.level = numMap.get(105) ?? 0;
applyOnlineStatus(info, bytesMap, numMap);
} }
return info; return info;
@@ -21,12 +21,37 @@ import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
import type { OidbBase } from '@snowluma/proto-defs/oidb'; import type { OidbBase } from '@snowluma/proto-defs/oidb';
import type { import type {
AvatarInfo, AvatarInfo,
OidbCustomStatus,
OidbUserInfoRequest, OidbUserInfoRequest,
OidbUserInfoResponse, OidbUserInfoResponse,
} from '@snowluma/proto-defs/oidb-actions/base'; } from '@snowluma/proto-defs/oidb-actions/base';
import type { UserProfileInfo } from '../../qq-info'; import type { UserProfileInfo } from '../../qq-info';
import { unpackStatusWord } from '../extras/get-stranger-status';
import { invokeOidb, type OidbSender } from '../../oidb-service'; import { invokeOidb, type OidbSender } from '../../oidb-service';
export function applyOnlineStatus(
info: UserProfileInfo,
bytesMap: Map<number, Uint8Array>,
numMap: Map<number, number>,
): void {
const statusRaw = numMap.get(27372);
if (statusRaw !== undefined) {
const unpacked = unpackStatusWord(statusRaw);
info.status = unpacked.status;
info.extStatus = unpacked.ext_status;
info.batteryStatus = 0;
}
const customBytes = bytesMap.get(27406);
if (customBytes && customBytes.length > 0) {
const custom = protobuf_decode<OidbCustomStatus>(customBytes);
info.customStatus = {
faceId: custom.faceId ?? 0,
wording: custom.msg ?? '',
};
info.customStatusDesc = custom.msg ?? '';
}
}
const REQUESTED_KEYS = [ const REQUESTED_KEYS = [
20002, 27394, 20009, 20031, 101, 103, 102, 20020, 20003, 20026, 20002, 27394, 20009, 20031, 101, 103, 102, 20020, 20003, 20026,
105, 27372, 27406, 20037, 105, 27372, 27406, 20037,
@@ -90,6 +115,7 @@ export namespace FetchUserProfile {
info.sex = sexNum === 1 ? 'male' : sexNum === 2 ? 'female' : 'unknown'; info.sex = sexNum === 1 ? 'male' : sexNum === 2 ? 'female' : 'unknown';
info.age = numMap.get(20037) ?? 0; info.age = numMap.get(20037) ?? 0;
info.level = numMap.get(105) ?? 0; info.level = numMap.get(105) ?? 0;
applyOnlineStatus(info, bytesMap, numMap);
} }
return info; return info;
@@ -16,6 +16,15 @@ export interface StrangerStatus {
ext_status: number; ext_status: number;
} }
export function unpackStatusWord(raw: bigint | number): { status: number; ext_status: number } {
const extBig = typeof raw === 'bigint' ? raw : BigInt(raw);
if (extBig <= 10n) {
return { status: Number(extBig) * 10, ext_status: 0 };
}
const status = Number((extBig & 0xff00n) + ((extBig >> 16n) & 0xffn));
return { status: 10, ext_status: status };
}
const STATUS_PROPERTY_KEY = 27372; const STATUS_PROPERTY_KEY = 27372;
export namespace GetStrangerStatus { export namespace GetStrangerStatus {
@@ -39,12 +48,7 @@ export namespace GetStrangerStatus {
?.find((entry) => entry.key === STATUS_PROPERTY_KEY) ?.find((entry) => entry.key === STATUS_PROPERTY_KEY)
?.value; ?.value;
if (raw === undefined || raw === null) return null; if (raw === undefined || raw === null) return null;
const extBig = typeof raw === 'bigint' ? raw : BigInt(raw); return unpackStatusWord(raw);
if (extBig <= 10n) {
return { status: Number(extBig) * 10, ext_status: 0 };
}
const status = Number((extBig & 0xff00n) + ((extBig >> 16n) & 0xffn));
return { status: 10, ext_status: status };
}; };
export const encode = (env: OidbBase<OidbStrangerStatusReq>): Uint8Array => export const encode = (env: OidbBase<OidbStrangerStatusReq>): Uint8Array =>
@@ -19,6 +19,7 @@ export namespace SetProfile {
export interface Params { export interface Params {
nickname?: string; nickname?: string;
personalNote?: string; personalNote?: string;
sex?: number;
} }
/** Needs identity to read `uin` for the request body. */ /** Needs identity to read `uin` for the request body. */
@@ -26,9 +27,15 @@ export namespace SetProfile {
export const serialize = (ctx: Deps, p: Params): OidbSetProfile => { export const serialize = (ctx: Deps, p: Params): OidbSetProfile => {
const stringProfiles: { fieldId: number; value: string }[] = []; const stringProfiles: { fieldId: number; value: string }[] = [];
const intProfiles: { fieldId: number; value: bigint }[] = [];
if (p.nickname !== undefined) stringProfiles.push({ fieldId: 20002, value: p.nickname }); if (p.nickname !== undefined) stringProfiles.push({ fieldId: 20002, value: p.nickname });
if (p.personalNote !== undefined) stringProfiles.push({ fieldId: 102, value: p.personalNote }); if (p.personalNote !== undefined) stringProfiles.push({ fieldId: 102, value: p.personalNote });
return { uin: BigInt(ctx.identity.uin), stringProfiles }; if (p.sex !== undefined) intProfiles.push({ fieldId: 20009, value: BigInt(p.sex) });
return {
uin: BigInt(ctx.identity.uin),
...(stringProfiles.length > 0 ? { stringProfiles } : {}),
...(intProfiles.length > 0 ? { intProfiles } : {}),
};
}; };
export const deserialize = (_ctx: Deps, _: Oidb0x112aResp): void => {}; export const deserialize = (_ctx: Deps, _: Oidb0x112aResp): void => {};
@@ -42,7 +49,11 @@ export namespace SetProfile {
export const invoke = (deps: Deps, params: Params): Promise<void> => { export const invoke = (deps: Deps, params: Params): Promise<void> => {
// No-op when there's nothing to write — preserves the facade's // No-op when there's nothing to write — preserves the facade's
// historic short-circuit (avoid a wasted SSO round-trip). // historic short-circuit (avoid a wasted SSO round-trip).
if (params.nickname === undefined && params.personalNote === undefined) { if (
params.nickname === undefined
&& params.personalNote === undefined
&& params.sex === undefined
) {
return Promise.resolve(); return Promise.resolve();
} }
return invokeOidb(deps, SetProfile, params); return invokeOidb(deps, SetProfile, params);
+5
View File
@@ -12,6 +12,11 @@ export interface UserProfileInfo {
* Already requested in `fetchUserProfile` keys[]; LagrangeV2 * Already requested in `fetchUserProfile` keys[]; LagrangeV2
* `FetchStrangerService.cs` confirms `// Level`. */ * `FetchStrangerService.cs` confirms `// Level`. */
level: number; level: number;
status?: number;
extStatus?: number;
batteryStatus?: number;
customStatus?: { faceId: number; wording: string } | null;
customStatusDesc?: string;
} }
export interface FriendInfo { export interface FriendInfo {
@@ -21,6 +21,14 @@ describe('SetProfile namespace', () => {
}); });
describe('invoke', () => { describe('invoke', () => {
it('packages sex into integer field 20009', async () => {
const deps = makeDeps();
await SetProfile.invoke(deps, { sex: 1 });
const [, bytes] = deps.sendRawPacket.mock.calls[0]!;
const env = protobuf_decode<OidbBase<OidbSetProfile>>(bytes);
expect(env.body?.intProfiles).toEqual([{ fieldId: 20009, value: 1n }]);
});
it('is a no-op when both nickname and personalNote are undefined', async () => { it('is a no-op when both nickname and personalNote are undefined', async () => {
const deps = makeDeps(); const deps = makeDeps();
await SetProfile.invoke(deps, {}); await SetProfile.invoke(deps, {});
+2 -1
View File
@@ -455,10 +455,11 @@ export abstract class SnowLumaApiClient {
return this.call('download_file', params, options); return this.call('download_file', params, options);
} }
setQqProfile(params: { nickname?: string; personalNote?: string }, options?: RequestOptions) { setQqProfile(params: { nickname?: string; personalNote?: string; sex?: number }, options?: RequestOptions) {
return this.call('set_qq_profile', { return this.call('set_qq_profile', {
nickname: params.nickname, nickname: params.nickname,
personal_note: params.personalNote, personal_note: params.personalNote,
...(params.sex !== undefined ? { sex: params.sex } : {}),
}, options); }, options);
} }
+1
View File
@@ -350,6 +350,7 @@ export interface DownloadFileParams extends JsonObject {
export interface SetQqProfileParams extends JsonObject { export interface SetQqProfileParams extends JsonObject {
nickname?: string; nickname?: string;
personal_note?: string; personal_note?: string;
sex?: number;
} }
export interface SetOnlineStatusParams extends JsonObject { export interface SetOnlineStatusParams extends JsonObject {