diff --git a/docs/controller/time-controller.yml b/docs/controller/time-controller.yml new file mode 100644 index 000000000..279d10d6b --- /dev/null +++ b/docs/controller/time-controller.yml @@ -0,0 +1,6 @@ +metadata: + kind: time-controller + name: test-time +data: + locale: en-US + zone: US/Central \ No newline at end of file diff --git a/docs/isolex.yml b/docs/isolex.yml index 08b283624..7f4deb734 100644 --- a/docs/isolex.yml +++ b/docs/isolex.yml @@ -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 diff --git a/src/controller/TimeController.ts b/src/controller/TimeController.ts index f14d4eb47..30ebba003 100644 --- a/src/controller/TimeController.ts +++ b/src/controller/TimeController.ts @@ -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; @@ -11,19 +14,28 @@ export interface TimeControllerData extends ControllerData { export type TimeControllerOptions = ControllerOptions; +@Inject('bot', 'clock') export class TimeController extends BaseController 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 { - 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}`); + } } } diff --git a/src/schema.yml b/src/schema.yml index 18eb1c491..9902aa857 100644 --- a/src/schema.yml +++ b/src/schema.yml @@ -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: diff --git a/src/utils/Clock.ts b/src/utils/Clock.ts index 6b20a2b45..635bea6f1 100644 --- a/src/utils/Clock.ts +++ b/src/utils/Clock.ts @@ -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); }