diff --git a/public/src/client/topic.js b/public/src/client/topic.js index 1cdd1cabbc..c25a910431 100644 --- a/public/src/client/topic.js +++ b/public/src/client/topic.js @@ -197,10 +197,19 @@ define('forum/topic', [ } } - var currentBookmark = localStorage.getItem('topic:' + ajaxify.data.tid + ':bookmark'); - - if (!currentBookmark || parseInt(postIndex, 10) >= parseInt(currentBookmark, 10)) { - localStorage.setItem('topic:' + ajaxify.data.tid + ':bookmark', postIndex); + var bookmarkKey = 'topic:' + ajaxify.data.tid + ':bookmark'; + var currentBookmark = localStorage.getItem(bookmarkKey); + + if (!currentBookmark || parseInt(postIndex, 10) > parseInt(currentBookmark, 10)) { + localStorage.setItem(bookmarkKey, postIndex); + if (app.user.uid) { + var data = { + 'tid': ajaxify.data.tid, + 'uid': app.user.uid, + 'postIndex': postIndex + } + socket.emit('topics.bookmark', data); + } app.removeAlert('bookmark'); } diff --git a/src/socket.io/topics.js b/src/socket.io/topics.js index 6e9cafcf63..02e092aad8 100644 --- a/src/socket.io/topics.js +++ b/src/socket.io/topics.js @@ -96,6 +96,11 @@ SocketTopics.postcount = function(socket, tid, callback) { topics.getTopicField(tid, 'postcount', callback); }; +SocketTopics.bookmark = function(socket, data, callback) { + // data contains tid, uid, and postIndex + topics.setUserBookmark(data, callback); +} + SocketTopics.markAsRead = function(socket, tids, callback) { if(!Array.isArray(tids) || !socket.uid) { return callback(new Error('[[error:invalid-data]]')); diff --git a/src/topics.js b/src/topics.js index 13522aad00..36a7dc89b1 100644 --- a/src/topics.js +++ b/src/topics.js @@ -327,6 +327,14 @@ var async = require('async'), }); } + Topics.getUserBookmark = function (tid, uid, callback) { + Topics.getTopicField(tid + ':bookmarks', uid, callback); + } + + Topics.setUserBookmark = function(data, callback) { + Topics.setTopicField(data.tid + ':bookmarks', data.uid, data.postIndex, callback); + } + Topics.getTopicField = function(tid, field, callback) { db.getObjectField('topic:' + tid, field, callback); };