diff --git a/public/src/client/notifications.js b/public/src/client/notifications.js index 4f2dfbf197..7c0dcd48c3 100644 --- a/public/src/client/notifications.js +++ b/public/src/client/notifications.js @@ -2,12 +2,12 @@ /* globals define, socket, app */ -define('forum/notifications', ['components', 'notifications'], function(components, notifs) { +define('forum/notifications', ['components', 'notifications', 'forum/infinitescroll'], function(components, notifs, infinitescroll) { var Notifications = {}; Notifications.init = function() { var listEl = $('.notifications-list'); - listEl.on('click', '[component="notifications/item/link"]', function(e) { + listEl.on('click', '[component="notifications/item/link"]', function() { var nid = $(this).parents('[data-nid]').attr('data-nid'); socket.emit('notifications.markRead', nid, function(err) { if (err) { @@ -28,7 +28,32 @@ define('forum/notifications', ['components', 'notifications'], function(componen 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(); + } + infinitescroll.parseAndTranslate('notifications', 'notifications', {notifications: data.notifications}, function(html) { + notifList.append(html); + html.find('.timeago').timeago(); + done(); + }); + }); + } + return Notifications; }); diff --git a/src/controllers/accounts/notifications.js b/src/controllers/accounts/notifications.js index 2f540e9d94..aa60892f47 100644 --- a/src/controllers/accounts/notifications.js +++ b/src/controllers/accounts/notifications.js @@ -7,12 +7,13 @@ var user = require('../../user'), var notificationsController = {}; notificationsController.get = function(req, res, next) { - user.notifications.getAll(req.uid, 40, function(err, notifications) { + user.notifications.getAll(req.uid, 0, 39, function(err, notifications) { if (err) { return next(err); } res.render('notifications', { notifications: notifications, + nextStart: 40, title: '[[pages:notifications]]', breadcrumbs: helpers.buildBreadcrumbs([{text: '[[pages:notifications]]'}]) }); diff --git a/src/socket.io/notifications.js b/src/socket.io/notifications.js index c957fff1a7..c540c338bb 100644 --- a/src/socket.io/notifications.js +++ b/src/socket.io/notifications.js @@ -12,6 +12,23 @@ SocketNotifs.get = function(socket, data, callback) { } }; +SocketNotifs.loadMore = function(socket, data, callback) { + if (!data || !parseInt(data.after, 10)) { + return callback(new Error('[[error:invalid-data]]')); + } + if (!socket.uid) { + return; + } + var start = parseInt(data.after, 10); + var stop = start + 20; + user.notifications.getAll(socket.uid, start, stop, function(err, notifications) { + if (err) { + return callback(err); + } + callback(null, {notifications: notifications, nextStart: stop}); + }); +}; + SocketNotifs.getCount = function(socket, data, callback) { user.notifications.getUnreadCount(socket.uid, callback); }; diff --git a/src/user/notifications.js b/src/user/notifications.js index 1a93a6ba4b..7cc309d1a8 100644 --- a/src/user/notifications.js +++ b/src/user/notifications.js @@ -21,7 +21,7 @@ var async = require('async'), if (!parseInt(uid, 10)) { return callback(null , {read: [], unread: []}); } - getNotifications(uid, 10, function(err, notifications) { + getNotifications(uid, 0, 9, function(err, notifications) { if (err) { return callback(err); } @@ -38,8 +38,8 @@ var async = require('async'), }); }; - UserNotifications.getAll = function(uid, count, callback) { - getNotifications(uid, count, function(err, notifs) { + UserNotifications.getAll = function(uid, start, stop, callback) { + getNotifications(uid, start, stop, function(err, notifs) { if (err) { return callback(err); } @@ -52,13 +52,13 @@ var async = require('async'), }); }; - function getNotifications(uid, count, callback) { + function getNotifications(uid, start, stop, callback) { async.parallel({ unread: function(next) { - getNotificationsFromSet('uid:' + uid + ':notifications:unread', false, uid, 0, count - 1, next); + getNotificationsFromSet('uid:' + uid + ':notifications:unread', false, uid, start, stop, next); }, read: function(next) { - getNotificationsFromSet('uid:' + uid + ':notifications:read', true, uid, 0, count - 1, next); + getNotificationsFromSet('uid:' + uid + ':notifications:read', true, uid, start, stop, next); } }, callback); }