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.

291 lines
8.5 KiB
JavaScript

'use strict';
var async = require('async');
8 years ago
var _ = require('lodash');
var meta = require('../meta');
var posts = require('../posts');
var topics = require('../topics');
var user = require('../user');
var helpers = require('./helpers');
var plugins = require('../plugins');
8 years ago
var utils = require('../utils');
module.exports = function (privileges) {
privileges.posts = {};
privileges.posts.get = function (pids, uid, callback) {
11 years ago
if (!Array.isArray(pids) || !pids.length) {
return callback(null, []);
}
9 years ago
async.waterfall([
function (next) {
9 years ago
posts.getCidsByPids(pids, next);
10 years ago
},
function (cids, next) {
9 years ago
async.parallel({
isAdmin: async.apply(user.isAdministrator, uid),
isModerator: async.apply(user.isModerator, uid, cids),
9 years ago
isOwner: async.apply(posts.isOwner, pids, uid),
'topics:read': async.apply(helpers.isUserAllowedTo, 'topics:read', uid, cids),
read: async.apply(helpers.isUserAllowedTo, 'read', uid, cids),
'posts:edit': async.apply(helpers.isUserAllowedTo, 'posts:edit', uid, cids),
7 years ago
'posts:view_deleted': async.apply(helpers.isUserAllowedTo, 'posts:view_deleted', uid, cids),
9 years ago
}, next);
},
function (results, next) {
var privileges = pids.map(function (pid, i) {
var isAdminOrMod = results.isAdmin || results.isModerator[i];
var editable = isAdminOrMod || (results.isOwner[i] && results['posts:edit'][i]);
7 years ago
var viewDeletedPosts = isAdminOrMod || (results.isOwner[i] && results['posts:view_deleted'][i]);
return {
editable: editable,
view_deleted: editable,
move: isAdminOrMod,
isAdminOrMod: isAdminOrMod,
'topics:read': results['topics:read'][i] || isAdminOrMod,
read: results.read[i] || isAdminOrMod,
7 years ago
'posts:view_deleted': viewDeletedPosts,
};
10 years ago
});
next(null, privileges);
},
], callback);
};
privileges.posts.can = function (privilege, pid, uid, callback) {
async.waterfall([
function (next) {
posts.getCidByPid(pid, next);
},
function (cid, next) {
privileges.categories.can(privilege, cid, uid, next);
},
], callback);
};
privileges.posts.filter = function (privilege, pids, uid, callback) {
if (!Array.isArray(pids) || !pids.length) {
return callback(null, []);
}
9 years ago
var cids;
var postData;
var tids;
var tidToTopic = {};
8 years ago
pids = _.uniq(pids);
9 years ago
async.waterfall([
function (next) {
posts.getPostsFields(pids, ['uid', 'tid', 'deleted'], next);
},
function (_posts, next) {
postData = _posts;
8 years ago
tids = _.uniq(_posts.map(function (post) {
9 years ago
return post && post.tid;
8 years ago
}).filter(Boolean));
9 years ago
topics.getTopicsFields(tids, ['deleted', 'cid'], next);
},
function (topicData, next) {
topicData.forEach(function (topic, index) {
9 years ago
if (topic) {
tidToTopic[tids[index]] = topic;
}
});
cids = postData.map(function (post, index) {
9 years ago
if (post) {
post.pid = pids[index];
post.topic = tidToTopic[post.tid];
}
return tidToTopic[post.tid] && tidToTopic[post.tid].cid;
}).filter(function (cid, index, array) {
9 years ago
return cid && array.indexOf(cid) === index;
});
9 years ago
privileges.categories.getBase(privilege, cids, uid, next);
},
function (results, next) {
var isModOf = {};
cids = cids.filter(function (cid, index) {
9 years ago
isModOf[cid] = results.isModerators[index];
return !results.categories[index].disabled &&
(results.allowedTo[index] || results.isAdmin || results.isModerators[index]);
});
pids = postData.filter(function (post) {
return post.topic && cids.indexOf(post.topic.cid) !== -1 &&
9 years ago
((parseInt(post.topic.deleted, 10) !== 1 && parseInt(post.deleted, 10) !== 1) || results.isAdmin || isModOf[post.cid]);
}).map(function (post) {
return post.pid;
});
plugins.fireHook('filter:privileges.posts.filter', {
privilege: privilege,
uid: uid,
pids: pids,
}, function (err, data) {
9 years ago
next(err, data ? data.pids : null);
});
},
9 years ago
], callback);
};
privileges.posts.canEdit = function (pid, uid, callback) {
async.waterfall([
function (next) {
async.parallel({
isEditable: async.apply(isPostEditable, pid, uid),
isAdminOrMod: async.apply(isAdminOrMod, pid, uid),
}, next);
},
function (results, next) {
if (results.isAdminOrMod) {
return next(null, { flag: true });
}
9 years ago
next(null, results.isEditable);
},
], callback);
};
privileges.posts.canDelete = function (pid, uid, callback) {
9 years ago
var postData;
async.waterfall([
function (next) {
8 years ago
posts.getPostFields(pid, ['uid', 'tid', 'timestamp', 'deleterUid'], next);
9 years ago
},
function (_postData, next) {
9 years ago
postData = _postData;
async.parallel({
isAdminOrMod: async.apply(isAdminOrMod, pid, uid),
isLocked: async.apply(topics.isLocked, postData.tid),
isOwner: async.apply(posts.isOwner, pid, uid),
'posts:delete': async.apply(privileges.posts.can, 'posts:delete', pid, uid),
9 years ago
}, next);
},
function (results, next) {
if (results.isAdminOrMod) {
return next(null, { flag: true });
}
9 years ago
if (results.isLocked) {
return next(null, { flag: false, message: '[[error:topic-locked]]' });
}
9 years ago
if (!results['posts:delete']) {
return next(null, { flag: false, message: '[[error:no-privileges]]' });
}
9 years ago
var postDeleteDuration = parseInt(meta.config.postDeleteDuration, 10);
if (postDeleteDuration && (Date.now() - parseInt(postData.timestamp, 10) > postDeleteDuration * 1000)) {
return next(null, { flag: false, message: '[[error:post-delete-duration-expired, ' + meta.config.postDeleteDuration + ']]' });
}
var deleterUid = parseInt(postData.deleterUid, 10) || 0;
var flag = results.isOwner && (deleterUid === 0 || deleterUid === parseInt(postData.uid, 10));
next(null, { flag: flag, message: '[[error:no-privileges]]' });
},
], callback);
9 years ago
};
8 years ago
privileges.posts.canFlag = function (pid, uid, callback) {
async.waterfall([
function (next) {
async.parallel({
userReputation: async.apply(user.getUserField, uid, 'reputation'),
isAdminOrMod: async.apply(isAdminOrMod, pid, uid),
}, next);
},
function (results, next) {
var minimumReputation = utils.isNumber(meta.config['min:rep:flag']) ? parseInt(meta.config['min:rep:flag'], 10) : 0;
8 years ago
var canFlag = results.isAdminOrMod || parseInt(results.userReputation, 10) >= minimumReputation;
next(null, { flag: canFlag });
},
], callback);
};
privileges.posts.canMove = function (pid, uid, callback) {
async.waterfall([
function (next) {
posts.isMain(pid, next);
},
function (isMain, next) {
if (isMain) {
return next(new Error('[[error:cant-move-mainpost]]'));
}
isAdminOrMod(pid, uid, next);
},
], callback);
11 years ago
};
privileges.posts.canPurge = function (pid, uid, callback) {
10 years ago
async.waterfall([
function (next) {
posts.getCidByPid(pid, next);
},
function (cid, next) {
async.parallel({
purge: async.apply(privileges.categories.isUserAllowedTo, 'purge', cid, uid),
owner: async.apply(posts.isOwner, pid, uid),
isAdminOrMod: async.apply(privileges.categories.isAdminOrMod, cid, uid),
10 years ago
}, next);
},
function (results, next) {
next(null, results.isAdminOrMod || (results.purge && results.owner));
},
10 years ago
], callback);
};
10 years ago
function isPostEditable(pid, uid, callback) {
async.waterfall([
function (next) {
10 years ago
posts.getPostFields(pid, ['tid', 'timestamp'], next);
},
function (postData, next) {
10 years ago
var postEditDuration = parseInt(meta.config.postEditDuration, 10);
if (postEditDuration && Date.now() - parseInt(postData.timestamp, 10) > postEditDuration * 1000) {
return callback(null, { flag: false, message: '[[error:post-edit-duration-expired, ' + meta.config.postEditDuration + ']]' });
10 years ago
}
topics.isLocked(postData.tid, next);
},
function (isLocked, next) {
10 years ago
if (isLocked) {
return callback(null, { flag: false, message: '[[error:topic-locked]]' });
10 years ago
}
10 years ago
async.parallel({
owner: async.apply(posts.isOwner, pid, uid),
edit: async.apply(privileges.posts.can, 'posts:edit', pid, uid),
}, next);
10 years ago
},
function (result, next) {
next(null, { flag: result.owner && result.edit, message: '[[error:no-privileges]]' });
},
10 years ago
], callback);
}
11 years ago
function isAdminOrMod(pid, uid, callback) {
helpers.some([
function (next) {
async.waterfall([
function (next) {
posts.getCidByPid(pid, next);
},
function (cid, next) {
user.isModerator(uid, cid, next);
},
], next);
},
function (next) {
user.isAdministrator(uid, next);
},
], callback);
11 years ago
}
8 years ago
};