From ec0103d5273a6eea59579f5fc33496bbfb03917c Mon Sep 17 00:00:00 2001 From: Il Harper Date: Wed, 19 Jun 2024 02:37:20 +0800 Subject: [PATCH] refactor: add type field for all asset types --- .../src/api/internal/assets/get.ts | 83 ++++++++++--------- .../src/parser/index.ts | 18 ++-- packages/red/src/redEntity.ts | 4 +- 3 files changed, 58 insertions(+), 47 deletions(-) diff --git a/packages/engine-chronocat-api/src/api/internal/assets/get.ts b/packages/engine-chronocat-api/src/api/internal/assets/get.ts index 032b174..75000df 100644 --- a/packages/engine-chronocat-api/src/api/internal/assets/get.ts +++ b/packages/engine-chronocat-api/src/api/internal/assets/get.ts @@ -13,51 +13,58 @@ export const buildAssetsGet = Buffer.from(raw, 'base64url').toString('utf-8'), ) as AssetRequest - if ('type' in data) { - switch (data.type) { - case 'marketface': { - const downloadId = data.tabId + '::' + data.faceId + switch (data.type) { + case 'mediav1': { + const downloadId = data.msgId + '::' + data.elementId - const downloadCompletePromise = new Promise((res, rej) => { - emojiDownloadMap[downloadId] = res - void ctx.chronocat.sleep(5000).then(rej) - }) + const downloadCompletePromise = new Promise((res, rej) => { + richMediaDownloadMap[downloadId] = res + void ctx.chronocat.sleep(1000).then(rej) + }) - await fetchMarketEmoticonAioImage({ - marketEmoticonAioImageReq: { - eId: data.faceId, - epId: data.tabId, - name: data.name, - width: 200, - height: 200, - jobType: 0, - encryptKey: data.key, - filePath: data.filePath, - downloadType: 3, - }, - }) + if ( + data.chatType === ChatType.Private && + !data.peerUid.startsWith('u_') + ) + data.peerUid = ctx.chronocat.uix.getUid(data.peerUid)! - return await downloadCompletePromise - } + await downloadRichMedia({ + getReq: { + msgId: data.msgId, + chatType: data.chatType, + peerUid: data.peerUid, + elementId: data.elementId, + thumbSize: data.thumbSize, + downloadType: 1, + }, + }) + + return await downloadCompletePromise } - } else { - const downloadId = data.msgId + '::' + data.elementId - const downloadCompletePromise = new Promise((res, rej) => { - richMediaDownloadMap[downloadId] = res - void ctx.chronocat.sleep(1000).then(rej) - }) + case 'mfacev1': { + const downloadId = data.tabId + '::' + data.faceId - if (data.chatType === ChatType.Private && !data.peerUid.startsWith('u_')) - data.peerUid = ctx.chronocat.uix.getUid(data.peerUid)! + const downloadCompletePromise = new Promise((res, rej) => { + emojiDownloadMap[downloadId] = res + void ctx.chronocat.sleep(5000).then(rej) + }) - await downloadRichMedia({ - getReq: { - ...data, - downloadType: 1, - }, - }) + await fetchMarketEmoticonAioImage({ + marketEmoticonAioImageReq: { + eId: data.faceId, + epId: data.tabId, + name: data.name, + width: 200, + height: 200, + jobType: 0, + encryptKey: data.key, + filePath: data.filePath, + downloadType: 3, + }, + }) - return await downloadCompletePromise + return await downloadCompletePromise + } } } diff --git a/packages/engine-chronocat-event/src/parser/index.ts b/packages/engine-chronocat-event/src/parser/index.ts index 4e3ef15..8e4583b 100644 --- a/packages/engine-chronocat-event/src/parser/index.ts +++ b/packages/engine-chronocat-event/src/parser/index.ts @@ -1,4 +1,4 @@ -import type { RedMessage } from '@chronocat/red' +import type { MarketFaceAssetRequest, Media, RedMessage } from '@chronocat/red' import { AtType, ChatType, FaceType, MsgType, SendType } from '@chronocat/red' import type { Channel, @@ -422,12 +422,13 @@ async function parseElements( ctx.chronocat.h('img', { src: `${config.self_url}/v1/assets/${Buffer.from( JSON.stringify({ + type: 'mediav1', msgId: message.msgId, chatType: message.chatType, peerUid: message.peerUid, elementId: m.elementId, thumbSize: m.picElement!.thumbFileSize, - }), + } satisfies Media), ).toString('base64url')}`, }), ) @@ -440,12 +441,13 @@ async function parseElements( ctx.chronocat.h('file', { src: `${config.self_url}/v1/assets/${Buffer.from( JSON.stringify({ + type: 'mediav1', msgId: message.msgId, chatType: message.chatType, peerUid: message.peerUid, elementId: m.elementId, thumbSize: m.fileElement!.thumbFileSize, - }), + } satisfies Media), ).toString('base64url')}`, }), ) @@ -458,12 +460,13 @@ async function parseElements( ctx.chronocat.h('audio', { src: `${config.self_url}/v1/assets/${Buffer.from( JSON.stringify({ + type: 'mediav1', msgId: message.msgId, chatType: message.chatType, peerUid: message.peerUid, elementId: m.elementId, thumbSize: 0, - }), + } satisfies Media), ).toString('base64url')}`, }), ) @@ -476,12 +479,13 @@ async function parseElements( ctx.chronocat.h('video', { src: `${config.self_url}/v1/assets/${Buffer.from( JSON.stringify({ + type: 'mediav1', msgId: message.msgId, chatType: message.chatType, peerUid: message.peerUid, elementId: m.elementId, thumbSize: m.videoElement!.thumbSize, - }), + } satisfies Media), ).toString('base64url')}`, }), ) @@ -565,13 +569,13 @@ async function parseElements( ctx.chronocat.h('img', { src: `${config.self_url}/v1/assets/${Buffer.from( JSON.stringify({ - type: 'marketface', + type: 'mfacev1', tabId: m.marketFaceElement!.emojiPackageId, faceId: m.marketFaceElement!.emojiId, key: m.marketFaceElement!.key, name: m.marketFaceElement!.faceName, filePath: m.marketFaceElement!.staticFacePath, - }), + } satisfies MarketFaceAssetRequest), ).toString('base64url')}`, }), ], diff --git a/packages/red/src/redEntity.ts b/packages/red/src/redEntity.ts index f32c94a..b1ee219 100644 --- a/packages/red/src/redEntity.ts +++ b/packages/red/src/redEntity.ts @@ -678,16 +678,16 @@ export interface RoleInfo { } export interface Media { + type: 'mediav1' msgId: string chatType: ChatType peerUid: string elementId: string thumbSize: number - downloadType: number } export interface MarketFaceAssetRequest { - type: 'marketface' + type: 'mfacev1' tabId: number faceId: string key: string