diff --git a/public/src/client/notifications.js b/public/src/client/notifications.js index 04213499e3..88216fe61e 100644 --- a/public/src/client/notifications.js +++ b/public/src/client/notifications.js @@ -1,7 +1,7 @@ 'use strict'; -define('forum/notifications', ['components', 'notifications'], function (components, notifs) { +define('forum/notifications', ['components'], function (components) { var Notifications = {}; Notifications.init = function () { @@ -12,14 +12,6 @@ define('forum/notifications', ['components', 'notifications'], function (compone if (err) { return app.alertError(err); } - - socket.emit('notifications.getCount', function (err, count) { - if (err) { - return app.alertError(err.message); - } - - notifs.updateNotifCount(count); - }); }); }); @@ -32,7 +24,6 @@ define('forum/notifications', ['components', 'notifications'], function (compone } components.get('notifications/item').removeClass('unread'); - notifs.updateNotifCount(0); }); }); }; diff --git a/public/src/client/topic.js b/public/src/client/topic.js index c88af60237..3651c8be2b 100644 --- a/public/src/client/topic.js +++ b/public/src/client/topic.js @@ -144,11 +144,11 @@ define('forum/topic', [ function handleBookmark(tid) { // use the user's bookmark data if available, fallback to local if available var bookmark = ajaxify.data.bookmark || storage.getItem('topic:' + tid + ':bookmark'); - var postIndex = getPostIndex(); + var postIndex = ajaxify.data.postIndex; - if (postIndex && window.location.search.indexOf('page=') === -1) { - if (components.get('post/anchor', postIndex).length) { - return navigator.scrollToPostIndex(postIndex, true, 0); + if (postIndex > 0) { + if (components.get('post/anchor', postIndex - 1).length) { + return navigator.scrollToPostIndex(postIndex - 1, true, 0); } } else if (bookmark && (!config.usePagination || (config.usePagination && ajaxify.data.pagination.currentPage === 1)) && ajaxify.data.postcount > ajaxify.data.bookmarkThreshold) { app.alert({ @@ -169,22 +169,6 @@ define('forum/topic', [ } } - function getPostIndex() { - var parts = window.location.pathname.split('/'); - var lastPart = parts[parts.length - 1]; - if (lastPart && utils.isNumber(lastPart)) { - lastPart = Math.max(0, parseInt(lastPart, 10) - 1); - } else { - return 0; - } - - if (lastPart > 0 && !components.get('post/anchor', lastPart).length) { - return components.get('post/anchor').last().attr('name'); - } - - return lastPart; - } - function addBlockQuoteHandler() { components.get('topic').on('click', 'blockquote .toggle', function () { var blockQuote = $(this).parent('blockquote'); diff --git a/public/src/modules/navigator.js b/public/src/modules/navigator.js index 9ca7e94fa9..a7fd657cac 100644 --- a/public/src/modules/navigator.js +++ b/public/src/modules/navigator.js @@ -76,7 +76,7 @@ define('navigator', ['forum/pagination', 'components'], function (pagination, co index = 1; navigator.callback = null; navigator.selector = null; - $(window).off('scroll', navigator.update); + $(window).off('scroll', navigator.delayedUpdate); toggle(false); }; diff --git a/public/src/modules/notifications.js b/public/src/modules/notifications.js index ecbfaadd1b..928884b69a 100644 --- a/public/src/modules/notifications.js +++ b/public/src/modules/notifications.js @@ -10,29 +10,27 @@ define('notifications', ['sounds', 'translator', 'components'], function (sounds var notifContainer = components.get('notifications'); var notifTrigger = notifContainer.children('a'); var notifList = components.get('notifications/list'); - var notifIcon = components.get('notifications/icon'); - notifTrigger - .on('click', function (e) { - e.preventDefault(); - if (notifContainer.hasClass('open')) { - return; - } + notifTrigger.on('click', function (e) { + e.preventDefault(); + if (notifContainer.hasClass('open')) { + return; + } - Notifications.loadNotifications(notifList); - }); + Notifications.loadNotifications(notifList); + }); notifList.on('click', '[data-nid]', function () { var unread = $(this).hasClass('unread'); - var nid = $(this).attr('data-nid'); if (!unread) { return; } + var nid = $(this).attr('data-nid'); socket.emit('notifications.markRead', nid, function (err) { if (err) { return app.alertError(err.message); } - incrementNotifCount(-1); + if (unreadNotifs[nid]) { delete unreadNotifs[nid]; } @@ -52,7 +50,7 @@ define('notifications', ['sounds', 'translator', 'components'], function (sounds } liEl.toggleClass('unread'); - incrementNotifCount(unread ? -1 : 1); + if (unread && unreadNotifs[nid]) { delete unreadNotifs[nid]; } @@ -60,11 +58,6 @@ define('notifications', ['sounds', 'translator', 'components'], function (sounds return false; }); - function incrementNotifCount(delta) { - var count = parseInt(notifIcon.attr('data-content'), 10) + delta; - Notifications.updateNotifCount(count); - } - socket.on('event:new_notification', function (notifData) { // If a path is defined, show notif data, otherwise show generic data var payload = { @@ -164,7 +157,6 @@ define('notifications', ['sounds', 'translator', 'components'], function (sounds if (err) { app.alertError(err.message); } - Notifications.updateNotifCount(0); unreadNotifs = {}; }); }; diff --git a/src/controllers/topics.js b/src/controllers/topics.js index eb69d48222..c702ba90fc 100644 --- a/src/controllers/topics.js +++ b/src/controllers/topics.js @@ -262,6 +262,7 @@ topicsController.get = function (req, res, callback) { data.postDeleteDuration = parseInt(meta.config.postDeleteDuration, 10) || 0; data.scrollToMyPost = settings.scrollToMyPost; data.rssFeedUrl = nconf.get('relative_path') + '/topic/' + data.tid + '.rss'; + data.postIndex = req.params.post_index; data.pagination = pagination.create(currentPage, pageCount, req.query); data.pagination.rel.forEach(function (rel) { rel.href = nconf.get('url') + '/topic/' + data.slug + rel.href; diff --git a/src/notifications.js b/src/notifications.js index b11e7ed392..3004a68fc7 100644 --- a/src/notifications.js +++ b/src/notifications.js @@ -292,7 +292,9 @@ Notifications.markUnread = function (nid, uid, callback) { async.apply(db.sortedSetAdd, 'uid:' + uid + ':notifications:unread', notification.datetime, nid), ], next); }, - ], callback); + ], function (err) { + callback(err); + }); }; Notifications.markReadMultiple = function (nids, uid, callback) { diff --git a/src/socket.io/notifications.js b/src/socket.io/notifications.js index 80f2bb4e12..3a56ff8a55 100644 --- a/src/socket.io/notifications.js +++ b/src/socket.io/notifications.js @@ -1,5 +1,7 @@ 'use strict'; +var async = require('async'); + var user = require('../user'); var notifications = require('../notifications'); var SocketNotifs = module.exports; @@ -25,13 +27,37 @@ SocketNotifs.deleteAll = function (socket, data, callback) { }; SocketNotifs.markRead = function (socket, nid, callback) { - notifications.markRead(nid, socket.uid, callback); + async.waterfall([ + function (next) { + notifications.markRead(nid, socket.uid, next); + }, + function (next) { + user.notifications.pushCount(socket.uid); + next(); + }, + ], callback); }; SocketNotifs.markUnread = function (socket, nid, callback) { - notifications.markUnread(nid, socket.uid, callback); + async.waterfall([ + function (next) { + notifications.markUnread(nid, socket.uid, next); + }, + function (next) { + user.notifications.pushCount(socket.uid); + next(); + }, + ], callback); }; SocketNotifs.markAllRead = function (socket, data, callback) { - notifications.markAllRead(socket.uid, callback); + async.waterfall([ + function (next) { + notifications.markAllRead(socket.uid, next); + }, + function (next) { + user.notifications.pushCount(socket.uid); + next(); + }, + ], callback); };