Meteor.methods({
	channelsList(filter, channelType, limit, sort) {
		this.unblock();

		check(filter, String);
		check(channelType, String);
		check(limit, Match.Optional(Number));
		check(sort, Match.Optional(String));

		if (!Meteor.userId()) {
			throw new Meteor.Error('error-invalid-user', 'Invalid user', {
				method: 'channelsList'
			});
		}

		const options = {
			fields: {
				name: 1,
				t: 1
			},
			sort: {
				msgs: -1
			}
		};

		if (_.isNumber(limit)) {
			options.limit = limit;
		}

		if (_.trim(sort)) {
			switch (sort) {
				case 'name':
					options.sort = {
						name: 1
					};
					break;
				case 'msgs':
					options.sort = {
						msgs: -1
					};
			}
		}

		const roomTypes = [];

		if (channelType !== 'private') {
			if (RocketChat.authz.hasPermission(Meteor.userId(), 'view-c-room')) {
				roomTypes.push({
					type: 'c'
				});
			} else if (RocketChat.authz.hasPermission(Meteor.userId(), 'view-joined-room')) {
				const roomIds = _.pluck(RocketChat.models.Subscriptions.findByTypeAndUserId('c', Meteor.userId()).fetch(), 'rid');
				roomTypes.push({
					type: 'c',
					ids: roomIds
				});
			}
		}

		if (channelType !== 'public' && RocketChat.authz.hasPermission(Meteor.userId(), 'view-p-room')) {
			const userPref = Meteor.user() && Meteor.user().settings && Meteor.user().settings.preferences && Meteor.user().settings.preferences.mergeChannels;
			const globalPref = RocketChat.settings.get('UI_Merge_Channels_Groups');
			const mergeChannels = userPref || globalPref;

			if (mergeChannels) {
				roomTypes.push({
					type: 'p',
					username: Meteor.user().username
				});
			}
		}

		if (roomTypes.length) {
			if (filter) {
				return {
					channels: RocketChat.models.Rooms.findByNameContainingTypesWithUsername(filter, roomTypes, options).fetch()
				};
			}

			return {
				channels: RocketChat.models.Rooms.findContainingTypesWithUsername(roomTypes, options).fetch()
			};
		}

		return {
			channels: []
		};
	}
});
