mirror of
				https://github.com/NodeBB/NodeBB.git
				synced 2025-10-31 11:05:54 +01:00 
			
		
		
		
	feat: load user posts/topics via xhr on infinitescroll
This commit is contained in:
		| @@ -9,7 +9,7 @@ define('forum/account/best', ['forum/account/header', 'forum/account/posts'], fu | |||||||
|  |  | ||||||
| 		$('[component="post/content"] img:not(.not-responsive)').addClass('img-responsive'); | 		$('[component="post/content"] img:not(.not-responsive)').addClass('img-responsive'); | ||||||
|  |  | ||||||
| 		posts.handleInfiniteScroll('posts.loadMoreBestPosts', 'account/best'); | 		posts.handleInfiniteScroll('account/best'); | ||||||
| 	}; | 	}; | ||||||
|  |  | ||||||
| 	return Best; | 	return Best; | ||||||
|   | |||||||
| @@ -9,7 +9,7 @@ define('forum/account/bookmarks', ['forum/account/header', 'forum/account/posts' | |||||||
|  |  | ||||||
| 		$('[component="post/content"] img:not(.not-responsive)').addClass('img-responsive'); | 		$('[component="post/content"] img:not(.not-responsive)').addClass('img-responsive'); | ||||||
|  |  | ||||||
| 		posts.handleInfiniteScroll('posts.loadMoreBookmarks', 'account/bookmarks'); | 		posts.handleInfiniteScroll('account/bookmarks'); | ||||||
| 	}; | 	}; | ||||||
|  |  | ||||||
| 	return Bookmarks; | 	return Bookmarks; | ||||||
|   | |||||||
| @@ -9,7 +9,7 @@ define('forum/account/downvoted', ['forum/account/header', 'forum/account/posts' | |||||||
|  |  | ||||||
| 		$('[component="post/content"] img:not(.not-responsive)').addClass('img-responsive'); | 		$('[component="post/content"] img:not(.not-responsive)').addClass('img-responsive'); | ||||||
|  |  | ||||||
| 		posts.handleInfiniteScroll('posts.loadMoreDownVotedPosts', 'account/downvoted'); | 		posts.handleInfiniteScroll('account/downvoted'); | ||||||
| 	}; | 	}; | ||||||
|  |  | ||||||
| 	return Downvoted; | 	return Downvoted; | ||||||
|   | |||||||
							
								
								
									
										13
									
								
								public/src/client/account/ignored.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								public/src/client/account/ignored.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,13 @@ | |||||||
