From 27da2e5e0febbd8cebde8901f6773a0e5babc26c Mon Sep 17 00:00:00 2001
From: Shigma <1700011071@pku.edu.cn>
Date: Mon, 5 Apr 2021 03:06:11 +0800
Subject: [PATCH] feat(chat): support send message
---
packages/plugin-chat/client/chat.vue | 28 ++++++++++++-----
packages/plugin-chat/src/debug.ts | 2 +-
packages/plugin-chat/src/index.ts | 14 +++++++--
.../client/components/chat-panel.vue | 30 ++++++++++---------
.../client/components/message.vue | 10 +++----
.../plugin-webui/client/views/sandbox.vue | 2 +-
packages/plugin-webui/src/adapter.ts | 2 +-
packages/plugin-webui/src/index.ts | 7 +++++
packages/plugin-webui/src/server.ts | 29 ++++++++----------
9 files changed, 77 insertions(+), 47 deletions(-)
diff --git a/packages/plugin-chat/client/chat.vue b/packages/plugin-chat/client/chat.vue
index 97508e3e8a..dda714d7d3 100644
--- a/packages/plugin-chat/client/chat.vue
+++ b/packages/plugin-chat/client/chat.vue
@@ -1,5 +1,5 @@
-
+
+
+ 发送到频道:{{ activeMessage.channelName }}
+
diff --git a/packages/plugin-webui/client/views/sandbox.vue b/packages/plugin-webui/client/views/sandbox.vue
index 1bde2d9acd..e36ac6486a 100644
--- a/packages/plugin-webui/client/views/sandbox.vue
+++ b/packages/plugin-webui/client/views/sandbox.vue
@@ -1,5 +1,5 @@
-
+
diff --git a/packages/plugin-webui/src/adapter.ts b/packages/plugin-webui/src/adapter.ts
index cb4a122d57..a7d97ce698 100644
--- a/packages/plugin-webui/src/adapter.ts
+++ b/packages/plugin-webui/src/adapter.ts
@@ -7,7 +7,7 @@ const states: Record = {}
const TOKEN_TIMEOUT = Time.minute * 10
-class SocketChannel {
+export class SocketChannel {
readonly app: App
readonly id = Random.uuid()
diff --git a/packages/plugin-webui/src/index.ts b/packages/plugin-webui/src/index.ts
index d7d7a47d6e..f93fcba7ae 100644
--- a/packages/plugin-webui/src/index.ts
+++ b/packages/plugin-webui/src/index.ts
@@ -88,16 +88,23 @@ const defaultConfig: Config = {
apiPath: '/status',
uiPath: '/console',
selfUrl: '',
+ whitelist: [],
title: 'Koishi 控制台',
expiration: Time.week,
tickInterval: Time.second * 5,
refreshInterval: Time.hour,
}
+const builtinWhitelist = [
+ 'http://gchat.qpic.cn/',
+ 'http://c2cpicdw.qpic.cn',
+]
+
export const name = 'status'
export function apply(ctx: Context, config: Config = {}) {
config = Object.assign(defaultConfig, config)
+ config.whitelist.push(...builtinWhitelist)
ctx.webui = new WebServer(ctx, config)
diff --git a/packages/plugin-webui/src/server.ts b/packages/plugin-webui/src/server.ts
index 144f281497..bd95210fbf 100644
--- a/packages/plugin-webui/src/server.ts
+++ b/packages/plugin-webui/src/server.ts
@@ -10,18 +10,20 @@ import type PluginVue from '@vitejs/plugin-vue'
Context.delegate('webui')
-export interface Config extends WebAdapter.Config, Profile.Config, Meta.Config, Registry.Config, Statistics.Config {
+interface BaseConfig {
title?: string
- selfUrl?: string
- uiPath?: string
devMode?: boolean
+ uiPath?: string
+ whitelist?: string[]
+}
+
+export interface Config extends BaseConfig, WebAdapter.Config, Profile.Config, Meta.Config, Registry.Config, Statistics.Config {
+ title?: string
+ selfUrl?: string
}
-export interface ClientConfig {
- title: string
- uiPath: string
+export interface ClientConfig extends Required {
endpoint: string
- devMode: boolean
extensions: string[]
}
@@ -43,11 +45,6 @@ export class WebServer {
private vite: Vite.ViteDevServer
private readonly [Context.current]: Context
- static whitelist = [
- 'http://gchat.qpic.cn/',
- 'http://c2cpicdw.qpic.cn',
- ]
-
constructor(private ctx: Context, public config: Config) {
this.root = resolve(__dirname, '..', config.devMode ? 'client' : 'dist')
this.sources = {
@@ -75,7 +72,7 @@ export class WebServer {
}
private async start() {
- const { uiPath, apiPath } = this.config
+ const { uiPath, apiPath, whitelist } = this.config
await Promise.all([this.createVite(), this.createAdapter()])
this.ctx.router.get(uiPath + '(/.+)*', async (ctx) => {
@@ -106,7 +103,7 @@ export class WebServer {
})
this.ctx.router.get(apiPath + '/assets/:url', async (ctx) => {
- if (!WebServer.whitelist.some(prefix => ctx.params.url.startsWith(prefix))) {
+ if (!whitelist.some(prefix => ctx.params.url.startsWith(prefix))) {
console.log(ctx.params.url)
return ctx.status = 403
}
@@ -117,12 +114,12 @@ export class WebServer {
private async transformHtml(template: string) {
if (this.vite) template = await this.vite.transformIndexHtml(this.config.uiPath, template)
- const { apiPath, uiPath, devMode, selfUrl, title } = this.config
+ const { apiPath, uiPath, devMode, selfUrl, title, whitelist } = this.config
const endpoint = selfUrl + apiPath
const extensions = Object.entries(this.entries).map(([name, filename]) => {
return this.config.devMode ? '/vite/@fs' + filename : `./${name}`
})
- const global: ClientConfig = { title, uiPath, endpoint, devMode, extensions }
+ const global: ClientConfig = { title, uiPath, endpoint, devMode, extensions, whitelist }
const headInjection = ``
return template.replace('', '' + headInjection)
}