mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-02 12:05:57 +01:00
#1375 part1
This commit is contained in:
@@ -49,7 +49,7 @@ define('notifications', ['sounds'], function(sound) {
|
|||||||
|
|
||||||
updateNotifCount(data.unread.length);
|
updateNotifCount(data.unread.length);
|
||||||
|
|
||||||
socket.emit('modules.notifications.mark_all_read', null, function(err) {
|
socket.emit('modules.notifications.markAllRead', null, function(err) {
|
||||||
if (!err) {
|
if (!err) {
|
||||||
updateNotifCount(0);
|
updateNotifCount(0);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,6 +41,9 @@ module.exports = function(redisClient, module) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
module.getObjectsFields = function(keys, fields, callback) {
|
module.getObjectsFields = function(keys, fields, callback) {
|
||||||
|
if (!Array.isArray(fields) || !fields.length) {
|
||||||
|
return callback(null, keys.map(function() { return {}; }));
|
||||||
|
}
|
||||||
var multi = redisClient.multi();
|
var multi = redisClient.multi();
|
||||||
|
|
||||||
for(var x=0; x<keys.length; ++x) {
|
for(var x=0; x<keys.length; ++x) {
|
||||||
|
|||||||
@@ -100,7 +100,7 @@ var db = require('./database'),
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Mark any chat notifications pertaining to this chat as read
|
// Mark any chat notifications pertaining to this chat as read
|
||||||
notifications.mark_read_by_uniqueid(fromuid, 'chat_' + touid + '_' + fromuid, function(err) {
|
notifications.markReadByUniqueId(fromuid, 'chat_' + touid + '_' + fromuid, function(err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
winston.error('[messaging] Could not mark notifications related to this chat as read: ' + err.message);
|
winston.error('[messaging] Could not mark notifications related to this chat as read: ' + err.message);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,17 +23,21 @@ var async = require('async'),
|
|||||||
new cron('0 0 * * *', Notifications.prune, null, true);
|
new cron('0 0 * * *', Notifications.prune, null, true);
|
||||||
};
|
};
|
||||||
|
|
||||||
Notifications.get = function(nid, uid, callback) {
|
Notifications.get = function(nid, callback) {
|
||||||
db.exists('notifications:' + nid, function(err, exists) {
|
db.exists('notifications:' + nid, function(err, exists) {
|
||||||
if (err) {
|
if (err) {
|
||||||
winston.error('[notifications.get] Could not retrieve nid ' + nid + ': ' + err.message);
|
winston.error('[notifications.get] Could not retrieve nid ' + nid + ': ' + err.message);
|
||||||
return callback(null);
|
return callback(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!exists) {
|
||||||
|
return callback(null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (exists) {
|
|
||||||
db.sortedSetRank('uid:' + uid + ':notifications:read', nid, function(err, rank) {
|
|
||||||
db.getObject('notifications:' + nid, function(err, notification) {
|
db.getObject('notifications:' + nid, function(err, notification) {
|
||||||
notification.read = rank !== null ? true:false;
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
|
||||||
// Backwards compatibility for old notification schema
|
// Backwards compatibility for old notification schema
|
||||||
// Remove this block when NodeBB v0.6.0 is released.
|
// Remove this block when NodeBB v0.6.0 is released.
|
||||||
@@ -48,9 +52,13 @@ var async = require('async'),
|
|||||||
|
|
||||||
if (notification.from && !notification.image) {
|
if (notification.from && !notification.image) {
|
||||||
User.getUserField(notification.from, 'picture', function(err, picture) {
|
User.getUserField(notification.from, 'picture', function(err, picture) {
|
||||||
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
notification.image = picture;
|
notification.image = picture;
|
||||||
callback(notification);
|
callback(null, notification);
|
||||||
});
|
});
|
||||||
|
return;
|
||||||
} else if (notification.image) {
|
} else if (notification.image) {
|
||||||
switch(notification.image) {
|
switch(notification.image) {
|
||||||
case 'brand:logo':
|
case 'brand:logo':
|
||||||
@@ -58,40 +66,15 @@ var async = require('async'),
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
callback(notification);
|
return callback(null, notification);
|
||||||
} else {
|
|
||||||
callback(notification);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
// Remove from the user's boxes
|
|
||||||
if (process.env.NODE_ENV === 'development') {
|
|
||||||
winston.info('[notifications.get] nid ' + nid + ' not found. Removing.');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async.parallel([
|
callback(null, notification);
|
||||||
function(next) {
|
|
||||||
db.sortedSetRemove('uid:' + uid + ':notifications:unread', nid, next);
|
|
||||||
},
|
|
||||||
function(next) {
|
|
||||||
db.sortedSetRemove('uid:' + uid + ':notifications:read', nid, next);
|
|
||||||
}
|
|
||||||
], function(err) {
|
|
||||||
callback(null);
|
|
||||||
});
|
});
|
||||||
}
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
Notifications.create = function(data, callback) {
|
Notifications.create = function(data, callback) {
|
||||||
/**
|
|
||||||
*data.uniqueId is used solely to override stale nids.
|
|
||||||
* If a new nid is pushed to a user and an existing nid in the user's
|
|
||||||
* (un)read list contains the same uniqueId, it will be removed, and
|
|
||||||
* the new one put in its place.
|
|
||||||
*/
|
|
||||||
|
|
||||||
// Add default values to data Object if not already set
|
// Add default values to data Object if not already set
|
||||||
var defaults = {
|
var defaults = {
|
||||||
bodyShort: '',
|
bodyShort: '',
|
||||||
@@ -117,36 +100,47 @@ var async = require('async'),
|
|||||||
}
|
}
|
||||||
|
|
||||||
db.incrObjectField('global', 'nextNid', function(err, nid) {
|
db.incrObjectField('global', 'nextNid', function(err, nid) {
|
||||||
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
|
||||||
data.nid = nid;
|
data.nid = nid;
|
||||||
db.setAdd('notifications', nid);
|
db.setAdd('notifications', nid);
|
||||||
db.setObject('notifications:' + nid, data, function(err, status) {
|
db.setObject('notifications:' + nid, data, function(err) {
|
||||||
if (!err) {
|
callback(err, nid);
|
||||||
callback(nid);
|
|
||||||
} else {
|
|
||||||
winston.error('[notifications.create] ' + err.message);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
Notifications.push = function(nid, uids, callback) {
|
Notifications.push = function(nid, uids, callback) {
|
||||||
|
callback = callback || function() {};
|
||||||
var websockets = require('./socket.io');
|
var websockets = require('./socket.io');
|
||||||
if (!Array.isArray(uids)) {
|
if (!Array.isArray(uids)) {
|
||||||
uids = [uids];
|
uids = [uids];
|
||||||
}
|
}
|
||||||
|
|
||||||
var numUids = uids.length,
|
Notifications.get(nid, function(err, notif_data) {
|
||||||
x;
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
|
||||||
Notifications.get(nid, null, function(notif_data) {
|
|
||||||
async.each(uids, function(uid, next) {
|
async.each(uids, function(uid, next) {
|
||||||
if (!parseInt(uid, 10)) {
|
if (!parseInt(uid, 10)) {
|
||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
|
|
||||||
checkReplace(notif_data.uniqueId, uid, notif_data, function(err, replace) {
|
shouldPush(uid, notif_data, function(err, shouldPush) {
|
||||||
if (replace) {
|
if (err || !shouldPush) {
|
||||||
db.sortedSetAdd('uid:' + uid + ':notifications:unread', notif_data.datetime, nid);
|
return callback(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
async.parallel([
|
||||||
|
async.apply(db.sortedSetAdd, 'uid:' + uid + ':notifications:unread', notif_data.datetime, notif_data.uniqueId),
|
||||||
|
async.apply(db.sortedSetRemove, 'uid:' + uid + ':notifications:read', notif_data.uniqueId)
|
||||||
|
], function(err) {
|
||||||
|
if (err) {
|
||||||
|
return next(err);
|
||||||
|
}
|
||||||
|
|
||||||
// Client-side
|
// Client-side
|
||||||
websockets.in('uid_' + uid).emit('event:new_notification', notif_data);
|
websockets.in('uid_' + uid).emit('event:new_notification', notif_data);
|
||||||
@@ -154,18 +148,57 @@ var async = require('async'),
|
|||||||
// Plugins
|
// Plugins
|
||||||
notif_data.uid = uid;
|
notif_data.uid = uid;
|
||||||
plugins.fireHook('action:notification.pushed', notif_data);
|
plugins.fireHook('action:notification.pushed', notif_data);
|
||||||
}
|
|
||||||
next();
|
|
||||||
});
|
|
||||||
|
|
||||||
}, function(err) {
|
db.setObjectField('uid:' + uid + ':notifications:uniqueId:nid', notif_data.uniqueId, nid, next);
|
||||||
if (callback) {
|
|
||||||
callback(null, true);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
}, callback);
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function shouldPush(uid, newNotifObj, callback) {
|
||||||
|
hasNotification(newNotifObj.uniqueId, uid, function(err, hasNotification) {
|
||||||
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!hasNotification) {
|
||||||
|
return callback(null, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
db.getObjectField('uid:' + uid + ':notifications:uniqueId:nid', newNotifObj.uniqueId, function(err, nid) {
|
||||||
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
db.getObjectFields('notifications:' + nid, ['nid', 'uniqueId', 'importance'], function(err, oldNotifObj) {
|
||||||
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!oldNotifObj || newNotifObj.uniqueId !== oldNotifObj.uniqueId) {
|
||||||
|
return callback(null, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
callback(null, parseInt(newNotifObj.importance, 10) >= parseInt(oldNotifObj.importance, 10));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function hasNotification(uniqueId, uid, callback) {
|
||||||
|
async.parallel([
|
||||||
|
async.apply(db.isSortedSetMember, 'uid:' + uid + ':notifications:unread', uniqueId),
|
||||||
|
async.apply(db.isSortedSetMember, 'uid:' + uid + ':notifications:read', uniqueId)
|
||||||
|
], function(err, results) {
|
||||||
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
callback(null, results[0] || results[1]);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
Notifications.pushGroup = function(nid, groupName, callback) {
|
Notifications.pushGroup = function(nid, groupName, callback) {
|
||||||
if (!callback) {
|
if (!callback) {
|
||||||
callback = function() {};
|
callback = function() {};
|
||||||
@@ -182,129 +215,55 @@ var async = require('async'),
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
function checkReplace(uniqueId, uid, newNotifObj, callback) {
|
|
||||||
var replace = false, matched = false;
|
|
||||||
|
|
||||||
function checkAndRemove(set, next) {
|
Notifications.markRead = function(nid, uid, callback) {
|
||||||
db.getSortedSetRange(set, 0, -1, function(err, nids) {
|
|
||||||
if (err || !nids || !nids.length) {
|
|
||||||
return next(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
var keys = nids.map(function(nid) {
|
|
||||||
return 'notifications:' + nid;
|
|
||||||
});
|
|
||||||
|
|
||||||
db.getObjectsFields(keys, ['nid', 'uniqueId', 'importance'], function(err, nid_infos) {
|
|
||||||
if (err) {
|
|
||||||
return next(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
nid_infos.forEach(function(nid_info) {
|
|
||||||
if (nid_info && nid_info.uniqueId === uniqueId) {
|
|
||||||
matched = true;
|
|
||||||
if ((nid_info.importance || 5) >= newNotifObj.importance) {
|
|
||||||
replace = true;
|
|
||||||
db.sortedSetRemove(set, nid_info.nid);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
next();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async.parallel([
|
|
||||||
function(next) {
|
|
||||||
checkAndRemove('uid:' + uid + ':notifications:unread', next);
|
|
||||||
},
|
|
||||||
function(next) {
|
|
||||||
checkAndRemove('uid:' + uid + ':notifications:read', next);
|
|
||||||
}
|
|
||||||
], function(err) {
|
|
||||||
if (!err) {
|
|
||||||
if (replace === false && matched === false) {
|
|
||||||
replace = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
callback(null, replace);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
Notifications.mark_read = function(nid, uid, callback) {
|
|
||||||
callback = callback || function() {};
|
callback = callback || function() {};
|
||||||
if (!parseInt(uid, 10)) {
|
if (!parseInt(uid, 10)) {
|
||||||
return callback();
|
return callback();
|
||||||
}
|
}
|
||||||
|
|
||||||
Notifications.get(nid, uid, function(notif_data) {
|
Notifications.get(nid, function(err, notificationData) {
|
||||||
|
if (err || !notificationData) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
|
||||||
async.parallel([
|
async.parallel([
|
||||||
function(next) {
|
async.apply(db.sortedSetRemove, 'uid:' + uid + ':notifications:unread', notificationData.uniqueId),
|
||||||
db.sortedSetRemove('uid:' + uid + ':notifications:unread', nid, next);
|
async.apply(db.sortedSetAdd, 'uid:' + uid + ':notifications:read', notificationData.datetime, notificationData.uniqueId)
|
||||||
},
|
|
||||||
function(next) {
|
|
||||||
if (!notif_data) {
|
|
||||||
return next();
|
|
||||||
}
|
|
||||||
db.sortedSetAdd('uid:' + uid + ':notifications:read', notif_data.datetime, nid, next);
|
|
||||||
}
|
|
||||||
], callback);
|
], callback);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
Notifications.mark_read_multiple = function(nids, uid, callback) {
|
Notifications.markReadMultiple = function(nids, uid, callback) {
|
||||||
|
callback = callback || function() {};
|
||||||
if (!Array.isArray(nids) && parseInt(nids, 10) > 0) {
|
if (!Array.isArray(nids) && parseInt(nids, 10) > 0) {
|
||||||
nids = [nids];
|
nids = [nids];
|
||||||
}
|
}
|
||||||
|
|
||||||
async.each(nids, function(nid, next) {
|
async.each(nids, function(nid, next) {
|
||||||
Notifications.mark_read(nid, uid, function(err) {
|
Notifications.markRead(nid, uid, next);
|
||||||
if (!err) {
|
}, callback);
|
||||||
next(null);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}, function(err) {
|
|
||||||
if (callback) {
|
|
||||||
callback(err);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Notifications.mark_all_read = function(uid, callback) {
|
Notifications.markAllRead = function(uid, callback) {
|
||||||
db.getSortedSetRange('uid:' + uid + ':notifications:unread', 0, 10, function(err, nids) {
|
db.getObjectValues('uid:' + uid + ':notifications:uniqueId:nid', function(err, nids) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return callback(err);
|
return callback(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (nids.length > 0) {
|
if (!Array.isArray(nids) || !nids.length) {
|
||||||
Notifications.mark_read_multiple(nids, uid, function(err) {
|
return callback(err);
|
||||||
callback(err);
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
callback();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Notifications.markReadMultiple(nids, uid, callback);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
// why_are_we_using_underscores_here_?
|
Notifications.markReadByUniqueId = function(uid, uniqueId, callback) {
|
||||||
// maybe_camel_case_ALL_THE_THINGS
|
|
||||||
Notifications.mark_read_by_uniqueid = function(uid, uniqueId, callback) {
|
|
||||||
async.waterfall([
|
async.waterfall([
|
||||||
async.apply(db.getSortedSetRange, 'uid:' + uid + ':notifications:unread', 0, 10),
|
async.apply(db.getObjectField, 'uid:' + uid + ':notifications:uniqueId:nid', uniqueId),
|
||||||
function(nids, next) {
|
function(nid, next) {
|
||||||
async.filter(nids, function(nid, next) {
|
Notifications.markRead(nid, uid, next);
|
||||||
db.getObjectField('notifications:' + nid, 'uniqueId', function(err, value) {
|
|
||||||
next(uniqueId === value);
|
|
||||||
});
|
|
||||||
}, function(nids) {
|
|
||||||
next(null, nids);
|
|
||||||
});
|
|
||||||
},
|
|
||||||
function(nids, next) {
|
|
||||||
Notifications.mark_read_multiple(nids, uid, next);
|
|
||||||
}
|
}
|
||||||
], callback);
|
], callback);
|
||||||
};
|
};
|
||||||
@@ -326,13 +285,13 @@ var async = require('async'),
|
|||||||
var cutoffTime = cutoff.getTime();
|
var cutoffTime = cutoff.getTime();
|
||||||
|
|
||||||
db.getSetMembers('notifications', function(err, nids) {
|
db.getSetMembers('notifications', function(err, nids) {
|
||||||
|
if (err) {
|
||||||
|
return winston.error(err.message);
|
||||||
|
}
|
||||||
|
|
||||||
async.filter(nids, function(nid, next) {
|
async.filter(nids, function(nid, next) {
|
||||||
db.getObjectField('notifications:' + nid, 'datetime', function(err, datetime) {
|
db.getObjectField('notifications:' + nid, 'datetime', function(err, datetime) {
|
||||||
if (parseInt(datetime, 10) < cutoffTime) {
|
next(!err && parseInt(datetime, 10) < cutoffTime);
|
||||||
next(true);
|
|
||||||
} else {
|
|
||||||
next(false);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}, function(expiredNids) {
|
}, function(expiredNids) {
|
||||||
async.each(expiredNids, function(nid, next) {
|
async.each(expiredNids, function(nid, next) {
|
||||||
|
|||||||
@@ -56,6 +56,38 @@ module.exports = function(app, middleware, controllers) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
router.get('/test', function(req, res) {
|
router.get('/test', function(req, res) {
|
||||||
res.redirect('404');
|
//res.redirect('404');
|
||||||
|
var notifications = require('../notifications');
|
||||||
|
var nconf = require('nconf');
|
||||||
|
|
||||||
|
var username = 'julian';
|
||||||
|
var topicTitle = 'testing tags';
|
||||||
|
var topicSlug = '1748/testing-tags';
|
||||||
|
var postIndex = 1;
|
||||||
|
var tid = 1748;
|
||||||
|
var fromUid = 2;
|
||||||
|
|
||||||
|
notifications.create({
|
||||||
|
bodyShort: '[[notifications:user_posted_to, ' + username + ', ' + topicTitle + ']]',
|
||||||
|
bodyLong: 'asdasd khajsdhakhdakj hdkash dakhdakjdhakjs',
|
||||||
|
path: nconf.get('relative_path') + '/topic/' + topicSlug + '/' + postIndex,
|
||||||
|
uniqueId: 'topic:' + tid,
|
||||||
|
from: fromUid
|
||||||
|
}, function(err, nid) {
|
||||||
|
notifications.push(nid, [1]);
|
||||||
|
res.json('done');
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
router.get('/dailyunread', function(req, res) {
|
||||||
|
//var userNotifs = require('./user');
|
||||||
|
user.notifications.getDailyUnread(1, function(err, data) {
|
||||||
|
if (err) {
|
||||||
|
res.json(500, err.message);
|
||||||
|
}
|
||||||
|
|
||||||
|
res.json(data);
|
||||||
|
|
||||||
|
});
|
||||||
|
})
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -231,8 +231,10 @@ function sendChatNotification(fromuid, touid, messageObj) {
|
|||||||
path: nconf.get('relative_path') + '/chats/' + utils.slugify(messageObj.fromUser.username),
|
path: nconf.get('relative_path') + '/chats/' + utils.slugify(messageObj.fromUser.username),
|
||||||
uniqueId: 'chat_' + fromuid + '_' + touid,
|
uniqueId: 'chat_' + fromuid + '_' + touid,
|
||||||
from: fromuid
|
from: fromuid
|
||||||
}, function(nid) {
|
}, function(err, nid) {
|
||||||
|
if (!err) {
|
||||||
notifications.push(nid, [touid]);
|
notifications.push(nid, [touid]);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -278,12 +280,12 @@ SocketModules.chats.list = function(socket, data, callback) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/* Notifications */
|
/* Notifications */
|
||||||
SocketModules.notifications.mark_read = function(socket, nid) {
|
SocketModules.notifications.markRead = function(socket, nid) {
|
||||||
notifications.mark_read(nid, socket.uid);
|
notifications.markRead(nid, socket.uid);
|
||||||
};
|
};
|
||||||
|
|
||||||
SocketModules.notifications.mark_all_read = function(socket, data, callback) {
|
SocketModules.notifications.markAllRead = function(socket, data, callback) {
|
||||||
notifications.mark_all_read(socket.uid, callback);
|
notifications.markAllRead(socket.uid, callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Sounds */
|
/* Sounds */
|
||||||
|
|||||||
@@ -124,8 +124,10 @@ function sendNotificationToPostOwner(data, uid, notification) {
|
|||||||
path: nconf.get('relative_path') + '/topic/' + results.slug + '/' + results.index,
|
path: nconf.get('relative_path') + '/topic/' + results.slug + '/' + results.index,
|
||||||
uniqueId: 'post:' + data.pid,
|
uniqueId: 'post:' + data.pid,
|
||||||
from: uid
|
from: uid
|
||||||
}, function(nid) {
|
}, function(err, nid) {
|
||||||
|
if (!err) {
|
||||||
notifications.push(nid, [postData.uid]);
|
notifications.push(nid, [postData.uid]);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -327,10 +329,11 @@ SocketPosts.flag = function(socket, pid, callback) {
|
|||||||
path: path,
|
path: path,
|
||||||
uniqueId: 'post_flag:' + pid,
|
uniqueId: 'post_flag:' + pid,
|
||||||
from: socket.uid
|
from: socket.uid
|
||||||
}, function(nid) {
|
}, function(err, nid) {
|
||||||
notifications.push(nid, adminGroup.members, function() {
|
if (err) {
|
||||||
next();
|
return next(err);
|
||||||
});
|
}
|
||||||
|
notifications.push(nid, adminGroup.members, next);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
function(next) {
|
function(next) {
|
||||||
|
|||||||
@@ -47,9 +47,7 @@ module.exports = function(Topics) {
|
|||||||
path: nconf.get('relative_path') + '/topic/' + results.topicData.slug + '/' + results.postIndex,
|
path: nconf.get('relative_path') + '/topic/' + results.topicData.slug + '/' + results.postIndex,
|
||||||
uniqueId: 'topic:' + tid,
|
uniqueId: 'topic:' + tid,
|
||||||
from: exceptUid
|
from: exceptUid
|
||||||
}, function(nid) {
|
}, next);
|
||||||
next(null, nid);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
followers: function(next) {
|
followers: function(next) {
|
||||||
|
|||||||
@@ -181,8 +181,8 @@ module.exports = function(Topics) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Topics.markTopicNotificationsRead = function(tid, uid) {
|
Topics.markTopicNotificationsRead = function(tid, uid) {
|
||||||
user.notifications.getUnreadByUniqueId(uid, 'topic:' + tid, function(err, nids) {
|
user.notifications.getUnreadByField(uid, 'tid', tid, function(err, nids) {
|
||||||
notifications.mark_read_multiple(nids, uid, function() {
|
notifications.markReadMultiple(nids, uid, function() {
|
||||||
user.notifications.pushCount(uid);
|
user.notifications.pushCount(uid);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -163,8 +163,10 @@ module.exports = function(User) {
|
|||||||
bodyLong: '',
|
bodyLong: '',
|
||||||
image: 'brand:logo',
|
image: 'brand:logo',
|
||||||
datetime: Date.now()
|
datetime: Date.now()
|
||||||
}, function(nid) {
|
}, function(err, nid) {
|
||||||
|
if (!err) {
|
||||||
notifications.push(nid, uid);
|
notifications.push(nid, uid);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -68,6 +68,9 @@ module.exports = function(User) {
|
|||||||
function(next) {
|
function(next) {
|
||||||
db.delete('uid:' + uid + ':notifications:unread', next);
|
db.delete('uid:' + uid + ':notifications:unread', next);
|
||||||
},
|
},
|
||||||
|
function(next) {
|
||||||
|
db.delete('uid:' + uid + ':notifications:uniqueId:nid', next);
|
||||||
|
},
|
||||||
function(next) {
|
function(next) {
|
||||||
db.sortedSetRemove('users:joindate', uid, next);
|
db.sortedSetRemove('users:joindate', uid, next);
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -15,31 +15,58 @@ var async = require('async'),
|
|||||||
privileges = require('../privileges');
|
privileges = require('../privileges');
|
||||||
|
|
||||||
(function(UserNotifications) {
|
(function(UserNotifications) {
|
||||||
|
|
||||||
UserNotifications.get = function(uid, callback) {
|
UserNotifications.get = function(uid, callback) {
|
||||||
|
|
||||||
|
|
||||||
function getNotifications(set, start, stop, iterator, done) {
|
function getNotifications(set, start, stop, iterator, done) {
|
||||||
db.getSortedSetRevRange(set, start, stop, function(err, nids) {
|
db.getSortedSetRevRange(set, start, stop, function(err, uniqueIds) {
|
||||||
if(err) {
|
if(err) {
|
||||||
return done(err);
|
return done(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!nids || nids.length === 0) {
|
if(!Array.isArray(uniqueIds) || !uniqueIds.length) {
|
||||||
return done(null, []);
|
return done(null, []);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (nids.length > maxNotifs) {
|
if (uniqueIds.length > maxNotifs) {
|
||||||
nids.length = maxNotifs;
|
uniqueIds.length = maxNotifs;
|
||||||
}
|
}
|
||||||
|
|
||||||
async.map(nids, function(nid, next) {
|
db.getObjectFields('uid:' + uid + ':notifications:uniqueId:nid', uniqueIds, function(err, uniqueIdToNids) {
|
||||||
notifications.get(nid, uid, function(notif_data) {
|
if (err) {
|
||||||
if(typeof iterator === 'function') {
|
return done(err);
|
||||||
iterator(notif_data);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var nidsToUniqueIds = {};
|
||||||
|
Object.keys(uniqueIdToNids).forEach(function(uniqueId) {
|
||||||
|
nidsToUniqueIds[uniqueIdToNids[uniqueId]] = uniqueId;
|
||||||
|
});
|
||||||
|
|
||||||
|
async.map(Object.keys(nidsToUniqueIds), function(nid, next) {
|
||||||
|
notifications.get(nid, function(err, notif_data) {
|
||||||
|
if (err) {
|
||||||
|
return next(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!notif_data) {
|
||||||
|
if (process.env.NODE_ENV === 'development') {
|
||||||
|
winston.info('[notifications.get] nid ' + nid + ' not found. Removing.');
|
||||||
|
}
|
||||||
|
|
||||||
|
db.sortedSetRemove(set, nidsToUniqueIds[nid]);
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof iterator === 'function') {
|
||||||
|
iterator(notif_data, next);
|
||||||
|
} else {
|
||||||
next(null, notif_data);
|
next(null, notif_data);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}, done);
|
}, done);
|
||||||
});
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
var maxNotifs = 15;
|
var maxNotifs = 15;
|
||||||
@@ -47,13 +74,16 @@ var async = require('async'),
|
|||||||
async.parallel({
|
async.parallel({
|
||||||
unread: function(next) {
|
unread: function(next) {
|
||||||
getNotifications('uid:' + uid + ':notifications:unread', 0, 9, function(notif_data) {
|
getNotifications('uid:' + uid + ':notifications:unread', 0, 9, function(notif_data) {
|
||||||
if (notif_data) {
|
notif_data.read = false;
|
||||||
notif_data.readClass = !notif_data.read ? 'label-warning' : '';
|
notif_data.readClass = !notif_data.read ? 'label-warning' : '';
|
||||||
}
|
next(null, notif_data);
|
||||||
}, next);
|
}, next);
|
||||||
},
|
},
|
||||||
read: function(next) {
|
read: function(next) {
|
||||||
getNotifications('uid:' + uid + ':notifications:read', 0, 9, null, next);
|
getNotifications('uid:' + uid + ':notifications:read', 0, 9, function(notif_data, next) {
|
||||||
|
notif_data.read = true;
|
||||||
|
next(null, notif_data);
|
||||||
|
}, next);
|
||||||
}
|
}
|
||||||
}, function(err, notifications) {
|
}, function(err, notifications) {
|
||||||
function filterDeleted(notifObj) {
|
function filterDeleted(notifObj) {
|
||||||
@@ -85,15 +115,30 @@ var async = require('async'),
|
|||||||
before = new Date(parseInt(before, 10));
|
before = new Date(parseInt(before, 10));
|
||||||
}
|
}
|
||||||
|
|
||||||
db.getSortedSetRevRangeByScore('uid:' + uid + ':notifications:read', 0, limit, before ? before.getTime(): now.getTime(), -Infinity, function(err, results1) {
|
db.getObjectValues('uid:' + uid + ':notifications:uniqueId:nid', function(err, nids) {
|
||||||
db.getSortedSetRevRangeByScore('uid:' + uid + ':notifications:unread', 0, limit, before ? before.getTime(): now.getTime(), -Infinity, function(err, results2) {
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
|
||||||
var nids = results1.concat(results2);
|
|
||||||
async.map(nids, function(nid, next) {
|
async.map(nids, function(nid, next) {
|
||||||
notifications.get(nid, uid, function(notif_data) {
|
notifications.get(nid, function(err, notif_data) {
|
||||||
|
if (err || !notif_data) {
|
||||||
|
return next(err);
|
||||||
|
}
|
||||||
|
UserNotifications.isNotificationRead(notif_data.nid, uid, function(err, isRead) {
|
||||||
|
if (err) {
|
||||||
|
return next(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
notif_data.read = isRead;
|
||||||
next(null, notif_data);
|
next(null, notif_data);
|
||||||
});
|
});
|
||||||
|
});
|
||||||
}, function(err, notifs) {
|
}, function(err, notifs) {
|
||||||
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
|
||||||
notifs = notifs.filter(function(notif) {
|
notifs = notifs.filter(function(notif) {
|
||||||
return notif !== null;
|
return notif !== null;
|
||||||
}).sort(function(a, b) {
|
}).sort(function(a, b) {
|
||||||
@@ -101,53 +146,86 @@ var async = require('async'),
|
|||||||
}).map(function(notif) {
|
}).map(function(notif) {
|
||||||
notif.datetimeISO = utils.toISOString(notif.datetime);
|
notif.datetimeISO = utils.toISOString(notif.datetime);
|
||||||
notif.readClass = !notif.read ? 'label-warning' : '';
|
notif.readClass = !notif.read ? 'label-warning' : '';
|
||||||
|
|
||||||
return notif;
|
return notif;
|
||||||
});
|
});
|
||||||
|
|
||||||
callback(err, notifs);
|
callback(null, notifs);
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
UserNotifications.isNotificationRead = function(nid, uid, callback) {
|
||||||
|
db.isSortedSetMember('uid:' + uid + ':notifications:read', nid, callback);
|
||||||
|
};
|
||||||
|
|
||||||
UserNotifications.getDailyUnread = function(uid, callback) {
|
UserNotifications.getDailyUnread = function(uid, callback) {
|
||||||
var now = Date.now(),
|
var now = Date.now(),
|
||||||
yesterday = now - (1000*60*60*24); // Approximate, can be more or less depending on time changes, makes no difference really.
|
yesterday = now - (1000*60*60*24); // Approximate, can be more or less depending on time changes, makes no difference really.
|
||||||
db.getSortedSetRangeByScore('uid:' + uid + ':notifications:unread', 0, 20, yesterday, now, function(err, nids) {
|
|
||||||
async.map(nids, function(nid, next) {
|
db.getSortedSetRangeByScore('uid:' + uid + ':notifications:unread', 0, 20, yesterday, now, function(err, uniqueIds) {
|
||||||
notifications.get(nid, uid, function(notif_data) {
|
if (err) {
|
||||||
next(null, notif_data);
|
return callback(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!Array.isArray(uniqueIds) || !uniqueIds.length) {
|
||||||
|
return callback(null, []);
|
||||||
|
}
|
||||||
|
|
||||||
|
db.getObjectFields('uid:' + uid + ':notifications:uniqueId:nid', uniqueIds, function(err, uniqueIdToNids) {
|
||||||
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
var nids = Object.keys(uniqueIdToNids).map(function(uniqueId) {
|
||||||
|
return uniqueIdToNids[uniqueId];
|
||||||
});
|
});
|
||||||
|
|
||||||
|
async.map(nids, function(nid, next) {
|
||||||
|
notifications.get(nid, next);
|
||||||
}, callback);
|
}, callback);
|
||||||
});
|
});
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
UserNotifications.getUnreadCount = function(uid, callback) {
|
UserNotifications.getUnreadCount = function(uid, callback) {
|
||||||
db.sortedSetCount('uid:' + uid + ':notifications:unread', -Infinity, Infinity, callback);
|
db.sortedSetCount('uid:' + uid + ':notifications:unread', -Infinity, Infinity, callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
UserNotifications.getUnreadByUniqueId = function(uid, uniqueId, callback) {
|
UserNotifications.getUnreadByField = function(uid, field, value, callback) {
|
||||||
db.getSortedSetRange('uid:' + uid + ':notifications:unread', 0, -1, function(err, nids) {
|
db.getSortedSetRange('uid:' + uid + ':notifications:unread', 0, -1, function(err, uniqueIds) {
|
||||||
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!Array.isArray(uniqueIds) || !uniqueIds.length) {
|
||||||
|
return callback(null, []);
|
||||||
|
}
|
||||||
|
|
||||||
|
db.getObjectFields('uid:' + uid + ':notifications:uniqueId:nid', uniqueIds, function(err, uniqueIdsToNids) {
|
||||||
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
var nids = Object.keys(uniqueIdsToNids).map(function(uniqueId) {
|
||||||
|
return uniqueIdsToNids[uniqueId];
|
||||||
|
});
|
||||||
|
|
||||||
async.filter(nids, function(nid, next) {
|
async.filter(nids, function(nid, next) {
|
||||||
notifications.get(nid, uid, function(notifObj) {
|
notifications.get(nid, uid, function(err, notifObj) {
|
||||||
if(!notifObj) {
|
if (err || !notifObj) {
|
||||||
return next(false);
|
return next(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (notifObj.uniqueId === uniqueId) {
|
next(notifObj[field] === value.toString());
|
||||||
next(true);
|
|
||||||
} else {
|
|
||||||
next(false);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}, function(nids) {
|
}, function(nids) {
|
||||||
callback(null, nids);
|
callback(null, nids);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
UserNotifications.sendPostNotificationToFollowers = function(uid, tid, pid) {
|
UserNotifications.sendPostNotificationToFollowers = function(uid, tid, pid) {
|
||||||
db.getSetMembers('followers:' + uid, function(err, followers) {
|
db.getSetMembers('followers:' + uid, function(err, followers) {
|
||||||
if (err || !followers || !followers.length) {
|
if (err || !followers || !followers.length) {
|
||||||
@@ -184,7 +262,10 @@ var async = require('async'),
|
|||||||
path: nconf.get('relative_path') + '/topic/' + results.topic.slug + '/' + results.postIndex,
|
path: nconf.get('relative_path') + '/topic/' + results.topic.slug + '/' + results.postIndex,
|
||||||
uniqueId: 'topic:' + tid,
|
uniqueId: 'topic:' + tid,
|
||||||
from: uid
|
from: uid
|
||||||
}, function(nid) {
|
}, function(err, nid) {
|
||||||
|
if (err) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
async.filter(followers, function(uid, next) {
|
async.filter(followers, function(uid, next) {
|
||||||
privileges.categories.can('read', results.topic.cid, uid, function(err, canRead) {
|
privileges.categories.can('read', results.topic.cid, uid, function(err, canRead) {
|
||||||
next(!err && canRead);
|
next(!err && canRead);
|
||||||
|
|||||||
Reference in New Issue
Block a user