You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nodebb/src/user/notifications.js

294 lines
8.0 KiB
JavaScript

'use strict';
var async = require('async'),
nconf = require('nconf'),
winston = require('winston'),
user = require('../user'),
utils = require('../../public/src/utils'),
db = require('../database'),
notifications = require('../notifications'),
11 years ago
posts = require('../posts'),
postTools = require('../postTools'),
11 years ago
topics = require('../topics'),
privileges = require('../privileges');
(function(UserNotifications) {
11 years ago
UserNotifications.get = function(uid, callback) {
11 years ago
function getNotifications(set, start, stop, iterator, done) {
11 years ago
db.getSortedSetRevRange(set, start, stop, function(err, uniqueIds) {
if(err) {
return done(err);
}
11 years ago
if(!Array.isArray(uniqueIds) || !uniqueIds.length) {
return done(null, []);
}
11 years ago
if (uniqueIds.length > maxNotifs) {
uniqueIds.length = maxNotifs;
}
11 years ago
db.getObjectFields('uid:' + uid + ':notifications:uniqueId:nid', uniqueIds, function(err, uniqueIdToNids) {
if (err) {
return done(err);
}
11 years ago
var nidsToUniqueIds = {};
Object.keys(uniqueIdToNids).forEach(function(uniqueId) {
nidsToUniqueIds[uniqueIdToNids[uniqueId]] = uniqueId;
});
11 years ago
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);
}
});
}, done);
});
});
}
var maxNotifs = 15;
async.parallel({
unread: function(next) {
11 years ago
getNotifications('uid:' + uid + ':notifications:unread', 0, 9, function(notif_data, next) {
11 years ago
notif_data.read = false;
notif_data.readClass = !notif_data.read ? 'label-warning' : '';
next(null, notif_data);
}, next);
},
read: function(next) {
11 years ago
getNotifications('uid:' + uid + ':notifications:read', 0, 9, function(notif_data, next) {
notif_data.read = true;
next(null, notif_data);
}, next);
}
}, function(err, notifications) {
function filterDeleted(notifObj) {
return !!notifObj;
}
11 years ago
if (err) {
return callback(err);
}
notifications.read = notifications.read.filter(filterDeleted);
notifications.unread = notifications.unread.filter(filterDeleted);
// Limit the number of notifications to `maxNotifs`, prioritising unread notifications
if (notifications.read.length + notifications.unread.length > maxNotifs) {
notifications.read.length = maxNotifs - notifications.unread.length;
}
callback(null, notifications);
});
};
UserNotifications.getAll = function(uid, limit, before, callback) {
var now = new Date();
if (!limit || parseInt(limit, 10) <= 0) {
limit = 25;
}
if (before) {
before = new Date(parseInt(before, 10));
}
11 years ago
db.getObjectValues('uid:' + uid + ':notifications:uniqueId:nid', function(err, nids) {
if (err) {
return callback(err);
}
11 years ago
async.map(nids, function(nid, next) {
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);
});
11 years ago
});
}, function(err, notifs) {
if (err) {
return callback(err);
}
11 years ago
notifs = notifs.filter(function(notif) {
return notif !== null;
}).sort(function(a, b) {
return parseInt(b.datetime, 10) - parseInt(a.datetime, 10);
}).map(function(notif) {
notif.datetimeISO = utils.toISOString(notif.datetime);
notif.readClass = !notif.read ? 'label-warning' : '';
return notif;
});
11 years ago
callback(null, notifs);
});
});
};
11 years ago
UserNotifications.isNotificationRead = function(nid, uid, callback) {
db.isSortedSetMember('uid:' + uid + ':notifications:read', nid, callback);
};
UserNotifications.getDailyUnread = function(uid, callback) {
var now = Date.now(),
yesterday = now - (1000*60*60*24); // Approximate, can be more or less depending on time changes, makes no difference really.
11 years ago
db.getSortedSetRangeByScore('uid:' + uid + ':notifications:unread', 0, 20, yesterday, now, 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, uniqueIdToNids) {
if (err) {
return callback(err);
}
var nids = Object.keys(uniqueIdToNids).map(function(uniqueId) {
return uniqueIdToNids[uniqueId];
});
11 years ago
async.map(nids, function(nid, next) {
notifications.get(nid, next);
}, callback);
});
});
};
UserNotifications.getUnreadCount = function(uid, callback) {
db.sortedSetCount('uid:' + uid + ':notifications:unread', -Infinity, Infinity, callback);
};
11 years ago
UserNotifications.getUnreadByField = function(uid, field, value, callback) {
db.getSortedSetRange('uid:' + uid + ':notifications:unread', 0, -1, function(err, uniqueIds) {
if (err) {
return callback(err);
}
11 years ago
if (!Array.isArray(uniqueIds) || !uniqueIds.length) {
return callback(null, []);
}
11 years ago
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) {
11 years ago
notifications.get(nid, function(err, notifObj) {
11 years ago
if (err || !notifObj) {
return next(false);
}
next(notifObj[field] === value.toString());
});
}, function(nids) {
callback(null, nids);
});
});
});
};
11 years ago
UserNotifications.sendPostNotificationToFollowers = function(uid, tid, pid) {
11 years ago
db.getSetMembers('followers:' + uid, function(err, followers) {
if (err || !followers || !followers.length) {
return;
}
async.parallel({
username: async.apply(user.getUserField, uid, 'username'),
topic: async.apply(topics.getTopicFields, tid, ['slug', 'cid', 'title']),
postIndex: async.apply(posts.getPidIndex, pid),
postContent: function(next) {
async.waterfall([
async.apply(posts.getPostField, pid, 'content'),
function(content, next) {
postTools.parse(content, next);
}
], next);
},
topicFollowers: function(next) {
db.isSetMembers('tid:' + tid + ':followers', followers, next);
}
11 years ago
}, function(err, results) {
if (err) {
return;
}
11 years ago
followers = followers.filter(function(value, index) {
return !results.topicFollowers[index];
});
11 years ago
notifications.create({
bodyShort: '[[notifications:user_posted_to, ' + results.username + ', ' + results.topic.title + ']]',
bodyLong: results.postContent,
11 years ago
path: nconf.get('relative_path') + '/topic/' + results.topic.slug + '/' + results.postIndex,
11 years ago
uniqueId: 'topic:' + tid,
from: uid
11 years ago
}, function(err, nid) {
if (err) {
return;
}
11 years ago
async.filter(followers, function(uid, next) {
privileges.categories.can('read', results.topic.cid, uid, function(err, canRead) {
next(!err && canRead);
});
}, function(followers){
notifications.push(nid, followers);
});
11 years ago
});
});
});
};
UserNotifications.pushCount = function(uid) {
var websockets = require('./../socket.io');
UserNotifications.getUnreadCount(uid, function(err, count) {
if (err) {
return winston.warn('[User.pushNotifCount] Count not retrieve unread notifications count to push to uid ' + uid + '\'s client(s)');
}
websockets.in('uid_' + uid).emit('event:notifications.updateCount', count);
});
};
}(exports));