first batch of changes

v1.18.x
Baris Usakli 12 years ago
parent 67247876ff
commit 1a992b7147

@ -84,7 +84,7 @@
<a href="/topic/{posts.tid}/{posts.pid}">
<div class="topic-row img-polaroid clearfix">
<span>{posts.content}</span>
<span class="pull-right">{posts.timestamp} ago</span>
<span class="pull-right">{posts.relativeTime} ago</span>
</div>
</a>

@ -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);
});
}

@ -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<ii; i++) {
content.push('pid:' + pids[i] + ':content');
uid.push('pid:' + pids[i] + ':uid');
timestamp.push('pid:' + pids[i] + ':timestamp');
post_rep.push('pid:' + pids[i] + ':rep');
editor.push('pid:' + pids[i] + ':editor');
editTime.push('pid:' + pids[i] + ':edited');
deleted.push('pid:' + pids[i] + ':deleted');
tid.push('pid:' + pids[i] + ':tid');
Posts.getPostData = function(pid, callback) {
RDB.hgetall('post:' + pid, function(err, data) {
if(err === null)
callback(data);
else
console.log(err);
});
}
Posts.getPostsByPids = function(pids, callback) {
var posts = [],
loaded = 0;
function getFavouritesData(next) {
favourites.getFavouritesByPostIDs(pids, current_user, function(fav_data) {
next(null, fav_data);
}); // to be moved
}
function getPostData(next) {
RDB.multi()
.mget(content)
.mget(uid)
.mget(timestamp)
.mget(post_rep)
.mget(editor)
.mget(editTime)
.mget(deleted)
.mget(tid)
.exec(function(err, replies) {
post_data = {
pid: pids,
content: replies[0],
uid: replies[1],
timestamp: replies[2],
reputation: replies[3],
editor: replies[4],
editTime: replies[5],
deleted: replies[6],
tid: replies[7]
};
for(var i=0, ii=pids.length; i<ii; ++i) {
(function(index, pid) {
Posts.getPostData(pid, function(postData) {
// below, to be deprecated
// Add any editors to the user_data object
for(var x = 0, numPosts = post_data.editor.length; x < numPosts; x++) {
if (post_data.editor[x] !== null && post_data.uid.indexOf(post_data.editor[x]) === -1) {
post_data.uid.push(post_data.editor[x]);
}
if(postData) {
postData.relativeTime = utils.relativeTime(postData.timestamp);
posts[index] = postData;
}
user.getMultipleUserFields(post_data.uid, ['username', 'userslug', 'reputation', 'picture', 'signature'], function(user_details) {
next(null, {
users: user_details,
posts: post_data
});
});
// above, to be deprecated
++loaded;
if(loaded === pids.length)
callback(posts);
});
}(i, pids[i]));
}
async.parallel([getFavouritesData, getPostData], function(err, results) {
callback({
'voteData' : results[0], // to be moved
'userData' : results[1].users, // to be moved
'postData' : results[1].posts
});
});
}
Posts.get_tid_by_pid = function(pid, callback) {
RDB.get('pid:' + pid + ':tid', function(err, tid) {
if (tid && parseInt(tid) > 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) {
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,35 +209,40 @@ 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.hmset('post:' + pid, {
'pid': pid,
'uid': uid,
'tid': tid,
'content': content,
'timestamp': timestamp,
'reputation': 0,
'editor': '',
'edited': 0,
'deleted': 0
});
topics.increasePostCount(tid);
topics.setTopicField(tid, 'lastposttime', timestamp);
topics.addToRecent(tid, timestamp);
RDB.incr('totalpostcount');
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);
feed.updateTopic(tid, cid);
// this is a bit of a naive implementation, defn something to look at post-MVP
@ -283,20 +251,12 @@ marked.setOptions({
RDB.spop('cid:' + cid + ':active_users');
}
//RDB.sadd('cid:' + cid + ':active_users', data.username);
RDB.sadd('cid:' + cid + ':active_users', uid);
});
});
});
// User Details - move this out later
RDB.lpush('uid:' + uid + ':posts', pid);
user.onNewPostMade(uid, tid, pid, timestamp);
user.incrementUserFieldBy(uid, 'postcount', 1);
user.setUserField(uid, 'lastposttime', timestamp);
user.sendPostNotificationToFollowers(uid, tid, pid);
if (callback)
callback(pid);
@ -307,39 +267,18 @@ 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) {
user.getPostIds(uid, start, end, function(pids) {
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);
Posts.getPostsByPids(pids, function(posts) {
callback(posts);
});
}
else
callback([]);
}
});
}

