Skip to content

Commit 8e6828c

Browse files
committed
feat: fix up time ctrl and add to ref config
1 parent a1894ae commit 8e6828c

File tree

5 files changed

+38
-6
lines changed

5 files changed

+38
-6
lines changed

docs/controller/time-controller.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
metadata:
2+
kind: time-controller
3+
name: test-time
4+
data:
5+
locale: en-US
6+
zone: US/Central

docs/isolex.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ data:
2121
- !include ../docs/controller/search-controller-mdn.yml
2222
- !include ../docs/controller/sed-controller.yml
2323
- !include ../docs/controller/session-controller.yml
24+
- !include ../docs/controller/time-controller.yml
2425
- !include ../docs/controller/token-controller.yml
2526
- !include ../docs/controller/user-controller.yml
2627
- !include ../docs/controller/weather-controller-owm.yml

src/controller/TimeController.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import { Inject } from 'noicejs';
2+
13
import { BaseController } from 'src/controller/BaseController';
24
import { Controller, ControllerData, ControllerOptions } from 'src/controller/Controller';
35
import { Command } from 'src/entity/Command';
4-
import { Message } from 'src/entity/Message';
5-
import { TYPE_TEXT } from 'src/utils/Mime';
6+
import { Clock } from 'src/utils/Clock';
7+
8+
export const NOUN_TIME = 'time';
69

710
export interface TimeControllerData extends ControllerData {
811
locale: string;
@@ -11,19 +14,28 @@ export interface TimeControllerData extends ControllerData {
1114

1215
export type TimeControllerOptions = ControllerOptions<TimeControllerData>;
1316

17+
@Inject('bot', 'clock')
1418
export class TimeController extends BaseController<TimeControllerData> implements Controller {
15-
protected template: HandlebarsTemplateDelegate;
19+
protected readonly clock: Clock;
1620

1721
constructor(options: TimeControllerOptions) {
18-
super(options, 'isolex#/definitions/service-controller-time');
22+
super(options, 'isolex#/definitions/service-controller-time', [NOUN_TIME]);
23+
24+
this.clock = options.clock;
1925
}
2026

2127
public async handle(cmd: Command): Promise<void> {
22-
const date = new Date();
28+
const date = this.clock.getDate();
2329
const locale = cmd.getHeadOrDefault('locale', this.data.locale);
2430
const zone = cmd.getHeadOrDefault('zone', this.data.zone);
2531

2632
this.logger.debug({ date, locale, zone }, 'handling time');
27-
await this.bot.sendMessage(Message.reply(cmd.context, TYPE_TEXT, date.toLocaleString(locale, { timeZone: zone })));
33+
34+
try {
35+
const localDate = date.toLocaleString(locale, { timeZone: zone });
36+
return this.reply(cmd.context, localDate);
37+
} catch (err) {
38+
return this.reply(cmd.context, `error formatting date: ${err.message}`);
39+
}
2840
}
2941
}

src/schema.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,15 @@ definitions:
276276
$ref: "#/definitions/service-controller"
277277
service-controller-session:
278278
$ref: "#/definitions/service-controller"
279+
service-controller-time:
280+
allOf:
281+
- $ref: "#/definitions/service-controller"
282+
- type: object
283+
properties:
284+
locale:
285+
type: string
286+
zone:
287+
type: string
279288
service-controller-token:
280289
$ref: "#/definitions/service-controller"
281290
service-controller-user:

src/utils/Clock.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ export class Clock {
1414
this.date = date;
1515
}
1616

17+
public getDate(): Date {
18+
return new Date();
19+
}
20+
1721
public getSeconds(): number {
1822
return Math.floor(this.date.now() / NOW_TO_SECONDS);
1923
}

0 commit comments

Comments
 (0)