Browse Source

add a scheduled notification cleanup job

Jonathan Baird 5 years ago
parent
commit
1e20e2601f
1 changed files with 36 additions and 0 deletions
  1. 36 0
      models/users.js

+ 36 - 0
models/users.js

@@ -1,3 +1,5 @@
+import { SyncedCron } from 'meteor/percolate:synced-cron';
+
 // Sandstorm context is detected using the METEOR_SETTINGS environment variable
 // Sandstorm context is detected using the METEOR_SETTINGS environment variable
 // in the package definition.
 // in the package definition.
 const isSandstorm =
 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) {
 if (Meteor.isServer) {
   // Let mongoDB ensure username unicity
   // Let mongoDB ensure username unicity
   Meteor.startup(() => {
   Meteor.startup(() => {
@@ -939,6 +972,9 @@ if (Meteor.isServer) {
       },
       },
       { unique: true },
       { unique: true },
     );
     );
+    Meteor.defer(() => {
+      addCronJob();
+    });
   });
   });
 
 
   // OLD WAY THIS CODE DID WORK: When user is last admin of board,
   // OLD WAY THIS CODE DID WORK: When user is last admin of board,