chore: 推动重构

This commit is contained in:
手瓜一十雪 2024-08-22 15:06:49 +08:00
parent a3e8c9b28a
commit a3bd4c0f73
3 changed files with 93 additions and 68 deletions

View File

@ -1,6 +1,6 @@
import { UUIDConverter } from '@/common/utils/helper';
import { MessageUnique } from '@/common/utils/MessageUnique';
import { AtType, ElementWrapper, MessageElement, NapCatCore, PicElement, RawMessage, ReplyElement, TextElement } from '@/core';
import { AtType, ElementType, MarketFaceElement, NapCatCore, PicElement, RawMessage, ReplyElement, TextElement, VideoElement } from '@/core';
import { NapCatOneBot11Adapter, OB11MessageData, OB11MessageDataType } from '@/onebot';
@ -82,6 +82,33 @@ export class OneBotMsgApi {
message_data['data']['file_size'] = picElement.fileSize;
return message_data;
}
async parseMarketFaceElement(msg: RawMessage, elementId: string, elementType: ElementType, marketFaceElement: MarketFaceElement) {
const NTQQFileApi = this.coreContext.apis.FileApi;
let message_data: OB11MessageData = {
data: {} as any,
type: 'unknown' as any,
};
message_data['type'] = OB11MessageDataType.image;
message_data['data']['file'] = 'marketface';
message_data['data']['file_id'] = UUIDConverter.encode(msg.peerUin, msg.msgId);
message_data['data']['path'] = elementId;
message_data['data']['url'] = elementId;
await NTQQFileApi.addFileCache(
{
peerUid: msg.peerUid,
chatType: msg.chatType,
guildId: '',
},
msg.msgId,
msg.msgSeq,
msg.senderUid,
elementId,
elementType.toString(),
'0',
'marketface'
);
return message_data;
}
async parseReplyElement(msg: RawMessage, replyElement: ReplyElement) {
const NTQQMsgApi = this.coreContext.apis.MsgApi;
let message_data: OB11MessageData = {
@ -131,4 +158,63 @@ export class OneBotMsgApi {
}
return message_data;
}
async parseVideoElement(msg: RawMessage, elementId: string, elementType: ElementType, videoElement: VideoElement) {
const NTQQFileApi = this.coreContext.apis.FileApi;
let message_data: OB11MessageData = {
data: {} as any,
type: 'unknown' as any,
};
//读取视频链接并兜底
let videoUrl; //Array
if (msg.peerUin === '284840486') {
//合并消息内部 应该进行特殊处理 可能需要重写peer 待测试与研究 Mlikiowa Taged TODO
}
try {
videoUrl = await NTQQFileApi.getVideoUrl({
chatType: msg.chatType,
peerUid: msg.peerUid,
guildId: '0',
}, msg.msgId, elementId);
} catch (error) {
videoUrl = undefined;
}
//读取在线URL
let videoDownUrl = undefined;
if (videoUrl) {
const videoDownUrlTemp = videoUrl.find((url) => {
return !!url.url;
});
if (videoDownUrlTemp) {
videoDownUrl = videoDownUrlTemp.url;
}
}
//开始兜底
if (!videoDownUrl) {
videoDownUrl = videoElement.filePath;
}
message_data['type'] = OB11MessageDataType.video;
message_data['data']['file'] = videoElement.fileName;
message_data['data']['path'] = videoDownUrl;
message_data['data']['url'] = videoDownUrl;
message_data['data']['file_id'] = UUIDConverter.encode(msg.peerUin, msg.msgId);
message_data['data']['file_size'] = videoElement.fileSize;
await NTQQFileApi.addFileCache(
{
peerUid: msg.peerUid,
chatType: msg.chatType,
guildId: '',
},
msg.msgId,
msg.msgSeq,
msg.senderUid,
elementId,
elementType.toString(),
videoElement.fileSize || '0',
videoElement.fileName
);
return message_data;
}
}

View File