|  | 'use strict'; | ||||||
|  |  | ||||||
|  | define('forum/account/ignored', ['forum/account/header', 'forum/account/topics'], function (header, topics) { | ||||||
|  | 	var AccountIgnored = {}; | ||||||
|  |  | ||||||
|  | 	AccountIgnored.init = function () { | ||||||
|  | 		header.init(); | ||||||
|  |  | ||||||
|  | 		topics.handleInfiniteScroll('account/ignored'); | ||||||
|  | 	}; | ||||||
|  |  | ||||||
|  | 	return AccountIgnored; | ||||||
|  | }); | ||||||
| @@ -3,20 +3,21 @@ | |||||||
|  |  | ||||||
| define('forum/account/posts', ['forum/account/header', 'forum/infinitescroll'], function (header, infinitescroll) { | define('forum/account/posts', ['forum/account/header', 'forum/infinitescroll'], function (header, infinitescroll) { | ||||||
| 	var AccountPosts = {}; | 	var AccountPosts = {}; | ||||||
| 	var method; |  | ||||||
| 	var template; | 	var template; | ||||||
|  | 	var page = 1; | ||||||
|  |  | ||||||
| 	AccountPosts.init = function () { | 	AccountPosts.init = function () { | ||||||
| 		header.init(); | 		header.init(); | ||||||
|  |  | ||||||
| 		$('[component="post/content"] img:not(.not-responsive)').addClass('img-responsive'); | 		$('[component="post/content"] img:not(.not-responsive)').addClass('img-responsive'); | ||||||
|  |  | ||||||
| 		AccountPosts.handleInfiniteScroll('posts.loadMoreUserPosts', 'account/posts'); | 		AccountPosts.handleInfiniteScroll('account/posts'); | ||||||
| 	}; | 	}; | ||||||
|  |  | ||||||
| 	AccountPosts.handleInfiniteScroll = function (_method, _template) { | 	AccountPosts.handleInfiniteScroll = function (_template) { | ||||||
| 		method = _method; |  | ||||||
| 		template = _template; | 		template = _template; | ||||||
|  | 		page = ajaxify.data.pagination.currentPage; | ||||||
| 		if (!config.usePagination) { | 		if (!config.usePagination) { | ||||||
| 			infinitescroll.init(loadMore); | 			infinitescroll.init(loadMore); | ||||||
| 		} | 		} | ||||||
| @@ -26,17 +27,16 @@ define('forum/account/posts', ['forum/account/header', 'forum/infinitescroll'], | |||||||
| 		if (direction < 0) { | 		if (direction < 0) { | ||||||
| 			return; | 			return; | ||||||
| 		} | 		} | ||||||
|  | 		var params = utils.params(); | ||||||
|  | 		page += 1; | ||||||
|  | 		params.page = page; | ||||||
|  |  | ||||||
| 		infinitescroll.loadMore(method, { | 		infinitescroll.loadMoreXhr(params, function (data, done) { | ||||||
| 			uid: ajaxify.data.theirid, |  | ||||||
| 			after: $('[component="posts"]').attr('data-nextstart'), |  | ||||||
| 		}, function (data, done) { |  | ||||||
| 			if (data.posts && data.posts.length) { | 			if (data.posts && data.posts.length) { | ||||||
| 				onPostsLoaded(data.posts, done); | 				onPostsLoaded(data.posts, done); | ||||||
| 			} else { | 			} else { | ||||||
| 				done(); | 				done(); | ||||||
| 			} | 			} | ||||||
| 			$('[component="posts"]').attr('data-nextstart', data.nextStart); |  | ||||||
| 		}); | 		}); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|   | |||||||
| @@ -3,21 +3,19 @@ | |||||||
|  |  | ||||||
| define('forum/account/topics', ['forum/account/header', 'forum/infinitescroll'], function (header, infinitescroll) { | define('forum/account/topics', ['forum/account/header', 'forum/infinitescroll'], function (header, infinitescroll) { | ||||||
| 	var AccountTopics = {}; | 	var AccountTopics = {}; | ||||||
| 	var method; |  | ||||||
| 	var template; | 	var template; | ||||||
| 	var set; | 	var page = 1; | ||||||
|  |  | ||||||
| 	AccountTopics.init = function () { | 	AccountTopics.init = function () { | ||||||
| 		header.init(); | 		header.init(); | ||||||
|  |  | ||||||
| 		AccountTopics.handleInfiniteScroll('topics.loadMoreUserTopics', 'account/topics'); | 		AccountTopics.handleInfiniteScroll('account/topics'); | ||||||
| 	}; | 	}; | ||||||
|  |  | ||||||
| 	AccountTopics.handleInfiniteScroll = function (_method, _template, _set) { | 	AccountTopics.handleInfiniteScroll = function (_template) { | ||||||
| 		method = _method; |  | ||||||
| 		template = _template; | 		template = _template; | ||||||
| 		set = _set; | 		page = ajaxify.data.pagination.currentPage; | ||||||
|  |  | ||||||
| 		if (!config.usePagination) { | 		if (!config.usePagination) { | ||||||
| 			infinitescroll.init(loadMore); | 			infinitescroll.init(loadMore); | ||||||
| 		} | 		} | ||||||
| @@ -27,20 +25,16 @@ define('forum/account/topics', ['forum/account/header', 'forum/infinitescroll'], | |||||||
| 		if (direction < 0) { | 		if (direction < 0) { | ||||||
| 			return; | 			return; | ||||||
| 		} | 		} | ||||||
|  | 		var params = utils.params(); | ||||||
|  | 		page += 1; | ||||||
|  | 		params.page = page; | ||||||
|  |  | ||||||
| 		infinitescroll.loadMore(method, { | 		infinitescroll.loadMoreXhr(params, function (data, done) { | ||||||
| 			set: set, |  | ||||||
| 			uid: ajaxify.data.theirid, |  | ||||||
| 			after: $('[component="category"]').attr('data-nextstart'), |  | ||||||
| 			count: config.topicsPerPage, |  | ||||||
| 		}, function (data, done) { |  | ||||||
| 			if (data.topics && data.topics.length) { | 			if (data.topics && data.topics.length) { | ||||||
| 				onTopicsLoaded(data.topics, done); | 				onTopicsLoaded(data.topics, done); | ||||||
| 			} else { | 			} else { | ||||||
| 				done(); | 				done(); | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
| 			$('[component="category"]').attr('data-nextstart', data.nextStart); |  | ||||||
| 		}); | 		}); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|   | |||||||
| @@ -9,7 +9,7 @@ define('forum/account/upvoted', ['forum/account/header', 'forum/account/posts'], | |||||||
|  |  | ||||||
| 		$('[component="post/content"] img:not(.not-responsive)').addClass('img-responsive'); | 		$('[component="post/content"] img:not(.not-responsive)').addClass('img-responsive'); | ||||||
|  |  | ||||||
| 		posts.handleInfiniteScroll('posts.loadMoreUpVotedPosts', 'account/upvoted'); | 		posts.handleInfiniteScroll('account/upvoted'); | ||||||
| 	}; | 	}; | ||||||
|  |  | ||||||
| 	return Upvoted; | 	return Upvoted; | ||||||
|   | |||||||
| @@ -7,7 +7,7 @@ define('forum/account/watched', ['forum/account/header', 'forum/account/topics'] | |||||||
| 	AccountWatched.init = function () { | 	AccountWatched.init = function () { | ||||||
| 		header.init(); | 		header.init(); | ||||||
|  |  | ||||||
| 		topics.handleInfiniteScroll('topics.loadMoreFromSet', 'account/watched', 'uid:' + ajaxify.data.theirid + ':followed_tids'); | 		topics.handleInfiniteScroll('account/watched'); | ||||||
| 	}; | 	}; | ||||||
|  |  | ||||||
| 	return AccountWatched; | 	return AccountWatched; | ||||||
|   | |||||||
| @@ -78,6 +78,25 @@ define('forum/infinitescroll', function () { | |||||||
| 		}); | 		}); | ||||||
| 	}; | 	}; | ||||||
|  |  | ||||||
|  | 	scroll.loadMoreXhr = function (data, callback) { | ||||||
|  | 		if (loadingMore) { | ||||||
|  | 			return; | ||||||
|  | 		} | ||||||
|  | 		loadingMore = true; | ||||||
|  | 		var url = config.relative_path + '/api' + location.pathname; | ||||||
|  | 		var hookData = { url: url, data: data }; | ||||||
|  | 		$(window).trigger('action:infinitescroll.loadmore.xhr', hookData); | ||||||
|  |  | ||||||
|  | 		$.get(url, data, function (data) { | ||||||
|  | 			callback(data, function () { | ||||||
|  | 				loadingMore = false; | ||||||
|  | 			}); | ||||||
|  | 		}).fail(function (jqXHR) { | ||||||
|  | 			loadingMore = false; | ||||||
|  | 			app.alertError(String(jqXHR.responseJSON || jqXHR.statusText)); | ||||||
|  | 		}); | ||||||
|  | 	}; | ||||||
|  |  | ||||||
| 	scroll.removeExtra = function (els, direction, count) { | 	scroll.removeExtra = function (els, direction, count) { | ||||||
| 		if (els.length <= count) { | 		if (els.length <= count) { | ||||||
| 			return; | 			return; | ||||||
|   | |||||||
| @@ -6,7 +6,6 @@ const privileges = require('../privileges'); | |||||||
| const plugins = require('../plugins'); | const plugins = require('../plugins'); | ||||||
| const meta = require('../meta'); | const meta = require('../meta'); | ||||||
| const topics = require('../topics'); | const topics = require('../topics'); | ||||||
| const categories = require('../categories'); |  | ||||||
| const user = require('../user'); | const user = require('../user'); | ||||||
| const socketHelpers = require('./helpers'); | const socketHelpers = require('./helpers'); | ||||||
| const utils = require('../utils'); | const utils = require('../utils'); | ||||||
| @@ -101,41 +100,6 @@ SocketPosts.getPost = async function (socket, pid) { | |||||||
| 	return await api.posts.get(socket, { pid }); | 	return await api.posts.get(socket, { pid }); | ||||||
| }; | }; | ||||||
|  |  | ||||||
| SocketPosts.loadMoreBookmarks = async function (socket, data) { |  | ||||||
| 	return await loadMorePosts(`uid:${data.uid}:bookmarks`, socket.uid, data); |  | ||||||
| }; |  | ||||||
|  |  | ||||||
| SocketPosts.loadMoreUserPosts = async function (socket, data) { |  | ||||||
| 	const cids = await categories.getCidsByPrivilege('categories:cid', socket.uid, 'topics:read'); |  | ||||||
| 	const keys = cids.map(c => `cid:${c}:uid:${data.uid}:pids`); |  | ||||||
| 	return await loadMorePosts(keys, socket.uid, data); |  | ||||||
| }; |  | ||||||
|  |  | ||||||
| SocketPosts.loadMoreBestPosts = async function (socket, data) { |  | ||||||
| 	const cids = await categories.getCidsByPrivilege('categories:cid', socket.uid, 'topics:read'); |  | ||||||
| 	const keys = cids.map(c => `cid:${c}:uid:${data.uid}:pids:votes`); |  | ||||||
| 	return await loadMorePosts(keys, socket.uid, data); |  | ||||||
| }; |  | ||||||
|  |  | ||||||
| SocketPosts.loadMoreUpVotedPosts = async function (socket, data) { |  | ||||||
| 	return await loadMorePosts(`uid:${data.uid}:upvote`, socket.uid, data); |  | ||||||
| }; |  | ||||||
|  |  | ||||||
| SocketPosts.loadMoreDownVotedPosts = async function (socket, data) { |  | ||||||
| 	return await loadMorePosts(`uid:${data.uid}:downvote`, socket.uid, data); |  | ||||||
| }; |  | ||||||
|  |  | ||||||
| async function loadMorePosts(set, uid, data) { |  | ||||||
| 	if (!data || !utils.isNumber(data.uid) || !utils.isNumber(data.after)) { |  | ||||||
| 		throw new Error('[[error:invalid-data]]'); |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	const start = Math.max(0, parseInt(data.after, 10)); |  | ||||||
| 	const stop = start + 9; |  | ||||||
|  |  | ||||||
| 	return await posts.getPostSummariesFromSet(set, uid, start, stop); |  | ||||||
| } |  | ||||||
|  |  | ||||||
| SocketPosts.getCategory = async function (socket, pid) { | SocketPosts.getCategory = async function (socket, pid) { | ||||||
| 	return await posts.getCidByPid(pid); | 	return await posts.getCidByPid(pid); | ||||||
| }; | }; | ||||||
|   | |||||||
| @@ -1,7 +1,6 @@ | |||||||
| 'use strict'; | 'use strict'; | ||||||
|  |  | ||||||
| const topics = require('../../topics'); | const topics = require('../../topics'); | ||||||
| const categories = require('../../categories'); |  | ||||||
| const privileges = require('../../privileges'); | const privileges = require('../../privileges'); | ||||||
| const meta = require('../../meta'); | const meta = require('../../meta'); | ||||||
| const utils = require('../../utils'); | const utils = require('../../utils'); | ||||||
| @@ -92,12 +91,6 @@ module.exports = function (SocketTopics) { | |||||||
| 		return await topics.getTopicsFromSet(data.set, socket.uid, start, stop); | 		return await topics.getTopicsFromSet(data.set, socket.uid, start, stop); | ||||||
| 	}; | 	}; | ||||||
|  |  | ||||||
| 	SocketTopics.loadMoreUserTopics = async function (socket, data) { |  | ||||||
| 		const cids = await categories.getCidsByPrivilege('categories:cid', socket.uid, 'topics:read'); |  | ||||||
| 		data.set = cids.map(c => `cid:${c}:uid:${data.uid}:tids`); |  | ||||||
| 		return await SocketTopics.loadMoreFromSet(socket, data); |  | ||||||
| 	}; |  | ||||||
|  |  | ||||||
| 	function calculateStartStop(data) { | 	function calculateStartStop(data) { | ||||||
| 		const itemsPerPage = Math.min( | 		const itemsPerPage = Math.min( | ||||||
| 			meta.config.topicsPerPage || 20, | 			meta.config.topicsPerPage || 20, | ||||||
|   | |||||||
| @@ -927,53 +927,6 @@ describe('Post\'s', () => { | |||||||
| 			}); | 			}); | ||||||
| 		}); | 		}); | ||||||
|  |  | ||||||
| 		it('shold error with invalid data', (done) => { |  | ||||||
| 			socketPosts.loadMoreBookmarks({ uid: voterUid }, { uid: voterUid, after: null }, (err) => { |  | ||||||
| 				assert.equal(err.message, '[[error:invalid-data]]'); |  | ||||||
| 				done(); |  | ||||||
| 			}); |  | ||||||
| 		}); |  | ||||||
|  |  | ||||||
| 		it('should load more bookmarks', (done) => { |  | ||||||
| 			socketPosts.loadMoreBookmarks({ uid: voterUid }, { uid: voterUid, after: 0 }, (err, data) => { |  | ||||||
| 				assert.ifError(err); |  | ||||||
| 				assert(data); |  | ||||||
| 				done(); |  | ||||||
| 			}); |  | ||||||
| 		}); |  | ||||||
|  |  | ||||||
| 		it('should load more user posts', (done) => { |  | ||||||
| 			socketPosts.loadMoreUserPosts({ uid: voterUid }, { uid: voterUid, after: 0 }, (err, data) => { |  | ||||||
| 				assert.ifError(err); |  | ||||||
| 				assert(data); |  | ||||||
| 				done(); |  | ||||||
| 			}); |  | ||||||
| 		}); |  | ||||||
|  |  | ||||||
| 		it('should load more best posts', (done) => { |  | ||||||
| 			socketPosts.loadMoreBestPosts({ uid: voterUid }, { uid: voterUid, after: 0 }, (err, data) => { |  | ||||||
| 				assert.ifError(err); |  | ||||||
| 				assert(data); |  | ||||||
| 				done(); |  | ||||||
| 			}); |  | ||||||
| 		}); |  | ||||||
|  |  | ||||||
| 		it('should load more up voted posts', (done) => { |  | ||||||
| 			socketPosts.loadMoreUpVotedPosts({ uid: voterUid }, { uid: voterUid, after: 0 }, (err, data) => { |  | ||||||
| 				assert.ifError(err); |  | ||||||
| 				assert(data); |  | ||||||
| 				done(); |  | ||||||
| 			}); |  | ||||||
| 		}); |  | ||||||
|  |  | ||||||
| 		it('should load more down voted posts', (done) => { |  | ||||||
| 			socketPosts.loadMoreDownVotedPosts({ uid: voterUid }, { uid: voterUid, after: 0 }, (err, data) => { |  | ||||||
| 				assert.ifError(err); |  | ||||||
| 				assert(data); |  | ||||||
| 				done(); |  | ||||||
| 			}); |  | ||||||
| 		}); |  | ||||||
|  |  | ||||||
| 		it('should get post category', (done) => { | 		it('should get post category', (done) => { | ||||||
| 			socketPosts.getCategory({ uid: voterUid }, pid, (err, postCid) => { | 			socketPosts.getCategory({ uid: voterUid }, pid, (err, postCid) => { | ||||||
| 				assert.ifError(err); | 				assert.ifError(err); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user