| 
									
										
										
										
											2014-03-12 18:00:27 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var async = require('async'), | 
					
						
							|  |  |  | 	nconf = require('nconf'), | 
					
						
							|  |  |  | 	winston = require('winston'), | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-18 15:35:48 -04:00
										 |  |  | 	user = require('../user'), | 
					
						
							|  |  |  | 	utils = require('../../public/src/utils'), | 
					
						
							|  |  |  | 	db = require('../database'), | 
					
						
							|  |  |  | 	notifications = require('../notifications'), | 
					
						
							| 
									
										
										
										
											2014-06-03 12:30:36 -04:00
										 |  |  | 	posts = require('../posts'), | 
					
						
							| 
									
										
										
										
											2014-06-21 01:14:49 -04:00
										 |  |  | 	postTools = require('../postTools'), | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							|  |  |  | 		var maxNotifs = 15; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		async.parallel({ | 
					
						
							|  |  |  | 			unread: function(next) { | 
					
						
							| 
									
										
										
										
											2014-08-16 23:25:30 -04:00
										 |  |  | 				getNotificationsFromSet('uid:' + uid + ':notifications:unread', uid, 0, 9, maxNotifs, next); | 
					
						
							| 
									
										
										
										
											2014-03-12 18:00:27 -04:00
										 |  |  | 			}, | 
					
						
							|  |  |  | 			read: function(next) { | 
					
						
							| 
									
										
										
										
											2014-08-16 23:25:30 -04:00
										 |  |  | 				getNotificationsFromSet('uid:' + uid + ':notifications:read', uid, 0, 9, maxNotifs, next); | 
					
						
							| 
									
										
										
										
											2014-03-12 18:00:27 -04:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		}, 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
										 |  |  | 
 | 
					
						
							|  |  |  | 			// Limit the number of notifications to `maxNotifs`, prioritising unread notifications
 | 
					
						
							|  |  |  | 			if (notifications.read.length + notifications.unread.length > maxNotifs) { | 
					
						
							|  |  |  | 				notifications.read.length = maxNotifs - notifications.unread.length; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			callback(null, notifications); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-16 23:25:30 -04:00
										 |  |  | 	function getNotificationsFromSet(set, uid, start, stop, max, callback) { | 
					
						
							|  |  |  | 		db.getSortedSetRevRange(set, start, stop, function(err, uniqueIds) { | 
					
						
							|  |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if(!Array.isArray(uniqueIds) || !uniqueIds.length) { | 
					
						
							|  |  |  | 				return callback(null, []); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (uniqueIds.length > max) { | 
					
						
							|  |  |  | 				uniqueIds.length = max; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			db.getObjectFields('uid:' + uid + ':notifications:uniqueId:nid', uniqueIds, function(err, uniqueIdToNids) { | 
					
						
							|  |  |  | 				if (err) { | 
					
						
							|  |  |  | 					return callback(err); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				var nidsToUniqueIds = {}; | 
					
						
							|  |  |  | 				var nids = []; | 
					
						
							|  |  |  | 				uniqueIds.forEach(function(uniqueId) { | 
					
						
							|  |  |  | 					nidsToUniqueIds[uniqueIdToNids[uniqueId]] = uniqueId; | 
					
						
							|  |  |  | 					nids.push(uniqueIdToNids[uniqueId]); | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2014-03-12 18:00:27 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-16 23:25:30 -04:00
										 |  |  | 				UserNotifications.getNotifications(nids, uid, function(err, notifications) { | 
					
						
							|  |  |  | 					if (err) { | 
					
						
							|  |  |  | 						return callback(err); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					notifications.forEach(function(notification, index) { | 
					
						
							|  |  |  | 						if (!notification) { | 
					
						
							|  |  |  | 							if (process.env.NODE_ENV === 'development') { | 
					
						
							|  |  |  | 								winston.info('[notifications.get] nid ' + nids[index] + ' not found. Removing.'); | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 							db.sortedSetRemove(set, nidsToUniqueIds[nids[index]]); | 
					
						
							|  |  |  | 							db.deleteObjectField('uid:' + uid + ':notifications:uniqueId:nid', nidsToUniqueIds[nids[index]]); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					callback(null, notifications); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	UserNotifications.getAll = function(uid, limit, callback) { | 
					
						
							| 
									
										
										
										
											2014-03-12 18:00:27 -04:00
										 |  |  | 		if (!limit || parseInt(limit, 10) <= 0) { | 
					
						
							|  |  |  | 			limit = 25; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-28 15:52:33 -04:00
										 |  |  | 		db.getObjectValues('uid:' + uid + ':notifications:uniqueId:nid', function(err, nids) { | 
					
						
							|  |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-03-12 18:00:27 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-16 23:25:30 -04:00
										 |  |  | 			UserNotifications.getNotifications(nids, uid, function(err, notifs) { | 
					
						
							| 
									
										
										
										
											2014-07-28 15:52:33 -04:00
										 |  |  | 				if (err) { | 
					
						
							|  |  |  | 					return callback(err); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2014-03-12 18:00:27 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-16 23:25:30 -04:00
										 |  |  | 				notifs = notifs.filter(Boolean).sort(function(a, b) { | 
					
						
							| 
									
										
										
										
											2014-07-28 15:52:33 -04:00
										 |  |  | 					return parseInt(b.datetime, 10) - parseInt(a.datetime, 10); | 
					
						
							| 
									
										
										
										
											2014-03-12 18:00:27 -04:00
										 |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2014-07-28 15:52:33 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				callback(null, notifs); | 
					
						
							| 
									
										
										
										
											2014-03-12 18:00:27 -04:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-03-18 15:35:48 -04:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2014-03-12 18:00:27 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			var uniqueIds = notifications.map(function(notification) { | 
					
						
							|  |  |  | 				return notification ? notification.uniqueId : null; | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			db.isSortedSetMembers('uid:' + uid + ':notifications:read', uniqueIds, function(err, hasRead) { | 
					
						
							|  |  |  | 				if (err) { | 
					
						
							|  |  |  | 					return callback(err); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-17 00:14:45 -04:00
										 |  |  | 				var pids = notifications.map(function(notification) { | 
					
						
							|  |  |  | 					return notification ? notification.pid : null; | 
					
						
							| 
									
										
										
										
											2014-08-16 23:25:30 -04:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-06 20:46:18 -04:00
										 |  |  | 				generatePostPaths(pids, uid, function(err, pidToPaths) { | 
					
						
							| 
									
										
										
										
											2014-08-17 00:14:45 -04:00
										 |  |  | 					if (err) { | 
					
						
							|  |  |  | 						return callback(err); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					notifications = notifications.map(function(notification, index) { | 
					
						
							|  |  |  | 						if (!notification) { | 
					
						
							|  |  |  | 							return null; | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 						notification.read = hasRead[index]; | 
					
						
							| 
									
										
										
										
											2014-09-06 20:46:18 -04:00
										 |  |  | 						notification.path = pidToPaths[notification.pid] || notification.path || ''; | 
					
						
							| 
									
										
										
										
											2014-08-17 00:14:45 -04:00
										 |  |  | 						notification.datetimeISO = utils.toISOString(notification.datetime); | 
					
						
							|  |  |  | 						notification.readClass = !notification.read ? 'label-warning' : ''; | 
					
						
							|  |  |  | 						return notification; | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					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-06 20:46:18 -04:00
										 |  |  | 		var postKeys = pids.filter(Boolean).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-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
										 |  |  | 
 | 
					
						
							|  |  |  | 		db.getSortedSetRangeByScore('uid:' + uid + ':notifications:unread', 0, 20, yesterday, now, function(err, uniqueIds) { | 
					
						
							|  |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (!Array.isArray(uniqueIds) || !uniqueIds.length) { | 
					
						
							|  |  |  | 				return callback(null, []); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			db.getObjectFields('uid:' + uid + ':notifications:uniqueId:nid', uniqueIds, function(err, uniqueIdToNids) { | 
					
						
							|  |  |  | 				if (err) { | 
					
						
							|  |  |  | 					return callback(err); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				var nids = Object.keys(uniqueIdToNids).map(function(uniqueId) { | 
					
						
							|  |  |  | 					return uniqueIdToNids[uniqueId]; | 
					
						
							| 
									
										
										
										
											2014-03-18 15:35:48 -04:00
										 |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2014-07-28 15:52:33 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-16 23:25:30 -04:00
										 |  |  | 				UserNotifications.getNotifications(nids, uid, callback); | 
					
						
							| 
									
										
										
										
											2014-07-28 15:52:33 -04:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2014-03-18 15:35:48 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-03-12 18:00:27 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	UserNotifications.getUnreadCount = function(uid, callback) { | 
					
						
							| 
									
										
										
										
											2014-08-17 22:10:16 -04:00
										 |  |  | 		db.sortedSetCard('uid:' + uid + ':notifications:unread', callback); | 
					
						
							| 
									
										
										
										
											2014-03-12 18:00:27 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-28 15:52:33 -04:00
										 |  |  | 	UserNotifications.getUnreadByField = function(uid, field, value, callback) { | 
					
						
							|  |  |  | 		db.getSortedSetRange('uid:' + uid + ':notifications:unread', 0, -1, function(err, uniqueIds) { | 
					
						
							|  |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-03-12 18:00:27 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-28 15:52:33 -04:00
										 |  |  | 			if (!Array.isArray(uniqueIds) || !uniqueIds.length) { | 
					
						
							|  |  |  | 				return callback(null, []); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-03-12 18:00:27 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-28 15:52:33 -04:00
										 |  |  | 			db.getObjectFields('uid:' + uid + ':notifications:uniqueId:nid', uniqueIds, function(err, uniqueIdsToNids) { | 
					
						
							|  |  |  | 				if (err) { | 
					
						
							|  |  |  | 					return callback(err); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				var nids = Object.keys(uniqueIdsToNids).map(function(uniqueId) { | 
					
						
							|  |  |  | 					return uniqueIdsToNids[uniqueId]; | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-16 23:25:30 -04:00
										 |  |  | 				UserNotifications.getNotifications(nids, uid, function(err, notifications) { | 
					
						
							|  |  |  | 					if (err) { | 
					
						
							|  |  |  | 						return callback(err); | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2014-07-28 15:52:33 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-16 23:25:30 -04:00
										 |  |  | 					notifications = notifications.filter(function(notification) { | 
					
						
							| 
									
										
										
										
											2014-08-27 14:09:15 -04:00
										 |  |  | 						return notification && notification[field] !== value.toString(); | 
					
						
							| 
									
										
										
										
											2014-08-16 23:25:30 -04:00
										 |  |  | 					}).map(function(notification) { | 
					
						
							|  |  |  | 						return notification.nid; | 
					
						
							| 
									
										
										
										
											2014-07-28 15:52:33 -04:00
										 |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2014-08-16 23:25:30 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-28 15:52:33 -04:00
										 |  |  | 					callback(null, nids); | 
					
						
							| 
									
										
										
										
											2014-03-12 18:00:27 -04:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-28 15:52:33 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-16 15:51:05 -04:00
										 |  |  | 	UserNotifications.sendPostNotificationToFollowers = function(uid, tid, pid) { | 
					
						
							| 
									
										
										
										
											2014-09-06 01:07:03 -04:00
										 |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2014-06-03 12:30:36 -04:00
										 |  |  | 		db.getSetMembers('followers:' + uid, function(err, followers) { | 
					
						
							|  |  |  | 			if (err || !followers || !followers.length) { | 
					
						
							|  |  |  | 				return; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			async.parallel({ | 
					
						
							| 
									
										
										
										
											2014-06-20 16:54:34 -04:00
										 |  |  | 				username: async.apply(user.getUserField, uid, 'username'), | 
					
						
							| 
									
										
										
										
											2014-08-17 00:14:45 -04:00
										 |  |  | 				topic: async.apply(topics.getTopicFields, tid, ['cid', 'title']), | 
					
						
							| 
									
										
										
										
											2014-06-21 01:14:49 -04:00
										 |  |  | 				postContent: function(next) { | 
					
						
							|  |  |  | 					async.waterfall([ | 
					
						
							|  |  |  | 						async.apply(posts.getPostField, pid, 'content'), | 
					
						
							|  |  |  | 						function(content, next) { | 
					
						
							|  |  |  | 							postTools.parse(content, next); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					], next); | 
					
						
							| 
									
										
										
										
											2014-07-21 17:43:38 -04:00
										 |  |  | 				}, | 
					
						
							|  |  |  | 				topicFollowers: function(next) { | 
					
						
							|  |  |  | 					db.isSetMembers('tid:' + tid + ':followers', followers, next); | 
					
						
							| 
									
										
										
										
											2014-06-21 01:14:49 -04:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2014-06-03 12:30:36 -04:00
										 |  |  | 			}, function(err, results) { | 
					
						
							|  |  |  | 				if (err) { | 
					
						
							|  |  |  | 					return; | 
					
						
							| 
									
										
										
										
											2014-04-16 15:51:05 -04:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2014-06-03 12:30:36 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-21 17:43:38 -04:00
										 |  |  | 				followers = followers.filter(function(value, index) { | 
					
						
							|  |  |  | 					return !results.topicFollowers[index]; | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-03 12:30:36 -04:00
										 |  |  | 				notifications.create({ | 
					
						
							| 
									
										
										
										
											2014-07-21 17:43:38 -04:00
										 |  |  | 					bodyShort: '[[notifications:user_posted_to, ' + results.username + ', ' + results.topic.title + ']]', | 
					
						
							| 
									
										
										
										
											2014-06-20 17:53:40 -04:00
										 |  |  | 					bodyLong: results.postContent, | 
					
						
							| 
									
										
										
										
											2014-08-17 00:14:45 -04:00
										 |  |  | 					pid: pid, | 
					
						
							| 
									
										
										
										
											2014-07-28 17:50:49 -04:00
										 |  |  | 					uniqueId: 'topic:' + tid + ':uid:' + uid, | 
					
						
							| 
									
										
										
										
											2014-08-01 14:07:01 -04:00
										 |  |  | 					tid: tid, | 
					
						
							| 
									
										
										
										
											2014-06-03 12:30:36 -04:00
										 |  |  | 					from: uid | 
					
						
							| 
									
										
										
										
											2014-07-28 15:52:33 -04:00
										 |  |  | 				}, function(err, nid) { | 
					
						
							|  |  |  | 					if (err) { | 
					
						
							|  |  |  | 						return; | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2014-06-26 13:43:57 -04:00
										 |  |  | 					async.filter(followers, function(uid, next) { | 
					
						
							|  |  |  | 						privileges.categories.can('read', results.topic.cid, uid, function(err, canRead) { | 
					
						
							|  |  |  | 							next(!err && canRead); | 
					
						
							|  |  |  | 						}); | 
					
						
							|  |  |  | 					}, function(followers){ | 
					
						
							|  |  |  | 						notifications.push(nid, followers); | 
					
						
							|  |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2014-06-03 12:30:36 -04:00
										 |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2014-04-16 15:51:05 -04:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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)); |