From b53a60c27857dcf2a172b787235405a4ef4db982 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Sat, 22 Jun 2019 22:52:10 -0400 Subject: [PATCH] feat: show best & latest posts on profile --- public/language/en-GB/pages.json | 1 + public/src/client/account/profile.js | 50 +---------- public/src/utils.js | 2 +- src/controllers/accounts/profile.js | 129 +++++++++++++++++---------- 4 files changed, 87 insertions(+), 95 deletions(-) diff --git a/public/language/en-GB/pages.json b/public/language/en-GB/pages.json index bf8b141c6f..9de2a50eb1 100644 --- a/public/language/en-GB/pages.json +++ b/public/language/en-GB/pages.json @@ -49,6 +49,7 @@ "account/following": "People %1 follows", "account/followers": "People who follow %1", "account/posts": "Posts made by %1", + "account/latest-posts": "Latest posts made by %1", "account/topics": "Topics created by %1", "account/groups": "%1's Groups", "account/watched_categories": "%1's Watched Categories", diff --git a/public/src/client/account/profile.js b/public/src/client/account/profile.js index 2f8a834d36..f69941e192 100644 --- a/public/src/client/account/profile.js +++ b/public/src/client/account/profile.js @@ -3,27 +3,19 @@ define('forum/account/profile', [ 'forum/account/header', - 'forum/infinitescroll', 'components', -], function (header, infinitescroll) { +], function (header) { var Account = {}; - var theirid; Account.init = function () { header.init(); - theirid = ajaxify.data.theirid; - - app.enterRoom('user/' + theirid); + app.enterRoom('user/' + ajaxify.data.theirid); processPage(); socket.removeListener('event:user_status_change', onUserStatusChange); socket.on('event:user_status_change', onUserStatusChange); - - if (!config.usePagination) { - infinitescroll.init(loadMorePosts); - } }; function processPage() { @@ -38,43 +30,5 @@ define('forum/account/profile', [ app.updateUserStatus($('.account [data-uid="' + data.uid + '"] [component="user/status"]'), data.status); } - function loadMorePosts(direction) { - if (direction < 0 || !$('[component="posts"]').length) { - return; - } - - $('[component="posts/loading"]').removeClass('hidden'); - - infinitescroll.loadMore('posts.loadMoreUserPosts', { - after: $('[component="posts"]').attr('data-nextstart'), - uid: theirid, - }, function (data, done) { - if (data.posts && data.posts.length) { - onPostsLoaded(data.posts, done); - } else { - done(); - } - $('[component="posts"]').attr('data-nextstart', data.nextStart); - $('[component="posts/loading"]').addClass('hidden'); - }); - } - - function onPostsLoaded(posts, callback) { - posts = posts.filter(function (post) { - return !$('[component="posts"] [data-pid=' + post.pid + ']').length; - }); - - if (!posts.length) { - return callback(); - } - - app.parseAndTranslate('account/profile', 'posts', { posts: posts }, function (html) { - $('[component="posts"]').append(html); - html.find('.timeago').timeago(); - - callback(); - }); - } - return Account; }); diff --git a/public/src/utils.js b/public/src/utils.js index 2f427fd49d..4b4acd5cb0 100644 --- a/public/src/utils.js +++ b/public/src/utils.js @@ -13,7 +13,7 @@ }; process.profile = function (operation, start) { - winston.log('%s took %d milliseconds', operation, process.elapsedTimeSince(start)); + console.log('%s took %d milliseconds', operation, process.elapsedTimeSince(start)); }; process.elapsedTimeSince = function (start) { diff --git a/src/controllers/accounts/profile.js b/src/controllers/accounts/profile.js index 62ce87065d..cb320e7dcf 100644 --- a/src/controllers/accounts/profile.js +++ b/src/controllers/accounts/profile.js @@ -3,13 +3,14 @@ var nconf = require('nconf'); var async = require('async'); +const db = require('../../database'); +const privileges = require('../../privileges'); var user = require('../../user'); var posts = require('../../posts'); var plugins = require('../../plugins'); var meta = require('../../meta'); var accountHelpers = require('./helpers'); var helpers = require('../helpers'); -var pagination = require('../../pagination'); var messaging = require('../../messaging'); var translator = require('../../translator'); var utils = require('../../utils'); @@ -26,10 +27,7 @@ profileController.get = function (req, res, callback) { return res.redirect(nconf.get('relative_path') + '/user/' + lowercaseSlug); } } - var page = Math.max(1, parseInt(req.query.page, 10) || 1); - var itemsPerPage = 10; - var start = (page - 1) * itemsPerPage; - var stop = start + itemsPerPage - 1; + var userData; async.waterfall([ function (next) { @@ -54,8 +52,11 @@ profileController.get = function (req, res, callback) { hasPrivateChat: function (next) { messaging.hasPrivateChat(req.uid, userData.uid, next); }, - posts: function (next) { - posts.getPostSummariesFromSet('uid:' + userData.theirid + ':posts', req.uid, start, stop, next); + latestPosts: function (next) { + getLatestPosts(req.uid, userData, next); + }, + bestPosts: function (next) { + getBestPosts(req.uid, userData, next); }, signature: function (next) { posts.parseSignature(userData, req.uid, next); @@ -74,55 +75,20 @@ profileController.get = function (req, res, callback) { delete userData.reputation; } - userData.posts = results.posts.posts.filter(p => p && !p.deleted); + userData.posts = results.latestPosts; // for backwards compat. + userData.latestPosts = results.latestPosts; + userData.bestPosts = results.bestPosts; userData.hasPrivateChat = results.hasPrivateChat; userData.aboutme = translator.escape(results.aboutme); - userData.nextStart = results.posts.nextStart; userData.breadcrumbs = helpers.buildBreadcrumbs([{ text: userData.username }]); userData.title = userData.username; userData.allowCoverPicture = !userData.isSelf || userData.reputation >= (meta.config['min:rep:cover-picture'] || 0); - var pageCount = Math.ceil(userData.postcount / itemsPerPage); - userData.pagination = pagination.create(page, pageCount, req.query); if (!userData.profileviews) { userData.profileviews = 1; } - var plainAboutMe = userData.aboutme ? utils.stripHTMLTags(utils.decodeHTMLEntities(userData.aboutme)) : ''; - - res.locals.metaTags = [ - { - name: 'title', - content: userData.fullname || userData.username, - }, - { - name: 'description', - content: plainAboutMe, - }, - { - property: 'og:title', - content: userData.fullname || userData.username, - }, - { - property: 'og:description', - content: plainAboutMe, - }, - ]; - - if (userData.picture) { - res.locals.metaTags.push( - { - property: 'og:image', - content: userData.picture, - noEscape: true, - }, - { - property: 'og:image:url', - content: userData.picture, - noEscape: true, - } - ); - } + addMetaTags(res, userData); userData.selectedGroup = userData.groups.filter(function (group) { return group && userData.groupTitleArray.includes(group.name); @@ -135,3 +101,74 @@ profileController.get = function (req, res, callback) { }, ], callback); }; + +function getLatestPosts(callerUid, userData, callback) { + async.waterfall([ + function (next) { + db.getSortedSetRevRange('uid:' + userData.uid + ':posts', 0, 99, next); + }, + function (pids, next) { + getPosts(callerUid, pids, next); + }, + ], callback); +} + +function getBestPosts(callerUid, userData, callback) { + async.waterfall([ + function (next) { + db.getSortedSetRevRange('uid:' + userData.uid + ':posts:votes', 0, 99, next); + }, + function (pids, next) { + getPosts(callerUid, pids, next); + }, + ], callback); +} + +function getPosts(callerUid, pids, callback) { + async.waterfall([ + function (next) { + privileges.posts.filter('topics:read', pids, callerUid, next); + }, + function (pids, next) { + pids = pids.slice(0, 10); + posts.getPostSummaryByPids(pids, callerUid, { stripTags: false }, next); + }, + ], callback); +} + +function addMetaTags(res, userData) { + var plainAboutMe = userData.aboutme ? utils.stripHTMLTags(utils.decodeHTMLEntities(userData.aboutme)) : ''; + res.locals.metaTags = [ + { + name: 'title', + content: userData.fullname || userData.username, + }, + { + name: 'description', + content: plainAboutMe, + }, + { + property: 'og:title', + content: userData.fullname || userData.username, + }, + { + property: 'og:description', + content: plainAboutMe, + }, + ]; + + if (userData.picture) { + res.locals.metaTags.push( + { + property: 'og:image', + content: userData.picture, + noEscape: true, + }, + { + property: 'og:image:url', + content: userData.picture, + noEscape: true, + } + ); + } +}