Files
NodeBB/src/topics/teaser.js

192 lines
5.4 KiB
JavaScript
Raw Normal View History

2014-11-11 19:47:56 -05:00
'use strict';
2016-04-27 09:03:34 +03:00
var async = require('async');
2017-06-25 20:00:05 -04:00
var _ = require('lodash');
2014-11-11 19:47:56 -05:00
var db = require('../database');
2016-04-27 09:03:34 +03:00
var meta = require('../meta');
var user = require('../user');
var posts = require('../posts');
var plugins = require('../plugins');
var utils = require('../utils');
2014-11-11 19:47:56 -05:00
module.exports = function (Topics) {
2017-12-15 10:29:12 -05:00
var stripTeaserTags = utils.stripTags.concat(['img']);
Topics.getTeasers = function (topics, uid, callback) {
2014-12-19 23:56:51 -05:00
if (!Array.isArray(topics) || !topics.length) {
2014-11-11 19:47:56 -05:00
return callback(null, []);
}
2014-12-19 23:56:51 -05:00
var counts = [];
var teaserPids = [];
2015-08-07 17:56:03 -04:00
var postData;
var tidToPost = {};
2019-02-22 12:33:27 -05:00
const teaserPost = this.teaserPost ? this.teaserPost : meta.config.teaserPost;
2014-12-19 23:56:51 -05:00
topics.forEach(function (topic) {
2018-10-25 17:02:59 -04:00
counts.push(topic && topic.postcount);
2015-07-10 16:43:25 -04:00
if (topic) {
if (topic.teaserPid === 'null') {
delete topic.teaserPid;
}
if (teaserPost === 'first') {
2017-02-18 02:38:03 -07:00
teaserPids.push(topic.mainPid);
} else if (teaserPost === 'last-post') {
2017-02-18 02:38:03 -07:00
teaserPids.push(topic.teaserPid || topic.mainPid);
2018-12-05 13:06:02 -05:00
} else { // last-reply and everything else uses teaserPid like `last` that was used before
2017-02-18 02:38:03 -07:00
teaserPids.push(topic.teaserPid);
}
2014-12-19 23:56:51 -05:00
}
});
2015-08-07 17:56:03 -04:00
async.waterfall([
function (next) {
2015-08-07 17:56:03 -04:00
posts.getPostsFields(teaserPids, ['pid', 'uid', 'timestamp', 'tid', 'content'], next);
},
2018-07-27 13:38:37 -04:00
function (_postData, next) {
2018-11-01 10:44:53 -04:00
_postData = _postData.filter(post => post && post.pid);
2018-09-05 14:02:59 -04:00
handleBlocks(uid, _postData, next);
2018-07-27 13:38:37 -04:00
},
function (_postData, next) {
2018-09-06 18:48:46 -04:00
postData = _postData.filter(Boolean);
2018-11-01 10:44:53 -04:00
const uids = _.uniq(postData.map(post => post.uid));
2014-11-11 19:47:56 -05:00
user.getUsersFields(uids, ['uid', 'username', 'userslug', 'picture'], next);
2015-08-07 17:56:03 -04:00
},
function (usersData, next) {
2014-12-19 23:56:51 -05:00
var users = {};
usersData.forEach(function (user) {
2014-12-19 23:56:51 -05:00
users[user.uid] = user;
});
2015-08-07 17:56:03 -04:00
async.each(postData, function (post, next) {
2015-07-24 10:22:21 -04:00
// If the post author isn't represented in the retrieved users' data, then it means they were deleted, assume guest.
if (!users.hasOwnProperty(post.uid)) {
post.uid = 0;
}
2014-12-19 23:56:51 -05:00
post.user = users[post.uid];
2016-03-03 20:13:30 +02:00
post.timestampISO = utils.toISOString(post.timestamp);
2014-12-19 23:56:51 -05:00
tidToPost[post.tid] = post;
posts.parsePost(post, next);
2015-08-07 17:56:03 -04:00
}, next);
},
function (next) {
var teasers = topics.map(function (topic, index) {
2015-08-07 17:56:03 -04:00
if (!topic) {
return null;
2014-11-11 19:47:56 -05:00
}
2015-08-07 17:56:03 -04:00
if (tidToPost[topic.tid]) {
tidToPost[topic.tid].index = meta.config.teaserPost === 'first' ? 1 : counts[index];
if (tidToPost[topic.tid].content) {
2017-12-15 10:29:12 -05:00
tidToPost[topic.tid].content = utils.stripHTMLTags(tidToPost[topic.tid].content, stripTeaserTags);
}
2015-08-07 17:56:03 -04:00
}
return tidToPost[topic.tid];
2015-03-20 12:21:27 -04:00
});
2015-08-07 17:56:03 -04:00
plugins.fireHook('filter:teasers.get', { teasers: teasers, uid: uid }, next);
2015-08-07 17:56:03 -04:00
},
function (data, next) {
2015-08-07 17:56:03 -04:00
next(null, data.teasers);
2017-02-17 19:31:21 -07:00
},
2015-08-07 17:56:03 -04:00
], callback);
2014-11-11 19:47:56 -05:00
};
2018-09-05 14:02:59 -04:00
function handleBlocks(uid, teasers, callback) {
user.blocks.list(uid, function (err, blockedUids) {
if (err || !blockedUids.length) {
return callback(err, teasers);
}
async.mapSeries(teasers, function (postData, nextPost) {
if (blockedUids.includes(parseInt(postData.uid, 10))) {
getPreviousNonBlockedPost(postData, blockedUids, nextPost);
} else {
setImmediate(nextPost, null, postData);
}
}, callback);
});
2018-09-05 14:02:59 -04:00
}
function getPreviousNonBlockedPost(postData, blockedUids, callback) {
2018-09-05 14:02:59 -04:00
let isBlocked = false;
let prevPost = postData;
const postsPerIteration = 5;
let start = 0;
let stop = start + postsPerIteration - 1;
let checkedAllReplies = false;
async.doWhilst(function (next) {
async.waterfall([
function (next) {
db.getSortedSetRevRange('tid:' + postData.tid + ':posts', start, stop, next);
},
function (pids, next) {
if (pids.length) {
return next(null, pids);
}
2018-09-05 14:02:59 -04:00
checkedAllReplies = true;
Topics.getTopicField(postData.tid, 'mainPid', function (err, mainPid) {
next(err, [mainPid]);
});
},
function (pids, next) {
posts.getPostsFields(pids, ['pid', 'uid', 'timestamp', 'tid', 'content'], next);
},
function (prevPosts, next) {
isBlocked = prevPosts.every(function (post) {
const isPostBlocked = blockedUids.includes(parseInt(post.uid, 10));
prevPost = !isPostBlocked ? post : prevPost;
return isPostBlocked;
});
start += postsPerIteration;
stop = start + postsPerIteration - 1;
next();
},
], next);
}, function () {
return isBlocked && prevPost && prevPost.pid && !checkedAllReplies;
}, function (err) {
callback(err, prevPost);
2018-09-05 14:02:59 -04:00
});
}
Topics.getTeasersByTids = function (tids, uid, callback) {
2014-12-19 23:56:51 -05:00
if (!Array.isArray(tids) || !tids.length) {
return callback(null, []);
}
async.waterfall([
function (next) {
2017-05-12 20:50:01 -04:00
Topics.getTopicsFields(tids, ['tid', 'postcount', 'teaserPid', 'mainPid'], next);
2014-12-19 23:56:51 -05:00
},
function (topics, next) {
Topics.getTeasers(topics, uid, next);
2017-02-17 19:31:21 -07:00
},
2014-12-19 23:56:51 -05:00
], callback);
};
Topics.getTeaser = function (tid, uid, callback) {
Topics.getTeasersByTids([tid], uid, function (err, teasers) {
callback(err, Array.isArray(teasers) && teasers.length ? teasers[0] : null);
});
};
Topics.updateTeaser = function (tid, callback) {
2017-05-12 20:50:01 -04:00
async.waterfall([
function (next) {
Topics.getLatestUndeletedReply(tid, next);
},
function (pid, next) {
pid = pid || null;
if (pid) {
Topics.setTopicField(tid, 'teaserPid', pid, next);
} else {
Topics.deleteTopicField(tid, 'teaserPid', next);
}
},
], callback);
2014-11-11 19:47:56 -05:00
};
2017-02-18 02:30:48 -07:00
};