<commit_before>RocketChat.models.Messages = new class extends RocketChat.models._Base
	constructor: ->
		super('message')

		@tryEnsureIndex { 'rid': 1, 'ts': 1 }
		@tryEnsureIndex { 'ts': 1 }
		@tryEnsureIndex { 'u._id': 1 }
		@tryEnsureIndex { 'editedAt': 1 }, { sparse: 1 }
		@tryEnsureIndex { 'editedBy._id': 1 }, { sparse: 1 }
		@tryEnsureIndex { 'rid': 1, 't': 1, 'u._id': 1 }
		@tryEnsureIndex { 'expireAt': 1 }, { expireAfterSeconds: 0 }
		@tryEnsureIndex { 'msg': 'text' }
		@tryEnsureIndex { 'file._id': 1 }, { sparse: 1 }
		@tryEnsureIndex { 'mentions.username': 1 }, { sparse: 1 }
		@tryEnsureIndex { 'pinned': 1 }, { sparse: 1 }
		@tryEnsureIndex { 'snippeted': 1 }, { sparse: 1 }
		@tryEnsureIndex { 'location': '2dsphere' }


	# FIND ONE
	findOneById: (_id, options) ->
		query =
			_id: _id

		return @findOne query, options

	# FIND
	findByMention: (username, options) ->
		query =
			"mentions.username": username

		return @find query, options

	findVisibleByMentionAndRoomId: (username, rid, options) ->
		query =
			_hidden: { $ne: true }
			"mentions.username": username
			"rid": rid

		return @find query, options

	findVisibleByRoomId: (roomId, options) ->
		query =
			_hidden:
				$ne: true
			rid: roomId

		return @find query, options

	findInvisibleByRoomId: (roomId, options) ->
		query =
			_hidden: true
			rid: roomId

		return @find query, options

	findVisibleByRoomIdAfterTimestamp: (roomId, timestamp, options) ->
		query =
			_hidden:
				$ne: true
			rid: roomId
			ts:
				$gt: timestamp

		return @find query, options

	findVisibleByRoomIdBeforeTimestamp: (roomId, timestamp, options) ->
		query =
			_hidden:
				$ne: true
			rid: roomId
			ts:
				$lt: timestamp

		return @find query, options

	findVisibleByRoomIdBetweenTimestamps: (roomId, afterTimestamp, beforeTimestamp, options) ->
		query =
			_hidden:
				$ne: true
			rid: roomId
			ts:
				$gt: afterTimestamp
				$lt: beforeTimestamp

		return @find query, options

	findVisibleCreatedOrEditedAfterTimestamp: (timestamp, options) ->
		query =
			_hidden: { $ne: true }
			$or: [
				ts:
					$gt: timestamp
			,
				'editedAt':
					$gt: timestamp
			]

		return @find query, options

	findStarredByUserAtRoom: (userId, roomId, options) ->
		query =
			_hidden: { $ne: true }
			'starred._id': userId
			rid: roomId

		return @find query, options

	findPinnedByRoom: (roomId, options) ->
		query =
			t: { $ne: 'rm' }
			_hidden: { $ne: true }
			pinned: true
			rid: roomId

		return @find query, options

	findSnippetedByRoom: (roomId, options) ->
		query =
			_hidden: { $ne: true }
			snippeted: true
			rid: roomId

		return @find query, options

	getLastTimestamp: (options = {}) ->
		query = { ts: { $exists: 1 } }
		options.sort = { ts: -1 }
		options.limit = 1

		return @find(query, options)?.fetch?()?[0]?.ts

	findByRoomIdAndMessageIds: (rid, messageIds, options) ->
		query =
			rid: rid
			_id:
				$in: messageIds

		return @find query, options

	cloneAndSaveAsHistoryById: (_id) ->
		me = RocketChat.models.Users.findOneById Meteor.userId()
		record = @findOneById _id
		record._hidden = true
		record.parent = record._id
		record.editedAt = new Date
		record.editedBy =
			_id: Meteor.userId()
			username: me.username
		delete record._id
		return @insert record

	# UPDATE
	setHiddenById: (_id, hidden=true) ->
		query =
			_id: _id

		update =
			$set:
				_hidden: hidden

		return @update query, update

	setAsDeletedByIdAndUser: (_id, user) ->
		query =
			_id: _id

		update =
			$set:
				msg: ''
				t: 'rm'
				urls: []
				mentions: []
				attachments: []
				editedAt: new Date()
				editedBy:
					_id: user._id
					username: user.username

		return @update query, update

	setPinnedByIdAndUserId: (_id, pinnedBy, pinned=true, pinnedAt=0) ->
		query =
			_id: _id

		update =
			$set:
				pinned: pinned
				pinnedAt: pinnedAt || new Date
				pinnedBy: pinnedBy

		return @update query, update

	setSnippetedByIdAndUserId: (_id, snippetedBy, snippeted=true, snippetedAt=0) ->
		query =
			_id: _id

		update =
			$set:
				snippeted: snippeted
				snippetedAt: snippetedAt || new Date
				snippetedBy: snippetedBy

		return @update query, update

	setUrlsById: (_id, urls) ->
		query =
			_id: _id

		update =
			$set:
				urls: urls

		return @update query, update

	updateAllUsernamesByUserId: (userId, username) ->
		query =
			'u._id': userId

		update =
			$set:
				"u.username": username
