feat(onebot): expose qidian enterprise name on stranger info
When a stranger is a qidian account, attach the enterprise name from the corp data card. Non-qidian accounts keep an empty name. Refs #404
This commit is contained in:
1 parent
e1bb9b4722
commit
c1a0091143
15 files changed
+238
-10
No files matched your search
@@ -92,6 +92,10 @@
|
||||
"types": "./src/highway/*.ts",
|
||||
"default": "./src/highway/*.ts"
|
||||
},
|
||||
"./services/*": {
|
||||
"types": "./src/services/*.ts",
|
||||
"default": "./src/services/*.ts"
|
||||
},
|
||||
"./web/*": {
|
||||
"types": "./src/web/*.ts",
|
||||
"default": "./src/web/*.ts"
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
|
||||
import type { SendPacketResult } from '@snowluma/common/packet-sender';
|
||||
import type { QidianCorpInfoRequest, QidianCorpInfoResponse } from '@snowluma/proto-defs/qidian-corp';
|
||||
|
||||
// 企点企业资料卡(trpc.basic.corp.Datacard.SsoCorpInfo)。
|
||||
// 请求体为 protobuf field1 = 目标账号 UIN(varint)
|
||||
// 返回企业名称等资料卡字段(来源见 SnowLuma issue #404 讨论)。
|
||||
export const QIDIAN_CORP_INFO_CMD = 'trpc.basic.corp.Datacard.SsoCorpInfo';
|
||||
|
||||
export interface QidianCorpInfo {
|
||||
name: string;
|
||||
intro: string;
|
||||
website: string;
|
||||
slogan: string;
|
||||
address: string;
|
||||
phone: string;
|
||||
email: string;
|
||||
}
|
||||
|
||||
export interface RawSender {
|
||||
sendRawPacket(serviceCmd: string, body: Uint8Array, timeoutMs?: number): Promise<SendPacketResult>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch the qidian enterprise data-card for a qidian account UIN.
|
||||
* Returns null when the account is not a qidian corp user or the server
|
||||
* returns no corp info. Best-effort by design — callers must NOT fail the
|
||||
* whole profile read if this lookup throws / returns null.
|
||||
*/
|
||||
export async function fetchQidianCorpInfo(
|
||||
sender: RawSender,
|
||||
uin: number,
|
||||
timeoutMs?: number,
|
||||
): Promise<QidianCorpInfo | null> {
|
||||
const req = protobuf_encode<QidianCorpInfoRequest>({ uin });
|
||||
const result = await sender.sendRawPacket(QIDIAN_CORP_INFO_CMD, req, timeoutMs);
|
||||
if (!result.success || !result.gotResponse || !result.responseData || result.responseData.length === 0) {
|
||||
return null;
|
||||
}
|
||||
const decoded = protobuf_decode<QidianCorpInfoResponse>(result.responseData);
|
||||
const name = decoded?.corpName ?? '';
|
||||
if (!name) return null;
|
||||
return {
|
||||
name,
|
||||
intro: decoded?.intro ?? '',
|
||||
website: decoded?.website ?? '',
|
||||
slogan: decoded?.slogan ?? '',
|
||||
address: decoded?.address ?? '',
|
||||
phone: decoded?.phone ?? '',
|
||||
email: decoded?.email ?? '',
|
||||
};
|
||||
}
|
||||
@@ -53,7 +53,7 @@ describe('FetchUserProfileByUid namespace (UID form)', () => {
|
||||
it('decodes qidian flags from number-properties (0 when absent)', async () => {
|
||||
const qidian = makeSender({
|
||||
body: {
|
||||
uin: 3004251964, uid: 'u',
|
||||
uin: 10002, uid: 'u',
|
||||
properties: {
|
||||
bytesProperties: [],
|
||||
numberProperties: [
|
||||
|
||||
@@ -52,7 +52,7 @@ describe('FetchUserProfile namespace', () => {
|
||||
// 企点员工号:两个标志均为 1;普通账号服务器不返回这两个 key → 缺省 0
|
||||
const qidian = makeSender({
|
||||
body: {
|
||||
uin: 3004251964, uid: 'u',
|
||||
uin: 10002, uid: 'u',
|
||||
properties: {
|
||||
bytesProperties: [],
|
||||
numberProperties: [
|
||||
@@ -62,7 +62,7 @@ describe('FetchUserProfile namespace', () => {
|
||||
},
|
||||
} as any,
|
||||
});
|
||||
const out = await FetchUserProfile.invoke(qidian, { uin: 3004251964 });
|
||||
const out = await FetchUserProfile.invoke(qidian, { uin: 10002 });
|
||||
expect(out.qidianMasterFlag).toBe(1);
|
||||
expect(out.qidianCrewFlag).toBe(1);
|
||||
expect(out.qidianCrewFlag2).toBe(0);
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
import { protobuf_encode } from '@snowluma/proton';
|
||||
import type { QidianCorpInfoResponse } from '@snowluma/proto-defs/qidian-corp';
|
||||
import type { SendPacketResult } from '@snowluma/common/packet-sender';
|
||||
import {
|
||||
fetchQidianCorpInfo,
|
||||
QIDIAN_CORP_INFO_CMD,
|
||||
} from '../../../src/services/qidian/fetch-corp-info';
|
||||
|
||||
function sender(body: Uint8Array, opts: Partial<SendPacketResult> = {}) {
|
||||
return {
|
||||
sendRawPacket: vi.fn(async () => ({
|
||||
success: true,
|
||||
gotResponse: true,
|
||||
responseData: body,
|
||||
...opts,
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
const CORP_BODY = protobuf_encode<QidianCorpInfoResponse>({
|
||||
corpName: '测试企业',
|
||||
intro: '测试简介',
|
||||
website: 'https://example.com',
|
||||
slogan: '测试签名',
|
||||
address: '测试地址',
|
||||
phone: '400-0000-0000',
|
||||
email: '[email protected]',
|
||||
});
|
||||
|
||||
describe('services/qidian / fetchQidianCorpInfo', () => {
|
||||
it('sends the corp info command with the uin as field 1', async () => {
|
||||
const s = sender(CORP_BODY);
|
||||
const out = await fetchQidianCorpInfo(s, 10001);
|
||||
expect(out?.name).toBe('测试企业');
|
||||
expect(s.sendRawPacket).toHaveBeenCalledWith(
|
||||
QIDIAN_CORP_INFO_CMD,
|
||||
expect.any(Uint8Array),
|
||||
undefined,
|
||||
);
|
||||
const body = s.sendRawPacket.mock.calls[0][1] as Uint8Array;
|
||||
expect(Buffer.from(body).toString('hex')).toBe('08914e');
|
||||
});
|
||||
|
||||
it('decodes the corp data-card fields', async () => {
|
||||
const out = await fetchQidianCorpInfo(sender(CORP_BODY), 10001);
|
||||
expect(out).toMatchObject({
|
||||
name: '测试企业',
|
||||
website: 'https://example.com',
|
||||
email: '[email protected]',
|
||||
});
|
||||
});
|
||||
|
||||
it('returns null for an empty / failed response', async () => {
|
||||
expect(await fetchQidianCorpInfo(sender(new Uint8Array(0)), 1)).toBeNull();
|
||||
expect(
|
||||
await fetchQidianCorpInfo(sender(new Uint8Array(0), { success: false, errorMessage: 'x' }), 1),
|
||||
).toBeNull();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user