<commit_before>/* globals filteredUsersMemory */
filteredUsersMemory = new Mongo.Collection(null);

Meteor.startup(function() {
	Tracker.autorun(function() {
		if (Meteor.user() == null || Session.get('openedRoom') == null) {
			return;
		}
		filteredUsersMemory.remove({});
		const messageUsers = RocketChat.models.Messages.find({
			rid: Session.get('openedRoom'),
			'u.username': {
				$ne: Meteor.user().username
			}
		}, {
			fields: {
				'u.username': 1,
				'u.name': 1,
				ts: 1
			},
			sort: {
				ts: -1
			}
		}).fetch();
		const uniqueMessageUsersControl = {};
		return messageUsers.forEach(function(messageUser) {
			if (uniqueMessageUsersControl[messageUser.u.username] == null) {
				uniqueMessageUsersControl[messageUser.u.username] = true;
				return filteredUsersMemory.upsert(messageUser.u.username, {
					_id: messageUser.u.username,
					username: messageUser.u.username,
					name: messageUser.u.name,
					status: Session.get(`user_${ messageUser.u.username }_status`) || 'offline',
					ts: messageUser.ts
				});
			}
		});
	});
});

const getUsersFromServer = (filter, records, cb, rid) => {
	const messageUsers = _.pluck(records, 'username');
	return Meteor.call('spotlight', filter, messageUsers, {
		users: true
	}, rid, function(err, results) {
		if (err != null) {
			return console.error(err);
		}
		if (results.users.length > 0) {
			results.users.forEach(result => {
				if (records.length < 5) {
					records.push({
						_id: result.username,
						username: result.username,
						status: 'offline',
						sort: 3
					});
				}
			});
			records = _.sortBy(records, 'sort');
			return cb(records);
		}
	});
};

const getRoomsFromServer = (filter, records, cb, rid) => {
	if (!RocketChat.authz.hasAllPermission('view-outside-room')) {
		return cb([]);
	}
	return Meteor.call('spotlight', filter, null, {
		rooms: true
	}, rid, function(err, results) {
		if (err != null) {
			return console.error(err);
		}
		if (results.rooms.length > 0) {
			results.rooms.forEach(room => {
				if (records.length < 5) {
					records.push(room);
				}
			});
			return cb(records);
		}
	});
};

const getUsersFromServerDelayed = _.throttle(getUsersFromServer, 500);

const getRoomsFromServerDelayed = _.throttle(getRoomsFromServer, 500);

