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-12-22 00:12:47 -05:00
|
|
|
winston = require('winston'),
|
2014-01-30 17:41:54 -05:00
|
|
|
|
2014-05-12 14:51:39 -04:00
|
|
|
db = require('../database'),
|
2014-01-30 17:41:54 -05:00
|
|
|
posts = require('../posts'),
|
2014-09-18 17:09:40 -04:00
|
|
|
plugins = require('../plugins'),
|
2014-05-14 17:53:23 -04:00
|
|
|
privileges = require('../privileges'),
|
2014-01-10 16:00:03 -05:00
|
|
|
meta = require('../meta'),
|
|
|
|
|
topics = require('../topics'),
|
|
|
|
|
favourites = require('../favourites'),
|
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'),
|
2014-04-09 20:55:49 -04:00
|
|
|
websockets = require('./index'),
|
2015-03-09 13:16:14 -04:00
|
|
|
socketTopics = require('./topics'),
|
2015-02-01 19:11:58 -05:00
|
|
|
events = require('../events'),
|
2014-11-17 23:57:31 -05:00
|
|
|
utils = require('../../public/src/utils'),
|
2014-01-10 16:00:03 -05:00
|
|
|
|
2014-04-27 00:47:08 -04:00
|
|
|
SocketPosts = {};
|
2014-04-26 03:00:56 -04:00
|
|
|
|
|
|
|
|
|
2014-01-16 15:34:43 -05:00
|
|
|
SocketPosts.reply = function(socket, data, callback) {
|
2014-02-22 18:56:37 -05:00
|
|
|
if(!data || !data.tid || !data.content) {
|
2014-04-09 14:12:46 -04:00
|
|
|
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;
|
2014-04-27 00:47:08 -04:00
|
|
|
data.req = websockets.reqFromSocket(socket);
|
2014-02-22 17:56:13 -05:00
|
|
|
|
|
|
|
|
topics.reply(data, function(err, postData) {
|
2014-09-14 14:19:36 -04:00
|
|
|
if (err) {
|
2014-01-16 22:06:23 -05:00
|
|
|
return callback(err);
|
2014-01-10 16:00:03 -05:00
|
|
|
}
|
|
|
|
|
|
2014-09-14 14:19:36 -04:00
|
|
|
var result = {
|
|
|
|
|
posts: [postData],
|
|
|
|
|
privileges: {
|
2014-05-22 18:24:58 -04:00
|
|
|
'topics:reply': true
|
2014-09-14 14:19:36 -04:00
|
|
|
},
|
|
|
|
|
'reputation:disabled': parseInt(meta.config['reputation:disabled'], 10) === 1,
|
|
|
|
|
'downvote:disabled': parseInt(meta.config['downvote:disabled'], 10) === 1,
|
|
|
|
|
};
|
|
|
|
|
|
2015-05-22 14:37:46 -04:00
|
|
|
callback(null, postData);
|
2014-09-14 14:19:36 -04:00
|
|
|
|
|
|
|
|
socket.emit('event:new_post', result);
|
|
|
|
|
|
2015-03-25 15:51:11 -04:00
|
|
|
user.updateOnlineUsers(socket.uid);
|
|
|
|
|
|
2015-01-31 15:42:10 -05:00
|
|
|
SocketPosts.notifyOnlineUsers(socket.uid, result);
|
2015-03-18 17:50:47 -04:00
|
|
|
|
|
|
|
|
if (data.lock) {
|
|
|
|
|
socketTopics.doTopicAction('lock', 'event:topic_locked', socket, {tids: [postData.topic.tid], cid: postData.topic.cid});
|
|
|
|
|
}
|
2015-01-31 15:42:10 -05:00
|
|
|
});
|
|
|
|
|
};
|
2014-09-18 17:09:40 -04:00
|
|
|
|
2015-01-31 15:42:10 -05:00
|
|
|
SocketPosts.notifyOnlineUsers = function(uid, result) {
|
|
|
|
|
var cid = result.posts[0].topic.cid;
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function(next) {
|
|
|
|
|
user.getUidsFromSet('users:online', 0, -1, next);
|
|
|
|
|
},
|
|
|
|
|
function(uids, next) {
|
|
|
|
|
privileges.categories.filterUids('read', cid, uids, next);
|
|
|
|
|
},
|
|
|
|
|
function(uids, next) {
|
|
|
|
|
plugins.fireHook('filter:sockets.sendNewPostToUids', {uidsTo: uids, uidFrom: uid, type: 'newPost'}, next);
|
|
|
|
|
}
|
|
|
|
|
], function(err, data) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return winston.error(err.stack);
|
|
|
|
|
}
|
2014-09-19 14:25:43 -04:00
|
|
|
|
2015-01-31 15:42:10 -05:00
|
|
|
var uids = data.uidsTo;
|
|
|
|
|
|
|
|
|
|
for(var i=0; i<uids.length; ++i) {
|
|
|
|
|
if (parseInt(uids[i], 10) !== uid) {
|
|
|
|
|
websockets.in('uid_' + uids[i]).emit('event:new_post', result);
|
2014-12-22 00:12:47 -05:00
|
|
|
}
|
2015-01-31 15:42:10 -05:00
|
|
|
}
|
2014-01-10 16:00:03 -05:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2014-12-02 14:59:29 -05:00
|
|
|
SocketPosts.getVoters = function(socket, data, callback) {
|
2014-12-07 16:32:40 -05:00
|
|
|
if (!data || !data.pid || !data.cid) {
|
|
|
|
|
return callback(new Error('[[error:invalid-data]]'));
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-02 14:59:29 -05:00
|
|
|
var pid = data.pid,
|
2014-12-07 16:32:40 -05:00
|
|
|
cid = data.cid;
|
2014-12-02 14:59:29 -05:00
|
|
|
|
2014-12-07 16:32:40 -05:00
|
|
|
async.parallel({
|
|
|
|
|
isAdmin: function(next) {
|
|
|
|
|
user.isAdministrator(socket.uid, next);
|
2014-12-02 14:59:29 -05:00
|
|
|
},
|
2014-12-07 16:32:40 -05:00
|
|
|
isModerator: function(next) {
|
|
|
|
|
user.isModerator(socket.uid, cid, next);
|
2014-12-02 14:59:29 -05:00
|
|
|
}
|
2014-12-07 16:32:40 -05:00
|
|
|
}, function(err, tests) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
2014-12-02 14:59:29 -05:00
|
|
|
}
|
2014-12-05 22:22:57 -05:00
|
|
|
|
2014-12-07 16:32:40 -05:00
|
|
|
if (tests.isAdmin || tests.isModerator) {
|
|
|
|
|
getVoters(pid, callback);
|
|
|
|
|
}
|
|
|
|
|
});
|
2014-12-02 14:59:29 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-09 20:55:49 -04:00
|
|
|
SocketPosts.upvote = function(socket, data, callback) {
|
2014-11-17 23:32:39 -05:00
|
|
|
favouriteCommand(socket, 'upvote', 'voted', 'notifications:upvoted_your_post_in', data, callback);
|
2014-02-06 12:44:21 -05:00
|
|
|
};
|
|
|
|
|
|
2014-04-09 20:55:49 -04:00
|
|
|
SocketPosts.downvote = function(socket, data, callback) {
|
2015-02-01 21:38:10 -05:00
|
|
|
favouriteCommand(socket, 'downvote', 'voted', '', data, callback);
|
2014-02-06 12:44:21 -05:00
|
|
|
};
|
|
|
|
|
|
2014-04-09 20:55:49 -04:00
|
|
|
SocketPosts.unvote = function(socket, data, callback) {
|
2014-11-17 23:32:39 -05:00
|
|
|
favouriteCommand(socket, 'unvote', 'voted', '', data, callback);
|
2014-02-06 12:44:21 -05:00
|
|
|
};
|
|
|
|
|
|
2014-04-09 20:55:49 -04:00
|
|
|
SocketPosts.favourite = function(socket, data, callback) {
|
2014-11-17 23:32:39 -05:00
|
|
|
favouriteCommand(socket, 'favourite', 'favourited', 'notifications:favourited_your_post_in', data, callback);
|
2014-01-10 16:00:03 -05:00
|
|
|
};
|
|
|
|
|
|
2014-04-09 20:55:49 -04:00
|
|
|
SocketPosts.unfavourite = function(socket, data, callback) {
|
2014-11-17 23:32:39 -05:00
|
|
|
favouriteCommand(socket, 'unfavourite', 'favourited', '', data, callback);
|
2014-03-11 21:52:22 -04:00
|
|
|
};
|
|
|
|
|
|
2014-11-17 23:32:39 -05:00
|
|
|
function favouriteCommand(socket, command, eventName, notification, data, callback) {
|
2014-10-31 22:27:50 -04:00
|
|
|
if(!data || !data.pid || !data.room_id) {
|
2014-11-17 23:32:39 -05:00
|
|
|
return callback(new Error('[[error:invalid-data]]'));
|
2014-10-31 22:27:50 -04:00
|
|
|
}
|
2014-11-04 15:19:30 -05:00
|
|
|
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) {
|
2014-11-17 23:32:39 -05:00
|
|
|
return callback(err || new Error('[[error:invalid-pid]]'));
|
2014-10-31 22:27:50 -04:00
|
|
|
}
|
|
|
|
|
|
2014-11-04 15:19:30 -05:00
|
|
|
if (parseInt(results.deleted, 10) === 1) {
|
|
|
|
|
return callback(new Error('[[error:post-deleted]]'));
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-18 13:38:42 +01:00
|
|
|
/*
|
|
|
|
|
hooks:
|
|
|
|
|
filter.post.upvote
|
|
|
|
|
filter.post.downvote
|
|
|
|
|
filter.post.unvote
|
|
|
|
|
filter.post.favourite
|
|
|
|
|
filter.post.unfavourite
|
|
|
|
|
*/
|
2015-01-18 19:09:26 +01:00
|
|
|
plugins.fireHook('filter:post.' + command, {data: data, uid: socket.uid}, function(err, filteredData) {
|
2014-04-09 20:55:49 -04:00
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-18 19:15:59 +01:00
|
|
|
executeFavouriteCommand(socket, command, eventName, notification, filteredData.data, callback);
|
2015-01-18 13:38:42 +01:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
2014-04-09 20:55:49 -04:00
|
|
|
|
2015-01-18 13:38:42 +01:00
|
|
|
function executeFavouriteCommand(socket, command, eventName, notification, data, callback) {
|
|
|
|
|
favourites[command](data.pid, socket.uid, function(err, result) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
2014-11-17 23:32:39 -05:00
|
|
|
|
2015-01-18 13:38:42 +01:00
|
|
|
socket.emit('posts.' + command, result);
|
|
|
|
|
|
|
|
|
|
if (result && eventName) {
|
|
|
|
|
websockets.in(data.room_id).emit('event:' + eventName, result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (notification) {
|
|
|
|
|
SocketPosts.sendNotificationToPostOwner(data.pid, socket.uid, notification);
|
|
|
|
|
}
|
|
|
|
|
callback();
|
2014-10-31 22:27:50 -04: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) {
|
2014-11-17 23:32:39 -05:00
|
|
|
if(!pid || !fromuid || !notification) {
|
2014-08-12 13:45:18 -04:00
|
|
|
return;
|
|
|
|
|
}
|
2014-09-12 18:05:09 -04:00
|
|
|
posts.getPostFields(pid, ['tid', 'uid', 'content'], function(err, postData) {
|
2014-08-12 13:45:18 -04:00
|
|
|
if (err) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-31 22:27:50 -04:00
|
|
|
if (!postData.uid || fromuid === parseInt(postData.uid, 10)) {
|
2014-08-12 13:45:18 -04:00
|
|
|
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'),
|
2014-09-12 18:05:09 -04:00
|
|
|
topicTitle: async.apply(topics.getTopicField, postData.tid, 'title'),
|
2015-04-22 11:10:42 -04:00
|
|
|
postObj: async.apply(posts.parsePost, postData)
|
2014-08-12 13:45:18 -04:00
|
|
|
}, function(err, results) {
|
|
|
|
|
if (err) {
|
2014-03-25 12:10:42 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-12 13:45:18 -04:00
|
|
|
notifications.create({
|
2014-09-12 18:05:09 -04:00
|
|
|
bodyShort: '[[' + notification + ', ' + results.username + ', ' + results.topicTitle + ']]',
|
2014-11-11 09:41:28 -05:00
|
|
|
bodyLong: results.postObj.content,
|
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
|
|
|
},
|
2014-05-14 17:53:23 -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) {
|
2015-04-07 20:46:20 -04:00
|
|
|
if (!socket.uid) {
|
2014-04-09 15:39:26 -04:00
|
|
|
return callback(new Error('[[error:not-logged-in]]'));
|
2015-04-07 20:46:20 -04:00
|
|
|
} else if (!data || !data.pid || !data.content) {
|
2014-04-09 15:39:26 -04:00
|
|
|
return callback(new Error('[[error:invalid-data]]'));
|
2015-04-07 20:46:20 -04: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 + ']]'));
|
2015-04-07 20:46:20 -04:00
|
|
|
} else if (data.title && data.title.length > parseInt(meta.config.maximumTitleLength, 10)) {
|
2014-08-07 20:03:56 -04:00
|
|
|
return callback(new Error('[[error:title-too-long, ' + meta.config.maximumTitleLength + ']]'));
|
2015-07-18 02:24:33 -04:00
|
|
|
} else if (data.tags && data.tags.length < parseInt(meta.config.minimumTagsPerTopic, 10)) {
|
2015-06-23 10:23:56 -04:00
|
|
|
return callback(new Error('[[error:not-enough-tags, ' + meta.config.minimumTagsPerTopic + ']]'));
|
2015-07-18 14:03:27 -04:00
|
|
|
} else if (data.tags && data.tags.length > parseInt(meta.config.maximumTagsPerTopic, 10)) {
|
|
|
|
|
return callback(new Error('[[error:too-many-tags, ' + meta.config.maximumTagsPerTopic + ']]'));
|
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 + ']]'));
|
2015-02-03 16:13:09 -05:00
|
|
|
} else if (data.content.length > parseInt(meta.config.maximumPostLength, 10)) {
|
|
|
|
|
return callback(new Error('[[error:content-too-long, ' + meta.config.maximumPostLength + ']]'));
|
2014-01-10 16:00:03 -05:00
|
|
|
}
|
|
|
|
|
|
2015-09-14 17:00:41 -04:00
|
|
|
posts.edit({
|
2014-12-31 16:27:35 -05:00
|
|
|
uid: socket.uid,
|
2014-12-31 21:27:41 -05:00
|
|
|
handle: data.handle,
|
2014-12-31 16:27:35 -05:00
|
|
|
pid: data.pid,
|
|
|
|
|
title: data.title,
|
|
|
|
|
content: data.content,
|
2015-04-20 17:56:43 -04:00
|
|
|
topic_thumb: data.topic_thumb,
|
|
|
|
|
tags: data.tags
|
|
|
|
|
}, function(err, result) {
|
2014-11-05 18:59:20 -05:00
|
|
|
if (err) {
|
2014-03-01 19:15:18 -05:00
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:15:33 -04:00
|
|
|
if (result.topic.renamed) {
|
|
|
|
|
events.log({
|
|
|
|
|
type: 'topic-rename',
|
|
|
|
|
uid: socket.uid,
|
|
|
|
|
ip: socket.ip,
|
|
|
|
|
oldTitle: result.topic.oldTitle,
|
|
|
|
|
newTitle: result.topic.title
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-20 17:56:43 -04:00
|
|
|
if (parseInt(result.post.deleted) !== 1) {
|
|
|
|
|
websockets.in('topic_' + result.topic.tid).emit('event:post_edited', result);
|
2015-05-22 14:37:46 -04:00
|
|
|
return callback(null, result.post);
|
2015-04-09 14:53:59 -04:00
|
|
|
}
|
2014-03-01 19:15:18 -05:00
|
|
|
|
2015-04-09 14:53:59 -04:00
|
|
|
socket.emit('event:post_edited', result);
|
2015-05-22 14:37:46 -04:00
|
|
|
callback(null, result.post);
|
2015-04-09 14:53:59 -04:00
|
|
|
|
|
|
|
|
async.parallel({
|
|
|
|
|
admins: async.apply(groups.getMembers, 'administrators', 0, -1),
|
2015-04-20 17:56:43 -04:00
|
|
|
moderators: async.apply(groups.getMembers, 'cid:' + result.topic.cid + ':privileges:mods', 0, -1),
|
|
|
|
|
uidsInTopic: async.apply(websockets.getUidsInRoom, 'topic_' + result.topic.tid)
|
2015-04-09 14:53:59 -04:00
|
|
|
}, function(err, results) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return winston.error(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var uids = results.uidsInTopic.filter(function(uid) {
|
|
|
|
|
return results.admins.indexOf(uid) !== -1 || results.moderators.indexOf(uid) !== -1;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
uids.forEach(function(uid) {
|
|
|
|
|
websockets.in('uid_' + uid).emit('event:post_edited', result);
|
|
|
|
|
});
|
|
|
|
|
});
|
2014-03-01 19:15:18 -05:00
|
|
|
});
|
2014-01-10 16:00:03 -05:00
|
|
|
};
|
|
|
|
|
|
2014-01-16 15:34:43 -05:00
|
|
|
SocketPosts.delete = function(socket, data, callback) {
|
2015-09-17 14:58:58 -04:00
|
|
|
doPostAction('delete', 'event:post_deleted', 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) {
|
2015-09-17 14:58:58 -04:00
|
|
|
doPostAction('restore', 'event:post_restored', socket, data, callback);
|
2014-03-11 21:52:22 -04:00
|
|
|
};
|
|
|
|
|
|
2015-09-17 14:58:58 -04:00
|
|
|
function doPostAction(command, eventName, socket, data, callback) {
|
2014-11-19 23:37:55 -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
|
|
|
}
|
|
|
|
|
|
2015-09-17 14:58:58 -04:00
|
|
|
posts.tools[command](socket.uid, data.pid, function(err, postData) {
|
2014-11-19 23:37:55 -05:00
|
|
|
if (err) {
|
2014-01-10 16:00:03 -05:00
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-28 19:40:47 -05:00
|
|
|
websockets.in('topic_' + data.tid).emit(eventName, postData);
|
2014-01-16 22:06:23 -05:00
|
|
|
|
2015-02-01 19:11:58 -05:00
|
|
|
events.log({
|
2015-09-17 14:58:58 -04:00
|
|
|
type: 'post-' + command,
|
2015-02-01 19:11:58 -05:00
|
|
|
uid: socket.uid,
|
|
|
|
|
pid: data.pid,
|
|
|
|
|
ip: socket.ip
|
|
|
|
|
});
|
|
|
|
|
|
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) {
|
2015-03-09 13:16:14 -04:00
|
|
|
function purgePost() {
|
2015-09-17 14:58:58 -04:00
|
|
|
posts.tools.purge(socket.uid, data.pid, function(err) {
|
2015-03-09 13:16:14 -04:00
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
websockets.in('topic_' + data.tid).emit('event:post_purged', data.pid);
|
|
|
|
|
|
|
|
|
|
events.log({
|
|
|
|
|
type: 'post-purge',
|
|
|
|
|
uid: socket.uid,
|
|
|
|
|
pid: data.pid,
|
|
|
|
|
ip: socket.ip
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
callback();
|
2015-03-18 17:50:47 -04:00
|
|
|
});
|
2015-03-09 13:16:14 -04:00
|
|
|
}
|
2015-03-18 17:50:47 -04:00
|
|
|
|
2015-03-09 13:16:14 -04:00
|
|
|
if (!data || !parseInt(data.pid, 10)) {
|
2014-06-10 14:24:50 -04:00
|
|
|
return callback(new Error('[[error:invalid-data]]'));
|
|
|
|
|
}
|
2015-03-09 13:16:14 -04:00
|
|
|
|
|
|
|
|
isMainAndLastPost(data.pid, function(err, results) {
|
|
|
|
|
if (err) {
|
2014-06-10 14:24:50 -04:00
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-09 13:16:14 -04:00
|
|
|
if (!results.isMain) {
|
|
|
|
|
return purgePost();
|
|
|
|
|
}
|
2014-06-10 14:24:50 -04:00
|
|
|
|
2015-03-09 13:16:14 -04:00
|
|
|
if (!results.isLast) {
|
|
|
|
|
return callback(new Error('[[error:cant-purge-main-post]]'));
|
|
|
|
|
}
|
2015-02-01 19:11:58 -05:00
|
|
|
|
2015-03-09 13:16:14 -04:00
|
|
|
posts.getTopicFields(data.pid, ['tid', 'cid'], function(err, topic) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
socketTopics.doTopicAction('delete', 'event:topic_deleted', socket, {tids: [topic.tid], cid: topic.cid}, callback);
|
|
|
|
|
});
|
2014-06-10 14:24:50 -04:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2015-03-09 13:16:14 -04:00
|
|
|
function isMainAndLastPost(pid, callback) {
|
|
|
|
|
async.parallel({
|
|
|
|
|
isMain: function(next) {
|
|
|
|
|
posts.isMain(pid, next);
|
|
|
|
|
},
|
|
|
|
|
isLast: function(next) {
|
|
|
|
|
posts.getTopicFields(pid, ['postcount'], function(err, topic) {
|
|
|
|
|
next(err, topic ? parseInt(topic.postcount, 10) === 1 : false);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}, callback);
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-02 13:55:01 -04:00
|
|
|
SocketPosts.getPrivileges = function(socket, pids, callback) {
|
|
|
|
|
privileges.posts.get(pids, socket.uid, function(err, privileges) {
|
2014-06-28 14:59:01 -04:00
|
|
|
if (err) {
|
2014-04-02 13:58:10 -04:00
|
|
|
return callback(err);
|
|
|
|
|
}
|
2014-06-28 14:59:01 -04:00
|
|
|
if (!Array.isArray(privileges) || !privileges.length) {
|
|
|
|
|
return callback(new Error('[[error:invalid-data]]'));
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-02 13:55:01 -04:00
|
|
|
callback(null, privileges);
|
2014-04-02 13:58:10 -04:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2015-01-19 17:06:43 -05:00
|
|
|
SocketPosts.getUpvoters = function(socket, pids, callback) {
|
|
|
|
|
if (!Array.isArray(pids)) {
|
|
|
|
|
return callback(new Error('[[error:invalid-data]]'));
|
|
|
|
|
}
|
|
|
|
|
favourites.getUpvotedUidsByPids(pids, function(err, data) {
|
2014-08-13 13:35:55 -04:00
|
|
|
if (err || !Array.isArray(data) || !data.length) {
|
|
|
|
|
return callback(err, []);
|
|
|
|
|
}
|
2015-01-24 13:16:07 -05:00
|
|
|
|
|
|
|
|
async.map(data, function(uids, next) {
|
|
|
|
|
var otherCount = 0;
|
|
|
|
|
if (uids.length > 6) {
|
|
|
|
|
otherCount = uids.length - 5;
|
|
|
|
|
uids = uids.slice(0, 5);
|
|
|
|
|
}
|
|
|
|
|
user.getUsernamesByUids(uids, function(err, usernames) {
|
|
|
|
|
next(err, {
|
|
|
|
|
otherCount: otherCount,
|
|
|
|
|
usernames: usernames
|
|
|
|
|
});
|
2014-09-06 20:46:18 -04:00
|
|
|
});
|
2015-01-24 13:16:07 -05:00
|
|
|
}, callback);
|
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 = '',
|
2015-06-19 17:10:18 -04:00
|
|
|
flaggingUser = {},
|
2014-05-12 14:51:39 -04:00
|
|
|
post;
|
2014-01-30 17:41:54 -05:00
|
|
|
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function(next) {
|
2015-06-19 17:22:14 -04:00
|
|
|
posts.getPostFields(pid, ['pid', 'tid', 'uid', 'content', 'deleted'], function(err, postData) {
|
|
|
|
|
if (parseInt(postData.deleted, 10) === 1) {
|
|
|
|
|
return next(new Error('[[error:post-deleted]]'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
post = postData;
|
|
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
function(next) {
|
|
|
|
|
topics.getTopicFields(post.tid, ['title', 'cid'], function(err, topicData) {
|
|
|
|
|
post.topic = topicData;
|
|
|
|
|
next();
|
|
|
|
|
});
|
2014-01-30 17:41:54 -05:00
|
|
|
},
|
2015-06-19 17:22:14 -04:00
|
|
|
function(next) {
|
|
|
|
|
async.parallel({
|
|
|
|
|
isAdmin: function(next) {
|
|
|
|
|
user.isAdministrator(socket.uid, next);
|
|
|
|
|
},
|
|
|
|
|
isModerator: function(next) {
|
|
|
|
|
user.isModerator(socket.uid, post.topic.cid, next);
|
|
|
|
|
},
|
|
|
|
|
userData: function(next) {
|
|
|
|
|
user.getUserFields(socket.uid, ['username', 'reputation'], next);
|
|
|
|
|
}
|
|
|
|
|
}, next);
|
|
|
|
|
},
|
|
|
|
|
function(user, next) {
|
|
|
|
|
if (!user.isAdmin && !user.isModerator && parseInt(user.userData.reputation, 10) < parseInt(meta.config['privileges:flag'] || 1, 10)) {
|
2014-08-30 15:39:20 -04:00
|
|
|
return next(new Error('[[error:not-enough-reputation-to-flag]]'));
|
|
|
|
|
}
|
2015-06-19 17:22:14 -04:00
|
|
|
|
|
|
|
|
flaggingUser = user.userData;
|
2015-06-19 17:10:18 -04:00
|
|
|
flaggingUser.uid = socket.uid;
|
|
|
|
|
|
2015-06-19 17:22:14 -04:00
|
|
|
next();
|
2014-01-30 17:41:54 -05:00
|
|
|
},
|
2015-06-19 17:22:14 -04:00
|
|
|
function(next) {
|
2015-02-19 15:56:04 -05:00
|
|
|
posts.flag(post, socket.uid, next);
|
2015-02-04 18:16:10 -05:00
|
|
|
},
|
|
|
|
|
function(next) {
|
2015-06-19 17:22:14 -04:00
|
|
|
message = '[[notifications:user_flagged_post_in, ' + flaggingUser.username + ', ' + post.topic.title + ']]';
|
2015-04-20 17:56:43 -04:00
|
|
|
posts.parsePost(post, next);
|
2014-09-12 18:05:09 -04:00
|
|
|
},
|
2014-11-05 18:59:20 -05:00
|
|
|
function(post, next) {
|
2014-12-16 15:09:15 -05:00
|
|
|
async.parallel({
|
|
|
|
|
admins: function(next) {
|
2015-01-29 13:37:45 -05:00
|
|
|
groups.getMembers('administrators', 0, -1, next);
|
2014-12-16 15:09:15 -05:00
|
|
|
},
|
|
|
|
|
moderators: function(next) {
|
2015-01-29 13:37:45 -05:00
|
|
|
groups.getMembers('cid:' + post.topic.cid + ':privileges:mods', 0, -1, next);
|
2014-12-16 15:09:15 -05:00
|
|
|
}
|
|
|
|
|
}, next);
|
2014-01-30 17:41:54 -05:00
|
|
|
},
|
2014-12-16 15:09:15 -05:00
|
|
|
function(results, next) {
|
2014-02-15 14:52:59 -05:00
|
|
|
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,
|
2014-02-15 15:43:44 -05:00
|
|
|
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);
|
|
|
|
|
}
|
2015-06-19 17:10:18 -04:00
|
|
|
|
2015-06-19 17:30:41 -04:00
|
|
|
plugins.fireHook('action:post.flag', {post: post, flaggingUser: flaggingUser});
|
2014-12-16 15:09:15 -05:00
|
|
|
notifications.push(notification, results.admins.concat(results.moderators), next);
|
2014-05-12 14:51:39 -04:00
|
|
|
});
|
2015-03-18 17:50:47 -04:00
|
|
|
}
|
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
|
|
|
|
2014-02-03 19:24:27 -05:00
|
|
|
SocketPosts.loadMoreFavourites = function(socket, data, callback) {
|
2015-03-31 23:40:58 -04:00
|
|
|
loadMorePosts('uid:' + data.uid + ':favourites', socket.uid, data, callback);
|
2014-02-03 19:24:27 -05:00
|
|
|
};
|
|
|
|
|
|
2014-02-04 17:31:05 -05:00
|
|
|
SocketPosts.loadMoreUserPosts = function(socket, data, callback) {
|
2015-03-31 23:40:58 -04:00
|
|
|
loadMorePosts('uid:' + data.uid + ':posts', socket.uid, data, callback);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function loadMorePosts(set, uid, data, callback) {
|
|
|
|
|
if (!data || !utils.isNumber(data.uid) || !utils.isNumber(data.after)) {
|
2014-04-09 21:40:39 -04:00
|
|
|
return callback(new Error('[[error:invalid-data]]'));
|
2014-02-04 17:31:05 -05:00
|
|
|
}
|
|
|
|
|
|
2014-11-17 23:57:31 -05:00
|
|
|
var start = Math.max(0, parseInt(data.after, 10)),
|
2015-03-31 23:40:58 -04:00
|
|
|
stop = start + 9;
|
2014-02-04 17:31:05 -05:00
|
|
|
|
2015-05-11 15:43:57 -04:00
|
|
|
posts.getPostSummariesFromSet(set, uid, start, stop, callback);
|
2015-03-31 23:40:58 -04:00
|
|
|
}
|
2014-03-11 21:52:22 -04:00
|
|
|
|
2014-03-11 23:31:23 -04:00
|
|
|
SocketPosts.getRecentPosts = function(socket, data, callback) {
|
2015-09-14 21:04:56 -04:00
|
|
|
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
|
|
|
|
2015-09-14 21:04:56 -04:00
|
|
|
SocketPosts.getPidIndex = function(socket, data, callback) {
|
|
|
|
|
if (!data) {
|
|
|
|
|
return callback(new Error('[[error:invalid-data]]'));
|
|
|
|
|
}
|
|
|
|
|
posts.getPidIndex(data.pid, data.tid, data.topicPostSort, callback);
|
2014-08-27 15:03:36 -04:00
|
|
|
};
|
|
|
|
|
|
2014-04-10 20:31:57 +01:00
|
|
|
module.exports = SocketPosts;
|