all files / src/topics/ create.js

92.41% Statements 134/145
66.18% Branches 45/68
100% Functions 50/50
92.41% Lines 134/145
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328            11× 11×   11×   11×     11×                               11×       11×     11× 11×     11×   11×             11×     11×     11×     11×         11× 11×         15× 15× 15×   15×   15×     14×     14× 13×   14×     13×     13×   12×     12×     11×       11×                                                                               16× 16× 16× 16× 16×   16×   16×     16× 16×                 16×     15×       15×     14×       14×     14×     14× 14× 13×     14×     13×     13× 13×     13×     13×       13× 13×     13× 13× 13×   13×         22× 22× 22×   22×     22×     22×   22×     22×     22×     22×         22× 22× 22×     22×       22× 22× 22× 22× 22× 22× 22×   22×         57× 54×     54×     25×       25×        
 
'use strict';
 
var async = require('async');
var validator = require('validator');
var db = require('../database');
var utils = require('../../public/src/utils');
var plugins = require('../plugins');
var analytics = require('../analytics');
var user = require('../user');
var meta = require('../meta');
var posts = require('../posts');
var privileges = require('../privileges');
var categories = require('../categories');
 
module.exports = function(Topics) {
 
	Topics.create = function(data, callback) {
		// This is an internal method, consider using Topics.post instead
		var timestamp = data.timestamp || Date.now();
		var topicData;
 
		async.waterfall([
			function(next) {
				db.incrObjectField('global', 'nextTid', next);
			},
			function(tid, next) {
				topicData = {
					'tid': tid,
					'uid': data.uid,
					'cid': data.cid,
					'mainPid': 0,
					'title': data.title,
					'slug': tid + '/' + (utils.slugify(data.title) || 'topic'),
					'timestamp': timestamp,
					'lastposttime': 0,
					'postcount': 0,
					'viewcount': 0,
					'locked': 0,
					'deleted': 0,
					'pinned': 0
				};
 
				Iif (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([
					function(next) {
						db.sortedSetsAdd([
							'topics:tid',
							'cid:' + topicData.cid + ':tids',
							'cid:' + topicData.cid + ':uid:' + topicData.uid + ':tids'
						], timestamp, topicData.tid, next);
					},
					function(next) {
						user.addTopicIdToUser(topicData.uid, topicData.tid, timestamp, next);
					},
					function(next) {
						db.incrObjectField('category:' + topicData.cid, 'topic_count', next);
					},
					function(next) {
						db.incrObjectField('global', 'topicCount', next);
					},
					function(next) {
						Topics.createTags(data.tags, topicData.tid, timestamp, next);
					}
				], next);
			},
			function(results, next) {
				plugins.fireHook('action:topic.save', topicData);
				next(null, topicData.tid);
			}
		], callback);
	};
 
	Topics.post = function(data, callback) {
		var uid = data.uid;
		var title = data.title ? data.title.trim() : data.title;
		data.tags = data.tags || [];
 
		async.waterfall([
			function(next) {
				check(title, meta.config.minimumTitleLength, meta.config.maximumTitleLength, 'title-too-short', 'title-too-long', next);
			},
			function(next) {
				check(data.tags, meta.config.minimumTagsPerTopic, meta.config.maximumTagsPerTopic, 'not-enough-tags', 'too-many-tags', next);
			},
			function(next) {
				if (data.content) {
					data.content = data.content.rtrim();
				}
				check(data.content, meta.config.minimumPostLength, meta.config.maximumPostLength, 'content-too-short', 'content-too-long', next);
			},
			function(next) {
				categories.exists(data.cid, next);
			},
			function(categoryExists, next) {
				if (!categoryExists) {
					return next(new Error('[[error:no-category]]'));
				}
				privileges.categories.can('topics:create', data.cid, data.uid, next);
			},
			function(canCreate, next) {
				if (!canCreate) {
					return next(new Error('[[error:no-privileges]]'));
				}
 
				Iif (!guestHandleValid(data)) {
					return next(new Error('[[error:guest-handle-invalid]]'));
				}
 
				user.isReadyToPost(data.uid, data.cid, next);
			},
			function(next) {
				plugins.fireHook('filter:topic.post', data, next);
			},
			function(filteredData, next) {
				data = filteredData;
				Topics.create({uid: data.uid, title: data.title, cid: data.cid, thumb: data.thumb, tags: data.tags, timestamp: data.timestamp}, next);
			},
			function(tid, next) {
				posts.create({uid: data.uid, tid: tid, handle: data.handle, content: data.content, timestamp: data.timestamp, ip: data.req ? data.req.ip : null}, next);
			},
			function(postData, next) {
				onNewPost(postData, data, next);
			},
			function(postData, next) {
				async.parallel({
					postData: function(next) {
						next(null, postData);
					},
					settings: function(next) {
						user.getSettings(uid, function(err, settings) {
							Iif (err) {
								return next(err);
							}
							Eif (settings.followTopicsOnCreate) {
								Topics.follow(postData.tid, uid, next);
							} else {
								next();
							}
						});
					},
					topicData: function(next) {
						Topics.getTopicsByTids([postData.tid], uid, next);
					}
				}, next);
			},
			function(data, next) {
				Iif (!Array.isArray(data.topicData) || !data.topicData.length) {
					return next(new Error('[[error:no-topic]]'));
				}
 
				data.topicData = data.topicData[0];
				data.topicData.unreplied = 1;
				data.topicData.mainPost = data.postData;
				data.postData.index = 0;
 
				analytics.increment(['topics', 'topics:byCid:' + data.topicData.cid]);
				plugins.fireHook('action:topic.post', data.topicData);
 
				Eif (parseInt(uid, 10)) {
					user.notifications.sendTopicNotificationToFollowers(uid, data.topicData, data.postData);
				}
 
				next(null, {
					topicData: data.topicData,
					postData: data.postData
				});
			}
		], callback);
	};
 
	Topics.reply = function(data, callback) {
		var tid = data.tid;
		var uid = data.uid;
		var content = data.content;
		var postData;
		var cid;
 
		async.waterfall([
			function(next) {
				Topics.getTopicField(tid, 'cid', next);
			},
			function(_cid, next) {
				cid = _cid;
				async.parallel({
					exists: async.apply(Topics.exists, tid),
					locked: async.apply(Topics.isLocked, tid),
					canReply: async.apply(privileges.topics.can, 'topics:reply', tid, uid),
					isAdmin: async.apply(user.isAdministrator, uid),
					isModerator: async.apply(user.isModerator, uid, cid)
				}, next);
			},
			function(results, next) {
				if (!results.exists) {
					return next(new Error('[[error:no-topic]]'));
				}
 
				Iif (results.locked && !results.isAdmin && !results.isModerator) {
					return next(new Error('[[error:topic-locked]]'));
				}
 
				if (!results.canReply) {
					return next(new Error('[[error:no-privileges]]'));
				}
 
				Iif (!guestHandleValid(data)) {
					return next(new Error('[[error:guest-handle-invalid]]'));
				}
 
				user.isReadyToPost(uid, cid, next);
			},
			function(next) {
				plugins.fireHook('filter:topic.reply', data, next);
			},
			function(filteredData, next) {
				content = filteredData.content || data.content;
				if (content) {
					content = content.rtrim();
				}
 
				check(content, meta.config.minimumPostLength, meta.config.maximumPostLength, 'content-too-short', 'content-too-long', next);
			},
			function(next) {
				posts.create({uid: uid, tid: tid, handle: data.handle, content: content, toPid: data.toPid, timestamp: data.timestamp, ip: data.req ? data.req.ip : null}, next);
			},
			function(_postData, next) {
				postData = _postData;
				onNewPost(postData, data, next);
			},
			function(postData, next) {
				user.getSettings(uid, next);
			},
			function(settings, next) {
				Iif (settings.followTopicsOnReply) {
					Topics.follow(postData.tid, uid);
				}
 
				Eif (parseInt(uid, 10)) {
					user.setUserField(uid, 'lastonline', Date.now());
				}
 
				Topics.notifyFollowers(postData, uid);
				analytics.increment(['posts', 'posts:byCid:' + cid]);
				plugins.fireHook('action:topic.reply', postData);
 
				next(null, postData);
			}
		], callback);
	};
 
	function onNewPost(postData, data, callback) {
		var tid = postData.tid;
		var uid = postData.uid;
		async.waterfall([
			function (next) {
				Topics.markAsUnreadForAll(tid, next);
			},
			function (next) {
				Topics.markAsRead([tid], uid, next);
			},
			function (markedRead, next) {
				async.parallel({
					userInfo: function(next) {
						posts.getUserInfoForPosts([postData.uid], uid, next);
					},
					topicInfo: function(next) {
						Topics.getTopicFields(tid, ['tid', 'title', 'slug', 'cid', 'postcount'], next);
					},
					parents: function(next) {
						Topics.addParentPosts([postData], next);
					},
					content: function(next) {
						posts.parsePost(postData, next);
					}
				}, next);
			},
			function (results, next) {
				postData.user = results.userInfo[0];
				postData.topic = results.topicInfo;
				postData.index = parseInt(results.topicInfo.postcount, 10) - 1;
 
				// Username override for guests, if enabled
				Iif (parseInt(meta.config.allowGuestHandles, 10) === 1 && parseInt(postData.uid, 10) === 0 && data.handle) {
					postData.user.username = validator.escape(data.handle);
				}
 
				postData.favourited = false;
				postData.votes = 0;
				postData.display_moderator_tools = true;
				postData.display_move_tools = true;
				postData.selfPost = false;
				postData.timestampISO = utils.toISOString(postData.timestamp);
				postData.topic.title = validator.escape(postData.topic.title);
 
				next(null, postData);
			}
		], callback);
	}
 
	function check(item, min, max, minError, maxError, callback) {
		if (!item || item.length < parseInt(min, 10)) {
			return callback(new Error('[[error:'+ minError + ', ' + min + ']]'));
		} else Iif (item.length > parseInt(max, 10)) {
			return callback(new Error('[[error:'+ maxError + ', ' + max + ']]'));
		}
		callback();
	}
 
	function guestHandleValid(data) {
		Iif (parseInt(meta.config.allowGuestHandles, 10) === 1 && parseInt(data.uid, 10) === 0 &&
			data.handle && data.handle.length > meta.config.maximumUsernameLength) {
			return false;
		}
		return true;
	}
 
};