Skip to content

Commit

Permalink
feat: fix up time ctrl and add to ref config
Browse files Browse the repository at this point in the history
  • Loading branch information
ssube committed Dec 22, 2018
1 parent a1894ae commit 8e6828c
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 6 deletions.
6 changes: 6 additions & 0 deletions docs/controller/time-controller.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
metadata:
kind: time-controller
name: test-time
data:
locale: en-US
zone: US/Central
1 change: 1 addition & 0 deletions docs/isolex.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ data:
- !include ../docs/controller/search-controller-mdn.yml
- !include ../docs/controller/sed-controller.yml
- !include ../docs/controller/session-controller.yml
- !include ../docs/controller/time-controller.yml
- !include ../docs/controller/token-controller.yml
- !include ../docs/controller/user-controller.yml
- !include ../docs/controller/weather-controller-owm.yml
Expand Down
24 changes: 18 additions & 6 deletions src/controller/TimeController.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { Inject } from 'noicejs';

import { BaseController } from 'src/controller/BaseController';
import { Controller, ControllerData, ControllerOptions } from 'src/controller/Controller';
import { Command } from 'src/entity/Command';
import { Message } from 'src/entity/Message';
import { TYPE_TEXT } from 'src/utils/Mime';
import { Clock } from 'src/utils/Clock';

export const NOUN_TIME = 'time';

export interface TimeControllerData extends ControllerData {
locale: string;
Expand All @@ -11,19 +14,28 @@ export interface TimeControllerData extends ControllerData {

export type TimeControllerOptions = ControllerOptions<TimeControllerData>;

@Inject('bot', 'clock')
export class TimeController extends BaseController<TimeControllerData> implements Controller {
protected template: HandlebarsTemplateDelegate;
protected readonly clock: Clock;

constructor(options: TimeControllerOptions) {
super(options, 'isolex#/definitions/service-controller-time');
super(options, 'isolex#/definitions/service-controller-time', [NOUN_TIME]);

this.clock = options.clock;
}

public async handle(cmd: Command): Promise<void> {
const date = new Date();
const date = this.clock.getDate();
const locale = cmd.getHeadOrDefault('locale', this.data.locale);
const zone = cmd.getHeadOrDefault('zone', this.data.zone);

this.logger.debug({ date, locale, zone }, 'handling time');
await this.bot.sendMessage(Message.reply(cmd.context, TYPE_TEXT, date.toLocaleString(locale, { timeZone: zone })));

try {
const localDate = date.toLocaleString(locale, { timeZone: zone });
return this.reply(cmd.context, localDate);
} catch (err) {
return this.reply(cmd.context, `error formatting date: ${err.message}`);
}
}
}
9 changes: 9 additions & 0 deletions src/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,15 @@ definitions:
$ref: "#/definitions/service-controller"
service-controller-session:
$ref: "#/definitions/service-controller"
service-controller-time:
allOf:
- $ref: "#/definitions/service-controller"
- type: object
properties:
locale:
type: string
zone:
type: string
service-controller-token:
$ref: "#/definitions/service-controller"
service-controller-user:
Expand Down
4 changes: 4 additions & 0 deletions src/utils/Clock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export class Clock {
this.date = date;
}

public getDate(): Date {
return new Date();
}

public getSeconds(): number {
return Math.floor(this.date.now() / NOW_TO_SECONDS);
}
Expand Down

0 comments on commit 8e6828c

Please sign in to comment.