mirror of
				https://github.com/NodeBB/NodeBB.git
				synced 2025-10-31 19:15:58 +01:00 
			
		
		
		
	| @@ -35,7 +35,7 @@ define('forum/category', ['composer', 'forum/pagination', 'forum/infinitescroll' | |||||||
| 		enableInfiniteLoadingOrPagination(); | 		enableInfiniteLoadingOrPagination(); | ||||||
|  |  | ||||||
| 		if (!config.usePagination) { | 		if (!config.usePagination) { | ||||||
| 			navigator.init('#topics-container > .category-item', ajaxify.variables.get('topic_count'), undefined, Category.toTop, Category.toBottom); | 			navigator.init('#topics-container > .category-item', ajaxify.variables.get('topic_count'), Category.toTop, Category.toBottom, Category.navigatorCallback); | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		$('#topics-container').on('click', '.topic-title', function() { | 		$('#topics-container').on('click', '.topic-title', function() { | ||||||
| @@ -60,6 +60,10 @@ define('forum/category', ['composer', 'forum/pagination', 'forum/infinitescroll' | |||||||
| 		}); | 		}); | ||||||
| 	}; | 	}; | ||||||
|  |  | ||||||
|  | 	Category.navigatorCallback = function(element, elementCount) { | ||||||
|  | 		return parseInt(element.attr('data-index'), 10) + 1; | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	$(window).on('action:popstate', function(ev, data) { | 	$(window).on('action:popstate', function(ev, data) { | ||||||
| 		if(data.url.indexOf('category/') === 0) { | 		if(data.url.indexOf('category/') === 0) { | ||||||
| 			var bookmark = localStorage.getItem('category:bookmark'); | 			var bookmark = localStorage.getItem('category:bookmark'); | ||||||
| @@ -285,8 +289,7 @@ define('forum/category', ['composer', 'forum/pagination', 'forum/infinitescroll' | |||||||
| 			return; | 			return; | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		infinitescroll.calculateAfter(direction, '#topics-container .category-item[data-tid]', config.topicsPerPage, function(after, offset, el) { | 		infinitescroll.calculateAfter(direction, '#topics-container .category-item[data-tid]', config.topicsPerPage, false, function(after, offset, el) { | ||||||
|  |  | ||||||
| 			loadTopicsAfter(after, function() { | 			loadTopicsAfter(after, function() { | ||||||
| 				if (direction < 0 && el) { | 				if (direction < 0 && el) { | ||||||
| 					Category.scrollToTopic(el.attr('data-tid'), null, 0, offset); | 					Category.scrollToTopic(el.attr('data-tid'), null, 0, offset); | ||||||
|   | |||||||
| @@ -55,13 +55,16 @@ define('forum/infinitescroll', function() { | |||||||
| 		}); | 		}); | ||||||
| 	}; | 	}; | ||||||
|  |  | ||||||
| 	scroll.calculateAfter = function(direction, selector, count, callback) { | 	scroll.calculateAfter = function(direction, selector, count, reverse, callback) { | ||||||
| 		var after = 0, | 		var after = 0, | ||||||
| 			offset = 0, | 			offset = 0, | ||||||
| 			el = direction > 0 ? $(selector).last() : $(selector).first(); | 			el = direction > 0 ? $(selector).last() : $(selector).first(); | ||||||
|  |  | ||||||
|  | 		var count = reverse ? -count : count; | ||||||
|  | 		var increment = reverse ? -1 : 1; | ||||||
|  |  | ||||||
| 		if (direction > 0) { | 		if (direction > 0) { | ||||||
| 			after = parseInt(el.attr('data-index'), 10) + 1; | 			after = parseInt(el.attr('data-index'), 10) + increment; | ||||||
| 		} else { | 		} else { | ||||||
| 			after = parseInt(el.attr('data-index'), 10); | 			after = parseInt(el.attr('data-index'), 10); | ||||||
| 			if (isNaN(after)) { | 			if (isNaN(after)) { | ||||||
|   | |||||||
| @@ -63,7 +63,7 @@ define('forum/topic', dependencies, function(pagination, infinitescroll, threadT | |||||||
|  |  | ||||||
| 		handleBookmark(tid); | 		handleBookmark(tid); | ||||||
|  |  | ||||||
| 		navigator.init('.posts > .post-row', postCount, Topic.navigatorCallback, Topic.toTop, Topic.toBottom); | 		navigator.init('.posts > .post-row', postCount, Topic.toTop, Topic.toBottom, Topic.navigatorCallback, Topic.calculateIndex); | ||||||
|  |  | ||||||
| 		socket.on('event:new_post', onNewPost); | 		socket.on('event:new_post', onNewPost); | ||||||
| 		socket.on('event:new_notification', onNewNotification); | 		socket.on('event:new_notification', onNewNotification); | ||||||
| @@ -80,8 +80,11 @@ define('forum/topic', dependencies, function(pagination, infinitescroll, threadT | |||||||
| 	}; | 	}; | ||||||
|  |  | ||||||
| 	Topic.toBottom = function() { | 	Topic.toBottom = function() { | ||||||
| 		socket.emit('topics.lastPostIndex', ajaxify.variables.get('topic_id'), function(err, index) { | 		socket.emit('topics.postcount', ajaxify.variables.get('topic_id'), function(err, postCount) { | ||||||
| 			navigator.scrollBottom(index); | 			if (config.topicPostSort !== 'oldest_to_newest') { | ||||||
|  | 				postCount = 1; | ||||||
|  | 			} | ||||||
|  | 			navigator.scrollBottom(postCount); | ||||||
| 		}); | 		}); | ||||||
| 	}; | 	}; | ||||||
|  |  | ||||||
| @@ -201,8 +204,23 @@ define('forum/topic', dependencies, function(pagination, infinitescroll, threadT | |||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	Topic.navigatorCallback = function(element) { | 	Topic.calculateIndex = function(index, elementCount) { | ||||||
|  | 		if (index !== 1 && config.topicPostSort !== 'oldest_to_newest') { | ||||||
|  | 			return elementCount - index + 2; | ||||||
|  | 		} | ||||||
|  | 		return index; | ||||||
|  | 	}; | ||||||
|  |  | ||||||
|  | 	Topic.navigatorCallback = function(element, elementCount) { | ||||||
| 		var postIndex = parseInt(element.attr('data-index'), 10); | 		var postIndex = parseInt(element.attr('data-index'), 10); | ||||||
|  | 		var index = postIndex + 1; | ||||||
|  | 		if (config.topicPostSort !== 'oldest_to_newest') { | ||||||
|  | 			if (postIndex === 0) { | ||||||
|  | 				index = 1; | ||||||
|  | 			} else  { | ||||||
|  | 				index = Math.max(elementCount - postIndex + 1, 1); | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  |  | ||||||
| 		var currentBookmark = localStorage.getItem('topic:' + ajaxify.variables.get('topic_id') + ':bookmark'); | 		var currentBookmark = localStorage.getItem('topic:' + ajaxify.variables.get('topic_id') + ':bookmark'); | ||||||
|  |  | ||||||
| @@ -230,6 +248,7 @@ define('forum/topic', dependencies, function(pagination, infinitescroll, threadT | |||||||
| 				currentUrl = newUrl; | 				currentUrl = newUrl; | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
|  | 		return index; | ||||||
| 	}; | 	}; | ||||||
|  |  | ||||||
| 	function onNewPostPagination(data) { | 	function onNewPostPagination(data) { | ||||||
| @@ -247,8 +266,9 @@ define('forum/topic', dependencies, function(pagination, infinitescroll, threadT | |||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	function createNewPosts(data, callback) { | 	function createNewPosts(data, callback) { | ||||||
|  | 		callback = callback || function() {}; | ||||||
| 		if(!data || (data.posts && !data.posts.length)) { | 		if(!data || (data.posts && !data.posts.length)) { | ||||||
| 			return; | 			return callback(false); | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		function removeAlreadyAddedPosts() { | 		function removeAlreadyAddedPosts() { | ||||||
| @@ -297,7 +317,7 @@ define('forum/topic', dependencies, function(pagination, infinitescroll, threadT | |||||||
|  |  | ||||||
| 		removeAlreadyAddedPosts(); | 		removeAlreadyAddedPosts(); | ||||||
| 		if(!data.posts.length) { | 		if(!data.posts.length) { | ||||||
| 			return; | 			return callback(false); | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		findInsertionPoint(); | 		findInsertionPoint(); | ||||||
| @@ -320,9 +340,7 @@ define('forum/topic', dependencies, function(pagination, infinitescroll, threadT | |||||||
|  |  | ||||||
| 			$(window).trigger('action:posts.loaded'); | 			$(window).trigger('action:posts.loaded'); | ||||||
| 			onNewPostsLoaded(html, data.posts); | 			onNewPostsLoaded(html, data.posts); | ||||||
| 			if (typeof callback === 'function') { | 			callback(true); | ||||||
| 				callback(); |  | ||||||
| 			} |  | ||||||
| 		}); | 		}); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| @@ -373,7 +391,9 @@ define('forum/topic', dependencies, function(pagination, infinitescroll, threadT | |||||||
| 			return; | 			return; | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		infinitescroll.calculateAfter(direction, '#post-container .post-row[data-index!="0"]', config.postsPerPage, function(after, offset, el) { | 		var reverse = config.topicPostSort === 'newest_to_oldest' || config.topicPostSort === 'most_votes'; | ||||||
|  |  | ||||||
|  | 		infinitescroll.calculateAfter(direction, '#post-container .post-row[data-index!="0"]', config.postsPerPage, reverse, function(after, offset, el) { | ||||||
| 			loadPostsAfter(after, function() { | 			loadPostsAfter(after, function() { | ||||||
| 				if (direction < 0 && el) { | 				if (direction < 0 && el) { | ||||||
| 					navigator.scrollToPost(el.attr('data-index'), false, 0, offset); | 					navigator.scrollToPost(el.attr('data-index'), false, 0, offset); | ||||||
| @@ -401,9 +421,11 @@ define('forum/topic', dependencies, function(pagination, infinitescroll, threadT | |||||||
| 			indicatorEl.fadeOut(); | 			indicatorEl.fadeOut(); | ||||||
|  |  | ||||||
| 			if (data && data.posts && data.posts.length) { | 			if (data && data.posts && data.posts.length) { | ||||||
| 				createNewPosts(data, function() { | 				createNewPosts(data, function(postsCreated) { | ||||||
| 					done(); | 					done(); | ||||||
| 					callback(); | 					if (postsCreated) { | ||||||
|  | 						callback(); | ||||||
|  | 					} | ||||||
| 				}); | 				}); | ||||||
| 				hidePostToolsForDeletedPosts(); | 				hidePostToolsForDeletedPosts(); | ||||||
| 			} else { | 			} else { | ||||||
|   | |||||||
| @@ -11,8 +11,8 @@ define('navigator', ['forum/pagination'], function(pagination) { | |||||||
| 	var count = 0; | 	var count = 0; | ||||||
| 	navigator.scrollActive = false; | 	navigator.scrollActive = false; | ||||||
|  |  | ||||||
| 	navigator.init = function(selector, count, callback, toTop, toBottom) { | 	navigator.init = function(selector, count, toTop, toBottom, callback, calculateIndex) { | ||||||
|  | 		index = 1; | ||||||
| 		navigator.selector = selector; | 		navigator.selector = selector; | ||||||
| 		navigator.callback = callback; | 		navigator.callback = callback; | ||||||
| 		toTop = toTop || function() {}; | 		toTop = toTop || function() {}; | ||||||
| @@ -36,7 +36,13 @@ define('navigator', ['forum/pagination'], function(pagination) { | |||||||
| 					input.val(''); | 					input.val(''); | ||||||
| 					return; | 					return; | ||||||
| 				} | 				} | ||||||
| 				var url = generateUrl(input.val()); |  | ||||||
|  | 				var index = parseInt(input.val(), 10); | ||||||
|  | 				if (typeof calculateIndex === 'function') { | ||||||
|  | 					index = calculateIndex(index, count); | ||||||
|  | 				} | ||||||
|  |  | ||||||
|  | 				var url = generateUrl(index); | ||||||
| 				input.val(''); | 				input.val(''); | ||||||
| 				$('.pagination-block .dropdown-toggle').trigger('click'); | 				$('.pagination-block .dropdown-toggle').trigger('click'); | ||||||
| 				ajaxify.go(url); | 				ajaxify.go(url); | ||||||
| @@ -76,12 +82,9 @@ define('navigator', ['forum/pagination'], function(pagination) { | |||||||
| 			var el = $(this); | 			var el = $(this); | ||||||
|  |  | ||||||
| 			if (elementInView(el)) { | 			if (elementInView(el)) { | ||||||
| 				index = parseInt(el.attr('data-index'), 10) + 1; |  | ||||||
|  |  | ||||||
| 				navigator.updateTextAndProgressBar(); |  | ||||||
|  |  | ||||||
| 				if (typeof navigator.callback === 'function') { | 				if (typeof navigator.callback === 'function') { | ||||||
| 					navigator.callback(el); | 					index = navigator.callback(el, count); | ||||||
|  | 					navigator.updateTextAndProgressBar(); | ||||||
| 				} | 				} | ||||||
|  |  | ||||||
| 				return false; | 				return false; | ||||||
| @@ -147,7 +150,7 @@ define('navigator', ['forum/pagination'], function(pagination) { | |||||||
| 			return scrollToPid(postIndex, highlight, duration, offset); | 			return scrollToPid(postIndex, highlight, duration, offset); | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		if(config.usePagination) { | 		if (config.usePagination) { | ||||||
| 			if (window.location.search.indexOf('page') !== -1) { | 			if (window.location.search.indexOf('page') !== -1) { | ||||||
| 				navigator.update(); | 				navigator.update(); | ||||||
| 				return; | 				return; | ||||||
| @@ -162,12 +165,19 @@ define('navigator', ['forum/pagination'], function(pagination) { | |||||||
| 			} else { | 			} else { | ||||||
| 				scrollToPid(postIndex, highlight, duration, offset); | 				scrollToPid(postIndex, highlight, duration, offset); | ||||||
| 			} | 			} | ||||||
|  | 		} else { | ||||||
|  | 			navigator.scrollActive = false; | ||||||
| 		} | 		} | ||||||
| 	}; | 	}; | ||||||
|  |  | ||||||
| 	function scrollToPid(postIndex, highlight, duration, offset) { | 	function scrollToPid(postIndex, highlight, duration, offset) { | ||||||
| 		var scrollTo = $('#post_anchor_' + postIndex); | 		var scrollTo = $('#post_anchor_' + postIndex); | ||||||
|  |  | ||||||
|  | 		if (!scrollTo) { | ||||||
|  | 			navigator.scrollActive = false; | ||||||
|  | 			return; | ||||||
|  | 		} | ||||||
|  |  | ||||||
| 		var done = false; | 		var done = false; | ||||||
| 		function animateScroll() { | 		function animateScroll() { | ||||||
| 			$('html, body').animate({ | 			$('html, body').animate({ | ||||||
| @@ -195,7 +205,7 @@ define('navigator', ['forum/pagination'], function(pagination) { | |||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		if ($('#post-container').length && scrollTo.length) { | 		if ($('#post-container').length) { | ||||||
| 			animateScroll(); | 			animateScroll(); | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
|   | |||||||
| @@ -20,11 +20,11 @@ module.exports = function(Categories) { | |||||||
| 				return callback(err, []); | 				return callback(err, []); | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
| 			posts.getPostSummaryByPids(pids, {stripTags: true}, callback); | 			posts.getPostSummaryByPids(pids, uid, {stripTags: true}, callback); | ||||||
| 		}); | 		}); | ||||||
| 	}; | 	}; | ||||||
|  |  | ||||||
| 	Categories.getRecentTopicReplies = function(categoryData, callback) { | 	Categories.getRecentTopicReplies = function(categoryData, uid, callback) { | ||||||
| 		async.map(categoryData, getRecentTopicPids, function(err, results) { | 		async.map(categoryData, getRecentTopicPids, function(err, results) { | ||||||
| 			var pids = _.flatten(results); | 			var pids = _.flatten(results); | ||||||
|  |  | ||||||
| @@ -32,7 +32,7 @@ module.exports = function(Categories) { | |||||||
| 				return !!pid && array.indexOf(pid) === index; | 				return !!pid && array.indexOf(pid) === index; | ||||||
| 			}); | 			}); | ||||||
|  |  | ||||||
| 			posts.getPostSummaryByPids(pids, {stripTags: true}, function(err, posts) { | 			posts.getPostSummaryByPids(pids, uid, {stripTags: true}, function(err, posts) { | ||||||
| 				if (err) { | 				if (err) { | ||||||
| 					return callback(err); | 					return callback(err); | ||||||
| 				} | 				} | ||||||
|   | |||||||
| @@ -17,6 +17,7 @@ groupsController.list = function(req, res) { | |||||||
| }; | }; | ||||||
|  |  | ||||||
| groupsController.details = function(req, res) { | groupsController.details = function(req, res) { | ||||||
|  | 	var uid = req.user ? parseInt(req.user.uid, 10) : 0; | ||||||
| 	async.parallel({ | 	async.parallel({ | ||||||
| 		group: function(next) { | 		group: function(next) { | ||||||
| 			groups.get(req.params.name, { | 			groups.get(req.params.name, { | ||||||
| @@ -24,7 +25,7 @@ groupsController.details = function(req, res) { | |||||||
| 			}, next); | 			}, next); | ||||||
| 		}, | 		}, | ||||||
| 		posts: function(next) { | 		posts: function(next) { | ||||||
| 			groups.getLatestMemberPosts(req.params.name, 10, next); | 			groups.getLatestMemberPosts(req.params.name, 10, uid, next); | ||||||
| 		} | 		} | ||||||
| 	}, function(err, results) { | 	}, function(err, results) { | ||||||
| 		if (!err) { | 		if (!err) { | ||||||
|   | |||||||
| @@ -69,7 +69,7 @@ Controllers.home = function(req, res, next) { | |||||||
| 					return next(err); | 					return next(err); | ||||||
| 				} | 				} | ||||||
|  |  | ||||||
| 				categories.getRecentTopicReplies(categoryData, function(err) { | 				categories.getRecentTopicReplies(categoryData, uid, function(err) { | ||||||
| 					next(err, categoryData); | 					next(err, categoryData); | ||||||
| 				}); | 				}); | ||||||
| 			}); | 			}); | ||||||
|   | |||||||
| @@ -30,20 +30,18 @@ topicsController.get = function(req, res, next) { | |||||||
|  |  | ||||||
| 			userPrivileges = privileges; | 			userPrivileges = privileges; | ||||||
|  |  | ||||||
| 			user.getSettings(uid, next); | 			async.parallel({ | ||||||
|  | 				postCount: function(next) { | ||||||
|  | 					topics.getPostCount(tid, next); | ||||||
|  | 				}, | ||||||
|  | 				settings: function(next) { | ||||||
|  | 					user.getSettings(uid, next); | ||||||
|  | 				} | ||||||
|  | 			}, next); | ||||||
| 		}, | 		}, | ||||||
| 		function (settings, next) { | 		function (results, next) { | ||||||
| 			var postIndex = 0; | 			var settings = results.settings; | ||||||
| 			if (!settings.usePagination) { | 			var postCount = parseInt(results.postCount, 10) + 1; | ||||||
| 				postIndex = Math.max((req.params.post_index || 1) - (settings.postsPerPage - 1), 0); |  | ||||||
| 			} else if (!req.query.page) { |  | ||||||
| 				var index = Math.max(parseInt((req.params.post_index || 0), 10), 0); |  | ||||||
| 				page = Math.ceil((index + 1) / settings.postsPerPage); |  | ||||||
| 			} |  | ||||||
|  |  | ||||||
| 			var start = (page - 1) * settings.postsPerPage + postIndex, |  | ||||||
| 				end = start + settings.postsPerPage - 1; |  | ||||||
|  |  | ||||||
| 			var set = 'tid:' + tid + ':posts', | 			var set = 'tid:' + tid + ':posts', | ||||||
| 				reverse = false; | 				reverse = false; | ||||||
|  |  | ||||||
| @@ -54,6 +52,24 @@ topicsController.get = function(req, res, next) { | |||||||
| 				set = 'tid:' + tid + ':posts:votes'; | 				set = 'tid:' + tid + ':posts:votes'; | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
|  | 			var postIndex = 0; | ||||||
|  | 			if (!settings.usePagination) { | ||||||
|  | 				if (reverse) { | ||||||
|  | 					if (!req.params.post_index || parseInt(req.params.post_index, 10) === 1) { | ||||||
|  | 						req.params.post_index = 0; | ||||||
|  | 					} | ||||||
|  | 					postIndex = Math.max(postCount - (req.params.post_index || postCount) - (settings.postsPerPage - 1), 0); | ||||||
|  | 				} else { | ||||||
|  | 					postIndex = Math.max((req.params.post_index || 1) - (settings.postsPerPage + 1), 0); | ||||||
|  | 				} | ||||||
|  | 			} else if (!req.query.page) { | ||||||
|  | 				var index = Math.max(parseInt(req.params.post_index, 10), 0); | ||||||
|  | 				page = Math.ceil((index + 1) / settings.postsPerPage); | ||||||
|  | 			} | ||||||
|  |  | ||||||
|  | 			var start = (page - 1) * settings.postsPerPage + postIndex, | ||||||
|  | 				end = start + settings.postsPerPage - 1; | ||||||
|  |  | ||||||
| 			topics.getTopicWithPosts(tid, set, uid, start, end, reverse, function (err, topicData) { | 			topics.getTopicWithPosts(tid, set, uid, start, end, reverse, function (err, topicData) { | ||||||
| 				if (topicData) { | 				if (topicData) { | ||||||
| 					if (topicData.deleted && !userPrivileges.view_deleted) { | 					if (topicData.deleted && !userPrivileges.view_deleted) { | ||||||
| @@ -191,6 +207,7 @@ topicsController.get = function(req, res, next) { | |||||||
|  |  | ||||||
| topicsController.teaser = function(req, res, next) { | topicsController.teaser = function(req, res, next) { | ||||||
| 	var tid = req.params.topic_id; | 	var tid = req.params.topic_id; | ||||||
|  | 	var uid = req.user ? parseInt(req.user.uid, 10) : 0; | ||||||
| 	topics.getLatestUndeletedPid(tid, function(err, pid) { | 	topics.getLatestUndeletedPid(tid, function(err, pid) { | ||||||
| 		if (err) { | 		if (err) { | ||||||
| 			return next(err); | 			return next(err); | ||||||
| @@ -200,7 +217,7 @@ topicsController.teaser = function(req, res, next) { | |||||||
| 			return res.json(404, 'not-found'); | 			return res.json(404, 'not-found'); | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		posts.getPostSummaryByPids([pid], {stripTags: false}, function(err, posts) { | 		posts.getPostSummaryByPids([pid], uid, {stripTags: false}, function(err, posts) { | ||||||
| 			if (err) { | 			if (err) { | ||||||
| 				return next(err); | 				return next(err); | ||||||
| 			} | 			} | ||||||
|   | |||||||
| @@ -418,7 +418,7 @@ | |||||||
| 		}); | 		}); | ||||||
| 	}; | 	}; | ||||||
|  |  | ||||||
| 	Groups.getLatestMemberPosts = function(groupName, max, callback) { | 	Groups.getLatestMemberPosts = function(groupName, max, uid, callback) { | ||||||
| 		Groups.get(groupName, {}, function(err, groupObj) { | 		Groups.get(groupName, {}, function(err, groupObj) { | ||||||
| 			if (err || parseInt(groupObj.memberCount, 10) === 0) { | 			if (err || parseInt(groupObj.memberCount, 10) === 0) { | ||||||
| 				return callback(null, []); | 				return callback(null, []); | ||||||
| @@ -433,7 +433,7 @@ | |||||||
| 					return callback(err); | 					return callback(err); | ||||||
| 				} | 				} | ||||||
|  |  | ||||||
| 				posts.getPostSummaryByPids(pids, {stripTags: false}, callback); | 				posts.getPostSummaryByPids(pids, uid, {stripTags: false}, callback); | ||||||
| 			}); | 			}); | ||||||
| 		}); | 		}); | ||||||
| 	}; | 	}; | ||||||
|   | |||||||
							
								
								
									
										54
									
								
								src/posts.js
									
									
									
									
									
								
							
							
						
						
									
										54
									
								
								src/posts.js
									
									
									
									
									
								
							| @@ -186,7 +186,7 @@ var async = require('async'), | |||||||
| 				if (err) { | 				if (err) { | ||||||
| 					return callback(err); | 					return callback(err); | ||||||
| 				} | 				} | ||||||
| 				Posts.getPostSummaryByPids(pids, {stripTags: true}, callback); | 				Posts.getPostSummaryByPids(pids, uid, {stripTags: true}, callback); | ||||||
| 			}); | 			}); | ||||||
| 		}); | 		}); | ||||||
| 	}; | 	}; | ||||||
| @@ -270,7 +270,7 @@ var async = require('async'), | |||||||
| 		}); | 		}); | ||||||
| 	}; | 	}; | ||||||
|  |  | ||||||
| 	Posts.getPostSummaryByPids = function(pids, options, callback) { | 	Posts.getPostSummaryByPids = function(pids, uid, options, callback) { | ||||||
| 		options.stripTags = options.hasOwnProperty('stripTags') ? options.stripTags : false; | 		options.stripTags = options.hasOwnProperty('stripTags') ? options.stripTags : false; | ||||||
| 		options.parse = options.hasOwnProperty('parse') ? options.parse : true; | 		options.parse = options.hasOwnProperty('parse') ? options.parse : true; | ||||||
|  |  | ||||||
| @@ -319,13 +319,7 @@ var async = require('async'), | |||||||
| 					}); | 					}); | ||||||
| 				}, | 				}, | ||||||
| 				indices: function(next) { | 				indices: function(next) { | ||||||
| 					var pids = [], keys = []; | 					Posts.getPostIndices(posts, uid, next); | ||||||
| 					for (var i=0; i<posts.length; ++i) { |  | ||||||
| 						pids.push(posts[i].pid); |  | ||||||
| 						keys.push('tid:' + posts[i].tid + ':posts'); |  | ||||||
| 					} |  | ||||||
|  |  | ||||||
| 					db.sortedSetsRanks(keys, pids, next); |  | ||||||
| 				} | 				} | ||||||
| 			}, function(err, results) { | 			}, function(err, results) { | ||||||
| 				function toObject(key, data) { | 				function toObject(key, data) { | ||||||
| @@ -345,7 +339,7 @@ var async = require('async'), | |||||||
| 				results.categories = toObject('cid', results.topicsAndCategories.categories); | 				results.categories = toObject('cid', results.topicsAndCategories.categories); | ||||||
|  |  | ||||||
| 				for (var i=0; i<posts.length; ++i) { | 				for (var i=0; i<posts.length; ++i) { | ||||||
| 					posts[i].index = utils.isNumber(results.indices[i]) ? parseInt(results.indices[i], 10) + 2 : 1; | 					posts[i].index = utils.isNumber(results.indices[i]) ? parseInt(results.indices[i], 10) + 1 : 1; | ||||||
| 				} | 				} | ||||||
|  |  | ||||||
| 				posts = posts.filter(function(post) { | 				posts = posts.filter(function(post) { | ||||||
| @@ -486,7 +480,7 @@ var async = require('async'), | |||||||
| 				if (err) { | 				if (err) { | ||||||
| 					return callback(err); | 					return callback(err); | ||||||
| 				} | 				} | ||||||
| 				getPostsFromSet('uid:' + uid + ':posts', pids, callback); | 				getPostsFromSet('uid:' + uid + ':posts', pids, callerUid, callback); | ||||||
| 			}); | 			}); | ||||||
| 		}); | 		}); | ||||||
| 	}; | 	}; | ||||||
| @@ -497,16 +491,16 @@ var async = require('async'), | |||||||
| 				return callback(err); | 				return callback(err); | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
| 			getPostsFromSet('uid:' + uid + ':favourites', pids, callback); | 			getPostsFromSet('uid:' + uid + ':favourites', pids, uid, callback); | ||||||
| 		}); | 		}); | ||||||
| 	}; | 	}; | ||||||
|  |  | ||||||
| 	function getPostsFromSet(set, pids, callback) { | 	function getPostsFromSet(set, pids, uid, callback) { | ||||||
| 		if (!Array.isArray(pids) || !pids.length) { | 		if (!Array.isArray(pids) || !pids.length) { | ||||||
| 			return callback(null, {posts: [], nextStart: 0}); | 			return callback(null, {posts: [], nextStart: 0}); | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		Posts.getPostSummaryByPids(pids, {stripTags: false}, function(err, posts) { | 		Posts.getPostSummaryByPids(pids, uid, {stripTags: false}, function(err, posts) { | ||||||
| 			if (err) { | 			if (err) { | ||||||
| 				return callback(err); | 				return callback(err); | ||||||
| 			} | 			} | ||||||
| @@ -566,6 +560,38 @@ var async = require('async'), | |||||||
| 		}); | 		}); | ||||||
| 	}; | 	}; | ||||||
|  |  | ||||||
|  | 	Posts.getPostIndices = function(posts, uid, callback) { | ||||||
|  | 		user.getSettings(uid, function(err, settings) { | ||||||
|  | 			if (err) { | ||||||
|  | 				return callback(err); | ||||||
|  | 			} | ||||||
|  | 			var byVotes = settings.topicPostSort === 'most_votes'; | ||||||
|  | 			var sets = posts.map(function(post) { | ||||||
|  | 				if (byVotes) { | ||||||
|  | 					return 'tid:' + post.tid + ':posts:votes'; | ||||||
|  | 				} else { | ||||||
|  | 					return 'tid:' + post.tid + ':posts'; | ||||||
|  | 				} | ||||||
|  | 			}); | ||||||
|  |  | ||||||
|  | 			var pids = posts.map(function(post) { | ||||||
|  | 				return post.pid; | ||||||
|  | 			}); | ||||||
|  |  | ||||||
|  | 			db.sortedSetsRanks(sets, pids, function(err, indices) { | ||||||
|  | 				if (err) { | ||||||
|  | 					return callback(err); | ||||||
|  | 				} | ||||||
|  |  | ||||||
|  | 				for (var i=0; i<indices.length; ++i) { | ||||||
|  | 					indices[i] = utils.isNumber(indices[i]) ? parseInt(indices[i], 10) + 1 : 0; | ||||||
|  | 				} | ||||||
|  |  | ||||||
|  | 				callback(null, indices); | ||||||
|  | 			}); | ||||||
|  | 		}); | ||||||
|  | 	}; | ||||||
|  |  | ||||||
| 	Posts.isOwner = function(pid, uid, callback) { | 	Posts.isOwner = function(pid, uid, callback) { | ||||||
| 		uid = parseInt(uid, 10); | 		uid = parseInt(uid, 10); | ||||||
| 		if (Array.isArray(pid)) { | 		if (Array.isArray(pid)) { | ||||||
|   | |||||||
| @@ -50,7 +50,7 @@ search.search = function(term, uid, callback) { | |||||||
| 					return callback(err); | 					return callback(err); | ||||||
| 				} | 				} | ||||||
|  |  | ||||||
| 				posts.getPostSummaryByPids(pids, {stripTags: true, parse: false}, function(err, posts) { | 				posts.getPostSummaryByPids(pids, uid, {stripTags: true, parse: false}, function(err, posts) { | ||||||
| 					if (err) { | 					if (err) { | ||||||
| 						return callback(err); | 						return callback(err); | ||||||
| 					} | 					} | ||||||
|   | |||||||
| @@ -70,10 +70,6 @@ SocketTopics.postcount = function(socket, tid, callback) { | |||||||
| 	topics.getTopicField(tid, 'postcount', callback); | 	topics.getTopicField(tid, 'postcount', callback); | ||||||
| }; | }; | ||||||
|  |  | ||||||
| SocketTopics.lastPostIndex = function(socket, tid, callback) { |  | ||||||
| 	db.sortedSetCard('tid:' + tid + ':posts', callback); |  | ||||||
| }; |  | ||||||
|  |  | ||||||
| SocketTopics.increaseViewCount = function(socket, tid) { | SocketTopics.increaseViewCount = function(socket, tid) { | ||||||
| 	topics.increaseViewCount(tid); | 	topics.increaseViewCount(tid); | ||||||
| }; | }; | ||||||
| @@ -421,6 +417,9 @@ SocketTopics.loadMore = function(socket, data, callback) { | |||||||
| 		}, | 		}, | ||||||
| 		privileges: function(next) { | 		privileges: function(next) { | ||||||
| 			privileges.topics.get(data.tid, socket.uid, next); | 			privileges.topics.get(data.tid, socket.uid, next); | ||||||
|  | 		}, | ||||||
|  | 		postCount: function(next) { | ||||||
|  | 			topics.getPostCount(data.tid, next); | ||||||
| 		} | 		} | ||||||
| 	}, function(err, results) { | 	}, function(err, results) { | ||||||
| 		if (err) { | 		if (err) { | ||||||
| @@ -431,19 +430,23 @@ SocketTopics.loadMore = function(socket, data, callback) { | |||||||
| 			return callback(new Error('[[error:no-privileges]]')); | 			return callback(new Error('[[error:no-privileges]]')); | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		var start = Math.max(parseInt(data.after, 10) - 1, 0), |  | ||||||
| 			end = start + results.settings.postsPerPage - 1; |  | ||||||
|  |  | ||||||
| 		var set = 'tid:' + data.tid + ':posts', | 		var set = 'tid:' + data.tid + ':posts', | ||||||
| 			reverse = false; | 			reverse = false, | ||||||
|  | 			start = Math.max(parseInt(data.after, 10) - 1, 0); | ||||||
|  |  | ||||||
| 		if (results.settings.topicPostSort === 'newest_to_oldest') { | 		if (results.settings.topicPostSort === 'newest_to_oldest') { | ||||||
| 			reverse = true; | 			reverse = true; | ||||||
|  | 			data.after = results.postCount - data.after; | ||||||
|  | 			start = Math.max(parseInt(data.after, 10), 0); | ||||||
| 		} else if (results.settings.topicPostSort === 'most_votes') { | 		} else if (results.settings.topicPostSort === 'most_votes') { | ||||||
| 			reverse = true; | 			reverse = true; | ||||||
|  | 			data.after = results.postCount - data.after; | ||||||
|  | 			start = Math.max(parseInt(data.after, 10), 0); | ||||||
| 			set = 'tid:' + data.tid + ':posts:votes'; | 			set = 'tid:' + data.tid + ':posts:votes'; | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
|  | 		var end = start + results.settings.postsPerPage - 1; | ||||||
|  |  | ||||||
| 		async.parallel({ | 		async.parallel({ | ||||||
| 			posts: function(next) { | 			posts: function(next) { | ||||||
| 				topics.getTopicPosts(data.tid, set, start, end, socket.uid, reverse, next); | 				topics.getTopicPosts(data.tid, set, start, end, socket.uid, reverse, next); | ||||||
|   | |||||||
| @@ -199,7 +199,7 @@ var async = require('async'), | |||||||
| 					privileges.categories.isAdminOrMod(cids, uid, next); | 					privileges.categories.isAdminOrMod(cids, uid, next); | ||||||
| 				}, | 				}, | ||||||
| 				teasers: function(next) { | 				teasers: function(next) { | ||||||
| 					Topics.getTeasers(tids, next); | 					Topics.getTeasers(tids, uid, next); | ||||||
| 				}, | 				}, | ||||||
| 				tags: function(next) { | 				tags: function(next) { | ||||||
| 					Topics.getTopicsTagsObjects(tids, next); | 					Topics.getTopicsTagsObjects(tids, next); | ||||||
| @@ -256,7 +256,7 @@ var async = require('async'), | |||||||
| 			} | 			} | ||||||
|  |  | ||||||
| 			async.parallel({ | 			async.parallel({ | ||||||
|  				posts: function(next) { | 				posts: function(next) { | ||||||
| 					posts.getPidsFromSet(set, start, end, reverse, function(err, pids) { | 					posts.getPidsFromSet(set, start, end, reverse, function(err, pids) { | ||||||
| 						if (err) { | 						if (err) { | ||||||
| 							return next(err); | 							return next(err); | ||||||
| @@ -270,11 +270,7 @@ var async = require('async'), | |||||||
| 							if (err) { | 							if (err) { | ||||||
| 								return next(err); | 								return next(err); | ||||||
| 							} | 							} | ||||||
| 							start = parseInt(start, 10); |  | ||||||
| 							for(var i=0; i<posts.length; ++i) { |  | ||||||
| 								posts[i].index = start + i; |  | ||||||
| 							} |  | ||||||
| 							posts[0].index = 0; |  | ||||||
| 							Topics.addPostData(posts, uid, next); | 							Topics.addPostData(posts, uid, next); | ||||||
| 						}); | 						}); | ||||||
| 					}); | 					}); | ||||||
| @@ -332,7 +328,7 @@ var async = require('async'), | |||||||
| 		}); | 		}); | ||||||
| 	}; | 	}; | ||||||
|  |  | ||||||
| 	Topics.getTeasers = function(tids, callback) { | 	Topics.getTeasers = function(tids, uid, callback) { | ||||||
| 		if(!Array.isArray(tids)) { | 		if(!Array.isArray(tids)) { | ||||||
| 			return callback(null, []); | 			return callback(null, []); | ||||||
| 		} | 		} | ||||||
| @@ -350,51 +346,41 @@ var async = require('async'), | |||||||
| 				return 'post:' + pid; | 				return 'post:' + pid; | ||||||
| 			}); | 			}); | ||||||
|  |  | ||||||
| 			async.parallel({ | 			db.getObjectsFields(postKeys, ['pid', 'uid', 'timestamp', 'tid'], function(err, postData) { | ||||||
| 				indices: function(next) { |  | ||||||
| 					var sets = tids.map(function(tid) { |  | ||||||
| 						return 'tid:' + tid + ':posts'; |  | ||||||
| 					}); |  | ||||||
| 					db.sortedSetsRanks(sets, pids, next); |  | ||||||
| 				}, |  | ||||||
| 				posts: function(next) { |  | ||||||
| 					db.getObjectsFields(postKeys, ['pid', 'uid', 'timestamp'], next); |  | ||||||
| 				} |  | ||||||
| 			}, function(err, results) { |  | ||||||
| 				if (err) { | 				if (err) { | ||||||
| 					return callback(err); | 					return callback(err); | ||||||
| 				} | 				} | ||||||
|  |  | ||||||
| 				var indices = results.indices.map(function(index) { | 				var uids = postData.map(function(post) { | ||||||
| 					if (!utils.isNumber(index)) { |  | ||||||
| 						return 1; |  | ||||||
| 					} |  | ||||||
| 					return parseInt(index, 10) + 2; |  | ||||||
| 				}); |  | ||||||
|  |  | ||||||
| 				var uids = results.posts.map(function(post) { |  | ||||||
| 					return post.uid; | 					return post.uid; | ||||||
| 				}).filter(function(uid, index, array) { | 				}).filter(function(uid, index, array) { | ||||||
| 					return array.indexOf(uid) === index; | 					return array.indexOf(uid) === index; | ||||||
| 				}); | 				}); | ||||||
|  |  | ||||||
| 				user.getMultipleUserFields(uids, ['uid', 'username', 'userslug', 'picture'], function(err, userData) { | 				async.parallel({ | ||||||
|  | 					users: function(next) { | ||||||
|  | 						user.getMultipleUserFields(uids, ['uid', 'username', 'userslug', 'picture'], next); | ||||||
|  | 					}, | ||||||
|  | 					indices: function(next) { | ||||||
|  | 						posts.getPostIndices(postData, uid, next); | ||||||
|  | 					} | ||||||
|  | 				}, function(err, results) { | ||||||
| 					if (err) { | 					if (err) { | ||||||
| 						return callback(err); | 						return callback(err); | ||||||
| 					} | 					} | ||||||
|  |  | ||||||
| 					var users = {}; | 					var users = {}; | ||||||
| 					userData.forEach(function(user) { | 					results.users.forEach(function(user) { | ||||||
| 						users[user.uid] = user; | 						users[user.uid] = user; | ||||||
| 					}); | 					}); | ||||||
|  |  | ||||||
| 					results.posts.forEach(function(post, index) { | 					postData.forEach(function(post, index) { | ||||||
| 						post.user = users[post.uid]; | 						post.user = users[post.uid]; | ||||||
| 						post.index = indices[index]; | 						post.index = results.indices[index] + 1; | ||||||
| 						post.timestamp = utils.toISOString(post.timestamp); | 						post.timestamp = utils.toISOString(post.timestamp); | ||||||
| 					}); | 					}); | ||||||
|  |  | ||||||
| 					callback(err, results.posts); | 					callback(null, postData); | ||||||
| 				}); | 				}); | ||||||
| 			}); | 			}); | ||||||
| 		}); | 		}); | ||||||
|   | |||||||
| @@ -32,11 +32,6 @@ module.exports = function(Topics) { | |||||||
| 				return callback(null, []); | 				return callback(null, []); | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
| 			start = parseInt(start, 10); |  | ||||||
| 			for(var i=0; i<postData.length; ++i) { |  | ||||||
| 				postData[i].index = start + i + 1; |  | ||||||
| 			} |  | ||||||
|  |  | ||||||
| 			Topics.addPostData(postData, uid, callback); | 			Topics.addPostData(postData, uid, callback); | ||||||
| 		}); | 		}); | ||||||
| 	}; | 	}; | ||||||
| @@ -96,6 +91,9 @@ module.exports = function(Topics) { | |||||||
| 			}, | 			}, | ||||||
| 			privileges: function(next) { | 			privileges: function(next) { | ||||||
| 				privileges.posts.get(pids, uid, next); | 				privileges.posts.get(pids, uid, next); | ||||||
|  | 			}, | ||||||
|  | 			indices: function(next) { | ||||||
|  | 				posts.getPostIndices(postData, uid, next); | ||||||
| 			} | 			} | ||||||
| 		}, function(err, results) { | 		}, function(err, results) { | ||||||
| 			if(err) { | 			if(err) { | ||||||
| @@ -103,6 +101,7 @@ module.exports = function(Topics) { | |||||||
| 			} | 			} | ||||||
|  |  | ||||||
| 			for (var i = 0; i < postData.length; ++i) { | 			for (var i = 0; i < postData.length; ++i) { | ||||||
|  | 				postData[i].index = results.indices[i]; | ||||||
| 				postData[i].deleted = parseInt(postData[i].deleted, 10) === 1; | 				postData[i].deleted = parseInt(postData[i].deleted, 10) === 1; | ||||||
| 				postData[i].user = results.userData[postData[i].uid]; | 				postData[i].user = results.userData[postData[i].uid]; | ||||||
| 				postData[i].editor = postData[i].editor ? results.editors[postData[i].editor] : null; | 				postData[i].editor = postData[i].editor ? results.editors[postData[i].editor] : null; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user