Baris Soner Usakli 12 years ago
commit 28b113c09f

@ -132,8 +132,8 @@ var RDB = require('./redis.js'),
} }
function getTeaserInfo(next) { function getTeaserInfo(next) {
topics.getTeaser(topicData.tid, function(teaser) { topics.getTeaser(topicData.tid, function(err, teaser) {
next(null, teaser); next(null, teaser || {});
}); });
} }
@ -178,10 +178,10 @@ var RDB = require('./redis.js'),
topicData.username = topicInfo.username; topicData.username = topicInfo.username;
topicData.badgeclass = (topicInfo.hasread && current_user != 0) ? '' : 'badge-important'; topicData.badgeclass = (topicInfo.hasread && current_user != 0) ? '' : 'badge-important';
topicData.teaser_text = topicInfo.teaserInfo.text, topicData.teaser_text = topicInfo.teaserInfo.text || '',
topicData.teaser_username = topicInfo.teaserInfo.username; topicData.teaser_username = topicInfo.teaserInfo.username || '';
topicData.teaser_userpicture = topicInfo.teaserInfo.picture; topicData.teaser_userpicture = topicInfo.teaserInfo.picture || '';
topicData.teaser_timestamp = utils.relativeTime(topicInfo.teaserInfo.timestamp); topicData.teaser_timestamp = topicInfo.teaserInfo.timestamp ? utils.relativeTime(topicInfo.teaserInfo.timestamp) : '';
if (isTopicVisible(topicData, topicInfo)) if (isTopicVisible(topicData, topicInfo))
retrieved_topics.push(topicData); retrieved_topics.push(topicData);

@ -88,6 +88,15 @@ marked.setOptions({
io.sockets.in('topic_' + tid).emit('event:post_deleted', { io.sockets.in('topic_' + tid).emit('event:post_deleted', {
pid: pid pid: pid
}); });
// Delete the thread if it is the last undeleted post
threadTools.get_latest_undeleted_pid(tid, function(err, pid) {
if (err && err.message === 'no-undeleted-pids-found') {
threadTools.delete(tid, -1, function(err) {
if (err) console.log('Error: Could not delete topic (tid: ' + tid + ')');
});
}
});
}); });
}; };

@ -3,7 +3,8 @@ var RDB = require('./redis.js'),
categories = require('./categories.js'), categories = require('./categories.js'),
user = require('./user.js'), user = require('./user.js'),
async = require('async'), async = require('async'),
notifications = require('./notifications.js'); notifications = require('./notifications.js'),
posts = require('./posts');
(function(ThreadTools) { (function(ThreadTools) {
@ -80,25 +81,20 @@ var RDB = require('./redis.js'),
}); });
} }
ThreadTools.delete = function(tid, uid, socket) { ThreadTools.delete = function(tid, uid, callback) {
ThreadTools.privileges(tid, uid, function(privileges) { ThreadTools.privileges(tid, uid, function(privileges) {
if (privileges.editable) { if (privileges.editable || uid === -1) {
topics.setTopicField(tid, 'deleted', 1); topics.setTopicField(tid, 'deleted', 1);
ThreadTools.lock(tid, uid); ThreadTools.lock(tid, uid);
if (socket) { io.sockets.in('topic_' + tid).emit('event:topic_deleted', {
io.sockets.in('topic_' + tid).emit('event:topic_deleted', { tid: tid,
tid: tid, status: 'ok'
status: 'ok' });
});
socket.emit('api:topic.delete', { callback(null);
status: 'ok', } else callback(new Error('not-enough-privs'));
tid: tid
});
}
}
}); });
} }
@ -243,10 +239,12 @@ var RDB = require('./redis.js'),
function(next) { function(next) {
topics.getTopicField(tid, 'title', function(title) { topics.getTopicField(tid, 'title', function(title) {
topics.getTeaser(tid, function(teaser) { topics.getTeaser(tid, function(err, teaser) {
if (!err) {
notifications.create(teaser.username + ' has posted a reply to: "' + title + '"', null, '/topic/' + tid, 'topic:' + tid, function(nid) { notifications.create(teaser.username + ' has posted a reply to: "' + title + '"', null, '/topic/' + tid, 'topic:' + tid, function(nid) {
next(null, nid); next(null, nid);
}); });
} else next(err);
}); });
}); });
@ -259,10 +257,28 @@ var RDB = require('./redis.js'),
}); });
} }
], function(err, results) { ], function(err, results) {
if (!err) { if (!err) notifications.push(results[0], results[1]);
notifications.push(results[0], results[1]); // Otherwise, do nothing
}
}); });
} }
ThreadTools.get_latest_undeleted_pid = function(tid, callback) {
posts.getPostsByTid(tid, 0, -1, function(posts) {
var numPosts = posts.length;
if(!numPosts)
return callback(new Error('no-undeleted-pids-found'));
while(numPosts--) {
if(posts[numPosts].deleted !== '1') {
callback(null, posts[numPosts].pid);
return;
}
}
// If we got here, nothing was found...
callback(new Error('no-undeleted-pids-found'));
});
}
}(exports)); }(exports));

