1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-12 08:54:00 +02:00
joplin/packages/lib/models/Alarm.ts

62 lines
1.6 KiB
TypeScript
Raw Normal View History

2020-11-05 18:58:23 +02:00
const BaseModel = require('../BaseModel').default;
const Note = require('./Note.js');
export interface Notification {
id: number;
noteId: string;
date: Date;
title: string;
body?: string;
}
export default class Alarm extends BaseModel {
static tableName() {
return 'alarms';
}
static modelType() {
return BaseModel.TYPE_ALARM;
}
static byNoteId(noteId: string) {
return this.modelSelectOne('SELECT * FROM alarms WHERE note_id = ?', [noteId]);
}
static async deleteExpiredAlarms() {
return this.db().exec('DELETE FROM alarms WHERE trigger_time <= ?', [Date.now()]);
}
static async alarmIdsWithoutNotes() {
// https://stackoverflow.com/a/4967229/561309
const alarms = await this.db().selectAll('SELECT alarms.id FROM alarms LEFT JOIN notes ON alarms.note_id = notes.id WHERE notes.id IS NULL');
return alarms.map((a: any) => {
2019-07-29 15:43:53 +02:00
return a.id;
});
}
static async makeNotification(alarm: any, note: any = null): Promise<Notification> {
if (!note) {
note = await Note.load(alarm.note_id);
} else if (!note.todo_due) {
this.logger().warn('Trying to create notification for note with todo_due property - reloading note object in case we are dealing with a partial note');
note = await Note.load(alarm.note_id);
this.logger().warn('Reloaded note:', note);
}
2017-11-28 22:17:34 +02:00
const output: Notification = {
2017-11-28 22:17:34 +02:00
id: alarm.id,
noteId: alarm.note_id,
2017-11-28 22:17:34 +02:00
date: new Date(note.todo_due),
2019-07-29 15:43:53 +02:00
title: note.title.substr(0, 128),
2017-11-28 22:17:34 +02:00
};
2019-07-29 15:43:53 +02:00
if (note.body) output.body = note.body.substr(0, 512);
2017-11-28 22:17:34 +02:00
2019-07-29 15:43:53 +02:00
return output;
2017-11-28 22:17:34 +02:00
}
static async allDue() {
return this.modelSelectAll('SELECT * FROM alarms WHERE trigger_time >= ?', [Date.now()]);
}
}