From ea6cf3bbd576be7a9f06f155fc804b14f58daf55 Mon Sep 17 00:00:00 2001 From: Baris Soner Usakli <barisusakli@gmail.com> Date: Wed, 26 Feb 2014 16:43:21 -0500 Subject: [PATCH 1/2] more cleanup and changes to topics --- public/templates/category.tpl | 12 +-- public/templates/noscript/category.tpl | 6 +- public/templates/noscript/topic.tpl | 4 +- public/templates/topic.tpl | 12 +-- src/categories.js | 42 ++++------ src/routes/api.js | 53 ++++++++----- src/routes/feeds.js | 12 +-- src/socket.io/posts.js | 1 - src/socket.io/topics.js | 2 +- src/topics.js | 103 +++++++++---------------- src/webserver.js | 16 ++-- tests/categories.js | 4 +- 12 files changed, 119 insertions(+), 148 deletions(-) diff --git a/public/templates/category.tpl b/public/templates/category.tpl index 24afd18f2d..d536a201cb 100644 --- a/public/templates/category.tpl +++ b/public/templates/category.tpl @@ -1,9 +1,15 @@ + +<input type="hidden" template-variable="category_id" value="{cid}" /> +<input type="hidden" template-variable="category_name" value="{name}" /> +<input type="hidden" template-variable="currentPage" value="{currentPage}" /> +<input type="hidden" template-variable="pageCount" value="{pageCount}" /> + <ol class="breadcrumb"> <li itemscope="itemscope" itemtype="http://data-vocabulary.org/Breadcrumb"> <a href="{relative_path}/" itemprop="url"><span itemprop="title">[[global:home]]</span></a> </li> <li class="active" itemscope="itemscope" itemtype="http://data-vocabulary.org/Breadcrumb"> - <span itemprop="title">{category_name} <a target="_blank" href="../{category_id}.rss"><i class="fa fa-rss-square"></i></a></span> + <span itemprop="title">{name} <a target="_blank" href="../{cid}.rss"><i class="fa fa-rss-square"></i></a></span> </li> </ol> @@ -109,7 +115,3 @@ <!-- ENDIF topics.length --> </div> -<input type="hidden" template-variable="category_id" value="{category_id}" /> -<input type="hidden" template-variable="category_name" value="{category_name}" /> -<input type="hidden" template-variable="currentPage" value="{currentPage}" /> -<input type="hidden" template-variable="pageCount" value="{pageCount}" /> \ No newline at end of file diff --git a/public/templates/noscript/category.tpl b/public/templates/noscript/category.tpl index ccfeffd740..52122e121c 100644 --- a/public/templates/noscript/category.tpl +++ b/public/templates/noscript/category.tpl @@ -3,17 +3,17 @@ <a href="{relative_path}/" itemprop="url"><span itemprop="title">[[global:home]]</span></a> </li> <li class="active" itemscope="itemscope" itemtype="http://data-vocabulary.org/Breadcrumb"> - <span itemprop="title">{category_name} <a target="_blank" href="../{category_id}.rss"><i class="fa fa-rss-square"></i></a></span> + <span itemprop="title">{name} <a target="_blank" href="../{cid}.rss"><i class="fa fa-rss-square"></i></a></span> </li> </ol> <ul class="topics" itemscope itemtype="http://www.schema.org/ItemList" data-nextstart="{nextStart}"> <!-- BEGIN topics --> <li itemprop="itemListElement"> <meta itemprop="name" content="{topics.title}"> - <span class="timestamp">{topics.teaser_timestamp}</span> + <span class="timestamp">{topics.teaser.timestamp}</span> <a href="../../topic/{topics.slug}" itemprop="url">{topics.title} ({topics.postcount})</a> <div class="teaser"> - <img class="img-thumbnail" src="{topics.teaser_userpicture}" /> + <img class="img-thumbnail" src="{topics.teaser.picture}" /> <div class="clear"></div> </div> </li> diff --git a/public/templates/noscript/topic.tpl b/public/templates/noscript/topic.tpl index f3d5180fe2..cf8b968a90 100644 --- a/public/templates/noscript/topic.tpl +++ b/public/templates/noscript/topic.tpl @@ -3,10 +3,10 @@ <a href="{relative_path}/" itemprop="url"><span itemprop="title">[[global:home]]</span></a> </li> <li itemscope="itemscope" itemtype="http://data-vocabulary.org/Breadcrumb"> - <a href="{relative_path}/category/{category_slug}" itemprop="url"><span itemprop="title">{category_name}</span></a> + <a href="{relative_path}/category/{category.slug}" itemprop="url"><span itemprop="title">{category.name}</span></a> </li> <li class="active" itemscope="itemscope" itemtype="http://data-vocabulary.org/Breadcrumb"> - <span itemprop="title">{topic_name} <a target="_blank" href="../{topic_id}.rss"><i class="fa fa-rss-square"></i></a></span> + <span itemprop="title">{title} <a target="_blank" href="../{tid}.rss"><i class="fa fa-rss-square"></i></a></span> </li> </ol> <ul class="posts"> diff --git a/public/templates/topic.tpl b/public/templates/topic.tpl index ef0d794d88..3b6cfa72e8 100644 --- a/public/templates/topic.tpl +++ b/public/templates/topic.tpl @@ -1,11 +1,11 @@ <input type="hidden" template-variable="expose_tools" value="{expose_tools}" /> -<input type="hidden" template-variable="topic_id" value="{topic_id}" /> +<input type="hidden" template-variable="topic_id" value="{tid}" /> <input type="hidden" template-variable="currentPage" value="{currentPage}" /> <input type="hidden" template-variable="pageCount" value="{pageCount}" /> <input type="hidden" template-variable="locked" value="{locked}" /> <input type="hidden" template-variable="deleted" value="{deleted}" /> <input type="hidden" template-variable="pinned" value="{pinned}" /> -<input type="hidden" template-variable="topic_name" value="{topic_name}" /> +<input type="hidden" template-variable="topic_name" value="{title}" /> <input type="hidden" template-variable="postcount" value="{postcount}" /> <div class="topic"> @@ -14,14 +14,14 @@ <a href="{relative_path}/" itemprop="url"><span itemprop="title">[[global:home]]</span></a> </li> <li itemscope="itemscope" itemtype="http://data-vocabulary.org/Breadcrumb"> - <a href="{relative_path}/category/{category_slug}" itemprop="url"><span itemprop="title">{category_name}</span></a> + <a href="{relative_path}/category/{category.slug}" itemprop="url"><span itemprop="title">{category.name}</span></a> </li> <li class="active" itemscope="itemscope" itemtype="http://data-vocabulary.org/Breadcrumb"> - <span itemprop="title">{topic_name} <a target="_blank" href="../{topic_id}.rss"><i class="fa fa-rss-square"></i></a></span> + <span itemprop="title">{title} <a target="_blank" href="../{tid}.rss"><i class="fa fa-rss-square"></i></a></span> </li> </ol> - <ul id="post-container" class="posts" data-tid="{topic_id}"> + <ul id="post-container" class="posts" data-tid="{tid}"> <!-- BEGIN posts --> <li class="post-row infiniteloaded" data-pid="{posts.pid}" data-uid="{posts.uid}" data-username="{posts.username}" data-userslug="{posts.userslug}" data-index="{posts.index}" data-deleted="{posts.deleted}" itemscope itemtype="http://schema.org/Comment"> <a id="post_anchor_{posts.pid}" name="{posts.pid}"></a> @@ -44,7 +44,7 @@ <img itemprop="image" src="{posts.picture}" align="left" class="img-thumbnail" width=150 height=150 /> </a> <h3 class="main-post"> - <p id="topic_title_{posts.pid}" class="topic-title" itemprop="name">{topic_name}</p> + <p id="topic_title_{posts.pid}" class="topic-title" itemprop="name">{title}</p> </h3> <div class="topic-buttons"> diff --git a/src/categories.js b/src/categories.js index 5eafaff6cb..577fa61fac 100644 --- a/src/categories.js +++ b/src/categories.js @@ -59,39 +59,27 @@ var db = require('./database'), Categories.markAsRead(cid, uid); } - function getCategoryData(next) { - Categories.getCategoryData(cid, next); - } - - function getTopics(next) { - Categories.getCategoryTopics(cid, start, end, uid, next); - } - - function getPageCount(next) { - Categories.getPageCount(cid, uid, next); - } - async.parallel({ - 'category': getCategoryData, - 'topics': getTopics, - 'pageCount': getPageCount + category: function(next) { + Categories.getCategoryData(cid, next); + }, + topics : function(next) { + Categories.getCategoryTopics(cid, start, end, uid, next); + }, + pageCount: function(next) { + Categories.getPageCount(cid, uid, next); + } }, function(err, results) { if(err) { return callback(err); } - var category = { - 'category_name': results.category.name, - 'category_description': results.category.description, - 'link': results.category.link, - 'disabled': results.category.disabled, - 'topic_row_size': 'col-md-9', - 'category_id': cid, - 'topics': results.topics.topics, - 'nextStart': results.topics.nextStart, - 'pageCount': results.pageCount, - 'disableSocialButtons': meta.config.disableSocialButtons !== undefined ? parseInt(meta.config.disableSocialButtons, 10) !== 0 : false, - }; + var category = results.category; + category.topics = results.topics.topics; + category.nextStart = results.topics.nextStart; + category.pageCount = results.pageCount; + category.disableSocialButtons = meta.config.disableSocialButtons !== undefined ? parseInt(meta.config.disableSocialButtons, 10) !== 0 : false; + category.topic_row_size = 'col-md-9'; callback(null, category); }); diff --git a/src/routes/api.js b/src/routes/api.js index 7cdd7a8589..89ec9fe2f4 100644 --- a/src/routes/api.js +++ b/src/routes/api.js @@ -169,7 +169,8 @@ var path = require('path'), }); app.get('/topic/:id/:slug?', function (req, res, next) { - var uid = (req.user) ? req.user.uid : 0; + var uid = req.user? parseInt(req.user.uid, 10) : 0; + var tid = req.params.id; var page = 1; if(req.query && req.query.page) { page = req.query.page; @@ -187,29 +188,41 @@ var path = require('path'), var start = (page - 1) * settings.postsPerPage; var end = start + settings.postsPerPage - 1; - ThreadTools.privileges(req.params.id, uid, function(err, privileges) { - if (privileges.read) { - topics.getTopicWithPosts(req.params.id, uid, start, end, false, function (err, data) { - if(err) { - return next(err); - } + ThreadTools.privileges(tid, uid, function(err, privileges) { + if(err) { + return next(err); + } - if(page > data.pageCount) { - return res.send(404); - } + if(!privileges.read) { + res.send(403); + } - data.currentPage = page; - data.privileges = privileges; + topics.getTopicWithPosts(tid, uid, start, end, function (err, data) { + if(err) { + return next(err); + } - if (parseInt(data.deleted, 10) === 1 && parseInt(data.expose_tools, 10) === 0) { - return res.json(404, {}); - } + if(page > data.pageCount) { + return res.send(404); + } - res.json(data); - }); - } else { - res.send(403); - } + if (parseInt(data.deleted, 10) === 1 && parseInt(data.expose_tools, 10) === 0) { + return res.json(404, {}); + } + + data.currentPage = page; + data.privileges = privileges; + + if (uid) { + topics.markAsRead(tid, uid, function(err) { + topics.pushUnreadCount(uid); + }); + } + + topics.increaseViewCount(tid); + + res.json(data); + }); }); }); }); diff --git a/src/routes/feeds.js b/src/routes/feeds.js index 1b18024742..e377614fb0 100644 --- a/src/routes/feeds.js +++ b/src/routes/feeds.js @@ -55,7 +55,7 @@ function generateForTopic(req, res, next) { var tid = req.params.topic_id; - topics.getTopicWithPosts(tid, 0, 0, 25, true, function (err, topicData) { + topics.getTopicWithPosts(tid, 0, 0, 25, function (err, topicData) { if (err) { return next(err); } @@ -65,7 +65,7 @@ var author = topicData.posts.length ? topicData.posts[0].username : ''; var feed = new rss({ - title: topicData.topic_name, + title: topicData.title, description: description, feed_url: nconf.get('url') + '/topic/' + tid + '.rss', site_url: nconf.get('url') + '/topic/' + topicData.slug, @@ -85,7 +85,7 @@ dateStamp = new Date(parseInt(parseInt(postData.edited, 10) === 0 ? postData.timestamp : postData.edited, 10)).toUTCString(); feed.item({ - title: 'Reply to ' + topicData.topic_name + ' on ' + dateStamp, + title: 'Reply to ' + topicData.title + ' on ' + dateStamp, description: postData.content, url: nconf.get('url') + '/topic/' + topicData.slug + '#' + postData.pid, author: postData.username, @@ -109,10 +109,10 @@ } var feed = new rss({ - title: categoryData.category_name, - description: categoryData.category_description, + title: categoryData.name, + description: categoryData.description, feed_url: nconf.get('url') + '/category/' + cid + '.rss', - site_url: nconf.get('url') + '/category/' + categoryData.category_id, + site_url: nconf.get('url') + '/category/' + categoryData.cid, ttl: 60 }); diff --git a/src/socket.io/posts.js b/src/socket.io/posts.js index 049735b71c..dcbf85daa1 100644 --- a/src/socket.io/posts.js +++ b/src/socket.io/posts.js @@ -217,7 +217,6 @@ SocketPosts.getFavouritedUsers = function(socket, pid, callback) { callback(null, ""); } - console.log(data); var max = 5; //hardcoded var usernames = ""; diff --git a/src/socket.io/topics.js b/src/socket.io/topics.js index 0950e9fd6c..e96fc2611d 100644 --- a/src/socket.io/topics.js +++ b/src/socket.io/topics.js @@ -247,7 +247,7 @@ SocketTopics.loadMore = function(socket, data, callback) { async.parallel({ posts: function(next) { - topics.getTopicPosts(data.tid, start, end, socket.uid, next); + topics.getTopicPosts(data.tid, start, end, socket.uid, false, next); }, privileges: function(next) { threadTools.privileges(data.tid, socket.uid, next); diff --git a/src/topics.js b/src/topics.js index fe062b8b52..328f50ae9f 100644 --- a/src/topics.js +++ b/src/topics.js @@ -634,7 +634,7 @@ var async = require('async'), }); }; - Topics.getTopicsByTids = function(tids, cid, current_user, callback) { + Topics.getTopicsByTids = function(tids, cid, uid, callback) { if (!Array.isArray(tids) || tids.length === 0) { return callback(null, []); @@ -643,13 +643,13 @@ var async = require('async'), function getTopicInfo(topicData, callback) { async.parallel({ hasread : function (next) { - Topics.hasReadTopic(topicData.tid, current_user, next); + Topics.hasReadTopic(topicData.tid, uid, next); }, teaser : function (next) { Topics.getTeaser(topicData.tid, next); }, privileges : function (next) { - categoryTools.privileges(topicData.cid, current_user, next); + categoryTools.privileges(topicData.cid, uid, next); }, categoryData : function (next) { categories.getCategoryFields(topicData.cid, ['name', 'slug', 'icon'], next); @@ -660,7 +660,7 @@ var async = require('async'), function isTopicVisible(topicData, topicInfo) { var deleted = parseInt(topicData.deleted, 10) !== 0; - return !deleted || (deleted && topicInfo.privileges.view_deleted) || parseInt(topicData.uid, 10) === parseInt(current_user, 10); + return !deleted || (deleted && topicInfo.privileges.view_deleted) || parseInt(topicData.uid, 10) === parseInt(uid, 10); } function loadTopic(tid, next) { @@ -686,7 +686,7 @@ var async = require('async'), topicData.pinned = parseInt(topicData.pinned, 10) === 1; topicData.locked = parseInt(topicData.locked, 10) === 1; topicData.deleted = parseInt(topicData.deleted, 10) === 1; - topicData.unread = !(topicInfo.hasread && parseInt(current_user, 10) !== 0); + topicData.unread = !(topicInfo.hasread && parseInt(uid, 10) !== 0); topicData.unreplied = parseInt(topicData.postcount, 10) === 1; topicData.category = topicInfo.categoryData; @@ -710,78 +710,47 @@ var async = require('async'), }); }; - Topics.getTopicWithPosts = function(tid, current_user, start, end, quiet, callback) { + Topics.getTopicWithPosts = function(tid, uid, start, end, callback) { threadTools.exists(tid, function(err, exists) { if (err || !exists) { return callback(err || new Error('Topic tid \'' + tid + '\' not found')); } - // "quiet" is used for things like RSS feed updating, HTML parsing for non-js users, etc - if (!quiet) { - Topics.markAsRead(tid, current_user, function(err) { - Topics.pushUnreadCount(current_user); - }); - Topics.increaseViewCount(tid); - } - - function getTopicData(next) { - Topics.getTopicData(tid, next); - } - - function getTopicPosts(next) { - Topics.getTopicPosts(tid, start, end, current_user, false, next); - } - - function getPrivileges(next) { - threadTools.privileges(tid, current_user, next); - } - - function getCategoryData(next) { - Topics.getCategoryData(tid, next); - } - - function getPageCount(next) { - Topics.getPageCount(tid, current_user, next); - } - - function getThreadTools(next) { - Plugins.fireHook('filter:topic.thread_tools', [], function(err, threadTools) { - next(err, threadTools); - }); - } - - async.parallel([getTopicData, getTopicPosts, getPrivileges, getCategoryData, getPageCount, getThreadTools], function(err, results) { + async.parallel({ + topicData : function(next) { + Topics.getTopicData(tid, next); + }, + posts : function(next) { + Topics.getTopicPosts(tid, start, end, uid, false, next); + }, + privileges : function(next) { + threadTools.privileges(tid, uid, next); + }, + category : function(next) { + Topics.getCategoryData(tid, next); + }, + pageCount : function(next) { + Topics.getPageCount(tid, uid, next); + }, + threadTools : function(next) { + Plugins.fireHook('filter:topic.thread_tools', [], next); + } + }, function(err, results) { if (err) { winston.error('[Topics.getTopicWithPosts] Could not retrieve topic data: ', err.message); return callback(err); } - var topicData = results[0], - privileges = results[2], - categoryData = results[3], - pageCount = results[4], - threadTools = results[5]; - - callback(null, { - 'topic_name': topicData.title, - 'category_name': categoryData.name, - 'category_slug': categoryData.slug, - 'locked': topicData.locked, - 'deleted': topicData.deleted, - 'pinned': topicData.pinned, - 'timestamp': topicData.timestamp, - 'slug': topicData.slug, - 'thumb': topicData.thumb, - 'postcount': topicData.postcount, - 'viewcount': topicData.viewcount, - 'pageCount': pageCount, - 'unreplied': parseInt(topicData.postcount, 10) === 1, - 'topic_id': tid, - 'expose_tools': privileges.editable ? 1 : 0, - 'thread_tools': threadTools, - 'disableSocialButtons': meta.config.disableSocialButtons !== undefined ? parseInt(meta.config.disableSocialButtons, 10) !== 0 : false, - 'posts': results[1] - }); + var topicData = results.topicData; + topicData.category = results.category; + topicData.posts = results.posts; + topicData.thread_tools = results.threadTools; + topicData.pageCount = results.pageCount; + topicData.unreplied = parseInt(topicData.postcount, 10) === 1; + topicData.expose_tools = results.privileges.editable ? 1 : 0; + topicData.disableSocialButtons = meta.config.disableSocialButtons !== undefined ? parseInt(meta.config.disableSocialButtons, 10) !== 0 : false; + + callback(null, topicData); }); }); }; diff --git a/src/webserver.js b/src/webserver.js index 9999c1a22e..1fe43f5743 100644 --- a/src/webserver.js +++ b/src/webserver.js @@ -596,7 +596,7 @@ process.on('uncaughtException', function(err) { var start = (page - 1) * settings.topicsPerPage, end = start + settings.topicsPerPage - 1; - topics.getTopicWithPosts(tid, uid, start, end, true, function (err, topicData) { + topics.getTopicWithPosts(tid, uid, start, end, function (err, topicData) { if (topicData) { if (parseInt(topicData.deleted, 10) === 1 && parseInt(topicData.expose_tools, 10) === 0) { return next(new Error('Topic deleted'), null); @@ -642,7 +642,7 @@ process.on('uncaughtException', function(err) { metaTags: [ { name: "title", - content: topicData.topic_name + content: topicData.title }, { name: "description", @@ -650,7 +650,7 @@ process.on('uncaughtException', function(err) { }, { property: 'og:title', - content: topicData.topic_name + content: topicData.title }, { property: 'og:description', @@ -682,7 +682,7 @@ process.on('uncaughtException', function(err) { }, { property: 'article:section', - content: topicData.category_name + content: topicData.category.name } ], linkTags: [ @@ -693,7 +693,7 @@ process.on('uncaughtException', function(err) { }, { rel: 'up', - href: nconf.get('url') + '/category/' + topicData.category_slug + href: nconf.get('url') + '/category/' + topicData.category.slug } ] }, function (err, header) { @@ -783,15 +783,15 @@ process.on('uncaughtException', function(err) { metaTags: [ { name: 'title', - content: categoryData.category_name + content: categoryData.name }, { property: 'og:title', - content: categoryData.category_name + content: categoryData.name }, { name: 'description', - content: categoryData.category_description + content: categoryData.description }, { property: "og:type", diff --git a/tests/categories.js b/tests/categories.js index 770ad0b498..1c1e1fac32 100644 --- a/tests/categories.js +++ b/tests/categories.js @@ -33,8 +33,8 @@ describe('Categories', function() { it('should retrieve a newly created category by its ID', function(done) { Categories.getCategoryById(categoryObj.cid, 0, -1, 0, function(err, categoryData) { assert(categoryData); - assert.equal(categoryObj.name, categoryData.category_name); - assert.equal(categoryObj.description, categoryData.category_description); + assert.equal(categoryObj.name, categoryData.name); + assert.equal(categoryObj.description, categoryData.description); done(); }); From 7f2d70d7f6cda562692e687d6f64987fda7b3aa1 Mon Sep 17 00:00:00 2001 From: Baris Soner Usakli <barisusakli@gmail.com> Date: Wed, 26 Feb 2014 16:58:02 -0500 Subject: [PATCH 2/2] minor cleanups --- src/categories.js | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/categories.js b/src/categories.js index 577fa61fac..248fb4417f 100644 --- a/src/categories.js +++ b/src/categories.js @@ -63,7 +63,7 @@ var db = require('./database'), category: function(next) { Categories.getCategoryData(cid, next); }, - topics : function(next) { + topics: function(next) { Categories.getCategoryTopics(cid, start, end, uid, next); }, pageCount: function(next) { @@ -287,14 +287,14 @@ var db = require('./database'), Categories.getCategories = function(cids, uid, callback) { if (!cids || !Array.isArray(cids) || cids.length === 0) { - return callback(new Error('invalid-cids'), null); + return callback(new Error('invalid-cids')); } function getCategory(cid, callback) { Categories.getCategoryData(cid, function(err, categoryData) { if (err) { winston.warn('Attempted to retrieve cid ' + cid + ', but nothing was returned!'); - return callback(err, null); + return callback(err); } Categories.hasReadCategory(cid, uid, function(hasRead) { @@ -326,7 +326,7 @@ var db = require('./database'), db.getSortedSetRange('uid:' + uid + ':posts', 0, -1, function(err, pids) { if (err) { - return callback(err, null); + return callback(err); } var index = 0, @@ -347,15 +347,11 @@ var db = require('./database'), } ++index; - callback(null); + callback(); }); }, function(err) { - if (err) { - return callback(err, null); - } - - callback(null, active); + callback(err, active); } ); });