@ -89,8 +89,8 @@ export async function RawNTMsg2Onebot(
} else if (element.picElement) {
let PicMsgData = await obcore.apiContext.MsgApi.parsePicElement(msg, element.picElement);
if (PicMsgData) message_data = PicMsgData
;
if (PicMsgData) message_data = PicMsgData;
} else if (element.fileElement) {
const FileElement = element.fileElement;
message_data['type'] = OB11MessageDataType.file;
@ -114,58 +114,8 @@ export async function RawNTMsg2Onebot(
FileElement.fileName
);
} else if (element.videoElement) {
const videoElement: VideoElement = element.videoElement;
//读取视频链接并兜底
let videoUrl; //Array
if (msg.peerUin === '284840486') {
//合并消息内部 应该进行特殊处理 可能需要重写peer 待测试与研究 Mlikiowa Taged TODO
}
try {
videoUrl = await NTQQFileApi.getVideoUrl({
chatType: msg.chatType,
peerUid: msg.peerUid,
guildId: '0',
}, msg.msgId, element.elementId);
} catch (error) {
videoUrl = undefined;
}
//读取在线URL
let videoDownUrl = undefined;
if (videoUrl) {
const videoDownUrlTemp = videoUrl.find((url) => {
return !!url.url;
});
if (videoDownUrlTemp) {
videoDownUrl = videoDownUrlTemp.url;
}
}
//开始兜底
if (!videoDownUrl) {
videoDownUrl = videoElement.filePath;
}
message_data['type'] = OB11MessageDataType.video;
message_data['data']['file'] = videoElement.fileName;
message_data['data']['path'] = videoDownUrl;
message_data['data']['url'] = videoDownUrl;
message_data['data']['file_id'] = UUIDConverter.encode(msg.peerUin, msg.msgId);
message_data['data']['file_size'] = videoElement.fileSize;
await NTQQFileApi.addFileCache(
{
peerUid: msg.peerUid,
chatType: msg.chatType,
guildId: '',
},
msg.msgId,
msg.msgSeq,
msg.senderUid,
element.elementId,
element.elementType.toString(),
videoElement.fileSize || '0',
videoElement.fileName
);
let videoMsgData = await obcore.apiContext.MsgApi.parseVideoElement(msg, element.elementId, element.elementType, element.videoElement);
if (videoMsgData) message_data = videoMsgData;
} else if (element.pttElement) {
message_data['type'] = OB11MessageDataType.voice;
message_data['data']['file'] = element.pttElement.fileName;
@ -203,18 +153,8 @@ export async function RawNTMsg2Onebot(
message_data['data']['id'] = element.faceElement.faceIndex.toString();
}
} else if (element.marketFaceElement) {
message_data['type'] = OB11MessageDataType.mface;
message_data['data']['summary'] = element.marketFaceElement.faceName;
const md5 = element.marketFaceElement.emojiId;
// 取md5的前两位
const dir = md5.substring(0, 2);
// 获取组装url
// const url = `https://p.qpic.cn/CDN_STATIC/0/data/imgcache/htdocs/club/item/parcel/item/${dir}/${md5}/300x300.gif?max_age=31536000`;
message_data['data']['url'] = `https://gxh.vip.qq.com/club/item/parcel/item/${dir}/${md5}/raw300.gif`;
message_data['data']['emoji_id'] = element.marketFaceElement.emojiId;
message_data['data']['emoji_package_id'] = String(element.marketFaceElement.emojiPackageId);
message_data['data']['key'] = element.marketFaceElement.key;
//mFaceCache.set(md5, element.marketFaceElement.faceName);
let marketFaceMsgData = await obcore.apiContext.MsgApi.parseMarketFaceElement(msg, element.elementId, element.elementType, element.marketFaceElement);
if (marketFaceMsgData) message_data = marketFaceMsgData;
} else if (element.markdownElement) {
message_data['type'] = OB11MessageDataType.markdown;
message_data['data']['data'] = element.markdownElement.content;

View File

@ -26,7 +26,6 @@ import { ActionMap, createActionMap } from '@/onebot/action';
import { WebUiDataRuntime } from '@/webui/src/helper/Data';
import { OB11InputStatusEvent } from '@/onebot/event/notice/OB11InputStatusEvent';
import { MessageUnique } from '@/common/utils/MessageUnique';
import { OB11Constructor } from './helper/converter';
import { proxiedListenerOf } from '@/common/utils/proxy-handler';
import { OB11FriendRequestEvent } from '@/onebot/event/request/OB11FriendRequest';
import { OB11GroupAdminNoticeEvent } from '@/onebot/event/notice/OB11GroupAdminNoticeEvent';