update: optimize entity factory (formerly converter.ts)

This commit is contained in:
Seijo Cecilia 2024-08-26 14:36:10 +08:00
parent 3f3d9cc6f1
commit d5dd2e9551
11 changed files with 51 additions and 64 deletions

View File

@ -1,4 +1,4 @@
import { OB11Constructor } from '@/onebot/helper/converter'; import { OB11Entities } from '@/onebot/helper/entities';
import BaseAction from '../BaseAction'; import BaseAction from '../BaseAction';
import { ActionName } from '../types'; import { ActionName } from '../types';
@ -7,7 +7,7 @@ export class GetFriendWithCategory extends BaseAction<void, any> {
async _handle(payload: void) { async _handle(payload: void) {
return (await this.core.apis.FriendApi.getBuddyV2ExWithCate(true)).map(category => ({ return (await this.core.apis.FriendApi.getBuddyV2ExWithCate(true)).map(category => ({
...category, ...category,
buddyList: OB11Constructor.friendsV2(category.buddyList), buddyList: OB11Entities.friendsV2(category.buddyList),
})); }));
} }
} }

View File

@ -1,6 +1,6 @@
import BaseAction from '../BaseAction'; import BaseAction from '../BaseAction';
import { OB11User, OB11UserSex } from '../../types'; import { OB11User, OB11UserSex } from '@/onebot';
import { OB11Constructor } from '@/onebot/helper/converter'; import { OB11Entities } from '@/onebot/helper/entities';
import { ActionName } from '../types'; import { ActionName } from '../types';
import { FromSchema, JSONSchema } from 'json-schema-to-ts'; import { FromSchema, JSONSchema } from 'json-schema-to-ts';
import { calcQQLevel } from '@/common/utils/helper'; import { calcQQLevel } from '@/common/utils/helper';
@ -41,6 +41,6 @@ export default class GoCQHTTPGetStrangerInfo extends BaseAction<Payload, OB11Use
return ret; return ret;
} }
const data = { ...extendData, ...(await NTQQUserApi.getUserDetailInfo(uid)) }; const data = { ...extendData, ...(await NTQQUserApi.getUserDetailInfo(uid)) };
return OB11Constructor.stranger(data); return OB11Entities.stranger(data);
} }
} }

View File

@ -1,5 +1,5 @@
import { OB11Group } from '../../types'; import { OB11Group } from '@/onebot';
import { OB11Constructor } from '@/onebot/helper/converter'; import { OB11Entities } from '@/onebot/helper/entities';
import BaseAction from '../BaseAction'; import BaseAction from '../BaseAction';
import { ActionName } from '../types'; import { ActionName } from '../types';
import { FromSchema, JSONSchema } from 'json-schema-to-ts'; import { FromSchema, JSONSchema } from 'json-schema-to-ts';
@ -22,7 +22,7 @@ class GetGroupInfo extends BaseAction<Payload, OB11Group> {
const NTQQGroupApi = this.core.apis.GroupApi; const NTQQGroupApi = this.core.apis.GroupApi;
const group = (await NTQQGroupApi.getGroups()).find(e => e.groupCode == payload.group_id.toString()); const group = (await NTQQGroupApi.getGroups()).find(e => e.groupCode == payload.group_id.toString());
if (!group) throw `${payload.group_id}不存在`; if (!group) throw `${payload.group_id}不存在`;
return OB11Constructor.group(group); return OB11Entities.group(group);
} }
} }

View File

@ -1,5 +1,5 @@
import { OB11Group } from '../../types'; import { OB11Group } from '@/onebot';
import { OB11Constructor } from '@/onebot/helper/converter'; import { OB11Entities } from '@/onebot/helper/entities';
import BaseAction from '../BaseAction'; import BaseAction from '../BaseAction';
import { ActionName } from '../types'; import { ActionName } from '../types';
import { Group } from '@/core/entities'; import { Group } from '@/core/entities';
@ -21,7 +21,7 @@ class GetGroupList extends BaseAction<Payload, OB11Group[]> {
async _handle(payload: Payload) { async _handle(payload: Payload) {
const NTQQGroupApi = this.core.apis.GroupApi; const NTQQGroupApi = this.core.apis.GroupApi;
const groupList: Group[] = await NTQQGroupApi.getGroups(typeof payload.no_cache === 'string' ? payload.no_cache === 'true' : !!payload.no_cache); const groupList: Group[] = await NTQQGroupApi.getGroups(typeof payload.no_cache === 'string' ? payload.no_cache === 'true' : !!payload.no_cache);
return OB11Constructor.groups(groupList); return OB11Entities.groups(groupList);
} }
} }

