/*globals StarredMessage */
Template.starredMessages.helpers({
	hasMessages() {
		return StarredMessage.find({
			rid: this.rid
		}, {
			sort: {
				ts: -1
			}
		}).count() > 0;
	},
	messages() {
		return StarredMessage.find({
			rid: this.rid
		}, {
			sort: {
				ts: -1
			}
		});
	},
	message() {
		return _.extend(this, {
			customClass: 'starred'
		});
	},
	hasMore() {
		return Template.instance().hasMore.get();
	}
});

Template.starredMessages.onCreated(function() {
	this.hasMore = new ReactiveVar(true);
	this.limit = new ReactiveVar(50);
	return this.autorun(() => {
		return () => {
			const sub = this.subscribe('starredMessages', this.data.rid, this.limit.get());
			if (sub.ready()) {
				if (StarredMessage.find({
					rid: this.data.rid
				}).count() < this.limit.get()) {
					return this.hasMore.set(false);
				}
			}
		};
	});
});

Template.starredMessages.events({
	'click .message-cog'(e, t) {
		e.stopPropagation();
		e.preventDefault();
		const message_id = $(e.currentTarget).closest('.message').attr('id');
		RocketChat.MessageAction.hideDropDown();
		t.$(`\#${ message_id } .message-dropdown`).remove();
		const message = StarredMessage.findOne(message_id);
		const actions = RocketChat.MessageAction.getButtons(message, 'starred');
		const el = Blaze.toHTMLWithData(Template.messageDropdown, {
			actions
		});
		t.$(`\#${ message_id } .message-cog-container`).append(el);
		const dropDown = t.$(`\#${ message_id } .message-dropdown`);
		return dropDown.show();
	},
	'scroll .content': _.throttle(function(e, instance) {
		if (e.target.scrollTop >= e.target.scrollHeight - e.target.clientHeight) {
			return instance.limit.set(instance.limit.get() + 50);
		}
	}, 200)
});

@@ -546,23 +546,16 @@
 r(50);%0A%09
-return 
 this.aut
@@ -571,26 +571,8 @@
 %3E %7B%0A
-%09%09return () =%3E %7B%0A%09
 %09%09co
@@ -655,36 +655,35 @@
 ;%0A%09%09
-%09if (sub.ready()) %7B%0A%09%09%09%09if (
+const findStarredMessage = 
 Star
@@ -699,22 +699,17 @@
 e.find(%7B
-%0A%09%09%09%09%09
+ 
 rid: thi
@@ -722,15 +722,59 @@
 .rid
-%0A%09%09%09%09%7D)
+ %7D);%0A%09%09if (sub.ready()) %7B%0A%09%09%09if (findStarredMessage
 .cou
@@ -800,17 +800,16 @@
 et()) %7B%0A
-%09
 %09%09%09%09retu
@@ -840,23 +840,16 @@
 e);%0A
-%09%09%09%09%7D%0A
 %09%09%09%7D%0A
+
 %09%09%7D
-;
 %0A%09%7D)

fix starredMessages.js