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');
|
2016-04-27 09:03:34 +03:00
|
|
|
var S = require('string');
|
2017-05-12 20:50:01 -04:00
|
|
|
var winston = require('winston');
|
2014-11-11 19:47:56 -05:00
|
|
|
|
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) {
|
2017-04-03 14:40:03 -07:00
|
|
|
Topics.getTeasers = function (topics, uid, callback) {
|
|
|
|
|
if (typeof uid === 'function') {
|
2017-05-12 20:50:01 -04:00
|
|
|
winston.warn('[Topics.getTeasers] this usage is deprecated please provide uid');
|
2017-04-03 14:40:03 -07:00
|
|
|
callback = uid;
|
|
|
|
|
uid = 0;
|
|
|
|
|
}
|
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 = {};
|
2014-12-19 23:56:51 -05:00
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
topics.forEach(function (topic) {
|
2014-12-19 23:56:51 -05:00
|
|
|
counts.push(topic && (parseInt(topic.postcount, 10) || 0));
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-18 01:52:56 -07:00
|
|
|
switch (meta.config.teaserPost) {
|
2017-02-18 02:38:03 -07:00
|
|
|
case 'first':
|
|
|
|
|
teaserPids.push(topic.mainPid);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'last-post':
|
|
|
|
|
teaserPids.push(topic.teaserPid || topic.mainPid);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'last-reply': // intentional fall-through
|
|
|
|
|
default:
|
|
|
|
|
teaserPids.push(topic.teaserPid);
|
|
|
|
|
break;
|
2016-04-28 11:52:05 -04:00
|
|
|
}
|
2014-12-19 23:56:51 -05:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2015-08-07 17:56:03 -04:00
|
|
|
async.waterfall([
|
2016-10-13 11:43:39 +02:00
|
|
|
function (next) {
|
2015-08-07 17:56:03 -04:00
|
|
|
posts.getPostsFields(teaserPids, ['pid', 'uid', 'timestamp', 'tid', 'content'], next);
|
|
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
function (_postData, next) {
|
2015-08-07 17:56:03 -04:00
|
|
|
postData = _postData;
|
2017-06-25 20:00:05 -04:00
|
|
|
var uids = _.uniq(postData.map(function (post) {
|
2015-08-07 17:56:03 -04:00
|
|
|
return post.uid;
|
2017-06-25 20:00:05 -04:00
|
|
|
}));
|
2014-11-11 19:47:56 -05:00
|
|
|
|
2015-10-29 16:35:44 -04:00
|
|
|
user.getUsersFields(uids, ['uid', 'username', 'userslug', 'picture'], next);
|
2015-08-07 17:56:03 -04:00
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
function (usersData, next) {
|
2014-12-19 23:56:51 -05:00
|
|
|
var users = {};
|
2016-10-13 11:43:39 +02:00
|
|
|
usersData.forEach(function (user) {
|
2014-12-19 23:56:51 -05:00
|
|
|
users[user.uid] = user;
|
|
|
|
|
});
|
2015-08-07 17:56:03 -04:00
|
|
|
|
2015-03-23 19:32:32 -04:00
|
|
|
|
2016-10-13 11:43:39 +02: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;
|
2015-04-20 17:56:43 -04:00
|
|
|
posts.parsePost(post, next);
|
2015-08-07 17:56:03 -04:00
|
|
|
}, next);
|
|
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
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) {
|
|
|
|
|
var s = S(tidToPost[topic.tid].content);
|
2016-04-27 09:03:34 +03:00
|
|
|
tidToPost[topic.tid].content = s.stripTags.apply(s, utils.stripTags).s;
|
2015-03-23 19:32:32 -04:00
|
|
|
}
|
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
|
|
|
|
2017-04-03 14:40:03 -07:00
|
|
|
plugins.fireHook('filter:teasers.get', { teasers: teasers, uid: uid }, next);
|
2015-08-07 17:56:03 -04:00
|
|
|
},
|
2016-10-13 11:43:39 +02: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
|
|
|
};
|
|
|
|
|
|
2017-04-03 14:40:03 -07:00
|
|
|
Topics.getTeasersByTids = function (tids, uid, callback) {
|
|
|
|
|
if (typeof uid === 'function') {
|
2017-05-12 20:50:01 -04:00
|
|
|
winston.warn('[Topics.getTeasersByTids] this usage is deprecated please provide uid');
|
2017-04-03 14:40:03 -07:00
|
|
|
callback = uid;
|
|
|
|
|
uid = 0;
|
|
|
|
|
}
|
2014-12-19 23:56:51 -05:00
|
|
|
if (!Array.isArray(tids) || !tids.length) {
|
|
|
|
|
return callback(null, []);
|
|
|
|
|
}
|
|
|
|
|
async.waterfall([
|
2016-10-13 11:43:39 +02:00
|
|
|
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
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
function (topics, next) {
|
2017-04-03 14:40:03 -07:00
|
|
|
Topics.getTeasers(topics, uid, next);
|
2017-02-17 19:31:21 -07:00
|
|
|
},
|
2014-12-19 23:56:51 -05:00
|
|
|
], callback);
|
|
|
|
|
};
|
|
|
|
|
|
2017-04-03 14:40:03 -07:00
|
|
|
Topics.getTeaser = function (tid, uid, callback) {
|
|
|
|
|
if (typeof uid === 'function') {
|
2017-05-12 20:50:01 -04:00
|
|
|
winston.warn('[Topics.getTeaser] this usage is deprecated please provide uid');
|
2017-04-03 14:40:03 -07:00
|
|
|
callback = uid;
|
|
|
|
|
uid = 0;
|
|
|
|
|
}
|
|
|
|
|
Topics.getTeasersByTids([tid], uid, function (err, teasers) {
|
2014-12-12 18:52:37 -05:00
|
|
|
callback(err, Array.isArray(teasers) && teasers.length ? teasers[0] : null);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
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
|
|
|
};
|