From 31f8be8a0a398acdea8cca63b89448db333c95b4 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Mon, 15 Jul 2013 15:03:42 -0400 Subject: [PATCH 1/3] updating topics.getTeaser and get_last_undeleted_pid to return err first, and handled methods that called it --- src/categories.js | 12 +++++----- src/threadTools.js | 13 +++++----- src/topics.js | 60 ++++++++++++++++++---------------------------- 3 files changed, 36 insertions(+), 49 deletions(-) diff --git a/src/categories.js b/src/categories.js index a88c79a1d2..117c57dbc1 100644 --- a/src/categories.js +++ b/src/categories.js @@ -132,8 +132,8 @@ var RDB = require('./redis.js'), } function getTeaserInfo(next) { - topics.getTeaser(topicData.tid, function(teaser) { - next(null, teaser); + topics.getTeaser(topicData.tid, function(err, teaser) { + next(null, teaser || {}); }); } @@ -178,10 +178,10 @@ var RDB = require('./redis.js'), topicData.username = topicInfo.username; topicData.badgeclass = (topicInfo.hasread && current_user != 0) ? '' : 'badge-important'; - topicData.teaser_text = topicInfo.teaserInfo.text, - topicData.teaser_username = topicInfo.teaserInfo.username; - topicData.teaser_userpicture = topicInfo.teaserInfo.picture; - topicData.teaser_timestamp = utils.relativeTime(topicInfo.teaserInfo.timestamp); + topicData.teaser_text = topicInfo.teaserInfo.text || '', + topicData.teaser_username = topicInfo.teaserInfo.username || ''; + topicData.teaser_userpicture = topicInfo.teaserInfo.picture || ''; + topicData.teaser_timestamp = topicInfo.teaserInfo.timestamp ? utils.relativeTime(topicInfo.teaserInfo.timestamp) : ''; if (isTopicVisible(topicData, topicInfo)) retrieved_topics.push(topicData); diff --git a/src/threadTools.js b/src/threadTools.js index fc870d846b..b5dd1fe2bc 100644 --- a/src/threadTools.js +++ b/src/threadTools.js @@ -243,10 +243,12 @@ var RDB = require('./redis.js'), function(next) { 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) { - next(null, nid); - }); + next(null, nid); + }); + } else next(err); }); }); @@ -259,9 +261,8 @@ var RDB = require('./redis.js'), }); } ], function(err, results) { - if (!err) { - notifications.push(results[0], results[1]); - } + if (!err) notifications.push(results[0], results[1]); + // Otherwise, do nothing }); } diff --git a/src/topics.js b/src/topics.js index 760c12e609..0282825531 100644 --- a/src/topics.js +++ b/src/topics.js @@ -153,7 +153,8 @@ marked.setOptions({ } function getTeaser(next) { - Topics.getTeaser(tid, function(teaser) { + Topics.getTeaser(tid, function(err, teaser) { + if (err) teaser = {}; next(null, teaser); }); } @@ -169,9 +170,9 @@ marked.setOptions({ topicData.relativeTime = utils.relativeTime(topicData.timestamp); topicData.badgeclass = hasRead ? '' : 'badge-important'; - topicData.teaser_text = teaser.text; - topicData.teaser_username = teaser.username; - topicData.teaser_timestamp = utils.relativeTime(teaser.timestamp); + topicData.teaser_text = teaser.text || ''; + topicData.teaser_username = teaser.username || ''; + topicData.teaser_timestamp = teaser.timestamp ? utils.relativeTime(teaser.timestamp) : ''; callback(topicData); }); @@ -262,23 +263,18 @@ marked.setOptions({ } Topics.getTeasers = function(tids, callback) { - var requests = []; + var teasers = []; if (Array.isArray(tids)) { - for(x=0,numTids=tids.length;x 0) - callback(posts[0].pid); - else - callback(null); + + // If we got here, nothing was found... + callback(new Error('no-undeleted-pids-found')); }); } Topics.getTeaser = function(tid, callback) { - Topics.get_latest_undeleted_pid(tid, function(pid) { - console.log(pid); - if (pid !== null) { - + Topics.get_latest_undeleted_pid(tid, function(err, pid) { + if (!err) { posts.getPostFields(pid, ['content', 'uid', 'timestamp'], function(postData) { user.getUserFields(postData.uid, ['username', 'picture'], function(userData) { @@ -317,7 +310,7 @@ marked.setOptions({ if(postData.content) stripped = utils.strip_tags(marked(postData.content)); - callback({ + callback(null, { "text": stripped, "username": userData.username, "picture": userData.picture, @@ -325,14 +318,7 @@ marked.setOptions({ }); }); }); - } else { - callback({ - "text": "", - "username": "", - "picture": "", - "timestamp" : "" - }); - } + } else callback(new Error('no-teaser-found')); }); } From 87aec422e95e5ea0716b693eb428aac37416f655 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Mon, 15 Jul 2013 15:08:54 -0400 Subject: [PATCH 2/3] moved get_last_undeleted_pid to threadTools.js --- src/threadTools.js | 22 +++++++++++++++++++++- src/topics.js | 23 +---------------------- 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/src/threadTools.js b/src/threadTools.js index b5dd1fe2bc..8b7355f19c 100644 --- a/src/threadTools.js +++ b/src/threadTools.js @@ -3,7 +3,8 @@ var RDB = require('./redis.js'), categories = require('./categories.js'), user = require('./user.js'), async = require('async'), - notifications = require('./notifications.js'); + notifications = require('./notifications.js'), + posts = require('./posts'); (function(ThreadTools) { @@ -266,4 +267,23 @@ var RDB = require('./redis.js'), }); } + 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)); \ No newline at end of file diff --git a/src/topics.js b/src/topics.js index 0282825531..a616df8bb3 100644 --- a/src/topics.js +++ b/src/topics.js @@ -277,29 +277,8 @@ marked.setOptions({ } else callback(teasers); } - - Topics.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')); - }); - } - Topics.getTeaser = function(tid, callback) { - Topics.get_latest_undeleted_pid(tid, function(err, pid) { + threadTools.get_latest_undeleted_pid(tid, function(err, pid) { if (!err) { posts.getPostFields(pid, ['content', 'uid', 'timestamp'], function(postData) { From 561ff38e5a2ff6ccb699bbdfc6ff5898110d98b0 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Mon, 15 Jul 2013 15:25:31 -0400 Subject: [PATCH 3/3] if every post gets deleted in a topic, then the topic deletes itself also --- src/postTools.js | 9 +++++++++ src/threadTools.js | 21 ++++++++------------- src/websockets.js | 9 ++++++++- 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/src/postTools.js b/src/postTools.js index 609124a384..a0e5f72ebd 100644 --- a/src/postTools.js +++ b/src/postTools.js @@ -88,6 +88,15 @@ marked.setOptions({ io.sockets.in('topic_' + tid).emit('event:post_deleted', { 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 + ')'); + }); + } + }); }); }; diff --git a/src/threadTools.js b/src/threadTools.js index 8b7355f19c..f3d5ceca42 100644 --- a/src/threadTools.js +++ b/src/threadTools.js @@ -81,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) { - if (privileges.editable) { + if (privileges.editable || uid === -1) { topics.setTopicField(tid, 'deleted', 1); ThreadTools.lock(tid, uid); - if (socket) { - io.sockets.in('topic_' + tid).emit('event:topic_deleted', { - tid: tid, - status: 'ok' - }); + io.sockets.in('topic_' + tid).emit('event:topic_deleted', { + tid: tid, + status: 'ok' + }); - socket.emit('api:topic.delete', { - status: 'ok', - tid: tid - }); - } - } + callback(null); + } else callback(new Error('not-enough-privs')); }); } diff --git a/src/websockets.js b/src/websockets.js index 9a703ebb0c..5815d597c4 100644 --- a/src/websockets.js +++ b/src/websockets.js @@ -286,7 +286,14 @@ var SocketIO = require('socket.io').listen(global.server, { log:false }), }); 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) {