| 
									
										
										
										
											2014-03-08 15:45:42 -05:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-15 17:50:30 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-03 20:35:36 +03:00
										 |  |  | var async = require('async'); | 
					
						
							| 
									
										
										
										
											2017-08-16 12:32:06 -04:00
										 |  |  | var validator = require('validator'); | 
					
						
							| 
									
										
										
										
											2016-10-03 20:35:36 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | var db = require('./database'); | 
					
						
							|  |  |  | var user = require('./user'); | 
					
						
							|  |  |  | var plugins = require('./plugins'); | 
					
						
							|  |  |  | var meta = require('./meta'); | 
					
						
							| 
									
										
										
										
											2017-04-08 20:22:21 -06:00
										 |  |  | var utils = require('./utils'); | 
					
						
							| 
									
										
										
										
											2013-08-26 13:18:20 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | var Messaging = module.exports; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | require('./messaging/data')(Messaging); | 
					
						
							|  |  |  | require('./messaging/create')(Messaging); | 
					
						
							|  |  |  | require('./messaging/delete')(Messaging); | 
					
						
							|  |  |  | require('./messaging/edit')(Messaging); | 
					
						
							|  |  |  | require('./messaging/rooms')(Messaging); | 
					
						
							|  |  |  | require('./messaging/unread')(Messaging); | 
					
						
							|  |  |  | require('./messaging/notifications')(Messaging); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Messaging.getMessages = function (params, callback) { | 
					
						
							|  |  |  | 	var uid = params.uid; | 
					
						
							|  |  |  | 	var roomId = params.roomId; | 
					
						
							|  |  |  | 	var isNew = params.isNew || false; | 
					
						
							|  |  |  | 	var start = params.hasOwnProperty('start') ? params.start : 0; | 
					
						
							|  |  |  | 	var stop = parseInt(start, 10) + ((params.count || 50) - 1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var indices = {}; | 
					
						
							|  |  |  | 	async.waterfall([ | 
					
						
							|  |  |  | 		function (next) { | 
					
						
							|  |  |  | 			canGet('filter:messaging.canGetMessages', params.callerUid, params.uid, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (canGet, next) { | 
					
						
							|  |  |  | 			if (!canGet) { | 
					
						
							|  |  |  | 				return callback(null, null); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			db.getSortedSetRevRange('uid:' + uid + ':chat:room:' + roomId + ':mids', start, stop, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (mids, next) { | 
					
						
							| 
									
										
										
										
											2017-05-27 00:30:07 -04:00
										 |  |  | 			if (!mids.length) { | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | 				return callback(null, []); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:50 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | 			mids.forEach(function (mid, index) { | 
					
						
							|  |  |  | 				indices[mid] = start + index; | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2016-10-03 20:35:36 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | 			mids.reverse(); | 
					
						
							| 
									
										
										
										
											2016-10-03 20:35:36 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | 			Messaging.getMessagesData(mids, uid, roomId, isNew, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (messageData, next) { | 
					
						
							|  |  |  | 			messageData.forEach(function (messageData) { | 
					
						
							|  |  |  | 				messageData.index = indices[messageData.messageId.toString()]; | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 			next(null, messageData); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | 	], callback); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function canGet(hook, callerUid, uid, callback) { | 
					
						
							|  |  |  | 	plugins.fireHook(hook, { | 
					
						
							|  |  |  | 		callerUid: callerUid, | 
					
						
							|  |  |  | 		uid: uid, | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 		canGet: parseInt(callerUid, 10) === parseInt(uid, 10), | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | 	}, function (err, data) { | 
					
						
							|  |  |  | 		callback(err, data ? data.canGet : false); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Messaging.parse = function (message, fromuid, uid, roomId, isNew, callback) { | 
					
						
							| 
									
										
										
										
											2017-10-26 18:27:54 -04:00
										 |  |  | 	message = S(message).stripTags().decodeHTMLEntities().s; | 
					
						
							|  |  |  | 	message = validator.escape(String(message)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | 	plugins.fireHook('filter:parse.raw', message, function (err, parsed) { | 
					
						
							|  |  |  | 		if (err) { | 
					
						
							|  |  |  | 			return callback(err); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-08-26 13:18:20 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-26 17:54:55 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | 		var messageData = { | 
					
						
							|  |  |  | 			message: message, | 
					
						
							|  |  |  | 			parsed: parsed, | 
					
						
							|  |  |  | 			fromuid: fromuid, | 
					
						
							| 
									
										
										
										
											2016-10-03 20:35:36 +03:00
										 |  |  | 			uid: uid, | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | 			roomId: roomId, | 
					
						
							|  |  |  | 			isNew: isNew, | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 			parsedMessage: parsed, | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | 		}; | 
					
						
							| 
									
										
										
										
											2016-10-03 20:35:36 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | 		plugins.fireHook('filter:messaging.parse', messageData, function (err, messageData) { | 
					
						
							|  |  |  | 			callback(err, messageData ? messageData.parsedMessage : ''); | 
					
						
							| 
									
										
										
										
											2015-12-15 17:50:30 +02:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | 	}); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Messaging.isNewSet = function (uid, roomId, timestamp, callback) { | 
					
						
							|  |  |  | 	var setKey = 'uid:' + uid + ':chat:room:' + roomId + ':mids'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	async.waterfall([ | 
					
						
							|  |  |  | 		function (next) { | 
					
						
							|  |  |  | 			db.getSortedSetRevRangeWithScores(setKey, 0, 0, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (messages, next) { | 
					
						
							|  |  |  | 			if (messages && messages.length) { | 
					
						
							| 
									
										
										
										
											2017-03-08 15:09:58 +03:00
										 |  |  | 				next(null, parseInt(timestamp, 10) > parseInt(messages[0].score, 10) + Messaging.newMessageCutoff); | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | 			} else { | 
					
						
							|  |  |  | 				next(null, true); | 
					
						
							| 
									
										
										
										
											2015-12-15 17:50:30 +02:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | 	], callback); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Messaging.getRecentChats = function (callerUid, uid, start, stop, callback) { | 
					
						
							|  |  |  | 	async.waterfall([ | 
					
						
							|  |  |  | 		function (next) { | 
					
						
							|  |  |  | 			canGet('filter:messaging.canGetRecentChats', callerUid, uid, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (canGet, next) { | 
					
						
							|  |  |  | 			if (!canGet) { | 
					
						
							|  |  |  | 				return callback(null, null); | 
					
						
							| 
									
										
										
										
											2015-12-17 11:47:32 +02:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | 			db.getSortedSetRevRange('uid:' + uid + ':chat:rooms', start, stop, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (roomIds, next) { | 
					
						
							|  |  |  | 			async.parallel({ | 
					
						
							|  |  |  | 				roomData: function (next) { | 
					
						
							|  |  |  | 					Messaging.getRoomsData(roomIds, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				unread: function (next) { | 
					
						
							|  |  |  | 					db.isSortedSetMembers('uid:' + uid + ':chat:rooms:unread', roomIds, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				users: function (next) { | 
					
						
							|  |  |  | 					async.map(roomIds, function (roomId, next) { | 
					
						
							|  |  |  | 						db.getSortedSetRevRange('chat:room:' + roomId + ':uids', 0, 9, function (err, uids) { | 
					
						
							|  |  |  | 							if (err) { | 
					
						
							|  |  |  | 								return next(err); | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 							uids = uids.filter(function (value) { | 
					
						
							|  |  |  | 								return value && parseInt(value, 10) !== parseInt(uid, 10); | 
					
						
							| 
									
										
										
										
											2016-10-03 20:35:36 +03:00
										 |  |  | 							}); | 
					
						
							| 
									
										
										
										
											2017-02-18 01:31:47 -07:00
										 |  |  | 							user.getUsersFields(uids, ['uid', 'username', 'userslug', 'picture', 'status', 'lastonline'], next); | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | 						}); | 
					
						
							|  |  |  | 					}, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				teasers: function (next) { | 
					
						
							|  |  |  | 					async.map(roomIds, function (roomId, next) { | 
					
						
							|  |  |  | 						Messaging.getTeaser(uid, roomId, next); | 
					
						
							|  |  |  | 					}, next); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | 			}, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (results, next) { | 
					
						
							|  |  |  | 			results.roomData.forEach(function (room, index) { | 
					
						
							| 
									
										
										
										
											2017-10-12 17:05:15 -04:00
										 |  |  | 				if (room) { | 
					
						
							|  |  |  | 					room.users = results.users[index]; | 
					
						
							|  |  |  | 					room.groupChat = room.hasOwnProperty('groupChat') ? room.groupChat : room.users.length > 2; | 
					
						
							|  |  |  | 					room.unread = results.unread[index]; | 
					
						
							|  |  |  | 					room.teaser = results.teasers[index]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					room.users.forEach(function (userData) { | 
					
						
							|  |  |  | 						if (userData && parseInt(userData.uid, 10)) { | 
					
						
							|  |  |  | 							userData.status = user.getStatus(userData); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 					room.users = room.users.filter(function (user) { | 
					
						
							|  |  |  | 						return user && parseInt(user.uid, 10); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 					room.lastUser = room.users[0]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					room.usernames = Messaging.generateUsernames(room.users, uid); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2016-09-30 19:39:08 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-12 17:05:15 -04:00
										 |  |  | 			results.roomData = results.roomData.filter(Boolean); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			next(null, { rooms: results.roomData, nextStart: stop + 1 }); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2017-04-26 10:45:40 -06:00
										 |  |  | 		function (ref, next) { | 
					
						
							|  |  |  | 			plugins.fireHook('filter:messaging.getRecentChats', { | 
					
						
							|  |  |  | 				rooms: ref.rooms, | 
					
						
							|  |  |  | 				nextStart: ref.nextStart, | 
					
						
							|  |  |  | 				uid: uid, | 
					
						
							|  |  |  | 				callerUid: callerUid, | 
					
						
							|  |  |  | 			}, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | 	], callback); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Messaging.generateUsernames = function (users, excludeUid) { | 
					
						
							|  |  |  | 	users = users.filter(function (user) { | 
					
						
							|  |  |  | 		return user && parseInt(user.uid, 10) !== excludeUid; | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 	return users.map(function (user) { | 
					
						
							|  |  |  | 		return user.username; | 
					
						
							|  |  |  | 	}).join(', '); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Messaging.getTeaser = function (uid, roomId, callback) { | 
					
						
							|  |  |  | 	var teaser; | 
					
						
							|  |  |  | 	async.waterfall([ | 
					
						
							|  |  |  | 		function (next) { | 
					
						
							|  |  |  | 			db.getSortedSetRevRange('uid:' + uid + ':chat:room:' + roomId + ':mids', 0, 0, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (mids, next) { | 
					
						
							|  |  |  | 			if (!mids || !mids.length) { | 
					
						
							|  |  |  | 				return next(null, null); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			Messaging.getMessageFields(mids[0], ['fromuid', 'content', 'timestamp'], next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (_teaser, next) { | 
					
						
							|  |  |  | 			teaser = _teaser; | 
					
						
							|  |  |  | 			if (!teaser) { | 
					
						
							|  |  |  | 				return callback(); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			if (teaser.content) { | 
					
						
							| 
									
										
										
										
											2017-10-13 21:02:41 -06:00
										 |  |  | 				teaser.content = utils.stripHTMLTags(utils.decodeHTMLEntities(teaser.content)); | 
					
						
							| 
									
										
										
										
											2017-08-16 12:32:06 -04:00
										 |  |  | 				teaser.content = validator.escape(String(teaser.content)); | 
					
						
							| 
									
										
										
										
											2015-12-17 12:35:49 +02:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | 			teaser.timestampISO = utils.toISOString(teaser.timestamp); | 
					
						
							| 
									
										
										
										
											2017-02-18 01:31:47 -07:00
										 |  |  | 			user.getUserFields(teaser.fromuid, ['uid', 'username', 'userslug', 'picture', 'status', 'lastonline'], next); | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		function (user, next) { | 
					
						
							|  |  |  | 			teaser.user = user; | 
					
						
							| 
									
										
										
										
											2017-01-27 11:26:05 -05:00
										 |  |  | 			plugins.fireHook('filter:messaging.getTeaser', { teaser: teaser }, function (err, data) { | 
					
						
							|  |  |  | 				next(err, data.teaser); | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | 	], callback); | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2015-04-16 20:48:38 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | Messaging.canMessageUser = function (uid, toUid, callback) { | 
					
						
							| 
									
										
										
										
											2017-02-18 14:29:52 -07:00
										 |  |  | 	if (parseInt(meta.config.disableChat, 10) === 1 || !uid || uid === toUid) { | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | 		return callback(new Error('[[error:chat-disabled]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-10-22 10:22:05 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | 	if (parseInt(uid, 10) === parseInt(toUid, 10)) { | 
					
						
							|  |  |  | 		return callback(new Error('[[error:cant-chat-with-yourself')); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-07-19 10:33:27 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | 	async.waterfall([ | 
					
						
							|  |  |  | 		function (next) { | 
					
						
							|  |  |  | 			user.exists(toUid, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (exists, next) { | 
					
						
							|  |  |  | 			if (!exists) { | 
					
						
							|  |  |  | 				return callback(new Error('[[error:no-user]]')); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			user.getUserFields(uid, ['banned', 'email:confirmed'], next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (userData, next) { | 
					
						
							|  |  |  | 			if (parseInt(userData.banned, 10) === 1) { | 
					
						
							|  |  |  | 				return callback(new Error('[[error:user-banned]]')); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2015-04-16 20:48:38 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | 			if (parseInt(meta.config.requireEmailConfirmation, 10) === 1 && parseInt(userData['email:confirmed'], 10) !== 1) { | 
					
						
							|  |  |  | 				return callback(new Error('[[error:email-not-confirmed-chat]]')); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-09-16 13:27:26 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | 			async.parallel({ | 
					
						
							|  |  |  | 				settings: async.apply(user.getSettings, toUid), | 
					
						
							|  |  |  | 				isAdmin: async.apply(user.isAdministrator, uid), | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 				isFollowing: async.apply(user.isFollowing, toUid, uid), | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | 			}, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (results, next) { | 
					
						
							| 
									
										
										
										
											2017-04-26 10:45:40 -06:00
										 |  |  | 			if (results.settings.restrictChat && !results.isAdmin && !results.isFollowing) { | 
					
						
							|  |  |  | 				return next(new Error('[[error:chat-restricted]]')); | 
					
						
							| 
									
										
										
										
											2015-12-16 11:15:43 +02:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-09-16 13:27:26 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-26 15:22:17 -06:00
										 |  |  | 			plugins.fireHook('static:messaging.canMessageUser', { | 
					
						
							| 
									
										
										
										
											2017-04-26 10:45:40 -06:00
										 |  |  | 				uid: uid, | 
					
						
							|  |  |  | 				toUid: toUid, | 
					
						
							|  |  |  | 			}, function (err) { | 
					
						
							|  |  |  | 				next(err); | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | 	], callback); | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2016-02-29 10:36:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | Messaging.canMessageRoom = function (uid, roomId, callback) { | 
					
						
							| 
									
										
										
										
											2017-02-18 14:29:52 -07:00
										 |  |  | 	if (parseInt(meta.config.disableChat, 10) === 1 || !uid) { | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | 		return callback(new Error('[[error:chat-disabled]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-02-29 10:36:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | 	async.waterfall([ | 
					
						
							|  |  |  | 		function (next) { | 
					
						
							|  |  |  | 			Messaging.isUserInRoom(uid, roomId, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (inRoom, next) { | 
					
						
							|  |  |  | 			if (!inRoom) { | 
					
						
							|  |  |  | 				return next(new Error('[[error:not-in-room]]')); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2016-02-29 10:36:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | 			Messaging.getUserCountInRoom(roomId, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (count, next) { | 
					
						
							|  |  |  | 			if (count < 2) { | 
					
						
							|  |  |  | 				return next(new Error('[[error:no-users-in-room]]')); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2015-04-16 20:32:16 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | 			user.getUserFields(uid, ['banned', 'email:confirmed'], next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (userData, next) { | 
					
						
							|  |  |  | 			if (parseInt(userData.banned, 10) === 1) { | 
					
						
							|  |  |  | 				return next(new Error('[[error:user-banned]]')); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2015-04-16 20:32:16 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | 			if (parseInt(meta.config.requireEmailConfirmation, 10) === 1 && parseInt(userData['email:confirmed'], 10) !== 1) { | 
					
						
							|  |  |  | 				return next(new Error('[[error:email-not-confirmed-chat]]')); | 
					
						
							| 
									
										
										
										
											2014-10-30 17:50:07 -04:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-26 15:22:17 -06:00
										 |  |  | 			plugins.fireHook('static:messaging.canMessageRoom', { | 
					
						
							|  |  |  | 				uid: uid, | 
					
						
							|  |  |  | 				roomId: roomId, | 
					
						
							|  |  |  | 			}, function (err) { | 
					
						
							|  |  |  | 				next(err); | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | 	], callback); | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2016-01-18 15:35:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | Messaging.hasPrivateChat = function (uid, withUid, callback) { | 
					
						
							|  |  |  | 	if (parseInt(uid, 10) === parseInt(withUid, 10)) { | 
					
						
							|  |  |  | 		return callback(null, 0); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	async.waterfall([ | 
					
						
							|  |  |  | 		function (next) { | 
					
						
							|  |  |  | 			async.parallel({ | 
					
						
							|  |  |  | 				myRooms: async.apply(db.getSortedSetRevRange, 'uid:' + uid + ':chat:rooms', 0, -1), | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 				theirRooms: async.apply(db.getSortedSetRevRange, 'uid:' + withUid + ':chat:rooms', 0, -1), | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | 			}, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (results, next) { | 
					
						
							|  |  |  | 			var roomIds = results.myRooms.filter(function (roomId) { | 
					
						
							|  |  |  | 				return roomId && results.theirRooms.indexOf(roomId) !== -1; | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2016-01-18 15:35:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | 			if (!roomIds.length) { | 
					
						
							|  |  |  | 				return callback(); | 
					
						
							| 
									
										
										
										
											2016-01-18 15:35:24 +02:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2015-12-11 12:07:02 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | 			var index = 0; | 
					
						
							|  |  |  | 			var roomId = 0; | 
					
						
							|  |  |  | 			async.whilst(function () { | 
					
						
							|  |  |  | 				return index < roomIds.length && !roomId; | 
					
						
							|  |  |  | 			}, function (next) { | 
					
						
							|  |  |  | 				Messaging.getUserCountInRoom(roomIds[index], function (err, count) { | 
					
						
							|  |  |  | 					if (err) { | 
					
						
							|  |  |  | 						return next(err); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					if (count === 2) { | 
					
						
							|  |  |  | 						roomId = roomIds[index]; | 
					
						
							|  |  |  | 						next(null, roomId); | 
					
						
							|  |  |  | 					} else { | 
					
						
							| 
									
										
										
										
											2017-02-18 01:12:18 -07:00
										 |  |  | 						index += 1; | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | 						next(); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}, function (err) { | 
					
						
							|  |  |  | 				next(err, roomId); | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | 	], callback); | 
					
						
							|  |  |  | }; |