Skip to content

Commit

Permalink
refactor: #637
Browse files Browse the repository at this point in the history
  • Loading branch information
MliKiowa committed Dec 21, 2024
1 parent 8c22f11 commit c94936d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 21 deletions.
5 changes: 2 additions & 3 deletions src/onebot/action/group/SetGroupAddRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ export default class SetGroupAddRequest extends OneBotAction<Payload, null> {
const flag = payload.flag.toString();
const approve = payload.approve?.toString() !== 'false';
const reason = payload.reason ?? ' ';

const notify = await this.findNotify(flag);
let invite_notify = this.obContext.apis.MsgApi.notifyGroupInvite.get(flag);
const notify = invite_notify ?? await this.findNotify(flag);
if (!notify) {
throw new Error('No such request');
}

await this.core.apis.GroupApi.handleGroupRequest(
notify,
approve ? NTGroupRequestOperateTypes.KAGREE : NTGroupRequestOperateTypes.KREFUSE,
Expand Down
72 changes: 57 additions & 15 deletions src/onebot/api/msg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
SendTextElement,
FaceType,
GrayTipElement,
GroupNotify,
} from '@/core';
import faceConfig from '@/core/external/face_config.json';
import { NapCatOneBot11Adapter, OB11Message, OB11MessageData, OB11MessageDataType, OB11MessageFileBase, OB11MessageForward, } from '@/onebot';
Expand All @@ -35,6 +36,7 @@ import { GroupAdmin } from '@/core/packet/transformer/proto/message/groupAdmin';
import { OB11GroupAdminNoticeEvent } from '../event/notice/OB11GroupAdminNoticeEvent';
import { GroupChange, GroupChangeInfo, GroupInvite, PushMsgBody } from '@/core/packet/transformer/proto';
import { OB11GroupRequestEvent } from '../event/request/OB11GroupRequest';
import { LRUCache } from '@/common/lru-cache';

type RawToOb11Converters = {
[Key in keyof MessageElement as Key extends `${string}Element` ? Key : never]: (
Expand Down Expand Up @@ -68,7 +70,8 @@ function keyCanBeParsed(key: string, parser: RawToOb11Converters): key is keyof
export class OneBotMsgApi {
obContext: NapCatOneBot11Adapter;
core: NapCatCore;

notifyGroupInvite: LRUCache<string, GroupNotify> = new LRUCache(50);
// seq -> notify
rawToOb11Converters: RawToOb11Converters = {
textElement: async element => {
if (element.atType === NTMsgAtType.ATTYPEUNKNOWN) {
Expand Down Expand Up @@ -1037,22 +1040,61 @@ export class OneBotMsgApi {
} else if (SysMessage.contentHead.type == 87 && SysMessage.body?.msgContent) {
let groupInvite = new NapProtoMsg(GroupInvite).decode(SysMessage.body.msgContent);
let request_seq = '';
await this.core.eventWrapper.registerListen('NodeIKernelMsgListener/onRecvMsg', (msgs) => {
for (const msg of msgs) {
if (msg.senderUid === groupInvite.invitorUid && msg.msgType === 11) {
let jumpUrl = JSON.parse(msg.elements.find(e => e.elementType === 10)?.arkElement?.bytesData ?? '').meta?.news?.jumpUrl;
let jumpUrlParams = new URLSearchParams(jumpUrl);
let groupcode = jumpUrlParams.get('groupcode');
let receiveruin = jumpUrlParams.get('receiveruin');
let msgseq = jumpUrlParams.get('msgseq');
request_seq = msgseq ?? '';
if (groupcode === groupInvite.groupUin.toString() && receiveruin === this.core.selfInfo.uin) {
return true;
try {
await this.core.eventWrapper.registerListen('NodeIKernelMsgListener/onRecvMsg', (msgs) => {
for (const msg of msgs) {
if (msg.senderUid === groupInvite.invitorUid && msg.msgType === 11) {
let jumpUrl = JSON.parse(msg.elements.find(e => e.elementType === 10)?.arkElement?.bytesData ?? '').meta?.news?.jumpUrl;
let jumpUrlParams = new URLSearchParams(jumpUrl);
let groupcode = jumpUrlParams.get('groupcode');
let receiveruin = jumpUrlParams.get('receiveruin');
let msgseq = jumpUrlParams.get('msgseq');
request_seq = msgseq ?? '';
if (groupcode === groupInvite.groupUin.toString() && receiveruin === this.core.selfInfo.uin) {
return true;
}
}
}
}
return false;
}, 1, 1000);
return false;
}, 1, 1000);
} catch (error) {
request_seq = '';
}
// 未拉取到seq
if (request_seq === '') {
return;
}
// 创建个假的
this.notifyGroupInvite.put(request_seq, {
seq: request_seq,
type: 1,
group: {
groupCode: groupInvite.groupUin.toString(),
groupName: '',
},
user1: {
uid: groupInvite.invitorUid,
nickName: '',
},
user2: {
uid: this.core.selfInfo.uid,
nickName: '',
},
actionUser: {
uid: groupInvite.invitorUid,
nickName: '',
},
actionTime: Date.now().toString(),
postscript: '',
repeatSeqs: [],
warningTips: '',
invitationExt: {
srcType: 1,
groupCode: groupInvite.groupUin.toString(),
waitStatus: 1,
},
status: 1
})
return new OB11GroupRequestEvent(
this.core,
+groupInvite.groupUin,
Expand Down
8 changes: 5 additions & 3 deletions src/onebot/api/quick-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,13 @@ export class OneBotQuickActionApi {
}

async handleGroupRequest(request: OB11GroupRequestEvent, quickAction: QuickActionGroupRequest) {
const noify = await this.findNotify(request.flag);

let invite_notify = this.obContext.apis.MsgApi.notifyGroupInvite.get(request.flag);
const notify = invite_notify ?? await this.findNotify(request.flag);

if (!isNull(quickAction.approve) && noify) {
if (!isNull(quickAction.approve) && notify) {
this.core.apis.GroupApi.handleGroupRequest(
noify,
notify,
quickAction.approve ? NTGroupRequestOperateTypes.KAGREE : NTGroupRequestOperateTypes.KREFUSE,
quickAction.reason,
).catch(e => this.core.context.logger.logError(e));
Expand Down

0 comments on commit c94936d

Please sign in to comment.