v1.18.x
barisusakli 9 years ago
parent d61ac000a8
commit f17ba88c0f

@ -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;
});

@ -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]]'}])
});

@ -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);
};

@ -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);
}

Loading…
Cancel
Save