diff --git a/public/src/utils.js b/public/src/utils.js index b4da160888..ce2bf395e0 100644 --- a/public/src/utils.js +++ b/public/src/utils.js @@ -182,7 +182,17 @@ return (n / 1000).toFixed(1) + 'k'; } return n; + }, + + toISOString: function(timestamp) { + try { + return new Date(parseInt(timestamp, 10)).toISOString(); + } catch(e){ + console.log(e.message); + } + return new Date(parseInt(0, 10)).toISOString(); } + }; diff --git a/src/posts.js b/src/posts.js index bf3be7bfdb..405865bca4 100644 --- a/src/posts.js +++ b/src/posts.js @@ -189,7 +189,7 @@ var db = require('./database'), if (parseInt(postData.deleted, 10) === 1) { return callback(null); } else { - postData.relativeTime = new Date(parseInt(postData.timestamp || 0, 10)).toISOString(); + postData.relativeTime = utils.toISOString(postData.timestamp); next(null, postData); } }); @@ -318,30 +318,28 @@ var db = require('./database'), } db.getObjects(keys, function(err, data) { - async.map(data, function(postData, _callback) { - if (postData) { - - try { - postData.relativeTime = new Date(parseInt(postData.timestamp,10)).toISOString(); - postData.relativeEditTime = parseInt(postData.edited, 10) !== 0 ? (new Date(parseInt(postData.edited, 10)).toISOString()) : ''; - } catch(e) { - require('winston').err('invalid time value'); - } + if(err) { + return callback(err); + } - postTools.parse(postData.content, function(err, content) { - postData.content = content; - _callback(null, postData); - }); - } else { - _callback(null); + async.map(data, function(postData, next) { + if(!postData) { + return next(null); } - }, function(err, posts) { - if (!err) { - return callback(null, posts); - } else { - return callback(err, null); - } - }); + + postData.relativeTime = utils.toISOString(postData.timestamp); + postData.relativeEditTime = parseInt(postData.edited, 10) !== 0 ? utils.toISOString(postData.edited) : ''; + + postTools.parse(postData.content, function(err, content) { + if(err) { + return next(err); + } + + postData.content = content; + next(null, postData); + }); + + }, callback); }); } diff --git a/src/routes/user.js b/src/routes/user.js index bbd8fca5bd..40bd5713c2 100644 --- a/src/routes/user.js +++ b/src/routes/user.js @@ -532,9 +532,9 @@ var fs = require('fs'), user.getUserData(uid, function (err, data) { if (data) { - data.joindate = new Date(parseInt(data.joindate, 10)).toISOString(); + data.joindate = utils.toISOString(data.joindate); if(data.lastonline) { - data.lastonline = new Date(parseInt(data.lastonline, 10)).toISOString(); + data.lastonline = utils.toISOString(data.lastonline); } else { data.lastonline = data.joindate; } diff --git a/src/threadTools.js b/src/threadTools.js index d0ccd20e50..dfcdb72fab 100644 --- a/src/threadTools.js +++ b/src/threadTools.js @@ -259,6 +259,16 @@ var winston = require('winston'), }); } + ThreadTools.getLatestUndeletedPost = function(tid, callback) { + ThreadTools.getLatestUndeletedPid(tid, function(err, pid) { + if(err) { + return callback(err); + } + + posts.getPostData(pid, callback); + }); + } + ThreadTools.getLatestUndeletedPid = function(tid, callback) { db.getSortedSetRevRange('tid:' + tid + ':posts', 0, -1, function(err, pids) { if(err) { diff --git a/src/topics.js b/src/topics.js index 44713da623..9a57648702 100644 --- a/src/topics.js +++ b/src/topics.js @@ -211,7 +211,7 @@ var async = require('async'), postData.favourited = false; postData.display_moderator_tools = true; - postData.relativeTime = new Date(postData.timestamp).toISOString(); + postData.relativeTime = utils.toISOString(postData.timestamp); callback(null, postData); }); @@ -314,9 +314,7 @@ var async = require('async'), if(data) { data.title = validator.sanitize(data.title).escape(); - if(data.timestamp) { - data.relativeTime = new Date(parseInt(data.timestamp, 10)).toISOString(); - } + data.relativeTime = utils.toISOString(data.timestamp); } callback(null, data); @@ -726,7 +724,7 @@ var async = require('async'), topicData.teaser_userslug = topicInfo.teaserInfo.userslug || ''; topicData.teaser_userpicture = topicInfo.teaserInfo.picture || gravatar.url('', {}, https = nconf.get('https')); topicData.teaser_pid = topicInfo.teaserInfo.pid; - topicData.teaser_timestamp = topicInfo.teaserInfo.timestamp ? (new Date(parseInt(topicInfo.teaserInfo.timestamp, 10)).toISOString()) : ''; + topicData.teaser_timestamp = utils.toISOString(topicInfo.teaserInfo.timestamp); if (isTopicVisible(topicData, topicInfo)) { retrieved_topics.push(topicData); @@ -775,8 +773,7 @@ var async = require('async'), async.parallel([getTopicData, getTopicPosts, getPrivileges, getCategoryData], function(err, results) { if (err) { winston.error('[Topics.getTopicWithPosts] Could not retrieve topic data: ', err.message); - callback(err, null); - return; + return callback(err, null); } var topicData = results[0], @@ -791,6 +788,7 @@ var async = require('async'), 'locked': topicData.locked, 'deleted': topicData.deleted, 'pinned': topicData.pinned, + 'timestamp': topicData.timestamp, 'slug': topicData.slug, 'postcount': topicData.postcount, 'viewcount': topicData.viewcount, @@ -844,7 +842,7 @@ var async = require('async'), topicData.teaser_username = teaser.username || ''; topicData.teaser_userslug = teaser.userslug || ''; topicData.userslug = teaser.userslug || ''; - topicData.teaser_timestamp = teaser.timestamp ? (new Date(parseInt(teaser.timestamp,10)).toISOString()) : ''; + topicData.teaser_timestamp = utils.toISOString(teaser.timestamp); topicData.teaser_userpicture = teaser.picture; callback(topicData); diff --git a/src/user.js b/src/user.js index 9a6e1dc133..066cd6eb43 100644 --- a/src/user.js +++ b/src/user.js @@ -1022,7 +1022,7 @@ var bcrypt = require('bcrypt'), }).sort(function(a, b) { return parseInt(b.datetime, 10) - parseInt(a.datetime, 10); }).map(function(notif) { - notif.datetimeISO = new Date(parseInt(notif.datetime, 10)).toISOString(); + notif.datetimeISO = utils.toISOString(notif.datetime); notif.readClass = !notif.read ? 'unread' : ''; return notif; diff --git a/src/webserver.js b/src/webserver.js index ca318f0d66..c2bdb58bf8 100644 --- a/src/webserver.js +++ b/src/webserver.js @@ -536,10 +536,14 @@ if(nconf.get('ssl')) { }); }, function (topicData, next) { - var lastMod = 0, + + var lastMod = topicData.timestamp, sanitize = validator.sanitize, description = (function() { - var content = S(topicData.posts[0].content).stripTags().s; + var content = ''; + if(topicData.posts.length) { + content = S(topicData.posts[0].content).stripTags().s; + } if (content.length > 255) { content = content.substr(0, 255) + '...'; @@ -586,15 +590,15 @@ if(nconf.get('ssl')) { }, { property: 'og:image', - content: topicData.posts[0].picture + content: topicData.posts.length?topicData.posts[0].picture:'' }, { property: "article:published_time", - content: new Date(parseInt(topicData.posts[0].timestamp, 10)).toISOString() + content: utils.toISOString(topicData.timestamp) }, { property: 'article:modified_time', - content: new Date(lastMod).toISOString() + content: utils.toISOString(lastMod) }, { property: 'article:section',