View File

@ -1,5 +1,5 @@
import { OB11GroupMember } from '../../types'; import { OB11GroupMember } from '@/onebot';
import { OB11Constructor } from '@/onebot/helper/converter'; import { OB11Entities } from '@/onebot/helper/entities';
import BaseAction from '../BaseAction'; import BaseAction from '../BaseAction';
import { ActionName } from '../types'; import { ActionName } from '../types';
import { FromSchema, JSONSchema } from 'json-schema-to-ts'; import { FromSchema, JSONSchema } from 'json-schema-to-ts';
@ -39,7 +39,7 @@ class GetGroupMemberInfo extends BaseAction<Payload, OB11GroupMember> {
this.core.context.logger.logDebug(`获取群成员详细信息失败, 只能返回基础信息 ${info.reason}`); this.core.context.logger.logDebug(`获取群成员详细信息失败, 只能返回基础信息 ${info.reason}`);
} }
const date = Math.round(Date.now() / 1000); const date = Math.round(Date.now() / 1000);
const retMember = OB11Constructor.groupMember(payload.group_id.toString(), member.value as GroupMember); const retMember = OB11Entities.groupMember(payload.group_id.toString(), member.value as GroupMember);
const Member = await this.core.apis.GroupApi.getGroupMember(payload.group_id.toString(), retMember.user_id); const Member = await this.core.apis.GroupApi.getGroupMember(payload.group_id.toString(), retMember.user_id);
retMember.last_sent_time = parseInt(Member?.lastSpeakTime || date.toString()); retMember.last_sent_time = parseInt(Member?.lastSpeakTime || date.toString());
retMember.join_time = parseInt(Member?.joinTime || date.toString()); retMember.join_time = parseInt(Member?.joinTime || date.toString());

View File

@ -1,5 +1,5 @@
import { OB11GroupMember } from '../../types'; import { OB11GroupMember } from '@/onebot';
import { OB11Constructor } from '@/onebot/helper/converter'; import { OB11Entities } from '@/onebot/helper/entities';
import BaseAction from '../BaseAction'; import BaseAction from '../BaseAction';
import { ActionName } from '../types'; import { ActionName } from '../types';
import { FromSchema, JSONSchema } from 'json-schema-to-ts'; import { FromSchema, JSONSchema } from 'json-schema-to-ts';
@ -26,7 +26,7 @@ class GetGroupMemberList extends BaseAction<Payload, OB11GroupMember[]> {
const groupMembersArr = Array.from(groupMembers.values()); const groupMembersArr = Array.from(groupMembers.values());
let _groupMembers = groupMembersArr.map(item => { let _groupMembers = groupMembersArr.map(item => {
return OB11Constructor.groupMember(payload.group_id.toString(), item); return OB11Entities.groupMember(payload.group_id.toString(), item);
}); });
const MemberMap: Map<number, OB11GroupMember> = new Map<number, OB11GroupMember>(); const MemberMap: Map<number, OB11GroupMember> = new Map<number, OB11GroupMember>();

View File

@ -1,5 +1,5 @@
import { OB11User } from '../../types'; import { OB11User } from '@/onebot';
import { OB11Constructor } from '@/onebot/helper/converter'; import { OB11Entities } from '@/onebot/helper/entities';
import BaseAction from '../BaseAction'; import BaseAction from '../BaseAction';
import { ActionName } from '../types'; import { ActionName } from '../types';
@ -7,7 +7,7 @@ class GetLoginInfo extends BaseAction<null, OB11User> {
actionName = ActionName.GetLoginInfo; actionName = ActionName.GetLoginInfo;
async _handle(payload: null) { async _handle(payload: null) {
return OB11Constructor.selfInfo(this.core.selfInfo); return OB11Entities.selfInfo(this.core.selfInfo);
} }
} }

