You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

487 lines
13 KiB
JavaScript

11 years ago
"use strict";
11 years ago
var async = require('async'),
nconf = require('nconf'),
10 years ago
winston = require('winston'),
11 years ago
11 years ago
db = require('../database'),
11 years ago
posts = require('../posts'),
plugins = require('../plugins'),
privileges = require('../privileges'),
meta = require('../meta'),
topics = require('../topics'),
favourites = require('../favourites'),
postTools = require('../postTools'),
11 years ago
notifications = require('../notifications'),
groups = require('../groups'),
user = require('../user'),
websockets = require('./index'),
10 years ago
utils = require('../../public/src/utils'),
SocketPosts = {};
SocketPosts.reply = function(socket, data, callback) {
if(!data || !data.tid || !data.content) {
return callback(new Error('[[error:invalid-data]]'));
11 years ago
}
data.uid = socket.uid;
data.req = websockets.reqFromSocket(socket);
topics.reply(data, function(err, postData) {
if (err) {
11 years ago
return callback(err);
}
var result = {
posts: [postData],
privileges: {
'topics:reply': true
},
'reputation:disabled': parseInt(meta.config['reputation:disabled'], 10) === 1,
'downvote:disabled': parseInt(meta.config['downvote:disabled'], 10) === 1,
};
callback();
socket.emit('event:new_post', result);
10 years ago
async.waterfall([
function(next) {
user.getUidsFromSet('users:online', 0, -1, next);
},
function(uids, next) {
privileges.categories.filterUids('read', postData.topic.cid, uids, next);
},
function(uids, next) {
plugins.fireHook('filter:sockets.sendNewPostToUids', {uidsTo: uids, uidFrom: data.uid, type: 'newPost'}, next);
}
], function(err, data) {
if (err) {
10 years ago
return winston.error(err.stack);
}
10 years ago
var uids = data.uidsTo;
10 years ago
for(var i=0; i<uids.length; ++i) {
if (parseInt(uids[i], 10) !== socket.uid) {
websockets.in('uid_' + uids[i]).emit('event:new_post', result);
}
}
});
});
};
SocketPosts.getVoters = function(socket, data, callback) {
if (!data || !data.pid || !data.cid) {
return callback(new Error('[[error:invalid-data]]'));
}
var pid = data.pid,
cid = data.cid;
async.parallel({
isAdmin: function(next) {
user.isAdministrator(socket.uid, next);
},
isModerator: function(next) {
user.isModerator(socket.uid, cid, next);
}
}, function(err, tests) {
if (err) {
return callback(err);
}
if (tests.isAdmin || tests.isModerator) {
getVoters(pid, callback);
}
});
};
function getVoters(pid, callback) {
async.parallel({
upvoteUids: function(next) {
db.getSetMembers('pid:' + pid + ':upvote', next);
},
downvoteUids: function(next) {
db.getSetMembers('pid:' + pid + ':downvote', next);
}
}, function(err, results) {
if (err) {
return callback(err);
}
async.parallel({
upvoters: function(next) {
user.getMultipleUserFields(results.upvoteUids, ['username', 'userslug', 'picture'], next);
},
upvoteCount: function(next) {
next(null, results.upvoteUids.length);
},
downvoters: function(next) {
user.getMultipleUserFields(results.downvoteUids, ['username', 'userslug', 'picture'], next);
},
downvoteCount: function(next) {
next(null, results.downvoteUids.length);
}
}, callback);
});
}
SocketPosts.upvote = function(socket, data, callback) {
10 years ago
favouriteCommand(socket, 'upvote', 'voted', 'notifications:upvoted_your_post_in', data, callback);
};
SocketPosts.downvote = function(socket, data, callback) {
10 years ago
favouriteCommand(socket, 'downvote', 'voted', '', data, callback);
};
SocketPosts.unvote = function(socket, data, callback) {
10 years ago
favouriteCommand(socket, 'unvote', 'voted', '', data, callback);
};
SocketPosts.favourite = function(socket, data, callback) {
10 years ago
favouriteCommand(socket, 'favourite', 'favourited', 'notifications:favourited_your_post_in', data, callback);
};
SocketPosts.unfavourite = function(socket, data, callback) {
10 years ago
favouriteCommand(socket, 'unfavourite', 'favourited', '', data, callback);
};
10 years ago
function favouriteCommand(socket, command, eventName, notification, data, callback) {
if(!data || !data.pid || !data.room_id) {
10 years ago
return callback(new Error('[[error:invalid-data]]'));
}
async.parallel({
exists: function(next) {
posts.exists(data.pid, next);
},
deleted: function(next) {
posts.getPostField(data.pid, 'deleted', next);
}
}, function(err, results) {
if (err || !results.exists) {
10 years ago
return callback(err || new Error('[[error:invalid-pid]]'));
}
if (parseInt(results.deleted, 10) === 1) {
return callback(new Error('[[error:post-deleted]]'));
}
favourites[command](data.pid, socket.uid, function(err, result) {
if (err) {
return callback(err);
}
socket.emit('posts.' + command, result);
10 years ago
if (result && eventName) {
websockets.in(data.room_id).emit('event:' + eventName, result);
}
10 years ago
if (notification) {
SocketPosts.sendNotificationToPostOwner(data.pid, socket.uid, notification);
}
callback();
});
});
}
11 years ago
SocketPosts.sendNotificationToPostOwner = function(pid, fromuid, notification) {
10 years ago
if(!pid || !fromuid || !notification) {
11 years ago
return;
}
posts.getPostFields(pid, ['tid', 'uid', 'content'], function(err, postData) {
11 years ago
if (err) {
return;
}
if (!postData.uid || fromuid === parseInt(postData.uid, 10)) {
11 years ago
return;
}
11 years ago
11 years ago
async.parallel({
username: async.apply(user.getUserField, fromuid, 'username'),
topicTitle: async.apply(topics.getTopicField, postData.tid, 'title'),
postObj: async.apply(postTools.parsePost, postData, postData.uid)
11 years ago
}, function(err, results) {
if (err) {
11 years ago
return;
}
11 years ago
notifications.create({
bodyShort: '[[' + notification + ', ' + results.username + ', ' + results.topicTitle + ']]',
bodyLong: results.postObj.content,
11 years ago
pid: pid,
nid: 'post:' + pid + ':uid:' + fromuid,
11 years ago
from: fromuid
}, function(err, notification) {
if (!err && notification) {
notifications.push(notification, [postData.uid]);
11 years ago
}
});
});
11 years ago
});
};
11 years ago
11 years ago
SocketPosts.getRawPost = function(socket, pid, callback) {
11 years ago
async.waterfall([
function(next) {
11 years ago
privileges.posts.can('read', pid, socket.uid, next);
11 years ago
},
function(canRead, next) {
if (!canRead) {
11 years ago
return next(new Error('[[error:no-privileges]]'));
}
posts.getPostFields(pid, ['content', 'deleted'], next);
11 years ago
},
function(postData, next) {
if (parseInt(postData.deleted, 10) === 1) {
return next(new Error('[[error:no-post]]'));
}
next(null, postData.content);
11 years ago
}
11 years ago
], callback);
};
SocketPosts.edit = function(socket, data, callback) {
if(!socket.uid) {
return callback(new Error('[[error:not-logged-in]]'));
11 years ago
} else if(!data || !data.pid || !data.title || !data.content) {
return callback(new Error('[[error:invalid-data]]'));
} else if (!data.title || data.title.length < parseInt(meta.config.minimumTitleLength, 10)) {
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 + ']]'));
} else if (!data.content || data.content.length < parseInt(meta.config.minimumPostLength, 10)) {
return callback(new Error('[[error:content-too-short, ' + meta.config.minimumPostLength + ']]'));
}
// uid, pid, title, content, options
postTools.edit({
uid: socket.uid,
handle: data.handle,
pid: data.pid,
title: data.title,
content: data.content,
options: {
topic_thumb: data.topic_thumb,
tags: data.tags
}
}, function(err, results) {
if (err) {
11 years ago
return callback(err);
}
10 years ago
websockets.in('topic_' + results.topic.tid).emit('event:post_edited', {
11 years ago
pid: data.pid,
handle: data.handle,
11 years ago
title: results.topic.title,
isMainPost: results.topic.isMainPost,
11 years ago
tags: results.topic.tags,
11 years ago
content: results.content
});
callback();
});
};
SocketPosts.delete = function(socket, data, callback) {
deleteOrRestore('delete', socket, data, callback);
};
SocketPosts.restore = function(socket, data, callback) {
deleteOrRestore('restore', socket, data, callback);
};
function deleteOrRestore(command, socket, data, callback) {
10 years ago
if (!data) {
return callback(new Error('[[error:invalid-data]]'));
11 years ago
}
11 years ago
postTools[command](socket.uid, data.pid, function(err, postData) {
10 years ago
if (err) {
return callback(err);
}
var eventName = command === 'restore' ? 'event:post_restored' : 'event:post_deleted';
10 years ago
websockets.in('topic_' + data.tid).emit(eventName, postData);
11 years ago
callback();
});
}
11 years ago
SocketPosts.purge = function(socket, data, callback) {
if(!data || !parseInt(data.pid, 10)) {
11 years ago
return callback(new Error('[[error:invalid-data]]'));
}
postTools.purge(socket.uid, data.pid, function(err) {
if(err) {
return callback(err);
}
10 years ago
websockets.in('topic_' + data.tid).emit('event:post_purged', data.pid);
11 years ago
callback();
});
};
11 years ago
SocketPosts.getPrivileges = function(socket, pids, callback) {
privileges.posts.get(pids, socket.uid, function(err, privileges) {
if (err) {
return callback(err);
}
if (!Array.isArray(privileges) || !privileges.length) {
return callback(new Error('[[error:invalid-data]]'));
}
11 years ago
callback(null, privileges);
});
};
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
});
});
});
};
11 years ago
SocketPosts.flag = function(socket, pid, callback) {
if (!socket.uid) {
return callback(new Error('[[error:not-logged-in]]'));
}
11 years ago
var message = '',
userName = '',
11 years ago
post;
11 years ago
async.waterfall([
function(next) {
posts.flag(pid, next);
},
11 years ago
function(next) {
11 years ago
user.getUserFields(socket.uid, ['username', 'reputation'], next);
11 years ago
},
11 years ago
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]]'));
}
userName = userData.username;
posts.getPostFields(pid, ['tid', 'uid', 'content', 'deleted'], next);
11 years ago
},
11 years ago
function(postData, next) {
10 years ago
if (parseInt(postData.deleted, 10) === 1) {
return next(new Error('[[error:post-deleted]]'));
}
11 years ago
post = postData;
10 years ago
topics.getTopicFields(postData.tid, ['title', 'cid'], next);
},
10 years ago
function(topic, next) {
post.topic = topic;
message = '[[notifications:user_flagged_post_in, ' + userName + ', ' + topic.title + ']]';
postTools.parsePost(post, socket.uid, next);
},
function(post, next) {
10 years ago
async.parallel({
admins: function(next) {
groups.getMembers('administrators', next);
},
moderators: function(next) {
groups.getMembers('cid:' + post.topic.cid + ':privileges:mods', next);
}
}, next);
11 years ago
},
10 years ago
function(results, next) {
notifications.create({
bodyShort: message,
bodyLong: post.content,
11 years ago
pid: pid,
nid: 'post_flag:' + pid + ':uid:' + socket.uid,
from: socket.uid
}, function(err, notification) {
if (err || !notification) {
11 years ago
return next(err);
}
10 years ago
notifications.push(notification, results.admins.concat(results.moderators), next);
11 years ago
});
},
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);
}
if (count >= (meta.config.flagsForBan || 3) && parseInt(meta.config.flagsForBan, 10) !== 0) {
11 years ago
var adminUser = require('./admin/user');
adminUser.banUser(post.uid, next);
return;
}
next();
11 years ago
});
});
}
], callback);
11 years ago
};
11 years ago
SocketPosts.loadMoreFavourites = function(socket, data, callback) {
if(!data || !data.after) {
return callback(new Error('[[error:invalid-data]]'));
}
var start = parseInt(data.after, 10),
end = start + 9;
posts.getPostsFromSet('uid:' + socket.uid + ':favourites', socket.uid, start, end, callback);
};
SocketPosts.loadMoreUserPosts = function(socket, data, callback) {
10 years ago
if(!data || !data.uid || !utils.isNumber(data.after)) {
return callback(new Error('[[error:invalid-data]]'));
}
10 years ago
var start = Math.max(0, parseInt(data.after, 10)),
end = start + 9;
10 years ago
posts.getPostsFromSet('uid:' + data.uid + ':posts', socket.uid, start, end, callback);
};
SocketPosts.getRecentPosts = function(socket, data, callback) {
if(!data || !data.count) {
return callback(new Error('[[error:invalid-data]]'));
}
posts.getRecentPosts(socket.uid, 0, data.count - 1, data.term, callback);
};
11 years ago
SocketPosts.getCategory = function(socket, pid, callback) {
posts.getCidByPid(pid, callback);
11 years ago
};
SocketPosts.getPidIndex = function(socket, pid, callback) {
posts.getPidIndex(pid, socket.uid, callback);
};
module.exports = SocketPosts;