Skip to content
Snippets Groups Projects
sendMessage.coffee 2.73 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
	console.log '[functions] RocketChat.sendMessage -> '.green, 'arguments:', arguments
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
	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

	###
	Remove the typing record
	###
	Meteor.defer ->

			rid: message.rid
			'u._id': message.u._id

	###
	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
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
		else
			message.mentions?.forEach (mention) ->
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
				###
				ChatSubscription.update
					# only subscriptions to the same room
					rid: message.rid
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
					# the mentioned user
					'u._id': mention._id
				,
					$set:
						# alert de user
						alert: true
						# open the room for the user
						open: true
					# increment unread couter
					$inc:
						unread: 1
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: false
			# 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