2 new hooks

v1.18.x
barisusakli 10 years ago
parent 9d562a8708
commit 5445e32522

@ -3,6 +3,7 @@
var async = require('async'), var async = require('async'),
db = require('../database'), db = require('../database'),
privileges = require('../privileges'), privileges = require('../privileges'),
plugins = require('../plugins'),
utils = require('../../public/src/utils'); utils = require('../../public/src/utils');
module.exports = function(Categories) { module.exports = function(Categories) {
@ -37,14 +38,19 @@ module.exports = function(Categories) {
imageClass: 'auto' imageClass: 'auto'
}; };
plugins.fireHook('filter:category.create', {category: category}, next);
},
function(data, next) {
category = data.category;
var defaultPrivileges = ['find', 'read', 'topics:create', 'topics:reply']; var defaultPrivileges = ['find', 'read', 'topics:create', 'topics:reply'];
async.series([ async.series([
async.apply(db.setObject, 'category:' + cid, category), async.apply(db.setObject, 'category:' + category.cid, category),
async.apply(db.sortedSetAdd, 'categories:cid', order, cid), async.apply(db.sortedSetAdd, 'categories:cid', category.order, category.cid),
async.apply(privileges.categories.give, defaultPrivileges, cid, 'administrators'), async.apply(privileges.categories.give, defaultPrivileges, category.cid, 'administrators'),
async.apply(privileges.categories.give, defaultPrivileges, cid, 'registered-users'), async.apply(privileges.categories.give, defaultPrivileges, category.cid, 'registered-users'),
async.apply(privileges.categories.give, ['find', 'read'], cid, 'guests') async.apply(privileges.categories.give, ['find', 'read'], category.cid, 'guests')
], next); ], next);
}, },
function(results, next) { function(results, next) {

@ -16,113 +16,100 @@ module.exports = function(Topics) {
Topics.create = function(data, callback) { Topics.create = function(data, callback) {
// This is an interal method, consider using Topics.post instead // This is an interal method, consider using Topics.post instead
var uid = data.uid, var timestamp = data.timestamp || Date.now();
title = data.title, var topicData;
cid = data.cid,
tags = data.tags;
db.incrObjectField('global', 'nextTid', function(err, tid) {
if (err) {
return callback(err);
}
var slug = utils.slugify(title),
timestamp = data.timestamp || Date.now();
if (!slug.length) { async.waterfall([
return callback(new Error('[[error:invalid-title]]')); function(next) {
} db.incrObjectField('global', 'nextTid', next);
},
function(tid, next) {
var slug = utils.slugify(data.title);
slug = tid + '/' + slug; if (!slug.length) {
return callback(new Error('[[error:invalid-title]]'));
var topicData = { }
'tid': tid,
'uid': uid,
'cid': cid,
'mainPid': 0,
'title': title,
'slug': slug,
'timestamp': timestamp,
'lastposttime': 0,
'postcount': 0,
'viewcount': 0,
'locked': 0,
'deleted': 0,
'pinned': 0
};
if (data.thumb) {
topicData.thumb = data.thumb;
}
db.setObject('topic:' + tid, topicData, function(err) { slug = tid + '/' + slug;
if (err) {
return callback(err); topicData = {
'tid': tid,
'uid': data.uid,
'cid': data.cid,
'mainPid': 0,
'title': data.title,
'slug': slug,
'timestamp': timestamp,
'lastposttime': 0,
'postcount': 0,
'viewcount': 0,
'locked': 0,
'deleted': 0,
'pinned': 0
};
if (data.thumb) {
topicData.thumb = data.thumb;
} }
plugins.fireHook('filter:topic.create', {topic: topicData}, next);
},
function(data, next) {
topicData = data.topic;
db.setObject('topic:' + topicData.tid, topicData, next);
},
function(next) {
async.parallel([ async.parallel([
function(next) { function(next) {
db.sortedSetsAdd([ db.sortedSetsAdd([
'topics:tid', 'topics:tid',
'cid:' + cid + ':tids', 'cid:' + topicData.cid + ':tids',
'cid:' + cid + ':uid:' + uid + ':tids' 'cid:' + topicData.cid + ':uid:' + topicData.uid + ':tids'
], timestamp, tid, next); ], timestamp, topicData.tid, next);
}, },
function(next) { function(next) {
user.addTopicIdToUser(uid, tid, timestamp, next); user.addTopicIdToUser(topicData.uid, topicData.tid, timestamp, next);
}, },
function(next) { function(next) {
db.incrObjectField('category:' + cid, 'topic_count', next); db.incrObjectField('category:' + topicData.cid, 'topic_count', next);
}, },
function(next) { function(next) {
db.incrObjectField('global', 'topicCount', next); db.incrObjectField('global', 'topicCount', next);
}, },
function(next) { function(next) {
Topics.createTags(tags, tid, timestamp, next); Topics.createTags(data.tags, topicData.tid, timestamp, next);
}
], function(err) {
if (err) {
return callback(err);
} }
plugins.fireHook('action:topic.save', topicData); ], next);
callback(null, tid); },
}); function(results, next) {
}); plugins.fireHook('action:topic.save', topicData);
}); next(null, topicData.tid);
}
], callback);
}; };
Topics.post = function(data, callback) { Topics.post = function(data, callback) {
var uid = data.uid, var uid = data.uid;
title = data.title, var title = data.title ? data.title.trim() : data.title;
content = data.content,
cid = data.cid,
tags = data.tags;
if (title) {
title = title.trim();
}
if (!title || title.length < parseInt(meta.config.minimumTitleLength, 10)) {
return callback(new Error('[[error:title-too-short, ' + meta.config.minimumTitleLength + ']]'));
} else if (title.length > parseInt(meta.config.maximumTitleLength, 10)) {
return callback(new Error('[[error:title-too-long, ' + meta.config.maximumTitleLength + ']]'));
}
async.waterfall([ async.waterfall([
function(next) { function(next) {
checkContentLength(content, next); checkTitleLength(title, next);
},
function(next) {
checkContentLength(data.content, next);
}, },
function(next) { function(next) {
categories.exists(cid, next); categories.exists(data.cid, next);
}, },
function(categoryExists, next) { function(categoryExists, next) {
if (!categoryExists) { if (!categoryExists) {
return next(new Error('[[error:no-category]]')); return next(new Error('[[error:no-category]]'));
} }
privileges.categories.can('topics:create', cid, uid, next); privileges.categories.can('topics:create', data.cid, data.uid, next);
}, },
function(canCreate, next) { function(canCreate, next) {
if(!canCreate) { if (!canCreate) {
return next(new Error('[[error:no-privileges]]')); return next(new Error('[[error:no-privileges]]'));
} }
@ -130,17 +117,17 @@ module.exports = function(Topics) {
return next(new Error('[[error:guest-handle-invalid]]')); return next(new Error('[[error:guest-handle-invalid]]'));
} }
user.isReadyToPost(uid, next); user.isReadyToPost(data.uid, next);
}, },
function(next) { function(next) {
plugins.fireHook('filter:topic.post', data, next); plugins.fireHook('filter:topic.post', data, next);
}, },
function(filteredData, next) { function(filteredData, next) {
content = filteredData.content || data.content; data = filteredData;
Topics.create({ uid: uid, title: title, cid: cid, thumb: data.thumb, tags: tags, timestamp: data.timestamp }, next); Topics.create({uid: data.uid, title: data.title, cid: data.cid, thumb: data.thumb, tags: data.tags, timestamp: data.timestamp}, next);
}, },
function(tid, next) { function(tid, next) {
Topics.reply({ uid:uid, tid:tid, handle: data.handle, content:content, timestamp: data.timestamp, req: data.req }, next); Topics.reply({uid: data.uid, tid: tid, handle: data.handle, content: data.content, timestamp: data.timestamp, req: data.req}, next);
}, },
function(postData, next) { function(postData, next) {
async.parallel({ async.parallel({
@ -165,7 +152,7 @@ module.exports = function(Topics) {
}, next); }, next);
}, },
function(data, next) { function(data, next) {
if(!Array.isArray(data.topicData) || !data.topicData.length) { if (!Array.isArray(data.topicData) || !data.topicData.length) {
return next(new Error('[[error:no-topic]]')); return next(new Error('[[error:no-topic]]'));
} }
@ -294,6 +281,15 @@ module.exports = function(Topics) {
], callback); ], callback);
}; };
function checkTitleLength(title, callback) {
if (!title || title.length < parseInt(meta.config.minimumTitleLength, 10)) {
return callback(new Error('[[error:title-too-short, ' + meta.config.minimumTitleLength + ']]'));
} else if (title.length > parseInt(meta.config.maximumTitleLength, 10)) {
return callback(new Error('[[error:title-too-long, ' + meta.config.maximumTitleLength + ']]'));
}
callback();
}
function checkContentLength(content, callback) { function checkContentLength(content, callback) {
if (!content || content.length < parseInt(meta.config.miminumPostLength, 10)) { if (!content || content.length < parseInt(meta.config.miminumPostLength, 10)) {
return callback(new Error('[[error:content-too-short, ' + meta.config.minimumPostLength + ']]')); return callback(new Error('[[error:content-too-short, ' + meta.config.minimumPostLength + ']]'));

Loading…
Cancel
Save