| 
									
										
										
										
											2014-03-08 15:45:42 -05:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-15 17:50:30 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-22 11:18:13 -04:00
										 |  |  | const validator = require('validator'); | 
					
						
							| 
									
										
										
										
											2016-10-03 20:35:36 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-22 11:18:13 -04:00
										 |  |  | const db = require('../database'); | 
					
						
							|  |  |  | const user = require('../user'); | 
					
						
							| 
									
										
										
										
											2020-10-13 22:42:50 -04:00
										 |  |  | const privileges = require('../privileges'); | 
					
						
							| 
									
										
										
										
											2019-07-22 11:18:13 -04:00
										 |  |  | const plugins = require('../plugins'); | 
					
						
							|  |  |  | const meta = require('../meta'); | 
					
						
							|  |  |  | const utils = require('../utils'); | 
					
						
							| 
									
										
										
										
											2013-08-26 13:18:20 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-22 11:18:13 -04:00
										 |  |  | const Messaging = module.exports; | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-23 15:03:32 -04:00
										 |  |  | require('./data')(Messaging); | 
					
						
							|  |  |  | require('./create')(Messaging); | 
					
						
							|  |  |  | require('./delete')(Messaging); | 
					
						
							|  |  |  | require('./edit')(Messaging); | 
					
						
							|  |  |  | require('./rooms')(Messaging); | 
					
						
							|  |  |  | require('./unread')(Messaging); | 
					
						
							|  |  |  | require('./notifications')(Messaging); | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-20 11:30:43 -05:00
										 |  |  | Messaging.messageExists = async mid => db.exists(`message:${mid}`); | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-13 14:36:15 -04:00
										 |  |  | Messaging.getMessages = async (params) => { | 
					
						
							| 
									
										
										
										
											2019-07-22 11:18:13 -04:00
										 |  |  | 	const isNew = params.isNew || false; | 
					
						
							|  |  |  | 	const start = params.hasOwnProperty('start') ? params.start : 0; | 
					
						
							|  |  |  | 	const stop = parseInt(start, 10) + ((params.count || 50) - 1); | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:50 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-22 11:18:13 -04:00
										 |  |  | 	const indices = {}; | 
					
						
							|  |  |  | 	const ok = await canGet('filter:messaging.canGetMessages', params.callerUid, params.uid); | 
					
						
							|  |  |  | 	if (!ok) { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-10-03 20:35:36 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 	const mids = await db.getSortedSetRevRange(`uid:${params.uid}:chat:room:${params.roomId}:mids`, start, stop); | 
					
						
							| 
									
										
										
										
											2019-07-22 11:18:13 -04:00
										 |  |  | 	if (!mids.length) { | 
					
						
							|  |  |  | 		return []; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	mids.forEach((mid, index) => { | 
					
						
							| 
									
										
										
										
											2019-07-22 11:18:13 -04:00
										 |  |  | 		indices[mid] = start + index; | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 	mids.reverse(); | 
					
						
							| 
									
										
										
										
											2016-10-03 20:35:36 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-14 23:26:54 -05:00
										 |  |  | 	const messageData = await Messaging.getMessagesData(mids, params.uid, params.roomId, isNew); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	messageData.forEach((messageData) => { | 
					
						
							| 
									
										
										
										
											2019-07-22 11:18:13 -04:00
										 |  |  | 		messageData.index = indices[messageData.messageId.toString()]; | 
					
						
							| 
									
										
										
										
											2020-02-14 23:26:54 -05:00
										 |  |  | 		messageData.isOwner = messageData.fromuid === parseInt(params.uid, 10); | 
					
						
							|  |  |  | 		if (messageData.deleted && !messageData.isOwner) { | 
					
						
							|  |  |  | 			messageData.content = '[[modules:chat.message-deleted]]'; | 
					
						
							| 
									
										
										
										
											2022-04-12 15:36:06 -04:00
										 |  |  | 			messageData.cleanedContent = messageData.content; | 
					
						
							| 
									
										
										
										
											2020-02-14 23:26:54 -05:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-07-22 11:18:13 -04:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2017-12-18 14:45:06 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-22 11:18:13 -04:00
										 |  |  | 	return messageData; | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-22 11:18:13 -04:00
										 |  |  | async function canGet(hook, callerUid, uid) { | 
					
						
							| 
									
										
										
										
											2020-11-20 16:06:26 -05:00
										 |  |  | 	const data = await plugins.hooks.fire(hook, { | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | 		callerUid: callerUid, | 
					
						
							|  |  |  | 		uid: uid, | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 		canGet: parseInt(callerUid, 10) === parseInt(uid, 10), | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2019-07-22 11:18:13 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return data ? data.canGet : false; | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-22 11:18:13 -04:00
										 |  |  | Messaging.parse = async (message, fromuid, uid, roomId, isNew) => { | 
					
						
							| 
									
										
										
										
											2021-03-15 17:55:14 -04:00
										 |  |  | 	const parsed = await plugins.hooks.fire('filter:parse.raw', String(message || '')); | 
					
						
							| 
									
										
										
										
											2019-07-22 11:18:13 -04:00
										 |  |  | 	let messageData = { | 
					
						
							|  |  |  | 		message: message, | 
					
						
							|  |  |  | 		parsed: parsed, | 
					
						
							|  |  |  | 		fromuid: fromuid, | 
					
						
							|  |  |  | 		uid: uid, | 
					
						
							|  |  |  | 		roomId: roomId, | 
					
						
							|  |  |  | 		isNew: isNew, | 
					
						
							|  |  |  | 		parsedMessage: parsed, | 
					
						
							|  |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-08-26 13:18:20 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-20 16:06:26 -05:00
										 |  |  | 	messageData = await plugins.hooks.fire('filter:messaging.parse', messageData); | 
					
						
							| 
									
										
										
										
											2019-07-22 11:18:13 -04:00
										 |  |  | 	return messageData ? messageData.parsedMessage : ''; | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-22 11:18:13 -04:00
										 |  |  | Messaging.isNewSet = async (uid, roomId, timestamp) => { | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 	const setKey = `uid:${uid}:chat:room:${roomId}:mids`; | 
					
						
							| 
									
										
										
										
											2019-07-22 11:18:13 -04:00
										 |  |  | 	const messages = await db.getSortedSetRevRangeWithScores(setKey, 0, 0); | 
					
						
							|  |  |  | 	if (messages && messages.length) { | 
					
						
							|  |  |  | 		return parseInt(timestamp, 10) > parseInt(messages[0].score, 10) + Messaging.newMessageCutoff; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return true; | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-22 11:18:13 -04:00
										 |  |  | Messaging.getRecentChats = async (callerUid, uid, start, stop) => { | 
					
						
							|  |  |  | 	const ok = await canGet('filter:messaging.canGetRecentChats', callerUid, uid); | 
					
						
							|  |  |  | 	if (!ok) { | 
					
						
							|  |  |  | 		return null; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 	const roomIds = await db.getSortedSetRevRange(`uid:${uid}:chat:rooms`, start, stop); | 
					
						
							| 
									
										
										
										
											2019-07-22 11:18:13 -04:00
										 |  |  | 	const results = await utils.promiseParallel({ | 
					
						
							|  |  |  | 		roomData: Messaging.getRoomsData(roomIds), | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 		unread: db.isSortedSetMembers(`uid:${uid}:chat:rooms:unread`, roomIds), | 
					
						
							| 
									
										
										
										
											2019-08-13 14:36:15 -04:00
										 |  |  | 		users: Promise.all(roomIds.map(async (roomId) => { | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 			let uids = await db.getSortedSetRevRange(`chat:room:${roomId}:uids`, 0, 9); | 
					
						
							| 
									
										
										
										
											2020-02-14 23:26:54 -05:00
										 |  |  | 			uids = uids.filter(_uid => _uid && parseInt(_uid, 10) !== parseInt(uid, 10)); | 
					
						
							| 
									
										
										
										
											2019-07-22 11:18:13 -04:00
										 |  |  | 			return await user.getUsersFields(uids, ['uid', 'username', 'userslug', 'picture', 'status', 'lastonline']); | 
					
						
							|  |  |  | 		})), | 
					
						
							|  |  |  | 		teasers: Promise.all(roomIds.map(async roomId => Messaging.getTeaser(uid, roomId))), | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	results.roomData.forEach((room, index) => { | 
					
						
							| 
									
										
										
										
											2019-07-22 11:18:13 -04:00
										 |  |  | 		if (room) { | 
					
						
							|  |  |  | 			room.users = results.users[index]; | 
					
						
							|  |  |  | 			room.groupChat = room.hasOwnProperty('groupChat') ? room.groupChat : room.users.length > 2; | 
					
						
							|  |  |  | 			room.unread = results.unread[index]; | 
					
						
							|  |  |  | 			room.teaser = results.teasers[index]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 			room.users.forEach((userData) => { | 
					
						
							| 
									
										
										
										
											2019-07-22 11:18:13 -04:00
										 |  |  | 				if (userData && parseInt(userData.uid, 10)) { | 
					
						
							|  |  |  | 					userData.status = user.getStatus(userData); | 
					
						
							| 
									
										
										
										
											2017-10-12 17:05:15 -04:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 			room.users = room.users.filter(user => user && parseInt(user.uid, 10)); | 
					
						
							| 
									
										
										
										
											2019-07-22 11:18:13 -04:00
										 |  |  | 			room.lastUser = room.users[0]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			room.usernames = Messaging.generateUsernames(room.users, uid); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2016-09-30 19:39:08 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-22 11:18:13 -04:00
										 |  |  | 	results.roomData = results.roomData.filter(Boolean); | 
					
						
							|  |  |  | 	const ref = { rooms: results.roomData, nextStart: stop + 1 }; | 
					
						
							| 
									
										
										
										
											2020-11-20 16:06:26 -05:00
										 |  |  | 	return await plugins.hooks.fire('filter:messaging.getRecentChats', { | 
					
						
							| 
									
										
										
										
											2019-07-22 11:18:13 -04:00
										 |  |  | 		rooms: ref.rooms, | 
					
						
							|  |  |  | 		nextStart: ref.nextStart, | 
					
						
							|  |  |  | 		uid: uid, | 
					
						
							|  |  |  | 		callerUid: callerUid, | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-23 22:19:15 -05:00
										 |  |  | Messaging.generateUsernames = (users, excludeUid) => users.filter(user => user && parseInt(user.uid, 10) !== excludeUid) | 
					
						
							|  |  |  | 	.map(user => user.username).join(', '); | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-22 11:18:13 -04:00
										 |  |  | Messaging.getTeaser = async (uid, roomId) => { | 
					
						
							|  |  |  | 	const mid = await Messaging.getLatestUndeletedMessage(uid, roomId); | 
					
						
							|  |  |  | 	if (!mid) { | 
					
						
							|  |  |  | 		return null; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	const teaser = await Messaging.getMessageFields(mid, ['fromuid', 'content', 'timestamp']); | 
					
						
							|  |  |  | 	if (!teaser.fromuid) { | 
					
						
							|  |  |  | 		return null; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	const blocked = await user.blocks.is(teaser.fromuid, uid); | 
					
						
							|  |  |  | 	if (blocked) { | 
					
						
							|  |  |  | 		return null; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-12-17 12:35:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-22 11:18:13 -04:00
										 |  |  | 	teaser.user = await user.getUserFields(teaser.fromuid, ['uid', 'username', 'userslug', 'picture', 'status', 'lastonline']); | 
					
						
							|  |  |  | 	if (teaser.content) { | 
					
						
							|  |  |  | 		teaser.content = utils.stripHTMLTags(utils.decodeHTMLEntities(teaser.content)); | 
					
						
							|  |  |  | 		teaser.content = validator.escape(String(teaser.content)); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-20 16:06:26 -05:00
										 |  |  | 	const payload = await plugins.hooks.fire('filter:messaging.getTeaser', { teaser: teaser }); | 
					
						
							| 
									
										
										
										
											2019-07-22 11:18:13 -04:00
										 |  |  | 	return payload.teaser; | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2015-04-16 20:48:38 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-22 11:18:13 -04:00
										 |  |  | Messaging.getLatestUndeletedMessage = async (uid, roomId) => { | 
					
						
							|  |  |  | 	let done = false; | 
					
						
							|  |  |  | 	let latestMid = null; | 
					
						
							|  |  |  | 	let index = 0; | 
					
						
							|  |  |  | 	let mids; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	while (!done) { | 
					
						
							|  |  |  | 		/* eslint-disable no-await-in-loop */ | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 		mids = await db.getSortedSetRevRange(`uid:${uid}:chat:room:${roomId}:mids`, index, index); | 
					
						
							| 
									
										
										
										
											2019-07-22 11:18:13 -04:00
										 |  |  | 		if (mids.length) { | 
					
						
							|  |  |  | 			const states = await Messaging.getMessageFields(mids[0], ['deleted', 'system']); | 
					
						
							|  |  |  | 			done = !states.deleted && !states.system; | 
					
						
							|  |  |  | 			if (done) { | 
					
						
							|  |  |  | 				latestMid = mids[0]; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			index += 1; | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			done = true; | 
					
						
							| 
									
										
										
										
											2019-06-05 22:04:19 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-07-22 11:18:13 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return latestMid; | 
					
						
							| 
									
										
										
										
											2019-06-05 22:04:19 -04:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-22 11:18:13 -04:00
										 |  |  | Messaging.canMessageUser = async (uid, toUid) => { | 
					
						
							| 
									
										
										
										
											2021-11-16 11:57:00 -05:00
										 |  |  | 	if (meta.config.disableChat || uid <= 0) { | 
					
						
							| 
									
										
										
										
											2019-07-22 11:18:13 -04:00
										 |  |  | 		throw new Error('[[error:chat-disabled]]'); | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-10-22 10:22:05 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | 	if (parseInt(uid, 10) === parseInt(toUid, 10)) { | 
					
						
							| 
									
										
										
										
											2021-11-16 11:57:00 -05:00
										 |  |  | 		throw new Error('[[error:cant-chat-with-yourself]]'); | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-11-16 11:57:00 -05:00
										 |  |  | 	const [exists, canChat] = await Promise.all([ | 
					
						
							|  |  |  | 		user.exists(toUid), | 
					
						
							|  |  |  | 		privileges.global.can('chat', uid), | 
					
						
							|  |  |  | 	]); | 
					
						
							| 
									
										
										
										
											2014-07-19 10:33:27 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-22 11:18:13 -04:00
										 |  |  | 	if (!exists) { | 
					
						
							|  |  |  | 		throw new Error('[[error:no-user]]'); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-04-16 20:48:38 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-13 22:42:50 -04:00
										 |  |  | 	if (!canChat) { | 
					
						
							|  |  |  | 		throw new Error('[[error:no-privileges]]'); | 
					
						
							| 
									
										
										
										
											2019-07-22 11:18:13 -04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-09-16 13:27:26 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-16 11:57:00 -05:00
										 |  |  | 	const [settings, isAdmin, isModerator, isFollowing, isBlocked] = await Promise.all([ | 
					
						
							|  |  |  | 		user.getSettings(toUid), | 
					
						
							|  |  |  | 		user.isAdministrator(uid), | 
					
						
							|  |  |  | 		user.isModeratorOfAnyCategory(uid), | 
					
						
							|  |  |  | 		user.isFollowing(toUid, uid), | 
					
						
							|  |  |  | 		user.blocks.is(uid, toUid), | 
					
						
							|  |  |  | 	]); | 
					
						
							| 
									
										
										
										
											2019-07-22 11:18:13 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-16 11:57:00 -05:00
										 |  |  | 	if (isBlocked || (settings.restrictChat && !isAdmin && !isModerator && !isFollowing)) { | 
					
						
							| 
									
										
										
										
											2019-07-22 11:18:13 -04:00
										 |  |  | 		throw new Error('[[error:chat-restricted]]'); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-20 16:06:26 -05:00
										 |  |  | 	await plugins.hooks.fire('static:messaging.canMessageUser', { | 
					
						
							| 
									
										
										
										
											2019-07-22 11:18:13 -04:00
										 |  |  | 		uid: uid, | 
					
						
							|  |  |  | 		toUid: toUid, | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2016-02-29 10:36:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-22 11:18:13 -04:00
										 |  |  | Messaging.canMessageRoom = async (uid, roomId) => { | 
					
						
							| 
									
										
										
										
											2018-11-17 22:31:39 -05:00
										 |  |  | 	if (meta.config.disableChat || uid <= 0) { | 
					
						
							| 
									
										
										
										
											2019-07-22 11:18:13 -04:00
										 |  |  | 		throw new Error('[[error:chat-disabled]]'); | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-02-29 10:36:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-22 11:18:13 -04:00
										 |  |  | 	const inRoom = await Messaging.isUserInRoom(uid, roomId); | 
					
						
							|  |  |  | 	if (!inRoom) { | 
					
						
							|  |  |  | 		throw new Error('[[error:not-in-room]]'); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-02-29 10:36:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-13 22:42:50 -04:00
										 |  |  | 	const canChat = await privileges.global.can('chat', uid); | 
					
						
							|  |  |  | 	if (!canChat) { | 
					
						
							|  |  |  | 		throw new Error('[[error:no-privileges]]'); | 
					
						
							| 
									
										
										
										
											2019-07-22 11:18:13 -04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-10-30 17:50:07 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-20 16:06:26 -05:00
										 |  |  | 	await plugins.hooks.fire('static:messaging.canMessageRoom', { | 
					
						
							| 
									
										
										
										
											2019-07-22 11:18:13 -04:00
										 |  |  | 		uid: uid, | 
					
						
							|  |  |  | 		roomId: roomId, | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2016-01-18 15:35:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-22 11:18:13 -04:00
										 |  |  | Messaging.hasPrivateChat = async (uid, withUid) => { | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | 	if (parseInt(uid, 10) === parseInt(withUid, 10)) { | 
					
						
							| 
									
										
										
										
											2019-07-22 11:18:13 -04:00
										 |  |  | 		return 0; | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-01-18 15:35:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-22 11:18:13 -04:00
										 |  |  | 	const results = await utils.promiseParallel({ | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 		myRooms: db.getSortedSetRevRange(`uid:${uid}:chat:rooms`, 0, -1), | 
					
						
							|  |  |  | 		theirRooms: db.getSortedSetRevRange(`uid:${withUid}:chat:rooms`, 0, -1), | 
					
						
							| 
									
										
										
										
											2019-07-22 11:18:13 -04:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	const roomIds = results.myRooms.filter(roomId => roomId && results.theirRooms.includes(roomId)); | 
					
						
							| 
									
										
										
										
											2015-12-11 12:07:02 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-22 11:18:13 -04:00
										 |  |  | 	if (!roomIds.length) { | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 	let index = 0; | 
					
						
							|  |  |  | 	let roomId = 0; | 
					
						
							| 
									
										
										
										
											2019-07-22 11:18:13 -04:00
										 |  |  | 	while (index < roomIds.length && !roomId) { | 
					
						
							|  |  |  | 		/* eslint-disable no-await-in-loop */ | 
					
						
							|  |  |  | 		const count = await Messaging.getUserCountInRoom(roomIds[index]); | 
					
						
							|  |  |  | 		if (count === 2) { | 
					
						
							|  |  |  | 			roomId = roomIds[index]; | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			index += 1; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return roomId; | 
					
						
							| 
									
										
										
										
											2017-01-03 20:02:24 +03:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2018-08-31 11:04:42 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-22 14:58:42 -05:00
										 |  |  | Messaging.canViewMessage = async (mids, roomId, uid) => { | 
					
						
							|  |  |  | 	let single = false; | 
					
						
							|  |  |  | 	if (!Array.isArray(mids) && isFinite(mids)) { | 
					
						
							|  |  |  | 		mids = [mids]; | 
					
						
							|  |  |  | 		single = true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	const canView = await db.isSortedSetMembers(`uid:${uid}:chat:room:${roomId}:mids`, mids); | 
					
						
							|  |  |  | 	return single ? canView.pop() : canView; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-24 12:02:16 -04:00
										 |  |  | require('../promisify')(Messaging); |