Skip to content

Commit

Permalink
refactor(webui): webui as adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Apr 12, 2021
1 parent f4ab9c1 commit fc0b2a7
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 186 deletions.
20 changes: 12 additions & 8 deletions packages/plugin-chat/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Bot, Context, Random, Session, template } from 'koishi-core'
import { resolve } from 'path'
import { WebAdapter } from 'koishi-plugin-webui'
import { WebServer } from 'koishi-plugin-webui'
import receiver, { Message, ReceiverConfig } from './receiver'

export * from './receiver'
Expand Down Expand Up @@ -31,7 +31,7 @@ export class SandboxBot extends Bot<'web'> {
username = 'sandbox'
status = Bot.Status.GOOD

constructor(public readonly adapter: WebAdapter) {
constructor(public readonly adapter: WebServer) {
super(adapter, { type: 'web', selfId: 'sandbox' })
}

Expand Down Expand Up @@ -72,15 +72,19 @@ export function apply(ctx: Context, options: Config = {}) {
ctx.bots[`${platform}:${selfId}`]?.sendMessage(channelId, content)
})

const sandbox = ctx.webui.adapter.create({}, SandboxBot)
Profile.initBot(sandbox)
ctx.on('connect', () => {
// create bot after connection
// to prevent mysql from altering user table
const sandbox = ctx.webui.create({}, SandboxBot)
Profile.initBot(sandbox)
})

ctx.webui.addListener('sandbox', async function ({ id, token, content }) {
const user = await this.validate(id, token, ['name'])
if (!user) return
content = await this.app.transformAssets(content)
content = await ctx.transformAssets(content)
this.send('sandbox:user', content)
const session = new Session(this.app, {
const session = new Session(ctx.app, {
platform: 'web',
userId: id,
content,
Expand All @@ -94,7 +98,7 @@ export function apply(ctx: Context, options: Config = {}) {
},
})
session.platform = 'id' as never
this.adapter.dispatch(session)
ctx.webui.dispatch(session)
})

ctx.self('sandbox')
Expand All @@ -104,7 +108,7 @@ export function apply(ctx: Context, options: Config = {}) {
})

ctx.on('chat/receive', async (message) => {
Object.values(ctx.webui.adapter.handles).forEach((handle) => {
Object.values(ctx.webui.handles).forEach((handle) => {
if (handle.authority >= 4) handle.socket.send(JSON.stringify({ type: 'chat', body: message }))
})
})
Expand Down
139 changes: 0 additions & 139 deletions packages/plugin-webui/src/adapter.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/plugin-webui/src/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export class Profile implements DataSource<Profile.Payload> {
this.apply(ctx, config)

ctx.on('status/tick', async () => {
this.ctx.webui.adapter?.broadcast('profile', await this.get(true))
this.ctx.webui.broadcast('profile', await this.get(true))
})
}

Expand Down Expand Up @@ -191,7 +191,7 @@ export class Registry implements DataSource<Registry.Payload> {
}

update = async () => {
this.ctx.webui.adapter?.broadcast('registry', await this.get(true))
this.ctx.webui.broadcast('registry', await this.get(true))
}

async get(forced = false) {
Expand Down
1 change: 0 additions & 1 deletion packages/plugin-webui/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { WebServer, Config } from './server'
import './mongo'
import './mysql'

export * from './adapter'
export * from './data'
export * from './stats'
export * from './server'
Expand Down

0 comments on commit fc0b2a7

Please sign in to comment.