@ -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;

@ -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<ii; ++i) {
addUserInfoToPost(topicPosts[i], function() {
++done;
if(done === topicPosts.length)
callback();
});
}
}
Topics.getTopicById = function(tid, current_user, callback) {
Topics.markAsRead(tid, current_user);
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(tid, function(topicData) {
next(null, topicData);
});
}
function getTopicPosts(next) {
posts.getPostsByTid(tid, current_user, 0, 9, function(postData) {
next(null, postData);
Topics.getTopicPosts(tid, function(topicPosts) {
next(null, topicPosts);
});
}
@ -53,29 +82,28 @@ marked.setOptions({
async.parallel([getTopicData, getTopicPosts, getPrivileges], function(err, results) {
var topicData = results[0],
topicPosts = results[1],
privileges = results[2];
postTools.constructPostObject(results[1], tid, current_user, privileges, function(postObj) {
if (postObj.length) {
var main_posts = postObj.splice(0, 1);
constructPosts(topicPosts, function() {
var main_posts = topicPosts.splice(0, 1);
callback({
'topic_name':topicData.topic_name,
'topic_name':topicData.title,
'category_name':topicData.category_name,
'category_slug':topicData.category_slug,
'locked': parseInt(topicData.locked) || 0,
'deleted': parseInt(topicData.deleted) || 0,
'pinned': parseInt(topicData.pinned) || 0,
'locked': topicData.locked,
'deleted': topicData.deleted,
'pinned': topicData.pinned,
'slug': topicData.slug,
'topic_id': tid,
'expose_tools': privileges.editable ? 1 : 0,
'posts': postObj,
'posts': topicPosts,
'main_posts': main_posts
});
} else {
return callback(false);
}
});
});
}
@ -195,25 +223,11 @@ marked.setOptions({
});
}
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) {
posts.getPostField(pid, 'tid', function(tid) {
Topics.getTopicField(tid, 'title', function(title) {
callback(title);
});
} else callback('Could not grab title');
});
}
@ -312,7 +326,8 @@ marked.setOptions({
// end: probably should be moved into posts
Topics.post = function(socket, uid, title, content, category_id) {
if (!category_id) throw new Error('Attempted to post without a category_id');
if (!category_id)
throw new Error('Attempted to post without a category_id');
if (uid === 0) {
socket.emit('event:alert', {
@ -352,16 +367,24 @@ marked.setOptions({
}
var slug = tid + '/' + utils.slugify(title);
var timestamp = Date.now();
// Topic Info
RDB.set(schema.topics(tid).title, title);
RDB.set(schema.topics(tid).uid, uid);
RDB.set(schema.topics(tid).slug, slug);
RDB.set(schema.topics(tid).timestamp, Date.now());
RDB.hmset('topic:' + tid, {
'tid': tid,
'uid': uid,
'cid': category_id,
'title': title,
'slug': slug,
'timestamp': timestamp,
'lastposttime': 0,
'postcount': 0,
'locked': 0,
'deleted': 0,
'pinned': 0
});
RDB.set('topic:slug:' + slug + ':tid', tid);
RDB.set('topicslug:' + slug + ':tid', tid);
// Posts
posts.create(uid, tid, content, function(pid) {
if (pid > 0) {
RDB.lpush(schema.topics(tid).posts, pid);
@ -379,15 +402,7 @@ 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) {
@ -395,24 +410,54 @@ marked.setOptions({
});
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));

@ -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';

@ -301,7 +301,18 @@ 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);
});

@ -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', {

Loading…
Cancel
Save