feat: QuickHandle From LLOB

This commit is contained in:
手瓜一十雪 2024-05-18 14:35:24 +08:00
parent 076c9cfed7
commit b3c6e2a0f3
4 changed files with 92 additions and 79 deletions

View File

@ -0,0 +1,17 @@
import { log } from '@/common/utils/log';
import BaseAction from '../BaseAction'
import { ActionName } from '../types'
import { QuickAction, QuickActionEvent, handleQuickOperation } from '@/onebot11/server/postOB11Event';
interface Payload{
context: QuickActionEvent,
operation: QuickAction
}
export class GoCQHTTHandleQuickOperation extends BaseAction<Payload, null>{
actionName = ActionName.GoCQHTTP_HandleQuickOperation
protected async _handle(payload: Payload): Promise<null> {
handleQuickOperation(payload.context, payload.operation).then().catch(log);
return null
}
}

View File

@ -42,13 +42,13 @@ export async function sendMsg(peer: Peer, sendElements: SendMessageElement[], de
try {
for (const fileElement of sendElements) {
if (fileElement.elementType === ElementType.PTT) {
totalSize += fs.statSync(fileElement.pttElement.filePath).size
totalSize += fs.statSync(fileElement.pttElement.filePath).size;
}
if (fileElement.elementType === ElementType.FILE) {
totalSize += fs.statSync(fileElement.fileElement.filePath).size
totalSize += fs.statSync(fileElement.fileElement.filePath).size;
}
if (fileElement.elementType === ElementType.VIDEO) {
totalSize += fs.statSync(fileElement.videoElement.filePath).size
totalSize += fs.statSync(fileElement.videoElement.filePath).size;
}
if (fileElement.elementType === ElementType.PIC) {
totalSize += fs.statSync(fileElement.picElement.sourcePath).size
@ -57,7 +57,7 @@ export async function sendMsg(peer: Peer, sendElements: SendMessageElement[], de
//且 PredictTime ((totalSize / 1024 / 512) * 1000)不等于Nan
let PredictTime = totalSize / 1024 / 512 * 1000;
if (!Number.isNaN(PredictTime)) {
timeout += PredictTime// 10S Basic Timeout + PredictTime( For File 512kb/s )
timeout += PredictTime// 5S Basic Timeout + PredictTime( For File 512kb/s )
}
} catch (e) {
logError("发送消息计算预计时间异常", e);

View File

@ -60,6 +60,7 @@ export enum ActionName {
CleanCache = 'clean_cache',
GetCookies = 'get_cookies',
// 以下为go-cqhttp api
GoCQHTTP_HandleQuickOperation = ".handle_quick_operation",
GetGroupHonorInfo = 'get_group_honor_info',
GoCQHTTP_GetEssenceMsg = 'get_essence_msg_list',
GoCQHTTP_SendGroupNotice = '_send_group_notice',

View File

@ -16,7 +16,7 @@ import { friendRequests, getGroup, getUidByUin, groupNotifies, selfInfo } from '
import { NTQQFriendApi, NTQQGroupApi, NTQQMsgApi } from '@/core/apis';
import createSendElements from '../action/msg/SendMsg/create-send-elements';
export type PostEventType = OB11Message | OB11BaseMetaEvent | OB11BaseNoticeEvent
export type QuickActionEvent = OB11Message | OB11BaseMetaEvent | OB11BaseNoticeEvent
interface QuickActionPrivateMessage {
reply?: string;
@ -43,7 +43,7 @@ interface QuickActionGroupRequest {
reason?: string;
}
type QuickAction =
export type QuickAction =
QuickActionPrivateMessage
& QuickActionGroupMessage
& QuickActionFriendRequest
@ -62,7 +62,7 @@ export function unregisterWsEventSender(ws: WebSocketClass) {
}
}
export function postWsEvent(event: PostEventType) {
export function postWsEvent(event: QuickActionEvent) {
for (const ws of eventWSList) {
new Promise(() => {
wsReply(ws, event);
@ -70,7 +70,7 @@ export function postWsEvent(event: PostEventType) {
}
}
export function postOB11Event(msg: PostEventType, reportSelf = false, postWs = true) {
export function postOB11Event(msg: QuickActionEvent, reportSelf = false, postWs = true) {
const config = ob11Config;
// 判断msg是否是event
@ -108,77 +108,7 @@ export function postOB11Event(msg: PostEventType, reportSelf = false, postWs = t
return;
}
try {
if (msg.post_type === 'message') {
msg = msg as OB11Message;
const rawMessage = await dbUtil.getMsgByShortId(msg.message_id);
resJson = resJson as QuickActionPrivateMessage | QuickActionGroupMessage;
const reply = resJson.reply;
const peer: Peer = {
chatType: ChatType.friend,
peerUid: getUidByUin(msg.user_id.toString()) as string
};
if (msg.message_type == 'private') {
if (msg.sub_type === 'group') {
peer.chatType = ChatType.temp;
}
} else {
peer.chatType = ChatType.group;
peer.peerUid = msg.group_id!.toString();
}
if (reply) {
let group: Group | undefined;
let replyMessage: OB11MessageData[] = [];
if (msg.message_type == 'group') {
group = await getGroup(msg.group_id!.toString());
if ((resJson as QuickActionGroupMessage).at_sender) {
replyMessage.push({
type: 'at',
data: {
qq: msg.user_id.toString()
}
} as OB11MessageAt);
}
}
replyMessage = replyMessage.concat(normalize(reply, resJson.auto_escape));
const { sendElements, deleteAfterSentFiles } = await createSendElements(replyMessage, group);
sendMsg(peer, sendElements, deleteAfterSentFiles, false).then().catch(logError);
} else if (resJson.delete) {
NTQQMsgApi.recallMsg(peer, [rawMessage!.msgId]).then().catch(logError);
} else if (resJson.kick) {
NTQQGroupApi.kickMember(peer.peerUid, [rawMessage!.senderUid]).then().catch(logError);
} else if (resJson.ban) {
NTQQGroupApi.banMember(peer.peerUid, [{
uid: rawMessage!.senderUid,
timeStamp: resJson.ban_duration || 60 * 30
}],).then().catch(logError);
}
} else if (msg.post_type === 'request') {
if ((msg as OB11FriendRequestEvent).request_type === 'friend') {
resJson = resJson as QuickActionFriendRequest;
if (!isNull(resJson.approve)) {
// todo: set remark
const flag = (msg as OB11FriendRequestEvent).flag;
// const [friendUid, seq] = flag.split('|');
const request = friendRequests[flag];
NTQQFriendApi.handleFriendRequest(
request,
!!resJson.approve,
).then().catch(logError);
}
} else if ((msg as OB11GroupRequestEvent).request_type === 'group') {
resJson = resJson as QuickActionGroupRequest;
if (!isNull(resJson.approve)) {
const flag = (msg as OB11GroupRequestEvent).flag;
const request = groupNotifies[flag];
// const [groupCode, seq] = flag.split('|');
NTQQGroupApi.handleGroupRequest(request,
resJson.approve ? GroupRequestOperateTypes.approve : GroupRequestOperateTypes.reject
).then().catch(logError);
}
}
}
handleQuickOperation(msg as QuickActionEvent, resJson).then().catch(logError);
} catch (e: any) {
logError('新消息事件HTTP上报返回快速操作失败', e);
}
@ -192,3 +122,68 @@ export function postOB11Event(msg: PostEventType, reportSelf = false, postWs = t
postWsEvent(msg);
}
}
async function handleMsg(msg: OB11Message, quickAction: QuickAction) {
msg = msg as OB11Message;
const rawMessage = await dbUtil.getMsgByShortId(msg.message_id);
const reply = quickAction.reply;
const peer: Peer = {
chatType: ChatType.friend,
peerUid: getUidByUin(msg.user_id.toString()) as string
};
if (msg.message_type == 'private') {
if (msg.sub_type === 'group') {
peer.chatType = ChatType.temp;
}
} else {
peer.chatType = ChatType.group;
peer.peerUid = msg.group_id!.toString();
}
if (reply) {
let group: Group | undefined;
let replyMessage: OB11MessageData[] = [];
if (msg.message_type == 'group') {
group = await getGroup(msg.group_id!.toString());
if ((quickAction as QuickActionGroupMessage).at_sender) {
replyMessage.push({
type: 'at',
data: {
qq: msg.user_id.toString()
}
} as OB11MessageAt);
}
}
replyMessage = replyMessage.concat(normalize(reply, quickAction.auto_escape));
const { sendElements, deleteAfterSentFiles } = await createSendElements(replyMessage, group);
sendMsg(peer, sendElements, deleteAfterSentFiles, false).then().catch(logError);
}
}
async function handleGroupRequest(request: OB11GroupRequestEvent, quickAction: QuickActionGroupRequest) {
if (!isNull(quickAction.approve)) {
NTQQGroupApi.handleGroupRequest(
groupNotifies[request.flag],
quickAction.approve ? GroupRequestOperateTypes.approve : GroupRequestOperateTypes.reject,
quickAction.reason,
).then().catch(logError)
}
}
async function handleFriendRequest(request: OB11FriendRequestEvent, quickAction: QuickActionFriendRequest) {
if (!isNull(quickAction.approve)) {
NTQQFriendApi.handleFriendRequest(friendRequests[request.flag], !!quickAction.approve).then().catch(logError)
}
}
export async function handleQuickOperation(context: QuickActionEvent, quickAction: QuickAction) {
if (context.post_type === 'message') {
handleMsg(context as OB11Message, quickAction).then().catch(logError)
}
if (context.post_type === 'request') {
const friendRequest = context as OB11FriendRequestEvent;
const groupRequest = context as OB11GroupRequestEvent;
if ((friendRequest).request_type === 'friend') {
handleFriendRequest(friendRequest, quickAction).then().catch(logError)
}
else if (groupRequest.request_type === 'group') {
handleGroupRequest(groupRequest, quickAction).then().catch(logError)
}
}
}