fix(onebot): decode user status property list (#330)
This commit is contained in:
@@ -104,14 +104,20 @@ export class ExtrasApi {
|
||||
// ─────────────── Stranger online/ext status (0xFE1_2) ───────────────
|
||||
|
||||
/**
|
||||
* Returns `null` on transport / decode failure rather than throwing,
|
||||
* so the OneBot action can produce a clean retcode without try/catch
|
||||
* gymnastics. Namespace throws on transport failure → swallow here.
|
||||
* Keeps the action response user-safe while retaining the underlying
|
||||
* failure in the service log for diagnosis.
|
||||
*/
|
||||
async getStrangerStatus(uin: number): Promise<StrangerStatus | null> {
|
||||
try {
|
||||
return await GetStrangerStatus.invoke(this.ctx, { uin });
|
||||
} catch {
|
||||
const status = await GetStrangerStatus.invoke(this.ctx, { uin });
|
||||
if (!status) log.warn('user status query returned no status target=%d', uin);
|
||||
return status;
|
||||
} catch (error) {
|
||||
log.warn(
|
||||
'user status query failed target=%d: %s',
|
||||
uin,
|
||||
error instanceof Error ? error.message : String(error),
|
||||
);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ describe('apis/extras / getStrangerStatus (0xFE1_2)', () => {
|
||||
it('issues the FE1_2 query with key=27372', async () => {
|
||||
const bridge = mockBridge();
|
||||
bridge.sendRawPacket.mockResolvedValueOnce(packResponse(
|
||||
protobuf_encode<OidbBase<OidbStrangerStatusResp>>({ body: { data: { status: { value: 5n } } } }),
|
||||
protobuf_encode<OidbBase<OidbStrangerStatusResp>>({ body: { data: { properties: { entries: [{ key: 27372, value: 5n }] } } } }),
|
||||
));
|
||||
await new ExtrasApi(bridge as any).getStrangerStatus(100200);
|
||||
const [wireName, bytes] = bridge.sendRawPacket.mock.calls[0]!;
|
||||
@@ -62,7 +62,7 @@ describe('apis/extras / getStrangerStatus (0xFE1_2)', () => {
|
||||
it('low-band values (≤10) map to status*10 with ext_status=0', async () => {
|
||||
const bridge = mockBridge();
|
||||
bridge.sendRawPacket.mockResolvedValueOnce(packResponse(
|
||||
protobuf_encode<OidbBase<OidbStrangerStatusResp>>({ body: { data: { status: { value: 7n } } } }),
|
||||
protobuf_encode<OidbBase<OidbStrangerStatusResp>>({ body: { data: { properties: { entries: [{ key: 27372, value: 7n }] } } } }),
|
||||
));
|
||||
expect(await new ExtrasApi(bridge as any).getStrangerStatus(1)).toEqual({ status: 70, ext_status: 0 });
|
||||
});
|
||||
@@ -70,7 +70,7 @@ describe('apis/extras / getStrangerStatus (0xFE1_2)', () => {
|
||||
it('high-band values decompose into the (0xff00 + (>>16 & 0xff)) status word', async () => {
|
||||
const bridge = mockBridge();
|
||||
bridge.sendRawPacket.mockResolvedValueOnce(packResponse(
|
||||
protobuf_encode<OidbBase<OidbStrangerStatusResp>>({ body: { data: { status: { value: 0x42F100n } } } }),
|
||||
protobuf_encode<OidbBase<OidbStrangerStatusResp>>({ body: { data: { properties: { entries: [{ key: 27372, value: 0x42F100n }] } } } }),
|
||||
));
|
||||
const status = await new ExtrasApi(bridge as any).getStrangerStatus(1);
|
||||
expect(status).toEqual({ status: 10, ext_status: 0xF142 });
|
||||
|
||||
@@ -944,12 +944,17 @@ export interface OidbSetFriendRemarkResponse {
|
||||
export interface OidbClearFriendRemark {
|
||||
target?: pb<1, OidbFriendRemarkTarget>;
|
||||
}
|
||||
export interface OidbStrangerStatusRespStatus {
|
||||
export interface OidbStrangerStatusRespProperty {
|
||||
key?: pb<1, uint_32>;
|
||||
value?: pb<2, uint_64>;
|
||||
}
|
||||
export interface OidbStrangerStatusRespProperties {
|
||||
entries?: pb_repeated<1, OidbStrangerStatusRespProperty>;
|
||||
}
|
||||
export interface OidbStrangerStatusRespData {
|
||||
status?: pb<2, OidbStrangerStatusRespStatus>;
|
||||
targetUin?: pb<1, uint_32>;
|
||||
properties?: pb<2, OidbStrangerStatusRespProperties>;
|
||||
uin?: pb<3, uint_32>;
|
||||
}
|
||||
export interface OidbStrangerStatusResp {
|
||||
data?: pb<1, OidbStrangerStatusRespData>;
|
||||
|
||||
@@ -1,17 +1,11 @@
|
||||
// 0xFE1_2 — fetch the stranger's status (online / away / busy /
|
||||
// invisible). Same OIDB cmd as fetchUserProfile but a different
|
||||
// request shape (only key 27372 = status, no other property keys),
|
||||
// and we decode the ext-bigint status word into napcat's
|
||||
// (status × 10, ext_status) pair.
|
||||
// 0xFE1_2 — fetch a user's online / extended status. The request asks
|
||||
// for property 27372; the response returns a property-list wrapper,
|
||||
// whose entries must be matched by key rather than by position.
|
||||
//
|
||||
// `uinForm` is set so the server takes the UIN-form validation path;
|
||||
// without it newer NTQQ rejects with `[oidb] one of uid/openid is
|
||||
// invaild`. Same flag fetchUserProfile uses.
|
||||
//
|
||||
// On any transport / decode failure the deserialize step is bypassed
|
||||
// — the facade swallows the namespace-level exception and surfaces
|
||||
// `null` so callers get a clean retcode.
|
||||
|
||||
import type { OidbBase } from '@snowluma/proto-defs/oidb';
|
||||
import type { OidbStrangerStatusReq, OidbStrangerStatusResp } from '@snowluma/proto-defs/oidb-actions/base';
|
||||
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
|
||||
@@ -22,6 +16,8 @@ export interface StrangerStatus {
|
||||
ext_status: number;
|
||||
}
|
||||
|
||||
const STATUS_PROPERTY_KEY = 27372;
|
||||
|
||||
export namespace GetStrangerStatus {
|
||||
export const command = 0xFE1;
|
||||
export const subCommand = 2;
|
||||
@@ -35,11 +31,13 @@ export namespace GetStrangerStatus {
|
||||
|
||||
export const serialize = (_ctx: Deps, p: Params): OidbStrangerStatusReq => ({
|
||||
uin: p.uin,
|
||||
key: [{ key: 27372 }],
|
||||
key: [{ key: STATUS_PROPERTY_KEY }],
|
||||
});
|
||||
|
||||
export const deserialize = (_ctx: Deps, body: OidbStrangerStatusResp): StrangerStatus | null => {
|
||||
const raw = body.data?.status?.value;
|
||||
const raw = body.data?.properties?.entries
|
||||
?.find((entry) => entry.key === STATUS_PROPERTY_KEY)
|
||||
?.value;
|
||||
if (raw === undefined || raw === null) return null;
|
||||
const extBig = typeof raw === 'bigint' ? raw : BigInt(raw);
|
||||
if (extBig <= 10n) {
|
||||
|
||||
@@ -1,11 +1,31 @@
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
|
||||
import type { pb, pb_repeated, uint_32, uint_64 } from '@snowluma/proton';
|
||||
import type { OidbBase } from '@snowluma/proto-defs/oidb';
|
||||
import type { OidbStrangerStatusReq, OidbStrangerStatusResp } from '@snowluma/proto-defs/oidb-actions/base';
|
||||
import type { SendPacketResult } from '@snowluma/common/packet-sender';
|
||||
|
||||
import { GetStrangerStatus } from '../../../src/oidb-services/extras/get-stranger-status';
|
||||
|
||||
interface ObservedStatusProperty {
|
||||
key?: pb<1, uint_32>;
|
||||
value?: pb<2, uint_64>;
|
||||
}
|
||||
|
||||
interface ObservedStatusProperties {
|
||||
entries?: pb_repeated<1, ObservedStatusProperty>;
|
||||
}
|
||||
|
||||
interface ObservedStatusData {
|
||||
targetUin?: pb<1, uint_32>;
|
||||
properties?: pb<2, ObservedStatusProperties>;
|
||||
uin?: pb<3, uint_32>;
|
||||
}
|
||||
|
||||
interface ObservedStatusResponse {
|
||||
data?: pb<1, ObservedStatusData>;
|
||||
}
|
||||
|
||||
function makeSender(body?: OidbStrangerStatusResp) {
|
||||
const responseData = body !== undefined
|
||||
? Buffer.from(protobuf_encode<OidbBase<OidbStrangerStatusResp>>({ body }))
|
||||
@@ -30,26 +50,55 @@ describe('GetStrangerStatus namespace', () => {
|
||||
|
||||
describe('deserialize', () => {
|
||||
it('low-band (≤10) values map to (value*10, 0)', () => {
|
||||
expect(GetStrangerStatus.deserialize({} as any, { data: { status: { value: 7n } } })).toEqual({ status: 70, ext_status: 0 });
|
||||
expect(GetStrangerStatus.deserialize({} as any, { data: { status: { value: 10n } } })).toEqual({ status: 100, ext_status: 0 });
|
||||
expect(GetStrangerStatus.deserialize({} as any, { data: { properties: { entries: [{ key: 27372, value: 7n }] } } })).toEqual({ status: 70, ext_status: 0 });
|
||||
expect(GetStrangerStatus.deserialize({} as any, { data: { properties: { entries: [{ key: 27372, value: 10n }] } } })).toEqual({ status: 100, ext_status: 0 });
|
||||
});
|
||||
|
||||
it('high-band values decompose into (0xff00 + (>>16 & 0xff)) ext_status', () => {
|
||||
// 0x42F100 → bits 8..15 = 0xF1<<8 = 0xF100, bits 16..23 = 0x42 → 0xF142
|
||||
expect(GetStrangerStatus.deserialize({} as any, { data: { status: { value: 0x42F100n } } }))
|
||||
expect(GetStrangerStatus.deserialize({} as any, { data: { properties: { entries: [{ key: 27372, value: 0x42F100n }] } } }))
|
||||
.toEqual({ status: 10, ext_status: 0xF142 });
|
||||
});
|
||||
|
||||
it('returns null when status field is missing', () => {
|
||||
it('returns null when the requested status property is missing', () => {
|
||||
expect(GetStrangerStatus.deserialize({} as any, {})).toBeNull();
|
||||
expect(GetStrangerStatus.deserialize({} as any, { data: {} })).toBeNull();
|
||||
expect(GetStrangerStatus.deserialize({} as any, { data: { status: {} } })).toBeNull();
|
||||
expect(GetStrangerStatus.deserialize({} as any, { data: { properties: {} } })).toBeNull();
|
||||
expect(GetStrangerStatus.deserialize({} as any, { data: { properties: { entries: [{ key: 10000, value: 7n }] } } })).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('invoke (e2e)', () => {
|
||||
it('decodes the property-list hierarchy returned by 0xFE1_2', async () => {
|
||||
const responseData = Buffer.from(protobuf_encode<OidbBase<ObservedStatusResponse>>({
|
||||
body: {
|
||||
data: {
|
||||
targetUin: 100200,
|
||||
properties: {
|
||||
entries: [
|
||||
{ key: 10000, value: 1n },
|
||||
{ key: 27372, value: 4n },
|
||||
],
|
||||
},
|
||||
uin: 100200,
|
||||
},
|
||||
},
|
||||
}));
|
||||
const result: SendPacketResult = {
|
||||
success: true,
|
||||
gotResponse: true,
|
||||
errorCode: 0,
|
||||
errorMessage: '',
|
||||
responseData,
|
||||
};
|
||||
const sender = { sendRawPacket: vi.fn(async () => result) };
|
||||
|
||||
await expect(GetStrangerStatus.invoke(sender, { uin: 100200 }))
|
||||
.resolves.toEqual({ status: 40, ext_status: 0 });
|
||||
});
|
||||
|
||||
it('routes to OidbSvcTrpcTcp.0xfe1_2 with uinForm=true (envelope reserved=1)', async () => {
|
||||
const sender = makeSender({ data: { status: { value: 5n } } });
|
||||
const sender = makeSender({ data: { properties: { entries: [{ key: 27372, value: 5n }] } } });
|
||||
await GetStrangerStatus.invoke(sender, { uin: 100200 });
|
||||
const [wireName, bytes] = sender.sendRawPacket.mock.calls[0]!;
|
||||
expect(wireName).toBe('OidbSvcTrpcTcp.0xfe1_2');
|
||||
@@ -58,7 +107,7 @@ describe('GetStrangerStatus namespace', () => {
|
||||
});
|
||||
|
||||
it('returns the decoded status object', async () => {
|
||||
const sender = makeSender({ data: { status: { value: 5n } } });
|
||||
const sender = makeSender({ data: { properties: { entries: [{ key: 27372, value: 5n }] } } });
|
||||
const out = await GetStrangerStatus.invoke(sender, { uin: 1 });
|
||||
expect(out).toEqual({ status: 50, ext_status: 0 });
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user