Skip to content

Commit

Permalink
Introduced a mechanism that prevents the repetition of notifications …
Browse files Browse the repository at this point in the history
…that have already been dispatched.
  • Loading branch information
ransome1 committed Nov 23, 2023
1 parent abcf739 commit 2389540
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
10 changes: 9 additions & 1 deletion src/main/config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ if (!fs.existsSync(filtersPath)) {
fs.writeFileSync(filtersPath, JSON.stringify(defaultFilterData));
}

const notifiedTodoObjectsPath = path.join(userDataDirectory, 'notifiedTodoObjects.json');
const notifiedTodoObjectsStorage = new Store<{}>({ cwd: userDataDirectory, name: 'notifiedTodoObjects' });

if (!fs.existsSync(notifiedTodoObjectsPath)) {
const defaultNotifiedTodoObjectsData = {};
fs.writeFileSync(notifiedTodoObjectsPath, JSON.stringify(defaultNotifiedTodoObjectsData));
}

if (!fs.existsSync(customStylesPath)) {
fs.writeFileSync(customStylesPath, '');
}
Expand Down Expand Up @@ -175,4 +183,4 @@ configStorage.onDidChange('tray', () => {

nativeTheme.on('updated', handleTheme);

export { configStorage, filterStorage };
export { configStorage, filterStorage, notifiedTodoObjectsStorage };
13 changes: 8 additions & 5 deletions src/main/modules/HandleNotification.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import crypto from 'crypto';
import { Notification } from 'electron';
import { configStorage } from '../config';
import { configStorage, notifiedTodoObjectsStorage } from '../config';
import { Badge } from '../util';
import dayjs, { Dayjs } from "dayjs";
import isToday from 'dayjs/plugin/isToday';
import isTomorrow from 'dayjs/plugin/isTomorrow';
dayjs.extend(isToday);
dayjs.extend(isTomorrow);

export const notifiedTodoObjects = new Set<number>();

export const sendNotification = (title: string, body: string) => {
const options = {
title,
Expand Down Expand Up @@ -37,6 +36,7 @@ export function mustNotify(date: Date): boolean {

export function handleNotification(id: number, due: string | null, body: string, badge: Badge) {
const notificationAllowed = configStorage.get('notificationsAllowed');
const hash = crypto.createHash('sha256').update(body).digest('hex');

if (notificationAllowed) {
const today = dayjs().startOf('day');
Expand All @@ -47,9 +47,12 @@ export function handleNotification(id: number, due: string | null, body: string,
if (dueDate.isBefore(today.add(notificationThreshold, 'day'))) {
badge.count += 1;

if (!notifiedTodoObjects.has(id)) {
const notifiedTodoObjects = new Set<number>(notifiedTodoObjectsStorage.get('notifiedTodoObjects', []));

if (!notifiedTodoObjects.has(hash)) {
sendNotification(daysUntilDue, body);
notifiedTodoObjects.add(id);
notifiedTodoObjects.add(hash);
notifiedTodoObjectsStorage.set('notifiedTodoObjects', Array.from(notifiedTodoObjects));
}
}
}
Expand Down
13 changes: 12 additions & 1 deletion src/main/modules/Ipc.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import processDataRequest from './ProcessDataRequest';
import { changeCompleteState } from './TodoObject/ChangeCompleteState';
import { writeTodoObjectToFile, removeLineFromFile } from './File/Write';
import archiveTodos from './File/Archive';
import { configStorage, filterStorage } from '../config';
import { configStorage, filterStorage, notifiedTodoObjectsStorage } from '../config';
import { addFile, setFile, removeFile, revealFile } from './File/File';
import { openFile, createFile, changeDoneFilePath } from './File/Dialog';

Expand Down Expand Up @@ -56,6 +56,15 @@ function handleStoreSetFilters(event: IpcMainEvent, value: any): void {
}
}

function handleStoreSetNotifiedTodoObjects(event: IpcMainEvent, value: any): void {
try {
notifiedTodoObjectsStorage.set('notifiedTodoObjects', value);
console.log(`ipcEvents.ts: notifiedTodoObjects saved`);
} catch (error: any) {
console.error('ipcEvents.ts:', error);
}
}

function handleSetFile(event: IpcMainEvent, index: number): void {
try {
setFile(index);
Expand Down Expand Up @@ -134,6 +143,7 @@ function removeEventListeners(): void {
ipcMain.off('storeGetConfig', handleStoreGetConfig);
ipcMain.off('storeSetConfig', handleStoreSetConfig);
ipcMain.off('storeSetFilters', handleStoreSetFilters);
ipcMain.off('storeSetNotifiedTodoObjects', handleStoreSetNotifiedTodoObjects);
ipcMain.off('setFile', handleSetFile);
ipcMain.off('removeFile', handleRemoveFile);
ipcMain.off('openFile', openFile);
Expand All @@ -154,6 +164,7 @@ app.on('before-quit', removeEventListeners);
ipcMain.on('storeGetConfig', handleStoreGetConfig);
ipcMain.on('storeSetConfig', handleStoreSetConfig);
ipcMain.on('storeSetFilters', handleStoreSetFilters);
ipcMain.on('storeSetNotifiedTodoObjects', handleStoreSetNotifiedTodoObjects);
ipcMain.on('setFile', handleSetFile);
ipcMain.on('removeFile', handleRemoveFile);
ipcMain.on('openFile', openFile);
Expand Down
3 changes: 3 additions & 0 deletions src/main/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ contextBridge.exposeInMainWorld('api', {
setFilters(value) {
ipcRenderer.send('storeSetFilters', value);
},
notifiedTodoObjects(value) {
ipcRenderer.send('storeSetNotifiedTodoObjects', value);
},
},
ipcRenderer: {
send(channel, ...args) {
Expand Down

0 comments on commit 2389540

Please sign in to comment.