Skip to content

Commit

Permalink
feat: turn keyword into a command fragment (#67)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: replaces the keyword table (renames old table)
  • Loading branch information
ssube committed Dec 22, 2018
1 parent 42d93c8 commit 2337d9f
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 42 deletions.
52 changes: 28 additions & 24 deletions src/controller/LearnController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Command, CommandVerb } from 'src/entity/Command';
import { Context } from 'src/entity/Context';
import { Keyword } from 'src/entity/misc/Keyword';
import { Checklist, ChecklistOptions } from 'src/utils/Checklist';
import { Fragment } from 'src/entity/Fragment';

export const NOUN_KEYWORD = 'keyword';

Expand Down Expand Up @@ -52,7 +53,7 @@ export class LearnController extends BaseController<LearnControllerData> impleme
}
}

protected async createKeyword(keyName: string, cmd: Command, args: Array<string>): Promise<void> {
protected async createKeyword(key: string, cmd: Command, args: Array<string>): Promise<void> {
const noun = cmd.getHead('noun');
const verb = cmd.getHead('verb') as CommandVerb;

Expand All @@ -61,47 +62,50 @@ export class LearnController extends BaseController<LearnControllerData> impleme
}

const keyword = new Keyword({
command: new Command({
context: cmd.context,
data: { args },
labels: {},
noun,
verb,
}),
controller: this.name,
name: keyName,
controllerId: this.name,
data: { args },
key,
labels: {},
noun,
verb,
});

this.logger.debug({ args, cmd, keyName, keyword }, 'learning command');
this.logger.debug({ args, cmd, key, keyword }, 'learning command');

if (await this.keywordRepository.findOne(keyName)) {
return this.reply(cmd.context, `command already exists: ${keyName}`);
const existing = await this.keywordRepository.findOne({
key,
});
if (existing) {
return this.reply(cmd.context, `command already exists: ${key}`);
}

await this.keywordRepository.save(keyword);
return this.reply(cmd.context, `Learned command ${keyName}.`);
return this.reply(cmd.context, `Learned command ${key}.`);
}

protected async deleteKeyword(keyName: string, context: Context): Promise<void> {
const keyword = await this.keywordRepository.findOne(keyName);
protected async deleteKeyword(key: string, context: Context): Promise<void> {
const keyword = await this.keywordRepository.findOne({
key,
});
if (!keyword) {
return this.reply(context, `command ${keyName} does not exist.`);
return this.reply(context, `command ${key} does not exist.`);
}

await this.keywordRepository.delete(keyName);
return this.reply(context, `deleted command ${keyName}.`);
await this.keywordRepository.delete(key);
return this.reply(context, `deleted command ${key}.`);
}

protected async executeKeyword(keyName: string, context: Context, body: Array<string>) {
const keyword = await this.keywordRepository.findOne(keyName, {
relations: ['command', 'command.context'],
protected async executeKeyword(key: string, context: Context, body: Array<string>) {
const keyword = await this.keywordRepository.findOne({
key,
});

if (!keyword || !keyword.command) {
if (!keyword) {
return this.reply(context, 'missing keyword or command');
}

const cmd = keyword.command.extend({
const cmd = new Command({
...keyword,
context,
data: {
[this.data.field]: body,
Expand Down
56 changes: 38 additions & 18 deletions src/entity/misc/Keyword.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,51 @@
import { Command } from 'src/entity/Command';
import { Column, Entity, JoinColumn, OneToOne, PrimaryColumn } from 'typeorm';
import { Column, Entity, PrimaryColumn, PrimaryGeneratedColumn } from 'typeorm';

export interface KeywordOptions {
command: Command;
controller: string;
name: string;
import { BaseCommand } from 'src/entity/base/BaseCommand';
import { CommandOptions } from 'src/entity/Command';
import { dictToMap } from 'src/utils/Map';

export interface KeywordOptions extends CommandOptions {
controllerId: string;

/**
* The keyword to execute with.
*/
key: string;
}

@Entity()
export class Keyword implements KeywordOptions {
@OneToOne((type) => Command, (cmd) => cmd.id, {
cascade: true,
})
@JoinColumn()
public command: Command;
export const TABLE_KEYWORD = 'keyword';

@Entity(TABLE_KEYWORD)
export class Keyword extends BaseCommand implements KeywordOptions {
@Column()
public controller: string;
public controllerId: string;

@PrimaryGeneratedColumn('uuid')
public id: string;

@PrimaryColumn()
public name: string;
public key: string;

constructor(options?: KeywordOptions) {
super();

if (options) {
this.command = options.command;
this.controller = options.controller;
this.name = options.name;
this.controllerId = options.controllerId;
this.data = dictToMap(options.data);
this.key = options.key;
this.labels = dictToMap(options.labels);
this.noun = options.noun;
this.verb = options.verb;
}
}

public toJSON() {
return {
controllerId: this.controllerId,
id: this.id,
key: this.key,
noun: this.noun,
verb: this.verb,
}
}
}
44 changes: 44 additions & 0 deletions src/migration/0001545509108-KeywordCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { MigrationInterface, QueryRunner, Table } from 'typeorm';

import { TABLE_KEYWORD } from 'src/entity/misc/Keyword';

const OLD_TABLE = `${TABLE_KEYWORD}_old`;

export class KeywordCommand0001545509108 implements MigrationInterface {
public async up(query: QueryRunner): Promise<any> {
await query.renameTable(TABLE_KEYWORD, OLD_TABLE);
await query.createTable(new Table({
columns: [{
isPrimary: true,
name: 'id',
type: 'varchar',
}, {
name: 'controllerId',
type: 'varchar',
}, {
name: 'data',
type: 'varchar',
}, {
name: 'key',
type: 'varchar',
}, {
name: 'labels',
type: 'varchar',
}, {
name: 'noun',
type: 'varchar',
}, {
name: 'verb',
type: 'varchar',
}],
name: 'keyword',
}));
}

public async down(query: QueryRunner): Promise<any> {
await query.dropTable(TABLE_KEYWORD);
if (await query.hasTable(OLD_TABLE)) {
await query.renameTable(OLD_TABLE, TABLE_KEYWORD);
}
}
}
2 changes: 2 additions & 0 deletions src/module/MigrationModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { CreateFragment0001544311954 } from 'src/migration/0001544311954-CreateF
import { CreateRole0001544312069 } from 'src/migration/0001544312069-CreateRole';
import { CreateUser0001544312112 } from 'src/migration/0001544312112-CreateUser';
import { CreateToken0001544317462 } from 'src/migration/0001544317462-CreateToken';
import { KeywordCommand0001545509108 } from 'src/migration/0001545509108-KeywordCommand';

export class MigrationModule extends Module {
public async configure(options: ModuleOptions): Promise<void> {
Expand All @@ -25,6 +26,7 @@ export class MigrationModule extends Module {
CreateRole0001544312069,
CreateUser0001544312112,
CreateToken0001544317462,
KeywordCommand0001545509108,
]);
}
}

0 comments on commit 2337d9f

Please sign in to comment.