Skip to content
Snippets Groups Projects
sendMessage.coffee 4.1 KiB
Newer Older
RocketChat.sendMessage = (user, message, room) ->
	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
	message._id = ChatMessage.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 ->
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
		ChatRoom.update
			# only subscriptions to the same room
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
		,
			# update the last message timestamp
			$set:
				lm: message.ts
			# increment the messages counter
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
			$inc:
				msgs: 1

	###
	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
			###
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
			ChatSubscription.update
Gabriel Engel's avatar
Gabriel Engel committed
				# only subscriptions to the same room
				rid: message.rid
				# only direct messages subscriptions
				t: 'd'
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
				# not the msg owner
				'u._id':
					$ne: message.u._id
Gabriel Engel's avatar
Gabriel Engel committed
			,
				$set:
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
					# alert de user
					alert: true
					# open the room for the user
					open: true
				# increment unread couter
Gabriel Engel's avatar
Gabriel Engel committed
				$inc:
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
					unread: 1
			if Push.enabled is true
				userOfMention = Meteor.users.findOne({_id: message.rid.replace(message.u._id, ''), statusConnection: {$ne: 'online'}}, {fields: {username: 1}})
				if userOfMention?
					Push.send
						from: 'push'
						title: "@#{user.username}"
						text: message.msg
						apn:
							text: "@#{user.username}:\n#{message.msg}"
						badge: 1
						sound: 'chime'
						payload:
							rid: message.rid
							sender: message.u
						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

			if mentionIds.length > 0
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
					# only subscriptions to the same room
					rid: message.rid
				if mentionIds.indexOf('all') > -1
					# all users except sender if mention is for all
					query['u._id'] = $ne: user._id
				else
					# the mentioned user if mention isn't for all
					query['u._id'] = $in: mentionIds
					$set:
						# alert de user
						alert: true
						# open the room for the user
						open: true
					# increment unread couter
					$inc:
						unread: 1
				if Push.enabled is true
					query =
						statusConnection: {$ne: 'online'}

					if mentionIds.indexOf('all') > -1
						if room.usernames?.length > 0
							query.username =
								$in: room.usernames
						else
							query.username =
								$in: []
					else
						query._id =
							$in: mentionIds

					usersOfMention = Meteor.users.find(query, {fields: {_id: 1}}).fetch()
					usersOfMentionIds = _.pluck(usersOfMention, '_id');
					if usersOfMentionIds.length > 0
						Push.send
							from: 'push'
							title: "##{room.name}"
							text: message.msg
							apn:
								text: "##{room.name}:\n#{message.msg}"
							badge: 1
							sound: 'chime'
							payload:
								rid: message.rid
								sender: message.u
							query:
								userId: $in: usersOfMentionIds
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
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
		ChatSubscription.update
			# only subscriptions to the same room
			rid: message.rid
			# only the ones that have not been alerted yet
			alert: { $ne: true }
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
			# not the msg owner
			'u._id':
				$ne: message.u._id
		,
			$set:
				# alert de user
				alert: true
				# open the room for the user
				open: true
		,
			# make sure we alert all matching subscription
			multi: true

	return message