user/notifications.js
v1.18.x
Barış Soner Uşaklı 6 years ago
parent f73468d5e7
commit 1c5fad6dae

@ -1,7 +1,6 @@
'use strict'; 'use strict';
var async = require('async');
var winston = require('winston'); var winston = require('winston');
var _ = require('lodash'); var _ = require('lodash');
@ -14,63 +13,39 @@ var utils = require('../utils');
var UserNotifications = module.exports; var UserNotifications = module.exports;
UserNotifications.get = function (uid, callback) { UserNotifications.get = async function (uid) {
if (parseInt(uid, 10) <= 0) { if (parseInt(uid, 10) <= 0) {
return setImmediate(callback, null, { read: [], unread: [] }); return { read: [], unread: [] };
} }
let unread; let unread = await getNotificationsFromSet('uid:' + uid + ':notifications:unread', uid, 0, 29);
async.waterfall([ unread = unread.filter(Boolean);
function (next) { let read = [];
getNotificationsFromSet('uid:' + uid + ':notifications:unread', uid, 0, 29, next);
},
function (_unread, next) {
unread = _unread.filter(Boolean);
if (unread.length < 30) { if (unread.length < 30) {
getNotificationsFromSet('uid:' + uid + ':notifications:read', uid, 0, 29 - unread.length, next); read = await getNotificationsFromSet('uid:' + uid + ':notifications:read', uid, 0, 29 - unread.length);
} else {
next(null, []);
} }
}, return {
function (read, next) {
next(null, {
read: read.filter(Boolean), read: read.filter(Boolean),
unread: unread, unread: unread,
}); };
},
], callback);
}; };
function filterNotifications(nids, filter, callback) { async function filterNotifications(nids, filter) {
if (!filter) { if (!filter) {
return setImmediate(callback, null, nids); return nids;
} }
async.waterfall([
function (next) {
const keys = nids.map(nid => 'notifications:' + nid); const keys = nids.map(nid => 'notifications:' + nid);
db.getObjectsFields(keys, ['nid', 'type'], next); const notifications = await db.getObjectsFields(keys, ['nid', 'type']);
}, return notifications.filter(n => n && n.nid && n.type === filter).map(n => n.nid);
function (notifications, next) {
nids = notifications.filter(n => n && n.nid && n.type === filter).map(n => n.nid);
next(null, nids);
},
], callback);
} }
UserNotifications.getAll = function (uid, filter, callback) { UserNotifications.getAll = async function (uid, filter) {
var nids; let nids = await db.getSortedSetRevRange([
async.waterfall([
function (next) {
db.getSortedSetRevRange([
'uid:' + uid + ':notifications:unread', 'uid:' + uid + ':notifications:unread',
'uid:' + uid + ':notifications:read', 'uid:' + uid + ':notifications:read',
], 0, -1, next); ], 0, -1);
}, nids = _.uniq(nids);
function (_nids, next) { const exists = await db.isSortedSetMembers('notifications', nids);
nids = _.uniq(_nids);
db.isSortedSetMembers('notifications', nids, next);
},
function (exists, next) {
var deleteNids = []; var deleteNids = [];
nids = nids.filter(function (nid, index) { nids = nids.filter(function (nid, index) {
@ -80,182 +55,115 @@ UserNotifications.getAll = function (uid, filter, callback) {
return nid && exists[index]; return nid && exists[index];
}); });
deleteUserNids(deleteNids, uid, next); await deleteUserNids(deleteNids, uid);
}, return await filterNotifications(nids, filter);
function (next) {
filterNotifications(nids, filter, next);
},
], callback);
}; };
function deleteUserNids(nids, uid, callback) { async function deleteUserNids(nids, uid) {
callback = callback || function () {}; await db.sortedSetRemove([
if (!nids.length) {
return setImmediate(callback);
}
db.sortedSetRemove([
'uid:' + uid + ':notifications:read', 'uid:' + uid + ':notifications:read',
'uid:' + uid + ':notifications:unread', 'uid:' + uid + ':notifications:unread',
], nids, callback); ], nids);
} }
function getNotificationsFromSet(set, uid, start, stop, callback) { async function getNotificationsFromSet(set, uid, start, stop) {
async.waterfall([ const nids = await db.getSortedSetRevRange(set, start, stop);
function (next) { return await UserNotifications.getNotifications(nids, uid);
db.getSortedSetRevRange(set, start, stop, next);
},
function (nids, next) {
UserNotifications.getNotifications(nids, uid, next);
},
], callback);
} }
UserNotifications.getNotifications = function (nids, uid, callback) { UserNotifications.getNotifications = async function (nids, uid) {
if (!Array.isArray(nids) || !nids.length) { if (!Array.isArray(nids) || !nids.length) {
return setImmediate(callback, null, []); return [];
} }
var notificationData = []; const [notifObjs, hasRead] = await Promise.all([
async.waterfall([ notifications.getMultiple(nids),
function (next) { db.isSortedSetMembers('uid:' + uid + ':notifications:read', nids),
async.parallel({ ]);
notifications: function (next) {
notifications.getMultiple(nids, next); const deletedNids = [];
}, let notificationData = notifObjs.filter(function (notification, index) {
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) { if (!notification || !notification.nid) {
deletedNids.push(nids[index]); deletedNids.push(nids[index]);
} }
if (notification) { if (notification) {
notification.read = results.hasRead[index]; notification.read = hasRead[index];
notification.readClass = !notification.read ? 'unread' : ''; notification.readClass = !notification.read ? 'unread' : '';
} }
return notification && notification.path; return notification && notification.path;
}); });
deleteUserNids(deletedNids, uid, next); await deleteUserNids(deletedNids, uid);
}, notificationData = await notifications.merge(notificationData);
function (next) { const result = await plugins.fireHook('filter:user.notifications.getNotifications', {
notifications.merge(notificationData, next);
},
function (notifications, next) {
plugins.fireHook('filter:user.notifications.getNotifications', {
uid: uid, uid: uid,
notifications: notifications, notifications: notificationData,
}, function (err, result) {
next(err, result && result.notifications);
}); });
}, return result && result.notifications;
], callback);
}; };
UserNotifications.getDailyUnread = function (uid, callback) { UserNotifications.getDailyUnread = async function (uid) {
var yesterday = Date.now() - (1000 * 60 * 60 * 24); // Approximate, can be more or less depending on time changes, makes no difference really. const yesterday = Date.now() - (1000 * 60 * 60 * 24); // Approximate, can be more or less depending on time changes, makes no difference really.
const nids = await db.getSortedSetRevRangeByScore('uid:' + uid + ':notifications:unread', 0, 20, '+inf', yesterday);
async.waterfall([ return await UserNotifications.getNotifications(nids, uid);
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) { UserNotifications.getUnreadCount = async function (uid) {
if (parseInt(uid, 10) <= 0) { if (parseInt(uid, 10) <= 0) {
return setImmediate(callback, null, 0); return 0;
} }
let nids = await db.getSortedSetRevRange('uid:' + uid + ':notifications:unread', 0, 99);
async.waterfall([ nids = await notifications.filterExists(nids);
function (next) {
db.getSortedSetRevRange('uid:' + uid + ':notifications:unread', 0, 99, next);
},
function (nids, next) {
notifications.filterExists(nids, next);
},
function (nids, next) {
const keys = nids.map(nid => 'notifications:' + nid); const keys = nids.map(nid => 'notifications:' + nid);
db.getObjectsFields(keys, ['mergeId'], next); const notifData = await db.getObjectsFields(keys, ['mergeId']);
}, const mergeIds = notifData.map(n => n.mergeId);
function (mergeIds, next) {
// Collapse any notifications with identical mergeIds
mergeIds = mergeIds.map(set => set.mergeId);
next(null, mergeIds.reduce(function (count, mergeId, idx, arr) { // Collapse any notifications with identical mergeIds
return mergeIds.reduce(function (count, mergeId, idx, arr) {
// A missing (null) mergeId means that notification is counted separately. // A missing (null) mergeId means that notification is counted separately.
if (mergeId === null || idx === arr.indexOf(mergeId)) { if (mergeId === null || idx === arr.indexOf(mergeId)) {
count += 1; count += 1;
} }
return count; return count;
}, 0)); }, 0);
},
], callback);
}; };
UserNotifications.getUnreadByField = function (uid, field, values, callback) { UserNotifications.getUnreadByField = async function (uid, field, values) {
var nids; const nids = await db.getSortedSetRevRange('uid:' + uid + ':notifications:unread', 0, 99);
async.waterfall([
function (next) {
db.getSortedSetRevRange('uid:' + uid + ':notifications:unread', 0, 99, next);
},
function (_nids, next) {
nids = _nids;
if (!nids.length) { if (!nids.length) {
return callback(null, []); return [];
} }
const keys = nids.map(nid => 'notifications:' + nid); const keys = nids.map(nid => 'notifications:' + nid);
db.getObjectsFields(keys, ['nid', field], next); const notifData = await db.getObjectsFields(keys, ['nid', field]);
},
function (notifications, next) {
const valuesSet = new Set(values.map(value => String(value))); const valuesSet = new Set(values.map(value => String(value)));
nids = notifications.filter(n => n && n[field] && valuesSet.has(String(n[field]))).map(n => n.nid); return notifData.filter(n => n && n[field] && valuesSet.has(String(n[field]))).map(n => n.nid);
next(null, nids);
},
], callback);
}; };
UserNotifications.deleteAll = function (uid, callback) { UserNotifications.deleteAll = async function (uid) {
if (parseInt(uid, 10) <= 0) { if (parseInt(uid, 10) <= 0) {
return setImmediate(callback); return;
} }
db.deleteAll([ await db.deleteAll([
'uid:' + uid + ':notifications:unread', 'uid:' + uid + ':notifications:unread',
'uid:' + uid + ':notifications:read', 'uid:' + uid + ':notifications:read',
], callback); ]);
}; };
UserNotifications.sendTopicNotificationToFollowers = function (uid, topicData, postData) { UserNotifications.sendTopicNotificationToFollowers = async function (uid, topicData, postData) {
var followers; try {
async.waterfall([ let followers = await db.getSortedSetRange('followers:' + uid, 0, -1);
function (next) { followers = await privileges.categories.filterUids('read', topicData.cid, followers);
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) { if (!followers.length) {
return; return;
} }
let title = topicData.title;
var title = topicData.title;
if (title) { if (title) {
title = utils.decodeHTMLEntities(title); title = utils.decodeHTMLEntities(title);
} }
notifications.create({ const notifObj = await notifications.create({
type: 'new-topic', type: 'new-topic',
bodyShort: '[[notifications:user_posted_topic, ' + postData.user.username + ', ' + title + ']]', bodyShort: '[[notifications:user_posted_topic, ' + postData.user.username + ', ' + title + ']]',
bodyLong: postData.content, bodyLong: postData.content,
@ -264,65 +172,46 @@ UserNotifications.sendTopicNotificationToFollowers = function (uid, topicData, p
nid: 'tid:' + postData.tid + ':uid:' + uid, nid: 'tid:' + postData.tid + ':uid:' + uid,
tid: postData.tid, tid: postData.tid,
from: uid, from: uid,
}, next); });
},
], function (err, notification) { await notifications.push(notifObj, followers);
} catch (err) {
if (err) { if (err) {
return winston.error(err); return winston.error(err);
} }
throw err;
if (notification) {
notifications.push(notification, followers);
} }
});
}; };
UserNotifications.sendWelcomeNotification = function (uid, callback) { UserNotifications.sendWelcomeNotification = async function (uid) {
callback = callback || function () {};
if (!meta.config.welcomeNotification) { if (!meta.config.welcomeNotification) {
return callback(); return;
} }
var path = meta.config.welcomeLink ? meta.config.welcomeLink : '#'; var path = meta.config.welcomeLink ? meta.config.welcomeLink : '#';
const notifObj = await notifications.create({
async.waterfall([
function (next) {
notifications.create({
bodyShort: meta.config.welcomeNotification, bodyShort: meta.config.welcomeNotification,
path: path, path: path,
nid: 'welcome_' + uid, nid: 'welcome_' + uid,
from: meta.config.welcomeUid ? meta.config.welcomeUid : null, from: meta.config.welcomeUid ? meta.config.welcomeUid : null,
}, next); });
},
function (notification, next) { await notifications.push(notifObj, [uid]);
if (!notification) {
return next();
}
notifications.push(notification, [uid], next);
},
], callback);
}; };
UserNotifications.sendNameChangeNotification = function (uid, username) { UserNotifications.sendNameChangeNotification = async function (uid, username) {
notifications.create({ const notifObj = await notifications.create({
bodyShort: '[[user:username_taken_workaround, ' + username + ']]', bodyShort: '[[user:username_taken_workaround, ' + username + ']]',
image: 'brand:logo', image: 'brand:logo',
nid: 'username_taken:' + uid, nid: 'username_taken:' + uid,
datetime: Date.now(), datetime: Date.now(),
}, function (err, notification) {
if (!err && notification) {
notifications.push(notification, uid);
}
}); });
await notifications.push(notifObj, uid);
}; };
UserNotifications.pushCount = function (uid) { UserNotifications.pushCount = async function (uid) {
var websockets = require('./../socket.io'); var websockets = require('./../socket.io');
UserNotifications.getUnreadCount(uid, function (err, count) { const count = await UserNotifications.getUnreadCount(uid);
if (err) {
return winston.error(err.stack);
}
websockets.in('uid_' + uid).emit('event:notifications.updateCount', count); websockets.in('uid_' + uid).emit('event:notifications.updateCount', count);
});
}; };

Loading…
Cancel
Save