Skip to content

Commit

Permalink
fix: enable strict properties, begin fixing errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ssube committed Jan 4, 2019
1 parent b3afa71 commit ad0f39c
Show file tree
Hide file tree
Showing 22 changed files with 126 additions and 238 deletions.
2 changes: 2 additions & 0 deletions config/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
"noImplicitThis": true,
"paths": {},
"sourceMap": true,
"strict": true,
"strictBindCallApply": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"strictPropertyInitialization": true,
"target": "es2017",
"types": [
"chai-as-promised",
Expand Down
34 changes: 16 additions & 18 deletions src/Bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { ContextFetchOptions, Listener, ListenerData } from 'src/listener/Listen
import { ServiceModule } from 'src/module/ServiceModule';
import { Parser, ParserData } from 'src/parser/Parser';
import { Service, ServiceDefinition, ServiceEvent } from 'src/Service';
import { filterNil, mustFind } from 'src/utils';
import { filterNil, mustFind, mustExist } from 'src/utils';
import { incrementServiceCounter } from 'src/utils/metrics/Service';
import { StorageLogger, StorageLoggerOptions } from 'src/utils/StorageLogger';

Expand All @@ -39,11 +39,11 @@ export class Bot extends BaseService<BotData> implements Service {
protected readonly container: Container;
protected readonly metrics: Registry;

protected storage: Connection;
protected storage?: Connection;

// counters
protected cmdCounter: Counter;
protected msgCounter: Counter;
protected cmdCounter!: Counter;
protected msgCounter!: Counter;

// services
protected controllers: Array<Controller>;
Expand Down Expand Up @@ -77,11 +77,11 @@ export class Bot extends BaseService<BotData> implements Service {
this.incoming = new Subject();
this.outgoing = new Subject();

bindAll(this, 'looseError');
this.startMetrics();
}

public getStorage(): Connection {
return this.storage;
return mustExist(this.storage);
}

public async notify(event: ServiceEvent) {
Expand All @@ -103,17 +103,13 @@ export class Bot extends BaseService<BotData> implements Service {
*/
public async start() {
await super.start();

this.logger.info('starting bot');

this.logger.info('setting up streams');
/* tslint:disable:no-unbound-method */
this.commands.subscribe((next) => this.receiveCommand(next).catch(this.looseError));
this.incoming.subscribe((next) => this.receive(next).catch(this.looseError));
this.outgoing.subscribe((next) => this.receiveMessage(next).catch(this.looseError));
/* tslint:enable */
const streamError = (err: Error) => this.looseError(err);
this.commands.subscribe((next) => this.receiveCommand(next).catch(streamError));
this.incoming.subscribe((next) => this.receive(next).catch(streamError));
this.outgoing.subscribe((next) => this.receiveMessage(next).catch(streamError));

await this.startMetrics();
await this.startStorage();
await this.startServices();

Expand Down Expand Up @@ -174,9 +170,10 @@ export class Bot extends BaseService<BotData> implements Service {
* Add a message to the send queue.
*/
public async sendMessage(...messages: Array<Message>): Promise<Array<Message>> {
const storage = mustExist(this.storage);
const results = [];
for (const data of messages) {
const msg = await this.storage.getRepository(Message).save(data);
const msg = await storage.getRepository(Message).save(data);
this.logger.debug({ msg }, 'message saved');
this.outgoing.next(msg);
results.push(msg);
Expand All @@ -185,9 +182,10 @@ export class Bot extends BaseService<BotData> implements Service {
}

public async executeCommand(...commands: Array<Command>): Promise<Array<Command>> {
const storage = mustExist(this.storage);
const results = [];
for (const data of commands) {
const cmd = await this.storage.getRepository(Command).save(data);
const cmd = await storage.getRepository(Command).save(data);
this.commands.next(cmd);
results.push(cmd);
}
Expand Down Expand Up @@ -281,7 +279,7 @@ export class Bot extends BaseService<BotData> implements Service {
return commands;
}

protected async startMetrics() {
protected startMetrics() {
this.logger.info('setting up metrics');

this.cmdCounter = new Counter({
Expand Down Expand Up @@ -354,7 +352,7 @@ export class Bot extends BaseService<BotData> implements Service {
* Note: this method is already bound, so it can be passed with `this.looseError`. Using that requires
* `tslint:disable:no-unbound-method` as well.
*/
protected async looseError(err: Error) {
protected looseError(err: Error) {
this.logger.error(err, 'bot stream did not handle error');
}
}
21 changes: 10 additions & 11 deletions src/controller/CompletionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export type CompletionControllerOptions = ControllerOptions<CompletionController
export class CompletionController extends BaseController<CompletionControllerData> implements Controller {
protected readonly storage: Connection;
protected readonly fragmentRepository: Repository<Fragment>;
protected target: Listener;
protected target?: Listener;

constructor(options: CompletionControllerOptions) {
super(options, 'isolex#/definitions/service-controller-completion', [NOUN_FRAGMENT]);
Expand All @@ -37,7 +37,7 @@ export class CompletionController extends BaseController<CompletionControllerDat
public async start() {
await super.start();

this.target = this.services.getService(this.data.defaultTarget);
this.target = this.services.getService<Listener>(this.data.defaultTarget);
}

@HandleNoun(NOUN_FRAGMENT)
Expand All @@ -51,15 +51,14 @@ export class CompletionController extends BaseController<CompletionControllerDat
const parserId = cmd.getHead('parser');
const verb = cmd.getHead('verb') as CommandVerb;

const fragment = await this.fragmentRepository.save(new Fragment({
data: cmd.data,
key,
labels: cmd.labels,
noun,
parserId,
userId: user.id,
verb,
}));
const fragment = new Fragment();
fragment.data = cmd.data;
fragment.key = key;
fragment.labels = cmd.labels;
fragment.noun = noun;
fragment.parserId = parserId;
fragment.userId = user.id;
fragment.verb = verb;

const context = await this.createContext(cmd.context);
this.logger.debug({ context, fragment }, 'creating fragment for later completion');
Expand Down
15 changes: 7 additions & 8 deletions src/controller/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,27 @@ import { NOUN_FRAGMENT } from 'src/controller/CompletionController';
import { Command, CommandVerb } from 'src/entity/Command';
import { InvalidArgumentError } from 'src/error/InvalidArgumentError';
import { Schema } from 'src/schema';
import { Dict, mapToDict } from 'src/utils/Map';
import { Dict, mapToDict, dictToMap } from 'src/utils/Map';

export function createCompletion(cmd: Command, key: string, msg: string): Command {
if (isNil(cmd.context.parser)) {
throw new InvalidArgumentError('command has no parser to prompt for completion');
}

const existingData = mapToDict(cmd.data);
return new Command({
context: cmd.context,
data: {
const fragment = new Command();
fragment.context = cmd.context;
fragment.data = dictToMap({
...existingData,
key: [key],
msg: [msg],
noun: [cmd.noun],
parser: [cmd.context.parser.id],
verb: [cmd.verb],
},
labels: {},
noun: NOUN_FRAGMENT,
verb: CommandVerb.Create,
});
fragment.noun = NOUN_FRAGMENT;
fragment.verb = CommandVerb.Create;
return fragment;
}

type CollectData = number | string | Array<string>;
Expand Down
15 changes: 4 additions & 11 deletions src/entity/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,10 @@ export class Command extends BaseCommand implements CommandOptions {
cascade: true,
})
@JoinColumn()
public context: Context;
public context!: Context;

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

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

if (options && options.context) {
this.context = options.context;
}
}
public id: string = '';

public extend(options: Partial<CommandOptions>) {
if (options.noun) {
Expand All @@ -56,7 +48,8 @@ export class Command extends BaseCommand implements CommandOptions {
throw new InvalidArgumentError('extended commands may not change verb');
}

const cmd = new Command(this);
const cmd = new Command();
Object.assign(cmd, this); // TODO: is this right?
if (options.context) {
cmd.context = options.context;
}
Expand Down
8 changes: 4 additions & 4 deletions src/entity/Context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ export const TABLE_CONTEXT = 'context';
@Entity(TABLE_CONTEXT)
export class Context extends BaseEntity implements ContextOptions {
@Column('simple-json')
public channel: ChannelData;
public channel!: ChannelData;

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

@Column()
public name: string;
public name: string = '';

public parser?: Parser;

Expand All @@ -64,7 +64,7 @@ export class Context extends BaseEntity implements ContextOptions {
public token?: Token;

@Column()
public uid: string;
public uid: string = '';

public user?: User;

Expand Down
22 changes: 4 additions & 18 deletions src/entity/Fragment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,16 @@ export interface FragmentOptions extends CommandOptions {
@Entity(TABLE_FRAGMENT)
export class Fragment extends BaseCommand implements FragmentOptions {
@PrimaryGeneratedColumn('uuid')
public id: string;
public id: string = '';

@Column()
public key: string;
public key: string = '';

@Column()
public parserId: string;
public parserId: string = '';

@Column()
public userId: string;

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

if (!isNil(options)) {
this.data = dictToMap(options.data);
this.key = options.key;
this.labels = dictToMap(options.labels);
this.noun = options.noun;
this.parserId = options.parserId;
this.userId = options.userId;
this.verb = options.verb;
}
}
public userId: string = '';

public toJSON() {
return {
Expand Down
33 changes: 11 additions & 22 deletions src/entity/Message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { LabelEntity } from 'src/entity/base/LabelEntity';
import { Context, GRAPH_OUTPUT_CONTEXT } from 'src/entity/Context';
import { GRAPH_INPUT_NAME_MULTI_VALUE_PAIR, GRAPH_INPUT_NAME_VALUE_PAIR } from 'src/schema/graph/input/Pairs';
import { GRAPH_OUTPUT_NAME_MULTI_VALUE_PAIR, GRAPH_OUTPUT_NAME_VALUE_PAIR } from 'src/schema/graph/output/Pairs';
import { TYPE_TEXT } from 'src/utils/Mime';

export interface MessageOptions {
body: string;
Expand All @@ -22,46 +23,34 @@ export class Message extends LabelEntity implements MessageOptions {
}

public static reply(context: Context, type: string, body: string): Message {
return new Message({
body,
context,
reactions: [],
type,
});
const msg = new Message();
msg.body = body;
msg.context = context;
msg.type = type;
return msg;
}

@Column()
public body: string;
public body: string = '';

@OneToOne((type) => Context, (context) => context.id, {
cascade: true,
})
@JoinColumn()
public context: Context;
public context!: Context;

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

@Column('simple-json')
public reactions: Array<string>;
public reactions: Array<string> = [];

/**
* MIME type of the message. Typically `text/plain`, but can be an `image/*` or `audio/*` type, depending on the
* listener.
*/
@Column()
public type: string;

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

if (options) {
this.body = options.body;
this.context = options.context;
this.reactions = Array.from(options.reactions || []);
this.type = options.type;
}
}
public type: string = TYPE_TEXT;

public toJSON(): object {
return {
Expand Down
6 changes: 3 additions & 3 deletions src/entity/Tick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ export const TABLE_TICK = 'tick';
@Entity(TABLE_TICK)
export class Tick extends BaseEntity {
@PrimaryGeneratedColumn('uuid')
public id: string;
public id: string = '';

@Column()
public intervalId: string;
public intervalId: string = '';

@Column()
public status: number;
public status: number = 0;

public toJSON(): object {
return {
Expand Down
15 changes: 3 additions & 12 deletions src/entity/auth/Role.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,15 @@ export const TABLE_ROLE = 'role';
@Entity(TABLE_ROLE)
export class Role extends BaseEntity implements RoleOptions {
@PrimaryGeneratedColumn('uuid')
public id: string;
public id: string = '';

@Column({
unique: true,
})
public name: string;
public name: string = '';

@Column('simple-json')
public grants: Array<string>;

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

if (options) {
this.grants = Array.from(options.grants);
this.name = options.name;
}
}
public grants: Array<string> = [];

public toJSON() {
return {
Expand Down
Loading

0 comments on commit ad0f39c

Please sign in to comment.