| 
									
										
										
										
											2014-03-08 15:45:42 -05:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-15 17:50:30 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | var async = require('async'), | 
					
						
							| 
									
										
										
										
											2014-07-25 16:46:04 -04:00
										 |  |  | 	winston = require('winston'), | 
					
						
							| 
									
										
										
										
											2015-09-16 15:44:10 -04:00
										 |  |  | 	S = require('string'), | 
					
						
							| 
									
										
										
										
											2015-12-16 11:15:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-16 15:44:10 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-15 17:50:30 +02:00
										 |  |  | 	db = require('./database'), | 
					
						
							| 
									
										
										
										
											2013-12-27 14:09:22 -05:00
										 |  |  | 	user = require('./user'), | 
					
						
							| 
									
										
										
										
											2014-01-25 01:34:56 -05:00
										 |  |  | 	plugins = require('./plugins'), | 
					
						
							| 
									
										
										
										
											2014-07-25 16:46:04 -04:00
										 |  |  | 	meta = require('./meta'), | 
					
						
							| 
									
										
										
										
											2014-08-08 15:28:39 -04:00
										 |  |  | 	utils = require('../public/src/utils'), | 
					
						
							| 
									
										
										
										
											2014-07-25 16:46:04 -04:00
										 |  |  | 	notifications = require('./notifications'), | 
					
						
							| 
									
										
										
										
											2015-12-16 11:15:43 +02:00
										 |  |  | 	userNotifications = require('./user/notifications'); | 
					
						
							| 
									
										
										
										
											2013-08-26 13:18:20 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | (function(Messaging) { | 
					
						
							| 
									
										
										
										
											2015-12-15 14:10:32 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-15 17:50:30 +02:00
										 |  |  | 	require('./messaging/create')(Messaging); | 
					
						
							|  |  |  | 	require('./messaging/delete')(Messaging); | 
					
						
							|  |  |  | 	require('./messaging/edit')(Messaging); | 
					
						
							|  |  |  | 	require('./messaging/rooms')(Messaging); | 
					
						
							|  |  |  | 	require('./messaging/unread')(Messaging); | 
					
						
							| 
									
										
										
										
											2015-12-16 11:15:43 +02:00
										 |  |  | 	require('./messaging/notifications')(Messaging); | 
					
						
							| 
									
										
										
										
											2015-12-15 14:10:32 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-30 16:51:15 -04:00
										 |  |  | 	var terms = { | 
					
						
							|  |  |  | 		day: 86400000, | 
					
						
							|  |  |  | 		week: 604800000, | 
					
						
							|  |  |  | 		month: 2592000000, | 
					
						
							|  |  |  | 		threemonths: 7776000000 | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-11 12:07:02 -05:00
										 |  |  | 	Messaging.getMessageField = function(mid, field, callback) { | 
					
						
							|  |  |  | 		Messaging.getMessageFields(mid, [field], function(err, fields) { | 
					
						
							| 
									
										
										
										
											2015-12-15 17:50:30 +02:00
										 |  |  | 			callback(err, fields ? fields[field] : null); | 
					
						
							| 
									
										
										
										
											2015-12-11 12:07:02 -05:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Messaging.getMessageFields = function(mid, fields, callback) { | 
					
						
							|  |  |  | 		db.getObjectFields('message:' + mid, fields, callback); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Messaging.setMessageField = function(mid, field, content, callback) { | 
					
						
							|  |  |  | 		db.setObjectField('message:' + mid, field, content, callback); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Messaging.setMessageFields = function(mid, data, callback) { | 
					
						
							|  |  |  | 		db.setObject('message:' + mid, data, callback); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-27 17:32:33 -04:00
										 |  |  | 	Messaging.getMessages = function(params, callback) { | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:50 +03:00
										 |  |  | 		var uid = params.uid; | 
					
						
							|  |  |  | 		var roomId = params.roomId; | 
					
						
							|  |  |  | 		var since = params.since; | 
					
						
							|  |  |  | 		var isNew = params.isNew; | 
					
						
							|  |  |  | 		var start = params.hasOwnProperty('start') ? params.start : 0; | 
					
						
							|  |  |  | 		var count = params.count || 250; | 
					
						
							|  |  |  | 		var markRead = params.markRead || true; | 
					
						
							| 
									
										
										
										
											2015-08-27 17:32:33 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-15 17:50:30 +02:00
										 |  |  | 		var min = params.count ? 0 : Date.now() - (terms[since] || terms.day); | 
					
						
							| 
									
										
										
										
											2013-08-26 13:18:20 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-30 16:51:15 -04:00
										 |  |  | 		if (since === 'recent') { | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:50 +03:00
										 |  |  | 			count = 50; | 
					
						
							| 
									
										
										
										
											2014-10-30 16:51:15 -04:00
										 |  |  | 			min = 0; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:50 +03:00
										 |  |  | 		db.getSortedSetRevRangeByScore('uid:' + uid + ':chat:room:' + roomId + ':mids', start, count, '+inf', min, function(err, mids) { | 
					
						
							| 
									
										
										
										
											2013-12-02 21:20:55 -05:00
										 |  |  | 			if (err) { | 
					
						
							| 
									
										
										
										
											2014-03-28 12:13:01 -04:00
										 |  |  | 				return callback(err); | 
					
						
							| 
									
										
										
										
											2013-12-02 21:20:55 -05:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-08-26 13:18:20 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-03 12:40:46 -04:00
										 |  |  | 			if (!Array.isArray(mids) || !mids.length) { | 
					
						
							| 
									
										
										
										
											2013-08-26 13:18:20 -04:00
										 |  |  | 				return callback(null, []); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:50 +03:00
										 |  |  | 			var indices = {}; | 
					
						
							|  |  |  | 			mids.forEach(function(mid, index) { | 
					
						
							|  |  |  | 				indices[mid] = start + index; | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2013-08-26 13:18:20 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-29 17:12:02 -04:00
										 |  |  | 			mids.reverse(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:50 +03:00
										 |  |  | 			Messaging.getMessagesData(mids, uid, roomId, isNew, function(err, messageData) { | 
					
						
							|  |  |  | 				if (err) { | 
					
						
							|  |  |  | 					return callback(err); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				for(var i=0; i<messageData.length; i++) { | 
					
						
							|  |  |  | 				 	messageData[i].index = indices[messageData[i].messageId.toString()]; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				callback(null, messageData); | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2014-03-28 12:13:01 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-07-25 16:46:04 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-08 18:29:57 -04:00
										 |  |  | 		if (markRead) { | 
					
						
							| 
									
										
										
										
											2015-12-15 17:50:30 +02:00
										 |  |  | 			notifications.markRead('chat_' + roomId + '_' + uid, uid, function(err) { | 
					
						
							| 
									
										
										
										
											2015-10-08 18:29:57 -04:00
										 |  |  | 				if (err) { | 
					
						
							|  |  |  | 					winston.error('[messaging] Could not mark notifications related to this chat as read: ' + err.message); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2014-07-25 16:46:04 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-15 17:50:30 +02:00
										 |  |  | 				userNotifications.pushCount(uid); | 
					
						
							| 
									
										
										
										
											2015-10-08 18:29:57 -04:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-03-28 12:13:01 -04:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-08-26 13:18:20 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-15 17:50:30 +02:00
										 |  |  | 	Messaging.getMessagesData = function(mids, uid, roomId, isNew, callback) { | 
					
						
							| 
									
										
										
										
											2014-01-26 22:10:06 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-15 17:50:30 +02:00
										 |  |  | 		var keys = mids.map(function(mid) { | 
					
						
							|  |  |  | 			return 'message:' + mid; | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-08-26 13:18:20 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-15 17:50:30 +02:00
										 |  |  | 		var messages; | 
					
						
							| 
									
										
										
										
											2015-12-11 12:07:02 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-15 17:50:30 +02:00
										 |  |  | 		async.waterfall([ | 
					
						
							|  |  |  | 			function (next) { | 
					
						
							|  |  |  | 				db.getObjects(keys, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function (_messages, next) { | 
					
						
							|  |  |  | 				messages = _messages.map(function(msg, idx) { | 
					
						
							|  |  |  | 					if (msg) { | 
					
						
							|  |  |  | 						msg.messageId = parseInt(mids[idx], 10); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					return msg; | 
					
						
							|  |  |  | 				}).filter(Boolean); | 
					
						
							| 
									
										
										
										
											2015-12-11 12:07:02 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-15 17:50:30 +02:00
										 |  |  | 				var uids = messages.map(function(msg) { | 
					
						
							|  |  |  | 					return msg && msg.fromuid; | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2014-07-24 22:04:33 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-15 17:50:30 +02:00
										 |  |  | 				user.getUsersFields(uids, ['uid', 'username', 'userslug', 'picture', 'status'], next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function (users, next) { | 
					
						
							|  |  |  | 				messages.forEach(function(message, index) { | 
					
						
							|  |  |  | 					message.fromUser = users[index]; | 
					
						
							|  |  |  | 					var self = parseInt(message.fromuid, 10) === parseInt(uid, 10); | 
					
						
							|  |  |  | 					message.self = self ? 1 : 0; | 
					
						
							|  |  |  | 					message.timestampISO = utils.toISOString(message.timestamp); | 
					
						
							|  |  |  | 					message.newSet = false; | 
					
						
							| 
									
										
										
										
											2016-07-13 17:32:00 +03:00
										 |  |  | 					message.roomId = String(message.roomId || roomId); | 
					
						
							| 
									
										
										
										
											2015-12-15 17:50:30 +02:00
										 |  |  | 					if (message.hasOwnProperty('edited')) { | 
					
						
							|  |  |  | 						message.editedISO = new Date(parseInt(message.edited, 10)).toISOString(); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2015-12-11 12:07:02 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-15 17:50:30 +02:00
										 |  |  | 				async.map(messages, function(message, next) { | 
					
						
							| 
									
										
										
										
											2016-08-17 14:12:35 +03:00
										 |  |  | 					Messaging.parse(message.content, message.fromuid, uid, roomId, isNew, function(err, result) { | 
					
						
							|  |  |  | 						if (err) { | 
					
						
							|  |  |  | 							return next(err); | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2015-12-15 17:50:30 +02:00
										 |  |  | 						message.content = result; | 
					
						
							|  |  |  | 						message.cleanedContent = S(result).stripTags().decodeHTMLEntities().s; | 
					
						
							|  |  |  | 						next(null, message); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				}, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function(messages, next) { | 
					
						
							|  |  |  | 				if (messages.length > 1) { | 
					
						
							|  |  |  | 					// Add a spacer in between messages with time gaps between them
 | 
					
						
							|  |  |  | 					messages = messages.map(function(message, index) { | 
					
						
							|  |  |  | 						// Compare timestamps with the previous message, and check if a spacer needs to be added
 | 
					
						
							|  |  |  | 						if (index > 0 && parseInt(message.timestamp, 10) > parseInt(messages[index-1].timestamp, 10) + (1000*60*5)) { | 
					
						
							|  |  |  | 							// If it's been 5 minutes, this is a new set of messages
 | 
					
						
							|  |  |  | 							message.newSet = true; | 
					
						
							|  |  |  | 						} else if (index > 0 && message.fromuid !== messages[index-1].fromuid) { | 
					
						
							|  |  |  | 							// If the previous message was from the other person, this is also a new set
 | 
					
						
							|  |  |  | 							message.newSet = true; | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 						return message; | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					next(undefined, messages); | 
					
						
							| 
									
										
										
										
											2015-12-21 15:40:53 +02:00
										 |  |  | 				} else if (messages.length === 1) { | 
					
						
							| 
									
										
										
										
											2015-12-15 17:50:30 +02:00
										 |  |  | 					// For single messages, we don't know the context, so look up the previous message and compare
 | 
					
						
							|  |  |  | 					var key = 'uid:' + uid + ':chat:room:' + roomId + ':mids'; | 
					
						
							|  |  |  | 					async.waterfall([ | 
					
						
							|  |  |  | 						async.apply(db.sortedSetRank, key, messages[0].messageId), | 
					
						
							|  |  |  | 						function(index, next) { | 
					
						
							|  |  |  | 							// Continue only if this isn't the first message in sorted set
 | 
					
						
							|  |  |  | 							if (index > 0) { | 
					
						
							|  |  |  | 								db.getSortedSetRange(key, index-1, index-1, next); | 
					
						
							|  |  |  | 							} else { | 
					
						
							| 
									
										
										
										
											2015-12-11 12:07:02 -05:00
										 |  |  | 								messages[0].newSet = true; | 
					
						
							| 
									
										
										
										
											2015-12-15 17:50:30 +02:00
										 |  |  | 								return next(undefined, messages); | 
					
						
							| 
									
										
										
										
											2015-12-11 12:07:02 -05:00
										 |  |  | 							} | 
					
						
							| 
									
										
										
										
											2015-12-15 17:50:30 +02:00
										 |  |  | 						}, | 
					
						
							|  |  |  | 						function(mid, next) { | 
					
						
							|  |  |  | 							Messaging.getMessageFields(mid, ['fromuid', 'timestamp'], next); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					], function(err, fields) { | 
					
						
							|  |  |  | 						if (err) { | 
					
						
							|  |  |  | 							return next(err); | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2015-12-11 12:07:02 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-15 17:50:30 +02:00
										 |  |  | 						if ( | 
					
						
							|  |  |  | 							(parseInt(messages[0].timestamp, 10) > parseInt(fields.timestamp, 10) + (1000*60*5)) || | 
					
						
							|  |  |  | 							(parseInt(messages[0].fromuid, 10) !== parseInt(fields.fromuid, 10)) | 
					
						
							|  |  |  | 						) { | 
					
						
							|  |  |  | 							// If it's been 5 minutes, this is a new set of messages
 | 
					
						
							|  |  |  | 							messages[0].newSet = true; | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 						next(undefined, messages); | 
					
						
							|  |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2015-12-21 15:40:53 +02:00
										 |  |  | 				} else { | 
					
						
							|  |  |  | 					next(null, []); | 
					
						
							| 
									
										
										
										
											2014-07-24 22:04:33 -04:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2015-12-15 17:50:30 +02:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		], callback); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-15 14:10:32 +02:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-08-26 13:18:20 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-15 17:50:30 +02:00
										 |  |  | 	Messaging.parse = function (message, fromuid, uid, roomId, isNew, callback) { | 
					
						
							| 
									
										
										
										
											2014-11-05 18:59:20 -05:00
										 |  |  | 		plugins.fireHook('filter:parse.raw', message, function(err, parsed) { | 
					
						
							| 
									
										
										
										
											2014-01-18 15:35:51 -05:00
										 |  |  | 			if (err) { | 
					
						
							| 
									
										
										
										
											2016-08-17 14:12:35 +03:00
										 |  |  | 				return callback(err); | 
					
						
							| 
									
										
										
										
											2014-01-18 15:35:51 -05:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-26 16:50:59 -05:00
										 |  |  | 			var messageData = { | 
					
						
							|  |  |  | 				message: message, | 
					
						
							|  |  |  | 				parsed: parsed, | 
					
						
							|  |  |  | 				fromuid: fromuid, | 
					
						
							| 
									
										
										
										
											2015-12-15 17:50:30 +02:00
										 |  |  | 				uid: uid, | 
					
						
							|  |  |  | 				roomId: roomId, | 
					
						
							| 
									
										
										
										
											2014-01-26 22:10:06 -05:00
										 |  |  | 				isNew: isNew, | 
					
						
							| 
									
										
										
										
											2014-03-28 12:13:01 -04:00
										 |  |  | 				parsedMessage: parsed | 
					
						
							| 
									
										
										
										
											2014-01-26 16:50:59 -05:00
										 |  |  | 			}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			plugins.fireHook('filter:messaging.parse', messageData, function(err, messageData) { | 
					
						
							| 
									
										
										
										
											2016-08-17 14:12:35 +03:00
										 |  |  | 				callback(err, messageData ? messageData.parsedMessage : ''); | 
					
						
							| 
									
										
										
										
											2014-01-26 16:50:59 -05:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2014-01-18 15:35:51 -05:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2014-01-15 18:20:05 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-16 21:52:42 +02:00
										 |  |  | 	Messaging.isNewSet = function(uid, roomId, timestamp, callback) { | 
					
						
							| 
									
										
										
										
											2015-12-15 17:50:30 +02:00
										 |  |  | 		var setKey = 'uid:' + uid + ':chat:room:' + roomId + ':mids'; | 
					
						
							| 
									
										
										
										
											2014-07-25 15:37:46 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		async.waterfall([ | 
					
						
							| 
									
										
										
										
											2015-12-16 21:52:42 +02:00
										 |  |  | 			function(next) { | 
					
						
							|  |  |  | 				db.getSortedSetRevRangeWithScores(setKey, 0, 0, next); | 
					
						
							| 
									
										
										
										
											2014-07-25 15:37:46 -04:00
										 |  |  | 			}, | 
					
						
							|  |  |  | 			function(messages, next) { | 
					
						
							| 
									
										
										
										
											2015-12-16 21:52:42 +02:00
										 |  |  | 				if (messages && messages.length) { | 
					
						
							|  |  |  | 					next(null, parseInt(timestamp, 10) > parseInt(messages[0].score, 10) + (1000 * 60 * 5)); | 
					
						
							| 
									
										
										
										
											2014-07-25 15:37:46 -04:00
										 |  |  | 				} else { | 
					
						
							| 
									
										
										
										
											2015-12-16 21:52:42 +02:00
										 |  |  | 					next(null, true); | 
					
						
							| 
									
										
										
										
											2014-07-25 15:37:46 -04:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2015-12-17 11:47:32 +02:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-07-25 15:37:46 -04:00
										 |  |  | 		], callback); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-05 17:35:44 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-31 23:40:58 -04:00
										 |  |  | 	Messaging.getRecentChats = function(uid, start, stop, callback) { | 
					
						
							| 
									
										
										
										
											2015-12-15 17:50:30 +02:00
										 |  |  | 		db.getSortedSetRevRange('uid:' + uid + ':chat:rooms', start, stop, function(err, roomIds) { | 
					
						
							| 
									
										
										
										
											2014-09-06 22:08:55 -04:00
										 |  |  | 			if (err) { | 
					
						
							| 
									
										
										
										
											2013-12-27 14:09:22 -05:00
										 |  |  | 				return callback(err); | 
					
						
							| 
									
										
										
										
											2013-12-05 17:35:44 -05:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-12-27 14:09:22 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-15 16:26:25 -04:00
										 |  |  | 			async.parallel({ | 
					
						
							| 
									
										
										
										
											2016-08-25 16:05:02 +03:00
										 |  |  | 				roomData: function(next) { | 
					
						
							|  |  |  | 					Messaging.getRoomsData(roomIds, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2014-09-15 16:26:25 -04:00
										 |  |  | 				unread: function(next) { | 
					
						
							| 
									
										
										
										
											2015-12-15 17:50:30 +02:00
										 |  |  | 					db.isSortedSetMembers('uid:' + uid + ':chat:rooms:unread', roomIds, next); | 
					
						
							| 
									
										
										
										
											2014-09-15 16:26:25 -04:00
										 |  |  | 				}, | 
					
						
							|  |  |  | 				users: function(next) { | 
					
						
							| 
									
										
										
										
											2015-12-15 17:50:30 +02:00
										 |  |  | 					async.map(roomIds, function(roomId, next) { | 
					
						
							| 
									
										
										
										
											2016-09-30 13:18:29 +03:00
										 |  |  | 						db.getSortedSetRevRange('chat:room:' + roomId + ':uids', 0, 9, function(err, uids) { | 
					
						
							| 
									
										
										
										
											2015-12-15 17:50:30 +02:00
										 |  |  | 							if (err) { | 
					
						
							|  |  |  | 								return next(err); | 
					
						
							|  |  |  | 							} | 
					
						
							| 
									
										
										
										
											2015-12-15 19:05:32 +02:00
										 |  |  | 							uids = uids.filter(function(value) { | 
					
						
							| 
									
										
										
										
											2015-12-15 17:50:30 +02:00
										 |  |  | 								return value && parseInt(value, 10) !== parseInt(uid, 10); | 
					
						
							|  |  |  | 							}); | 
					
						
							| 
									
										
										
										
											2016-08-25 16:05:02 +03:00
										 |  |  | 							user.getUsersFields(uids, ['uid', 'username', 'userslug', 'picture', 'status', 'lastonline'] , next); | 
					
						
							| 
									
										
										
										
											2015-12-15 17:50:30 +02:00
										 |  |  | 						}); | 
					
						
							|  |  |  | 					}, next); | 
					
						
							| 
									
										
										
										
											2015-08-27 17:58:29 -04:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2015-08-28 17:36:44 -04:00
										 |  |  | 				teasers: function(next) { | 
					
						
							| 
									
										
										
										
											2015-12-15 17:50:30 +02:00
										 |  |  | 					async.map(roomIds, function(roomId, next) { | 
					
						
							| 
									
										
										
										
											2015-12-17 12:35:49 +02:00
										 |  |  | 						Messaging.getTeaser(uid, roomId, next); | 
					
						
							| 
									
										
										
										
											2015-09-14 15:01:35 -04:00
										 |  |  | 					}, next); | 
					
						
							| 
									
										
										
										
											2014-09-15 16:26:25 -04:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			}, function(err, results) { | 
					
						
							| 
									
										
										
										
											2014-03-14 19:34:05 -04:00
										 |  |  | 				if (err) { | 
					
						
							| 
									
										
										
										
											2014-03-14 20:06:46 -04:00
										 |  |  | 					return callback(err); | 
					
						
							| 
									
										
										
										
											2014-03-14 19:34:05 -04:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2016-08-25 16:05:02 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				results.roomData.forEach(function(room, index) { | 
					
						
							|  |  |  | 					room.users = results.users[index]; | 
					
						
							|  |  |  | 					room.unread = results.unread[index]; | 
					
						
							|  |  |  | 					room.teaser = results.teasers[index]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					room.users.forEach(function(userData) { | 
					
						
							| 
									
										
										
										
											2015-12-15 17:50:30 +02:00
										 |  |  | 						if (userData && parseInt(userData.uid, 10)) { | 
					
						
							|  |  |  | 							userData.status = user.getStatus(userData); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2016-08-25 16:05:02 +03:00
										 |  |  | 					room.users = room.users.filter(function(user) { | 
					
						
							| 
									
										
										
										
											2015-12-15 17:50:30 +02:00
										 |  |  | 						return user && parseInt(user.uid, 10); | 
					
						
							|  |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2016-08-25 16:05:02 +03:00
										 |  |  | 					room.lastUser = room.users[0]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					room.usernames = room.users.map(function(user) { | 
					
						
							| 
									
										
										
										
											2015-12-17 11:47:32 +02:00
										 |  |  | 						return user.username; | 
					
						
							|  |  |  | 					}).join(', '); | 
					
						
							| 
									
										
										
										
											2014-09-15 16:26:25 -04:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-25 16:05:02 +03:00
										 |  |  | 				callback(null, {rooms: results.roomData, nextStart: stop + 1}); | 
					
						
							| 
									
										
										
										
											2014-03-14 19:34:05 -04:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2013-12-05 17:35:44 -05:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-17 12:35:49 +02:00
										 |  |  | 	Messaging.getTeaser = function (uid, roomId, callback) { | 
					
						
							|  |  |  | 		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], ['content', 'timestamp'], next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function (teaser, next) { | 
					
						
							|  |  |  | 				if (teaser && teaser.content) { | 
					
						
							|  |  |  | 					teaser.content = S(teaser.content).stripTags().decodeHTMLEntities().s; | 
					
						
							|  |  |  | 					teaser.timestampISO = utils.toISOString(teaser.timestamp); | 
					
						
							|  |  |  | 			 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				next(null, teaser); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		], callback); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-16 11:15:43 +02:00
										 |  |  | 	Messaging.canMessageUser = function(uid, toUid, callback) { | 
					
						
							|  |  |  | 		if (parseInt(meta.config.disableChat) === 1 || !uid || uid === toUid) { | 
					
						
							| 
									
										
										
										
											2016-02-29 10:36:20 +02:00
										 |  |  | 			return callback(new Error('[[error:chat-disabled]]')); | 
					
						
							| 
									
										
										
										
											2015-12-16 11:15:43 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-04-16 20:48:38 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-16 11:15:43 +02:00
										 |  |  | 		async.waterfall([ | 
					
						
							|  |  |  | 			function (next) { | 
					
						
							|  |  |  | 				user.exists(toUid, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function (exists, next) { | 
					
						
							|  |  |  | 				if (!exists) { | 
					
						
							| 
									
										
										
										
											2016-02-29 10:36:20 +02:00
										 |  |  | 					return callback(new Error('[[error:no-user]]')); | 
					
						
							| 
									
										
										
										
											2015-12-16 11:15:43 +02:00
										 |  |  | 				} | 
					
						
							|  |  |  | 				user.getUserFields(uid, ['banned', 'email:confirmed'], next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function (userData, next) { | 
					
						
							|  |  |  | 				if (parseInt(userData.banned, 10) === 1) { | 
					
						
							| 
									
										
										
										
											2016-02-29 10:36:20 +02:00
										 |  |  | 					return callback(new Error('[[error:user-banned]]')); | 
					
						
							| 
									
										
										
										
											2015-12-16 11:15:43 +02:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2014-07-19 10:33:27 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-16 11:15:43 +02:00
										 |  |  | 				if (parseInt(meta.config.requireEmailConfirmation, 10) === 1 && parseInt(userData['email:confirmed'], 10) !== 1) { | 
					
						
							| 
									
										
										
										
											2016-02-29 10:36:20 +02:00
										 |  |  | 					return callback(new Error('[[error:email-not-confirmed-chat]]')); | 
					
						
							| 
									
										
										
										
											2015-12-16 11:15:43 +02:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2015-04-16 20:48:38 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-29 10:36:20 +02:00
										 |  |  | 				async.parallel({ | 
					
						
							|  |  |  | 					settings: async.apply(user.getSettings, toUid), | 
					
						
							|  |  |  | 					isAdmin: async.apply(user.isAdministrator, uid), | 
					
						
							|  |  |  | 					isFollowing: async.apply(user.isFollowing, toUid, uid) | 
					
						
							|  |  |  | 				}, next); | 
					
						
							| 
									
										
										
										
											2015-12-16 11:15:43 +02:00
										 |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2016-02-29 10:36:20 +02:00
										 |  |  | 			function(results, next) { | 
					
						
							|  |  |  | 				if (!results.settings.restrictChat || results.isAdmin || results.isFollowing) { | 
					
						
							|  |  |  | 					return next(); | 
					
						
							| 
									
										
										
										
											2015-12-16 11:15:43 +02:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2014-09-16 13:27:26 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-29 10:36:20 +02:00
										 |  |  |  				next(new Error('[[error:chat-restricted]]')); | 
					
						
							| 
									
										
										
										
											2015-12-16 11:15:43 +02:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		], callback); | 
					
						
							| 
									
										
										
										
											2014-09-16 13:27:26 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-16 11:15:43 +02:00
										 |  |  | 	Messaging.canMessageRoom = function(uid, roomId, callback) { | 
					
						
							| 
									
										
										
										
											2015-12-15 19:05:32 +02:00
										 |  |  | 		if (parseInt(meta.config.disableChat) === 1 || !uid) { | 
					
						
							| 
									
										
										
										
											2016-02-01 21:09:35 +02:00
										 |  |  | 			return callback(new Error('[[error:chat-disabled]]')); | 
					
						
							| 
									
										
										
										
											2015-04-16 20:32:16 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-02-29 10:36:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-30 17:50:07 -04:00
										 |  |  | 		async.waterfall([ | 
					
						
							| 
									
										
										
										
											2015-10-02 17:26:12 -04:00
										 |  |  | 			function (next) { | 
					
						
							| 
									
										
										
										
											2015-12-15 20:00:51 +02:00
										 |  |  | 				Messaging.isUserInRoom(uid, roomId, next); | 
					
						
							| 
									
										
										
										
											2015-10-02 17:26:12 -04:00
										 |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2015-12-15 20:00:51 +02:00
										 |  |  | 			function (inRoom, next) { | 
					
						
							|  |  |  | 				if (!inRoom) { | 
					
						
							| 
									
										
										
										
											2016-02-01 21:09:35 +02:00
										 |  |  | 					return next(new Error('[[error:not-in-room]]')); | 
					
						
							| 
									
										
										
										
											2015-10-02 17:26:12 -04:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2016-02-29 10:36:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-23 00:52:43 +00:00
										 |  |  | 				Messaging.getUserCountInRoom(roomId, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function(count, next) { | 
					
						
							|  |  |  | 				if (count < 2) { | 
					
						
							|  |  |  | 					return next(new Error('[[error:no-users-in-room]]')); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2016-02-29 10:36:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-15 19:05:32 +02:00
										 |  |  | 				user.getUserFields(uid, ['banned', 'email:confirmed'], next); | 
					
						
							| 
									
										
										
										
											2015-10-02 17:26:12 -04:00
										 |  |  | 			}, | 
					
						
							|  |  |  | 			function (userData, next) { | 
					
						
							|  |  |  | 				if (parseInt(userData.banned, 10) === 1) { | 
					
						
							| 
									
										
										
										
											2016-02-01 21:09:35 +02:00
										 |  |  | 					return next(new Error('[[error:user-banned]]')); | 
					
						
							| 
									
										
										
										
											2015-10-02 17:26:12 -04:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2015-04-16 20:32:16 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-02 17:26:12 -04:00
										 |  |  | 				if (parseInt(meta.config.requireEmailConfirmation, 10) === 1 && parseInt(userData['email:confirmed'], 10) !== 1) { | 
					
						
							| 
									
										
										
										
											2016-02-01 21:09:35 +02:00
										 |  |  | 					return next(new Error('[[error:email-not-confirmed-chat]]')); | 
					
						
							| 
									
										
										
										
											2015-10-02 17:26:12 -04:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2015-04-16 20:32:16 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-01 21:09:35 +02:00
										 |  |  | 				next(); | 
					
						
							| 
									
										
										
										
											2014-10-30 17:50:07 -04:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-11-06 12:57:55 -05:00
										 |  |  | 		], callback); | 
					
						
							|  |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2014-10-30 17:50:07 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-18 15:35:24 +02:00
										 |  |  | 	Messaging.hasPrivateChat = function(uid, withUid, callback) { | 
					
						
							|  |  |  | 		async.waterfall([ | 
					
						
							|  |  |  | 			function (next) { | 
					
						
							|  |  |  | 				async.parallel({ | 
					
						
							|  |  |  | 					myRooms: async.apply(db.getSortedSetRevRange, 'uid:' + uid + ':chat:rooms', 0, -1), | 
					
						
							|  |  |  | 					theirRooms: async.apply(db.getSortedSetRevRange, 'uid:' + withUid + ':chat:rooms', 0, -1) | 
					
						
							|  |  |  | 				}, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function (results, next) { | 
					
						
							|  |  |  | 				var roomIds = results.myRooms.filter(function(roomId) { | 
					
						
							|  |  |  | 					return roomId && results.theirRooms.indexOf(roomId) !== -1; | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if (!roomIds.length) { | 
					
						
							|  |  |  | 					return callback(); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				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 { | 
					
						
							|  |  |  | 							++ index; | 
					
						
							|  |  |  | 							next(); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2016-02-16 20:25:23 +02:00
										 |  |  | 				}, function(err) { | 
					
						
							|  |  |  | 					next(err, roomId); | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2016-01-18 15:35:24 +02:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		], callback); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-11 12:07:02 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-10 20:31:57 +01:00
										 |  |  | }(exports)); |