View File

@ -1,5 +1,5 @@
import { OB11User } from '../../types'; import { OB11User } from '@/onebot';
import { OB11Constructor } from '@/onebot/helper/converter'; import { OB11Entities } from '@/onebot/helper/entities';
import BaseAction from '../BaseAction'; import BaseAction from '../BaseAction';
import { ActionName } from '../types'; import { ActionName } from '../types';
import { FromSchema, JSONSchema } from 'json-schema-to-ts'; import { FromSchema, JSONSchema } from 'json-schema-to-ts';
@ -20,6 +20,6 @@ export default class GetFriendList extends BaseAction<Payload, OB11User[]> {
async _handle(payload: Payload) { async _handle(payload: Payload) {
//全新逻辑 //全新逻辑
const NTQQFriendApi = this.core.apis.FriendApi; const NTQQFriendApi = this.core.apis.FriendApi;
return OB11Constructor.friendsV2(await NTQQFriendApi.getBuddyV2(typeof payload.no_cache === 'string' ? payload.no_cache === 'true' : !!payload.no_cache)); return OB11Entities.friendsV2(await NTQQFriendApi.getBuddyV2(typeof payload.no_cache === 'string' ? payload.no_cache === 'true' : !!payload.no_cache));
} }
} }

View File

@ -24,7 +24,7 @@ import {
OB11MessageFileBase, OB11MessageFileBase,
OB11MessageForward, OB11MessageForward,
} from '@/onebot'; } from '@/onebot';
import { OB11Constructor } from '../helper'; import { OB11Entities } from '../helper';
import { EventType } from '@/onebot/event/OB11BaseEvent'; import { EventType } from '@/onebot/event/OB11BaseEvent';
import { encodeCQCode } from '@/onebot/helper/cqcode'; import { encodeCQCode } from '@/onebot/helper/cqcode';
import { uri2local } from '@/common/utils/file'; import { uri2local } from '@/common/utils/file';
@ -689,7 +689,7 @@ export class OneBotMsgApi {
let member = await NTQQGroupApi.getGroupMember(msg.peerUin, msg.senderUin); let member = await NTQQGroupApi.getGroupMember(msg.peerUin, msg.senderUin);
if (!member) member = await NTQQGroupApi.getGroupMember(msg.peerUin, msg.senderUin); if (!member) member = await NTQQGroupApi.getGroupMember(msg.peerUin, msg.senderUin);
if (member) { if (member) {
resMsg.sender.role = OB11Constructor.groupMemberRole(member.role); resMsg.sender.role = OB11Entities.groupMemberRole(member.role);
resMsg.sender.nickname = member.nick; resMsg.sender.nickname = member.nick;
} }
} else if (msg.chatType == ChatType.KCHATTYPEC2C) { } else if (msg.chatType == ChatType.KCHATTYPEC2C) {

View File

@ -1,9 +1,8 @@
import { calcQQLevel } from '@/common/utils/helper'; import { calcQQLevel } from '@/common/utils/helper';
import { SelfInfo, FriendV2, Friend, Sex, GroupMember, User, Group } from '@/core'; import { Friend, FriendV2, Group, GroupMember, SelfInfo, Sex, User } from '@/core';
import { OB11User, OB11GroupMemberRole, OB11UserSex, OB11GroupMember, OB11Group } from '../types'; import { OB11Group, OB11GroupMember, OB11GroupMemberRole, OB11User, OB11UserSex } from '../types';
export class OB11Entities {
export class OB11Constructor {
static selfInfo(selfInfo: SelfInfo): OB11User { static selfInfo(selfInfo: SelfInfo): OB11User {
return { return {
user_id: parseInt(selfInfo.uin), user_id: parseInt(selfInfo.uin),
@ -12,35 +11,25 @@ export class OB11Constructor {
} }
static friendsV2(friends: FriendV2[]): OB11User[] { static friendsV2(friends: FriendV2[]): OB11User[] {
const data: OB11User[] = []; return friends.map(rawFriend => ({
friends.forEach(friend => { ...rawFriend.baseInfo,
const sexValue = this.sex(friend.baseInfo.sex!); ...rawFriend.coreInfo,
data.push({ user_id: parseInt(rawFriend.coreInfo.uin),
...friend.baseInfo, nickname: rawFriend.coreInfo.nick,
...friend.coreInfo, remark: rawFriend.coreInfo.nick,
user_id: parseInt(friend.coreInfo.uin), sex: this.sex(rawFriend.baseInfo.sex!),
nickname: friend.coreInfo.nick, level: 0,
remark: friend.coreInfo.nick, }));
sex: sexValue,
level: 0,
});
});
return data;
} }
static friends(friends: Friend[]): OB11User[] { static friends(friends: Friend[]): OB11User[] {
const data: OB11User[] = []; return friends.map(rawFriend => ({
friends.forEach(friend => { user_id: parseInt(rawFriend.uin),
const sexValue = this.sex(friend.sex!); nickname: rawFriend.nick,
data.push({ remark: rawFriend.remark,
user_id: parseInt(friend.uin), sex: this.sex(rawFriend.sex!),
nickname: friend.nick, level: 0,
remark: friend.remark, }));
sex: sexValue,
level: 0,
});
});
return data;
} }
static groupMemberRole(role: number): OB11GroupMemberRole | undefined { static groupMemberRole(role: number): OB11GroupMemberRole | undefined {
@ -52,12 +41,11 @@ export class OB11Constructor {
} }
static sex(sex: Sex): OB11UserSex { static sex(sex: Sex): OB11UserSex {
const sexMap = { return {
[Sex.male]: OB11UserSex.male, [Sex.male]: OB11UserSex.male,
[Sex.female]: OB11UserSex.female, [Sex.female]: OB11UserSex.female,
[Sex.unknown]: OB11UserSex.unknown, [Sex.unknown]: OB11UserSex.unknown,
}; }[sex] || OB11UserSex.unknown;
return sexMap[sex] || OB11UserSex.unknown;
} }
static groupMember(group_id: string, member: GroupMember): OB11GroupMember { static groupMember(group_id: string, member: GroupMember): OB11GroupMember {
@ -66,7 +54,7 @@ export class OB11Constructor {
user_id: parseInt(member.uin), user_id: parseInt(member.uin),
nickname: member.nick, nickname: member.nick,
card: member.cardName, card: member.cardName,
sex: OB11Constructor.sex(member.sex!), sex: OB11Entities.sex(member.sex!),
age: member.age ?? 0, age: member.age ?? 0,
area: '', area: '',
level: '0', level: '0',
@ -78,18 +66,17 @@ export class OB11Constructor {
card_changeable: true, card_changeable: true,
is_robot: member.isRobot, is_robot: member.isRobot,
shut_up_timestamp: member.shutUpTime, shut_up_timestamp: member.shutUpTime,
role: OB11Constructor.groupMemberRole(member.role), role: OB11Entities.groupMemberRole(member.role),
title: member.memberSpecialTitle || '', title: member.memberSpecialTitle || '',
}; };
} }
static stranger(user: User): OB11User { static stranger(user: User): OB11User {
//logDebug('construct ob11 stranger', user);
return { return {
...user, ...user,
user_id: parseInt(user.uin), user_id: parseInt(user.uin),
nickname: user.nick, nickname: user.nick,
sex: OB11Constructor.sex(user.sex!), sex: OB11Entities.sex(user.sex!),
age: 0, age: 0,
qid: user.qid, qid: user.qid,
login_days: 0, login_days: 0,
@ -108,6 +95,6 @@ export class OB11Constructor {
} }
static groups(groups: Group[]): OB11Group[] { static groups(groups: Group[]): OB11Group[] {
return groups.map(OB11Constructor.group); return groups.map(OB11Entities.group);
} }
} }

View File

@ -1,3 +1,3 @@
export * from './config'; export * from './config';
export * from './converter'; export * from './entities';
export * from './event'; export * from './event';