Files
NodeBB/src/user/notifications.js

320 lines
8.1 KiB
JavaScript
Raw Normal View History

'use strict';
var async = require('async');
var winston = require('winston');
2019-06-12 19:59:57 -04:00
var _ = require('lodash');
var db = require('../database');
var meta = require('../meta');
var notifications = require('../notifications');
var privileges = require('../privileges');
2017-10-13 21:02:41 -06:00
var utils = require('../utils');
2017-05-20 22:30:12 -04:00
var UserNotifications = module.exports;
2017-05-20 22:30:12 -04:00
UserNotifications.get = function (uid, callback) {
2018-11-12 00:20:44 -05:00
if (parseInt(uid, 10) <= 0) {
return setImmediate(callback, null, { read: [], unread: [] });
2017-05-20 22:30:12 -04:00
}
2019-01-31 13:13:59 -05:00
let unread;
2017-05-20 22:30:12 -04:00
async.waterfall([
function (next) {
2019-01-31 13:13:59 -05:00
getNotificationsFromSet('uid:' + uid + ':notifications:unread', uid, 0, 29, next);
2017-05-20 22:30:12 -04:00
},
2019-01-31 13:13:59 -05:00
function (_unread, next) {
unread = _unread.filter(Boolean);
if (unread.length < 30) {
getNotificationsFromSet('uid:' + uid + ':notifications:read', uid, 0, 29 - unread.length, next);
} else {
next(null, []);
}
2019-01-31 13:13:59 -05:00
},
function (read, next) {
next(null, {
read: read.filter(Boolean),
unread: unread,
});
2017-05-20 22:30:12 -04:00
},
], callback);
};
2017-03-14 23:03:03 +03:00
2017-05-20 22:30:12 -04:00
function filterNotifications(nids, filter, callback) {
if (!filter) {
return setImmediate(callback, null, nids);
2014-10-17 18:31:20 -04:00
}
2017-05-20 22:30:12 -04:00
async.waterfall([
function (next) {
2019-06-12 19:59:57 -04:00
const keys = nids.map(nid => 'notifications:' + nid);
2017-05-20 22:30:12 -04:00
db.getObjectsFields(keys, ['nid', 'type'], next);
},
function (notifications, next) {
2019-06-12 19:59:57 -04:00
nids = notifications.filter(n => n && n.nid && n.type === filter).map(n => n.nid);
2017-05-20 22:30:12 -04:00
next(null, nids);
},
], callback);
}
UserNotifications.getAll = function (uid, filter, callback) {
var nids;
async.waterfall([
function (next) {
2019-06-12 19:59:57 -04:00
db.getSortedSetRevRange([
'uid:' + uid + ':notifications:unread',
'uid:' + uid + ':notifications:read',
], 0, -1, next);
2017-05-20 22:30:12 -04:00
},
2019-06-12 19:59:57 -04:00
function (_nids, next) {
nids = _.uniq(_nids);
2017-05-20 22:30:12 -04:00
db.isSortedSetMembers('notifications', nids, next);
},
function (exists, next) {
var deleteNids = [];
nids = nids.filter(function (nid, index) {
if (!nid || !exists[index]) {
deleteNids.push(nid);
}
2017-05-20 22:30:12 -04:00
return nid && exists[index];
});
2017-05-20 22:30:12 -04:00
deleteUserNids(deleteNids, uid, next);
},
function (next) {
filterNotifications(nids, filter, next);
},
], callback);
};
function deleteUserNids(nids, uid, callback) {
callback = callback || function () {};
if (!nids.length) {
return setImmediate(callback);
2017-03-14 23:03:03 +03:00
}
db.sortedSetRemove([
'uid:' + uid + ':notifications:read',
'uid:' + uid + ':notifications:unread',
], nids, callback);
2017-05-20 22:30:12 -04:00
}
2019-01-31 13:13:59 -05:00
function getNotificationsFromSet(set, uid, start, stop, callback) {
2017-05-20 22:30:12 -04:00
async.waterfall([
function (next) {
db.getSortedSetRevRange(set, start, stop, next);
},
function (nids, next) {
UserNotifications.getNotifications(nids, uid, next);
},
], callback);
}
UserNotifications.getNotifications = function (nids, uid, callback) {
2017-05-27 00:30:07 -04:00
if (!Array.isArray(nids) || !nids.length) {
return callback(null, []);
}
2017-05-20 22:30:12 -04:00
var notificationData = [];
async.waterfall([
function (next) {
async.parallel({
notifications: function (next) {
2017-08-18 19:00:48 -04:00
notifications.getMultiple(nids, next);
2017-05-20 22:30:12 -04:00
},
hasRead: function (next) {
db.isSortedSetMembers('uid:' + uid + ':notifications:read', nids, next);
},
}, next);
},
function (results, next) {
var deletedNids = [];
notificationData = results.notifications.filter(function (notification, index) {
if (!notification || !notification.nid) {
deletedNids.push(nids[index]);
}
if (notification) {
notification.read = results.hasRead[index];
notification.readClass = !notification.read ? 'unread' : '';
}
2016-03-01 16:13:01 -05:00
2017-05-20 22:30:12 -04:00
return notification && notification.path;
});
2017-05-20 22:30:12 -04:00
deleteUserNids(deletedNids, uid, next);
},
function (next) {
notifications.merge(notificationData, next);
},
], callback);
};
UserNotifications.getDailyUnread = function (uid, callback) {
var yesterday = Date.now() - (1000 * 60 * 60 * 24); // Approximate, can be more or less depending on time changes, makes no difference really.
async.waterfall([
function (next) {
db.getSortedSetRevRangeByScore('uid:' + uid + ':notifications:unread', 0, 20, '+inf', yesterday, next);
},
function (nids, next) {
UserNotifications.getNotifications(nids, uid, next);
},
], callback);
};
UserNotifications.getUnreadCount = function (uid, callback) {
2018-11-12 00:20:44 -05:00
if (parseInt(uid, 10) <= 0) {
return setImmediate(callback, null, 0);
2017-05-20 22:30:12 -04:00
}
async.waterfall([
function (next) {
db.getSortedSetRevRange('uid:' + uid + ':notifications:unread', 0, 99, next);
},
function (nids, next) {
notifications.filterExists(nids, next);
},
function (nids, next) {
2019-06-12 19:59:57 -04:00
const keys = nids.map(nid => 'notifications:' + nid);
2017-05-20 22:30:12 -04:00
db.getObjectsFields(keys, ['mergeId'], next);
},
function (mergeIds, next) {
// Collapse any notifications with identical mergeIds
2019-06-12 19:59:57 -04:00
mergeIds = mergeIds.map(set => set.mergeId);
2017-05-20 22:30:12 -04:00
next(null, mergeIds.reduce(function (count, mergeId, idx, arr) {
// A missing (null) mergeId means that notification is counted separately.
if (mergeId === null || idx === arr.indexOf(mergeId)) {
count += 1;
2014-07-28 15:52:33 -04:00
}
2014-09-30 23:19:53 -04:00
2017-05-20 22:30:12 -04:00
return count;
}, 0));
},
], callback);
};
UserNotifications.getUnreadByField = function (uid, field, values, callback) {
var nids;
async.waterfall([
function (next) {
db.getSortedSetRevRange('uid:' + uid + ':notifications:unread', 0, 99, next);
},
function (_nids, next) {
nids = _nids;
2017-05-27 00:30:07 -04:00
if (!nids.length) {
2017-05-20 22:30:12 -04:00
return callback(null, []);
}
2014-07-28 15:52:33 -04:00
2019-06-12 19:59:57 -04:00
const keys = nids.map(nid => 'notifications:' + nid);
2017-05-20 22:30:12 -04:00
db.getObjectsFields(keys, ['nid', field], next);
},
function (notifications, next) {
const valuesSet = new Set(values.map(value => String(value)));
2019-06-12 19:59:57 -04:00
nids = notifications.filter(n => n && n[field] && valuesSet.has(String(n[field]))).map(n => n.nid);
2017-05-20 22:30:12 -04:00
next(null, nids);
},
], callback);
};
2014-10-18 16:45:35 -04:00
2017-05-20 22:30:12 -04:00
UserNotifications.deleteAll = function (uid, callback) {
2018-11-12 00:20:44 -05:00
if (parseInt(uid, 10) <= 0) {
return setImmediate(callback);
2017-05-20 22:30:12 -04:00
}
2019-06-12 19:59:57 -04:00
db.deleteAll([
'uid:' + uid + ':notifications:unread',
'uid:' + uid + ':notifications:read',
2017-05-20 22:30:12 -04:00
], callback);
};
UserNotifications.sendTopicNotificationToFollowers = function (uid, topicData, postData) {
var followers;
async.waterfall([
function (next) {
db.getSortedSetRange('followers:' + uid, 0, -1, next);
},
function (followers, next) {
privileges.categories.filterUids('read', topicData.cid, followers, next);
},
function (_followers, next) {
followers = _followers;
if (!followers.length) {
return;
2016-05-06 13:47:10 +03:00
}
2017-05-20 22:30:12 -04:00
var title = topicData.title;
if (title) {
2017-10-13 21:02:41 -06:00
title = utils.decodeHTMLEntities(title);
2016-05-06 13:47:10 +03:00
}
2014-04-16 15:51:05 -04:00
2017-05-20 22:30:12 -04:00
notifications.create({
type: 'new-topic',
bodyShort: '[[notifications:user_posted_topic, ' + postData.user.username + ', ' + title + ']]',
bodyLong: postData.content,
pid: postData.pid,
path: '/post/' + postData.pid,
nid: 'tid:' + postData.tid + ':uid:' + uid,
tid: postData.tid,
from: uid,
}, next);
},
], function (err, notification) {
if (err) {
return winston.error(err);
2014-09-29 12:30:03 -04:00
}
2017-05-20 22:30:12 -04:00
if (notification) {
notifications.push(notification, followers);
}
});
};
2014-09-29 12:30:03 -04:00
2017-05-20 22:30:12 -04:00
UserNotifications.sendWelcomeNotification = function (uid, callback) {
callback = callback || function () {};
if (!meta.config.welcomeNotification) {
return callback();
}
2016-01-27 20:03:28 +02:00
2017-05-20 22:30:12 -04:00
var path = meta.config.welcomeLink ? meta.config.welcomeLink : '#';
async.waterfall([
function (next) {
notifications.create({
bodyShort: meta.config.welcomeNotification,
path: path,
nid: 'welcome_' + uid,
from: meta.config.welcomeUid ? meta.config.welcomeUid : null,
2017-05-20 22:30:12 -04:00
}, next);
},
function (notification, next) {
if (!notification) {
return next();
}
2017-05-20 22:30:12 -04:00
notifications.push(notification, [uid], next);
},
], callback);
};
UserNotifications.sendNameChangeNotification = function (uid, username) {
notifications.create({
bodyShort: '[[user:username_taken_workaround, ' + username + ']]',
image: 'brand:logo',
nid: 'username_taken:' + uid,
datetime: Date.now(),
}, function (err, notification) {
if (!err && notification) {
notifications.push(notification, uid);
}
});
};
UserNotifications.pushCount = function (uid) {
var websockets = require('./../socket.io');
UserNotifications.getUnreadCount(uid, function (err, count) {
if (err) {
return winston.error(err.stack);
}
2017-05-20 22:30:12 -04:00
websockets.in('uid_' + uid).emit('event:notifications.updateCount', count);
});
};