Skip to content

Commit e3e6f7c

Browse files
committed
work on web-push
1 parent fdeadaa commit e3e6f7c

1 file changed

Lines changed: 15 additions & 7 deletions

File tree

api-server/source/notifications/notify-command.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,19 @@ class NotifyContext {
2020
webPush: WebPushRepo;
2121
sql: SqlDependency;
2222
appConfig: AppConfig;
23+
date: Date;
2324
constructor(
2425
options: NotifyOptions,
2526
webPush: WebPushRepo,
2627
sql: SqlDependency,
2728
appConfig: AppConfig,
29+
date: Date,
2830
) {
2931
this.options = options;
3032
this.webPush = webPush;
3133
this.sql = sql;
3234
this.appConfig = appConfig;
35+
this.date = date;
3336
}
3437

3538
/** Log a timestamped message */
@@ -49,27 +52,32 @@ export interface NotifyOptions {
4952
forever: boolean;
5053
interval: number;
5154
grace: number;
55+
date?: string;
5256
}
5357

5458
export async function notifyCommand(options: NotifyOptions) {
59+
// Parse the date from options or use "now"
60+
const date = options.date ? new Date(options.date) : new Date();
61+
if (Number.isNaN(date.getTime())) throw new Error("invalid date option");
62+
5563
// Set up context
5664
const appConfig = useAppConfig();
5765
const store = useStore();
5866
const sql = useDatabase();
5967
const webPush = WebPushRepo.use();
60-
const ctx = new NotifyContext(options, webPush, sql, appConfig);
68+
const ctx = new NotifyContext(options, webPush, sql, appConfig, date);
6169

6270
ctx.log("init");
6371

6472
try {
65-
while (options.forever) {
73+
do {
6674
ctx.log("starting");
6775

6876
await enqueueMySchedule(ctx);
6977
await sendPendingMessages(ctx);
7078

71-
await ctx.pause(options.interval);
72-
}
79+
if (options.forever) await ctx.pause(options.interval);
80+
} while (options.forever);
7381

7482
ctx.log("done");
7583
} catch (error) {
@@ -86,16 +94,16 @@ interface PendingMessage {
8694
payload: WebPushPayload;
8795
}
8896

89-
async function enqueueMySchedule(ctx: NotifyContext, date = new Date()) {
97+
async function enqueueMySchedule(ctx: NotifyContext) {
9098
ctx.log("enqueue from schedule…");
9199

92100
// Get sessions starting in 15 minutes or started 5 minutes ago
93101
const upcoming = await SessionTable.select(
94102
ctx.sql,
95103
ctx.sql`
96104
start_date IS NOT NULL
97-
AND start_date >= ${date} - INTERVAL '15 minutes'
98-
AND start_date <= ${date} + INTERVAL '5 minutes'
105+
AND start_date >= ${ctx.date} - INTERVAL '15 minutes'
106+
AND start_date <= ${ctx.date} + INTERVAL '5 minutes'
99107
`,
100108
);
101109

0 commit comments

Comments
 (0)