Node.js based forum software built for the modern web
relative time on topics and posts
Julian Lam62779ef0dde4
diff --git a/public/templates/home.tpl b/public/templates/home.tplindex 3149404251..e65f469776 100644--- a/public/templates/home.tpl+++ b/public/templates/home.tpl@@ -3,7 +3,7 @@<!-- BEGIN topics --><a href="topics/{topics.slug}"><li class="topic-row"><h4>{topics.title}</h4>- <p>Posted on {topics.timestamp} by user {topics.uid}. {topics.post_count} posts.</p>+ <p>Posted {topics.relativeTime} by user {topics.uid}. {topics.post_count} posts.</p></li></a><!-- END topics --></ul>diff --git a/public/templates/topic.tpl b/public/templates/topic.tplindex 56c230a31f..777c72fb89 100644--- a/public/templates/topic.tpl+++ b/public/templates/topic.tpl@@ -2,7 +2,7 @@<!-- BEGIN posts --><li class="topic-row"><p>{posts.content}</p>- <p>Posted on {posts.timestamp} by user {posts.uid}.</p>+ <p>Posted {posts.relativeTime} by user {posts.uid}.</p></li><!-- END posts --></ul>\ No newline at end of filediff --git a/src/posts.js b/src/posts.jsindex c34d088232..c0d7e50acc 100644--- a/src/posts.js+++ b/src/posts.js@@ -1,4 +1,5 @@-var RDB = require('./redis.js');+var RDB = require('./redis.js'),+ utils = require('./utils.js');(function(Posts) {//data structure@@ -42,7 +43,8 @@ var RDB = require('./redis.js');posts.push({'content' : content[i],'uid' : uid[i],- 'timestamp' : timestamp[i]+ 'timestamp' : timestamp[i],+ 'relativeTime': utils.relativeTime(timestamp[i])});}diff --git a/src/topics.js b/src/topics.jsindex fdc76cd8c7..aa41f8684e 100644--- a/src/topics.js+++ b/src/topics.js@@ -1,8 +1,6 @@var RDB = require('./redis.js'),- posts = require('./posts.js');---+ posts = require('./posts.js'),+ utils = require('./utils.js');(function(Topics) {//data structure@@ -82,6 +80,7 @@ var RDB = require('./redis.js'),'title' : title[i],'uid' : uid[i],'timestamp' : timestamp[i],+ 'relativeTime': utils.relativeTime(timestamp[i]),'slug' : slug[i],'post_count' : postcount[i]});diff --git a/src/user.js b/src/user.jsindex 59589271dc..a743a39abe 100644--- a/src/user.js+++ b/src/user.js@@ -1,5 +1,5 @@var config = require('../config.js'),- utils = require('../utils.js'),+ utils = require('./utils.js'),RDB = require('./redis.js'),crypto = require('crypto'),emailjs = require('emailjs'),diff --git a/src/utils.js b/src/utils.jsnew file mode 100644index 0000000000..6728175336--- /dev/null+++ b/src/utils.js@@ -0,0 +1,35 @@+var utils = {+ generateUUID: function() {+ return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {+ var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);+ return v.toString(16);+ });+ },+ relativeTime: function(timestamp) {+ var now = +new Date(),+ difference = now - Math.floor(parseFloat(timestamp));++ difference = Math.floor(difference / 1000);+ if (difference < 60) return difference + ' second' + (difference !== 1 ? 's' : '') + ' ago';++ difference = Math.floor(difference / 60);+ if (difference < 60) return difference + ' minute' + (difference !== 1 ? 's' : '') + ' ago';++ difference = Math.floor(difference / 60);+ if (difference < 24) return difference + ' hour' + (difference !== 1 ? 's' : '') + ' ago';++ difference = Math.floor(difference / 24);+ if (difference < 3) return difference + ' day' + (difference !== 1 ? 's' : '') + ' ago';++ // Lastly, just return a formatted date+ var date = new Date(timestamp);+ // hour = date.getHours(),+ // minute = date.getMinutes(),+ // day = date.getDate(),+ // month = date.getMonth(),+ // months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];+ return date.toDateString();+ }+}++module.exports = utils;\ No newline at end of filediff --git a/utils.js b/utils.jsindex 7b003e466a..6728175336 100644--- a/utils.js+++ b/utils.js@@ -4,6 +4,31 @@ var utils = {var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);return v.toString(16);});+ },+ relativeTime: function(timestamp) {+ var now = +new Date(),+ difference = now - Math.floor(parseFloat(timestamp));++ difference = Math.floor(difference / 1000);+ if (difference < 60) return difference + ' second' + (difference !== 1 ? 's' : '') + ' ago';++ difference = Math.floor(difference / 60);+ if (difference < 60) return difference + ' minute' + (difference !== 1 ? 's' : '') + ' ago';++ difference = Math.floor(difference / 60);+ if (difference < 24) return difference + ' hour' + (difference !== 1 ? 's' : '') + ' ago';++ difference = Math.floor(difference / 24);+ if (difference < 3) return difference + ' day' + (difference !== 1 ? 's' : '') + ' ago';++ // Lastly, just return a formatted date+ var date = new Date(timestamp);+ // hour = date.getHours(),+ // minute = date.getMinutes(),+ // day = date.getDate(),+ // month = date.getMonth(),+ // months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];+ return date.toDateString();}}