1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-11-29 22:48:10 +02:00

Electron: Fixes #476 (maybe): Trying to fix notification flood. Added more log statements in case something goes wrong.

This commit is contained in:
Laurent Cozic
2018-12-08 00:42:29 +01:00
parent 8e601e80df
commit 71098102c5
3 changed files with 70 additions and 56 deletions

View File

@@ -43,65 +43,69 @@ class AlarmService {
// When passing a note, make sure it has all the required properties
// (better to pass a complete note or else just the ID)
static async updateNoteNotification(noteOrId, isDeleted = false) {
let note = null;
let noteId = null;
try {
let note = null;
let noteId = null;
if (typeof noteOrId === 'object') {
note = noteOrId;
noteId = note.id;
} else {
note = await Note.load(noteOrId);
noteId = note ? note.id : null;
}
if (!note && !isDeleted) return;
const driver = this.driver();
let alarm = noteId ? await Alarm.byNoteId(noteId) : null;
let clearAlarm = false;
if (isDeleted ||
!Note.needAlarm(note) ||
(alarm && alarm.trigger_time !== note.todo_due))
{
clearAlarm = !!alarm;
}
if (!clearAlarm && alarm) { // Alarm already exists and set at the right time
// For persistent notifications (those that stay active after the app has been closed, like on mobile), if we have
// an alarm object we can be sure that the notification has already been set, so there's nothing to do.
// For non-persistent notifications however we need to check that the notification has been set because, for example,
// if the app has just started the notifications need to be set again. so we do this below.
if (!driver.hasPersistentNotifications() && !driver.notificationIsSet(alarm.id)) {
const notification = await Alarm.makeNotification(alarm, note);
this.logger().info('Scheduling (non-persistent) notification for note ' + note.id, notification);
driver.scheduleNotification(notification);
if (typeof noteOrId === 'object') {
note = noteOrId;
noteId = note.id;
} else {
note = await Note.load(noteOrId);
noteId = note ? note.id : null;
}
return;
if (!note && !isDeleted) return;
const driver = this.driver();
let alarm = noteId ? await Alarm.byNoteId(noteId) : null;
let clearAlarm = false;
if (isDeleted ||
!Note.needAlarm(note) ||
(alarm && alarm.trigger_time !== note.todo_due))
{
clearAlarm = !!alarm;
}
if (!clearAlarm && alarm) { // Alarm already exists and set at the right time
// For persistent notifications (those that stay active after the app has been closed, like on mobile), if we have
// an alarm object we can be sure that the notification has already been set, so there's nothing to do.
// For non-persistent notifications however we need to check that the notification has been set because, for example,
// if the app has just started the notifications need to be set again. so we do this below.
if (!driver.hasPersistentNotifications() && !driver.notificationIsSet(alarm.id)) {
const notification = await Alarm.makeNotification(alarm, note);
this.logger().info('Scheduling (non-persistent) notification for note ' + note.id, notification);
driver.scheduleNotification(notification);
}
return;
}
if (clearAlarm) {
this.logger().info('Clearing notification for note ' + noteId);
await driver.clearNotification(alarm.id);
await Alarm.delete(alarm.id);
}
if (isDeleted || !Note.needAlarm(note)) return;
await Alarm.save({
note_id: note.id,
trigger_time: note.todo_due,
});
// Reload alarm to get its ID
alarm = await Alarm.byNoteId(note.id);
const notification = await Alarm.makeNotification(alarm, note);
this.logger().info('Scheduling notification for note ' + note.id, notification);
await driver.scheduleNotification(notification);
} catch (error) {
this.logger().error('Could not update notification', error);
}
if (clearAlarm) {
this.logger().info('Clearing notification for note ' + noteId);
await driver.clearNotification(alarm.id);
await Alarm.delete(alarm.id);
}
if (isDeleted || !Note.needAlarm(note)) return;
await Alarm.save({
note_id: note.id,
trigger_time: note.todo_due,
});
// Reload alarm to get its ID
alarm = await Alarm.byNoteId(note.id);
const notification = await Alarm.makeNotification(alarm, note);
this.logger().info('Scheduling notification for note ' + note.id, notification);
await driver.scheduleNotification(notification);
}
static async updateAllNotifications() {