Template.messagePopupConfig.helpers({
	popupUserConfig() {
		const self = this;
		const config = {
			title: t('People'),
			collection: filteredUsersMemory,
			template: 'messagePopupUser',
			getInput: self.getInput,
			textFilterDelay: 200,
			trigger: '@',
			suffix: ' ',
			getFilter(collection, filter, cb) {
				let exp = new RegExp(`${ RegExp.escape(filter) }`, 'i');
				// Get users from messages
				const items = filteredUsersMemory.find({
					ts: {
						$exists: true
					},
					$or: [
						{
							username: exp
						}, {
							name: exp
						}
					]
				}, {
					limit: 5,
					sort: {
						ts: -1
					}
				}).fetch();
				// Get online users
				if (items.length < 5 && filter && filter.trim() !== '') {
					const messageUsers = _.pluck(items, 'username');
					const user = Meteor.user();
					if (!RocketChat.authz.hasAllPermission('view-outside-room')) {
						const usernames = RocketChat.models.Subscriptions.find({$or :[{'name': exp}, { fname: exp}]}).fetch().map(({name}) =>name);
						items.push(...
							RocketChat.models.Users.find({username:{$in:usernames}}, {fields:{
								username: 1,
								name: 1,
								status: 1
							}}).fetch()
						);
					} else {
						items.push(...Meteor.users.find({
							$and: [
								{
									$or: [
										{
											username: exp
										}, {
											name: exp
										}
									]
								}, {
									username: {
										$nin: [(user && user.username), ...messageUsers]
									}
								}
							]
						}, {
							limit: 5 - messageUsers.length
						}).fetch().map(function(item) {
							return {
								_id: item.username,
								username: item.username,
								name: item.name,
								status: item.status,
								sort: 1
							};
						}));
					}
				}
				// Get users from db
				if (items.length < 5 && filter && filter.trim() !== '') {
					getUsersFromServerDelayed(filter, items, cb, RocketChat.openedRoom);
				}
				const all = {
					_id: 'all',
					username: 'all',
					system: true,
					name: t('Notify_all_in_this_room'),
					compatibility: 'channel group',
					sort: 4
				};
				exp = new RegExp(`(^|\\s)${ RegExp.escape(filter) }`, 'i');
				if (exp.test(all.username) || exp.test(all.compatibility)) {
					items.push(all);<commit_msg>fix mentions from outside<commit_after>/* globals filteredUsersMemory */
filteredUsersMemory = new Mongo.Collection(null);

Meteor.startup(function() {
	Tracker.autorun(function() {
		if (Meteor.user() == null || Session.get('openedRoom') == null) {
			return;
		}
		filteredUsersMemory.remove({});
		const messageUsers = RocketChat.models.Messages.find({
			rid: Session.get('openedRoom'),
			'u.username': {
				$ne: Meteor.user().username
			}
		}, {
			fields: {
				'u.username': 1,
				'u.name': 1,
				ts: 1
			},
			sort: {
				ts: -1
			}
		}).fetch();
		const uniqueMessageUsersControl = {};
		return messageUsers.forEach(function(messageUser) {
			if (uniqueMessageUsersControl[messageUser.u.username] == null) {
				uniqueMessageUsersControl[messageUser.u.username] = true;
				return filteredUsersMemory.upsert(messageUser.u.username, {
					_id: messageUser.u.username,
					username: messageUser.u.username,
					name: messageUser.u.name,
					status: Session.get(`user_${ messageUser.u.username }_status`) || 'offline',
					ts: messageUser.ts
				});
			}
		});
	});
});

const getUsersFromServer = (filter, records, cb, rid) => {
	const messageUsers = _.pluck(records, 'username');
	return Meteor.call('spotlight', filter, messageUsers, {
		users: true
	}, rid, function(err, results) {
		if (err != null) {
			return console.error(err);
		}
		if (results.users.length > 0) {
			results.users.forEach(result => {
				if (records.length < 5) {
					records.push({
						_id: result.username,
						username: result.username,
						status: 'offline',
						sort: 3
					});
				}
			});
			records = _.sortBy(records, 'sort');
			return cb(records);
		}
	});
};

const getRoomsFromServer = (filter, records, cb, rid) => {
	if (!RocketChat.authz.hasAllPermission('view-outside-room')) {
		return cb([]);
	}
	return Meteor.call('spotlight', filter, null, {
		rooms: true
	}, rid, function(err, results) {
		if (err != null) {
			return console.error(err);
		}
		if (results.rooms.length > 0) {
			results.rooms.forEach(room => {
				if (records.length < 5) {
					records.push(room);
				}
			});
			return cb(records);
		}
	});
};

const getUsersFromServerDelayed = _.throttle(getUsersFromServer, 500);

const getRoomsFromServerDelayed = _.throttle(getRoomsFromServer, 500);

Template.messagePopupConfig.helpers({
	popupUserConfig() {
		const self = this;
		const config = {
			title: t('People'),
			collection: filteredUsersMemory,
			template: 'messagePopupUser',
			getInput: self.getInput,
			textFilterDelay: 200,
			trigger: '@',
			suffix: ' ',
			getFilter(collection, filter, cb) {
				let exp = new RegExp(`${ RegExp.escape(filter) }`, 'i');
				// Get users from messages
				const items = filteredUsersMemory.find({
					ts: {
						$exists: true
					},
					$or: [
						{
							username: exp
						}, {
							name: exp
						}
					]
				}, {
					limit: 5,
					sort: {
						ts: -1
					}
				}).fetch();
				// Get online users
				if (items.length < 5 && filter && filter.trim() !== '') {
					const messageUsers = _.pluck(items, 'username');
					const user = Meteor.user();
					if (!RocketChat.authz.hasAllPermission('view-outside-room')) {
						const usernames = RocketChat.models.Subscriptions.find({$or :[{'name': exp}, { fname: exp}]}).fetch().map(({name}) =>name);
						items.push(...
							RocketChat.models.Users.find({username:{$in:usernames}}, {fields:{
								username: 1,
								name: 1,
								status: 1
							}}, {
								limit: 5 - messageUsers.length
							}).fetch().map(({username, name, status}) => ({ _id: username, username, name, status, sort: 1 }))
						);
					} else {
						items.push(...Meteor.users.find({
							$and: [
								{
									$or: [
										{
											username: exp
										}, {
											name: exp
										}
									]
								}, {
									username: {
										$nin: [(user && user.username), ...messageUsers]
									}
								}
							]
						}, {
							limit: 5 - messageUsers.length
						}).fetch().map(({username, name, status}) => ({ _id: username, username, name, status, sort: 1 })));
					}
				}
				// Get users from db
				if (items.length < 5 && filter && filter.trim() !== '') {
					getUsersFromServerDelayed(filter, items, cb, RocketChat.openedRoom);
				}
				const all = {
					_id: 'all',
					username: 'all',
					system: true,
					name: t('Notify_all_in_this_room'),
					compatibility: 'channel group',
					sort: 4
				};
				exp = new RegExp(`(^|\\s)${ RegExp.escape(filter) }`, 'i');
				if (exp.test(all.username) || exp.test(all.compatibility)) {
					items.push(all);
{'fraction_non_alphanumeric': 0.16724928992790036, 'fraction_numerical': 0.00633602796591654, 'mean_word_length': 2.5279383429672446, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 19, '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': '678349', 'n_tokens_mistral': 4171, 'n_tokens_neox': 2996, 'n_words': 600}