Skip to content

Commit

Permalink
refactor(core): refactor the sending and support spoiler (#116)
Browse files Browse the repository at this point in the history
* chore(core): add `spoiler` config

* refactor(core): using JSX

* refactor(core): image output in support `spoiler`

* refactor(core): output generation in JSX

* chore: update language files with new output format

* chore(core): add support for spoiler tag

* Revert "chore: update language files with new output format"

This reverts commit 4b846c1.

* chore: update Chinese locale

* chore(core): fix typo

* chore(core): add experimental feature

---------

Co-authored-by: Maiko Tan <[email protected]>
  • Loading branch information
Lipraty and MaikoTan authored Nov 12, 2023
1 parent 649f1ed commit 97332fe
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 27 deletions.
62 changes: 52 additions & 10 deletions packages/core/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ export enum OutputType {
All = 3,
}

export enum SpoilerType {
Disabled = 0,
All = 1,
OnlyNSFW = 2,
}

export interface Config {
detectLanguage: boolean
confidence: number
Expand All @@ -123,6 +129,7 @@ export interface Config {
nsfw: boolean
asset: boolean
base64: boolean
spoiler: SpoilerType
}

interface ImageArray extends Array<ImageSource.Result> {
Expand Down Expand Up @@ -153,7 +160,12 @@ export const Config = Schema.intersect([
Schema.const(3).description('发送全部信息'),
]).description('输出方式。').default(1),
asset: Schema.boolean().default(false).description('优先使用 [assets服务](https://assets.koishi.chat/) 转存图片。'),
base64: Schema.boolean().default(false).description('使用 base64 发送图片。')
base64: Schema.boolean().default(false).description('使用 base64 发送图片。'),
spoiler: Schema.union([
Schema.const(0).description('禁用'),
Schema.const(1).description('所有图片'),
Schema.const(2).description('仅 NSFW 图片'),
]).description('发送为隐藏图片,单击后显示(在 QQ 平台中以「合并转发」发送)。').default(0).experimental(),
}).description('输出设置'),
])

Expand Down Expand Up @@ -196,38 +208,68 @@ export function apply(ctx: Context, config: Config) {

if (!filtered?.length) return session?.text('.no-result')

const output: (string | Element)[] = []
const output: Element[] = []

for (const image of filtered) {
if (config.asset && ctx.assets) {
image.url = await ctx.booru.imgUrlToAssetUrl(image)
if (!image.url) {
output.unshift(session.text('.no-image'))
output.unshift(<i18n path=".no-image"></i18n>)
continue
}
}
if (config.base64) {
image.url = await ctx.booru.imgUrlToBase64(image)
if (!image.url) {
output.unshift(session.text('.no-image'))
output.unshift(<i18n path=".no-image"></i18n>)
continue
}
}
switch (config.output) {
case OutputType.All:
if (image.tags)
output.unshift(session.text('.output.source', { ...image, source, tags: image.tags.join(' ') }))
output.unshift(<message>
<p><i18n path='.output.source'>{[source]}</i18n></p>
<p><i18n path='.output.tags'>{[image.tags.join(', ')]}</i18n></p>
</message>)
case OutputType.ImageAndLink:
if (image.pageUrl || image.authorUrl)
output.unshift(session.text('.output.link', image))
output.unshift(<message>
<p><i18n path='.output.link'>{[image.pageUrl]}</i18n></p>
<p><i18n path='.output.homepage'>{[image.authorUrl]}</i18n></p>
</message>)
case OutputType.ImageAndInfo:
if (image.title && image.author && image.desc)
output.unshift(session.text('.output.info', image))
output.unshift(<message>
<p>{image.title}</p>
<p><i18n path='.output.author'>{[image.author]}</i18n></p>
<p><i18n path='.output.desc'>{[image.desc]}</i18n></p>
</message>)
case OutputType.ImageOnly:
output.unshift(session.text('.output.image', image))
output.unshift(
/**
* @TODO waiting for upstream to support spoiler tag
* but is only is attribute, so it's can work now.
*/
<message>
<image spoiler={(() => {
switch (config.spoiler) {
case SpoilerType.Disabled:
return false
case SpoilerType.All:
return true
case SpoilerType.OnlyNSFW:
return Boolean(image.nsfw)
}
})()} url={image.url}></image></message>
)
}
}

return output.length === 1 ? output[0] : <message forward>{output.join('\n')}</message>
// the qq platform will can merge the all forward message with one element(forward message block).
// so can treat it as a spoiler message.
if (['qq', 'red', 'onebot'].includes(session.platform) && config.spoiler !== SpoilerType.Disabled)
return <message forward>{output}</message>
else
return output.length === 1 ? output[0] : <message forward>{output}</message>
})
}
22 changes: 5 additions & 17 deletions packages/core/src/locales/zh-CN.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,8 @@ commands:
no-result: 没有找到符合条件的图片
no-image: 获取图片失败
output:
image: <message><image url={url}></image></message>
info: |
<message>
<p>{title}</p>
<p>作者: {author}</p>
<p>{desc}</p>
</message>
link: |
<message>
<p>页面地址: {pageUrl}</p>
<p>作者主页: {authorUrl}</p>
</message>
source: |
<message>
<p>图源: {source}</p>
<p>标签: {tags}</p>
</message>
author: "作者: {0}"
homepage: "作者主页: {0}"
link: "页面地址: {0}"
source: "图源: {0}"
tags: "标签: {0}"

0 comments on commit 97332fe

Please sign in to comment.