feat(api): implement chronocat.internal.assets.get

This commit is contained in:
Il Harper 2024-03-05 23:27:33 +08:00
parent 740a8cb7fc
commit b3589da439
No known key found for this signature in database
GPG Key ID: 4B71FCA698E7E8EC

View File

@ -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<string>((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
}