diff --git a/src/core/apis/friend.ts b/src/core/apis/friend.ts index 5d02aea4..0bf6005c 100644 --- a/src/core/apis/friend.ts +++ b/src/core/apis/friend.ts @@ -1,30 +1,33 @@ -import { FriendRequest, FriendV2, SimpleInfo, User } from '@/core/entities'; -import { BuddyListReqType, napCatCore, NodeIKernelBuddyListener, NodeIKernelProfileService, OnBuddyChangeParams } from '@/core'; -import { NTEventDispatch } from '@/common/utils/EventTask'; +import { FriendV2, User } from '@/core/entities'; +import { BuddyListReqType, InstanceContext, NapCatCore, NodeIKernelProfileService, OnBuddyChangeParams } from '@/core'; import { LimitedHashTable } from '@/common/utils/MessageUnique'; -import { CacheClassFuncAsyncExtend } from '@/common/utils/helper'; export class NTQQFriendApi { + context: InstanceContext; + core: NapCatCore; + constructor(context: InstanceContext, core: NapCatCore) { + this.context = context; + this.core = core; + } async getBuddyV2(refresh = false): Promise { let uids: string[] = []; - const buddyService = napCatCore.session.getBuddyService(); + const buddyService = this.context.session.getBuddyService(); const buddyListV2 = refresh ? await buddyService.getBuddyListV2('0', BuddyListReqType.KNOMAL) : await buddyService.getBuddyListV2('0', BuddyListReqType.KNOMAL); uids.push(...buddyListV2.data.flatMap(item => item.buddyUids)); - const data = await NTEventDispatch.CallNoListenerEvent( + const data = await this.core.eventWrapper.callNoListenerEvent( 'NodeIKernelProfileService/getCoreAndBaseInfo', 5000, 'nodeStore', uids ); return Array.from(data.values()); } - @CacheClassFuncAsyncExtend(3600 * 1000, 'getBuddyIdMap', () => true) async getBuddyIdMapCache(refresh = false): Promise> { - return await NTQQFriendApi.getBuddyIdMap(refresh); + return await this.getBuddyIdMap(refresh); } async getBuddyIdMap(refresh = false): Promise> { let uids: string[] = []; let retMap: LimitedHashTable = new LimitedHashTable(5000); - const buddyService = napCatCore.session.getBuddyService(); + const buddyService = this.context.session.getBuddyService(); const buddyListV2 = refresh ? await buddyService.getBuddyListV2('0', BuddyListReqType.KNOMAL) : await buddyService.getBuddyListV2('0', BuddyListReqType.KNOMAL); uids.push(...buddyListV2.data.flatMap(item => item.buddyUids)); - const data = await NTEventDispatch.CallNoListenerEvent( + const data = await this.core.eventWrapper.callNoListenerEvent( 'NodeIKernelProfileService/getCoreAndBaseInfo', 5000, 'nodeStore', uids ); data.forEach((value, key) => { @@ -36,7 +39,7 @@ export class NTQQFriendApi { async getBuddyV2ExWithCate(refresh = false) { let uids: string[] = []; let categoryMap: Map = new Map(); - const buddyService = napCatCore.session.getBuddyService(); + const buddyService = this.context.session.getBuddyService(); const buddyListV2 = refresh ? (await buddyService.getBuddyListV2('0', BuddyListReqType.KNOMAL)).data : (await buddyService.getBuddyListV2('0', BuddyListReqType.KNOMAL)).data; uids.push( ...buddyListV2.flatMap(item => { @@ -45,7 +48,7 @@ export class NTQQFriendApi { }); return item.buddyUids })); - const data = await NTEventDispatch.CallNoListenerEvent( + const data = await this.core.eventWrapper.callNoListenerEvent( 'NodeIKernelProfileService/getCoreAndBaseInfo', 5000, 'nodeStore', uids ); return Array.from(data).map(([key, value]) => { @@ -54,7 +57,7 @@ export class NTQQFriendApi { }); } async isBuddy(uid: string) { - return napCatCore.session.getBuddyService().isBuddy(uid); + return this.context.session.getBuddyService().isBuddy(uid); } /** * @deprecated @@ -62,7 +65,7 @@ export class NTQQFriendApi { * @returns */ async getFriends(forced = false): Promise { - let [_retData, _BuddyArg] = await NTEventDispatch.CallNormalEvent + let [_retData, _BuddyArg] = await this.core.eventWrapper.CallNormalEvent <(force: boolean) => Promise, (arg: OnBuddyChangeParams) => void> ( 'NodeIKernelBuddyService/getBuddyList', @@ -88,7 +91,7 @@ export class NTQQFriendApi { } let friendUid = data[0]; let reqTime = data[1]; - napCatCore.session.getBuddyService()?.approvalFriendRequest({ + this.context.session.getBuddyService()?.approvalFriendRequest({ friendUid: friendUid, reqTime: reqTime, accept diff --git a/src/core/apis/group.ts b/src/core/apis/group.ts index 5b07046c..6f9a5694 100644 --- a/src/core/apis/group.ts +++ b/src/core/apis/group.ts @@ -1,16 +1,20 @@ import { GroupMember, GroupRequestOperateTypes, GroupMemberRole, GroupNotify, Group, MemberExtSourceType, GroupNotifyTypes, ChatType, Peer, GroupListUpdateType } from '../entities'; -import { GeneralCallResult, NTQQUserApi, NodeIKernelGroupListener, NodeIKernelGroupService, napCatCore } from '@/core'; -import { NTEventDispatch } from '@/common/utils/EventTask'; -import { log } from '@/common/utils/log'; -import { groupMembers } from '../data'; -import { CacheClassFuncAsyncExtend, runAllWithTimeout } from '@/common/utils/helper'; +import { GeneralCallResult, InstanceContext, NapCatCore, NodeIKernelGroupService } from '@/core'; +import { runAllWithTimeout } from '@/common/utils/helper'; +import { NodeIKernelGroupListener } from '../listeners'; export class NTQQGroupApi { - async setGroupAvatar(gc: string, filePath: string) { - return napCatCore.session.getGroupService().setHeader(gc, filePath); + context: InstanceContext; + core: NapCatCore; + constructor(context: InstanceContext, core: NapCatCore) { + this.context = context; + this.core = core; } - async getGroups(forced = false) { + async setGroupAvatar(gc: string, filePath: string) { + return this.context.session.getGroupService().setHeader(gc, filePath); + } + async getGroups(forced = false) { type ListenerType = NodeIKernelGroupListener['onGroupListUpdate']; - let [_retData, _updateType, groupList] = await NTEventDispatch.CallNormalEvent + let [_retData, _updateType, groupList] = await this.core.eventWrapper.CallNormalEvent <(force: boolean) => Promise, ListenerType> ( 'NodeIKernelGroupService/getGroupList', @@ -22,10 +26,8 @@ export class NTQQGroupApi { ); return groupList; } - - @CacheClassFuncAsyncExtend(3600 * 1000, "LastestSendTime", () => true) - async getGroupMemberLastestSendTimeCache(GroupCode: string) { - return NTQQGroupApi.getGroupMemberLastestSendTime(GroupCode); + async getGroupMemberLastestSendTimeCache(GroupCode: string) { + return this.getGroupMemberLastestSendTime(GroupCode); } /** * 通过QQ自带数据库获取群成员最后发言时间(仅返回有效数据 且消耗延迟大 需要进行缓存) @@ -37,9 +39,9 @@ export class NTQQGroupApi { * console.log(uin, sendTime); * } */ - async getGroupMemberLastestSendTime(GroupCode: string) { + async getGroupMemberLastestSendTime(GroupCode: string) { async function getdata(uid: string) { - let NTRet = await NTQQGroupApi.getLastestMsgByUids(GroupCode, [uid]); + let NTRet = await this.getLastestMsgByUids(GroupCode, [uid]); if (NTRet.result != 0 && NTRet.msgList.length < 1) { return undefined; } @@ -65,8 +67,8 @@ export class NTQQGroupApi { } return ret; } - async getLastestMsgByUids(GroupCode: string, uids: string[]) { - let ret = await napCatCore.session.getMsgService().queryMsgsWithFilterEx('0', '0', '0', { + async getLastestMsgByUids(GroupCode: string, uids: string[]) { + let ret = await this.context.session.getMsgService().queryMsgsWithFilterEx('0', '0', '0', { chatInfo: { peerUid: GroupCode, chatType: ChatType.group, @@ -81,18 +83,18 @@ export class NTQQGroupApi { }); return ret; } - async getGroupMemberAll(GroupCode: string, forced = false) { - return napCatCore.session.getGroupService().getAllMemberList(GroupCode, forced); + async getGroupMemberAll(GroupCode: string, forced = false) { + return this.context.session.getGroupService().getAllMemberList(GroupCode, forced); } - async getLastestMsg(GroupCode: string, uins: string[]) { + async getLastestMsg(GroupCode: string, uins: string[]) { let uids: Array = []; for (let uin of uins) { - let uid = await NTQQUserApi.getUidByUin(uin) + let uid = await this.core.getApiContext().UserApi.getUidByUin(uin) if (uid) { uids.push(uid); } } - let ret = await napCatCore.session.getMsgService().queryMsgsWithFilterEx('0', '0', '0', { + let ret = await this.context.session.getMsgService().queryMsgsWithFilterEx('0', '0', '0', { chatInfo: { peerUid: GroupCode, chatType: ChatType.group, @@ -107,44 +109,44 @@ export class NTQQGroupApi { }); return ret; } - async getGroupRecommendContactArkJson(GroupCode: string) { - return napCatCore.session.getGroupService().getGroupRecommendContactArkJson(GroupCode); + async getGroupRecommendContactArkJson(GroupCode: string) { + return this.context.session.getGroupService().getGroupRecommendContactArkJson(GroupCode); } - async CreatGroupFileFolder(groupCode: string, folderName: string) { - return napCatCore.session.getRichMediaService().createGroupFolder(groupCode, folderName); + async CreatGroupFileFolder(groupCode: string, folderName: string) { + return this.context.session.getRichMediaService().createGroupFolder(groupCode, folderName); } - async DelGroupFile(groupCode: string, files: string[]) { - return napCatCore.session.getRichMediaService().deleteGroupFile(groupCode, [102], files); + async DelGroupFile(groupCode: string, files: string[]) { + return this.context.session.getRichMediaService().deleteGroupFile(groupCode, [102], files); } - async DelGroupFileFolder(groupCode: string, folderId: string) { - return napCatCore.session.getRichMediaService().deleteGroupFolder(groupCode, folderId); + async DelGroupFileFolder(groupCode: string, folderId: string) { + return this.context.session.getRichMediaService().deleteGroupFolder(groupCode, folderId); } - async addGroupEssence(GroupCode: string, msgId: string) { + async addGroupEssence(GroupCode: string, msgId: string) { // 代码没测过 // 需要 ob11msgid->msgId + (peer) -> msgSeq + msgRandom - let MsgData = await napCatCore.session.getMsgService().getMsgsIncludeSelf({ chatType: 2, guildId: '', peerUid: GroupCode }, msgId, 1, false); + let MsgData = await this.context.session.getMsgService().getMsgsIncludeSelf({ chatType: 2, guildId: '', peerUid: GroupCode }, msgId, 1, false); let param = { groupCode: GroupCode, msgRandom: parseInt(MsgData.msgList[0].msgRandom), msgSeq: parseInt(MsgData.msgList[0].msgSeq) }; // GetMsgByShoretID(ShoretID); -> MsgService.getMsgs(Peer,MsgId,1,false); -> 组出参数 - return napCatCore.session.getGroupService().addGroupEssence(param); + return this.context.session.getGroupService().addGroupEssence(param); } - async removeGroupEssence(GroupCode: string, msgId: string) { + async removeGroupEssence(GroupCode: string, msgId: string) { // 代码没测过 // 需要 ob11msgid->msgId + (peer) -> msgSeq + msgRandom - let MsgData = await napCatCore.session.getMsgService().getMsgsIncludeSelf({ chatType: 2, guildId: '', peerUid: GroupCode }, msgId, 1, false); + let MsgData = await this.context.session.getMsgService().getMsgsIncludeSelf({ chatType: 2, guildId: '', peerUid: GroupCode }, msgId, 1, false); let param = { groupCode: GroupCode, msgRandom: parseInt(MsgData.msgList[0].msgRandom), msgSeq: parseInt(MsgData.msgList[0].msgSeq) }; // GetMsgByShoretID(ShoretID); -> MsgService.getMsgs(Peer,MsgId,1,false); -> 组出参数 - return napCatCore.session.getGroupService().removeGroupEssence(param); + return this.context.session.getGroupService().removeGroupEssence(param); } - async getSingleScreenNotifies(num: number) { - let [_retData, _doubt, _seq, notifies] = await NTEventDispatch.CallNormalEvent + async getSingleScreenNotifies(num: number) { + let [_retData, _doubt, _seq, notifies] = await this.core.eventWrapper.CallNormalEvent <(arg1: boolean, arg2: string, arg3: number) => Promise, (doubt: boolean, seq: string, notifies: GroupNotify[]) => void> ( 'NodeIKernelGroupService/getSingleScreenNotifies', @@ -158,12 +160,12 @@ export class NTQQGroupApi { ); return notifies; } - async getGroupMemberV2(GroupCode: string, uid: string, forced = false) { + async getGroupMemberV2(GroupCode: string, uid: string, forced = false) { type ListenerType = NodeIKernelGroupListener['onMemberInfoChange']; type EventType = NodeIKernelGroupService['getMemberInfo']; // NTEventDispatch.CreatListenerFunction('NodeIKernelGroupListener/onGroupMemberInfoUpdate', //return napCatCore.session.getGroupService().getMemberInfo(GroupCode, [uid], forced); - const [ret, _groupCode, _changeType, _members] = await NTEventDispatch.CallNormalEvent + const [ret, _groupCode, _changeType, _members] = await this.core.eventWrapper.CallNormalEvent ( 'NodeIKernelGroupService/getMemberInfo', @@ -177,8 +179,8 @@ export class NTQQGroupApi { ); return _members.get(uid); } - async getGroupMembers(groupQQ: string, num = 3000): Promise> { - const groupService = napCatCore.session.getGroupService(); + async getGroupMembers(groupQQ: string, num = 3000): Promise> { + const groupService = this.context.session.getGroupService(); const sceneId = groupService.createMemberListScene(groupQQ, 'groupMemberList_MainWindow'); const result = await groupService.getNextMemberList(sceneId!, undefined, num); if (result.errCode !== 0) { @@ -196,18 +198,18 @@ export class NTQQGroupApi { */ } - async getGroupNotifies() { + async getGroupNotifies() { // 获取管理员变更 // 加群通知,退出通知,需要管理员权限 } - async GetGroupFileCount(Gids: Array) { - return napCatCore.session.getRichMediaService().batchGetGroupFileCount(Gids); + async GetGroupFileCount(Gids: Array) { + return this.context.session.getRichMediaService().batchGetGroupFileCount(Gids); } - async getGroupIgnoreNotifies() { + async getGroupIgnoreNotifies() { } - async getArkJsonGroupShare(GroupCode: string) { - let ret = await NTEventDispatch.CallNoListenerEvent + async getArkJsonGroupShare(GroupCode: string) { + let ret = await this.core.eventWrapper.callNoListenerEvent <(GroupId: string) => Promise>( 'NodeIKernelGroupService/getGroupRecommendContactArkJson', 5000, @@ -216,17 +218,17 @@ export class NTQQGroupApi { return ret.arkJson; } //需要异常处理 - async uploadGroupBulletinPic(GroupCode: string, imageurl: string) { - const _Pskey = (await NTQQUserApi.getPSkey(['qun.qq.com'])).domainPskeyMap.get('qun.qq.com')!; - return napCatCore.session.getGroupService().uploadGroupBulletinPic(GroupCode, _Pskey, imageurl); + async uploadGroupBulletinPic(GroupCode: string, imageurl: string) { + const _Pskey = (await this.core.getApiContext().UserApi.getPSkey(['qun.qq.com'])).domainPskeyMap.get('qun.qq.com')!; + return this.context.session.getGroupService().uploadGroupBulletinPic(GroupCode, _Pskey, imageurl); } - async handleGroupRequest(flag: string, operateType: GroupRequestOperateTypes, reason?: string) { + async handleGroupRequest(flag: string, operateType: GroupRequestOperateTypes, reason?: string) { let flagitem = flag.split('|'); let groupCode = flagitem[0]; let seq = flagitem[1]; let type = parseInt(flagitem[2]); - return napCatCore.session.getGroupService().operateSysNotify( + return this.context.session.getGroupService().operateSysNotify( false, { 'operateType': operateType, // 2 拒绝 @@ -239,42 +241,42 @@ export class NTQQGroupApi { }); } - async quitGroup(groupQQ: string) { - return napCatCore.session.getGroupService().quitGroup(groupQQ); + async quitGroup(groupQQ: string) { + return this.context.session.getGroupService().quitGroup(groupQQ); } - async kickMember(groupQQ: string, kickUids: string[], refuseForever: boolean = false, kickReason: string = '') { - return napCatCore.session.getGroupService().kickMember(groupQQ, kickUids, refuseForever, kickReason); + async kickMember(groupQQ: string, kickUids: string[], refuseForever: boolean = false, kickReason: string = '') { + return this.context.session.getGroupService().kickMember(groupQQ, kickUids, refuseForever, kickReason); } - async banMember(groupQQ: string, memList: Array<{ uid: string, timeStamp: number }>) { + async banMember(groupQQ: string, memList: Array<{ uid: string, timeStamp: number }>) { // timeStamp为秒数, 0为解除禁言 - return napCatCore.session.getGroupService().setMemberShutUp(groupQQ, memList); + return this.context.session.getGroupService().setMemberShutUp(groupQQ, memList); } - async banGroup(groupQQ: string, shutUp: boolean) { - return napCatCore.session.getGroupService().setGroupShutUp(groupQQ, shutUp); + async banGroup(groupQQ: string, shutUp: boolean) { + return this.context.session.getGroupService().setGroupShutUp(groupQQ, shutUp); } - async setMemberCard(groupQQ: string, memberUid: string, cardName: string) { - return napCatCore.session.getGroupService().modifyMemberCardName(groupQQ, memberUid, cardName); + async setMemberCard(groupQQ: string, memberUid: string, cardName: string) { + return this.context.session.getGroupService().modifyMemberCardName(groupQQ, memberUid, cardName); } - async setMemberRole(groupQQ: string, memberUid: string, role: GroupMemberRole) { - return napCatCore.session.getGroupService().modifyMemberRole(groupQQ, memberUid, role); + async setMemberRole(groupQQ: string, memberUid: string, role: GroupMemberRole) { + return this.context.session.getGroupService().modifyMemberRole(groupQQ, memberUid, role); } - async setGroupName(groupQQ: string, groupName: string) { - return napCatCore.session.getGroupService().modifyGroupName(groupQQ, groupName, false); + async setGroupName(groupQQ: string, groupName: string) { + return this.context.session.getGroupService().modifyGroupName(groupQQ, groupName, false); } // 头衔不可用 - async setGroupTitle(groupQQ: string, uid: string, title: string) { + async setGroupTitle(groupQQ: string, uid: string, title: string) { } - async publishGroupBulletin(groupQQ: string, content: string, picInfo: { id: string, width: number, height: number } | undefined = undefined, pinned: number = 0, confirmRequired: number = 0,) { - const _Pskey = (await NTQQUserApi.getPSkey(['qun.qq.com'])).domainPskeyMap.get('qun.qq.com'); + async publishGroupBulletin(groupQQ: string, content: string, picInfo: { id: string, width: number, height: number } | undefined = undefined, pinned: number = 0, confirmRequired: number = 0,) { + const _Pskey = (await this.core.getApiContext().UserApi.getPSkey(['qun.qq.com'])).domainPskeyMap.get('qun.qq.com'); //text是content内容url编码 let data = { text: encodeURI(content), @@ -283,14 +285,14 @@ export class NTQQGroupApi { pinned: pinned, confirmRequired: confirmRequired }; - return napCatCore.session.getGroupService().publishGroupBulletin(groupQQ, _Pskey!, data); + return this.context.session.getGroupService().publishGroupBulletin(groupQQ, _Pskey!, data); } - async getGroupRemainAtTimes(GroupCode: string) { - napCatCore.session.getGroupService().getGroupRemainAtTimes(GroupCode); + async getGroupRemainAtTimes(GroupCode: string) { + this.context.session.getGroupService().getGroupRemainAtTimes(GroupCode); } - async getMemberExtInfo(groupCode: string, uin: string) { + async getMemberExtInfo(groupCode: string, uin: string) { // 仅NTQQ 9.9.11 24568测试 容易炸开谨慎使用 - return napCatCore.session.getGroupService().getMemberExtInfo( + return this.context.session.getGroupService().getMemberExtInfo( { groupCode: groupCode, sourceType: MemberExtSourceType.TITLETYPE, diff --git a/src/core/index.ts b/src/core/index.ts index 88249339..987f6ae1 100644 --- a/src/core/index.ts +++ b/src/core/index.ts @@ -1,3 +1,4 @@ export * from './core'; export * from './wrapper'; -export * from './entities'; \ No newline at end of file +export * from './entities'; +export * from './services'; \ No newline at end of file