mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2024-11-21 09:36:35 +00:00
update: optimize entity factory (formerly converter.ts
)
This commit is contained in:
parent
3f3d9cc6f1
commit
d5dd2e9551
@ -1,4 +1,4 @@
|
||||
import { OB11Constructor } from '@/onebot/helper/converter';
|
||||
import { OB11Entities } from '@/onebot/helper/entities';
|
||||
import BaseAction from '../BaseAction';
|
||||
import { ActionName } from '../types';
|
||||
|
||||
@ -7,7 +7,7 @@ export class GetFriendWithCategory extends BaseAction<void, any> {
|
||||
async _handle(payload: void) {
|
||||
return (await this.core.apis.FriendApi.getBuddyV2ExWithCate(true)).map(category => ({
|
||||
...category,
|
||||
buddyList: OB11Constructor.friendsV2(category.buddyList),
|
||||
buddyList: OB11Entities.friendsV2(category.buddyList),
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
import BaseAction from '../BaseAction';
|
||||
import { OB11User, OB11UserSex } from '../../types';
|
||||
import { OB11Constructor } from '@/onebot/helper/converter';
|
||||
import { OB11User, OB11UserSex } from '@/onebot';
|
||||
import { OB11Entities } from '@/onebot/helper/entities';
|
||||
import { ActionName } from '../types';
|
||||
import { FromSchema, JSONSchema } from 'json-schema-to-ts';
|
||||
import { calcQQLevel } from '@/common/utils/helper';
|
||||
@ -41,6 +41,6 @@ export default class GoCQHTTPGetStrangerInfo extends BaseAction<Payload, OB11Use
|
||||
return ret;
|
||||
}
|
||||
const data = { ...extendData, ...(await NTQQUserApi.getUserDetailInfo(uid)) };
|
||||
return OB11Constructor.stranger(data);
|
||||
return OB11Entities.stranger(data);
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { OB11Group } from '../../types';
|
||||
import { OB11Constructor } from '@/onebot/helper/converter';
|
||||
import { OB11Group } from '@/onebot';
|
||||
import { OB11Entities } from '@/onebot/helper/entities';
|
||||
import BaseAction from '../BaseAction';
|
||||
import { ActionName } from '../types';
|
||||
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 group = (await NTQQGroupApi.getGroups()).find(e => e.groupCode == payload.group_id.toString());
|
||||
if (!group) throw `群${payload.group_id}不存在`;
|
||||
return OB11Constructor.group(group);
|
||||
return OB11Entities.group(group);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { OB11Group } from '../../types';
|
||||
import { OB11Constructor } from '@/onebot/helper/converter';
|
||||
import { OB11Group } from '@/onebot';
|
||||
import { OB11Entities } from '@/onebot/helper/entities';
|
||||
import BaseAction from '../BaseAction';
|
||||
import { ActionName } from '../types';
|
||||
import { Group } from '@/core/entities';
|
||||
@ -21,7 +21,7 @@ class GetGroupList extends BaseAction<Payload, OB11Group[]> {
|
||||
async _handle(payload: Payload) {
|
||||
const NTQQGroupApi = this.core.apis.GroupApi;
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { OB11GroupMember } from '../../types';
|
||||
import { OB11Constructor } from '@/onebot/helper/converter';
|
||||
import { OB11GroupMember } from '@/onebot';
|
||||
import { OB11Entities } from '@/onebot/helper/entities';
|
||||
import BaseAction from '../BaseAction';
|
||||
import { ActionName } from '../types';
|
||||
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}`);
|
||||
}
|
||||
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);
|
||||
retMember.last_sent_time = parseInt(Member?.lastSpeakTime || date.toString());
|
||||
retMember.join_time = parseInt(Member?.joinTime || date.toString());
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { OB11GroupMember } from '../../types';
|
||||
import { OB11Constructor } from '@/onebot/helper/converter';
|
||||
import { OB11GroupMember } from '@/onebot';
|
||||
import { OB11Entities } from '@/onebot/helper/entities';
|
||||
import BaseAction from '../BaseAction';
|
||||
import { ActionName } from '../types';
|
||||
import { FromSchema, JSONSchema } from 'json-schema-to-ts';
|
||||
@ -26,7 +26,7 @@ class GetGroupMemberList extends BaseAction<Payload, OB11GroupMember[]> {
|
||||
const groupMembersArr = Array.from(groupMembers.values());
|
||||
|
||||
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>();
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { OB11User } from '../../types';
|
||||
import { OB11Constructor } from '@/onebot/helper/converter';
|
||||
import { OB11User } from '@/onebot';
|
||||
import { OB11Entities } from '@/onebot/helper/entities';
|
||||
import BaseAction from '../BaseAction';
|
||||
import { ActionName } from '../types';
|
||||
|
||||
@ -7,7 +7,7 @@ class GetLoginInfo extends BaseAction<null, OB11User> {
|
||||
actionName = ActionName.GetLoginInfo;
|
||||
|
||||
async _handle(payload: null) {
|
||||
return OB11Constructor.selfInfo(this.core.selfInfo);
|
||||
return OB11Entities.selfInfo(this.core.selfInfo);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { OB11User } from '../../types';
|
||||
import { OB11Constructor } from '@/onebot/helper/converter';
|
||||
import { OB11User } from '@/onebot';
|
||||
import { OB11Entities } from '@/onebot/helper/entities';
|
||||
import BaseAction from '../BaseAction';
|
||||
import { ActionName } from '../types';
|
||||
import { FromSchema, JSONSchema } from 'json-schema-to-ts';
|
||||
@ -20,6 +20,6 @@ export default class GetFriendList extends BaseAction<Payload, OB11User[]> {
|
||||
async _handle(payload: Payload) {
|
||||
//全新逻辑
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
@ -24,7 +24,7 @@ import {
|
||||
OB11MessageFileBase,
|
||||
OB11MessageForward,
|
||||
} from '@/onebot';
|
||||
import { OB11Constructor } from '../helper';
|
||||
import { OB11Entities } from '../helper';
|
||||
import { EventType } from '@/onebot/event/OB11BaseEvent';
|
||||
import { encodeCQCode } from '@/onebot/helper/cqcode';
|
||||
import { uri2local } from '@/common/utils/file';
|
||||
@ -689,7 +689,7 @@ export class OneBotMsgApi {
|
||||
let member = await NTQQGroupApi.getGroupMember(msg.peerUin, msg.senderUin);
|
||||
if (!member) member = await NTQQGroupApi.getGroupMember(msg.peerUin, msg.senderUin);
|
||||
if (member) {
|
||||
resMsg.sender.role = OB11Constructor.groupMemberRole(member.role);
|
||||
resMsg.sender.role = OB11Entities.groupMemberRole(member.role);
|
||||
resMsg.sender.nickname = member.nick;
|
||||
}
|
||||
} else if (msg.chatType == ChatType.KCHATTYPEC2C) {
|
||||
|
@ -1,9 +1,8 @@
|
||||
import { calcQQLevel } from '@/common/utils/helper';
|
||||
import { SelfInfo, FriendV2, Friend, Sex, GroupMember, User, Group } from '@/core';
|
||||
import { OB11User, OB11GroupMemberRole, OB11UserSex, OB11GroupMember, OB11Group } from '../types';
|
||||
import { Friend, FriendV2, Group, GroupMember, SelfInfo, Sex, User } from '@/core';
|
||||
import { OB11Group, OB11GroupMember, OB11GroupMemberRole, OB11User, OB11UserSex } from '../types';
|
||||
|
||||
|
||||
export class OB11Constructor {
|
||||
export class OB11Entities {
|
||||
static selfInfo(selfInfo: SelfInfo): OB11User {
|
||||
return {
|
||||
user_id: parseInt(selfInfo.uin),
|
||||
@ -12,35 +11,25 @@ export class OB11Constructor {
|
||||
}
|
||||
|
||||
static friendsV2(friends: FriendV2[]): OB11User[] {
|
||||
const data: OB11User[] = [];
|
||||
friends.forEach(friend => {
|
||||
const sexValue = this.sex(friend.baseInfo.sex!);
|
||||
data.push({
|
||||
...friend.baseInfo,
|
||||
...friend.coreInfo,
|
||||
user_id: parseInt(friend.coreInfo.uin),
|
||||
nickname: friend.coreInfo.nick,
|
||||
remark: friend.coreInfo.nick,
|
||||
sex: sexValue,
|
||||
level: 0,
|
||||
});
|
||||
});
|
||||
return data;
|
||||
return friends.map(rawFriend => ({
|
||||
...rawFriend.baseInfo,
|
||||
...rawFriend.coreInfo,
|
||||
user_id: parseInt(rawFriend.coreInfo.uin),
|
||||
nickname: rawFriend.coreInfo.nick,
|
||||
remark: rawFriend.coreInfo.nick,
|
||||
sex: this.sex(rawFriend.baseInfo.sex!),
|
||||
level: 0,
|
||||
}));
|
||||
}
|
||||
|
||||
static friends(friends: Friend[]): OB11User[] {
|
||||
const data: OB11User[] = [];
|
||||
friends.forEach(friend => {
|
||||
const sexValue = this.sex(friend.sex!);
|
||||
data.push({
|
||||
user_id: parseInt(friend.uin),
|
||||
nickname: friend.nick,
|
||||
remark: friend.remark,
|
||||
sex: sexValue,
|
||||
level: 0,
|
||||
});
|
||||
});
|
||||
return data;
|
||||
return friends.map(rawFriend => ({
|
||||
user_id: parseInt(rawFriend.uin),
|
||||
nickname: rawFriend.nick,
|
||||
remark: rawFriend.remark,
|
||||
sex: this.sex(rawFriend.sex!),
|
||||
level: 0,
|
||||
}));
|
||||
}
|
||||
|
||||
static groupMemberRole(role: number): OB11GroupMemberRole | undefined {
|
||||
@ -52,12 +41,11 @@ export class OB11Constructor {
|
||||
}
|
||||
|
||||
static sex(sex: Sex): OB11UserSex {
|
||||
const sexMap = {
|
||||
return {
|
||||
[Sex.male]: OB11UserSex.male,
|
||||
[Sex.female]: OB11UserSex.female,
|
||||
[Sex.unknown]: OB11UserSex.unknown,
|
||||
};
|
||||
return sexMap[sex] || OB11UserSex.unknown;
|
||||
}[sex] || OB11UserSex.unknown;
|
||||
}
|
||||
|
||||
static groupMember(group_id: string, member: GroupMember): OB11GroupMember {
|
||||
@ -66,7 +54,7 @@ export class OB11Constructor {
|
||||
user_id: parseInt(member.uin),
|
||||
nickname: member.nick,
|
||||
card: member.cardName,
|
||||
sex: OB11Constructor.sex(member.sex!),
|
||||
sex: OB11Entities.sex(member.sex!),
|
||||
age: member.age ?? 0,
|
||||
area: '',
|
||||
level: '0',
|
||||
@ -78,18 +66,17 @@ export class OB11Constructor {
|
||||
card_changeable: true,
|
||||
is_robot: member.isRobot,
|
||||
shut_up_timestamp: member.shutUpTime,
|
||||
role: OB11Constructor.groupMemberRole(member.role),
|
||||
role: OB11Entities.groupMemberRole(member.role),
|
||||
title: member.memberSpecialTitle || '',
|
||||
};
|
||||
}
|
||||
|
||||
static stranger(user: User): OB11User {
|
||||
//logDebug('construct ob11 stranger', user);
|
||||
return {
|
||||
...user,
|
||||
user_id: parseInt(user.uin),
|
||||
nickname: user.nick,
|
||||
sex: OB11Constructor.sex(user.sex!),
|
||||
sex: OB11Entities.sex(user.sex!),
|
||||
age: 0,
|
||||
qid: user.qid,
|
||||
login_days: 0,
|
||||
@ -108,6 +95,6 @@ export class OB11Constructor {
|
||||
}
|
||||
|
||||
static groups(groups: Group[]): OB11Group[] {
|
||||
return groups.map(OB11Constructor.group);
|
||||
return groups.map(OB11Entities.group);
|
||||
}
|
||||
}
|
@ -1,3 +1,3 @@
|
||||
export * from './config';
|
||||
export * from './converter';
|
||||
export * from './entities';
|
||||
export * from './event';
|
||||
|
Loading…
x
Reference in New Issue
Block a user