fix(onebot): preserve profile regions in user and member responses (#2107)

This commit is contained in:
cherry-xw
2026-10-15 15:31:19 +08:00
committed by GitHub
parent 3f66b36901
commit 810052c605
4 changed files with 37 additions and 2 deletions
@@ -0,0 +1,24 @@
/** Self-reported profile region only; never infer a location from other fields. */
export interface ProfileRegion {
country?: string;
province?: string;
city?: string;
}
function text (value: unknown): string {
return typeof value === 'string' ? value.trim() : '';
}
/** Keep one source together: mixing old/new components can invent a region. */
export function selectProfileRegion (fresh?: ProfileRegion, fallback?: ProfileRegion): Required<ProfileRegion> {
for (const source of [fresh, fallback]) {
const region = { country: text(source?.country), province: text(source?.province), city: text(source?.city) };
if (Object.values(region).some(Boolean)) return region;
}
return { country: '', province: '', city: '' };
}
export function profileRegionArea (source?: ProfileRegion): string {
const region = selectProfileRegion(source);
return [...new Set(Object.values(region).filter(Boolean))].join(' ');
}
@@ -1,3 +1,4 @@
import { selectProfileRegion } from '@/napcat-core/helper/profile-region';
import { OneBotAction } from '@/napcat-onebot/action/OneBotAction';
import { OB11UserSex } from '@/napcat-onebot/index';
import { OB11Construct } from '@/napcat-onebot/helper/data';
@@ -56,6 +57,7 @@ export default class GoCQHTTPGetStrangerInfo extends OneBotAction<PayloadType, R
...extendData.detail.simpleInfo.baseInfo,
...extendData.detail.simpleInfo.relationFlags ?? {},
...extendData.detail.simpleInfo.status ?? {},
...selectProfileRegion(info, extendData.detail.commonExt),
user_id: parseInt(extendData.detail.uin) ?? 0,
uid: info.uid ?? uid,
nickname: extendData.detail.simpleInfo.coreInfo.nick ?? '',
+3 -2
View File
@@ -1,3 +1,4 @@
import { profileRegionArea } from '@/napcat-core/helper/profile-region';
import { calcQQLevel } from 'napcat-common/src/helper';
import { FileNapCatOneBotUUID } from 'napcat-common/src/file-uuid';
import { FriendV2, Group, GroupFileInfoUpdateParamType, GroupMember, SelfInfo, NTSex } from 'napcat-core';
@@ -70,7 +71,7 @@ export class OB11Construct {
}[sex] || OB11UserSex.unknown;
}
static groupMember (group_id: string, member: GroupMember): OB11GroupMember {
static groupMember (group_id: string, member: GroupMember & { country?: string; province?: string; city?: string }): OB11GroupMember {
return {
group_id: +group_id,
user_id: +member.uin,
@@ -78,7 +79,7 @@ export class OB11Construct {
card: member.cardName,
sex: this.sex(member.sex),
age: member.age ?? 0,
area: '',
area: profileRegionArea(member),
level: member.memberRealLevel?.toString() ?? '0',
qq_level: (member.qqLevel && calcQQLevel(member.qqLevel)) || 0,
join_time: +member.joinTime,
+8
View File
@@ -0,0 +1,8 @@
import assert from 'node:assert/strict';
import { selectProfileRegion as select, profileRegionArea as area } from './packages/napcat-core/helper/profile-region.ts';
assert.equal(area({country:'中国',province:'广东',city:'广州'}),'中国 广东 广州');
assert.equal(area({country:'',province:'',city:''}),'');
assert.equal(area({province:'北京',city:'北京'}),'北京');
assert.deepEqual(select({city:'广州'},{country:'旧国家',province:'旧省'}),{country:'',province:'',city:'广州'});
assert.equal(area(select({}, {country:'中国',city:'广州'})),'中国 广州');
console.log('PASS: 5 offline region cases; no QQ requests');