Skip to content

Commit

Permalink
🐛 upd
Browse files Browse the repository at this point in the history
  • Loading branch information
undefined-moe committed Aug 27, 2020
1 parent 6b3bc95 commit 4315f92
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 19 deletions.
5 changes: 2 additions & 3 deletions packages/plugin-teach/src/database/mongo.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Context, extendDatabase } from 'koishi-core'
import { clone, defineProperty, Observed, pick } from 'koishi-utils'
import { Dialogue, DialogueTest } from '../utils'
import { FilterQuery } from 'mongodb'
import MongoDatabase from 'koishi-plugin-mongo/dist/database'
import { Dialogue, DialogueTest } from '../utils'

declare module 'koishi-core/dist/context' {
interface EventMap {
Expand All @@ -26,8 +26,7 @@ extendDatabase<typeof MongoDatabase>('koishi-plugin-mongo', {
async getDialoguesByTest(test: DialogueTest) {
const query: FilterQuery<Dialogue> = { $and: [] }
this.app.emit('dialogue/mongo', test, query.$and)
const dialogues = (await this.db.collection('dialogue').find(query).toArray())
.filter((dialogue) => !this.app.bail('dialogue/fetch', dialogue, test))
const dialogues = await this.db.collection('dialogue').find(query).toArray()
dialogues.forEach(d => defineProperty(d, '_backup', clone(d)))
return dialogues.filter(value => {
if (value.flag & Dialogue.Flag.regexp) {
Expand Down
13 changes: 7 additions & 6 deletions packages/plugin-teach/src/database/mysql.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Context, extendDatabase, Message } from 'koishi-core'
import { Context, extendDatabase } from 'koishi-core'
import { clone, defineProperty, Observed, pick } from 'koishi-utils'
import { Dialogue, DialogueTest } from '../utils'
import { Dialogue, equal, DialogueTest } from '../utils'
import { escape } from 'mysql'
import { format } from 'util'
import MysqlDatabase from 'koishi-plugin-mysql/dist/database'

declare module 'koishi-core/dist/context' {
Expand All @@ -24,10 +23,12 @@ extendDatabase<typeof MysqlDatabase>('koishi-plugin-mysql', {
const conditionals: string[] = []
this.app.emit('dialogue/mysql', test, conditionals)
if (conditionals.length) query += ' WHERE ' + conditionals.join(' && ')
const dialogues = (await this.query<Dialogue[]>(query))
.filter((dialogue) => !this.app.bail('dialogue/fetch', dialogue, test))
const dialogues = await this.query<Dialogue[]>(query)
dialogues.forEach(d => defineProperty(d, '_backup', clone(d)))
return dialogues
return dialogues.filter((data) => {
if (!test.groups || test.partial) return true
return !(data.flag & Dialogue.Flag.complement) === test.reversed || !equal(test.groups, data.groups)
})
},

async createDialogue(dialogue: Dialogue, argv: Dialogue.Argv, revert = false) {
Expand Down
6 changes: 0 additions & 6 deletions packages/plugin-teach/src/plugins/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,6 @@ export default function apply(ctx: Context, config: Dialogue.Config) {
.option('groups', '-g <gids> 设置具体的生效环境', { authority: 3, type: 'string', validate: RE_GROUPS })
.option('global', '-G 无视上下文搜索')

// TODO: ???
ctx.on('dialogue/fetch', (data, test) => {
if (!test.groups || test.partial) return
return !(data.flag & Dialogue.Flag.complement) === test.reversed || !equal(test.groups, data.groups)
})

ctx.on('dialogue/validate', (argv) => {
const { options, session } = argv

Expand Down
1 change: 0 additions & 1 deletion packages/plugin-teach/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ declare module 'koishi-core/dist/app' {

declare module 'koishi-core/dist/context' {
interface EventMap {
'dialogue/fetch'(dialogue: Dialogue, test: DialogueTest): boolean | void
'dialogue/permit'(argv: Dialogue.Argv, dialogue: Dialogue): boolean
'dialogue/flag'(flag: keyof typeof Dialogue.Flag): void
}
Expand Down
8 changes: 5 additions & 3 deletions packages/plugin-teach/tests/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { extendDatabase, Context } from 'koishi-core'
import { defineProperty, Observed, clone, intersection } from 'koishi-utils'
import { Dialogue, DialogueTest } from 'koishi-plugin-teach'
import { Dialogue, DialogueTest, equal } from 'koishi-plugin-teach'
import { MemoryDatabase, memory } from 'koishi-test-utils'

declare module 'koishi-core/dist/context' {
Expand All @@ -23,10 +23,12 @@ extendDatabase(MemoryDatabase, {
async getDialoguesByTest(test: DialogueTest) {
const dialogues = Object.values(this.$table('dialogue')).filter((dialogue) => {
return !this.app.bail('dialogue/memory', dialogue, test)
&& !this.app.bail('dialogue/fetch', dialogue, test)
}).map(clone)
dialogues.forEach(d => defineProperty(d, '_backup', clone(d)))
return dialogues
return dialogues.filter((data) => {
if (!test.groups || test.partial) return true
return !(data.flag & Dialogue.Flag.complement) === test.reversed || !equal(test.groups, data.groups)
})
},

async createDialogue(dialogue: Dialogue, argv: Dialogue.Argv, revert = false) {
Expand Down

0 comments on commit 4315f92

Please sign in to comment.