1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-04-20 11:28:40 +02:00

49 lines
1.2 KiB
JavaScript
Raw Normal View History

2018-03-09 17:49:35 +00:00
const BaseModel = require("lib/BaseModel.js");
const Note = require("lib/models/Note.js");
class Alarm extends BaseModel {
static tableName() {
2018-03-09 17:49:35 +00:00
return "alarms";
}
static modelType() {
return BaseModel.TYPE_ALARM;
}
static byNoteId(noteId) {
2018-03-09 17:49:35 +00:00
return this.modelSelectOne("SELECT * FROM alarms WHERE note_id = ?", [noteId]);
}
static async deleteExpiredAlarms() {
2018-03-09 17:49:35 +00:00
return this.db().exec("DELETE FROM alarms WHERE trigger_time <= ?", [Date.now()]);
}
static async alarmIdsWithoutNotes() {
// https://stackoverflow.com/a/4967229/561309
2018-03-09 17:49:35 +00:00
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 => {
return a.id;
});
}
2017-11-28 20:17:34 +00:00
static async makeNotification(alarm, note = null) {
if (!note) note = await Note.load(alarm.note_id);
const output = {
id: alarm.id,
date: new Date(note.todo_due),
2018-03-09 17:49:35 +00:00
title: note.title.substr(0, 128),
2017-11-28 20:17:34 +00:00
};
2018-03-09 17:49:35 +00:00
if (note.body) output.body = note.body.substr(0, 512);
2017-11-28 20:17:34 +00:00
2018-03-09 17:49:35 +00:00
return output;
2017-11-28 20:17:34 +00:00
}
static async allDue() {
2018-03-09 17:49:35 +00:00
return this.modelSelectAll("SELECT * FROM alarms WHERE trigger_time >= ?", [Date.now()]);
}
}
2018-03-09 17:49:35 +00:00
module.exports = Alarm;