Skip to content

Commit

Permalink
refactor: add type field for all asset types
Browse files Browse the repository at this point in the history
  • Loading branch information
ilharp committed Jun 18, 2024
1 parent 635b6dd commit ec0103d
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 50 deletions.
89 changes: 48 additions & 41 deletions packages/engine-chronocat-api/src/api/internal/assets/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

const downloadCompletePromise = new Promise<string>((res, rej) => {
emojiDownloadMap[downloadId] = res
void ctx.chronocat.sleep(5000).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,
},
})

return await downloadCompletePromise
}
switch (data.type) {
case 'mediav1': {
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: {
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<string>((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<string>((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
}
}
}
18 changes: 11 additions & 7 deletions packages/engine-chronocat-event/src/parser/index.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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')}`,
}),
)
Expand All @@ -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')}`,
}),
)
Expand All @@ -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')}`,
}),
)
Expand All @@ -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')}`,
}),
)
Expand Down Expand Up @@ -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')}`,
}),
],
Expand Down
4 changes: 2 additions & 2 deletions packages/red/src/redEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ec0103d

Please sign in to comment.