mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-01 03:26:04 +01:00
closes #5527
This commit is contained in:
@@ -12,6 +12,17 @@
|
|||||||
"new_notification": "New Notification",
|
"new_notification": "New Notification",
|
||||||
"you_have_unread_notifications": "You have unread notifications.",
|
"you_have_unread_notifications": "You have unread notifications.",
|
||||||
|
|
||||||
|
"all": "All",
|
||||||
|
"topics": "Topics",
|
||||||
|
"replies": "Replies",
|
||||||
|
"chat": "Chats",
|
||||||
|
"follows": "Follows",
|
||||||
|
"upvote": "Upvotes",
|
||||||
|
"new-flags": "New Flags",
|
||||||
|
"my-flags": "Flags assigned to me",
|
||||||
|
"bans": "Bans",
|
||||||
|
|
||||||
|
|
||||||
"new_message_from": "New message from <strong>%1</strong>",
|
"new_message_from": "New message from <strong>%1</strong>",
|
||||||
"upvoted_your_post_in": "<strong>%1</strong> has upvoted your post in <strong>%2</strong>.",
|
"upvoted_your_post_in": "<strong>%1</strong> has upvoted your post in <strong>%2</strong>.",
|
||||||
"upvoted_your_post_in_dual": "<strong>%1</strong> and <strong>%2</strong> have upvoted your post in <strong>%3</strong>.",
|
"upvoted_your_post_in_dual": "<strong>%1</strong> and <strong>%2</strong> have upvoted your post in <strong>%3</strong>.",
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
|
||||||
define('forum/notifications', ['components', 'notifications', 'forum/infinitescroll'], function (components, notifs, infinitescroll) {
|
define('forum/notifications', ['components', 'notifications'], function (components, notifs) {
|
||||||
var Notifications = {};
|
var Notifications = {};
|
||||||
|
|
||||||
Notifications.init = function () {
|
Notifications.init = function () {
|
||||||
@@ -35,32 +35,7 @@ define('forum/notifications', ['components', 'notifications', 'forum/infinitescr
|
|||||||
notifs.updateNotifCount(0);
|
notifs.updateNotifCount(0);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
infinitescroll.init(loadMoreNotifications);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
function loadMoreNotifications(direction) {
|
|
||||||
if (direction < 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
var notifList = $('.notifications-list');
|
|
||||||
infinitescroll.loadMore('notifications.loadMore', {
|
|
||||||
after: notifList.attr('data-nextstart'),
|
|
||||||
}, function (data, done) {
|
|
||||||
if (!data) {
|
|
||||||
return done();
|
|
||||||
}
|
|
||||||
notifList.attr('data-nextstart', data.nextStart);
|
|
||||||
if (!data.notifications || !data.notifications.length) {
|
|
||||||
return done();
|
|
||||||
}
|
|
||||||
app.parseAndTranslate('notifications', 'notifications', { notifications: data.notifications }, function (html) {
|
|
||||||
notifList.append(html);
|
|
||||||
html.find('.timeago').timeago();
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return Notifications;
|
return Notifications;
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,23 +1,91 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
var async = require('async');
|
||||||
|
|
||||||
var user = require('../../user');
|
var user = require('../../user');
|
||||||
var helpers = require('../helpers');
|
var helpers = require('../helpers');
|
||||||
|
var plugins = require('../../plugins');
|
||||||
|
var pagination = require('../../pagination');
|
||||||
|
|
||||||
var notificationsController = {};
|
var notificationsController = module.exports;
|
||||||
|
|
||||||
notificationsController.get = function (req, res, next) {
|
notificationsController.get = function (req, res, next) {
|
||||||
user.notifications.getAll(req.uid, 0, 39, function (err, notifications) {
|
var regularFilters = [
|
||||||
if (err) {
|
{ name: '[[notifications:all]]', filter: '' },
|
||||||
return next(err);
|
{ name: '[[notifications:topics]]', filter: 'new-topic' },
|
||||||
}
|
{ name: '[[notifications:replies]]', filter: 'new-reply' },
|
||||||
res.render('notifications', {
|
{ name: '[[notifications:chat]]', filter: 'new-chat' },
|
||||||
notifications: notifications,
|
{ name: '[[notifications:follows]]', filter: 'follow' },
|
||||||
nextStart: 40,
|
{ name: '[[notifications:upvote]]', filter: 'upvote' },
|
||||||
title: '[[pages:notifications]]',
|
];
|
||||||
breadcrumbs: helpers.buildBreadcrumbs([{ text: '[[pages:notifications]]' }]),
|
|
||||||
});
|
var moderatorFilters = [
|
||||||
});
|
{ name: '[[notifications:new-flags]]', filter: 'new-post-flag' },
|
||||||
|
{ name: '[[notifications:my-flags]]', filter: 'my-flags' },
|
||||||
|
{ name: '[[notifications:bans]]', filter: 'ban' },
|
||||||
|
];
|
||||||
|
|
||||||
|
var filter = req.query.filter || '';
|
||||||
|
var page = Math.max(1, req.query.page || 1);
|
||||||
|
var itemsPerPage = 20;
|
||||||
|
var start = (page - 1) * itemsPerPage;
|
||||||
|
var stop = start + itemsPerPage - 1;
|
||||||
|
var selectedFilter;
|
||||||
|
var pageCount = 1;
|
||||||
|
var allFilters = [];
|
||||||
|
|
||||||
|
async.waterfall([
|
||||||
|
function (next) {
|
||||||
|
async.parallel({
|
||||||
|
filters: function (next) {
|
||||||
|
plugins.fireHook('filter:notifications.addFilters', {
|
||||||
|
regularFilters: regularFilters,
|
||||||
|
moderatorFilters: moderatorFilters,
|
||||||
|
uid: req.uid,
|
||||||
|
}, next);
|
||||||
|
},
|
||||||
|
isPrivileged: function (next) {
|
||||||
|
user.isPrivileged(req.uid, next);
|
||||||
|
},
|
||||||
|
}, next);
|
||||||
|
},
|
||||||
|
function (data, _next) {
|
||||||
|
allFilters = data.filters.regularFilters;
|
||||||
|
|
||||||
|
if (data.isPrivileged) {
|
||||||
|
allFilters = allFilters.concat([
|
||||||
|
{ separator: true },
|
||||||
|
]).concat(data.filters.moderatorFilters);
|
||||||
|
}
|
||||||
|
|
||||||
|
selectedFilter = allFilters.find(function (filterData) {
|
||||||
|
filterData.selected = filterData.filter === filter;
|
||||||
|
return filterData.selected;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!selectedFilter) {
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
|
||||||
|
user.notifications.getAll(req.uid, selectedFilter.filter, _next);
|
||||||
|
},
|
||||||
|
function (nids, next) {
|
||||||
|
pageCount = nids.length / itemsPerPage;
|
||||||
|
nids = nids.slice(start, stop + 1);
|
||||||
|
|
||||||
|
user.notifications.getNotifications(nids, req.uid, next);
|
||||||
|
},
|
||||||
|
function (notifications) {
|
||||||
|
res.render('notifications', {
|
||||||
|
notifications: notifications,
|
||||||
|
pagination: pagination.create(page, pageCount, req.query),
|
||||||
|
filters: allFilters,
|
||||||
|
regularFilters: regularFilters,
|
||||||
|
moderatorFilters: moderatorFilters,
|
||||||
|
selectedFilter: selectedFilter,
|
||||||
|
title: '[[pages:notifications]]',
|
||||||
|
breadcrumbs: helpers.buildBreadcrumbs([{ text: '[[pages:notifications]]' }]),
|
||||||
|
});
|
||||||
|
},
|
||||||
|
], next);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
module.exports = notificationsController;
|
|
||||||
|
|||||||
@@ -60,9 +60,6 @@ Flags.list = function (filters, uid, callback) {
|
|||||||
value.forEach(function (x) {
|
value.forEach(function (x) {
|
||||||
orSets.push(setPrefix + x);
|
orSets.push(setPrefix + x);
|
||||||
});
|
});
|
||||||
} else {
|
|
||||||
// Empty array, do nothing
|
|
||||||
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -604,6 +601,7 @@ Flags.notify = function (flagObj, uid, callback) {
|
|||||||
var titleEscaped = title.replace(/%/g, '%').replace(/,/g, ',');
|
var titleEscaped = title.replace(/%/g, '%').replace(/,/g, ',');
|
||||||
|
|
||||||
notifications.create({
|
notifications.create({
|
||||||
|
type: 'new-post-flag',
|
||||||
bodyShort: '[[notifications:user_flagged_post_in, ' + flagObj.reporter.username + ', ' + titleEscaped + ']]',
|
bodyShort: '[[notifications:user_flagged_post_in, ' + flagObj.reporter.username + ', ' + titleEscaped + ']]',
|
||||||
bodyLong: flagObj.description,
|
bodyLong: flagObj.description,
|
||||||
pid: flagObj.targetId,
|
pid: flagObj.targetId,
|
||||||
|
|||||||
@@ -68,6 +68,7 @@ module.exports = function (Messaging) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
notifications.create({
|
notifications.create({
|
||||||
|
type: 'new-chat',
|
||||||
bodyShort: '[[notifications:new_message_from, ' + messageObj.fromUser.username + ']]',
|
bodyShort: '[[notifications:new_message_from, ' + messageObj.fromUser.username + ']]',
|
||||||
bodyLong: messageObj.content,
|
bodyLong: messageObj.content,
|
||||||
nid: 'chat_' + fromuid + '_' + roomId,
|
nid: 'chat_' + fromuid + '_' + roomId,
|
||||||
|
|||||||
@@ -28,64 +28,66 @@ var utils = require('../public/src/utils');
|
|||||||
};
|
};
|
||||||
|
|
||||||
Notifications.getMultiple = function (nids, callback) {
|
Notifications.getMultiple = function (nids, callback) {
|
||||||
|
if (!nids.length) {
|
||||||
|
return setImmediate(callback, null, []);
|
||||||
|
}
|
||||||
var keys = nids.map(function (nid) {
|
var keys = nids.map(function (nid) {
|
||||||
return 'notifications:' + nid;
|
return 'notifications:' + nid;
|
||||||
});
|
});
|
||||||
|
|
||||||
db.getObjects(keys, function (err, notifications) {
|
var notifications;
|
||||||
if (err) {
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
notifications = notifications.filter(Boolean);
|
async.waterfall([
|
||||||
if (!notifications.length) {
|
function (next) {
|
||||||
return callback(null, []);
|
db.getObjects(keys, next);
|
||||||
}
|
},
|
||||||
|
function (_notifications, next) {
|
||||||
|
notifications = _notifications;
|
||||||
|
var userKeys = notifications.map(function (notification) {
|
||||||
|
return notification && notification.from;
|
||||||
|
});
|
||||||
|
|
||||||
var userKeys = notifications.map(function (notification) {
|
User.getUsersFields(userKeys, ['username', 'userslug', 'picture'], next);
|
||||||
return notification.from;
|
},
|
||||||
});
|
function (usersData, next) {
|
||||||
|
|
||||||
User.getUsersFields(userKeys, ['username', 'userslug', 'picture'], function (err, usersData) {
|
|
||||||
if (err) {
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
notifications.forEach(function (notification, index) {
|
notifications.forEach(function (notification, index) {
|
||||||
notification.datetimeISO = utils.toISOString(notification.datetime);
|
if (notification) {
|
||||||
|
notification.datetimeISO = utils.toISOString(notification.datetime);
|
||||||
|
|
||||||
if (notification.bodyLong) {
|
if (notification.bodyLong) {
|
||||||
notification.bodyLong = S(notification.bodyLong).escapeHTML().s;
|
notification.bodyLong = S(notification.bodyLong).escapeHTML().s;
|
||||||
}
|
}
|
||||||
|
|
||||||
notification.user = usersData[index];
|
notification.user = usersData[index];
|
||||||
if (notification.user) {
|
if (notification.user) {
|
||||||
notification.image = notification.user.picture || null;
|
notification.image = notification.user.picture || null;
|
||||||
if (notification.user.username === '[[global:guest]]') {
|
if (notification.user.username === '[[global:guest]]') {
|
||||||
notification.bodyShort = notification.bodyShort.replace(/([\s\S]*?),[\s\S]*?,([\s\S]*?)/, '$1, [[global:guest]], $2');
|
notification.bodyShort = notification.bodyShort.replace(/([\s\S]*?),[\s\S]*?,([\s\S]*?)/, '$1, [[global:guest]], $2');
|
||||||
|
}
|
||||||
|
} else if (notification.image === 'brand:logo' || !notification.image) {
|
||||||
|
notification.image = meta.config['brand:logo'] || nconf.get('relative_path') + '/logo.png';
|
||||||
}
|
}
|
||||||
} else if (notification.image === 'brand:logo' || !notification.image) {
|
|
||||||
notification.image = meta.config['brand:logo'] || nconf.get('relative_path') + '/logo.png';
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
callback(null, notifications);
|
next(null, notifications);
|
||||||
});
|
},
|
||||||
});
|
], callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
Notifications.filterExists = function (nids, callback) {
|
Notifications.filterExists = function (nids, callback) {
|
||||||
// Removes nids that have been pruned
|
async.waterfall([
|
||||||
db.isSortedSetMembers('notifications', nids, function (err, exists) {
|
function (next) {
|
||||||
if (err) {
|
db.isSortedSetMembers('notifications', nids, next);
|
||||||
return callback(err);
|
},
|
||||||
}
|
function (exists, next) {
|
||||||
|
nids = nids.filter(function (notifId, idx) {
|
||||||
|
return exists[idx];
|
||||||
|
});
|
||||||
|
|
||||||
nids = nids.filter(function (notifId, idx) {
|
next(null, nids);
|
||||||
return exists[idx];
|
},
|
||||||
});
|
], callback);
|
||||||
|
|
||||||
callback(null, nids);
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Notifications.findRelated = function (mergeIds, set, callback) {
|
Notifications.findRelated = function (mergeIds, set, callback) {
|
||||||
|
|||||||
@@ -106,6 +106,7 @@ SocketHelpers.sendNotificationToPostOwner = function (pid, fromuid, command, not
|
|||||||
var titleEscaped = title.replace(/%/g, '%').replace(/,/g, ',');
|
var titleEscaped = title.replace(/%/g, '%').replace(/,/g, ',');
|
||||||
|
|
||||||
notifications.create({
|
notifications.create({
|
||||||
|
type: command,
|
||||||
bodyShort: '[[' + notification + ', ' + results.username + ', ' + titleEscaped + ']]',
|
bodyShort: '[[' + notification + ', ' + results.username + ', ' + titleEscaped + ']]',
|
||||||
bodyLong: results.postObj.content,
|
bodyLong: results.postObj.content,
|
||||||
pid: pid,
|
pid: pid,
|
||||||
|
|||||||
@@ -1,11 +1,9 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var async = require('async');
|
|
||||||
var user = require('../user');
|
var user = require('../user');
|
||||||
var notifications = require('../notifications');
|
var notifications = require('../notifications');
|
||||||
var utils = require('../../public/src/utils');
|
|
||||||
|
|
||||||
var SocketNotifs = {};
|
var SocketNotifs = module.exports;
|
||||||
|
|
||||||
SocketNotifs.get = function (socket, data, callback) {
|
SocketNotifs.get = function (socket, data, callback) {
|
||||||
if (data && Array.isArray(data.nids) && socket.uid) {
|
if (data && Array.isArray(data.nids) && socket.uid) {
|
||||||
@@ -15,25 +13,6 @@ SocketNotifs.get = function (socket, data, callback) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
SocketNotifs.loadMore = function (socket, data, callback) {
|
|
||||||
if (!data || !utils.isNumber(data.after) || parseInt(data.after, 10) < 0) {
|
|
||||||
return callback(new Error('[[error:invalid-data]]'));
|
|
||||||
}
|
|
||||||
if (!socket.uid) {
|
|
||||||
return callback(new Error('[[error:no-privileges]]'));
|
|
||||||
}
|
|
||||||
var start = parseInt(data.after, 10);
|
|
||||||
var stop = start + 20;
|
|
||||||
async.waterfall([
|
|
||||||
function (next) {
|
|
||||||
user.notifications.getAll(socket.uid, start, stop, next);
|
|
||||||
},
|
|
||||||
function (notifications, next) {
|
|
||||||
next(null, { notifications: notifications, nextStart: stop });
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
SocketNotifs.getCount = function (socket, data, callback) {
|
SocketNotifs.getCount = function (socket, data, callback) {
|
||||||
user.notifications.getUnreadCount(socket.uid, callback);
|
user.notifications.getUnreadCount(socket.uid, callback);
|
||||||
};
|
};
|
||||||
@@ -57,5 +36,3 @@ SocketNotifs.markUnread = function (socket, nid, callback) {
|
|||||||
SocketNotifs.markAllRead = function (socket, data, callback) {
|
SocketNotifs.markAllRead = function (socket, data, callback) {
|
||||||
notifications.markAllRead(socket.uid, callback);
|
notifications.markAllRead(socket.uid, callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = SocketNotifs;
|
|
||||||
|
|||||||
@@ -171,6 +171,7 @@ SocketUser.follow = function (socket, data, callback) {
|
|||||||
function (_userData, next) {
|
function (_userData, next) {
|
||||||
userData = _userData;
|
userData = _userData;
|
||||||
notifications.create({
|
notifications.create({
|
||||||
|
type: 'follow',
|
||||||
bodyShort: '[[notifications:user_started_following_you, ' + userData.username + ']]',
|
bodyShort: '[[notifications:user_started_following_you, ' + userData.username + ']]',
|
||||||
nid: 'follow:' + data.uid + ':uid:' + socket.uid,
|
nid: 'follow:' + data.uid + ':uid:' + socket.uid,
|
||||||
from: socket.uid,
|
from: socket.uid,
|
||||||
|
|||||||
@@ -224,6 +224,7 @@ module.exports = function (Topics) {
|
|||||||
postData.content = posts.relativeToAbsolute(postData.content);
|
postData.content = posts.relativeToAbsolute(postData.content);
|
||||||
|
|
||||||
notifications.create({
|
notifications.create({
|
||||||
|
type: 'new-reply',
|
||||||
bodyShort: '[[notifications:user_posted_to, ' + postData.user.username + ', ' + titleEscaped + ']]',
|
bodyShort: '[[notifications:user_posted_to, ' + postData.user.username + ', ' + titleEscaped + ']]',
|
||||||
bodyLong: postData.content,
|
bodyLong: postData.content,
|
||||||
pid: postData.pid,
|
pid: postData.pid,
|
||||||
|
|||||||
@@ -32,20 +32,80 @@ var privileges = require('../privileges');
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
UserNotifications.getAll = function (uid, start, stop, callback) {
|
function filterNotifications(nids, filter, callback) {
|
||||||
getNotifications(uid, start, stop, function (err, notifs) {
|
if (!filter) {
|
||||||
if (err) {
|
return setImmediate(callback, null, nids);
|
||||||
return callback(err);
|
}
|
||||||
}
|
async.waterfall([
|
||||||
notifs = notifs.unread.concat(notifs.read);
|
function (next) {
|
||||||
notifs = notifs.filter(Boolean).sort(function (a, b) {
|
var keys = nids.map(function (nid) {
|
||||||
return b.datetime - a.datetime;
|
return 'notifications:' + nid;
|
||||||
});
|
});
|
||||||
|
db.getObjectsFields(keys, ['nid', 'type'], next);
|
||||||
|
},
|
||||||
|
function (notifications, next) {
|
||||||
|
nids = notifications.filter(function (notification) {
|
||||||
|
return notification && notification.nid && notification.type === filter;
|
||||||
|
}).map(function (notification) {
|
||||||
|
return notification.nid;
|
||||||
|
});
|
||||||
|
next(null, nids);
|
||||||
|
},
|
||||||
|
], callback);
|
||||||
|
}
|
||||||
|
|
||||||
callback(null, notifs);
|
UserNotifications.getAll = function (uid, filter, callback) {
|
||||||
});
|
var nids;
|
||||||
|
async.waterfall([
|
||||||
|
function (next) {
|
||||||
|
async.parallel({
|
||||||
|
unread: function (next) {
|
||||||
|
db.getSortedSetRevRange('uid:' + uid + ':notifications:unread', 0, -1, next);
|
||||||
|
},
|
||||||
|
read: function (next) {
|
||||||
|
db.getSortedSetRevRange('uid:' + uid + ':notifications:read', 0, -1, next);
|
||||||
|
},
|
||||||
|
}, next);
|
||||||
|
},
|
||||||
|
function (results, next) {
|
||||||
|
nids = results.unread.concat(results.read);
|
||||||
|
db.isSortedSetMembers('notifications', nids, next);
|
||||||
|
},
|
||||||
|
function (exists, next) {
|
||||||
|
var deleteNids = [];
|
||||||
|
|
||||||
|
nids = nids.filter(function (nid, index) {
|
||||||
|
if (!nid || !exists[index]) {
|
||||||
|
deleteNids.push(nid);
|
||||||
|
}
|
||||||
|
return nid && exists[index];
|
||||||
|
});
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
async.parallel([
|
||||||
|
function (next) {
|
||||||
|
db.sortedSetRemove('uid:' + uid + ':notifications:read', nids, next);
|
||||||
|
},
|
||||||
|
function (next) {
|
||||||
|
db.sortedSetRemove('uid:' + uid + ':notifications:unread', nids, next);
|
||||||
|
},
|
||||||
|
], function (err) {
|
||||||
|
callback(err);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
function getNotifications(uid, start, stop, callback) {
|
function getNotifications(uid, start, stop, callback) {
|
||||||
async.parallel({
|
async.parallel({
|
||||||
unread: function (next) {
|
unread: function (next) {
|
||||||
@@ -58,52 +118,54 @@ var privileges = require('../privileges');
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getNotificationsFromSet(set, read, uid, start, stop, callback) {
|
function getNotificationsFromSet(set, read, uid, start, stop, callback) {
|
||||||
var setNids;
|
|
||||||
|
|
||||||
async.waterfall([
|
async.waterfall([
|
||||||
async.apply(db.getSortedSetRevRange, set, start, stop),
|
function (next) {
|
||||||
|
db.getSortedSetRevRange(set, start, stop, next);
|
||||||
|
},
|
||||||
function (nids, next) {
|
function (nids, next) {
|
||||||
if (!Array.isArray(nids) || !nids.length) {
|
if (!Array.isArray(nids) || !nids.length) {
|
||||||
return callback(null, []);
|
return callback(null, []);
|
||||||
}
|
}
|
||||||
|
|
||||||
setNids = nids;
|
|
||||||
UserNotifications.getNotifications(nids, uid, next);
|
UserNotifications.getNotifications(nids, uid, next);
|
||||||
},
|
},
|
||||||
function (notifs, next) {
|
|
||||||
var deletedNids = [];
|
|
||||||
|
|
||||||
notifs.forEach(function (notification, index) {
|
|
||||||
if (!notification) {
|
|
||||||
winston.verbose('[notifications.get] nid ' + setNids[index] + ' not found. Removing.');
|
|
||||||
deletedNids.push(setNids[index]);
|
|
||||||
} else {
|
|
||||||
notification.read = read;
|
|
||||||
notification.readClass = !notification.read ? 'unread' : '';
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
if (deletedNids.length) {
|
|
||||||
db.sortedSetRemove(set, deletedNids);
|
|
||||||
}
|
|
||||||
|
|
||||||
notifications.merge(notifs, next);
|
|
||||||
},
|
|
||||||
], callback);
|
], callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
UserNotifications.getNotifications = function (nids, uid, callback) {
|
UserNotifications.getNotifications = function (nids, uid, callback) {
|
||||||
notifications.getMultiple(nids, function (err, notifications) {
|
var notificationData = [];
|
||||||
if (err) {
|
async.waterfall([
|
||||||
return callback(err);
|
function (next) {
|
||||||
}
|
async.parallel({
|
||||||
notifications = notifications.filter(function (notification) {
|
notifications: function (next) {
|
||||||
return notification && notification.path;
|
notifications.getMultiple(nids, next);
|
||||||
});
|
},
|
||||||
callback(null, notifications);
|
hasRead: function (next) {
|
||||||
});
|
db.isSortedSetMember('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' : '';
|
||||||
|
}
|
||||||
|
|
||||||
|
return notification && notification.path;
|
||||||
|
});
|
||||||
|
|
||||||
|
deleteUserNids(deletedNids, uid, next);
|
||||||
|
},
|
||||||
|
function (next) {
|
||||||
|
notifications.merge(notificationData, next);
|
||||||
|
},
|
||||||
|
], callback);
|
||||||
|
};
|
||||||
|
|
||||||
UserNotifications.getDailyUnread = function (uid, 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.
|
var yesterday = Date.now() - (1000 * 60 * 60 * 24); // Approximate, can be more or less depending on time changes, makes no difference really.
|
||||||
@@ -222,6 +284,7 @@ var privileges = require('../privileges');
|
|||||||
}
|
}
|
||||||
|
|
||||||
notifications.create({
|
notifications.create({
|
||||||
|
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,
|
||||||
pid: postData.pid,
|
pid: postData.pid,
|
||||||
|
|||||||
@@ -232,31 +232,6 @@ describe('Notifications', function () {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should error with invalid data', function (done) {
|
|
||||||
socketNotifications.loadMore({ uid: uid }, { after: 'test' }, function (err) {
|
|
||||||
assert.equal(err.message, '[[error:invalid-data]]');
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should error if not logged in', function (done) {
|
|
||||||
socketNotifications.loadMore({ uid: 0 }, { after: 10 }, function (err) {
|
|
||||||
assert.equal(err.message, '[[error:no-privileges]]');
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should load more notifications', function (done) {
|
|
||||||
socketNotifications.loadMore({ uid: uid }, { after: 0 }, function (err, data) {
|
|
||||||
assert.ifError(err);
|
|
||||||
assert.equal(data.notifications[0].bodyShort, 'bodyShort');
|
|
||||||
assert.equal(data.notifications[0].nid, 'notification_id');
|
|
||||||
assert.equal(data.notifications[0].path, '/notification/path');
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
it('should error if not logged in', function (done) {
|
it('should error if not logged in', function (done) {
|
||||||
socketNotifications.deleteAll({ uid: 0 }, null, function (err) {
|
socketNotifications.deleteAll({ uid: 0 }, null, function (err) {
|
||||||
assert.equal(err.message, '[[error:no-privileges]]');
|
assert.equal(err.message, '[[error:no-privileges]]');
|
||||||
|
|||||||
Reference in New Issue
Block a user