Node.js based forum software built for the modern web
Merge branch 'master' into notif-abort
Peter Jaszkowiak932ae4e86daa
diff --cc src/messaging/data.jsindex b565294902,b9c70dcfda..b3a2ba58dd--- a/src/messaging/data.js+++ b/src/messaging/data.js@@@ -6,9 -6,10 +6,11 @@@ var S = require('string')var db = require('../database');var user = require('../user');var utils = require('../utils');+var plugins = require('../plugins');module.exports = function (Messaging) {+ Messaging.newMessageCutoff = 1000 * 60 * 3;+Messaging.getMessageField = function (mid, field, callback) {Messaging.getMessageFields(mid, [field], function (err, fields) {callback(err, fields ? fields[field] : null);diff --cc src/notifications.jsindex 057a86e674,bb002238e6..a158a036fb--- a/src/notifications.js+++ b/src/notifications.js@@@ -84,202 -86,287 +86,290 @@@ Notifications.filterExists = function (return exists[idx];});- callback(null, nids);- });- };-- Notifications.findRelated = function (mergeIds, set, callback) {- // A related notification is one in a zset that has the same mergeId- var _nids;-- async.waterfall([- async.apply(db.getSortedSetRevRange, set, 0, -1),- function (nids, next) {- _nids = nids;+ next(null, nids);+ },+ ], callback);+ };- var keys = nids.map(function (nid) {- return 'notifications:' + nid;- });+ Notifications.findRelated = function (mergeIds, set, callback) {+ // A related notification is one in a zset that has the same mergeId+ var _nids;- db.getObjectsFields(keys, ['mergeId'], next);- },- ], function (err, sets) {- if (err) {- return callback(err);- }+ async.waterfall([+ async.apply(db.getSortedSetRevRange, set, 0, -1),+ function (nids, next) {+ _nids = nids;- sets = sets.map(function (set) {- return set.mergeId;+ var keys = nids.map(function (nid) {+ return 'notifications:' + nid;});- callback(null, _nids.filter(function (nid, idx) {- return mergeIds.indexOf(sets[idx]) !== -1;- }));+ db.getObjectsFields(keys, ['mergeId'], next);+ },+ ], function (err, sets) {+ if (err) {+ return callback(err);+ }++ sets = sets.map(function (set) {+ return set.mergeId;});- };- Notifications.create = function (data, callback) {- if (!data.nid) {- return callback(new Error('no-notification-id'));+ callback(null, _nids.filter(function (nid, idx) {+ return mergeIds.indexOf(sets[idx]) !== -1;+ }));+ });+ };++ Notifications.create = function (data, callback) {+ if (!data.nid) {+ return callback(new Error('no-notification-id'));+ }+ data.importance = data.importance || 5;+ db.getObject('notifications:' + data.nid, function (err, oldNotification) {+ if (err) {+ return callback(err);}- data.importance = data.importance || 5;- db.getObject('notifications:' + data.nid, function (err, oldNotification) {- if (err) {- return callback(err);- }- if (oldNotification) {- if (parseInt(oldNotification.pid, 10) === parseInt(data.pid, 10) && parseInt(oldNotification.importance, 10) > parseInt(data.importance, 10)) {- return callback(null, null);- }+ if (oldNotification) {+ if (parseInt(oldNotification.pid, 10) === parseInt(data.pid, 10) && parseInt(oldNotification.importance, 10) > parseInt(data.importance, 10)) {+ return callback(null, null);}+ }- var now = Date.now();- data.datetime = now;- async.parallel([- function (next) {- db.sortedSetAdd('notifications', now, data.nid, next);- },- function (next) {- db.setObject('notifications:' + data.nid, data, next);- },- ], function (err) {- callback(err, data);- });+ var now = Date.now();+ data.datetime = now;+ async.parallel([+ function (next) {+ db.sortedSetAdd('notifications', now, data.nid, next);+ },+ function (next) {+ db.setObject('notifications:' + data.nid, data, next);+ },+ ], function (err) {+ callback(err, data);});- };+ });+ };- Notifications.push = function (notification, uids, callback) {- callback = callback || function () {};+ Notifications.push = function (notification, uids, callback) {+ callback = callback || function () {};- if (!notification || !notification.nid) {- return callback();- }+ if (!notification || !notification.nid) {+ return callback();+ }- if (!Array.isArray(uids)) {- uids = [uids];- }+ if (!Array.isArray(uids)) {+ uids = [uids];+ }++ uids = uids.filter(function (uid, index, array) {+ return parseInt(uid, 10) && array.indexOf(uid) === index;+ });++ if (!uids.length) {+ return callback();+ }- uids = uids.filter(function (uid, index, array) {- return parseInt(uid, 10) && array.indexOf(uid) === index;+ setTimeout(function () {+ batch.processArray(uids, function (uids, next) {+ pushToUids(uids, notification, next);+ }, { interval: 1000 }, function (err) {+ if (err) {+ winston.error(err.stack);+ }});+ }, 1000);++ callback();+ };++ function pushToUids(uids, notification, callback) {+ var oneWeekAgo = Date.now() - 604800000;+ var unreadKeys = [];+ var readKeys = [];++ async.waterfall([+ function (next) {+ plugins.fireHook('filter:notification.push', { notification: notification, uids: uids }, next);+ },+ function (data, next) {++ if (!data || !data.notification || !data.uids || !data.uids.length) {++ return callback();++ }+++ uids = data.uids;+ notification = data.notification;++ uids.forEach(function (uid) {+ unreadKeys.push('uid:' + uid + ':notifications:unread');+ readKeys.push('uid:' + uid + ':notifications:read');+ });- if (!uids.length) {- return callback();+ db.sortedSetsAdd(unreadKeys, notification.datetime, notification.nid, next);+ },+ function (next) {+ db.sortedSetsRemove(readKeys, notification.nid, next);+ },+ function (next) {+ db.sortedSetsRemoveRangeByScore(unreadKeys, '-inf', oneWeekAgo, next);+ },+ function (next) {+ db.sortedSetsRemoveRangeByScore(readKeys, '-inf', oneWeekAgo, next);+ },+ function (next) {+ var websockets = require('./socket.io');+ if (websockets.server) {+ uids.forEach(function (uid) {+ websockets.in('uid_' + uid).emit('event:new_notification', notification);+ });+ }++ plugins.fireHook('action:notification.pushed', { notification: notification, uids: uids });+ next();+ },+ ], callback);+ }++ Notifications.pushGroup = function (notification, groupName, callback) {+ callback = callback || function () {};+ groups.getMembers(groupName, 0, -1, function (err, members) {+ if (err || !Array.isArray(members) || !members.length) {+ return callback(err);}- setTimeout(function () {- batch.processArray(uids, function (uids, next) {- pushToUids(uids, notification, next);- }, { interval: 1000 }, function (err) {- if (err) {- winston.error(err.stack);- }- });- }, 1000);+ Notifications.push(notification, members, callback);+ });+ };- callback();- };+ Notifications.pushGroups = function (notification, groupNames, callback) {+ callback = callback || function () {};+ groups.getMembersOfGroups(groupNames, function (err, groupMembers) {+ if (err) {+ return callback(err);+ }- function pushToUids(uids, notification, callback) {- var oneWeekAgo = Date.now() - 604800000;- var unreadKeys = [];- var readKeys = [];+ var members = _.unique(_.flatten(groupMembers));-+ Notifications.push(notification, members, callback);+ });+ };++ Notifications.rescind = function (nid, callback) {+ callback = callback || function () {};++ async.parallel([+ async.apply(db.sortedSetRemove, 'notifications', nid),+ async.apply(db.delete, 'notifications:' + nid),+ ], function (err) {+ if (err) {+ winston.error('Encountered error rescinding notification (' + nid + '): ' + err.message);+ } else {+ winston.verbose('[notifications/rescind] Rescinded notification "' + nid + '"');+ }- async.waterfall([- function (next) {- plugins.fireHook('filter:notification.push', { notification: notification, uids: uids }, next);- },- function (data, next) {- if (!data || !data.notification || !data.uids || !data.uids.length) {- return callback();- }+ callback(err, nid);+ });+ };- uids = data.uids;- notification = data.notification;+ Notifications.markRead = function (nid, uid, callback) {+ callback = callback || function () {};+ if (!parseInt(uid, 10) || !nid) {+ return callback();+ }+ Notifications.markReadMultiple([nid], uid, callback);+ };- uids.forEach(function (uid) {- unreadKeys.push('uid:' + uid + ':notifications:unread');- readKeys.push('uid:' + uid + ':notifications:read');- });+ Notifications.markUnread = function (nid, uid, callback) {+ callback = callback || function () {};+ if (!parseInt(uid, 10) || !nid) {+ return callback();+ }- db.sortedSetsAdd(unreadKeys, notification.datetime, notification.nid, next);- },- function (next) {- db.sortedSetsRemove(readKeys, notification.nid, next);- },- function (next) {- db.sortedSetsRemoveRangeByScore(unreadKeys, '-inf', oneWeekAgo, next);- },- function (next) {- db.sortedSetsRemoveRangeByScore(readKeys, '-inf', oneWeekAgo, next);- },- function (next) {- var websockets = require('./socket.io');- if (websockets.server) {- uids.forEach(function (uid) {- websockets.in('uid_' + uid).emit('event:new_notification', notification);- });- }+ db.getObject('notifications:' + nid, function (err, notification) {+ if (err || !notification) {+ return callback(err || new Error('[[error:no-notification]]'));+ }+ notification.datetime = notification.datetime || Date.now();- plugins.fireHook('action:notification.pushed', { notification: notification, uids: uids });- next();- },+ async.parallel([+ async.apply(db.sortedSetRemove, 'uid:' + uid + ':notifications:read', nid),+ async.apply(db.sortedSetAdd, 'uid:' + uid + ':notifications:unread', notification.datetime, nid),], callback);+ });+ };++ Notifications.markReadMultiple = function (nids, uid, callback) {+ callback = callback || function () {};+ nids = nids.filter(Boolean);+ if (!Array.isArray(nids) || !nids.length) {+ return callback();}- Notifications.pushGroup = function (notification, groupName, callback) {- callback = callback || function () {};- groups.getMembers(groupName, 0, -1, function (err, members) {- if (err || !Array.isArray(members) || !members.length) {- return callback(err);- }+ var notificationKeys = nids.map(function (nid) {+ return 'notifications:' + nid;+ });- Notifications.push(notification, members, callback);- });- };+ async.waterfall([+ async.apply(db.getObjectsFields, notificationKeys, ['mergeId']),+ function (mergeIds, next) {+ // Isolate mergeIds and find related notifications+ mergeIds = mergeIds.map(function (set) {+ return set.mergeId;+ }).reduce(function (memo, mergeId, idx, arr) {+ if (mergeId && idx === arr.indexOf(mergeId)) {+ memo.push(mergeId);+ }+ return memo;+ }, []);- Notifications.pushGroups = function (notification, groupNames, callback) {- callback = callback || function () {};- groups.getMembersOfGroups(groupNames, function (err, groupMembers) {- if (err) {- return callback(err);- }+ Notifications.findRelated(mergeIds, 'uid:' + uid + ':notifications:unread', next);+ },+ function (relatedNids, next) {+ notificationKeys = _.union(nids, relatedNids).map(function (nid) {+ return 'notifications:' + nid;+ });- var members = _.unique(_.flatten(groupMembers));+ db.getObjectsFields(notificationKeys, ['nid', 'datetime'], next);+ },+ ], function (err, notificationData) {+ if (err) {+ return callback(err);+ }- Notifications.push(notification, members, callback);+ // Filter out notifications that didn't exist+ notificationData = notificationData.filter(function (notification) {+ return notification && notification.nid;});- };- Notifications.rescind = function (nid, callback) {- callback = callback || function () {};+ // Extract nid+ nids = notificationData.map(function (notification) {+ return notification.nid;+ });++ var datetimes = notificationData.map(function (notification) {+ return (notification && notification.datetime) || Date.now();+ });async.parallel([- async.apply(db.sortedSetRemove, 'notifications', nid),- async.apply(db.delete, 'notifications:' + nid),+ function (next) {+ db.sortedSetRemove('uid:' + uid + ':notifications:unread', nids, next);+ },+ function (next) {+ db.sortedSetAdd('uid:' + uid + ':notifications:read', datetimes, nids, next);+ },], function (err) {- if (err) {- winston.error('Encountered error rescinding notification (' + nid + '): ' + err.message);- } else {- winston.verbose('[notifications/rescind] Rescinded notification "' + nid + '"');- }-- callback(err, nid);+ callback(err);});- };+ });+ };- Notifications.markRead = function (nid, uid, callback) {- callback = callback || function () {};- if (!parseInt(uid, 10) || !nid) {- return callback();+ Notifications.markAllRead = function (uid, callback) {+ db.getSortedSetRevRange('uid:' + uid + ':notifications:unread', 0, 99, function (err, nids) {+ if (err) {+ return callback(err);}- Notifications.markReadMultiple([nid], uid, callback);- };- Notifications.markUnread = function (nid, uid, callback) {- callback = callback || function () {};- if (!parseInt(uid, 10) || !nid) {+ if (!Array.isArray(nids) || !nids.length) {return callback();}