| 
									
										
										
										
											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) { | 
					
						
							| 
									
										
										
										
											2015-12-15 17:50:30 +02:00
										 |  |  | 		var uid = params.uid, | 
					
						
							|  |  |  | 			roomId = params.roomId, | 
					
						
							| 
									
										
										
										
											2015-08-27 17:32:33 -04:00
										 |  |  | 			since = params.since, | 
					
						
							|  |  |  | 			isNew = params.isNew, | 
					
						
							| 
									
										
										
										
											2015-10-08 18:29:57 -04:00
										 |  |  | 			count = params.count || parseInt(meta.config.chatMessageInboxSize, 10) || 250, | 
					
						
							|  |  |  | 			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') { | 
					
						
							|  |  |  | 			count = 49; | 
					
						
							|  |  |  | 			min = 0; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-15 17:50:30 +02:00
										 |  |  | 		db.getSortedSetRevRangeByScore('uid:' + uid + ':chat:room:' + roomId + ':mids', 0, 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, []); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-29 17:12:02 -04:00
										 |  |  | 			mids.reverse(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-15 17:50:30 +02:00
										 |  |  | 			Messaging.getMessagesData(mids, uid, roomId, isNew, callback); | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  | 					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) { | 
					
						
							|  |  |  | 					Messaging.parse(message.content, message.fromuid, uid, roomId, isNew, function(result) { | 
					
						
							|  |  |  | 						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); | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					// 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); | 
					
						
							|  |  |  | 					}); | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							|  |  |  | 				return callback(message); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							|  |  |  | 				callback(messageData.parsedMessage); | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2014-01-18 15:35:51 -05:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2014-01-15 18:20:05 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-15 17:50:30 +02:00
										 |  |  | 	Messaging.isNewSet = function(uid, roomId, mid, callback) { | 
					
						
							|  |  |  | 		var setKey = 'uid:' + uid + ':chat:room:' + roomId + ':mids'; | 
					
						
							| 
									
										
										
										
											2014-07-25 15:37:46 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		async.waterfall([ | 
					
						
							|  |  |  | 			async.apply(db.sortedSetRank, setKey, mid), | 
					
						
							|  |  |  | 			function(index, next) { | 
					
						
							|  |  |  | 				if (index > 0) { | 
					
						
							| 
									
										
										
										
											2015-12-15 17:50:30 +02:00
										 |  |  | 					db.getSortedSetRange(setKey, index - 1, index, next); | 
					
						
							| 
									
										
										
										
											2014-07-25 15:37:46 -04:00
										 |  |  | 				} else { | 
					
						
							|  |  |  | 					next(null, true); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function(mids, next) { | 
					
						
							| 
									
										
										
										
											2015-05-13 13:02:00 -04:00
										 |  |  | 				if (typeof mids !== 'boolean' && mids && mids.length) { | 
					
						
							| 
									
										
										
										
											2014-08-12 09:17:41 -04:00
										 |  |  | 					db.getObjects(['message:' + mids[0], 'message:' + mids[1]], next); | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					next(null, mids); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2014-07-25 15:37:46 -04:00
										 |  |  | 			}, | 
					
						
							|  |  |  | 			function(messages, next) { | 
					
						
							| 
									
										
										
										
											2015-05-13 13:02:00 -04:00
										 |  |  | 				if (typeof messages !== 'boolean' && messages && messages.length) { | 
					
						
							| 
									
										
										
										
											2014-07-25 15:37:46 -04:00
										 |  |  | 					next(null, parseInt(messages[1].timestamp, 10) > parseInt(messages[0].timestamp, 10) + (1000*60*5)); | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					next(null, messages); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		], 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({ | 
					
						
							|  |  |  | 				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) { | 
					
						
							|  |  |  | 						db.getSortedSetRevRange('chat:room:' + roomId + ':uids', 0, 3, function(err, uids) { | 
					
						
							|  |  |  | 							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); | 
					
						
							|  |  |  | 							}); | 
					
						
							|  |  |  | 							user.getUsersFields(uids, ['uid', 'username', 'picture', 'status', 'lastonline'] , next); | 
					
						
							|  |  |  | 						}); | 
					
						
							|  |  |  | 					}, 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-08-27 17:58:29 -04:00
										 |  |  | 						Messaging.getMessages({ | 
					
						
							| 
									
										
										
										
											2015-12-15 17:50:30 +02:00
										 |  |  | 							uid: uid, | 
					
						
							|  |  |  | 							roomId: roomId, | 
					
						
							| 
									
										
										
										
											2015-08-27 17:58:29 -04:00
										 |  |  | 							isNew: false, | 
					
						
							| 
									
										
										
										
											2015-10-08 18:29:57 -04:00
										 |  |  | 							count: 1, | 
					
						
							|  |  |  | 							markRead: false | 
					
						
							| 
									
										
										
										
											2015-08-27 17:58:29 -04:00
										 |  |  | 						}, function(err, teaser) { | 
					
						
							| 
									
										
										
										
											2015-12-11 20:04:27 -05:00
										 |  |  | 							teaser = teaser[0]; | 
					
						
							| 
									
										
										
										
											2015-12-16 13:35:24 +02:00
										 |  |  | 							if (teaser && teaser.content) { | 
					
						
							|  |  |  | 								teaser.content = S(teaser.content).stripTags().decodeHTMLEntities().s; | 
					
						
							|  |  |  | 							} | 
					
						
							| 
									
										
										
										
											2015-09-16 15:44:10 -04:00
										 |  |  | 							next(err, teaser); | 
					
						
							| 
									
										
										
										
											2015-08-27 17:58:29 -04:00
										 |  |  | 						}); | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2015-12-15 17:50:30 +02:00
										 |  |  | 				var rooms = results.users.map(function(users, index) { | 
					
						
							|  |  |  | 					var data = { | 
					
						
							|  |  |  | 						users: users, | 
					
						
							|  |  |  | 						unread: results.unread[index], | 
					
						
							|  |  |  | 						roomId: roomIds[index], | 
					
						
							|  |  |  | 						teaser: results.teasers[index] | 
					
						
							|  |  |  | 					}; | 
					
						
							|  |  |  | 					data.users.forEach(function(userData) { | 
					
						
							|  |  |  | 						if (userData && parseInt(userData.uid, 10)) { | 
					
						
							|  |  |  | 							userData.status = user.getStatus(userData); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 					data.users = data.users.filter(function(user) { | 
					
						
							|  |  |  | 						return user && parseInt(user.uid, 10); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 					return data; | 
					
						
							| 
									
										
										
										
											2014-09-15 16:26:25 -04:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-15 17:50:30 +02:00
										 |  |  | 				callback(null, {rooms: rooms, nextStart: stop + 1}); | 
					
						
							| 
									
										
										
										
											2014-03-14 19:34:05 -04:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2013-12-05 17:35:44 -05:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-16 11:15:43 +02:00
										 |  |  | 	Messaging.canMessageUser = function(uid, toUid, callback) { | 
					
						
							|  |  |  | 		if (parseInt(meta.config.disableChat) === 1 || !uid || uid === toUid) { | 
					
						
							|  |  |  | 			return callback(null, false); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							|  |  |  | 					return callback(null, false); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				user.getUserFields(uid, ['banned', 'email:confirmed'], next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function (userData, next) { | 
					
						
							|  |  |  | 				if (parseInt(userData.banned, 10) === 1) { | 
					
						
							|  |  |  | 					return callback(null, false); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							|  |  |  | 					return callback(null, false); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2015-04-16 20:48:38 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-16 11:15:43 +02:00
										 |  |  | 				user.getSettings(toUid, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function(settings, next) { | 
					
						
							|  |  |  | 				if (!settings.restrictChat) { | 
					
						
							|  |  |  | 					return callback(null, true); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2014-09-16 13:27:26 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-16 11:15:43 +02:00
										 |  |  | 				user.isAdministrator(uid, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function(isAdmin, next) { | 
					
						
							|  |  |  | 				if (isAdmin) { | 
					
						
							|  |  |  | 					return callback(null, true); | 
					
						
							| 
									
										
										
										
											2014-09-16 13:27:26 -04:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2015-12-16 11:15:43 +02:00
										 |  |  | 				user.isFollowing(toUid, uid, next); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		], 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) { | 
					
						
							| 
									
										
										
										
											2015-10-27 05:25:14 -04:00
										 |  |  | 			return callback(null, false); | 
					
						
							| 
									
										
										
										
											2015-04-16 20:32:16 -04: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) { | 
					
						
							| 
									
										
										
										
											2015-10-27 05:25:14 -04:00
										 |  |  | 					return callback(null, false); | 
					
						
							| 
									
										
										
										
											2015-10-02 17:26:12 -04: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) { | 
					
						
							| 
									
										
										
										
											2015-10-27 05:25:14 -04:00
										 |  |  | 					return callback(null, false); | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							| 
									
										
										
										
											2015-10-27 05:25:14 -04:00
										 |  |  | 					return callback(null, false); | 
					
						
							| 
									
										
										
										
											2015-10-02 17:26:12 -04:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2015-04-16 20:32:16 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-15 19:05:32 +02:00
										 |  |  | 				next(null, true); | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-11 12:07:02 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-10 20:31:57 +01:00
										 |  |  | }(exports)); |