<commit_msg>Add syntax highlighting when a snippet is created.<commit_after>RocketChat.models.Messages = new class extends RocketChat.models._Base
	constructor: ->
		super('message')

		@tryEnsureIndex { 'rid': 1, 'ts': 1 }
		@tryEnsureIndex { 'ts': 1 }
		@tryEnsureIndex { 'u._id': 1 }
		@tryEnsureIndex { 'editedAt': 1 }, { sparse: 1 }
		@tryEnsureIndex { 'editedBy._id': 1 }, { sparse: 1 }
		@tryEnsureIndex { 'rid': 1, 't': 1, 'u._id': 1 }
		@tryEnsureIndex { 'expireAt': 1 }, { expireAfterSeconds: 0 }
		@tryEnsureIndex { 'msg': 'text' }
		@tryEnsureIndex { 'file._id': 1 }, { sparse: 1 }
		@tryEnsureIndex { 'mentions.username': 1 }, { sparse: 1 }
		@tryEnsureIndex { 'pinned': 1 }, { sparse: 1 }
		@tryEnsureIndex { 'snippeted': 1 }, { sparse: 1 }
		@tryEnsureIndex { 'location': '2dsphere' }


	# FIND ONE
	findOneById: (_id, options) ->
		query =
			_id: _id

		return @findOne query, options

	# FIND
	findByMention: (username, options) ->
		query =
			"mentions.username": username

		return @find query, options

	findVisibleByMentionAndRoomId: (username, rid, options) ->
		query =
			_hidden: { $ne: true }
			"mentions.username": username
			"rid": rid

		return @find query, options

	findVisibleByRoomId: (roomId, options) ->
		query =
			_hidden:
				$ne: true
			rid: roomId

		return @find query, options

	findInvisibleByRoomId: (roomId, options) ->
		query =
			_hidden: true
			rid: roomId

		return @find query, options

	findVisibleByRoomIdAfterTimestamp: (roomId, timestamp, options) ->
		query =
			_hidden:
				$ne: true
			rid: roomId
			ts:
				$gt: timestamp

		return @find query, options

	findVisibleByRoomIdBeforeTimestamp: (roomId, timestamp, options) ->
		query =
			_hidden:
				$ne: true
			rid: roomId
			ts:
				$lt: timestamp

		return @find query, options

	findVisibleByRoomIdBetweenTimestamps: (roomId, afterTimestamp, beforeTimestamp, options) ->
		query =
			_hidden:
				$ne: true
			rid: roomId
			ts:
				$gt: afterTimestamp
				$lt: beforeTimestamp

		return @find query, options

	findVisibleCreatedOrEditedAfterTimestamp: (timestamp, options) ->
		query =
			_hidden: { $ne: true }
			$or: [
				ts:
					$gt: timestamp
			,
				'editedAt':
					$gt: timestamp
			]

		return @find query, options

	findStarredByUserAtRoom: (userId, roomId, options) ->
		query =
			_hidden: { $ne: true }
			'starred._id': userId
			rid: roomId

		return @find query, options

	findPinnedByRoom: (roomId, options) ->
		query =
			t: { $ne: 'rm' }
			_hidden: { $ne: true }
			pinned: true
			rid: roomId

		return @find query, options

	findSnippetedByRoom: (roomId, options) ->
		query =
			_hidden: { $ne: true }
			snippeted: true
			rid: roomId

		return @find query, options

	getLastTimestamp: (options = {}) ->
		query = { ts: { $exists: 1 } }
		options.sort = { ts: -1 }
		options.limit = 1

		return @find(query, options)?.fetch?()?[0]?.ts

	findByRoomIdAndMessageIds: (rid, messageIds, options) ->
		query =
			rid: rid
			_id:
				$in: messageIds

		return @find query, options

	cloneAndSaveAsHistoryById: (_id) ->
		me = RocketChat.models.Users.findOneById Meteor.userId()
		record = @findOneById _id
		record._hidden = true
		record.parent = record._id
		record.editedAt = new Date
		record.editedBy =
			_id: Meteor.userId()
			username: me.username
		delete record._id
		return @insert record

	# UPDATE
	setHiddenById: (_id, hidden=true) ->
		query =
			_id: _id

		update =
			$set:
				_hidden: hidden

		return @update query, update

	setAsDeletedByIdAndUser: (_id, user) ->
		query =
			_id: _id

		update =
			$set:
				msg: ''
				t: 'rm'
				urls: []
				mentions: []
				attachments: []
				editedAt: new Date()
				editedBy:
					_id: user._id
					username: user.username

		return @update query, update

	setPinnedByIdAndUserId: (_id, pinnedBy, pinned=true, pinnedAt=0) ->
		query =
			_id: _id

		update =
			$set:
				pinned: pinned
				pinnedAt: pinnedAt || new Date
				pinnedBy: pinnedBy

		return @update query, update

	setSnippetedByIdAndUserId: (message, snippetedBy, snippeted=true, snippetedAt=0) ->
		query =
			_id: message._id


		msg = message.msg

		if RocketChat.settings.get "Message_SnippetReplaceTabs"
			nbSpaces = RocketChat.settings.get "Message_SnippetNbSpaces"

			msg = msg.replace /\t/g, Array(nbSpaces).join " "

		msg = "```" + msg + "```"

		update =
			$set:
				msg: msg
				snippeted: snippeted
				snippetedAt: snippetedAt || new Date
				snippetedBy: snippetedBy

		return @update query, update

	setUrlsById: (_id, urls) ->
		query =
			_id: _id

		update =
			$set:
				urls: urls

		return @update query, update

	updateAllUsernamesByUserId: (userId, username) ->
		query =
			'u._id': userId

		update =
			$set:
				"u.username": username

{'fraction_non_alphanumeric': 0.12709832134292565, 'fraction_numerical': 0.006104207543056464, 'mean_word_length': 3.119892231701841, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1082079', 'n_tokens_mistral': 3965, 'n_tokens_neox': 3271, 'n_words': 849}