| 
									
										
										
										
											2015-12-15 14:10:32 +02:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var db = require('../database'); | 
					
						
							|  |  |  | var sockets = require('../socket.io'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | module.exports = function (Messaging) { | 
					
						
							| 
									
										
										
										
											2019-08-13 14:36:15 -04:00
										 |  |  | 	Messaging.getUnreadCount = async (uid) => { | 
					
						
							| 
									
										
										
										
											2018-11-12 00:20:44 -05:00
										 |  |  | 		if (parseInt(uid, 10) <= 0) { | 
					
						
							| 
									
										
										
										
											2019-07-22 11:18:13 -04:00
										 |  |  | 			return 0; | 
					
						
							| 
									
										
										
										
											2016-03-11 13:38:52 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-07-22 11:18:13 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 		return await db.sortedSetCard(`uid:${uid}:chat:rooms:unread`); | 
					
						
							| 
									
										
										
										
											2015-12-15 14:10:32 +02:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-13 14:36:15 -04:00
										 |  |  | 	Messaging.pushUnreadCount = async (uid) => { | 
					
						
							| 
									
										
										
										
											2018-11-12 00:20:44 -05:00
										 |  |  | 		if (parseInt(uid, 10) <= 0) { | 
					
						
							| 
									
										
										
										
											2017-02-17 21:55:19 -07:00
										 |  |  | 			return; | 
					
						
							| 
									
										
										
										
											2016-03-11 13:38:52 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-07-22 11:18:13 -04:00
										 |  |  | 		const unreadCount = await Messaging.getUnreadCount(uid); | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 		sockets.in(`uid_${uid}`).emit('event:unread.updateChatCount', unreadCount); | 
					
						
							| 
									
										
										
										
											2015-12-15 14:10:32 +02:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-10 20:49:41 -04:00
										 |  |  | 	Messaging.markRead = async (uid, roomId) => { | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 		await db.sortedSetRemove(`uid:${uid}:chat:rooms:unread`, roomId); | 
					
						
							| 
									
										
										
										
											2020-06-10 20:49:41 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Messaging.markAllRead = async (uid) => { | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 		await db.delete(`uid:${uid}:chat:rooms:unread`); | 
					
						
							| 
									
										
										
										
											2020-06-10 20:49:41 -04:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2016-03-11 13:38:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-22 11:18:13 -04:00
										 |  |  | 	Messaging.markUnread = async (uids, roomId) => { | 
					
						
							|  |  |  | 		const exists = await Messaging.roomExists(roomId); | 
					
						
							|  |  |  | 		if (!exists) { | 
					
						
							|  |  |  | 			throw new Error('[[error:chat-room-does-not-exist]]'); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 		const keys = uids.map(uid => `uid:${uid}:chat:rooms:unread`); | 
					
						
							| 
									
										
										
										
											2019-07-22 11:18:13 -04:00
										 |  |  | 		return await db.sortedSetsAdd(keys, Date.now(), roomId); | 
					
						
							| 
									
										
										
										
											2015-12-15 14:10:32 +02:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2017-02-18 02:30:48 -07:00
										 |  |  | }; |