@@ -65,7 +65,7 @@
diff --git a/src/admin/categories.js b/src/admin/categories.js
index c5c8212edf..f287f9a1ef 100644
--- a/src/admin/categories.js
+++ b/src/admin/categories.js
@@ -11,14 +11,17 @@ var RDB = require('./../redis.js'),
var slug = cid + '/' + utils.slugify(data.name);
RDB.rpush('categories:cid', cid);
- // Topic Info
- RDB.set('cid:' + cid + ':name', data.name);
- RDB.set('cid:' + cid + ':description', data.description);
- RDB.set('cid:' + cid + ':icon', data.icon);
- RDB.set('cid:' + cid + ':blockclass', data.blockclass);
- RDB.set('cid:' + cid + ':slug', slug);
-
- RDB.set('category:slug:' + slug + ':cid', cid);
+ RDB.hmset('category:' + cid, {
+ cid: cid,
+ name: data.name,
+ description: data.description,
+ icon: data.icon,
+ blockclass: data.blockclass,
+ slug: slug,
+ topic_count: 0
+ });
+
+ RDB.set('categoryslug:' + slug + ':cid', cid);
if (callback) callback({'status': 1});
});
@@ -29,15 +32,15 @@ var RDB = require('./../redis.js'),
for (var cid in modified) {
var category = modified[cid];
-
+
for (var key in category) {
- RDB.set('cid:' + cid + ':' + key, category[key]);
+ RDB.hset('category:' + cid, key, category[key]);
if (key == 'name') {
// reset slugs if name is updated
var slug = cid + '/' + utils.slugify(category[key]);
- RDB.set('cid:' + cid + ':slug', slug);
- RDB.set('category:slug:' + slug + ':cid', cid);
+ RDB.hset('category:' + cid, 'slug', slug);
+ RDB.set('categoryslug:' + slug + ':cid', cid);
RDB.smembers('categories:' + cid + ':tid', function(err, tids) {
var pipe = RDB.multi();
diff --git a/src/categories.js b/src/categories.js
index e4ba8ad55e..036fb5b490 100644
--- a/src/categories.js
+++ b/src/categories.js
@@ -9,14 +9,13 @@ var RDB = require('./redis.js'),
Categories.getCategoryById = function(category_id, current_user, callback) {
RDB.smembers('categories:' + category_id + ':tid', function(err, tids) {
- RDB.multi()
- .get('cid:' + category_id + ':name')
- .smembers('cid:' + category_id + ':active_users')
- .get('cid:' + category_id + ':slug')
- .exec(function(err, replies) {
- var category_name = replies[0],
- active_users = replies[1],
- category_slug = replies[2];
+
+ Categories.getCategoryData(category_id, function(categoryData) {
+
+ var category_name = categoryData.name;
+ category_slug = categoryData.slug;
+
+ RDB.smembers('cid:' + category_id + ':active_users', function(err, active_users) {
if (category_name === null) {
callback(false);
@@ -57,7 +56,7 @@ var RDB = require('./redis.js'),
}
function getActiveUsers(next) {
- user.getMultipleUserFields(active_users, ['username','userslug'], function(users) {
+ user.getMultipleUserFields(active_users, ['username', 'userslug'], function(users) {
var activeUserData = [];
for(var uid in users) {
activeUserData.push(users[uid]);
@@ -85,6 +84,7 @@ var RDB = require('./redis.js'),
});
}
});
+ });
});
}
@@ -115,111 +115,83 @@ var RDB = require('./redis.js'),
// not the permanent location for this function
Categories.getTopicsByTids = function(tids, current_user, callback, category_id /*temporary*/) {
- var title = [],
- uid = [],
- timestamp = [],
- lastposttime = [],
- slug = [],
- postcount = [],
- locked = [],
- deleted = [],
- pinned = [];
-
- for (var i=0, ii=tids.length; i
0) {
- next(null, author === uid);
- }
- });
+ posts.getPostField(pid, 'uid', function(author) {
+ if (author && parseInt(author) > 0) {
+ next(null, author === uid);
+ }
+ });
}
function hasEnoughRep(next) {
- // DRY fail in threadTools.
-
user.getUserField(uid, 'reputation', function(reputation) {
next(null, reputation >= global.config['privileges:manage_content']);
});
@@ -55,23 +53,25 @@ marked.setOptions({
}
PostTools.edit = function(uid, pid, title, content) {
+
var success = function() {
- RDB.set('pid:' + pid + ':content', content);
- RDB.set('pid:' + pid + ':edited', Date.now());
- RDB.set('pid:' + pid + ':editor', uid);
-
- posts.get_tid_by_pid(pid, function(tid) {
- PostTools.isMain(pid, tid, function(isMainPost) {
- if (isMainPost) RDB.set('tid:' + tid + ':title', title);
-
- io.sockets.in('topic_' + tid).emit('event:post_edited', {
- pid: pid,
- title: title,
- content: marked(content || '')
- });
+ posts.setPostField(pid, 'content', content);
+ posts.setPostField(pid, 'edited', Date.now());
+ posts.setPostField(pid, 'editor', uid);
+
+ posts.getPostField(pid, 'tid', function(tid) {
+ PostTools.isMain(pid, tid, function(isMainPost) {
+ if (isMainPost)
+ topics.setTopicField(tid, 'title', title);
+
+ io.sockets.in('topic_' + tid).emit('event:post_edited', {
+ pid: pid,
+ title: title,
+ content: marked(content || '')
});
});
- };
+ });
+ };
PostTools.privileges(pid, uid, function(privileges) {
if (privileges.editable) {
@@ -82,14 +82,14 @@ marked.setOptions({
PostTools.delete = function(uid, pid) {
var success = function() {
- RDB.set('pid:' + pid + ':deleted', 1);
+ posts.setPostField(pid, 'deleted', 1);
- posts.get_tid_by_pid(pid, function(tid) {
- io.sockets.in('topic_' + tid).emit('event:post_deleted', {
- pid: pid
- });
+ posts.getPostField(pid, 'tid', function(tid) {
+ io.sockets.in('topic_' + tid).emit('event:post_deleted', {
+ pid: pid
});
- };
+ });
+ };
PostTools.privileges(pid, uid, function(privileges) {
if (privileges.editable) {
@@ -100,14 +100,14 @@ marked.setOptions({
PostTools.restore = function(uid, pid) {
var success = function() {
- RDB.del('pid:' + pid + ':deleted');
+ posts.setPostField(pid, 'deleted', 0);
- posts.get_tid_by_pid(pid, function(tid) {
- io.sockets.in('topic_' + tid).emit('event:post_restored', {
- pid: pid
- });
+ posts.getPostField(pid, 'tid', function(tid) {
+ io.sockets.in('topic_' + tid).emit('event:post_restored', {
+ pid: pid
});
- };
+ });
+ };
PostTools.privileges(pid, uid, function(privileges) {
if (privileges.editable) {
@@ -116,65 +116,5 @@ marked.setOptions({
});
}
- PostTools.constructPostObject = function(rawPosts, tid, current_user, privileges, callback) {
- var postObj = [];
-
- async.waterfall([
- function(next) {
- if (!privileges) {
- threadTools.privileges(tid, current_user, function(privs) {
- privileges = privs;
- next();
- });
- } else {
- next();
- }
- },
- function(next) {
- var postData = rawPosts.postData,
- userData = rawPosts.userData,
- voteData = rawPosts.voteData;
-
- if (!postData) {
- return next(null, []);
- }
-
- for (var i=0, ii= postData.pid.length; i 0) {
- callback(tid);
- } else {
- callback(false);
+ Posts.setPostField = function(pid, field, value) {
+ RDB.hset('post:' + pid, field, value);
+ }
+
+ Posts.getPostFields = function(pid, fields, callback) {
+ RDB.hmget('post:' + pid, fields, function(err, data) {
+ if(err === null) {
+ var returnData = {};
+
+ for(var i=0, ii=fields.length; i 0) {
RDB.rpush('tid:' + tid + ':posts', pid);
- RDB.del('tid:' + tid + ':read_by_uid'); // let everybody know there is an unread post
+ RDB.del('tid:' + tid + ':read_by_uid');
Posts.get_cid_by_pid(pid, function(cid) {
RDB.del('cid:' + cid + ':read_by_uid', function(err, data) {
@@ -193,7 +200,12 @@ marked.setOptions({
RDB.zadd('categories:recent_posts:cid:' + cid, Date.now(), pid);
});
+ Posts.getTopicPostStats(socket);
+
+ // Send notifications to users who are following this topic
+ threadTools.notify_followers(tid, uid);
+
socket.emit('event:alert', {
title: 'Reply Successful',
message: 'You have successfully replied. Click here to view your reply.',
@@ -201,38 +213,29 @@ marked.setOptions({
timeout: 2000
});
- Posts.getTopicPostStats(socket);
-
- // Send notifications to users who are following this topic
- threadTools.notify_followers(tid, uid);
- user.getUserFields(uid, ['username','reputation','picture','signature'], function(data) {
-
- var timestamp = Date.now();
- var socketData = {
- 'posts' : [
- {
- 'pid' : pid,
- 'content' : marked(content || ''),
- 'uid' : uid,
- 'username' : data.username || 'anonymous',
- 'user_rep' : data.reputation || 0,
- 'post_rep' : 0,
- 'gravatar' : data.picture,
- 'signature' : marked(data.signature || ''),
- 'timestamp' : timestamp,
- 'relativeTime': utils.relativeTime(timestamp),
- 'fav_star_class' :'icon-star-empty',
- 'edited-class': 'none',
- 'editor': '',
- }
- ]
- };
+ var timestamp = Date.now();
+ var socketData = {
+ 'posts' : [
+ {
+ 'pid' : pid,
+ 'content' : marked(content || ''),
+ 'uid' : uid,
+ 'post_rep' : 0,
+ 'timestamp' : timestamp,
+ 'relativeTime': utils.relativeTime(timestamp),
+ 'fav_star_class' :'icon-star-empty',
+ 'edited-class': 'none',
+ 'editor': '',
+ }
+ ]
+ };
+ posts.addUserInfoToPost(socketData['posts'][0], function() {
io.sockets.in('topic_' + tid).emit('event:new_post', socketData);
io.sockets.in('recent_posts').emit('event:new_post', socketData);
-
- });
+ });
+
} else {
socket.emit('event:alert', {
title: 'Reply Unsuccessful',
@@ -246,57 +249,53 @@ marked.setOptions({
};
Posts.create = function(uid, tid, content, callback) {
- if (uid === null) return;
+ if (uid === null) {
+ callback(-1);
+ return;
+ }
- RDB.get('tid:' + tid + ':locked', function(err, locked) {
- RDB.handle(err);
+ topics.isLocked(tid, function(locked) {
if (!locked || locked === '0') {
RDB.incr('global:next_post_id', function(err, pid) {
RDB.handle(err);
var timestamp = Date.now();
- // Posts Info
- RDB.set('pid:' + pid + ':content', content);
- RDB.set('pid:' + pid + ':uid', uid);
- RDB.set('pid:' + pid + ':timestamp', timestamp);
- RDB.set('pid:' + pid + ':rep', 0);
- RDB.set('pid:' + pid + ':tid', tid);
- RDB.incr('tid:' + tid + ':postcount');
- RDB.zadd(schema.topics().recent, timestamp, tid);
- RDB.set('tid:' + tid + ':lastposttime', timestamp);
-
- RDB.incr('totalpostcount');
+ RDB.hmset('post:' + pid, {
+ 'pid': pid,
+ 'uid': uid,
+ 'tid': tid,
+ 'content': content,
+ 'timestamp': timestamp,
+ 'reputation': 0,
+ 'editor': '',
+ 'edited': 0,
+ 'deleted': 0
+ });
- user.getUserFields(uid, ['username'], function(data) { // todo parallel
- //add active users to this category
- RDB.get('tid:' + tid + ':cid', function(err, cid) {
- RDB.handle(err);
+ topics.increasePostCount(tid);
+ topics.setTopicField(tid, 'lastposttime', timestamp);
+ topics.addToRecent(tid, timestamp);
+ RDB.incr('totalpostcount');
+
+ topics.getTopicField(tid, 'cid', function(cid) {
+ RDB.handle(err);
- feed.updateTopic(tid, cid);
+ feed.updateTopic(tid, cid);
- // this is a bit of a naive implementation, defn something to look at post-MVP
- RDB.scard('cid:' + cid + ':active_users', function(amount) {
- if (amount > 10) {
- RDB.spop('cid:' + cid + ':active_users');
- }
+ // this is a bit of a naive implementation, defn something to look at post-MVP
+ RDB.scard('cid:' + cid + ':active_users', function(amount) {
+ if (amount > 10) {
+ RDB.spop('cid:' + cid + ':active_users');
+ }
- //RDB.sadd('cid:' + cid + ':active_users', data.username);
- RDB.sadd('cid:' + cid + ':active_users', uid);
- });
+ RDB.sadd('cid:' + cid + ':active_users', uid);
});
});
-
- // User Details - move this out later
- RDB.lpush('uid:' + uid + ':posts', pid);
-
- user.incrementUserFieldBy(uid, 'postcount', 1);
- user.setUserField(uid, 'lastposttime', timestamp);
-
- user.sendPostNotificationToFollowers(uid, tid, pid);
+ user.onNewPostMade(uid, tid, pid, timestamp);
if (callback)
callback(pid);
@@ -306,40 +305,19 @@ marked.setOptions({
}
});
}
-
- Posts.getRawContent = function(pid, callback) {
- RDB.get('pid:' + pid + ':content', function(err, raw) {
- callback(raw);
- });
- }
- Posts.getPostsByUid = function(uid, callback) {
+ Posts.getPostsByUid = function(uid, start, end, callback) {
- RDB.lrange('uid:' + uid + ':posts', 0, 10, function(err, pids) {
- if(err === null) {
-
- if(pids && pids.length) {
-
- Posts.getPostsByPids(pids, uid, function(posts) {
- var returnData = [];
-
- var len = posts.postData.pid.length;
-
- for (var i=0; i < len; ++i) {
- returnData.push({
- pid: posts.postData.pid[i],
- content: posts.postData.content[i],
- timestamp: utils.relativeTime(posts.postData.timestamp[i]),
- tid: posts.postData.tid[i]
- });
- };
-
- callback(returnData);
- });
- }
- else
- callback([]);
+ user.getPostIds(uid, start, end, function(pids) {
+
+ if(pids && pids.length) {
+
+ Posts.getPostsByPids(pids, function(posts) {
+ callback(posts);
+ });
}
+ else
+ callback([]);
});
}
diff --git a/src/routes/user.js b/src/routes/user.js
index 8145a42d2c..d4111570de 100644
--- a/src/routes/user.js
+++ b/src/routes/user.js
@@ -255,10 +255,10 @@ var user = require('./../user.js'),
if (!req.params.section && !req.params.userslug) {
user.getUserList(function(data) {
- data = data.sort(function(a, b) {
- return b.joindate - a.joindate;
- });
- res.json({search_display: 'none', users:data});
+ data = data.sort(function(a, b) {
+ return b.joindate - a.joindate;
+ });
+ res.json({search_display: 'none', users:data});
});
}
else if(String(req.params.section).toLowerCase() === 'following') {
@@ -292,7 +292,7 @@ var user = require('./../user.js'),
user.isFollowing(callerUID, userData.theirid, function(isFollowing) {
- posts.getPostsByUid(userData.theirid, function(posts) {
+ posts.getPostsByUid(userData.theirid, 0, 9, function(posts) {
userData.posts = posts;
userData.isFollowing = isFollowing;
diff --git a/src/schema.js b/src/schema.js
index 0a64da28ba..1672b7b71f 100644
--- a/src/schema.js
+++ b/src/schema.js
@@ -13,19 +13,6 @@
Schema.topics = function(tid) {
return {
- /* strings */
- title: 'tid:' + tid + ':title',
- locked: 'tid:' + tid + ':locked',
- category_name: 'tid:' + tid + ':category_name',
- category_slug: 'tid:' + tid + ':category_slug',
- deleted: 'tid:' + tid + ':deleted',
- pinned: 'tid:' + tid + ':pinned',
- uid: 'tid:' + tid + ':uid',
- timestamp: 'tid:' + tid + ':timestamp',
- slug: 'tid:' + tid + ':slug',
- postcount: 'tid:' + tid + ':postcount',
- cid: 'tid:' + tid + ':cid',
-
/* sets */
tid: 'topics:tid',
read_by_uid: 'tid:' + tid + ':read_by_uid',
diff --git a/src/threadTools.js b/src/threadTools.js
index c387b0bd2b..d0a0cbfd6c 100644
--- a/src/threadTools.js
+++ b/src/threadTools.js
@@ -11,7 +11,7 @@ var RDB = require('./redis.js'),
//todo: break early if one condition is true
function getCategoryPrivileges(next) {
- topics.get_cid_by_tid(tid, function(cid) {
+ topics.getTopicField(tid, 'cid', function(cid) {
categories.privileges(cid, uid, function(privileges) {
next(null, privileges);
});
@@ -19,8 +19,6 @@ var RDB = require('./redis.js'),
}
function hasEnoughRep(next) {
- // DRY fail in postTools
-
user.getUserField(uid, 'reputation', function(reputation) {
next(null, reputation >= global.config['privileges:manage_topic']);
});
@@ -38,8 +36,7 @@ var RDB = require('./redis.js'),
ThreadTools.lock = function(tid, uid, socket) {
ThreadTools.privileges(tid, uid, function(privileges) {
if (privileges.editable) {
- // Mark thread as locked
- RDB.set('tid:' + tid + ':locked', 1);
+ topics.setTopicField(tid, 'locked', 1);
if (socket) {
io.sockets.in('topic_' + tid).emit('event:topic_locked', {
@@ -59,8 +56,7 @@ var RDB = require('./redis.js'),
ThreadTools.unlock = function(tid, uid, socket) {
ThreadTools.privileges(tid, uid, function(privileges) {
if (privileges.editable) {
- // Mark thread as unlocked
- RDB.del('tid:' + tid + ':locked');
+ topics.setTopicField(tid, 'locked', 0);
if (socket) {
io.sockets.in('topic_' + tid).emit('event:topic_unlocked', {
@@ -80,8 +76,8 @@ var RDB = require('./redis.js'),
ThreadTools.delete = function(tid, uid, socket) {
ThreadTools.privileges(tid, uid, function(privileges) {
if (privileges.editable) {
- // Mark thread as deleted
- RDB.set('tid:' + tid + ':deleted', 1);
+
+ topics.setTopicField(tid, 'deleted', 1);
ThreadTools.lock(tid, uid);
if (socket) {
@@ -102,8 +98,8 @@ var RDB = require('./redis.js'),
ThreadTools.restore = function(tid, uid, socket) {
ThreadTools.privileges(tid, uid, function(privileges) {
if (privileges.editable) {
- // Mark thread as restored
- RDB.del('tid:' + tid + ':deleted');
+
+ topics.setTopicField(tid, 'deleted', 0);
ThreadTools.unlock(tid, uid);
if (socket) {
@@ -124,8 +120,8 @@ var RDB = require('./redis.js'),
ThreadTools.pin = function(tid, uid, socket) {
ThreadTools.privileges(tid, uid, function(privileges) {
if (privileges.editable) {
- // Mark thread as pinned
- RDB.set('tid:' + tid + ':pinned', 1);
+
+ topics.setTopicField(tid, 'pinned', 1);
if (socket) {
io.sockets.in('topic_' + tid).emit('event:topic_pinned', {
@@ -145,8 +141,8 @@ var RDB = require('./redis.js'),
ThreadTools.unpin = function(tid, uid, socket) {
ThreadTools.privileges(tid, uid, function(privileges) {
if (privileges.editable) {
- // Mark thread as unpinned
- RDB.del('tid:' + tid + ':pinned');
+
+ topics.setTopicField(tid, 'pinned', 0);
if (socket) {
io.sockets.in('topic_' + tid).emit('event:topic_unpinned', {
@@ -164,15 +160,17 @@ var RDB = require('./redis.js'),
}
ThreadTools.move = function(tid, cid, socket) {
- RDB.get('tid:' + tid + ':cid', function(err, oldCid) {
- RDB.handle(err);
+
+ topics.getTopicField(tid, 'cid', function(oldCid) {
RDB.smove('categories:' + oldCid + ':tid', 'categories:' + cid + ':tid', tid, function(err, result) {
if (!err && result === 1) {
- RDB.set('tid:' + tid + ':cid', cid);
+
+ topics.setTopicField(tid, 'cid', cid);
+
categories.getCategories([cid], function(data) {
- RDB.set('tid:' + tid + ':category_name', data.categories[0].name);
- RDB.set('tid:' + tid + ':category_slug', data.categories[0].slug);
+ topics.setTopicField(tid, 'category_name', data.categories[0].name);
+ topics.setTopicField(tid, 'category_slug', data.categories[0].slug);
});
socket.emit('api:topic.move', {
@@ -233,11 +231,16 @@ var RDB = require('./redis.js'),
ThreadTools.notify_followers = function(tid, exceptUid) {
async.parallel([
function(next) {
- topics.get_topic(tid, 0, function(threadData) {
- notifications.create(threadData.teaser_username + ' has posted a reply to: "' + threadData.title + '"', null, '/topic/' + tid, 'topic:' + tid, function(nid) {
- next(null, nid);
+
+ topics.getTopicField(tid, 'title', function(title) {
+ topics.getTeaser(tid, function(teaser) {
+ notifications.create(teaser.username + ' has posted a reply to: "' + title + '"', null, '/topic/' + tid, 'topic:' + tid, function(nid) {
+ next(null, nid);
+ });
});
});
+
+
},
function(next) {
ThreadTools.get_followers(tid, function(err, followers) {
diff --git a/src/topics.js b/src/topics.js
index 0ba2054647..541db1f43d 100644
--- a/src/topics.js
+++ b/src/topics.js
@@ -9,39 +9,94 @@ var RDB = require('./redis.js')
threadTools = require('./threadTools.js'),
postTools = require('./postTools'),
async = require('async'),
- feed = require('./feed.js');
+ feed = require('./feed.js'),
+ favourites = require('./favourites.js');
marked.setOptions({
breaks: true
});
(function(Topics) {
- Topics.getTopicById = function(tid, current_user, callback) {
- function getTopicData(next) {
- RDB.multi()
- .get(schema.topics(tid).title)
- .get(schema.topics(tid).locked)
- .get(schema.topics(tid).category_name)
- .get(schema.topics(tid).category_slug)
- .get(schema.topics(tid).deleted)
- .get(schema.topics(tid).pinned)
- .get(schema.topics(tid).slug)
- .exec(function(err, replies) {
- next(null, {
- topic_name: replies[0],
- locked: replies[1] || 0,
- category_name: replies[2],
- category_slug: replies[3],
- deleted: replies[4] || 0,
- pinned: replies[5] || 0,
- slug: replies[6]
- });
+
+ Topics.getTopicData = function(tid, callback) {
+ RDB.hgetall('topic:' + tid, function(err, data) {
+ if(err === null)
+ callback(data);
+ else
+ console.log(err);
+ });
+ }
+
+ Topics.getTopicDataWithUsername = function(tid, callback) {
+ Topics.getTopicData(tid, function(topic) {
+ user.getUserField(topic.uid, 'username', function(username) {
+
+ topic.username = username;
+ callback(topic);
+ });
+ });
+ }
+
+ Topics.getTopicPosts = function(tid, start, end, current_user, callback) {
+ posts.getPostsByTid(tid, start, end, function(postData) {
+
+ function getFavouritesData(next) {
+ var pids = [];
+ for(var i=0; i 0) {
RDB.sismember(schema.topics(tid).read_by_uid, uid, function(err, read) {
- topicData.badgeclass = read ? '' : 'badge-important';
-
- next();
+ next(null, read);
});
} else {
- next();
+ next(null, null);
}
}
- function get_teaser(next) {
- Topics.get_teaser(tid, function(teaser) {
- topicData.teaser_text = teaser.text;
- topicData.teaser_username = teaser.username;
-
- next();
+ function getTeaser(next) {
+ Topics.getTeaser(tid, function(teaser) {
+ next(null, teaser);
});
}
- async.parallel([get_topic_data, get_read_status, get_teaser], function(err) {
+ async.parallel([getTopicData, getReadStatus, getTeaser], function(err, results) {
if (err) {
throw new Error(err);
}
+
+ var topicData = results[0],
+ hasRead = results[1],
+ teaser = results[2];
+
+ 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.tid = tid;
callback(topicData);
});
}
@@ -175,7 +201,7 @@ marked.setOptions({
tids.sort(function(a, b) { return b - a; });
async.each(tids, function(tid, next) {
- Topics.get_topic(tid, 0, function(topicData) {
+ Topics.getTopicDataWithUsername(tid, function(topicData) {
topics.push(topicData);
next();
});
@@ -185,35 +211,11 @@ marked.setOptions({
});
}
- Topics.get_cid_by_tid = function(tid, callback) {
- RDB.get(schema.topics(tid).cid, function(err, cid) {
- if (cid && parseInt(cid) > 0) {
- callback(cid);
- } else {
- callback(false);
- }
- });
- }
-
- Topics.getTitle = function(tid, callback) {
- RDB.get('tid:' + tid + ':title', function(err, title) {
- callback(title);
- });
- }
-
- Topics.getSlug = function(tid, callback) {
- RDB.get('tid:' + tid + ':slug', function(err, slug) {
- callback(slug);
- });
- }
-
Topics.getTitleByPid = function(pid, callback) {
- RDB.get('pid:' + pid + ':tid', function(err, tid) {
- if (!err) {
- Topics.getTitle(tid, function(title) {
- callback(title);
- });
- } else callback('Could not grab title');
+ posts.getPostField(pid, 'tid', function(tid) {
+ Topics.getTopicField(tid, 'title', function(title) {
+ callback(title);
+ });
});
}
@@ -221,7 +223,7 @@ marked.setOptions({
RDB.sadd(schema.topics(tid).read_by_uid, uid);
- Topics.get_cid_by_tid(tid, function(cid) {
+ Topics.getTopicField(tid, 'cid', function(cid) {
categories.isTopicsRead(cid, uid, function(read) {
if(read) {
@@ -243,13 +245,13 @@ marked.setOptions({
});
}
- Topics.get_teasers = function(tids, callback) {
+ Topics.getTeasers = function(tids, callback) {
var requests = [];
if (Array.isArray(tids)) {
for(x=0,numTids=tids.length;x 0) {
RDB.lpush(schema.topics(tid).posts, pid);
@@ -370,7 +375,7 @@ marked.setOptions({
threadTools.toggleFollow(tid, uid);
// Notify any users looking at the category that a new topic has arrived
- Topics.get_topic(tid, uid, function(topicData) {
+ Topics.getTopicForCategoryView(tid, uid, function(topicData) {
io.sockets.in('category_' + category_id).emit('event:new_topic', topicData);
io.sockets.in('recent_posts').emit('event:new_topic', topicData);
});
@@ -379,40 +384,62 @@ marked.setOptions({
}
});
- // User Details - move this out later
- RDB.lpush('uid:' + uid + ':topics', tid);
-
- socket.emit('event:alert', {
- title: 'Thank you for posting',
- message: 'You have successfully posted. Click here to view your post.',
- type: 'notify',
- timeout: 2000
- });
+ user.addTopicIdToUser(uid, tid);
// let everyone know that there is an unread topic in this category
RDB.del('cid:' + category_id + ':read_by_uid', function(err, data) {
Topics.markAsRead(tid, uid);
});
-
- RDB.zadd(schema.topics().recent, Date.now(), tid);
- //RDB.zadd('topics:active', tid);
// in future it may be possible to add topics to several categories, so leaving the door open here.
RDB.sadd('categories:' + category_id + ':tid', tid);
- RDB.set(schema.topics(tid).cid, category_id);
+
categories.getCategories([category_id], function(data) {
- RDB.set(schema.topics(tid).category_name, data.categories[0].name);
- RDB.set(schema.topics(tid).category_slug, data.categories[0].slug);
+ Topics.setTopicField(tid, 'category_name', data.categories[0].name);
+ Topics.setTopicField(tid, 'category_slug', data.categories[0].slug);
});
- RDB.incr('cid:' + category_id + ':topiccount');
+ RDB.hincrby('category:' + category_id, 'topic_count', 1);
RDB.incr('totaltopiccount');
feed.updateCategory(category_id);
+
+ socket.emit('event:alert', {
+ title: 'Thank you for posting',
+ message: 'You have successfully posted. Click here to view your post.',
+ type: 'notify',
+ timeout: 2000
+ });
});
});
};
+ Topics.getTopicField = function(tid, field, callback) {
+ RDB.hget('topic:' + tid, field, function(err, data) {
+ if(err === null)
+ callback(data);
+ else
+ console.log(err);
+ });
+ }
+
+ Topics.setTopicField = function(tid, field, value) {
+ RDB.hset('topic:' + tid, field, value);
+ }
+
+ Topics.increasePostCount = function(tid) {
+ RDB.hincrby('topic:' + tid, 'postcount', 1);
+ }
+
+ Topics.isLocked = function(tid, callback) {
+ Topics.getTopicField(tid, 'locked', function(locked) {
+ callback(locked);
+ });
+ }
+
+ Topics.addToRecent = function(tid, timestamp) {
+ RDB.zadd(schema.topics().recent, timestamp, tid);
+ }
}(exports));
\ No newline at end of file
diff --git a/src/user.js b/src/user.js
index 69be28fc71..10e3bdc415 100644
--- a/src/user.js
+++ b/src/user.js
@@ -11,7 +11,7 @@ var utils = require('./../public/src/utils.js'),
(function(User) {
User.getUserField = function(uid, field, callback) {
- RDB.hget('user:'+uid, field, function(err, data) {
+ RDB.hget('user:' + uid, field, function(err, data) {
if(err === null)
callback(data);
else
@@ -20,7 +20,7 @@ var utils = require('./../public/src/utils.js'),
}
User.getUserFields = function(uid, fields, callback) {
- RDB.hmget('user:'+uid, fields, function(err, data) {
+ RDB.hmget('user:' + uid, fields, function(err, data) {
if(err === null) {
var returnData = {};
@@ -55,13 +55,13 @@ var utils = require('./../public/src/utils.js'),
loaded ++;
if (loaded == uuids.length) callback(data);
});
- }(uuids[i]))
+ }(uuids[i]));
}
}
User.getUserData = function(uid, callback) {
- RDB.hgetall('user:'+uid, function(err, data) {
+ RDB.hgetall('user:' + uid, function(err, data) {
if(err === null) {
if(data) {
if(data['password'])
@@ -107,11 +107,11 @@ var utils = require('./../public/src/utils.js'),
}
User.setUserField = function(uid, field, value) {
- RDB.hset('user:'+uid, field, value);
+ RDB.hset('user:' + uid, field, value);
}
User.incrementUserFieldBy = function(uid, field, value) {
- RDB.hincrby('user:'+uid, field, value);
+ RDB.hincrby('user:' + uid, field, value);
}
User.getUserList = function(callback) {
@@ -129,7 +129,7 @@ var utils = require('./../public/src/utils.js'),
User.getUserData(uid, function(userData) {
data.push(userData);
- if(data.length == userkeys.length)
+ if(data.length === userkeys.length)
callback(data);
});
}
@@ -221,13 +221,13 @@ var utils = require('./../public/src/utils.js'),
RDB.lpush('userlist', username);
io.sockets.emit('user.latest', {userslug: userslug, username: username});
- callback(null, uid);
-
if (password) {
User.hashPassword(password, function(hash) {
- RDB.hset('user:'+uid, 'password', hash);
+ User.setUserField(uid, 'password', hash);
});
}
+
+ callback(null, uid);
});
});
};
@@ -276,6 +276,38 @@ var utils = require('./../public/src/utils.js'),
});
}
+ User.onNewPostMade = function(uid, tid, pid, timestamp) {
+ User.addPostIdToUser(uid, pid)
+
+ User.incrementUserFieldBy(uid, 'postcount', 1);
+ User.setUserField(uid, 'lastposttime', timestamp);
+
+ User.sendPostNotificationToFollowers(uid, tid, pid);
+ }
+
+ User.addPostIdToUser = function(uid, pid) {
+ RDB.lpush('uid:' + uid + ':posts', pid);
+ }
+
+ User.addTopicIdToUser = function(uid, tid) {
+ RDB.lpush('uid:' + uid + ':topics', tid);
+ }
+
+ User.getPostIds = function(uid, start, end, callback) {
+ RDB.lrange('uid:' + uid + ':posts', start, end, function(err, pids) {
+ if(err === null) {
+ if(pids && pids.length)
+ callback(pids);
+ else
+ callback([]);
+ }
+ else {
+ console.log(err);
+ callback([]);
+ }
+ });
+ }
+
User.sendConfirmationEmail = function (email) {
if (global.config['email:host'] && global.config['email:port'] && global.config['email:from']) {
var confirm_code = utils.generateUUID(),
@@ -397,7 +429,7 @@ var utils = require('./../public/src/utils.js'),
User.getUserField(uid, 'username', function(username) {
RDB.smembers('followers:'+uid, function(err, followers) {
- topics.getSlug(tid, function(slug) {
+ topics.getTopicField(tid, 'slug', function(slug) {
var message = username + ' made a new post';
diff --git a/src/webserver.js b/src/webserver.js
index ea4a96f322..a0e5f3e5a8 100644
--- a/src/webserver.js
+++ b/src/webserver.js
@@ -134,7 +134,7 @@ var express = require('express'),
var topic_url = tid + (req.params.slug ? '/' + req.params.slug : '');
- topics.getTopicById(tid, ((req.user) ? req.user.uid : 0), function(topic) {
+ topics.getTopicWithPosts(tid, ((req.user) ? req.user.uid : 0), function(topic) {
res.send(
app.build_header(res) +
'\n\t' +
@@ -241,7 +241,7 @@ var express = require('express'),
res.json(data);
break;
case 'topic' :
- topics.getTopicById(req.params.id, uid, function(data) {
+ topics.getTopicWithPosts(req.params.id, uid, function(data) {
res.json(data);
});
break;
@@ -294,8 +294,30 @@ var express = require('express'),
app.get('/api/:method/:id/:section?', api_method);
app.get('/api/:method/:id*', api_method);
+ app.get('/tid/:tid', function(req, res) {
+ topics.getTopicData(req.params.tid, function(data){
+ if(data)
+ res.send(data);
+ else
+ res.send("Topic doesn't exist!");
+ });
+ });
+
+ app.get('/pid/:pid', function(req, res) {
+ posts.getPostData(req.params.pid, function(data){
+ if(data)
+ res.send(data);
+ else
+ res.send("Post doesn't exist!");
+ });
+ });
+
app.all('/test', function(req, res) {
- res.send();
+
+ categories.getCategoryById(1,1, function(data) {
+ res.send(data);
+ },1);
+
});
//START TODO: MOVE TO GRAPH.JS
diff --git a/src/websockets.js b/src/websockets.js
index 99a9388e13..6fa55784ec 100644
--- a/src/websockets.js
+++ b/src/websockets.js
@@ -247,7 +247,7 @@ var SocketIO = require('socket.io').listen(global.server, { log:false }),
});
socket.on('api:posts.getRawPost', function(data) {
- posts.getRawContent(data.pid, function(raw) {
+ posts.getPostField(data.pid, 'content', function(raw) {
socket.emit('api:posts.getRawPost', { post: raw });
});
});
@@ -332,10 +332,16 @@ var SocketIO = require('socket.io').listen(global.server, { log:false }),
socket.on('api:composer.push', function(data) {
if (uid > 0) {
if (parseInt(data.tid) > 0) {
- topics.get_topic(data.tid, uid, function(topicData) {
- topicData.tid = data.tid;
- if (data.body) topicData.body = data.body;
- socket.emit('api:composer.push', topicData);
+ topics.getTopicData(data.tid, function(topicData) {
+
+ if (data.body)
+ topicData.body = data.body;
+
+ socket.emit('api:composer.push', {
+ tid: data.tid,
+ title: topicData.title,
+ body: topicData.body
+ });
});
} else if (parseInt(data.cid) > 0) {
user.getUserField(uid, 'username', function(username) {
@@ -349,7 +355,7 @@ var SocketIO = require('socket.io').listen(global.server, { log:false }),
} else if (parseInt(data.pid) > 0) {
async.parallel([
function(next) {
- posts.getRawContent(data.pid, function(raw) {
+ posts.getPostField(data.pid, 'content', function(raw) {
next(null, raw);
});
},
@@ -374,7 +380,7 @@ var SocketIO = require('socket.io').listen(global.server, { log:false }),
});
socket.on('api:composer.editCheck', function(pid) {
- posts.get_tid_by_pid(pid, function(tid) {
+ posts.getPostField(pid, 'tid', function(tid) {
postTools.isMain(pid, tid, function(isMain) {
socket.emit('api:composer.editCheck', {
titleEditable: isMain
@@ -413,14 +419,10 @@ var SocketIO = require('socket.io').listen(global.server, { log:false }),
var start = data.after,
end = start + 10;
- posts.getPostsByTid(data.tid, uid, start, end, function(posts){
- if (!posts.error) {
- postTools.constructPostObject(posts, data.tid, uid, null, function(postObj) {
- io.sockets.in('topic_' + data.tid).emit('event:new_post', {
- posts: postObj
- });
- });
- }
+ topics.getTopicPosts(data.tid, start, end, uid, function(posts) {
+ io.sockets.in('topic_' + data.tid).emit('event:new_post', {
+ posts: posts
+ });
});
});