all files / src/socket.io/posts/ edit.js

21.43% Statements 9/42
0% Branches 0/39
16.67% Functions 1/6
21.43% Lines 9/42
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                                                                                                                                                   
'use strict';
 
var async = require('async');
var winston = require('winston');
 
var posts = require('../../posts');
var groups = require('../../groups');
var events = require('../../events');
var meta = require('../../meta');
var websockets = require('../index');
 
module.exports = function(SocketPosts) {
 
	SocketPosts.edit = function(socket, data, callback) {
		if (!socket.uid) {
			return callback(new Error('[[error:not-logged-in]]'));
		} else if (!data || !data.pid || !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 && data.title.length > parseInt(meta.config.maximumTitleLength, 10)) {
			return callback(new Error('[[error:title-too-long, ' + meta.config.maximumTitleLength + ']]'));
		} else if (data.tags && data.tags.length < parseInt(meta.config.minimumTagsPerTopic, 10)) {
			return callback(new Error('[[error:not-enough-tags, ' + meta.config.minimumTagsPerTopic + ']]'));
		} else if (data.tags && data.tags.length > parseInt(meta.config.maximumTagsPerTopic, 10)) {
			return callback(new Error('[[error:too-many-tags, ' + meta.config.maximumTagsPerTopic + ']]'));
		} else if (!data.content || data.content.length < parseInt(meta.config.minimumPostLength, 10)) {
			return callback(new Error('[[error:content-too-short, ' + meta.config.minimumPostLength + ']]'));
		} else if (data.content.length > parseInt(meta.config.maximumPostLength, 10)) {
			return callback(new Error('[[error:content-too-long, ' + meta.config.maximumPostLength + ']]'));
		}
 
		posts.edit({
			uid: socket.uid,
			handle: data.handle,
			pid: data.pid,
			title: data.title,
			content: data.content,
			topic_thumb: data.topic_thumb,
			tags: data.tags,
			req: websockets.reqFromSocket(socket)
		}, function(err, result) {
			if (err) {
				return callback(err);
			}
 
			if (result.topic.renamed) {
				events.log({
					type: 'topic-rename',
					uid: socket.uid,
					ip: socket.ip,
					oldTitle: result.topic.oldTitle,
					newTitle: result.topic.title
				});
			}
 
			if (parseInt(result.post.deleted) !== 1) {
				websockets.in('topic_' + result.topic.tid).emit('event:post_edited', result);
				return callback(null, result.post);
			}
 
			socket.emit('event:post_edited', result);
			callback(null, result.post);
 
			async.parallel({
				admins: async.apply(groups.getMembers, 'administrators', 0, -1),
				moderators: async.apply(groups.getMembers, 'cid:' + result.topic.cid + ':privileges:mods', 0, -1)
			}, function(err, results) {
				if (err) {
					return winston.error(err);
				}
 
				var uids = results.admins.concat(results.moderators).filter(function(uid, index, array) {
					return uid && array.indexOf(uid) === index;
				});
 
				uids.forEach(function(uid) {
					websockets.in('uid_' + uid).emit('event:post_edited', result);
				});
			});
		});
	};
};