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:
makotowu authored and GitHub committed 2026-08-26 23:31:15 +08:00
1 parent e1bb9b4722
commit c1a0091143
15 files changed
+238 -10

No files matched your search

+2 -1
View File
@@ -32,7 +32,7 @@ export const actions = [
name: 'get_stranger_info',
summary: '获取陌生人信息',
readOnly: true,
returns: '用户资料:QQ 号、昵称、好友备注、性别、年龄与个性签名,命中资料时另含等级与企点标志。',
returns: '用户资料:QQ 号、昵称、好友备注、性别、年龄与个性签名,命中资料时另含等级、企点标志与企业名称。',
returnsSchema: {
type: 'object',
properties: {
@@ -53,6 +53,7 @@ export const actions = [
qidian_master_flag: { type: 'integer', description: '企点主号标志,0 或 1;普通账号为 0' },
qidian_crew_flag: { type: 'integer', description: '企点员工标志,0 或 1;普通账号为 0' },
qidian_crew_flag_2: { type: 'integer', description: '企点保留标志,0 或 1;普通账号为 0' },
qidian_enterprise_name: { type: 'string', description: '企点企业名称;非企点账号或未获取到时为空字符串' },
},
required: ['user_id', 'nickname', 'remark', 'sex', 'age', 'long_nick'],
},
+24 -4
View File
@@ -7,6 +7,7 @@ import {
type GroupRequestInfo,
type UserProfileInfo,
} from '@snowluma/protocol/qq-info';
import type { QidianCorpInfo } from '@snowluma/protocol/services/qidian/fetch-corp-info';
import type { OneBotInstanceContext } from '../instance-context';
import type { JsonObject } from '../types';
@@ -289,7 +290,7 @@ export async function getGroupFiles(
};
}
function formatStrangerInfo(p: UserProfileInfo): JsonObject {
function formatStrangerInfo(p: UserProfileInfo, corp?: QidianCorpInfo | null): JsonObject {
return {
user_id: p.uin,
nickname: p.nickname,
@@ -308,23 +309,42 @@ function formatStrangerInfo(p: UserProfileInfo): JsonObject {
qidian_master_flag: p.qidianMasterFlag ?? 0,
qidian_crew_flag: p.qidianCrewFlag ?? 0,
qidian_crew_flag_2: p.qidianCrewFlag2 ?? 0,
// #404 后续 PR:企点企业资料卡。企业资料卡不含独立的企业 ID,
// 因此这里只透出企业名称(SsoCorpInfo.corpName)。
qidian_enterprise_name: corp?.name ?? '',
};
}
/** 命中的是企点账号时才去拉一次企业资料卡(best-effort,失败返回 null)。 */
async function strangerCorpInfo(
bridge: BridgeInterface,
p: UserProfileInfo,
): Promise<QidianCorpInfo | null> {
if (p.qidianMasterFlag === 0 && p.qidianCrewFlag === 0) return null;
try {
return await bridge.apis.contacts.fetchQidianCorpInfo(p.uin);
} catch {
return null;
}
}
export async function getStrangerInfo(
bridge: BridgeInterface,
userId: number,
): Promise<JsonObject | null> {
try {
const p = await bridge.apis.contacts.fetchUserProfile(userId);
return formatStrangerInfo(p);
const corp = await strangerCorpInfo(bridge, p);
return formatStrangerInfo(p, corp);
} catch {
const p = bridge.identity.findUserProfile(userId);
if (p) {
return formatStrangerInfo({
const profile: UserProfileInfo = {
...p,
remark: p.remark || bridge.identity.findFriend(userId)?.remark || '',
});
};
const corp = await strangerCorpInfo(bridge, profile);
return formatStrangerInfo(profile, corp);
}
const friend = bridge.identity.findFriend(userId);
@@ -614,6 +614,55 @@ describe('onebot/contact-actions / getStrangerInfo', () => {
});
});
it('passes through the qidian enterprise (企业) data-card when available', async () => {
const fetchQidianCorpInfo = vi.fn(async () => ({
name: '测试企业', intro: '测试简介', website: 'https://example.com',
slogan: '测试签名', address: '测试地址', phone: '400-0000-0000', email: '[email protected]',
}));
const bridge = fakeBridge({
apis: {
contacts: {
fetchUserProfile: vi.fn(async () => ({
...makeProfile(55555, 'QidianWorker', 'male'),
qidianMasterFlag: 0,
qidianCrewFlag: 1,
qidianCrewFlag2: 0,
})),
fetchQidianCorpInfo,
},
},
});
const out = await getStrangerInfo(bridge, 55555);
expect(out).toMatchObject({
user_id: 55555,
qidian_master_flag: 0,
qidian_crew_flag: 1,
qidian_crew_flag_2: 0,
qidian_enterprise_name: '测试企业',
});
expect(fetchQidianCorpInfo).toHaveBeenCalledWith(55555);
});
it('leaves enterprise fields empty for non-qidian accounts and never calls the corp lookup', async () => {
const fetchQidianCorpInfo = vi.fn(async () => ({
name: 'x', intro: '', website: '', slogan: '', address: '', phone: '', email: '',
}));
const bridge = fakeBridge({
apis: {
contacts: {
fetchUserProfile: vi.fn(async () => makeProfile(88888, 'Normal', 'male')),
fetchQidianCorpInfo,
},
},
});
const out = await getStrangerInfo(bridge, 88888);
expect(out).toMatchObject({
user_id: 88888,
qidian_enterprise_name: '',
});
expect(fetchQidianCorpInfo).not.toHaveBeenCalled();
});
it('falls back to identity.findUserProfile when fetch fails but the profile is cached', async () => {
const bridge = fakeBridge({
fetchUserProfile: vi.fn(async () => { throw new Error('net'); }),
@@ -34,6 +34,7 @@ describe('get_stranger_info action', () => {
qidian_master_flag: { type: 'integer' },
qidian_crew_flag: { type: 'integer' },
qidian_crew_flag_2: { type: 'integer' },
qidian_enterprise_name: { type: 'string' },
},
});
expect(schema?.required).toContain('remark');
@@ -823,6 +823,7 @@ describe('buildApiContext contact reads', () => {
qidian_master_flag: 0,
qidian_crew_flag: 0,
qidian_crew_flag_2: 0,
qidian_enterprise_name: '',
});
});