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
|
|
|
|
2018-09-24 12:58:59 -04: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');
|
2017-04-08 20:22:21 -06:00
|
|
|
var utils = require('../utils');
|
2014-11-11 19:47:56 -05:00
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
module.exports = function (Topics) {
|
2019-07-09 12:46:49 -04:00
|
|
|
Topics.getTeasers = async function (topics, options) {
|
2014-12-19 23:56:51 -05:00
|
|
|
if (!Array.isArray(topics) || !topics.length) {
|
2019-07-09 12:46:49 -04:00
|
|
|
return [];
|
2014-11-11 19:47:56 -05:00
|
|
|
}
|
2019-02-22 14:39:05 -05:00
|
|
|
let uid = options;
|
|
|
|
|
let teaserPost = meta.config.teaserPost;
|
|
|
|
|
if (typeof options === 'object') {
|
|
|
|
|
uid = options.uid;
|
|
|
|
|
teaserPost = options.teaserPost || meta.config.teaserPost;
|
|
|
|
|
}
|
2014-11-11 19:47:56 -05:00
|
|
|
|
2014-12-19 23:56:51 -05:00
|
|
|
var counts = [];
|
|
|
|
|
var teaserPids = [];
|
2015-08-07 17:56:03 -04:00
|
|
|
var tidToPost = {};
|
2014-12-19 23:56:51 -05:00
|
|
|
|
2016-10-13 11:43:39 +02: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) {
|
2016-04-28 11:52:05 -04:00
|
|
|
if (topic.teaserPid === 'null') {
|
|
|
|
|
delete topic.teaserPid;
|
|
|
|
|
}
|
2019-02-20 16:13:26 -05:00
|
|
|
if (teaserPost === 'first') {
|
2017-02-18 02:38:03 -07:00
|
|
|
teaserPids.push(topic.mainPid);
|
2019-02-20 16:13:26 -05:00
|
|
|
} 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);
|
2016-04-28 11:52:05 -04:00
|
|
|
}
|
2014-12-19 23:56:51 -05:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2019-07-09 12:46:49 -04:00
|
|
|
let postData = await posts.getPostsFields(teaserPids, ['pid', 'uid', 'timestamp', 'tid', 'content']);
|
|
|
|
|
postData = postData.filter(post => post && post.pid);
|
|
|
|
|
postData = await handleBlocks(uid, postData);
|
|
|
|
|
postData = postData.filter(Boolean);
|
|
|
|
|
const uids = _.uniq(postData.map(post => post.uid));
|
|
|
|
|
|
|
|
|
|
const usersData = await user.getUsersFields(uids, ['uid', 'username', 'userslug', 'picture']);
|
|
|
|
|
|
|
|
|
|
var users = {};
|
|
|
|
|
usersData.forEach(function (user) {
|
|
|
|
|
users[user.uid] = user;
|
|
|
|
|
});
|
|
|
|
|
postData.forEach(function (post) {
|
|
|
|
|
// 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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
post.user = users[post.uid];
|
|
|
|
|
post.timestampISO = utils.toISOString(post.timestamp);
|
|
|
|
|
tidToPost[post.tid] = post;
|
|
|
|
|
});
|
|
|
|
|
await Promise.all(postData.map(p => posts.parsePost(p)));
|
2014-11-11 19:47:56 -05:00
|
|
|
|
2020-10-21 11:26:32 -04:00
|
|
|
const { tags } = await plugins.fireHook('filter:teasers.configureStripTags', { tags: utils.stripTags.concat(['img']) });
|
|
|
|
|
|
2019-07-09 12:46:49 -04:00
|
|
|
var teasers = topics.map(function (topic, index) {
|
|
|
|
|
if (!topic) {
|
|
|
|
|
return null;
|
2018-09-24 12:58:59 -04:00
|
|
|
}
|
2019-07-09 12:46:49 -04:00
|
|
|
if (tidToPost[topic.tid]) {
|
|
|
|
|
tidToPost[topic.tid].index = meta.config.teaserPost === 'first' ? 1 : counts[index];
|
|
|
|
|
if (tidToPost[topic.tid].content) {
|
2020-10-21 11:26:32 -04:00
|
|
|
tidToPost[topic.tid].content = utils.stripHTMLTags(tidToPost[topic.tid].content, tags);
|
2018-09-24 12:58:59 -04:00
|
|
|
}
|
2019-07-09 12:46:49 -04:00
|
|
|
}
|
|
|
|
|
return tidToPost[topic.tid];
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const result = await plugins.fireHook('filter:teasers.get', { teasers: teasers, uid: uid });
|
|
|
|
|
return result.teasers;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
async function handleBlocks(uid, teasers) {
|
|
|
|
|
const blockedUids = await user.blocks.list(uid);
|
|
|
|
|
if (!blockedUids.length) {
|
|
|
|
|
return teasers;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return await async.mapSeries(teasers, async function (postData) {
|
|
|
|
|
if (blockedUids.includes(parseInt(postData.uid, 10))) {
|
|
|
|
|
return await getPreviousNonBlockedPost(postData, blockedUids);
|
|
|
|
|
}
|
|
|
|
|
return postData;
|
2018-09-24 12:58:59 -04:00
|
|
|
});
|
2018-09-05 14:02:59 -04:00
|
|
|
}
|
|
|
|
|
|
2019-07-09 12:46:49 -04:00
|
|
|
async function getPreviousNonBlockedPost(postData, blockedUids) {
|
2018-09-05 14:02:59 -04:00
|
|
|
let isBlocked = false;
|
|
|
|
|
let prevPost = postData;
|
2018-09-24 12:58:59 -04:00
|
|
|
const postsPerIteration = 5;
|
|
|
|
|
let start = 0;
|
|
|
|
|
let stop = start + postsPerIteration - 1;
|
2018-10-15 11:09:57 -04:00
|
|
|
let checkedAllReplies = false;
|
2019-07-09 12:46:49 -04:00
|
|
|
|
|
|
|
|
function checkBlocked(post) {
|
|
|
|
|
const isPostBlocked = blockedUids.includes(parseInt(post.uid, 10));
|
|
|
|
|
prevPost = !isPostBlocked ? post : prevPost;
|
|
|
|
|
return isPostBlocked;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
/* eslint-disable no-await-in-loop */
|
|
|
|
|
let pids = await db.getSortedSetRevRange('tid:' + postData.tid + ':posts', start, stop);
|
|
|
|
|
if (!pids.length) {
|
|
|
|
|
checkedAllReplies = true;
|
|
|
|
|
const mainPid = await Topics.getTopicField(postData.tid, 'mainPid');
|
|
|
|
|
pids = [mainPid];
|
|
|
|
|
}
|
|
|
|
|
const prevPosts = await posts.getPostsFields(pids, ['pid', 'uid', 'timestamp', 'tid', 'content']);
|
|
|
|
|
isBlocked = prevPosts.every(checkBlocked);
|
|
|
|
|
start += postsPerIteration;
|
|
|
|
|
stop = start + postsPerIteration - 1;
|
|
|
|
|
} while (isBlocked && prevPost && prevPost.pid && !checkedAllReplies);
|
|
|
|
|
|
|
|
|
|
return prevPost;
|
2018-09-05 14:02:59 -04:00
|
|
|
}
|
|
|
|
|
|
2019-07-09 12:46:49 -04:00
|
|
|
Topics.getTeasersByTids = async function (tids, uid) {
|
2014-12-19 23:56:51 -05:00
|
|
|
if (!Array.isArray(tids) || !tids.length) {
|
2019-07-09 12:46:49 -04:00
|
|
|
return [];
|
2014-12-19 23:56:51 -05:00
|
|
|
}
|
2019-07-09 12:46:49 -04:00
|
|
|
const topics = await Topics.getTopicsFields(tids, ['tid', 'postcount', 'teaserPid', 'mainPid']);
|
|
|
|
|
return await Topics.getTeasers(topics, uid);
|
2014-12-19 23:56:51 -05:00
|
|
|
};
|
|
|
|
|
|
2019-07-09 12:46:49 -04:00
|
|
|
Topics.getTeaser = async function (tid, uid) {
|
|
|
|
|
const teasers = await Topics.getTeasersByTids([tid], uid);
|
|
|
|
|
return Array.isArray(teasers) && teasers.length ? teasers[0] : null;
|
2014-12-12 18:52:37 -05:00
|
|
|
};
|
|
|
|
|
|
2019-07-09 12:46:49 -04:00
|
|
|
Topics.updateTeaser = async function (tid) {
|
|
|
|
|
let pid = await Topics.getLatestUndeletedReply(tid);
|
|
|
|
|
pid = pid || null;
|
|
|
|
|
if (pid) {
|
|
|
|
|
await Topics.setTopicField(tid, 'teaserPid', pid);
|
|
|
|
|
} else {
|
|
|
|
|
await Topics.deleteTopicField(tid, 'teaserPid');
|
|
|
|
|
}
|
2014-11-11 19:47:56 -05:00
|
|
|
};
|
2017-02-18 02:30:48 -07:00
|
|
|
};
|