Skip to content

Node.js based forum software built for the modern web

removing static saving of recent post, and added in dynamic pulling on Topics.get

Julian Lam4b6ad9607e74
diff --git a/public/templates/category.tpl b/public/templates/category.tpl
index 827147e7c6..e711f90433 100644
--- a/public/templates/category.tpl
+++ b/public/templates/category.tpl
@@ -21,8 +21,8 @@
<div class="span11 topic-row img-polaroid">
<div class="latest-post visible-desktop">
<div class="pull-right">
- <img style="width: 48px; height: 48px; /*temporary*/" src="/graph/users/{topics.recent_author}/picture" />
- <p><strong>{topics.recent_author}</strong>: {topics.recent_post}</p>
+ <img style="width: 48px; height: 48px; /*temporary*/" src="/graph/users/{topics.teaser_username}/picture" />
+ <p><strong>{topics.teaser_username}</strong>: {topics.teaser_text}</p>
</div>
</div>
<div>
diff --git a/src/posts.js b/src/posts.js
index 8c5eaccc71..35931d5a90 100644
--- a/src/posts.js
+++ b/src/posts.js
@@ -37,7 +37,6 @@ marked.setOptions({
var uid = post_data.uid[i],
pid = post_data.pid[i];
- console.log(current_user, uid);
if (post_data.deleted[i] === null || (post_data.deleted[i] === '1' && manage_content) || current_user === uid) {
var post_obj = {
'pid' : pid,
@@ -307,9 +306,6 @@ marked.setOptions({
user.getUserFields(uid, ['username'], function(data){
- RDB.set('tid:' + tid + ':recent:post', content);
- RDB.set('tid:' + tid + ':recent:author', data.username);
-
//add active users to this category
RDB.get('tid:' + tid + ':cid', function(err, cid) {
RDB.handle(err);
diff --git a/src/topics.js b/src/topics.js
index 6929760f0a..e7f8619f70 100644
--- a/src/topics.js
+++ b/src/topics.js
@@ -3,7 +3,13 @@ var RDB = require('./redis.js'),
utils = require('./utils.js'),
user = require('./user.js'),
configs = require('../config.js'),
- categories = require('./categories.js');
+ categories = require('./categories.js'),
+ marked = require('marked')
+ async = require('async');
+
+marked.setOptions({
+ breaks: true
+});
(function(Topics) {
@@ -30,8 +36,6 @@ var RDB = require('./redis.js'),
locked = [],
deleted = [],
pinned = [],
- recent_post = [],
- recent_author = [];
for (var i=0, ii=tids.length; i<ii; i++) {
title.push('tid:' + tids[i] + ':title');
@@ -42,8 +46,6 @@ var RDB = require('./redis.js'),
locked.push('tid:' + tids[i] + ':locked');
deleted.push('tid:' + tids[i] + ':deleted');
pinned.push('tid:' + tids[i] + ':pinned');
- recent_post.push('tid:' + tids[i] + ':recent:post');
- recent_author.push('tid:' + tids[i] + ':recent:author');
}
var multi = RDB.multi()
@@ -60,8 +62,6 @@ var RDB = require('./redis.js'),
.mget(locked)
.mget(deleted)
.mget(pinned)
- .mget(recent_post)
- .mget(recent_author)
}
@@ -84,18 +84,17 @@ var RDB = require('./redis.js'),
locked = replies[7];
deleted = replies[8];
pinned = replies[9];
- recent_post = replies[10];
- recent_author = replies[11];
var usernames,
has_read,
- moderators;
+ moderators,
+ teaser_info;
function generate_topic() {
- if (!usernames || !has_read || !moderators) return;
-
+ if (!usernames || !has_read || !moderators || !teaser_info) return;
- for (var i=0, ii=title.length; i<ii; i++) {
+ for (var i=0, ii=title.length; i<ii; i++) {
+ console.log(teaser_info[i]);
topics.push({
'title' : title[i],
'uid' : uid[i],
@@ -109,9 +108,10 @@ var RDB = require('./redis.js'),
'pinned': parseInt(pinned[i] || 0), // For sorting purposes
'pin-icon': pinned[i] === '1' ? 'icon-pushpin' : 'none',
'badgeclass' : (has_read[i] && current_user !=0) ? '' : 'badge-important',
- 'recent_post' : recent_post[i],
- 'recent_author' : recent_author[i]
+ 'teaser_text': teaser_info[i].text,
+ 'teaser_username': teaser_info[i].username
});
+ console.log(teaser_info[i].text, teaser_info[i].username);
}
// Float pinned topics to the top
@@ -152,7 +152,12 @@ var RDB = require('./redis.js'),
categories.getModerators(category_id, function(mods) {
moderators = mods;
generate_topic();
- })
+ });
+
+ Topics.get_teasers(tids, function(teasers) {
+ teaser_info = teasers;
+ generate_topic();
+ });
}
else {
callback({
@@ -189,6 +194,42 @@ var RDB = require('./redis.js'),
});
}
+ Topics.get_teasers = function(tids, callback) {
+ var requests = [];
+ if (Array.isArray(tids)) {
+ for(x=0,numTids=tids.length;x<numTids;x++) {
+ (function(x) {
+ requests.push(function(next) {
+ Topics.get_teaser(tids[x], function(teaser_info) {
+ next(null, teaser_info);
+ });
+ });
+ })(x);
+ }
+ async.parallel(requests, function(err, teasers) {
+ callback(teasers);
+ });
+ } else callback([]);
+ }
+
+ Topics.get_teaser = function(tid, callback) {
+ RDB.lrange('tid:' + tid + ':posts', -1, -1, function(err, pid) {
+ if (pid !== null) {
+ RDB.mget([
+ 'pid:' + pid + ':content',
+ 'pid:' + pid + ':uid'
+ ], function(err, content) {
+ user.getUserField(content[1], 'username', function(username) {
+ callback({
+ "text": utils.strip_tags(marked(content[0])),
+ "username": username
+ });
+ });
+ });
+ }
+ });
+ }
+
Topics.post = function(socket, uid, title, content, category_id) {
if (!category_id) throw new Error('Attempted to post without a category_id');
diff --git a/src/utils.js b/src/utils.js
index 8c08302920..172505b433 100644
--- a/src/utils.js
+++ b/src/utils.js
@@ -47,8 +47,18 @@ var utils = {
.replace(/-+/g, '-'); // collapse dashes
return str;
- }
+ },
+
+ // Willingly stolen from: http://phpjs.org/functions/strip_tags/
+ 'strip_tags': function(input, allowed) {
+ allowed = (((allowed || "") + "").toLowerCase().match(/<[a-z][a-z0-9]*>/g) || []).join(''); // making sure the allowed arg is a string containing only tags in lowercase (<a><b><c>)
+ var tags = /<\/?([a-z][a-z0-9]*)\b[^>]*>/gi,
+ commentsAndPhpTags = /<!--[\s\S]*?-->|<\?(?:php)?[\s\S]*?\?>/gi;
+ return input.replace(commentsAndPhpTags, '').replace(tags, function ($0, $1) {
+ return allowed.indexOf('<' + $1.toLowerCase() + '>') > -1 ? $0 : '';
+ });
+ }
}
module.exports = utils;
\ No newline at end of file
diff --git a/src/webserver.js b/src/webserver.js
index 44ed5f0838..ad451882b2 100644
--- a/src/webserver.js
+++ b/src/webserver.js
@@ -227,13 +227,12 @@ var express = require('express'),
app.get('/api/:method/:id*', api_method);
app.get('/test', function(req, res) {
- categories.getModerators(2, function(mods) {
- res.send(JSON.stringify(mods));
+ topics.get_teasers([1, 2, 3], function(teasers) {
+ res.send(JSON.stringify(teasers));
});
});
-
//START TODO: MOVE TO GRAPH.JS
app.get('/graph/users/:username/picture', function(req, res) {