Skip to content

Commit

Permalink
fix(satori): fix incorrect impl about resource elements
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jan 8, 2024
1 parent 912a801 commit f32984d
Show file tree
Hide file tree
Showing 22 changed files with 88 additions and 90 deletions.
13 changes: 7 additions & 6 deletions adapters/dingtalk/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class DingtalkMessageEncoder<C extends Context = Context> extends Message

// https://open.dingtalk.com/document/orgapp/upload-media-files?spm=ding_open_doc.document.0.0.3b166172ERBuHw
async uploadMedia(attrs: Dict) {
const { data, mime } = await this.bot.ctx.http.file(attrs.url, attrs)
const { data, mime } = await this.bot.ctx.http.file(attrs.src || attrs.url, attrs)
const form = new FormData()
// https://github.com/form-data/form-data/issues/468
const value = process.env.KOISHI_ENV === 'browser'
Expand Down Expand Up @@ -89,20 +89,21 @@ export class DingtalkMessageEncoder<C extends Context = Context> extends Message
const { type, attrs, children } = element
if (type === 'text') {
this.buffer += escape(attrs.content)
} else if (type === 'image' && attrs.url) {
} else if ((type === 'img' || type === 'image') && (attrs.src || attrs.url)) {
const src = attrs.src || attrs.url
// await this.flush()
// await this.sendMessage('sampleImageMsg', {
// photoURL: attrs.url
// photoURL: src,
// })
if (await this.bot.http.isPrivate(attrs.url)) {
if (await this.bot.http.isPrivate(src)) {
const temp = this.bot.ctx.get('server.temp')
if (!temp) {
return this.bot.logger.warn('missing temporary file service, cannot send assets with private url')
}
const entry: Entry | undefined = await temp.create(attrs.url)
const entry: Entry | undefined = await temp.create(src)
this.buffer += `![${attrs.alt ?? ''}](${entry.url})`
} else {
this.buffer += `![${attrs.alt ?? ''}](${attrs.url})`
this.buffer += `![${attrs.alt ?? ''}](${src})`
}
} else if (type === 'message') {
await this.flush()
Expand Down
2 changes: 1 addition & 1 deletion adapters/discord/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class DiscordBot<C extends Context = Context> extends Bot<C, DiscordBot.C
async editMessage(channelId: string, messageId: string, content: Fragment) {
const elements = h.normalize(content)
content = elements.toString()
const image = elements.find(v => v.type === 'image')
const image = elements.find(v => v.type === 'img' || v.type === 'image')
if (image) {
throw new Error("You can't include embed object(s) while editing message.")
}
Expand Down
10 changes: 5 additions & 5 deletions adapters/discord/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export class DiscordMessageEncoder<C extends Context = Context> extends MessageE
}

async sendEmbed(attrs: Dict, payload: Dict) {
const { filename, data, mime } = await this.bot.ctx.http.file(attrs.url, attrs)
const { filename, data, mime } = await this.bot.ctx.http.file(attrs.src || attrs.url, attrs)
const form = new FormData()
// https://github.com/form-data/form-data/issues/468
const value = process.env.KOISHI_ENV === 'browser'
Expand All @@ -109,10 +109,10 @@ export class DiscordMessageEncoder<C extends Context = Context> extends MessageE
if (addition.content) {
await this.post(addition)
}
return this.post({ ...addition, content: attrs.url })
return this.post({ ...addition, content: attrs.src || attrs.url })
}

if (await this.bot.http.isPrivate(attrs.url)) {
if (await this.bot.http.isPrivate(attrs.src || attrs.url)) {
return await this.sendEmbed(attrs, addition)
}

Expand All @@ -124,7 +124,7 @@ export class DiscordMessageEncoder<C extends Context = Context> extends MessageE
}

// auto mode
if (await this.checkMediaType(attrs.url, type)) {
if (await this.checkMediaType(attrs.src || attrs.url, type)) {
return sendDirect()
} else {
return this.sendEmbed(attrs, addition)
Expand Down Expand Up @@ -285,7 +285,7 @@ export class DiscordMessageEncoder<C extends Context = Context> extends MessageE
} else {
this.buffer += `<${attrs.animated ? 'a' : ''}:${attrs.name}:${attrs.id}>`
}
} else if ((type === 'image' || type === 'video') && attrs.url) {
} else if ((type === 'img' || type === 'image' || type === 'video') && (attrs.src || attrs.url)) {
if (this.mode === 'figure') {
this.figure = element
} else {
Expand Down
16 changes: 8 additions & 8 deletions adapters/discord/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,26 +92,26 @@ export async function decodeMessage(
if (!/\s$/.test(message.content)) message.content += ' '
message.content += data.attachments.map(v => {
if (v.height && v.width && v.content_type?.startsWith('image/')) {
return h('image', {
url: v.url,
return h('img', {
src: v.url,
proxy_url: v.proxy_url,
file: v.filename,
})
} else if (v.height && v.width && v.content_type?.startsWith('video/')) {
return h('video', {
url: v.url,
src: v.url,
proxy_url: v.proxy_url,
file: v.filename,
})
} else if (v.content_type?.startsWith('audio/')) {
return h('record', {
url: v.url,
src: v.url,
proxy_url: v.proxy_url,
file: v.filename,
})
} else {
return h('file', {
url: v.url,
src: v.url,
proxy_url: v.proxy_url,
file: v.filename,
})
Expand All @@ -122,13 +122,13 @@ export async function decodeMessage(
// not using embed types
// https://discord.com/developers/docs/resources/channel#embed-object-embed-types
if (embed.image) {
message.content += h('image', { url: embed.image.url, proxy_url: embed.image.proxy_url })
message.content += h('img', { src: embed.image.url, proxy_url: embed.image.proxy_url })
}
if (embed.thumbnail) {
message.content += h('image', { url: embed.thumbnail.url, proxy_url: embed.thumbnail.proxy_url })
message.content += h('img', { src: embed.thumbnail.url, proxy_url: embed.thumbnail.proxy_url })
}
if (embed.video) {
message.content += h('video', { url: embed.video.url, proxy_url: embed.video.proxy_url })
message.content += h('video', { src: embed.video.url, proxy_url: embed.video.proxy_url })
}
}
message.elements = h.parse(message.content)
Expand Down
13 changes: 7 additions & 6 deletions adapters/kook/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,17 @@ export class KookMessageEncoder<C extends Context = Context> extends MessageEnco
}

private async transformUrl({ type, attrs }: h) {
if (await this.bot.http.isPrivate(attrs.url)) {
const src = attrs.src || attrs.url
if (await this.bot.http.isPrivate(src)) {
const payload = new FormData()
const result = await this.bot.ctx.http.file(attrs.url, attrs)
const result = await this.bot.ctx.http.file(src, attrs)
payload.append('file', Buffer.from(result.data), {
filename: attrs.file || result.filename,
})
const { url } = await this.bot.request('POST', '/asset/create', payload, payload.getHeaders())
return url
} else if (!attrs.url.includes('kookapp.cn')) {
const res = await this.bot.ctx.http.get<internal.Readable>(attrs.url, {
} else if (!src.includes('kookapp.cn')) {
const res = await this.bot.ctx.http.get<internal.Readable>(src, {
headers: { accept: type + '/*' },
responseType: 'stream',
timeout: +attrs.timeout || undefined,
Expand All @@ -69,7 +70,7 @@ export class KookMessageEncoder<C extends Context = Context> extends MessageEnco
const { url } = await this.bot.request('POST', '/asset/create', payload, payload.getHeaders())
return url
} else {
return attrs.url
return src
}
}

Expand Down Expand Up @@ -163,7 +164,7 @@ export class KookMessageEncoder<C extends Context = Context> extends MessageEnco
src: await this.transformUrl(element),
title: attrs.title,
})
} else if (type === 'image' || type === 'kook:image') {
} else if (type === 'img' || type === 'image' || type === 'kook:image') {
this.flushText()
this.cardBuffer.modules.push({
type: 'container',
Expand Down
2 changes: 1 addition & 1 deletion adapters/kook/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function adaptMessageMeta(
.replace(/#channel:(\d+);/, (_, id) => h.sharp(id).toString())
message.elements = h.parse(message.content)
} else if (base.type === Kook.Type.image) {
const element = h('image', { url: base.content, file: data.attachments?.name })
const element = h('img', { src: base.content, file: data.attachments?.name })
message.elements = [element]
message.content = element.toString()
} else if (base.type === Kook.Type.card) {
Expand Down
11 changes: 6 additions & 5 deletions adapters/lark/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ export class LarkMessageEncoder<C extends Context = Context> extends MessageEnco
this.richText = undefined
}

async sendFile(type: 'image' | 'video' | 'audio' | 'file', url: string): Promise<Addition> {
async sendFile(type: 'img' | 'image' | 'video' | 'audio' | 'file', url: string): Promise<Addition> {
const payload = new FormData()

const assetKey = type === 'image' ? 'image' : 'file'
const assetKey = type === 'img' || type === 'image' ? 'image' : 'file'
const [schema, file] = url.split('://')
const filename = schema === 'base64' ? 'unknown' : new URL(url).pathname.split('/').pop()
if (schema === 'file') {
Expand All @@ -90,7 +90,7 @@ export class LarkMessageEncoder<C extends Context = Context> extends MessageEnco
payload.append(assetKey, resp)
}

if (type === 'image') {
if (type === 'img' || type === 'image') {
payload.append('image_type', 'message')
const { data } = await this.bot.internal.uploadImage(payload)
return {
Expand Down Expand Up @@ -162,13 +162,14 @@ export class LarkMessageEncoder<C extends Context = Context> extends MessageEnco
await this.flush()
this.quote = attrs.id
break
case 'img':
case 'image':
case 'video':
case 'audio':
case 'file':
if (attrs.url) {
if (attrs.src || attrs.url) {
await this.flush()
this.addition = await this.sendFile(type, attrs.url)
this.addition = await this.sendFile(type, attrs.src || attrs.url)
}
break
case 'figure': // FIXME: treat as message element for now
Expand Down
18 changes: 9 additions & 9 deletions adapters/line/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,28 +93,28 @@ export class LineMessageEncoder<C extends Context = Context> extends MessageEnco
if (!this.buffer.endsWith('\n')) this.buffer += '\n'
await this.render(children)
if (!this.buffer.endsWith('\n')) this.buffer += '\n'
} else if (type === 'image' && attrs.url) {
} else if ((type === 'img' || type === 'image') && (attrs.src || attrs.url)) {
await this.insertBlock()
this.blocks.push({
type: 'image',
originalContentUrl: attrs.url,
previewImageUrl: attrs.url,
originalContentUrl: attrs.src || attrs.url,
previewImageUrl: attrs.src || attrs.url,
})
} else if (type === 'video' && attrs.url) {
} else if (type === 'video' && (attrs.src || attrs.url)) {
await this.insertBlock()
this.blocks.push({
type: 'video',
originalContentUrl: attrs.url,
previewImageUrl: attrs.url,
originalContentUrl: attrs.src || attrs.url,
previewImageUrl: attrs.src || attrs.url,
})
} else if (type === 'audio' && attrs.url) {
} else if (type === 'audio' && (attrs.src || attrs.url)) {
await this.insertBlock()
this.blocks.push({
type: 'audio',
originalContentUrl: attrs.url,
originalContentUrl: attrs.src || attrs.url,
duration: 1145,
})
} else if (type === 'face' && attrs.id) {
} else if (type === 'face') {
if (attrs.id.startsWith('s')) {
// https://developers.line.biz/en/reference/messaging-api/#sticker-message
await this.insertBlock()
Expand Down
10 changes: 4 additions & 6 deletions adapters/mail/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,11 @@ export class MailMessageEncoder<C extends Context = Context> extends MessageEnco
}
} else if (type === 'sharp' && attrs.id) {
this.buffer += ` #${attrs.id} `
} else if (['image', 'audio', 'video', 'file'].includes(type) && attrs.url) {
let url: string
if (attrs.url.match(/^https?:/)) {
url = attrs.url
} else {
} else if (['image', 'audio', 'video', 'file'].includes(type) && (attrs.src || attrs.url)) {
let url: string = attrs.src || attrs.url
if (!url.match(/^https?:/)) {
const cid = randomId()
const { filename, mime, data } = await this.bot.ctx.http.file(attrs.url)
const { filename, mime, data } = await this.bot.ctx.http.file(url)
this.attachments.push({
cid,
filename,
Expand Down
2 changes: 1 addition & 1 deletion adapters/mail/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export async function adaptMessage(
break
}
if (src.match(/^(data|https?):/)) {
content += `<image url="${src}"/>`
content += `<img src="${src}"/>`
break
}
break
Expand Down
6 changes: 3 additions & 3 deletions adapters/matrix/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ export class MatrixMessageEncoder<C extends Context = Context> extends MessageEn
}
} else if (type === 'sharp' && attrs.id) {
this.buffer += ` #${attrs.id} `
} else if ((type === 'image' || type === 'video' || type === 'record' || type === 'file') && attrs.url) {
} else if ((type === 'image' || type === 'img' || type === 'video' || type === 'record' || type === 'file') && (attrs.src || attrs.url)) {
await this.flush()
const matrixType = type === 'record' ? 'audio' : type
await this.sendMedia(attrs.url, matrixType)
const matrixType = type === 'record' ? 'audio' : type === 'img' ? 'image' : type
await this.sendMedia(attrs.src || attrs.url, matrixType)
} else if (type === 'quote') {
this.reply = await this.bot.getMessage(this.channelId, attrs.id)
} else if (type === 'message') {
Expand Down
4 changes: 2 additions & 2 deletions adapters/matrix/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ export async function adaptMessage(
case 'm.file':
case 'm.audio':
case 'm.video': {
const url = bot.internal.getAssetUrl((content as any).url)
const src = bot.internal.getAssetUrl((content as any).url)
const type = content.msgtype.substring(2)
message.content = segment(type === 'audio' ? 'record' : type, { url }).toString()
message.content = segment(type === 'audio' ? 'record' : type === 'image' ? 'img' : type, { src }).toString()
break
}
default:
Expand Down
2 changes: 1 addition & 1 deletion adapters/qq/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@satorijs/adapter-qq",
"description": "QQ Adapter for Satorijs",
"version": "4.2.5",
"version": "4.2.7",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"files": [
Expand Down
19 changes: 8 additions & 11 deletions adapters/qq/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export class QQGuildMessageEncoder<C extends Context = Context> extends MessageE
private passiveId: string
reference: string
private retry = false
private resource: Dict
// 先文后图
async flush() {
if (!this.content.trim().length && !this.file && !this.fileUrl) {
Expand Down Expand Up @@ -110,7 +109,6 @@ export class QQGuildMessageEncoder<C extends Context = Context> extends MessageE
this.file = null
this.filename = null
this.fileUrl = null
this.resource = null
this.retry = false
}

Expand All @@ -134,11 +132,10 @@ export class QQGuildMessageEncoder<C extends Context = Context> extends MessageE
}

async resolveFile(attrs: Dict, download = false) {
if (attrs) this.resource = attrs
if (!download && !await this.bot.ctx.http.isPrivate(this.resource.url)) {
return this.fileUrl = this.resource.url
if (!download && !await this.bot.ctx.http.isPrivate(attrs.src || attrs.url)) {
return this.fileUrl = attrs.src || attrs.url
}
const { data, filename } = await this.bot.ctx.http.file(this.resource.url, this.resource)
const { data, filename } = await this.bot.ctx.http.file(attrs.src || attrs.url, attrs)
this.file = Buffer.from(data)
this.filename = filename
this.fileUrl = null
Expand Down Expand Up @@ -169,7 +166,7 @@ export class QQGuildMessageEncoder<C extends Context = Context> extends MessageE
await this.flush()
} else if (type === 'passive') {
this.passiveId = attrs.id
} else if (type === 'image' && attrs.url) {
} else if ((type === 'img' || type === 'image') && (attrs.src || attrs.url)) {
await this.flush()
await this.resolveFile(attrs)
await this.flush()
Expand Down Expand Up @@ -287,7 +284,7 @@ export class QQMessageEncoder<C extends Context = Context> extends MessageEncode
}

async sendFile(type: string, attrs: Dict) {
let url = attrs.url, entry: Entry | undefined
let url = attrs.src || attrs.url, entry: Entry | undefined
if (await this.bot.ctx.http.isPrivate(url)) {
const temp = this.bot.ctx.get('server.temp')
if (!temp) {
Expand All @@ -298,7 +295,7 @@ export class QQMessageEncoder<C extends Context = Context> extends MessageEncode
}
await this.flush()
let file_type = 0
if (type === 'image') file_type = 1
if (type === 'img' || type === 'image') file_type = 1
else if (type === 'video') file_type = 2
else return
const data: QQ.Message.File.Request = {
Expand Down Expand Up @@ -371,11 +368,11 @@ export class QQMessageEncoder<C extends Context = Context> extends MessageEncode
} else if (type === 'passive') {
this.passiveId = attrs.id
this.passiveSeq = Number(attrs.seq)
} else if (type === 'image' && attrs.url) {
} else if ((type === 'img' || type === 'image') && (attrs.src || attrs.url)) {
await this.flush()
const data = await this.sendFile(type, attrs)
if (data) this.attachedFile = data
} else if (type === 'video' && attrs.url) {
} else if (type === 'video' && (attrs.src || attrs.url)) {
await this.flush()
const data = await this.sendFile(type, attrs)
if (data) this.attachedFile = data
Expand Down
Loading

0 comments on commit f32984d

Please sign in to comment.