feat: load user posts/topics via xhr on infinitescroll

v1.18.x
Barış Soner Uşaklı 4 years ago
parent b753c69cfe
commit 3595473485

@ -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;

@ -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;

@ -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;

@ -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) {
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);
});
}

@ -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);
});
}

@ -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;

@ -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;

@ -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;

@ -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);
};

@ -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,

@ -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);

Loading…
Cancel
Save