From 359547348510d5aea1092f1f54ce450e437fde4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Thu, 11 Feb 2021 16:47:51 -0500 Subject: [PATCH] feat: load user posts/topics via xhr on infinitescroll --- public/src/client/account/best.js | 2 +- public/src/client/account/bookmarks.js | 2 +- public/src/client/account/downvoted.js | 2 +- public/src/client/account/ignored.js | 13 +++++++ public/src/client/account/posts.js | 18 +++++----- public/src/client/account/topics.js | 24 +++++-------- public/src/client/account/upvoted.js | 2 +- public/src/client/account/watched.js | 2 +- public/src/client/infinitescroll.js | 19 +++++++++++ src/socket.io/posts.js | 36 -------------------- src/socket.io/topics/infinitescroll.js | 7 ---- test/posts.js | 47 -------------------------- 12 files changed, 55 insertions(+), 119 deletions(-) create mode 100644 public/src/client/account/ignored.js diff --git a/public/src/client/account/best.js b/public/src/client/account/best.js index 562438297a..ebd61760bc 100644 --- a/public/src/client/account/best.js +++ b/public/src/client/account/best.js @@ -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'); - posts.handleInfiniteScroll('posts.loadMoreBestPosts', 'account/best'); + posts.handleInfiniteScroll('account/best'); }; return Best; diff --git a/public/src/client/account/bookmarks.js b/public/src/client/account/bookmarks.js index 80357fe89b..56f9983e9f 100644 --- a/public/src/client/account/bookmarks.js +++ b/public/src/client/account/bookmarks.js @@ -9,7 +9,7 @@ define('forum/account/bookmarks', ['forum/account/header', 'forum/account/posts' $('[component="post/content"] img:not(.not-responsive)').addClass('img-responsive'); - posts.handleInfiniteScroll('posts.loadMoreBookmarks', 'account/bookmarks'); + posts.handleInfiniteScroll('account/bookmarks'); }; return Bookmarks; diff --git a/public/src/client/account/downvoted.js b/public/src/client/account/downvoted.js index b4f0cc181f..86252758d8 100644 --- a/public/src/client/account/downvoted.js +++ b/public/src/client/account/downvoted.js @@ -9,7 +9,7 @@ define('forum/account/downvoted', ['forum/account/header', 'forum/account/posts' $('[component="post/content"] img:not(.not-responsive)').addClass('img-responsive'); - posts.handleInfiniteScroll('posts.loadMoreDownVotedPosts', 'account/downvoted'); + posts.handleInfiniteScroll('account/downvoted'); }; return Downvoted; diff --git a/public/src/client/account/ignored.js b/public/src/client/account/ignored.js new file mode 100644 index 0000000000..0664325d8e --- /dev/null +++ b/public/src/client/account/ignored.js @@ -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; +}); diff --git a/public/src/client/account/posts.js b/public/src/client/account/posts.js index e247905852..7c9fff5df4 100644 --- a/public/src/client/account/posts.js +++ b/public/src/client/account/posts.js @@ -3,20 +3,21 @@ define('forum/account/posts', ['forum/account/header', 'forum/infinitescroll'], function (header, infinitescroll) { var AccountPosts = {}; - var method; + var template; + var page = 1; AccountPosts.init = function () { header.init(); $('[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) { - method = _method; + AccountPosts.handleInfiniteScroll = function (_template) { template = _template; + page = ajaxify.data.pagination.currentPage; if (!config.usePagination) { infinitescroll.init(loadMore); } @@ -26,17 +27,16 @@ define('forum/account/posts', ['forum/account/header', 'forum/infinitescroll'], if (direction < 0) { return; } + var params = utils.params(); + page += 1; + params.page = page; - infinitescroll.loadMore(method, { - uid: ajaxify.data.theirid, - after: $('[component="posts"]').attr('data-nextstart'), - }, function (data, done) { + infinitescroll.loadMoreXhr(params, function (data, done) { if (data.posts && data.posts.length) { onPostsLoaded(data.posts, done); } else { done(); } - $('[component="posts"]').attr('data-nextstart', data.nextStart); }); } diff --git a/public/src/client/account/topics.js b/public/src/client/account/topics.js index a9c0452eef..51d052f16e 100644 --- a/public/src/client/account/topics.js +++ b/public/src/client/account/topics.js @@ -3,21 +3,19 @@ define('forum/account/topics', ['forum/account/header', 'forum/infinitescroll'], function (header, infinitescroll) { var AccountTopics = {}; - var method; + var template; - var set; + var page = 1; AccountTopics.init = function () { header.init(); - AccountTopics.handleInfiniteScroll('topics.loadMoreUserTopics', 'account/topics'); + AccountTopics.handleInfiniteScroll('account/topics'); }; - AccountTopics.handleInfiniteScroll = function (_method, _template, _set) { - method = _method; + AccountTopics.handleInfiniteScroll = function (_template) { template = _template; - set = _set; - + page = ajaxify.data.pagination.currentPage; if (!config.usePagination) { infinitescroll.init(loadMore); } @@ -27,20 +25,16 @@ define('forum/account/topics', ['forum/account/header', 'forum/infinitescroll'], if (direction < 0) { return; } + var params = utils.params(); + page += 1; + params.page = page; - infinitescroll.loadMore(method, { - set: set, - uid: ajaxify.data.theirid, - after: $('[component="category"]').attr('data-nextstart'), - count: config.topicsPerPage, - }, function (data, done) { + infinitescroll.loadMoreXhr(params, function (data, done) { if (data.topics && data.topics.length) { onTopicsLoaded(data.topics, done); } else { done(); } - - $('[component="category"]').attr('data-nextstart', data.nextStart); }); } diff --git a/public/src/client/account/upvoted.js b/public/src/client/account/upvoted.js index 74c1f6c753..fb14f58111 100644 --- a/public/src/client/account/upvoted.js +++ b/public/src/client/account/upvoted.js @@ -9,7 +9,7 @@ define('forum/account/upvoted', ['forum/account/header', 'forum/account/posts'], $('[component="post/content"] img:not(.not-responsive)').addClass('img-responsive'); - posts.handleInfiniteScroll('posts.loadMoreUpVotedPosts', 'account/upvoted'); + posts.handleInfiniteScroll('account/upvoted'); }; return Upvoted; diff --git a/public/src/client/account/watched.js b/public/src/client/account/watched.js index 77450333d5..a2c22f90f7 100644 --- a/public/src/client/account/watched.js +++ b/public/src/client/account/watched.js @@ -7,7 +7,7 @@ define('forum/account/watched', ['forum/account/header', 'forum/account/topics'] AccountWatched.init = function () { header.init(); - topics.handleInfiniteScroll('topics.loadMoreFromSet', 'account/watched', 'uid:' + ajaxify.data.theirid + ':followed_tids'); + topics.handleInfiniteScroll('account/watched'); }; return AccountWatched; diff --git a/public/src/client/infinitescroll.js b/public/src/client/infinitescroll.js index c1e181f2ab..0715fe6aee 100644 --- a/public/src/client/infinitescroll.js +++ b/public/src/client/infinitescroll.js @@ -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) { if (els.length <= count) { return; diff --git a/src/socket.io/posts.js b/src/socket.io/posts.js index b9361dd650..fbf4e4a01f 100644 --- a/src/socket.io/posts.js +++ b/src/socket.io/posts.js @@ -6,7 +6,6 @@ const privileges = require('../privileges'); const plugins = require('../plugins'); const meta = require('../meta'); const topics = require('../topics'); -const categories = require('../categories'); const user = require('../user'); const socketHelpers = require('./helpers'); const utils = require('../utils'); @@ -101,41 +100,6 @@ SocketPosts.getPost = async function (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) { return await posts.getCidByPid(pid); }; diff --git a/src/socket.io/topics/infinitescroll.js b/src/socket.io/topics/infinitescroll.js index a46ea8d19f..4bd3c08db7 100644 --- a/src/socket.io/topics/infinitescroll.js +++ b/src/socket.io/topics/infinitescroll.js @@ -1,7 +1,6 @@ 'use strict'; const topics = require('../../topics'); -const categories = require('../../categories'); const privileges = require('../../privileges'); const meta = require('../../meta'); const utils = require('../../utils'); @@ -92,12 +91,6 @@ module.exports = function (SocketTopics) { 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) { const itemsPerPage = Math.min( meta.config.topicsPerPage || 20, diff --git a/test/posts.js b/test/posts.js index 3f188b1ea4..a8504b0b50 100644 --- a/test/posts.js +++ b/test/posts.js @@ -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) => { socketPosts.getCategory({ uid: voterUid }, pid, (err, postCid) => { assert.ifError(err);