1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | 1× 1× 1× 1× 1× 1× 1× 1× 1× 9× 9× 9× 9× 9× 9× 9× 9× 9× 9× 9× 9× 9× 9× 9× 9× 9× 9× 9× 9× 9× 9× 9× 9× 9× 9× 9× 9× 9× 9× 9× 9× 9× 9× 9× 1× 1× 1× 30× 30× 30× 30× 19× 11× | 'use strict'; var async = require('async'); var S = require('string'); var meta = require('../meta'); var user = require('../user'); var posts = require('../posts'); var plugins = require('../plugins'); var utils = require('../../public/src/utils'); module.exports = function(Topics) { Topics.getTeasers = function(topics, callback) { Iif (!Array.isArray(topics) || !topics.length) { return callback(null, []); } var counts = []; var teaserPids = []; var postData; var tidToPost = {}; topics.forEach(function(topic) { counts.push(topic && (parseInt(topic.postcount, 10) || 0)); Eif (topic) { Iif (topic.teaserPid === 'null') { delete topic.teaserPid; } switch(meta.config.teaserPost) { 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; } } }); async.waterfall([ function(next) { posts.getPostsFields(teaserPids, ['pid', 'uid', 'timestamp', 'tid', 'content'], next); }, function(_postData, next) { postData = _postData; var uids = postData.map(function(post) { return post.uid; }).filter(function(uid, index, array) { return array.indexOf(uid) === index; }); user.getUsersFields(uids, ['uid', 'username', 'userslug', 'picture'], next); }, function(usersData, next) { var users = {}; usersData.forEach(function(user) { users[user.uid] = user; }); async.each(postData, function(post, next) { // If the post author isn't represented in the retrieved users' data, then it means they were deleted, assume guest. Eif (!users.hasOwnProperty(post.uid)) { post.uid = 0; } post.user = users[post.uid]; post.timestampISO = utils.toISOString(post.timestamp); tidToPost[post.tid] = post; posts.parsePost(post, next); }, next); }, function(next) { var teasers = topics.map(function(topic, index) { Iif (!topic) { return null; } Iif (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); tidToPost[topic.tid].content = s.stripTags.apply(s, utils.stripTags).s; } } return tidToPost[topic.tid]; }); plugins.fireHook('filter:teasers.get', {teasers: teasers}, next); }, function(data, next) { next(null, data.teasers); } ], callback); }; Topics.getTeasersByTids = function(tids, callback) { if (!Array.isArray(tids) || !tids.length) { return callback(null, []); } async.waterfall([ function(next) { Topics.getTopicsFields(tids, ['tid', 'postcount', 'teaserPid'], next); }, function(topics, next) { Topics.getTeasers(topics, next); } ], callback); }; Topics.getTeaser = function(tid, callback) { Topics.getTeasersByTids([tid], function(err, teasers) { callback(err, Array.isArray(teasers) && teasers.length ? teasers[0] : null); }); }; Topics.updateTeaser = function(tid, callback) { Topics.getLatestUndeletedReply(tid, function(err, pid) { Iif (err) { return callback(err); } pid = pid || null; if (pid) { Topics.setTopicField(tid, 'teaserPid', pid, callback); } else { Topics.deleteTopicField(tid, 'teaserPid', callback); } }); }; }; |