| 
									
										
										
										
											2015-12-15 14:10:32 +02:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var async = require('async'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var db = require('../database'); | 
					
						
							|  |  |  | var sockets = require('../socket.io'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | module.exports = function (Messaging) { | 
					
						
							| 
									
										
										
										
											2015-12-15 14:10:32 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	Messaging.getUnreadCount = function (uid, callback) { | 
					
						
							| 
									
										
										
										
											2016-03-11 13:38:52 +02:00
										 |  |  | 		if (!parseInt(uid, 10)) { | 
					
						
							|  |  |  | 			return callback(null, 0); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-12-15 14:10:32 +02:00
										 |  |  | 		db.sortedSetCard('uid:' + uid + ':chat:rooms:unread', callback); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	Messaging.pushUnreadCount = function (uid) { | 
					
						
							| 
									
										
										
										
											2016-03-11 13:38:52 +02:00
										 |  |  | 		if (!parseInt(uid, 10)) { | 
					
						
							|  |  |  | 			return callback(null, 0); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		Messaging.getUnreadCount(uid, function (err, unreadCount) { | 
					
						
							| 
									
										
										
										
											2015-12-15 14:10:32 +02:00
										 |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			sockets.in('uid_' + uid).emit('event:unread.updateChatCount', unreadCount); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	Messaging.markRead = function (uid, roomId, callback) { | 
					
						
							| 
									
										
										
										
											2015-12-15 14:10:32 +02:00
										 |  |  | 		db.sortedSetRemove('uid:' + uid + ':chat:rooms:unread', roomId, callback); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	Messaging.markAllRead = function (uid, callback) { | 
					
						
							| 
									
										
										
										
											2016-03-11 13:38:52 +02:00
										 |  |  | 		db.delete('uid:' + uid + ':chat:rooms:unread', callback); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	Messaging.markUnread = function (uids, roomId, callback) { | 
					
						
							| 
									
										
										
										
											2015-12-15 14:10:32 +02:00
										 |  |  | 		async.waterfall([ | 
					
						
							|  |  |  | 			function (next) { | 
					
						
							|  |  |  | 				Messaging.roomExists(roomId, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function (exists, next) { | 
					
						
							|  |  |  | 				if (!exists) { | 
					
						
							|  |  |  | 					return next(new Error('[[error:chat-room-does-not-exist]]')); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 				var keys = uids.map(function (uid) { | 
					
						
							| 
									
										
										
										
											2015-12-15 14:10:32 +02:00
										 |  |  | 					return 'uid:' + uid + ':chat:rooms:unread'; | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2015-12-16 15:09:14 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				db.sortedSetsAdd(keys, Date.now(), roomId, next); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2015-12-15 14:10:32 +02:00
										 |  |  | 		], callback); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | }; |