From 1e20e2601fa9c2951d811861ff97f3f555aac6af Mon Sep 17 00:00:00 2001 From: Jonathan Baird Date: Wed, 8 Apr 2020 11:54:00 -0600 Subject: add a scheduled notification cleanup job --- models/users.js | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/models/users.js b/models/users.js index 20581e65..9b2a5465 100644 --- a/models/users.js +++ b/models/users.js @@ -1,3 +1,5 @@ +import { SyncedCron } from 'meteor/percolate:synced-cron'; + // Sandstorm context is detected using the METEOR_SETTINGS environment variable // in the package definition. const isSandstorm = @@ -926,6 +928,37 @@ if (Meteor.isServer) { }); } +const addCronJob = _.debounce( + Meteor.bindEnvironment(function notificationCleanupDebounced() { + // passed in the removeAge has to be a number standing for the number of days after a notification is read before we remove it + const envRemoveAge = process.env.NOTIFICATION_REMOVAL_AGE; + // default notifications will be removed 2 days after they are read + const defaultRemoveAge = 2; + const removeAge = parseInt(envRemoveAge, 10) || defaultRemoveAge; + + SyncedCron.add({ + name: 'notification_cleanup', + schedule: parser => parser.text('every 1 days'), + job: () => { + for (const user of Users.find()) { + for (const notification of user.profile.notifications) { + if (notification.read) { + const removeDate = new Date(notification.read); + removeDate.setDate(removeDate.getDate() + removeAge); + if (removeDate <= new Date()) { + user.removeNotification(notification.activity); + } + } + } + } + }, + }); + + SyncedCron.start(); + }), + 500, +); + if (Meteor.isServer) { // Let mongoDB ensure username unicity Meteor.startup(() => { @@ -939,6 +972,9 @@ if (Meteor.isServer) { }, { unique: true }, ); + Meteor.defer(() => { + addCronJob(); + }); }); // OLD WAY THIS CODE DID WORK: When user is last admin of board, -- cgit v1.2.3-1-g7c22