From b3589da4397a906a44ebdf77012a2c736d799d4d Mon Sep 17 00:00:00 2001 From: Il Harper Date: Tue, 5 Mar 2024 23:27:33 +0800 Subject: [PATCH] feat(api): implement chronocat.internal.assets.get --- .../src/api/internal/assets/get.ts | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 packages/engine-chronocat-api/src/api/internal/assets/get.ts diff --git a/packages/engine-chronocat-api/src/api/internal/assets/get.ts b/packages/engine-chronocat-api/src/api/internal/assets/get.ts new file mode 100644 index 0000000..c35c8a4 --- /dev/null +++ b/packages/engine-chronocat-api/src/api/internal/assets/get.ts @@ -0,0 +1,31 @@ +import type { Media } from '@chronocat/red' +import { ChatType } from '@chronocat/red' +import type { ChronocatContext } from '@chronocat/shell' +import { downloadRichMedia } from '../../../definitions/msgService' +import { richMediaDownloadMap } from '../../../globalVars' + +export const buildAssetsGet = + (ctx: ChronocatContext) => async (raw: string) => { + const data = JSON.parse( + Buffer.from(raw, 'base64url').toString('utf-8'), + ) as Media + + const downloadId = data.msgId + '::' + data.elementId + + const downloadCompletePromise = new Promise((res, rej) => { + richMediaDownloadMap[downloadId] = res + void ctx.chronocat.sleep(1000).then(rej) + }) + + if (data.chatType === ChatType.Private && !data.peerUid.startsWith('u_')) + data.peerUid = ctx.chronocat.uix.getUid(data.peerUid)! + + await downloadRichMedia({ + getReq: { + ...data, + downloadType: 1, + }, + }) + + return await downloadCompletePromise + }