Skip to content

Commit

Permalink
feat(chat): add config maxMessages
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Apr 18, 2021
1 parent 2060ed5 commit 8b91a7b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/plugin-chat/client/chat.vue
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const divs = ref<Record<string, HTMLElement>>({})
receive('chat', (body) => {
messages.value.push(body)
messages.value.splice(0, messages.value.length - KOISHI_CONFIG.maxMessages)
})
function isSuccessive({ quote, userId, channelId }: Message, index: number) {
Expand Down
15 changes: 11 additions & 4 deletions packages/plugin-chat/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@ declare module 'koishi-core' {
}

declare module 'koishi-plugin-webui' {
interface ClientConfig {
whitelist: string[]
}
interface ClientConfig extends ClientExtension {}
}

export interface Config extends ReceiverConfig {
interface ClientExtension {
whitelist?: string[]
maxMessages?: number
}

export interface Config extends ReceiverConfig, ClientExtension {}

template.set('chat', {
send: '[{{ channelName || "私聊" }}] {{ abstract }}',
receive: '[{{ channelName || "私聊" }}] {{ username }}: {{ abstract }}',
Expand All @@ -52,9 +53,14 @@ const builtinWhitelist = [
'http://c2cpicdw.qpic.cn',
]

const defaultOptions: Config = {
maxMessages: 1000,
}

export const name = 'chat'

export function apply(ctx: Context, options: Config = {}) {
options = { ...defaultOptions, ...options }
ctx.plugin(receiver, options)

ctx.on('chat/receive', async (message, session) => {
Expand All @@ -71,6 +77,7 @@ export function apply(ctx: Context, options: Config = {}) {
const whitelist = [...builtinWhitelist, ...options.whitelist || []]

ctx.webui.global.whitelist = whitelist
ctx.webui.global.maxMessages = options.maxMessages
ctx.webui.addEntry(resolve(__dirname, filename))

ctx.webui.addListener('chat', async function ({ id, token, content, platform, selfId, channelId }) {
Expand Down

0 comments on commit 8b91a7b

Please sign in to comment.