| 
									
										
										
										
											2014-03-12 18:00:27 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var async = require('async'), | 
					
						
							|  |  |  | 	nconf = require('nconf'), | 
					
						
							|  |  |  | 	winston = require('winston'), | 
					
						
							| 
									
										
										
										
											2014-10-18 16:45:35 -04:00
										 |  |  | 	S = require('string'), | 
					
						
							| 
									
										
										
										
											2014-03-12 18:00:27 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-18 15:35:48 -04:00
										 |  |  | 	user = require('../user'), | 
					
						
							|  |  |  | 	db = require('../database'), | 
					
						
							| 
									
										
										
										
											2014-09-29 12:30:03 -04:00
										 |  |  | 	meta = require('../meta'), | 
					
						
							| 
									
										
										
										
											2014-03-18 15:35:48 -04:00
										 |  |  | 	notifications = require('../notifications'), | 
					
						
							| 
									
										
										
										
											2014-06-03 12:30:36 -04:00
										 |  |  | 	posts = require('../posts'), | 
					
						
							| 
									
										
										
										
											2014-06-26 13:43:57 -04:00
										 |  |  | 	topics = require('../topics'), | 
					
						
							| 
									
										
										
										
											2014-08-30 15:39:20 -04:00
										 |  |  | 	privileges = require('../privileges'), | 
					
						
							|  |  |  | 	utils = require('../../public/src/utils'); | 
					
						
							| 
									
										
										
										
											2014-03-12 18:00:27 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | (function(UserNotifications) { | 
					
						
							| 
									
										
										
										
											2014-07-28 15:52:33 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-12 18:00:27 -04:00
										 |  |  | 	UserNotifications.get = function(uid, callback) { | 
					
						
							| 
									
										
										
										
											2014-11-05 20:41:31 -05:00
										 |  |  | 		if (!parseInt(uid, 10)) { | 
					
						
							|  |  |  | 			return callback(null , {read: [], unread: []}); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-10-17 18:31:20 -04:00
										 |  |  | 		getNotifications(uid, 10, function(err, notifications) { | 
					
						
							| 
									
										
										
										
											2014-07-28 16:08:16 -04:00
										 |  |  | 			if (err) { | 
					
						
							| 
									
										
										
										
											2014-03-12 18:00:27 -04:00
										 |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-16 23:25:30 -04:00
										 |  |  | 			notifications.read = notifications.read.filter(Boolean); | 
					
						
							|  |  |  | 			notifications.unread = notifications.unread.filter(Boolean); | 
					
						
							| 
									
										
										
										
											2014-03-12 18:00:27 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-17 18:31:20 -04:00
										 |  |  | 			var maxNotifs = 15; | 
					
						
							| 
									
										
										
										
											2014-03-12 18:00:27 -04:00
										 |  |  | 			if (notifications.read.length + notifications.unread.length > maxNotifs) { | 
					
						
							|  |  |  | 				notifications.read.length = maxNotifs - notifications.unread.length; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			callback(null, notifications); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-25 15:56:52 -05:00
										 |  |  | 	UserNotifications.getAll = function(uid, count, callback) { | 
					
						
							|  |  |  | 		getNotifications(uid, count, function(err, notifs) { | 
					
						
							|  |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			notifs = notifs.unread.concat(notifs.read); | 
					
						
							|  |  |  | 			notifs = notifs.filter(Boolean).sort(function(a, b) { | 
					
						
							|  |  |  | 				return b.datetime - a.datetime; | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			callback(null, notifs); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-17 18:31:20 -04:00
										 |  |  | 	function getNotifications(uid, count, callback) { | 
					
						
							|  |  |  | 		async.parallel({ | 
					
						
							|  |  |  | 			unread: function(next) { | 
					
						
							|  |  |  | 				getNotificationsFromSet('uid:' + uid + ':notifications:unread', false, uid, 0, count - 1, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			read: function(next) { | 
					
						
							|  |  |  | 				getNotificationsFromSet('uid:' + uid + ':notifications:read', true, uid, 0, count - 1, next); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}, callback); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	function getNotificationsFromSet(set, read, uid, start, stop, callback) { | 
					
						
							| 
									
										
										
										
											2014-09-08 23:03:37 -04:00
										 |  |  | 		db.getSortedSetRevRange(set, start, stop, function(err, nids) { | 
					
						
							| 
									
										
										
										
											2014-08-16 23:25:30 -04:00
										 |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-08 23:03:37 -04:00
										 |  |  | 			if(!Array.isArray(nids) || !nids.length) { | 
					
						
							| 
									
										
										
										
											2014-08-16 23:25:30 -04:00
										 |  |  | 				return callback(null, []); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-08 23:03:37 -04:00
										 |  |  | 			UserNotifications.getNotifications(nids, uid, function(err, notifications) { | 
					
						
							| 
									
										
										
										
											2014-08-16 23:25:30 -04:00
										 |  |  | 				if (err) { | 
					
						
							|  |  |  | 					return callback(err); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-08 23:03:37 -04:00
										 |  |  | 				var deletedNids = []; | 
					
						
							| 
									
										
										
										
											2014-08-16 23:25:30 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-08 23:03:37 -04:00
										 |  |  | 				notifications.forEach(function(notification, index) { | 
					
						
							|  |  |  | 					if (!notification) { | 
					
						
							|  |  |  | 						if (process.env.NODE_ENV === 'development') { | 
					
						
							|  |  |  | 							winston.info('[notifications.get] nid ' + nids[index] + ' not found. Removing.'); | 
					
						
							| 
									
										
										
										
											2014-08-16 23:25:30 -04:00
										 |  |  | 						} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-23 16:02:19 -04:00
										 |  |  | 						deletedNids.push(nids[index]); | 
					
						
							| 
									
										
										
										
											2014-10-17 18:31:20 -04:00
										 |  |  | 					} else { | 
					
						
							|  |  |  | 						notification.read = read; | 
					
						
							| 
									
										
										
										
											2015-02-19 12:05:19 -05:00
										 |  |  | 						notification.readClass = !notification.read ? 'unread' : ''; | 
					
						
							| 
									
										
										
										
											2014-09-08 23:03:37 -04:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2014-08-16 23:25:30 -04:00
										 |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2014-09-08 23:03:37 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				if (deletedNids.length) { | 
					
						
							|  |  |  | 					db.sortedSetRemove(set, deletedNids); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				callback(null, notifications); | 
					
						
							| 
									
										
										
										
											2014-08-16 23:25:30 -04:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	UserNotifications.getNotifications = function(nids, uid, callback) { | 
					
						
							|  |  |  | 		notifications.getMultiple(nids, function(err, notifications) { | 
					
						
							|  |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-19 15:24:38 -04:00
										 |  |  | 			UserNotifications.generateNotificationPaths(notifications, uid, callback); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2014-10-17 18:31:20 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-19 15:24:38 -04:00
										 |  |  | 	UserNotifications.generateNotificationPaths = function (notifications, uid, callback) { | 
					
						
							|  |  |  | 		var pids = notifications.map(function(notification) { | 
					
						
							|  |  |  | 			return notification ? notification.pid : null; | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-08-16 23:25:30 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-19 15:24:38 -04:00
										 |  |  | 		generatePostPaths(pids, uid, function(err, pidToPaths) { | 
					
						
							|  |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-08-17 00:14:45 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-19 15:24:38 -04:00
										 |  |  | 			notifications = notifications.map(function(notification, index) { | 
					
						
							|  |  |  | 				if (!notification) { | 
					
						
							|  |  |  | 					return null; | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2015-02-19 18:14:38 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-19 15:24:38 -04:00
										 |  |  | 				notification.path = pidToPaths[notification.pid] || notification.path || ''; | 
					
						
							| 
									
										
										
										
											2015-02-19 18:14:38 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-19 15:24:38 -04:00
										 |  |  | 				if (notification.nid.startsWith('chat')) { | 
					
						
							|  |  |  | 					notification.path = nconf.get('relative_path') + '/chats/' + notification.user.userslug; | 
					
						
							|  |  |  | 				} else if (notification.nid.startsWith('follow')) { | 
					
						
							|  |  |  | 					notification.path = nconf.get('relative_path') + '/user/' + notification.user.userslug; | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2014-10-17 18:31:20 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-19 15:24:38 -04:00
										 |  |  | 				notification.datetimeISO = utils.toISOString(notification.datetime); | 
					
						
							|  |  |  | 				return notification; | 
					
						
							| 
									
										
										
										
											2014-08-16 23:25:30 -04:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2015-03-19 15:24:38 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			callback(null, notifications); | 
					
						
							| 
									
										
										
										
											2014-08-16 23:25:30 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-07-28 15:52:33 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-17 00:14:45 -04:00
										 |  |  | 	function generatePostPaths(pids, uid, callback) { | 
					
						
							| 
									
										
										
										
											2014-09-08 23:03:37 -04:00
										 |  |  | 		pids = pids.filter(Boolean); | 
					
						
							|  |  |  | 		var postKeys = pids.map(function(pid) { | 
					
						
							| 
									
										
										
										
											2014-08-17 00:14:45 -04:00
										 |  |  | 			return 'post:' + pid; | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		db.getObjectsFields(postKeys, ['pid', 'tid'], function(err, postData) { | 
					
						
							|  |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			var topicKeys = postData.map(function(post) { | 
					
						
							|  |  |  | 				return post ? 'topic:' + post.tid : null; | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			async.parallel({ | 
					
						
							|  |  |  | 				indices: function(next) { | 
					
						
							|  |  |  | 					posts.getPostIndices(postData, uid, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				topics: function(next) { | 
					
						
							|  |  |  | 					db.getObjectsFields(topicKeys, ['slug'], next); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}, function(err, results) { | 
					
						
							|  |  |  | 				if (err) { | 
					
						
							|  |  |  | 					return callback(err); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-06 20:46:18 -04:00
										 |  |  | 				var pidToPaths = {}; | 
					
						
							| 
									
										
										
										
											2014-08-17 00:14:45 -04:00
										 |  |  | 				pids.forEach(function(pid, index) { | 
					
						
							|  |  |  | 					var slug = results.topics[index] ? results.topics[index].slug : null; | 
					
						
							| 
									
										
										
										
											2014-08-30 15:39:20 -04:00
										 |  |  | 					var postIndex = utils.isNumber(results.indices[index]) ? parseInt(results.indices[index], 10) + 1 : null; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-17 00:14:45 -04:00
										 |  |  | 					if (slug && postIndex) { | 
					
						
							| 
									
										
										
										
											2014-09-06 20:46:18 -04:00
										 |  |  | 						pidToPaths[pid] = nconf.get('relative_path') + '/topic/' + slug + '/' + postIndex; | 
					
						
							| 
									
										
										
										
											2014-08-17 00:14:45 -04:00
										 |  |  | 					} | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2014-09-08 23:03:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-06 20:46:18 -04:00
										 |  |  | 				callback(null, pidToPaths); | 
					
						
							| 
									
										
										
										
											2014-08-17 00:14:45 -04:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-18 15:35:48 -04:00
										 |  |  | 	UserNotifications.getDailyUnread = function(uid, callback) { | 
					
						
							|  |  |  | 		var	now = Date.now(), | 
					
						
							|  |  |  | 			yesterday = now - (1000*60*60*24);	// Approximate, can be more or less depending on time changes, makes no difference really.
 | 
					
						
							| 
									
										
										
										
											2014-07-28 15:52:33 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-17 18:51:58 -04:00
										 |  |  | 		db.getSortedSetRevRangeByScore('uid:' + uid + ':notifications:unread', 0, 20, now, yesterday, function(err, nids) { | 
					
						
							| 
									
										
										
										
											2014-07-28 15:52:33 -04:00
										 |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-08 23:03:37 -04:00
										 |  |  | 			if (!Array.isArray(nids) || !nids.length) { | 
					
						
							| 
									
										
										
										
											2014-07-28 15:52:33 -04:00
										 |  |  | 				return callback(null, []); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-19 16:39:36 -04:00
										 |  |  | 			UserNotifications.getNotifications(nids, uid, callback); | 
					
						
							| 
									
										
										
										
											2014-03-18 15:35:48 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-03-12 18:00:27 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	UserNotifications.getUnreadCount = function(uid, callback) { | 
					
						
							| 
									
										
										
										
											2014-10-01 15:32:54 -04:00
										 |  |  | 		if (!parseInt(uid, 10)) { | 
					
						
							|  |  |  | 			return callback(null, 0); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-10-17 18:51:58 -04:00
										 |  |  | 		db.getSortedSetRevRange('uid:' + uid + ':notifications:unread', 0, 20, function(err, nids) { | 
					
						
							| 
									
										
										
										
											2014-10-14 23:12:47 -04:00
										 |  |  | 			callback(err, Array.isArray(nids) ? nids.length : 0); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-03-12 18:00:27 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-28 15:52:33 -04:00
										 |  |  | 	UserNotifications.getUnreadByField = function(uid, field, value, callback) { | 
					
						
							| 
									
										
										
										
											2014-10-17 18:51:58 -04:00
										 |  |  | 		db.getSortedSetRevRange('uid:' + uid + ':notifications:unread', 0, 99, function(err, nids) { | 
					
						
							| 
									
										
										
										
											2014-07-28 15:52:33 -04:00
										 |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-03-12 18:00:27 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-08 23:03:37 -04:00
										 |  |  | 			if (!Array.isArray(nids) || !nids.length) { | 
					
						
							| 
									
										
										
										
											2014-07-28 15:52:33 -04:00
										 |  |  | 				return callback(null, []); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-03-12 18:00:27 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-17 17:20:45 -04:00
										 |  |  | 			var keys = nids.map(function(nid) { | 
					
						
							|  |  |  | 				return 'notifications:' + nid; | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-17 17:23:47 -04:00
										 |  |  | 			db.getObjectsFields(keys, ['nid', field], function(err, notifications) { | 
					
						
							| 
									
										
										
										
											2014-07-28 15:52:33 -04:00
										 |  |  | 				if (err) { | 
					
						
							|  |  |  | 					return callback(err); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2014-09-30 23:19:53 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-19 19:24:28 -04:00
										 |  |  | 				value = value ? value.toString() : ''; | 
					
						
							| 
									
										
										
										
											2014-09-08 23:03:37 -04:00
										 |  |  | 				nids = notifications.filter(function(notification) { | 
					
						
							| 
									
										
										
										
											2014-12-18 23:28:19 -05:00
										 |  |  | 					return notification && notification[field] && notification[field].toString() === value; | 
					
						
							| 
									
										
										
										
											2014-09-08 23:03:37 -04:00
										 |  |  | 				}).map(function(notification) { | 
					
						
							|  |  |  | 					return notification.nid; | 
					
						
							| 
									
										
										
										
											2014-07-28 15:52:33 -04:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-08 23:03:37 -04:00
										 |  |  | 				callback(null, nids); | 
					
						
							| 
									
										
										
										
											2014-03-12 18:00:27 -04:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2014-11-05 20:41:31 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	UserNotifications.deleteAll = function(uid, callback) { | 
					
						
							|  |  |  | 		if (!parseInt(uid, 10)) { | 
					
						
							|  |  |  | 			return callback(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		async.parallel([ | 
					
						
							|  |  |  | 			function(next) { | 
					
						
							|  |  |  | 				db.delete('uid:' + uid + ':notifications:unread', next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function(next) { | 
					
						
							|  |  |  | 				db.delete('uid:' + uid + ':notifications:read', next); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		], callback); | 
					
						
							|  |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2014-03-12 18:00:27 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-15 21:55:31 -04:00
										 |  |  | 	UserNotifications.sendTopicNotificationToFollowers = function(uid, topicData, postData) { | 
					
						
							| 
									
										
										
										
											2015-05-21 16:11:19 -07:00
										 |  |  | 		db.getSortedSetRange('followers:' + uid, 0, -1, function(err, followers) { | 
					
						
							| 
									
										
										
										
											2014-09-08 23:03:37 -04:00
										 |  |  | 			if (err || !Array.isArray(followers) || !followers.length) { | 
					
						
							| 
									
										
										
										
											2014-06-03 12:30:36 -04:00
										 |  |  | 				return; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-15 21:55:31 -04:00
										 |  |  | 			privileges.categories.filterUids('read', topicData.cid, followers, function(err, followers) { | 
					
						
							|  |  |  | 				if (err || !followers.length) { | 
					
						
							| 
									
										
										
										
											2014-09-08 23:03:37 -04:00
										 |  |  | 					return; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-18 16:45:35 -04:00
										 |  |  | 				var title = topicData.title; | 
					
						
							|  |  |  | 				if (title) { | 
					
						
							|  |  |  | 					title = S(title).decodeHTMLEntities().s; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-15 21:55:31 -04:00
										 |  |  | 				notifications.create({ | 
					
						
							| 
									
										
										
										
											2014-10-18 16:45:35 -04:00
										 |  |  | 					bodyShort: '[[notifications:user_posted_topic, ' + postData.user.username + ', ' + title + ']]', | 
					
						
							| 
									
										
										
										
											2014-10-15 21:55:31 -04:00
										 |  |  | 					bodyLong: postData.content, | 
					
						
							|  |  |  | 					pid: postData.pid, | 
					
						
							| 
									
										
										
										
											2014-10-19 18:41:54 -04:00
										 |  |  | 					nid: 'tid:' + postData.tid + ':uid:' + uid, | 
					
						
							| 
									
										
										
										
											2014-10-15 21:55:31 -04:00
										 |  |  | 					tid: postData.tid, | 
					
						
							|  |  |  | 					from: uid | 
					
						
							|  |  |  | 				}, function(err, notification) { | 
					
						
							|  |  |  | 					if (!err && notification) { | 
					
						
							|  |  |  | 						notifications.push(notification, followers); | 
					
						
							| 
									
										
										
										
											2014-07-28 15:52:33 -04:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2014-06-03 12:30:36 -04:00
										 |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2014-04-16 15:51:05 -04:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-27 21:26:19 -04:00
										 |  |  | 	UserNotifications.sendWelcomeNotification = function(uid, callback) { | 
					
						
							|  |  |  | 		callback = callback || function() {}; | 
					
						
							| 
									
										
										
										
											2014-09-29 12:30:03 -04:00
										 |  |  | 		if (!meta.config.welcomeNotification) { | 
					
						
							| 
									
										
										
										
											2015-06-27 21:26:19 -04:00
										 |  |  | 			return callback(); | 
					
						
							| 
									
										
										
										
											2014-09-29 12:30:03 -04:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		var path = meta.config.welcomeLink ? meta.config.welcomeLink : '#'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		notifications.create({ | 
					
						
							|  |  |  | 			bodyShort: meta.config.welcomeNotification, | 
					
						
							|  |  |  | 			path: path, | 
					
						
							|  |  |  | 			nid: 'welcome_' + uid | 
					
						
							|  |  |  | 		}, function(err, notification) { | 
					
						
							| 
									
										
										
										
											2015-06-27 21:26:19 -04:00
										 |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			if (notification) { | 
					
						
							|  |  |  | 				notifications.push(notification, [uid], callback); | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				callback(); | 
					
						
							| 
									
										
										
										
											2014-09-29 12:30:03 -04:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-28 18:28:26 -04:00
										 |  |  | 	UserNotifications.sendNameChangeNotification = function(uid, username) { | 
					
						
							|  |  |  | 		notifications.create({ | 
					
						
							|  |  |  | 			bodyShort: '[[user:username_taken_workaround, ' + username + ']]', | 
					
						
							|  |  |  | 			image: 'brand:logo', | 
					
						
							|  |  |  | 			nid: 'username_taken:' + uid, | 
					
						
							|  |  |  | 			datetime: Date.now() | 
					
						
							|  |  |  | 		}, function(err, notification) { | 
					
						
							|  |  |  | 			if (!err && notification) { | 
					
						
							|  |  |  | 				notifications.push(notification, uid); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-12 18:00:27 -04:00
										 |  |  | 	UserNotifications.pushCount = function(uid) { | 
					
						
							| 
									
										
										
										
											2014-03-12 20:53:42 -04:00
										 |  |  | 		var websockets = require('./../socket.io'); | 
					
						
							| 
									
										
										
										
											2014-03-12 18:00:27 -04:00
										 |  |  | 		UserNotifications.getUnreadCount(uid, function(err, count) { | 
					
						
							|  |  |  | 			if (err) { | 
					
						
							| 
									
										
										
										
											2014-09-05 01:36:30 -04:00
										 |  |  | 				return winston.error(err.stack); | 
					
						
							| 
									
										
										
										
											2014-03-12 18:00:27 -04:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			websockets.in('uid_' + uid).emit('event:notifications.updateCount', count); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-10 20:31:57 +01:00
										 |  |  | }(exports)); |