Skip to content

Commit

Permalink
feat(kook): support message event with card elements, close #87
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Mar 31, 2023
1 parent 8583315 commit bc2623e
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions adapters/kook/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ export const adaptAuthor = (author: Kook.Author): Universal.Author => ({
nickname: author.nickname,
})

function transformCardElement(data: any) {
const { type, modules, text, elements, fields, ...attrs } = data
const children = modules || elements || fields || (text ? [text] : [])
return h(type, attrs, children.map(transformCardElement))
}

function adaptMessageMeta(base: Kook.MessageBase, meta: Kook.MessageMeta, session: Universal.MessageBase = {}) {
if (meta.author) {
session.author = adaptAuthor(meta.author)
Expand All @@ -35,6 +41,10 @@ function adaptMessageMeta(base: Kook.MessageBase, meta: Kook.MessageMeta, sessio
const element = h('image', { url: base.content, file: meta.attachments?.name })
session.elements = [element]
session.content = element.toString()
} else if (base.type === Kook.Type.card) {
const data = JSON.parse(base.content)
session.elements = data.map(transformCardElement)
session.content = session.elements.join('')
} else if (base.type === Kook.Type.kmarkdown) {
session.content = base.content
.replace(/\(met\)all\(met\)/g, () => h('at', { type: 'all' }).toString())
Expand Down

0 comments on commit bc2623e

Please sign in to comment.