2013-05-09 01:04:15 -04:00
|
|
|
var RDB = require('./redis.js'),
|
2013-05-23 13:21:38 -04:00
|
|
|
utils = require('./../public/src/utils.js'),
|
2013-06-25 14:38:17 -04:00
|
|
|
schema = require('./schema.js'),
|
2013-05-09 01:04:15 -04:00
|
|
|
user = require('./user.js'),
|
2013-05-09 07:27:55 +00:00
|
|
|
topics = require('./topics.js'),
|
2013-05-23 15:11:55 -04:00
|
|
|
favourites = require('./favourites.js'),
|
2013-05-23 12:52:16 -04:00
|
|
|
threadTools = require('./threadTools.js'),
|
2013-07-22 12:44:50 -04:00
|
|
|
postTools = require('./postTools'),
|
2013-05-27 14:55:03 -04:00
|
|
|
feed = require('./feed.js'),
|
2013-05-16 12:49:39 -04:00
|
|
|
async = require('async');
|
2013-05-09 01:04:15 -04:00
|
|
|
|
|
|
|
|
(function(Posts) {
|
2013-07-02 16:24:13 -04:00
|
|
|
|
2013-07-18 13:50:42 -04:00
|
|
|
Posts.minimumPostLength = 8;
|
|
|
|
|
|
2013-07-02 16:24:13 -04:00
|
|
|
Posts.getPostsByTid = function(tid, start, end, callback) {
|
2013-05-23 12:52:16 -04:00
|
|
|
RDB.lrange('tid:' + tid + ':posts', start, end, function(err, pids) {
|
2013-07-02 16:24:13 -04:00
|
|
|
|
2013-05-23 12:52:16 -04:00
|
|
|
RDB.handle(err);
|
2013-05-24 12:46:19 -04:00
|
|
|
|
2013-06-26 13:38:05 -04:00
|
|
|
if (pids.length) {
|
2013-07-02 16:24:13 -04:00
|
|
|
Posts.getPostsByPids(pids, function(posts) {
|
2013-06-26 13:38:05 -04:00
|
|
|
callback(posts);
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
callback({
|
|
|
|
|
error: 'no-posts'
|
|
|
|
|
});
|
|
|
|
|
}
|
2013-05-24 12:46:19 -04:00
|
|
|
});
|
|
|
|
|
}
|
2013-07-02 19:46:58 -04:00
|
|
|
|
|
|
|
|
Posts.addUserInfoToPost = function(post, callback) {
|
2013-07-17 10:05:29 -04:00
|
|
|
user.getUserFields(post.uid, ['username', 'userslug', 'reputation', 'postcount', 'picture', 'signature'], function(userData) {
|
2013-05-23 12:52:16 -04:00
|
|
|
|
2013-07-02 19:46:58 -04:00
|
|
|
post.username = userData.username || 'anonymous';
|
|
|
|
|
post.userslug = userData.userslug || '';
|
|
|
|
|
post.user_rep = userData.reputation || 0;
|
2013-07-17 10:05:29 -04:00
|
|
|
post.user_postcount = userData.postcount || 0;
|
2013-07-16 15:22:59 -04:00
|
|
|
post.picture = userData.picture || require('gravatar').url('', {}, https=global.nconf.get('https'));
|
2013-07-22 12:58:10 -04:00
|
|
|
post.signature = postTools.markdownToHTML(userData.signature);
|
2013-05-23 12:52:16 -04:00
|
|
|
|
2013-07-13 12:27:37 -04:00
|
|
|
if(post.editor !== '') {
|
|
|
|
|
user.getUserFields(post.editor, ['username', 'userslug'], function(editorData) {
|
|
|
|
|
post.editorname = editorData.username;
|
|
|
|
|
post.editorslug = editorData.userslug;
|
|
|
|
|
callback();
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
callback();
|
|
|
|
|
}
|
2013-07-02 19:46:58 -04:00
|
|
|
});
|
|
|
|
|
}
|
2013-05-24 14:34:55 -04:00
|
|
|
|
2013-07-02 19:46:58 -04:00
|
|
|
Posts.getPostSummaryByPids = function(pids, callback) {
|
|
|
|
|
|
|
|
|
|
var returnData = [];
|
|
|
|
|
|
2013-07-15 14:34:15 -04:00
|
|
|
function getPostSummary(pid, callback) {
|
2013-07-19 10:36:42 -04:00
|
|
|
Posts.getPostFields(pid, ['pid', 'tid', 'content', 'uid', 'timestamp', 'deleted'], function(postData) {
|
2013-07-15 14:34:15 -04:00
|
|
|
Posts.addUserInfoToPost(postData, function() {
|
|
|
|
|
|
|
|
|
|
if(postData.deleted !== '1') {
|
|
|
|
|
returnData.push(postData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
callback(null);
|
2013-05-24 14:34:55 -04:00
|
|
|
});
|
2013-07-15 14:34:15 -04:00
|
|
|
});
|
2013-07-02 19:46:58 -04:00
|
|
|
}
|
2013-07-15 14:34:15 -04:00
|
|
|
|
|
|
|
|
async.eachSeries(pids, getPostSummary, function(err) {
|
|
|
|
|
if(!err) {
|
|
|
|
|
callback(returnData);
|
|
|
|
|
}
|
|
|
|
|
});
|
2013-05-24 12:46:19 -04:00
|
|
|
};
|
|
|
|
|
|
2013-07-02 16:24:13 -04:00
|
|
|
Posts.getPostData = function(pid, callback) {
|
|
|
|
|
RDB.hgetall('post:' + pid, function(err, data) {
|
2013-07-19 10:59:24 -04:00
|
|
|
if(err === null) {
|
2013-07-02 16:24:13 -04:00
|
|
|
callback(data);
|
2013-07-19 10:59:24 -04:00
|
|
|
}
|
2013-07-02 16:24:13 -04:00
|
|
|
else
|
|
|
|
|
console.log(err);
|
|
|
|
|
});
|
|
|
|
|
}
|
2013-05-24 12:46:19 -04:00
|
|
|
|
2013-07-02 19:46:58 -04:00
|
|
|
Posts.getPostFields = function(uid, fields, callback) {
|
|
|
|
|
RDB.hmget('post:' + uid, fields, function(err, data) {
|
|
|
|
|
if(err === null) {
|
|
|
|
|
var returnData = {};
|
|
|
|
|
|
|
|
|
|
for(var i=0, ii=fields.length; i<ii; ++i) {
|
|
|
|
|
returnData[fields[i]] = data[i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
callback(returnData);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
console.log(err);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-02 16:24:13 -04:00
|
|
|
Posts.getPostsByPids = function(pids, callback) {
|
2013-07-19 15:26:29 -04:00
|
|
|
var posts = [];
|
|
|
|
|
|
|
|
|
|
function iterator(pid, callback) {
|
|
|
|
|
Posts.getPostData(pid, function(postData) {
|
|
|
|
|
if(postData) {
|
|
|
|
|
postData.relativeTime = utils.relativeTime(postData.timestamp);
|
|
|
|
|
postData.post_rep = postData.reputation;
|
|
|
|
|
postData['edited-class'] = postData.editor !== '' ? '' : 'none';
|
|
|
|
|
postData['relativeEditTime'] = postData.edited !== '0' ? utils.relativeTime(postData.edited) : '';
|
2013-07-22 12:44:50 -04:00
|
|
|
|
|
|
|
|
postData.content = postTools.markdownToHTML(postData.content);
|
|
|
|
|
|
2013-07-21 12:29:57 -04:00
|
|
|
if(postData.uploadedImages) {
|
|
|
|
|
postData.uploadedImages = JSON.parse(postData.uploadedImages);
|
|
|
|
|
} else {
|
|
|
|
|
postData.uploadedImages = [];
|
|
|
|
|
}
|
2013-07-19 15:26:29 -04:00
|
|
|
posts.push(postData);
|
|
|
|
|
}
|
|
|
|
|
callback(null);
|
|
|
|
|
});
|
2013-05-24 12:46:19 -04:00
|
|
|
}
|
2013-07-19 15:26:29 -04:00
|
|
|
|
|
|
|
|
async.eachSeries(pids, iterator, function(err) {
|
|
|
|
|
if(!err) {
|
|
|
|
|
callback(posts);
|
|
|
|
|
} else {
|
|
|
|
|
callback([]);
|
|
|
|
|
}
|
|
|
|
|
});
|
2013-05-16 18:15:58 -04:00
|
|
|
}
|
|
|
|
|
|
2013-07-02 16:24:13 -04:00
|
|
|
Posts.getPostField = function(pid, field, callback) {
|
2013-07-02 19:46:58 -04:00
|
|
|
RDB.hget('post:' + pid, field, function(err, data) {
|
2013-07-02 16:24:13 -04:00
|
|
|
if(err === null)
|
|
|
|
|
callback(data);
|
|
|
|
|
else
|
|
|
|
|
console.log(err);
|
2013-05-16 18:15:58 -04:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-03 13:08:32 -04:00
|
|
|
Posts.setPostField = function(pid, field, value) {
|
|
|
|
|
RDB.hset('post:' + pid, field, value);
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-02 17:01:49 -04:00
|
|
|
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<ii; ++i) {
|
|
|
|
|
returnData[fields[i]] = data[i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
callback(returnData);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
console.log(err);
|
|
|
|
|
});
|
|
|
|
|
}
|
2013-07-02 16:24:13 -04:00
|
|
|
|
2013-05-16 18:15:58 -04:00
|
|
|
Posts.get_cid_by_pid = function(pid, callback) {
|
2013-07-02 16:24:13 -04:00
|
|
|
Posts.getPostField(pid, 'tid', function(tid) {
|
|
|
|
|
if (tid) {
|
2013-07-02 19:02:23 -04:00
|
|
|
topics.getTopicField(tid, 'cid', function(cid) {
|
2013-07-02 16:24:13 -04:00
|
|
|
if (cid) {
|
|
|
|
|
callback(cid);
|
|
|
|
|
} else {
|
|
|
|
|
callback(false);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
2013-05-16 18:15:58 -04:00
|
|
|
}
|
2013-05-09 01:04:15 -04:00
|
|
|
|
2013-07-18 13:50:42 -04:00
|
|
|
Posts.emitContentTooShortAlert = function(socket) {
|
|
|
|
|
socket.emit('event:alert', {
|
|
|
|
|
type: 'error',
|
|
|
|
|
timeout: 2000,
|
|
|
|
|
title: 'Content too short',
|
2013-07-18 14:50:58 -04:00
|
|
|
message: "Please enter a longer post. At least " + Posts.minimumPostLength + " characters.",
|
2013-07-18 13:50:42 -04:00
|
|
|
alert_id: 'post_error'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-21 12:29:57 -04:00
|
|
|
Posts.reply = function(socket, tid, uid, content, images) {
|
2013-05-14 17:19:22 -04:00
|
|
|
if (uid < 1) {
|
|
|
|
|
socket.emit('event:alert', {
|
|
|
|
|
title: 'Reply Unsuccessful',
|
|
|
|
|
message: 'You don't seem to be logged in, so you cannot reply.',
|
|
|
|
|
type: 'error',
|
|
|
|
|
timeout: 2000
|
|
|
|
|
});
|
|
|
|
|
return;
|
2013-07-18 13:50:42 -04:00
|
|
|
} else if (!content || content.length < Posts.minimumPostLength) {
|
|
|
|
|
Posts.emitContentTooShortAlert(socket);
|
2013-07-17 11:22:09 -04:00
|
|
|
return;
|
2013-05-14 17:19:22 -04:00
|
|
|
}
|
|
|
|
|
|
2013-05-24 12:53:45 -04:00
|
|
|
user.getUserField(uid, 'lastposttime', function(lastposttime) {
|
2013-07-02 16:24:13 -04:00
|
|
|
|
2013-06-19 21:29:50 -04:00
|
|
|
if(Date.now() - lastposttime < config.post_delay) {
|
2013-05-09 01:04:15 -04:00
|
|
|
socket.emit('event:alert', {
|
2013-05-24 12:32:28 -04:00
|
|
|
title: 'Too many posts!',
|
|
|
|
|
message: 'You can only post every '+ (config.post_delay / 1000) + ' seconds.',
|
|
|
|
|
type: 'error',
|
2013-05-09 01:04:15 -04:00
|
|
|
timeout: 2000
|
|
|
|
|
});
|
2013-05-24 12:32:28 -04:00
|
|
|
return;
|
|
|
|
|
}
|
2013-05-09 01:04:15 -04:00
|
|
|
|
2013-07-21 12:29:57 -04:00
|
|
|
Posts.create(uid, tid, content, images, function(postData) {
|
|
|
|
|
if (postData) {
|
2013-07-22 13:06:31 -04:00
|
|
|
topics.addPostToTopic(tid, postData.pid);
|
|
|
|
|
|
|
|
|
|
topics.markUnRead(tid);
|
2013-05-24 14:34:55 -04:00
|
|
|
|
2013-07-21 12:29:57 -04:00
|
|
|
Posts.get_cid_by_pid(postData.pid, function(cid) {
|
2013-06-28 11:36:41 -04:00
|
|
|
RDB.del('cid:' + cid + ':read_by_uid', function(err, data) {
|
|
|
|
|
topics.markAsRead(tid, uid);
|
|
|
|
|
});
|
2013-05-09 01:04:15 -04:00
|
|
|
});
|
2013-05-24 12:32:28 -04:00
|
|
|
|
2013-07-02 19:46:58 -04:00
|
|
|
Posts.getTopicPostStats(socket);
|
|
|
|
|
|
|
|
|
|
// Send notifications to users who are following this topic
|
|
|
|
|
threadTools.notify_followers(tid, uid);
|
2013-05-24 12:32:28 -04:00
|
|
|
|
|
|
|
|
socket.emit('event:alert', {
|
|
|
|
|
title: 'Reply Successful',
|
|
|
|
|
message: 'You have successfully replied. Click here to view your reply.',
|
|
|
|
|
type: 'notify',
|
|
|
|
|
timeout: 2000
|
|
|
|
|
});
|
|
|
|
|
|
2013-07-22 12:58:10 -04:00
|
|
|
postData.content = postTools.markdownToHTML(postData.content);
|
2013-07-21 12:29:57 -04:00
|
|
|
postData.post_rep = 0;
|
|
|
|
|
postData.relativeTime = utils.relativeTime(postData.timestamp)
|
|
|
|
|
postData.fav_star_class = 'icon-star-empty';
|
|
|
|
|
postData['edited-class'] = 'none';
|
|
|
|
|
postData.uploadedImages = JSON.parse(postData.uploadedImages);
|
2013-06-06 20:39:45 -04:00
|
|
|
|
2013-07-03 13:40:55 -04:00
|
|
|
var socketData = {
|
|
|
|
|
'posts' : [
|
2013-07-21 12:29:57 -04:00
|
|
|
postData
|
2013-07-03 13:40:55 -04:00
|
|
|
]
|
|
|
|
|
};
|
2013-06-27 21:11:04 -04:00
|
|
|
|
2013-07-03 13:40:55 -04:00
|
|
|
posts.addUserInfoToPost(socketData['posts'][0], function() {
|
2013-06-27 21:11:04 -04:00
|
|
|
io.sockets.in('topic_' + tid).emit('event:new_post', socketData);
|
|
|
|
|
io.sockets.in('recent_posts').emit('event:new_post', socketData);
|
2013-07-03 13:40:55 -04:00
|
|
|
});
|
2013-07-21 12:29:57 -04:00
|
|
|
|
2013-07-03 13:40:55 -04:00
|
|
|
|
2013-05-24 12:32:28 -04:00
|
|
|
} else {
|
|
|
|
|
socket.emit('event:alert', {
|
|
|
|
|
title: 'Reply Unsuccessful',
|
|
|
|
|
message: 'Your reply could not be posted at this time. Please try again later.',
|
|
|
|
|
type: 'notify',
|
|
|
|
|
timeout: 2000
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
2013-05-09 01:04:15 -04:00
|
|
|
});
|
|
|
|
|
};
|
2013-07-21 12:29:57 -04:00
|
|
|
|
|
|
|
|
Posts.create = function(uid, tid, content, images, callback) {
|
2013-07-02 16:24:13 -04:00
|
|
|
if (uid === null) {
|
2013-07-21 12:29:57 -04:00
|
|
|
callback(null);
|
2013-07-02 16:24:13 -04:00
|
|
|
return;
|
|
|
|
|
}
|
2013-05-09 01:04:15 -04:00
|
|
|
|
2013-07-02 16:24:13 -04:00
|
|
|
topics.isLocked(tid, function(locked) {
|
2013-05-09 01:04:15 -04:00
|
|
|
|
|
|
|
|
if (!locked || locked === '0') {
|
|
|
|
|
RDB.incr('global:next_post_id', function(err, pid) {
|
|
|
|
|
RDB.handle(err);
|
2013-05-24 12:32:28 -04:00
|
|
|
|
2013-06-19 21:29:50 -04:00
|
|
|
var timestamp = Date.now();
|
2013-05-09 01:04:15 -04:00
|
|
|
|
2013-07-21 12:29:57 -04:00
|
|
|
var postData = {
|
2013-07-02 16:24:13 -04:00
|
|
|
'pid': pid,
|
|
|
|
|
'uid': uid,
|
|
|
|
|
'tid': tid,
|
|
|
|
|
'content': content,
|
|
|
|
|
'timestamp': timestamp,
|
|
|
|
|
'reputation': 0,
|
|
|
|
|
'editor': '',
|
|
|
|
|
'edited': 0,
|
2013-07-21 12:29:57 -04:00
|
|
|
'deleted': 0,
|
|
|
|
|
'uploadedImages': ''
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
RDB.hmset('post:' + pid, postData);
|
2013-06-28 10:56:38 -04:00
|
|
|
|
2013-07-02 16:24:13 -04:00
|
|
|
topics.increasePostCount(tid);
|
2013-07-18 15:14:06 -04:00
|
|
|
topics.updateTimestamp(tid, timestamp);
|
2013-05-15 15:42:24 -04:00
|
|
|
|
2013-07-02 16:24:13 -04:00
|
|
|
RDB.incr('totalpostcount');
|
|
|
|
|
|
2013-07-02 19:02:23 -04:00
|
|
|
topics.getTopicField(tid, 'cid', function(cid) {
|
2013-07-02 16:24:13 -04:00
|
|
|
RDB.handle(err);
|
2013-05-27 14:55:03 -04:00
|
|
|
|
2013-07-02 16:24:13 -04:00
|
|
|
feed.updateTopic(tid, cid);
|
2013-05-27 14:55:03 -04:00
|
|
|
|
2013-07-06 22:02:24 -04:00
|
|
|
RDB.zadd('categories:recent_posts:cid:' + cid, Date.now(), pid);
|
|
|
|
|
|
2013-07-02 16:24:13 -04:00
|
|
|
// 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');
|
|
|
|
|
}
|
2013-05-15 15:42:24 -04:00
|
|
|
|
2013-07-02 16:24:13 -04:00
|
|
|
RDB.sadd('cid:' + cid + ':active_users', uid);
|
2013-05-15 15:42:24 -04:00
|
|
|
});
|
2013-07-18 15:14:06 -04:00
|
|
|
});
|
2013-07-06 22:02:24 -04:00
|
|
|
|
2013-07-02 19:46:58 -04:00
|
|
|
user.onNewPostMade(uid, tid, pid, timestamp);
|
2013-06-18 10:01:42 -04:00
|
|
|
|
2013-07-21 12:29:57 -04:00
|
|
|
var imgur = require('./imgur');
|
|
|
|
|
// move clientID to config
|
|
|
|
|
imgur.setClientID('09f3955fee9a0a6');
|
|
|
|
|
|
|
|
|
|
var uploadedImages = [];
|
|
|
|
|
|
|
|
|
|
function uploadImage(image, callback) {
|
|
|
|
|
imgur.upload(image.data, 'base64', function(err, data) {
|
|
|
|
|
if(err) {
|
|
|
|
|
callback(err);
|
|
|
|
|
} else {
|
|
|
|
|
if(data.success) {
|
|
|
|
|
var img= {url:data.data.link, name:image.name};
|
|
|
|
|
uploadedImages.push(img);
|
|
|
|
|
callback(null);
|
|
|
|
|
} else {
|
|
|
|
|
callback(data);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async.each(images, uploadImage, function(err) {
|
|
|
|
|
if(!err) {
|
|
|
|
|
postData.uploadedImages = JSON.stringify(uploadedImages);
|
|
|
|
|
Posts.setPostField(pid, 'uploadedImages', postData.uploadedImages);
|
|
|
|
|
|
|
|
|
|
callback(postData);
|
|
|
|
|
} else {
|
|
|
|
|
console.log(err);
|
|
|
|
|
callback(null);
|
|
|
|
|
}
|
|
|
|
|
});
|
2013-05-09 01:04:15 -04:00
|
|
|
});
|
|
|
|
|
} else {
|
2013-07-21 12:29:57 -04:00
|
|
|
callback(null);
|
2013-05-09 01:04:15 -04:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2013-06-26 23:04:49 -04:00
|
|
|
|
2013-07-02 16:24:13 -04:00
|
|
|
Posts.getPostsByUid = function(uid, start, end, callback) {
|
2013-06-26 23:04:49 -04:00
|
|
|
|
2013-07-02 16:24:13 -04:00
|
|
|
user.getPostIds(uid, start, end, function(pids) {
|
|
|
|
|
|
|
|
|
|
if(pids && pids.length) {
|
|
|
|
|
|
|
|
|
|
Posts.getPostsByPids(pids, function(posts) {
|
|
|
|
|
callback(posts);
|
|
|
|
|
});
|
2013-06-26 23:04:49 -04:00
|
|
|
}
|
2013-07-02 16:24:13 -04:00
|
|
|
else
|
|
|
|
|
callback([]);
|
2013-06-26 23:04:49 -04:00
|
|
|
});
|
|
|
|
|
}
|
2013-05-23 15:11:55 -04:00
|
|
|
|
2013-06-28 10:56:38 -04:00
|
|
|
Posts.getTopicPostStats = function(socket) {
|
|
|
|
|
RDB.mget(['totaltopiccount', 'totalpostcount'], function(err, data) {
|
|
|
|
|
if(err === null) {
|
|
|
|
|
var stats = {
|
2013-06-28 12:54:56 -04:00
|
|
|
topics: data[0]?data[0]:0,
|
|
|
|
|
posts: data[1]?data[1]:0
|
2013-06-28 10:56:38 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
socket.emit('post.stats', stats);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
console.log(err);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-24 20:40:34 +00:00
|
|
|
}(exports));
|