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,315 +13,205 @@ 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); if (unread.length < 30) {
}, read = await getNotificationsFromSet('uid:' + uid + ':notifications:read', uid, 0, 29 - unread.length);
function (_unread, next) { }
unread = _unread.filter(Boolean); return {
if (unread.length < 30) { read: read.filter(Boolean),
getNotificationsFromSet('uid:' + uid + ':notifications:read', uid, 0, 29 - unread.length, next); unread: unread,
} else { };
next(null, []);
}
},
function (read, next) {
next(null, {
read: read.filter(Boolean),
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([ const keys = nids.map(nid => 'notifications:' + nid);
function (next) { const notifications = await db.getObjectsFields(keys, ['nid', 'type']);
const keys = nids.map(nid => 'notifications:' + nid); return notifications.filter(n => n && n.nid && n.type === filter).map(n => n.nid);
db.getObjectsFields(keys, ['nid', 'type'], next);
},
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([ 'uid:' + uid + ':notifications:unread',
function (next) { 'uid:' + uid + ':notifications:read',
db.getSortedSetRevRange([ ], 0, -1);
'uid:' + uid + ':notifications:unread', nids = _.uniq(nids);
'uid:' + uid + ':notifications:read', const exists = await db.isSortedSetMembers('notifications', nids);
], 0, -1, next); var deleteNids = [];
},
function (_nids, next) { nids = nids.filter(function (nid, index) {
nids = _.uniq(_nids); if (!nid || !exists[index]) {
db.isSortedSetMembers('notifications', nids, next); deleteNids.push(nid);
}, }
function (exists, next) { return nid && exists[index];
var deleteNids = []; });
nids = nids.filter(function (nid, index) { await deleteUserNids(deleteNids, uid);
if (!nid || !exists[index]) { return await filterNotifications(nids, filter);
deleteNids.push(nid);
}
return nid && exists[index];
});
deleteUserNids(deleteNids, uid, next);
},
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) { if (!notification || !notification.nid) {
db.isSortedSetMembers('uid:' + uid + ':notifications:read', nids, next); deletedNids.push(nids[index]);
}, }
}, next); if (notification) {
}, notification.read = hasRead[index];
function (results, next) { notification.readClass = !notification.read ? 'unread' : '';
var deletedNids = []; }
notificationData = results.notifications.filter(function (notification, index) {
if (!notification || !notification.nid) { return notification && notification.path;
deletedNids.push(nids[index]); });
}
if (notification) { await deleteUserNids(deletedNids, uid);
notification.read = results.hasRead[index]; notificationData = await notifications.merge(notificationData);
notification.readClass = !notification.read ? 'unread' : ''; const result = await plugins.fireHook('filter:user.notifications.getNotifications', {
} uid: uid,
notifications: notificationData,
return notification && notification.path; });
}); return result && result.notifications;
deleteUserNids(deletedNids, uid, next);
},
function (next) {
notifications.merge(notificationData, next);
},
function (notifications, next) {
plugins.fireHook('filter:user.notifications.getNotifications', {
uid: uid,
notifications: notifications,
}, function (err, result) {
next(err, 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);
nids = await notifications.filterExists(nids);
const keys = nids.map(nid => 'notifications:' + nid);
const notifData = await db.getObjectsFields(keys, ['mergeId']);
const mergeIds = notifData.map(n => n.mergeId);
// Collapse any notifications with identical mergeIds
return 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;
}
async.waterfall([ return count;
function (next) { }, 0);
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);
db.getObjectsFields(keys, ['mergeId'], next);
},
function (mergeIds, next) {
// Collapse any notifications with identical mergeIds
mergeIds = mergeIds.map(set => set.mergeId);
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;
}
return count;
}, 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([ if (!nids.length) {
function (next) { return [];
db.getSortedSetRevRange('uid:' + uid + ':notifications:unread', 0, 99, next); }
}, const keys = nids.map(nid => 'notifications:' + nid);
function (_nids, next) { const notifData = await db.getObjectsFields(keys, ['nid', field]);
nids = _nids; const valuesSet = new Set(values.map(value => String(value)));
if (!nids.length) { return notifData.filter(n => n && n[field] && valuesSet.has(String(n[field]))).map(n => n.nid);
return callback(null, []);
}
const keys = nids.map(nid => 'notifications:' + nid);
db.getObjectsFields(keys, ['nid', field], next);
},
function (notifications, next) {
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);
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); if (!followers.length) {
}, return;
function (followers, next) { }
privileges.categories.filterUids('read', topicData.cid, followers, next); let title = topicData.title;
}, if (title) {
function (_followers, next) { title = utils.decodeHTMLEntities(title);
followers = _followers;
if (!followers.length) {
return;
}
var title = topicData.title;
if (title) {
title = utils.decodeHTMLEntities(title);
}
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);
} }
if (notification) { const notifObj = await notifications.create({
notifications.push(notification, followers); 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,
});
await notifications.push(notifObj, followers);
} catch (err) {
if (err) {
return winston.error(err);
} }
}); throw err;
}
}; };
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({
bodyShort: meta.config.welcomeNotification,
path: path,
nid: 'welcome_' + uid,
from: meta.config.welcomeUid ? meta.config.welcomeUid : null,
});
async.waterfall([ await notifications.push(notifObj, [uid]);
function (next) {
notifications.create({
bodyShort: meta.config.welcomeNotification,
path: path,
nid: 'welcome_' + uid,
from: meta.config.welcomeUid ? meta.config.welcomeUid : null,
}, next);
},
function (notification, next) {
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) { websockets.in('uid_' + uid).emit('event:notifications.updateCount', count);
return winston.error(err.stack);
}
websockets.in('uid_' + uid).emit('event:notifications.updateCount', count);
});
}; };

Loading…
Cancel
Save