@ -153,7 +153,8 @@ marked.setOptions({
} }
function getTeaser(next) { function getTeaser(next) {
Topics.getTeaser(tid, function(teaser) { Topics.getTeaser(tid, function(err, teaser) {
if (err) teaser = {};
next(null, teaser); next(null, teaser);
}); });
} }
@ -169,9 +170,9 @@ marked.setOptions({
topicData.relativeTime = utils.relativeTime(topicData.timestamp); topicData.relativeTime = utils.relativeTime(topicData.timestamp);
topicData.badgeclass = hasRead ? '' : 'badge-important'; topicData.badgeclass = hasRead ? '' : 'badge-important';
topicData.teaser_text = teaser.text; topicData.teaser_text = teaser.text || '';
topicData.teaser_username = teaser.username; topicData.teaser_username = teaser.username || '';
topicData.teaser_timestamp = utils.relativeTime(teaser.timestamp); topicData.teaser_timestamp = teaser.timestamp ? utils.relativeTime(teaser.timestamp) : '';
callback(topicData); callback(topicData);
}); });
@ -262,52 +263,23 @@ marked.setOptions({
} }
Topics.getTeasers = function(tids, callback) { Topics.getTeasers = function(tids, callback) {
var requests = []; var teasers = [];
if (Array.isArray(tids)) { if (Array.isArray(tids)) {
for(x=0,numTids=tids.length;x<numTids;x++) { async.each(tids, function(tid, next) {
(function(tid) { Topics.getTeaser(tid, function(err, teaser_info) {
requests.push(function(next) { if (err) teaser_info = {};
Topics.getTeaser(tid, function(teaser_info) { teasers.push(teaser_info);
next(null, teaser_info); next();
}); });
}); }, function() {
})(tids[x]);
}
async.parallel(requests, function(err, teasers) {
callback(teasers); callback(teasers);
}); });
} else { } else callback(teasers);
callback([]);
}
}
Topics.get_latest_undeleted_pid = function(tid, callback) {
posts.getPostsByTid(tid, 0, -1, function(posts) {
var numPosts = posts.length;
if(!numPosts)
callback(null);
while(numPosts--) {
if(posts[numPosts].deleted !== '1') {
callback(posts[numPosts].pid);
return;
}
}
if(posts.length > 0)
callback(posts[0].pid);
else
callback(null);
});
} }
Topics.getTeaser = function(tid, callback) { Topics.getTeaser = function(tid, callback) {
Topics.get_latest_undeleted_pid(tid, function(pid) { threadTools.get_latest_undeleted_pid(tid, function(err, pid) {
if (!err) {
if (pid !== null) {
posts.getPostFields(pid, ['content', 'uid', 'timestamp'], function(postData) { posts.getPostFields(pid, ['content', 'uid', 'timestamp'], function(postData) {
user.getUserFields(postData.uid, ['username', 'picture'], function(userData) { user.getUserFields(postData.uid, ['username', 'picture'], function(userData) {
@ -317,7 +289,7 @@ marked.setOptions({
if(postData.content) if(postData.content)
stripped = utils.strip_tags(marked(postData.content)); stripped = utils.strip_tags(marked(postData.content));
callback({ callback(null, {
"text": stripped, "text": stripped,
"username": userData.username, "username": userData.username,
"picture": userData.picture, "picture": userData.picture,
@ -325,14 +297,7 @@ marked.setOptions({
}); });
}); });
}); });
} else { } else callback(new Error('no-teaser-found'));
callback({
"text": "",
"username": "",
"picture": "",
"timestamp" : ""
});
}
}); });
} }

@ -336,7 +336,14 @@ var SocketIO = require('socket.io').listen(global.server, { log:false }),
}); });
socket.on('api:topic.delete', function(data) { socket.on('api:topic.delete', function(data) {
threadTools.delete(data.tid, uid, socket); threadTools.delete(data.tid, uid, function(err) {
if (!err) {
socket.emit('api:topic.delete', {
status: 'ok',
tid: data.tid
});
}
});
}); });
socket.on('api:topic.restore', function(data) { socket.on('api:topic.restore', function(data) {

Loading…
Cancel
Save