Skip to content
Snippets Groups Projects
sendMessage.coffee 4.89 KiB
Newer Older
RocketChat.sendMessage = (user, message, room, options) ->
	if not user or not message or not room._id
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
		return false
haosdent's avatar
haosdent committed
	unless message.ts?
		message.ts = new Date()
Gabriel Engel's avatar
Gabriel Engel committed

	message.u = _.pick user, ['_id','username']

	message.rid = room._id
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
	if urls = message.msg.match /([A-Za-z]{3,9}):\/\/([-;:&=\+\$,\w]+@{1})?([-A-Za-z0-9\.]+)+:?(\d+)?((\/[-\+=!:~%\/\.@\,\w]+)?\??([-\+=&!:;%@\/\.\,\w]+)?#?([\w]+)?)?/g
		message.urls = urls.map (url) -> url: url
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
	message = RocketChat.callbacks.run 'beforeSaveMessage', message
	if message._id? and options?.upsert is true
		RocketChat.models.Messages.upsert {_id: message._id}, message
		message._id = RocketChat.models.Messages.insert message

	###
	Defer other updates as their return is not interesting to the user
	###

Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
	###
	Execute all callbacks
	###
	Meteor.defer ->

		RocketChat.callbacks.run 'afterSaveMessage', message

	###
	Update all the room activity tracker fields
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
	###
	Meteor.defer ->
		RocketChat.models.Rooms.incUnreadAndSetLastMessageTimestampById message.rid, 1, message.ts
	###
	Increment unread couter if direct messages
	###
	Meteor.defer ->
		if not room.t? or room.t is 'd'
Gabriel Engel's avatar
Gabriel Engel committed
			###
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
			Update the other subscriptions
Gabriel Engel's avatar
Gabriel Engel committed
			###
			RocketChat.models.Subscriptions.incUnreadOfDirectForRoomIdExcludingUserId message.rid, message.u._id, 1
			userOfMention = RocketChat.models.Users.findOne({_id: message.rid.replace(message.u._id, '')}, {fields: {username: 1, statusConnection: 1}})
			if userOfMention?
				RocketChat.Notifications.notifyUser userOfMention._id, 'notification',
					title: "@#{user.username}"
					text: message.msg
					payload:
						rid: message.rid
						sender: message.u
						type: room.t
						name: room.name

				if Push.enabled is true and userOfMention.statusConnection isnt 'online'
					Push.send
						from: 'push'
						title: "@#{user.username}"
						text: message.msg
						apn:
							text: "@#{user.username}:\n#{message.msg}"
						badge: 1
						sound: 'chime'
						payload:
							host: Meteor.absoluteUrl()
							rid: message.rid
							sender: message.u
							type: room.t
							name: room.name
						query:
							userId: userOfMention._id

Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
		else
			mentionIds = []
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
			message.mentions?.forEach (mention) ->
				mentionIds.push mention._id

			# @all?
			toAll = mentionIds.indexOf('all') > -1

			if mentionIds.length > 0
				usersOfMention = RocketChat.models.Users.find({_id: {$in: mentionIds}}, {fields: {_id: 1, username: 1}}).fetch()
				if room.t is 'c' and !toAll
					for usersOfMentionItem in usersOfMention
						if room.usernames.indexOf(usersOfMentionItem.username) is -1
							Meteor.runAsUser usersOfMentionItem._id, ->
								Meteor.call 'joinRoom', room._id

Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
				Update all other subscriptions of mentioned users to alert their owners and incrementing
				the unread counter for mentions and direct messages
					RocketChat.models.Subscriptions.incUnreadForRoomIdExcludingUserId message.rid, user._id, 1
					RocketChat.models.Subscriptions.incUnreadForRoomIdAndUserIds message.rid, mentionIds, 1
				# Get ids of all mentioned users.
				userIdsToNotify = _.pluck(usersOfMention, '_id')
				userIdsToPushNotify = userIdsToNotify
				# If the message is @all, notify all room users except for the sender.
				if toAll and room.usernames?.length > 0
					usersOfRoom = RocketChat.models.Users.find({
							username: {$in: room.usernames},
							_id: {$ne: user._id}},
						{fields: {_id: 1, username: 1, status: 1}})
					onlineUsersOfRoom = _.filter usersOfRoom, (user) ->
						user.status in ['online', 'away', 'busy']
					userIdsToNotify = _.union userIdsToNotify, _.pluck(onlineUsersOfRoom, '_id')
					userIdsToPushNotify = _.union userIdsToPushNotify, _.pluck(usersOfRoom, '_id')

				if userIdsToNotify.length > 0
					for usersOfMentionId in userIdsToNotify
						RocketChat.Notifications.notifyUser usersOfMentionId, 'notification',
							title: "@#{user.username} @ ##{room.name}"
							text: message.msg
							payload:
								rid: message.rid
								sender: message.u
								type: room.t
								name: room.name
				if userIdsToPushNotify.length > 0
						Push.send
							from: 'push'
							title: "@#{user.username} @ ##{room.name}"
							text: message.msg
							apn:
								text: "@#{user.username} @ ##{room.name}:\n#{message.msg}"
							badge: 1
							sound: 'chime'
							payload:
								host: Meteor.absoluteUrl()
								rid: message.rid
								sender: message.u
								type: room.t
								name: room.name
								userId: $in: userIdsToPushNotify
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
		Update all other subscriptions to alert their owners but witout incrementing
		the unread counter, as it is only for mentions and direct messages
		RocketChat.models.Subscriptions.setAlertForRoomIdExcludingUserId message.rid, message.u._id, true
	return message