From c5291b9b1fc0014513d19ed515d1effc95a2bac8 Mon Sep 17 00:00:00 2001 From: Shigma Date: Wed, 30 Oct 2024 00:21:43 +0800 Subject: [PATCH] feat(lark): refactor message --- adapters/lark/src/message.ts | 77 ++++++++++++++---------------------- 1 file changed, 30 insertions(+), 47 deletions(-) diff --git a/adapters/lark/src/message.ts b/adapters/lark/src/message.ts index 17562df9..69c489e8 100644 --- a/adapters/lark/src/message.ts +++ b/adapters/lark/src/message.ts @@ -121,56 +121,39 @@ export class LarkMessageEncoder extends MessageEnco async visit(element: h) { const { type, attrs, children } = element - - switch (type) { - case 'text': - this.content += attrs.content - break - case 'at': { - if (attrs.type === 'all') { - this.content += `${attrs.name ?? '所有人'}` - } else { - this.content += `${attrs.name}` - } - break + if (type === 'text') { + this.content += attrs.content + } else if (type === 'at') { + if (attrs.type === 'all') { + this.content += `${attrs.name ?? '所有人'}` + } else { + this.content += `${attrs.name}` } - case 'a': - await this.render(children) - if (attrs.href) this.content += ` (${attrs.href})` - break - case 'p': - if (!this.content.endsWith('\n')) this.content += '\n' - await this.render(children) - if (!this.content.endsWith('\n')) this.content += '\n' - break - case 'br': - this.content += '\n' - break - case 'sharp': - // platform does not support sharp - break - case 'quote': + } else if (type === 'a') { + await this.render(children) + if (attrs.href) this.content += ` (${attrs.href})` + } else if (type === 'p') { + if (!this.content.endsWith('\n')) this.content += '\n' + await this.render(children) + if (!this.content.endsWith('\n')) this.content += '\n' + } else if (type === 'br') { + this.content += '\n' + } else if (type === 'sharp') { + // platform does not support sharp + } else if (type === 'quote') { + await this.flush() + this.quote = attrs.id + } else if (['img', 'image', 'video', 'audio', 'file'].includes(type)) { + if (attrs.src || attrs.url) { await this.flush() - this.quote = attrs.id - break - case 'img': - case 'image': - case 'video': - case 'audio': - case 'file': - if (attrs.src || attrs.url) { - await this.flush() - this.addition = await this.sendFile(type, attrs.src || attrs.url) - await this.flush() - } - break - case 'figure': // FIXME: treat as message element for now - case 'message': + this.addition = await this.sendFile(type as any, attrs.src || attrs.url) await this.flush() - await this.render(children, true) - break - default: - await this.render(children) + } + } else if (type === 'figure' || type === 'message') { + await this.flush() + await this.render(children, true) + } else { + await this.render(children) } } }