| 
									
										
										
										
											2014-03-08 15:45:42 -05:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-02 21:20:55 -05:00
										 |  |  | var db = require('./database'), | 
					
						
							| 
									
										
										
										
											2013-10-17 12:05:53 -04:00
										 |  |  | 	async = require('async'), | 
					
						
							| 
									
										
										
										
											2014-09-16 13:27:26 -04:00
										 |  |  | 	nconf = require('nconf'), | 
					
						
							| 
									
										
										
										
											2014-07-25 16:46:04 -04:00
										 |  |  | 	winston = require('winston'), | 
					
						
							| 
									
										
										
										
											2015-09-16 15:44:10 -04:00
										 |  |  | 	S = require('string'), | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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'), | 
					
						
							| 
									
										
										
										
											2014-09-16 13:27:26 -04:00
										 |  |  | 	userNotifications = require('./user/notifications'), | 
					
						
							| 
									
										
										
										
											2015-04-16 20:48:38 -04:00
										 |  |  | 	emailer = require('./emailer'), | 
					
						
							|  |  |  | 	sockets = require('./socket.io'); | 
					
						
							| 
									
										
										
										
											2013-08-26 13:18:20 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | (function(Messaging) { | 
					
						
							| 
									
										
										
										
											2014-09-16 13:27:26 -04:00
										 |  |  | 	Messaging.notifyQueue = {};	// Only used to notify a user of a new chat message, see Messaging.notifyUser
 | 
					
						
							| 
									
										
										
										
											2013-08-26 13:18:20 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-30 16:51:15 -04:00
										 |  |  | 	var terms = { | 
					
						
							|  |  |  | 		day: 86400000, | 
					
						
							|  |  |  | 		week: 604800000, | 
					
						
							|  |  |  | 		month: 2592000000, | 
					
						
							|  |  |  | 		threemonths: 7776000000 | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-26 13:18:20 -04:00
										 |  |  | 	function sortUids(fromuid, touid) { | 
					
						
							| 
									
										
										
										
											2014-03-14 20:06:46 -04:00
										 |  |  | 		return [fromuid, touid].sort(); | 
					
						
							| 
									
										
										
										
											2013-08-26 13:18:20 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-17 14:38:01 -04:00
										 |  |  | 	Messaging.addMessage = function(fromuid, touid, content, timestamp, callback) { | 
					
						
							|  |  |  | 		if (typeof timestamp === 'function') { | 
					
						
							|  |  |  | 			callback = timestamp; | 
					
						
							|  |  |  | 			timestamp = Date.now(); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			timestamp = timestamp || Date.now(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-14 17:42:06 -04:00
										 |  |  | 		if (!content) { | 
					
						
							|  |  |  | 			return callback(new Error('[[error:invalid-chat-message]]')); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (content.length > (meta.config.maximumChatMessageLength || 1000)) { | 
					
						
							|  |  |  | 			return callback(new Error('[[error:chat-message-too-long]]')); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		var uids = sortUids(fromuid, touid); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-02 21:20:55 -05:00
										 |  |  | 		db.incrObjectField('global', 'nextMid', function(err, mid) { | 
					
						
							|  |  |  | 			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
										 |  |  | 			var message = { | 
					
						
							|  |  |  | 				content: content, | 
					
						
							| 
									
										
										
										
											2014-07-24 22:38:35 -04:00
										 |  |  | 				timestamp: timestamp, | 
					
						
							| 
									
										
										
										
											2013-08-26 13:18:20 -04:00
										 |  |  | 				fromuid: fromuid, | 
					
						
							|  |  |  | 				touid: touid | 
					
						
							|  |  |  | 			}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-25 18:35:33 -04:00
										 |  |  | 			async.waterfall([ | 
					
						
							|  |  |  | 				function(next) { | 
					
						
							|  |  |  | 					plugins.fireHook('filter:messaging.save', message, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function(message, next) { | 
					
						
							|  |  |  | 					db.setObject('message:' + mid, message, next); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			], function(err) { | 
					
						
							| 
									
										
										
										
											2014-03-28 12:13:01 -04:00
										 |  |  | 				if (err) { | 
					
						
							|  |  |  | 					return callback(err); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2013-08-26 13:18:20 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-25 18:35:33 -04:00
										 |  |  | 				async.parallel([ | 
					
						
							|  |  |  | 					async.apply(db.sortedSetAdd, 'messages:uid:' + uids[0] + ':to:' + uids[1], timestamp, mid), | 
					
						
							|  |  |  | 					async.apply(Messaging.updateChatTime, fromuid, touid), | 
					
						
							|  |  |  | 					async.apply(Messaging.updateChatTime, touid, fromuid), | 
					
						
							|  |  |  | 					async.apply(Messaging.markRead, fromuid, touid), | 
					
						
							|  |  |  | 					async.apply(Messaging.markUnread, touid, fromuid), | 
					
						
							| 
									
										
										
										
											2015-10-02 17:26:12 -04:00
										 |  |  | 				], function(err) { | 
					
						
							| 
									
										
										
										
											2014-07-09 12:43:02 -04:00
										 |  |  | 					if (err) { | 
					
						
							|  |  |  | 						return callback(err); | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2014-03-28 12:13:01 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-25 18:35:33 -04:00
										 |  |  | 					async.waterfall([ | 
					
						
							| 
									
										
										
										
											2014-07-19 10:33:27 -04:00
										 |  |  | 						function(next) { | 
					
						
							| 
									
										
										
										
											2014-07-25 18:35:33 -04:00
										 |  |  | 							getMessages([mid], fromuid, touid, true, next); | 
					
						
							| 
									
										
										
										
											2014-07-19 10:33:27 -04:00
										 |  |  | 						}, | 
					
						
							| 
									
										
										
										
											2014-07-25 18:35:33 -04:00
										 |  |  | 						function(messages, next) { | 
					
						
							| 
									
										
										
										
											2014-07-25 15:37:46 -04:00
										 |  |  | 							Messaging.isNewSet(fromuid, touid, mid, function(err, isNewSet) { | 
					
						
							| 
									
										
										
										
											2014-07-25 18:35:33 -04:00
										 |  |  | 								if (err) { | 
					
						
							|  |  |  | 									return next(err); | 
					
						
							|  |  |  | 								} | 
					
						
							| 
									
										
										
										
											2014-12-13 08:19:49 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 								if (!messages || !messages[0]) { | 
					
						
							|  |  |  | 									return next(null, null); | 
					
						
							|  |  |  | 								} | 
					
						
							| 
									
										
										
										
											2014-12-31 14:16:27 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-25 15:37:46 -04:00
										 |  |  | 								messages[0].newSet = isNewSet; | 
					
						
							| 
									
										
										
										
											2014-12-13 08:19:49 -05:00
										 |  |  | 								messages[0].mid = mid; | 
					
						
							|  |  |  | 								next(null, messages[0]); | 
					
						
							| 
									
										
										
										
											2014-07-25 15:37:46 -04:00
										 |  |  | 							}); | 
					
						
							| 
									
										
										
										
											2014-07-25 18:35:33 -04:00
										 |  |  | 						} | 
					
						
							|  |  |  | 					], callback); | 
					
						
							| 
									
										
										
										
											2014-03-28 12:13:01 -04:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2013-08-26 13:18:20 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-03-08 15:45:42 -05:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-08-26 13:18:20 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-11 12:07:02 -05:00
										 |  |  | 	Messaging.editMessage = function(mid, content, callback) { | 
					
						
							|  |  |  | 		async.series([ | 
					
						
							|  |  |  | 			function(next) { | 
					
						
							|  |  |  | 				// Verify that the message actually changed
 | 
					
						
							|  |  |  | 				Messaging.getMessageField(mid, 'content', function(err, raw) { | 
					
						
							|  |  |  | 					if (raw === content) { | 
					
						
							|  |  |  | 						// No dice.
 | 
					
						
							|  |  |  | 						return callback(); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					next(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			async.apply(Messaging.setMessageFields, mid, { | 
					
						
							|  |  |  | 				content: content, | 
					
						
							|  |  |  | 				edited: Date.now() | 
					
						
							|  |  |  | 			}), | 
					
						
							|  |  |  | 			function(next) { | 
					
						
							|  |  |  | 				Messaging.getMessageFields(mid, ['fromuid', 'touid'], function(err, data) { | 
					
						
							|  |  |  | 					getMessages([mid], data.fromuid, data.touid, true, function(err, messages) { | 
					
						
							|  |  |  | 						sockets.in('uid_' + data.fromuid).emit('event:chats.edit', { | 
					
						
							|  |  |  | 							messages: messages | 
					
						
							|  |  |  | 						}); | 
					
						
							|  |  |  | 						sockets.in('uid_' + data.touid).emit('event:chats.edit', { | 
					
						
							|  |  |  | 							messages: messages | 
					
						
							|  |  |  | 						}); | 
					
						
							|  |  |  | 						next(); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		], callback); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Messaging.deleteMessage = function(mid, callback) { | 
					
						
							|  |  |  | 		var uids = []; | 
					
						
							|  |  |  | 		async.series([ | 
					
						
							|  |  |  | 			function(next) { | 
					
						
							|  |  |  | 				db.getObject('message:' + mid, function(err, messageObj) { | 
					
						
							|  |  |  | 					messageObj.fromuid = parseInt(messageObj.fromuid, 10); | 
					
						
							|  |  |  | 					messageObj.touid = parseInt(messageObj.touid, 10); | 
					
						
							|  |  |  | 					uids.push(messageObj.fromuid, messageObj.touid); | 
					
						
							|  |  |  | 					uids.sort(function(a, b) { | 
					
						
							|  |  |  | 						return a > b ? 1 : -1; | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 					next(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function(next) { | 
					
						
							|  |  |  | 				next(); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function(next) { | 
					
						
							|  |  |  | 				db.sortedSetRemove('messages:uid:' + uids[0] + ':to:' + uids[1], mid, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			async.apply(db.delete, 'message:' + mid) | 
					
						
							|  |  |  | 		], callback); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Messaging.getMessageField = function(mid, field, callback) { | 
					
						
							|  |  |  | 		Messaging.getMessageFields(mid, [field], function(err, fields) { | 
					
						
							|  |  |  | 			callback(err, fields[field]); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	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) { | 
					
						
							|  |  |  | 		var fromuid = params.fromuid, | 
					
						
							|  |  |  | 			touid = params.touid, | 
					
						
							|  |  |  | 			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
										 |  |  | 
 | 
					
						
							|  |  |  | 		var uids = sortUids(fromuid, touid), | 
					
						
							| 
									
										
										
										
											2015-08-27 17:37:34 -04:00
										 |  |  | 			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; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-07 19:40:41 -05:00
										 |  |  | 		db.getSortedSetRevRangeByScore('messages:uid:' + uids[0] + ':to:' + uids[1], 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(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-28 12:13:01 -04:00
										 |  |  | 			getMessages(mids, fromuid, touid, isNew, callback); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-07-25 16:46:04 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-08 18:29:57 -04:00
										 |  |  | 		if (markRead) { | 
					
						
							|  |  |  | 			notifications.markRead('chat_' + touid + '_' + fromuid, fromuid, function(err) { | 
					
						
							|  |  |  | 				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-10-08 18:29:57 -04:00
										 |  |  | 				userNotifications.pushCount(fromuid); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-03-28 12:13:01 -04:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-08-26 13:18:20 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-28 12:13:01 -04:00
										 |  |  | 	function getMessages(mids, fromuid, touid, isNew, callback) { | 
					
						
							| 
									
										
										
										
											2015-10-29 16:35:44 -04:00
										 |  |  | 		user.getUsersFields([fromuid, touid], ['uid', 'username', 'userslug', 'picture', 'status'], function(err, userData) { | 
					
						
							| 
									
										
										
										
											2014-03-28 12:13:01 -04:00
										 |  |  | 			if(err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-01-26 22:10:06 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-28 12:13:01 -04:00
										 |  |  | 			var keys = mids.map(function(mid) { | 
					
						
							|  |  |  | 				return 'message:' + mid; | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2013-08-26 13:18:20 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-24 22:04:33 -04:00
										 |  |  | 			async.waterfall([ | 
					
						
							|  |  |  | 				async.apply(db.getObjects, keys), | 
					
						
							|  |  |  | 				function(messages, next) { | 
					
						
							| 
									
										
										
										
											2015-12-11 12:07:02 -05:00
										 |  |  | 					messages = messages.map(function(msg, idx) { | 
					
						
							|  |  |  | 						if (msg) { | 
					
						
							|  |  |  | 							msg.messageId = parseInt(mids[idx], 10); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 						return msg; | 
					
						
							|  |  |  | 					}).filter(Boolean); | 
					
						
							| 
									
										
										
										
											2014-07-24 22:04:33 -04:00
										 |  |  | 					async.map(messages, function(message, next) { | 
					
						
							|  |  |  | 						var self = parseInt(message.fromuid, 10) === parseInt(fromuid, 10); | 
					
						
							|  |  |  | 						message.fromUser = self ? userData[0] : userData[1]; | 
					
						
							|  |  |  | 						message.toUser = self ? userData[1] : userData[0]; | 
					
						
							| 
									
										
										
										
											2014-08-08 15:28:39 -04:00
										 |  |  | 						message.timestampISO = utils.toISOString(message.timestamp); | 
					
						
							| 
									
										
										
										
											2014-07-24 22:04:33 -04:00
										 |  |  | 						message.self = self ? 1 : 0; | 
					
						
							|  |  |  | 						message.newSet = false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-11 12:07:02 -05:00
										 |  |  | 						if (message.hasOwnProperty('edited')) { | 
					
						
							|  |  |  | 							message.editedISO = new Date(parseInt(message.edited, 10)).toISOString(); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-24 22:04:33 -04:00
										 |  |  | 						Messaging.parse(message.content, message.fromuid, fromuid, userData[1], userData[0], isNew, function(result) { | 
					
						
							|  |  |  | 							message.content = result; | 
					
						
							| 
									
										
										
										
											2015-09-16 18:14:33 -04:00
										 |  |  | 							message.cleanedContent = S(result).stripTags().decodeHTMLEntities().s; | 
					
						
							| 
									
										
										
										
											2014-07-24 22:04:33 -04:00
										 |  |  | 							next(null, message); | 
					
						
							|  |  |  | 						}); | 
					
						
							|  |  |  | 					}, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function(messages, next) { | 
					
						
							| 
									
										
										
										
											2015-12-11 12:07:02 -05:00
										 |  |  | 					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; | 
					
						
							|  |  |  | 						}); | 
					
						
							| 
									
										
										
										
											2014-07-24 22:04:33 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-11 12:07:02 -05:00
										 |  |  | 						next(undefined, messages); | 
					
						
							|  |  |  | 					} else { | 
					
						
							|  |  |  | 						// For single messages, we don't know the context, so look up the previous message and compare
 | 
					
						
							|  |  |  | 						var uids = [fromuid, touid].sort(function(a, b) { return a > b ? 1 : -1 }); | 
					
						
							|  |  |  | 						var key = 'messages:uid:' + uids[0] + ':to:' + uids[1]; | 
					
						
							|  |  |  | 						async.waterfall([ | 
					
						
							|  |  |  | 							async.apply(db.sortedSetRank, key, messages[0].messageId), | 
					
						
							|  |  |  | 							function(index, next) { | 
					
						
							|  |  |  | 								db.getSortedSetRange(key, index-1, index-1, next); | 
					
						
							|  |  |  | 							}, | 
					
						
							|  |  |  | 							function(mid, next) { | 
					
						
							|  |  |  | 								Messaging.getMessageFields(mid, ['fromuid', 'timestamp'], next); | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 						], function(err, fields) { | 
					
						
							|  |  |  | 							if (err) { | 
					
						
							|  |  |  | 								return next(err); | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 							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
										 |  |  | 				} | 
					
						
							|  |  |  | 			], callback); | 
					
						
							| 
									
										
										
										
											2013-08-26 13:18:20 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-03-28 12:13:01 -04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-08-26 13:18:20 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-26 22:10:06 -05:00
										 |  |  | 	Messaging.parse = function (message, fromuid, myuid, toUserData, myUserData, 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, | 
					
						
							|  |  |  | 				myuid: myuid, | 
					
						
							|  |  |  | 				toUserData: toUserData, | 
					
						
							|  |  |  | 				myUserData: myUserData, | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-25 15:37:46 -04:00
										 |  |  | 	Messaging.isNewSet = function(fromuid, touid, mid, callback) { | 
					
						
							|  |  |  | 		var uids = sortUids(fromuid, touid), | 
					
						
							|  |  |  | 			setKey = 'messages:uid:' + uids[0] + ':to:' + uids[1]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		async.waterfall([ | 
					
						
							|  |  |  | 			async.apply(db.sortedSetRank, setKey, mid), | 
					
						
							|  |  |  | 			function(index, next) { | 
					
						
							|  |  |  | 				if (index > 0) { | 
					
						
							|  |  |  | 					db.getSortedSetRange(setKey, index-1, index, next); | 
					
						
							|  |  |  | 				} 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
										 |  |  | 	Messaging.updateChatTime = function(uid, toUid, callback) { | 
					
						
							| 
									
										
										
										
											2014-07-19 10:33:27 -04:00
										 |  |  | 		callback = callback || function() {}; | 
					
						
							|  |  |  | 		db.sortedSetAdd('uid:' + uid + ':chats', Date.now(), toUid, callback); | 
					
						
							| 
									
										
										
										
											2013-12-05 17:35:44 -05:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-31 23:40:58 -04:00
										 |  |  | 	Messaging.getRecentChats = function(uid, start, stop, callback) { | 
					
						
							|  |  |  | 		db.getSortedSetRevRange('uid:' + uid + ':chats', start, stop, function(err, uids) { | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							|  |  |  | 					db.isSortedSetMembers('uid:' + uid + ':chats:unread', uids, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				users: function(next) { | 
					
						
							| 
									
										
										
										
											2015-11-05 12:34:39 -05:00
										 |  |  | 					user.getUsersFields(uids, ['uid', 'username', 'picture', 'status', 'lastonline'] , next); | 
					
						
							| 
									
										
										
										
											2015-08-27 17:58:29 -04:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2015-08-28 17:36:44 -04:00
										 |  |  | 				teasers: function(next) { | 
					
						
							| 
									
										
										
										
											2015-09-14 15:01:35 -04:00
										 |  |  | 					async.map(uids, function(fromuid, next) { | 
					
						
							| 
									
										
										
										
											2015-08-27 17:58:29 -04:00
										 |  |  | 						Messaging.getMessages({ | 
					
						
							|  |  |  | 							fromuid: fromuid, | 
					
						
							|  |  |  | 							touid: uid, | 
					
						
							|  |  |  | 							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 19:50:21 -05:00
										 |  |  | 							var teaser = teaser[0]; | 
					
						
							| 
									
										
										
										
											2015-09-16 15:44:10 -04:00
										 |  |  | 							teaser.content = S(teaser.content).stripTags().decodeHTMLEntities().s; | 
					
						
							|  |  |  | 							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-11-05 12:34:39 -05:00
										 |  |  | 				results.users.forEach(function(userData, index) { | 
					
						
							|  |  |  | 					if (userData && parseInt(userData.uid, 10)) { | 
					
						
							|  |  |  | 						userData.unread = results.unread[index]; | 
					
						
							|  |  |  | 						userData.status = user.getStatus(userData); | 
					
						
							|  |  |  | 						userData.teaser = results.teasers[index]; | 
					
						
							| 
									
										
										
										
											2015-04-22 10:41:44 -04:00
										 |  |  | 					} | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-15 16:26:25 -04:00
										 |  |  | 				results.users = results.users.filter(function(user) { | 
					
						
							|  |  |  | 					return user && parseInt(user.uid, 10); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-31 23:40:58 -04:00
										 |  |  | 				callback(null, {users: results.users, nextStart: stop + 1}); | 
					
						
							| 
									
										
										
										
											2014-03-14 19:34:05 -04:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2013-12-05 17:35:44 -05:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-19 10:33:27 -04:00
										 |  |  | 	Messaging.getUnreadCount = function(uid, callback) { | 
					
						
							|  |  |  | 		db.sortedSetCard('uid:' + uid + ':chats:unread', callback); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-16 20:48:38 -04:00
										 |  |  | 	Messaging.pushUnreadCount = function(uid) { | 
					
						
							|  |  |  | 		Messaging.getUnreadCount(uid, function(err, unreadCount) { | 
					
						
							|  |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2015-10-20 19:19:50 -04:00
										 |  |  | 			sockets.in('uid_' + uid).emit('event:unread.updateChatCount', unreadCount); | 
					
						
							| 
									
										
										
										
											2015-04-16 20:48:38 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-19 10:33:27 -04:00
										 |  |  | 	Messaging.markRead = function(uid, toUid, callback) { | 
					
						
							|  |  |  | 		db.sortedSetRemove('uid:' + uid + ':chats:unread', toUid, callback); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Messaging.markUnread = function(uid, toUid, callback) { | 
					
						
							| 
									
										
										
										
											2015-10-02 17:31:18 -04:00
										 |  |  | 		async.waterfall([ | 
					
						
							|  |  |  | 			function (next) { | 
					
						
							|  |  |  | 				user.exists(toUid, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function (exists, next) { | 
					
						
							|  |  |  | 				if (!exists) { | 
					
						
							|  |  |  | 					return next(new Error('[[error:no-user]]')); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				db.sortedSetAdd('uid:' + uid + ':chats:unread', Date.now(), toUid, next); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		], callback); | 
					
						
							| 
									
										
										
										
											2014-07-19 10:33:27 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-16 13:27:26 -04:00
										 |  |  | 	Messaging.notifyUser = function(fromuid, touid, messageObj) { | 
					
						
							| 
									
										
										
										
											2015-04-16 20:48:38 -04:00
										 |  |  | 		// Immediate notifications
 | 
					
						
							|  |  |  | 		// Recipient
 | 
					
						
							|  |  |  | 		Messaging.pushUnreadCount(touid); | 
					
						
							|  |  |  | 		sockets.in('uid_' + touid).emit('event:chats.receive', { | 
					
						
							|  |  |  | 			withUid: fromuid, | 
					
						
							|  |  |  | 			message: messageObj, | 
					
						
							|  |  |  | 			self: 0 | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 		// Sender
 | 
					
						
							|  |  |  | 		Messaging.pushUnreadCount(fromuid); | 
					
						
							|  |  |  | 		sockets.in('uid_' + fromuid).emit('event:chats.receive', { | 
					
						
							|  |  |  | 			withUid: touid, | 
					
						
							|  |  |  | 			message: messageObj, | 
					
						
							|  |  |  | 			self: 1 | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Delayed notifications
 | 
					
						
							| 
									
										
										
										
											2014-09-16 13:27:26 -04:00
										 |  |  | 		var queueObj = Messaging.notifyQueue[fromuid + ':' + touid]; | 
					
						
							|  |  |  | 		if (queueObj) { | 
					
						
							|  |  |  | 			queueObj.message.content += '\n' + messageObj.content; | 
					
						
							|  |  |  | 			clearTimeout(queueObj.timeout); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			queueObj = Messaging.notifyQueue[fromuid + ':' + touid] = { | 
					
						
							|  |  |  | 				message: messageObj | 
					
						
							|  |  |  | 			}; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		queueObj.timeout = setTimeout(function() { | 
					
						
							|  |  |  | 			sendNotifications(fromuid, touid, queueObj.message, function(err) { | 
					
						
							|  |  |  | 				if (!err) { | 
					
						
							|  |  |  | 					delete Messaging.notifyQueue[fromuid + ':' + touid]; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}, 1000*60);	// wait 60s before sending
 | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-30 17:50:07 -04:00
										 |  |  | 	Messaging.canMessage = function(fromUid, toUid, callback) { | 
					
						
							| 
									
										
										
										
											2015-10-27 05:25:14 -04:00
										 |  |  | 		if (parseInt(meta.config.disableChat) === 1 || !fromUid || toUid === fromUid) { | 
					
						
							|  |  |  | 			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) { | 
					
						
							|  |  |  | 				user.exists(toUid, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function (exists, next) { | 
					
						
							|  |  |  | 				if (!exists) { | 
					
						
							| 
									
										
										
										
											2015-10-27 05:25:14 -04:00
										 |  |  | 					return callback(null, false); | 
					
						
							| 
									
										
										
										
											2015-10-02 17:26:12 -04:00
										 |  |  | 				} | 
					
						
							|  |  |  | 				user.getUserFields(fromUid, ['banned', 'email:confirmed'], next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-06 12:57:55 -05:00
										 |  |  | 				user.getSettings(toUid, next); | 
					
						
							| 
									
										
										
										
											2014-10-30 17:50:07 -04:00
										 |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2014-11-06 12:57:55 -05:00
										 |  |  | 			function(settings, next) { | 
					
						
							|  |  |  | 				if (!settings.restrictChat) { | 
					
						
							|  |  |  | 					return callback(null, true); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				user.isAdministrator(fromUid, next); | 
					
						
							| 
									
										
										
										
											2014-10-30 17:50:07 -04:00
										 |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2014-11-06 12:57:55 -05:00
										 |  |  | 			function(isAdmin, next) { | 
					
						
							|  |  |  | 				if (isAdmin) { | 
					
						
							|  |  |  | 					return callback(null, true); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2014-10-30 17:50:07 -04:00
										 |  |  | 				user.isFollowing(toUid, fromUid, next); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 	Messaging.canEdit = function(messageId, uid, callback) { | 
					
						
							|  |  |  | 		if (parseInt(meta.config.disableChat) === 1) { | 
					
						
							|  |  |  | 			return callback(null, false); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		async.waterfall([ | 
					
						
							|  |  |  | 			function (next) { | 
					
						
							|  |  |  | 				user.getUserFields(uid, ['banned', 'email:confirmed'], next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function (userData, next) { | 
					
						
							|  |  |  | 				if (parseInt(userData.banned, 10) === 1) { | 
					
						
							|  |  |  | 					return callback(null, false); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if (parseInt(meta.config.requireEmailConfirmation, 10) === 1 && parseInt(userData['email:confirmed'], 10) !== 1) { | 
					
						
							|  |  |  | 					return callback(null, false); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				Messaging.getMessageField(messageId, 'fromuid', next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function(fromUid, next) { | 
					
						
							|  |  |  | 				if (parseInt(fromUid, 10) === parseInt(uid, 10)) { | 
					
						
							|  |  |  | 					return callback(null, true); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				user.isAdministrator(uid, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function(isAdmin, next) { | 
					
						
							|  |  |  | 				next(null, isAdmin); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		], callback); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-16 13:27:26 -04:00
										 |  |  | 	function sendNotifications(fromuid, touid, messageObj, callback) { | 
					
						
							| 
									
										
										
										
											2015-11-05 12:34:39 -05:00
										 |  |  | 		user.isOnline(touid, function(err, isOnline) { | 
					
						
							|  |  |  | 			if (err || isOnline) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							| 
									
										
										
										
											2015-02-19 18:14:38 -05:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-05 12:34:39 -05:00
										 |  |  | 			notifications.create({ | 
					
						
							|  |  |  | 				bodyShort: '[[notifications:new_message_from, ' + messageObj.fromUser.username + ']]', | 
					
						
							|  |  |  | 				bodyLong: messageObj.content, | 
					
						
							|  |  |  | 				nid: 'chat_' + fromuid + '_' + touid, | 
					
						
							|  |  |  | 				from: fromuid, | 
					
						
							|  |  |  | 				path: '/chats/' + messageObj.fromUser.username | 
					
						
							|  |  |  | 			}, function(err, notification) { | 
					
						
							|  |  |  | 				if (!err && notification) { | 
					
						
							|  |  |  | 					notifications.push(notification, [touid], callback); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			user.getSettings(messageObj.toUser.uid, function(err, settings) { | 
					
						
							|  |  |  | 				if (settings.sendChatNotifications && !parseInt(meta.config.disableEmailSubscriptions, 10)) { | 
					
						
							|  |  |  | 					emailer.send('notif_chat', touid, { | 
					
						
							|  |  |  | 						subject: '[[email:notif.chat.subject, ' + messageObj.fromUser.username + ']]', | 
					
						
							|  |  |  | 						username: messageObj.toUser.username, | 
					
						
							|  |  |  | 						userslug: utils.slugify(messageObj.toUser.username), | 
					
						
							|  |  |  | 						summary: '[[notifications:new_message_from, ' + messageObj.fromUser.username + ']]', | 
					
						
							|  |  |  | 						message: messageObj, | 
					
						
							|  |  |  | 						site_title: meta.config.title || 'NodeBB', | 
					
						
							|  |  |  | 						url: nconf.get('url'), | 
					
						
							|  |  |  | 						fromUserslug: utils.slugify(messageObj.fromUser.username) | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2015-02-19 18:14:38 -05:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-07-23 18:23:03 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-10 20:31:57 +01:00
										 |  |  | }(exports)); |