teaser fixes

if a post is deleted/restored update the teaser to the latest undeleted
pid, if all posts are deleted it will be set to null
v1.18.x
Barış Soner Uşaklı
parent edb856ba90
commit e7dd881092

@ -33,6 +33,9 @@ module.exports = function(Posts) {
}, },
function(next) { function(next) {
Posts.dismissFlag(pid, next); Posts.dismissFlag(pid, next);
},
function(next) {
topics.updateTeaser(postData.tid, next);
} }
], function(err) { ], function(err) {
callback(err, postData); callback(err, postData);
@ -64,6 +67,9 @@ module.exports = function(Posts) {
}, },
function(next) { function(next) {
db.sortedSetAdd('cid:' + cid + ':pids', postData.timestamp, pid, next); db.sortedSetAdd('cid:' + cid + ':pids', postData.timestamp, pid, next);
},
function(next) {
topics.updateTeaser(postData.tid, next);
} }
], function(err) { ], function(err) {
callback(err, postData); callback(err, postData);

@ -165,17 +165,23 @@ module.exports = function(Topics) {
}; };
Topics.getLatestUndeletedPid = function(tid, callback) { Topics.getLatestUndeletedPid = function(tid, callback) {
Topics.getLatestUndeletedReply(tid, function(err, pid) { async.waterfall([
if (err) { function(next) {
return callback(err); Topics.getLatestUndeletedReply(tid, next);
} },
if (parseInt(pid, 10)) { function(pid, next) {
return callback(null, pid.toString()); if (parseInt(pid, 10)) {
return callback(null, pid.toString());
}
Topics.getTopicField(tid, 'mainPid', next);
},
function(mainPid, next) {
posts.getPostFields(mainPid, ['pid', 'deleted'], next);
},
function(mainPost, next) {
next(null, parseInt(mainPost.pid, 10) && parseInt(mainPost.deleted, 10) !== 1 ? mainPost.pid.toString() : null);
} }
Topics.getTopicField(tid, 'mainPid', function(err, mainPid) { ], callback);
callback(err, parseInt(mainPid, 10) ? mainPid.toString() : null);
});
});
}; };
Topics.getLatestUndeletedReply = function(tid, callback) { Topics.getLatestUndeletedReply = function(tid, callback) {
@ -199,8 +205,11 @@ module.exports = function(Topics) {
if (err) { if (err) {
return next(err); return next(err);
} }
latestPid = pids[0];
isDeleted = parseInt(deleted, 10) === 1; isDeleted = parseInt(deleted, 10) === 1;
if (!isDeleted) {
latestPid = pids[0];
}
++index; ++index;
next(); next();
}); });

@ -101,11 +101,12 @@ module.exports = function(Topics) {
}; };
Topics.updateTeaser = function(tid, callback) { Topics.updateTeaser = function(tid, callback) {
db.getSortedSetRevRange('tid:' + tid + ':posts', 0, 0, function(err, pids) { Topics.getLatestUndeletedPid(tid, function(err, pid) {
if (err) { if (err) {
return callback(err); return callback(err);
} }
var pid = Array.isArray(pids) && pids.length ? pids[0] : null;
pid = pid || null;
Topics.setTopicField(tid, 'teaserPid', pid, callback); Topics.setTopicField(tid, 'teaserPid', pid, callback);
}); });
}; };

Loading…
Cancel
Save