mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-30 18:46:01 +01:00
Merge pull request #3346 from bdharrington7/bookmark2
Saves user bookmarks on server if available
This commit is contained in:
@@ -116,7 +116,8 @@ define('forum/topic', [
|
|||||||
};
|
};
|
||||||
|
|
||||||
function handleBookmark(tid) {
|
function handleBookmark(tid) {
|
||||||
var bookmark = localStorage.getItem('topic:' + tid + ':bookmark');
|
// use the user's bookmark data if available, fallback to local if available
|
||||||
|
var bookmark = ajaxify.data.bookmark || localStorage.getItem('topic:' + tid + ':bookmark');
|
||||||
var postIndex = getPostIndex();
|
var postIndex = getPostIndex();
|
||||||
|
|
||||||
if (postIndex && window.location.search.indexOf('page=') === -1) {
|
if (postIndex && window.location.search.indexOf('page=') === -1) {
|
||||||
@@ -128,7 +129,7 @@ define('forum/topic', [
|
|||||||
timeout: 0,
|
timeout: 0,
|
||||||
type: 'info',
|
type: 'info',
|
||||||
clickfn : function() {
|
clickfn : function() {
|
||||||
navigator.scrollToPost(parseInt(bookmark, 10), true);
|
navigator.scrollToPost(parseInt(bookmark - 1, 10), true);
|
||||||
},
|
},
|
||||||
closefn : function() {
|
closefn : function() {
|
||||||
localStorage.removeItem('topic:' + tid + ':bookmark');
|
localStorage.removeItem('topic:' + tid + ':bookmark');
|
||||||
@@ -197,10 +198,28 @@ define('forum/topic', [
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var currentBookmark = localStorage.getItem('topic:' + ajaxify.data.tid + ':bookmark');
|
var bookmarkKey = 'topic:' + ajaxify.data.tid + ':bookmark';
|
||||||
|
var currentBookmark = ajaxify.data.bookmark || localStorage.getItem(bookmarkKey);
|
||||||
|
|
||||||
|
if (!currentBookmark || parseInt(postIndex, 10) > parseInt(currentBookmark, 10)) {
|
||||||
|
if (app.user.uid) {
|
||||||
|
var payload = {
|
||||||
|
'tid': ajaxify.data.tid,
|
||||||
|
'index': postIndex
|
||||||
|
};
|
||||||
|
socket.emit('topics.bookmark', payload, function(err) {
|
||||||
|
if (err) {
|
||||||
|
console.warn('Error saving bookmark:', err);
|
||||||
|
}
|
||||||
|
ajaxify.data.bookmark = postIndex;
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
localStorage.setItem(bookmarkKey, postIndex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// removes the bookmark alert when we get to / past the bookmark
|
||||||
if (!currentBookmark || parseInt(postIndex, 10) >= parseInt(currentBookmark, 10)) {
|
if (!currentBookmark || parseInt(postIndex, 10) >= parseInt(currentBookmark, 10)) {
|
||||||
localStorage.setItem('topic:' + ajaxify.data.tid + ':bookmark', postIndex);
|
|
||||||
app.removeAlert('bookmark');
|
app.removeAlert('bookmark');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -96,6 +96,10 @@ SocketTopics.postcount = function(socket, tid, callback) {
|
|||||||
topics.getTopicField(tid, 'postcount', callback);
|
topics.getTopicField(tid, 'postcount', callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
SocketTopics.bookmark = function(socket, payload, callback) {
|
||||||
|
topics.setUserBookmark(payload.tid, socket.uid, payload.index, callback);
|
||||||
|
};
|
||||||
|
|
||||||
SocketTopics.markAsRead = function(socket, tids, callback) {
|
SocketTopics.markAsRead = function(socket, tids, callback) {
|
||||||
if (!Array.isArray(tids) || !socket.uid) {
|
if (!Array.isArray(tids) || !socket.uid) {
|
||||||
return callback(new Error('[[error:invalid-data]]'));
|
return callback(new Error('[[error:invalid-data]]'));
|
||||||
|
|||||||
@@ -222,7 +222,8 @@ var async = require('async'),
|
|||||||
category: async.apply(Topics.getCategoryData, tid),
|
category: async.apply(Topics.getCategoryData, tid),
|
||||||
threadTools: async.apply(plugins.fireHook, 'filter:topic.thread_tools', {topic: topicData, uid: uid, tools: []}),
|
threadTools: async.apply(plugins.fireHook, 'filter:topic.thread_tools', {topic: topicData, uid: uid, tools: []}),
|
||||||
tags: async.apply(Topics.getTopicTagsObjects, tid),
|
tags: async.apply(Topics.getTopicTagsObjects, tid),
|
||||||
isFollowing: async.apply(Topics.isFollowing, [tid], uid)
|
isFollowing: async.apply(Topics.isFollowing, [tid], uid),
|
||||||
|
bookmark: async.apply(Topics.getUserBookmark, tid, uid)
|
||||||
}, function(err, results) {
|
}, function(err, results) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return callback(err);
|
return callback(err);
|
||||||
@@ -233,6 +234,7 @@ var async = require('async'),
|
|||||||
topicData.thread_tools = results.threadTools.tools;
|
topicData.thread_tools = results.threadTools.tools;
|
||||||
topicData.tags = results.tags;
|
topicData.tags = results.tags;
|
||||||
topicData.isFollowing = results.isFollowing[0];
|
topicData.isFollowing = results.isFollowing[0];
|
||||||
|
topicData.bookmark = results.bookmark;
|
||||||
|
|
||||||
topicData.unreplied = parseInt(topicData.postcount, 10) === 1;
|
topicData.unreplied = parseInt(topicData.postcount, 10) === 1;
|
||||||
topicData.deleted = parseInt(topicData.deleted, 10) === 1;
|
topicData.deleted = parseInt(topicData.deleted, 10) === 1;
|
||||||
@@ -328,6 +330,14 @@ var async = require('async'),
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Topics.getUserBookmark = function (tid, uid, callback) {
|
||||||
|
db.sortedSetScore('topic:' + tid + ':bookmarks', uid, callback);
|
||||||
|
};
|
||||||
|
|
||||||
|
Topics.setUserBookmark = function(tid, uid, index, callback) {
|
||||||
|
db.sortedSetAdd('topic:' + tid + ':bookmarks', index, uid, callback);
|
||||||
|
};
|
||||||
|
|
||||||
Topics.getTopicField = function(tid, field, callback) {
|
Topics.getTopicField = function(tid, field, callback) {
|
||||||
db.getObjectField('topic:' + tid, field, callback);
|
db.getObjectField('topic:' + tid, field, callback);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user