Files
NodeBB/src/posts.js

396 lines
9.7 KiB
JavaScript
Raw Normal View History

var RDB = require('./redis.js'),
utils = require('./../public/src/utils.js'),
schema = require('./schema.js'),
user = require('./user.js'),
topics = require('./topics.js'),
2013-05-23 15:11:55 -04:00
favourites = require('./favourites.js'),
threadTools = require('./threadTools.js'),
postTools = require('./postTools'),
feed = require('./feed.js'),
2013-05-16 12:49:39 -04:00
async = require('async');
(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) {
RDB.lrange('tid:' + tid + ':posts', start, end, function(err, pids) {
2013-07-02 16:24:13 -04:00
RDB.handle(err);
2013-05-24 12:46:19 -04:00
if (pids.length) {
2013-07-02 16:24:13 -04:00
Posts.getPostsByPids(pids, function(posts) {
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-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;
post.picture = userData.picture || require('gravatar').url('', {}, https=global.nconf.get('https'));
post.signature = postTools.markdownToHTML(userData.signature, true);
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-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-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) {
if(err === null) {
2013-07-02 16:24:13 -04:00
callback(data);
}
2013-07-02 16:24:13 -04:00
else
console.log(err);
});
}
2013-05-24 12:46:19 -04:00
2013-07-22 17:08:07 -04:00
Posts.getPostFields = function(pid, fields, callback) {
RDB.hmgetObject('post:' + pid, fields, function(err, data) {
2013-07-02 19:46:58 -04:00
if(err === null) {
2013-07-22 17:08:07 -04:00
callback(data);
2013-07-02 19:46:58 -04:00
}
2013-07-22 17:08:07 -04:00
else {
2013-07-02 19:46:58 -04:00
console.log(err);
2013-07-22 17:08:07 -04:00
}
2013-07-02 19:46:58 -04:00
});
}
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) : '';
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-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);
});
}
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
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-07-18 13:50:42 -04:00
Posts.emitContentTooShortAlert = function(socket) {
socket.emit('event:alert', {
type: 'error',
timeout: 2000,
title: 'Content too short',
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) {
if (uid < 1) {
socket.emit('event:alert', {
title: 'Reply Unsuccessful',
message: 'You don&apos;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;
}
user.getUserField(uid, 'lastposttime', function(lastposttime) {
2013-07-02 16:24:13 -04:00
if(Date.now() - lastposttime < config.post_delay) {
socket.emit('event:alert', {
title: 'Too many posts!',
message: 'You can only post every '+ (config.post_delay / 1000) + ' seconds.',
type: 'error',
timeout: 2000
});
return;
}
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-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-07-02 19:46:58 -04:00
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.',
type: 'notify',
timeout: 2000
});
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-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-07-03 13:40:55 -04:00
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);
2013-07-03 13:40:55 -04:00
});
2013-07-21 12:29:57 -04:00
2013-07-03 13:40:55 -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-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-07-02 16:24:13 -04:00
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();
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-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-07-02 16:24:13 -04:00
feed.updateTopic(tid, cid);
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-07-02 16:24:13 -04:00
RDB.sadd('cid:' + cid + ':active_users', uid);
});
2013-07-18 15:14:06 -04:00
});
2013-07-02 19:46:58 -04:00
user.onNewPostMade(uid, tid, pid, timestamp);
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);
}
}
});
}
2013-07-22 17:26:18 -04:00
if(!images) {
postData.uploadedImages = JSON.stringify(uploadedImages);
Posts.setPostField(pid, 'uploadedImages', postData.uploadedImages);
callback(postData);
} else {
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);
}
});
}
});
} else {
2013-07-21 12:29:57 -04:00
callback(null);
}
});
}
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);
});
}
}(exports));