all files / src/socket.io/ helpers.js

30.51% Statements 18/59
0% Branches 0/38
0% Functions 0/15
30.51% Lines 18/59
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                                                                                                                                                                                                                               
'use strict';
 
var async = require('async');
var winston = require('winston');
var S = require('string');
var nconf = require('nconf');
 
var websockets = require('./index');
var user = require('../user');
var posts = require('../posts');
var topics = require('../topics');
var privileges = require('../privileges');
var notifications = require('../notifications');
var plugins = require('../plugins');
 
var SocketHelpers = {};
 
SocketHelpers.notifyOnlineUsers = function(uid, result) {
	winston.warn('[deprecated] SocketHelpers.notifyOnlineUsers, consider using socketHelpers.notifyNew(uid, \'newPost\', result);');
	SocketHelpers.notifyNew(uid, 'newPost', result);
};
 
SocketHelpers.notifyNew = function(uid, type, result) {
	async.waterfall([
		function(next) {
			user.getUidsFromSet('users:online', 0, -1, next);
		},
		function(uids, next) {
			privileges.topics.filterUids('read', result.posts[0].topic.tid, uids, next);
		},
		function(uids, next) {
			plugins.fireHook('filter:sockets.sendNewPostToUids', {uidsTo: uids, uidFrom: uid, type: type}, next);
		}
	], function(err, data) {
		if (err) {
			return winston.error(err.stack);
		}
 
		result.posts[0].ip = undefined;
 
		data.uidsTo.forEach(function(toUid) {
			if (parseInt(toUid, 10) !== uid) {
				websockets.in('uid_' + toUid).emit('event:new_post', result);
				if (result.topic && type === 'newTopic') {
					websockets.in('uid_' + toUid).emit('event:new_topic', result.topic);
				}
			}
		});
	});
};
 
SocketHelpers.sendNotificationToPostOwner = function(pid, fromuid, notification) {
	if (!pid || !fromuid || !notification) {
		return;
	}
	posts.getPostFields(pid, ['tid', 'uid', 'content'], function(err, postData) {
		if (err) {
			return;
		}
 
		if (!postData.uid || fromuid === parseInt(postData.uid, 10)) {
			return;
		}
 
		async.parallel({
			username: async.apply(user.getUserField, fromuid, 'username'),
			topicTitle: async.apply(topics.getTopicField, postData.tid, 'title'),
			postObj: async.apply(posts.parsePost, postData)
		}, function(err, results) {
			if (err) {
				return;
			}
 
			var title = S(results.topicTitle).decodeHTMLEntities().s;
			var titleEscaped = title.replace(/%/g, '%').replace(/,/g, ',');
 
			notifications.create({
				bodyShort: '[[' + notification + ', ' + results.username + ', ' + titleEscaped + ']]',
				bodyLong: results.postObj.content,
				pid: pid,
				nid: 'post:' + pid + ':uid:' + fromuid,
				from: fromuid,
				mergeId: notification + '|' + pid,
				topicTitle: results.topicTitle
			}, function(err, notification) {
				if (!err && notification) {
					notifications.push(notification, [postData.uid]);
				}
			});
		});
	});
};
 
 
SocketHelpers.sendNotificationToTopicOwner = function(tid, fromuid, notification) {
	if (!tid || !fromuid || !notification) {
		return;
	}
 
	async.parallel({
		username: async.apply(user.getUserField, fromuid, 'username'),
		topicData: async.apply(topics.getTopicFields, tid, ['uid', 'slug', 'title']),
	}, function(err, results) {
		if (err || fromuid === parseInt(results.topicData.uid, 10)) {
			return;
		}
 
		var title = S(results.topicData.title).decodeHTMLEntities().s;
		var titleEscaped = title.replace(/%/g, '%').replace(/,/g, ',');
 
		notifications.create({
			bodyShort: '[[' + notification + ', ' + results.username + ', ' + titleEscaped + ']]',
			path: nconf.get('relative_path') + '/topic/' + results.topicData.slug,
			nid: 'tid:' + tid + ':uid:' + fromuid,
			from: fromuid
		}, function(err, notification) {
			if (!err && notification) {
				notifications.push(notification, [results.topicData.uid]);
			}
		});
	});
};
 
SocketHelpers.emitToTopicAndCategory = function(event, data) {
	websockets.in('topic_' + data.tid).emit(event, data);
	websockets.in('category_' + data.cid).emit(event, data);
};
 
module.exports = SocketHelpers;