From 1a992b71475b20330e5ef6a5d4a601dc9794b7d4 Mon Sep 17 00:00:00 2001 From: Baris Usakli Date: Tue, 2 Jul 2013 16:24:13 -0400 Subject: [PATCH] first batch of changes --- public/templates/account.tpl | 2 +- src/feed.js | 2 +- src/posts.js | 237 +++++++++++++---------------------- src/routes/user.js | 2 +- src/topics.js | 211 +++++++++++++++++++------------ src/user.js | 42 ++++++- src/webserver.js | 13 +- src/websockets.js | 6 +- 8 files changed, 271 insertions(+), 244 deletions(-) diff --git a/public/templates/account.tpl b/public/templates/account.tpl index 8a39b7161f..e32bc4ed82 100644 --- a/public/templates/account.tpl +++ b/public/templates/account.tpl @@ -84,7 +84,7 @@
{posts.content} - {posts.timestamp} ago + {posts.relativeTime} ago
diff --git a/src/feed.js b/src/feed.js index f40e2b5129..a13a391167 100644 --- a/src/feed.js +++ b/src/feed.js @@ -36,7 +36,7 @@ } function getPostsData(next) { - posts.getPostsByTid(tid, 0, -20, -1, function(postsData) { + posts.getPostsByTid(tid, -20, -1, function(postsData) { next(null, postsData); }); } diff --git a/src/posts.js b/src/posts.js index a34a2751a9..ed850fc74f 100644 --- a/src/posts.js +++ b/src/posts.js @@ -14,13 +14,14 @@ marked.setOptions({ }); (function(Posts) { - Posts.getPostsByTid = function(tid, current_user, start, end, callback) { + + Posts.getPostsByTid = function(tid, start, end, callback) { RDB.lrange('tid:' + tid + ':posts', start, end, function(err, pids) { + RDB.handle(err); - topics.markAsRead(tid, current_user); if (pids.length) { - Posts.getPostsByPids(pids, current_user, function(posts) { + Posts.getPostsByPids(pids, function(posts) { callback(posts); }); } else { @@ -63,97 +64,58 @@ marked.setOptions({ }); }; - Posts.getPostsByPids = function(pids, current_user, callback) { - var content = [], uid = [], timestamp = [], post_rep = [], editor = [], editTime = [], deleted = [], tid = []; - - for (var i=0, ii=pids.length; i 0) { - callback(tid); - } else { - callback(false); - } + Posts.getPostField = function(pid, field, callback) { + RDB.hget('post:' + pid, field, function(data) { + if(err === null) + callback(data); + else + console.log(err); }); } + Posts.get_cid_by_pid = function(pid, callback) { - Posts.get_tid_by_pid(pid, function(tid) { - if (tid) topics.get_cid_by_tid(tid, function(cid) { - if (cid) { - callback(cid); - } else { - callback(false); - } - }); - }) + Posts.getPostField(pid, 'tid', function(tid) { + if (tid) { + topics.get_cid_by_tid(tid, function(cid) { + if (cid) { + callback(cid); + } else { + callback(false); + } + }); + } + }); } Posts.reply = function(socket, tid, uid, content) { @@ -169,6 +131,7 @@ marked.setOptions({ user.getUserField(uid, 'lastposttime', function(lastposttime) { + if(Date.now() - lastposttime < config.post_delay) { socket.emit('event:alert', { title: 'Too many posts!', @@ -183,7 +146,7 @@ marked.setOptions({ if (pid > 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) { @@ -246,57 +209,54 @@ 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'); + + RDB.get('tid:' + tid + ':cid', function(err, 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.onNewPostMade(uid, tid, pid, timestamp); - // 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); if (callback) callback(pid); @@ -306,40 +266,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..7921124fa7 100644 --- a/src/routes/user.js +++ b/src/routes/user.js @@ -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/topics.js b/src/topics.js index 0ba2054647..f32c2331aa 100644 --- a/src/topics.js +++ b/src/topics.js @@ -16,32 +16,61 @@ marked.setOptions({ }); (function(Topics) { + + Topics.getTopicData = function(tid, callback) { + RDB.hgetall('topic:' + tid, function(err, data) { + if(err === null) + callback(data); + else + console.log(err); + }); + } + + Topics.getTopicPosts = function(tid, callback) { + posts.getPostsByTid(tid, 0, 9, function(postData) { + callback(postData); + }); + } + + function addUserInfoToPost(post, callback) { + user.getUserFields(post.uid, ['username', 'userslug', 'reputation', 'picture', 'signature'], function(userData) { + + post.username = userData.username || 'anonymous'; + post.userslug = userData.userslug || ''; + post.user_rep = userData.reputation || 0; + post.gravatar = userData.picture || 'http://www.gravatar.com/avatar/d41d8cd98f00b204e9800998ecf8427e'; + post.signature = marked(userData.signature || ''); + + callback(); + }); + } + + function constructPosts(topicPosts, callback) { + var done = 0; + + for(var i=0, ii=topicPosts.length; i 0) { RDB.lpush(schema.topics(tid).posts, pid); @@ -379,40 +402,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.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..b237b338d4 100644 --- a/src/user.js +++ b/src/user.js @@ -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) { @@ -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 46b2ee0a9e..7b7bd52e24 100644 --- a/src/webserver.js +++ b/src/webserver.js @@ -301,8 +301,19 @@ var express = require('express'), res.send(data); });*/ - posts.getPostsByTid(1, 1, 0, 10, function(data) { + /*posts.getPostsByTid(1, 1, 0, 10, function(data) { res.send(data); + });*/ + +/* posts.getPostsByPids([1,2,3], function(data) { + res.send(data); + });*/ + + /*posts.getPostsByUid(1, 0, 10, function(data) { + res.send(data); + });*/ + topics.getTopicById(1, 1, function(data) { + res.send(data); }); diff --git a/src/websockets.js b/src/websockets.js index 99a9388e13..9d2a0b0157 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 }); }); }); @@ -349,7 +349,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); }); }, @@ -413,7 +413,7 @@ 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){ + posts.getPostsByTid(data.tid, 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', {