Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix mongo support for teach #93

Merged
merged 5 commits into from
Aug 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ rules:
yoda: off
standard/no-callback-literal: off
'@typescript-eslint/keyword-spacing': error
'@typescript-eslint/no-unused-vars': warn
'@typescript-eslint/space-before-function-paren':
- error
- anonymous: always
Expand Down
24 changes: 16 additions & 8 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, equal } from '../utils'

declare module 'koishi-core/dist/context' {
interface EventMap {
Expand All @@ -26,10 +26,18 @@ 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
return dialogues.filter(value => {
undefined-moe marked this conversation as resolved.
Show resolved Hide resolved
if (value.flag & Dialogue.Flag.regexp) {
const regex = new RegExp(value.question, 'i')
if (!(regex.test(test.question) || regex.test(test.original))) return false
}
if (test.groups && !test.partial) {
return !(value.flag & Dialogue.Flag.complement) === test.reversed || !equal(test.groups, value.groups)
}
return true
})
},

async createDialogue(dialogue: Dialogue, argv: Dialogue.Argv, revert = false) {
Expand Down Expand Up @@ -151,12 +159,12 @@ export default function apply(ctx: Context) {

ctx.on('dialogue/mongo', (test, conditionals) => {
if (!test.groups || !test.groups.length) return
const $and: FilterQuery<Dialogue>[] = test.groups.map(group => ({ $not: { groups: group } }))
$and.push({ flag: { [test.reversed ? '$bitsAllSet' : '$bitsAllClear']: Dialogue.Flag.complement } })
const $and: FilterQuery<Dialogue>[] = test.groups.map((group) => ({ groups: { $ne: group } }))
$and.push({ flag: { [test.reversed ? '$bitsAllClear' : '$bitsAllSet']: Dialogue.Flag.complement } })
conditionals.push({
$or: [
{
flag: { [test.reversed ? '$bitsAllClear' : '$bitsAllSet']: Dialogue.Flag.complement },
flag: { [test.reversed ? '$bitsAllSet' : '$bitsAllClear']: Dialogue.Flag.complement },
groups: { $all: test.groups },
},
{ $and },
Expand Down Expand Up @@ -192,7 +200,7 @@ export default function apply(ctx: Context) {
if (test.matchTime !== undefined) {
conditionals.push({ $expr: { $gte: [expr, 0] } })
}
if (test.matchTime !== undefined) {
if (test.mismatchTime !== undefined) {
conditionals.push({ $expr: { $lt: [expr, 0] } })
}
})
Expand Down
15 changes: 8 additions & 7 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 Expand Up @@ -98,7 +99,7 @@ extendDatabase<typeof MysqlDatabase>('koishi-plugin-mysql', ({ listFields }) =>

export default function apply(ctx: Context, config: Dialogue.Config) {
config.validateRegExp = {
onEscapeCharacterSet(start, end, kind, negate) {
onEscapeCharacterSet() {
throw new SyntaxError('unsupported escape character set')
},
onQuantifier(start, end, min, max, greedy) {
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