Files
NodeBB/src/socket.io/posts.js

388 lines
10 KiB
JavaScript
Raw Normal View History

2014-03-11 14:48:35 -04:00
"use strict";
2014-01-30 17:41:54 -05:00
var async = require('async'),
nconf = require('nconf'),
2014-05-12 14:51:39 -04:00
db = require('../database'),
2014-01-30 17:41:54 -05:00
posts = require('../posts'),
privileges = require('../privileges'),
2014-01-10 16:00:03 -05:00
meta = require('../meta'),
topics = require('../topics'),
favourites = require('../favourites'),
postTools = require('../postTools'),
2014-01-30 17:41:54 -05:00
notifications = require('../notifications'),
groups = require('../groups'),
2014-01-22 21:08:43 +01:00
user = require('../user'),
websockets = require('./index'),
2014-01-10 16:00:03 -05:00
SocketPosts = {};
2014-01-16 15:34:43 -05:00
SocketPosts.reply = function(socket, data, callback) {
if(!data || !data.tid || !data.content) {
return callback(new Error('[[error:invalid-data]]'));
2014-01-16 22:06:23 -05:00
}
2014-02-22 17:56:13 -05:00
data.uid = socket.uid;
data.req = websockets.reqFromSocket(socket);
2014-02-22 17:56:13 -05:00
topics.reply(data, function(err, postData) {
2014-01-10 16:00:03 -05:00
if(err) {
2014-01-16 22:06:23 -05:00
return callback(err);
2014-01-10 16:00:03 -05:00
}
if (postData) {
var privileges = {
2014-05-22 18:24:58 -04:00
'topics:reply': true
};
callback();
socket.emit('event:new_post', {
posts: [postData],
privileges: privileges,
'reputation:disabled': parseInt(meta.config['reputation:disabled'], 10) === 1,
'downvote:disabled': parseInt(meta.config['downvote:disabled'], 10) === 1,
});
socket.broadcast.emit('event:new_post', {
posts: [postData],
privileges: privileges,
2014-09-02 05:04:39 -04:00
'reputation:disabled': parseInt(meta.config['reputation:disabled'], 10) === 1,
'downvote:disabled': parseInt(meta.config['downvote:disabled'], 10) === 1,
2014-04-16 15:46:50 -04:00
});
2014-03-11 15:15:48 -04:00
2014-09-07 14:13:05 -04:00
websockets.emitTopicPostStats();
2014-07-17 18:50:03 -04:00
topics.pushUnreadCount();
2014-01-10 16:00:03 -05:00
}
});
};
SocketPosts.upvote = function(socket, data, callback) {
2014-08-12 13:45:18 -04:00
if (!data || !data.pid) {
return callback(new Error('[[error:invalid-data]]'));
}
favouriteCommand('upvote', 'voted', socket, data, callback);
2014-08-12 13:45:18 -04:00
SocketPosts.sendNotificationToPostOwner(data.pid, socket.uid, 'notifications:upvoted_your_post');
2014-02-06 12:44:21 -05:00
};
SocketPosts.downvote = function(socket, data, callback) {
favouriteCommand('downvote', 'voted', socket, data, callback);
2014-02-06 12:44:21 -05:00
};
SocketPosts.unvote = function(socket, data, callback) {
favouriteCommand('unvote', 'voted', socket, data, callback);
2014-02-06 12:44:21 -05:00
};
SocketPosts.favourite = function(socket, data, callback) {
2014-08-12 13:45:18 -04:00
if (!data || !data.pid) {
return callback(new Error('[[error:invalid-data]]'));
}
favouriteCommand('favourite', 'favourited', socket, data, callback);
2014-08-12 13:45:18 -04:00
SocketPosts.sendNotificationToPostOwner(data.pid, socket.uid, 'notifications:favourited_your_post');
2014-01-10 16:00:03 -05:00
};
SocketPosts.unfavourite = function(socket, data, callback) {
favouriteCommand('unfavourite', 'favourited', socket, data, callback);
2014-03-11 21:52:22 -04:00
};
function favouriteCommand(command, eventName, socket, data, callback) {
2014-01-16 22:06:23 -05:00
if(data && data.pid && data.room_id) {
favourites[command](data.pid, socket.uid, function(err, result) {
if (err) {
return callback(err);
}
socket.emit('posts.' + command, result);
if(data.room_id && result && eventName) {
websockets.in(data.room_id).emit('event:' + eventName, result);
}
callback();
});
2014-01-16 22:06:23 -05:00
}
2014-03-11 21:52:22 -04:00
}
2014-01-10 16:00:03 -05:00
2014-08-12 13:45:18 -04:00
SocketPosts.sendNotificationToPostOwner = function(pid, fromuid, notification) {
if(!pid || !fromuid) {
return;
}
posts.getPostFields(pid, ['tid', 'uid'], function(err, postData) {
if (err) {
return;
}
if (fromuid === parseInt(postData.uid, 10)) {
return;
}
2014-03-24 14:30:11 -04:00
2014-08-12 13:45:18 -04:00
async.parallel({
username: async.apply(user.getUserField, fromuid, 'username'),
postContent: function(next) {
async.waterfall([
async.apply(posts.getPostField, pid, 'content'),
function(content, next) {
postTools.parse(content, next);
}
], next);
}
}, function(err, results) {
if (err) {
2014-03-25 12:10:42 -04:00
return;
}
2014-08-12 13:45:18 -04:00
notifications.create({
bodyShort: '[[' + notification + ', ' + results.username + ']]',
bodyLong: results.postContent,
2014-08-17 00:14:45 -04:00
pid: pid,
2014-09-08 23:03:37 -04:00
nid: 'post:' + pid + ':uid:' + fromuid,
2014-08-12 13:45:18 -04:00
from: fromuid
2014-09-08 23:03:37 -04:00
}, function(err, notification) {
if (!err && notification) {
notifications.push(notification, [postData.uid]);
2014-03-24 14:30:11 -04:00
}
});
});
2014-08-12 13:45:18 -04:00
});
};
2014-03-24 14:30:11 -04:00
2014-01-16 22:06:23 -05:00
SocketPosts.getRawPost = function(socket, pid, callback) {
2014-05-08 13:24:34 -04:00
async.waterfall([
function(next) {
2014-05-17 18:59:34 -04:00
privileges.posts.can('read', pid, socket.uid, next);
2014-05-08 13:24:34 -04:00
},
function(canRead, next) {
if (!canRead) {
2014-05-08 13:24:34 -04:00
return next(new Error('[[error:no-privileges]]'));
}
posts.getPostFields(pid, ['content', 'deleted'], next);
2014-08-02 16:41:44 -04:00
},
function(postData, next) {
if (parseInt(postData.deleted, 10) === 1) {
return next(new Error('[[error:no-post]]'));
}
next(null, postData.content);
2014-05-08 13:24:34 -04:00
}
2014-08-02 16:41:44 -04:00
], callback);
2014-01-10 16:00:03 -05:00
};
2014-01-16 15:34:43 -05:00
SocketPosts.edit = function(socket, data, callback) {
if(!socket.uid) {
2014-04-09 15:39:26 -04:00
return callback(new Error('[[error:not-logged-in]]'));
2014-01-16 22:06:23 -05:00
} else if(!data || !data.pid || !data.title || !data.content) {
2014-04-09 15:39:26 -04:00
return callback(new Error('[[error:invalid-data]]'));
2014-01-10 16:00:03 -05:00
} else if (!data.title || data.title.length < parseInt(meta.config.minimumTitleLength, 10)) {
2014-04-09 15:39:26 -04:00
return callback(new Error('[[error:title-too-short, ' + meta.config.minimumTitleLength + ']]'));
} else if (data.title.length > parseInt(meta.config.maximumTitleLength, 10)) {
return callback(new Error('[[error:title-too-long, ' + meta.config.maximumTitleLength + ']]'));
2014-01-10 16:00:03 -05:00
} else if (!data.content || data.content.length < parseInt(meta.config.minimumPostLength, 10)) {
2014-04-09 15:39:26 -04:00
return callback(new Error('[[error:content-too-short, ' + meta.config.minimumPostLength + ']]'));
2014-01-10 16:00:03 -05:00
}
postTools.edit(socket.uid, data.pid, data.title, data.content, {topic_thumb: data.topic_thumb, tags: data.tags}, function(err, results) {
2014-03-01 19:15:18 -05:00
if(err) {
return callback(err);
}
websockets.server.sockets.in('topic_' + results.topic.tid).emit('event:post_edited', {
2014-03-01 19:15:18 -05:00
pid: data.pid,
title: results.topic.title,
isMainPost: results.topic.isMainPost,
2014-07-01 14:49:45 -04:00
tags: results.topic.tags,
2014-03-01 19:15:18 -05:00
content: results.content
});
callback();
});
2014-01-10 16:00:03 -05:00
};
2014-01-16 15:34:43 -05:00
SocketPosts.delete = function(socket, data, callback) {
2014-03-11 21:52:22 -04:00
deleteOrRestore('delete', socket, data, callback);
2014-01-10 16:00:03 -05:00
};
2014-01-16 15:34:43 -05:00
SocketPosts.restore = function(socket, data, callback) {
2014-03-11 21:52:22 -04:00
deleteOrRestore('restore', socket, data, callback);
};
function deleteOrRestore(command, socket, data, callback) {
2014-01-16 22:06:23 -05:00
if(!data) {
2014-04-09 21:40:39 -04:00
return callback(new Error('[[error:invalid-data]]'));
2014-01-16 22:06:23 -05:00
}
2014-05-27 14:53:47 -04:00
postTools[command](socket.uid, data.pid, function(err, postData) {
2014-01-10 16:00:03 -05:00
if(err) {
return callback(err);
}
2014-09-07 14:13:05 -04:00
websockets.emitTopicPostStats();
2014-01-10 16:00:03 -05:00
2014-03-11 21:52:22 -04:00
var eventName = command === 'restore' ? 'event:post_restored' : 'event:post_deleted';
2014-05-27 14:53:47 -04:00
websockets.server.sockets.in('topic_' + data.tid).emit(eventName, postData);
2014-01-16 22:06:23 -05:00
callback();
2014-01-10 16:00:03 -05:00
});
2014-03-11 21:52:22 -04:00
}
2014-01-10 16:00:03 -05:00
2014-06-10 14:24:50 -04:00
SocketPosts.purge = function(socket, data, callback) {
if(!data) {
return callback(new Error('[[error:invalid-data]]'));
}
postTools.purge(socket.uid, data.pid, function(err) {
if(err) {
return callback(err);
}
2014-09-07 14:13:05 -04:00
websockets.emitTopicPostStats();
2014-06-10 14:24:50 -04:00
websockets.server.sockets.in('topic_' + data.tid).emit('event:post_purged', data.pid);
callback();
});
};
SocketPosts.getPrivileges = function(socket, pid, callback) {
privileges.posts.get([pid], socket.uid, function(err, privileges) {
if (err) {
return callback(err);
}
if (!Array.isArray(privileges) || !privileges.length) {
return callback(new Error('[[error:invalid-data]]'));
}
privileges[0].pid = parseInt(pid, 10);
callback(null, privileges[0]);
});
};
SocketPosts.getUpvoters = function(socket, pid, callback) {
favourites.getUpvotedUidsByPids([pid], function(err, data) {
if (err || !Array.isArray(data) || !data.length) {
return callback(err, []);
}
var otherCount = 0;
if (data[0].length > 6) {
otherCount = data[0].length - 5;
data[0] = data[0].slice(0, 5);
}
user.getUsernamesByUids(data[0], function(err, usernames) {
callback(err, {
otherCount: otherCount,
usernames: usernames
});
});
2014-01-22 21:08:43 +01:00
});
};
2014-01-30 17:41:54 -05:00
SocketPosts.flag = function(socket, pid, callback) {
2014-02-01 23:14:44 -05:00
if (!socket.uid) {
2014-04-09 21:40:39 -04:00
return callback(new Error('[[error:not-logged-in]]'));
2014-02-01 23:14:44 -05:00
}
2014-01-30 17:41:54 -05:00
var message = '',
2014-05-12 14:51:39 -04:00
post;
2014-01-30 17:41:54 -05:00
async.waterfall([
function(next) {
2014-08-30 15:39:20 -04:00
user.getUserFields(socket.uid, ['username', 'reputation'], next);
2014-01-30 17:41:54 -05:00
},
2014-08-30 15:39:20 -04:00
function(userData, next) {
if (parseInt(userData.reputation, 10) < parseInt(meta.config['privileges:flag'] || 1, 10)) {
return next(new Error('[[error:not-enough-reputation-to-flag]]'));
}
message = '[[notifications:user_flagged_post, ' + userData.username + ']]';
posts.getPostFields(pid, ['tid', 'uid', 'content'], next);
2014-01-30 17:41:54 -05:00
},
function(postData, next) {
postTools.parse(postData.content, function(err, parsed) {
2014-08-30 15:39:20 -04:00
if (err) {
return next(err);
}
postData.content = parsed;
2014-08-30 15:39:20 -04:00
next(null, postData);
});
},
2014-05-12 14:51:39 -04:00
function(postData, next) {
post = postData;
groups.get('administrators', {}, next);
2014-01-30 17:41:54 -05:00
},
function(adminGroup, next) {
notifications.create({
2014-06-20 17:53:40 -04:00
bodyShort: message,
bodyLong: post.content,
2014-08-17 00:14:45 -04:00
pid: pid,
2014-09-08 23:03:37 -04:00
nid: 'post_flag:' + pid + ':uid:' + socket.uid,
from: socket.uid
2014-09-08 23:03:37 -04:00
}, function(err, notification) {
if (err || !notification) {
2014-07-28 15:52:33 -04:00
return next(err);
}
2014-09-08 23:03:37 -04:00
notifications.push(notification, adminGroup.members, next);
2014-05-12 14:51:39 -04:00
});
},
function(next) {
if (!parseInt(post.uid, 10)) {
return next();
}
db.setAdd('uid:' + post.uid + ':flagged_by', socket.uid, function(err) {
if (err) {
return next(err);
}
db.setCount('uid:' + post.uid + ':flagged_by', function(err, count) {
if (err) {
return next(err);
}
2014-05-12 14:53:53 -04:00
if (count >= (meta.config.flagsForBan || 3) && parseInt(meta.config.flagsForBan, 10) !== 0) {
2014-05-12 14:51:39 -04:00
var adminUser = require('./admin/user');
adminUser.banUser(post.uid, next);
return;
}
next();
2014-01-30 17:41:54 -05:00
});
});
}
], callback);
2014-03-11 14:48:35 -04:00
};
2014-01-30 17:41:54 -05:00
SocketPosts.loadMoreFavourites = function(socket, data, callback) {
if(!data || !data.after) {
2014-04-09 21:40:39 -04:00
return callback(new Error('[[error:invalid-data]]'));
}
var start = parseInt(data.after, 10),
end = start + 9;
posts.getFavourites(socket.uid, start, end, callback);
};
2014-02-04 17:31:05 -05:00
SocketPosts.loadMoreUserPosts = function(socket, data, callback) {
if(!data || !data.after || !data.uid) {
2014-04-09 21:40:39 -04:00
return callback(new Error('[[error:invalid-data]]'));
2014-02-04 17:31:05 -05:00
}
var start = parseInt(data.after, 10),
end = start + 9;
posts.getPostsByUid(socket.uid, data.uid, start, end, callback);
};
2014-03-11 21:52:22 -04:00
2014-03-11 23:31:23 -04:00
SocketPosts.getRecentPosts = function(socket, data, callback) {
if(!data || !data.count) {
2014-04-09 21:40:39 -04:00
return callback(new Error('[[error:invalid-data]]'));
2014-03-11 23:31:23 -04:00
}
posts.getRecentPosts(socket.uid, 0, data.count - 1, data.term, callback);
2014-03-11 18:46:16 -04:00
};
2014-03-13 20:24:04 -04:00
SocketPosts.getCategory = function(socket, pid, callback) {
posts.getCidByPid(pid, callback);
2014-03-24 14:30:11 -04:00
};
2014-03-11 18:46:16 -04:00
2014-08-27 15:03:36 -04:00
SocketPosts.getPidIndex = function(socket, pid, callback) {
posts.getPidIndex(pid, socket.uid, callback);
};
2014-04-10 20:31:57 +01:00
module.exports = SocketPosts;