|  | @@ -1,4 +1,5 @@
 | 
	
		
			
				|  |  |  import escapeForRegex from 'escape-string-regexp';
 | 
	
		
			
				|  |  | +import DOMPurify from 'dompurify';
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  CardComments = new Mongo.Collection('card_comments');
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -101,39 +102,43 @@ CardComments.helpers({
 | 
	
		
			
				|  |  |    },
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |    toggleReaction(reactionCodepoint) {
 | 
	
		
			
				|  |  | +    if (reactionCodepoint !== DOMPurify.sanitize(reactionCodepoint)) {
 | 
	
		
			
				|  |  | +      return false;
 | 
	
		
			
				|  |  | +    } else {
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -    const cardCommentReactions = CardCommentReactions.findOne({cardCommentId: this._id});
 | 
	
		
			
				|  |  | -    const reactions = !!cardCommentReactions ? cardCommentReactions.reactions : [];
 | 
	
		
			
				|  |  | -    const userId = Meteor.userId();
 | 
	
		
			
				|  |  | -    const reaction = reactions.find(r => r.reactionCodepoint === reactionCodepoint);
 | 
	
		
			
				|  |  | +      const cardCommentReactions = CardCommentReactions.findOne({cardCommentId: this._id});
 | 
	
		
			
				|  |  | +      const reactions = !!cardCommentReactions ? cardCommentReactions.reactions : [];
 | 
	
		
			
				|  |  | +      const userId = Meteor.userId();
 | 
	
		
			
				|  |  | +      const reaction = reactions.find(r => r.reactionCodepoint === reactionCodepoint);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -    // If no reaction is set for the codepoint, add this
 | 
	
		
			
				|  |  | -    if (!reaction) {
 | 
	
		
			
				|  |  | -      reactions.push({ reactionCodepoint, userIds: [userId] });
 | 
	
		
			
				|  |  | -    } else {
 | 
	
		
			
				|  |  | +      // If no reaction is set for the codepoint, add this
 | 
	
		
			
				|  |  | +      if (!reaction) {
 | 
	
		
			
				|  |  | +        reactions.push({ reactionCodepoint, userIds: [userId] });
 | 
	
		
			
				|  |  | +      } else {
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -      // toggle user reaction upon previous reaction state
 | 
	
		
			
				|  |  | -      const userHasReacted = reaction.userIds.includes(userId);
 | 
	
		
			
				|  |  | -      if (userHasReacted) {
 | 
	
		
			
				|  |  | -        reaction.userIds.splice(reaction.userIds.indexOf(userId), 1);
 | 
	
		
			
				|  |  | -        if (reaction.userIds.length === 0) {
 | 
	
		
			
				|  |  | -          reactions.splice(reactions.indexOf(reaction), 1);
 | 
	
		
			
				|  |  | +        // toggle user reaction upon previous reaction state
 | 
	
		
			
				|  |  | +        const userHasReacted = reaction.userIds.includes(userId);
 | 
	
		
			
				|  |  | +        if (userHasReacted) {
 | 
	
		
			
				|  |  | +          reaction.userIds.splice(reaction.userIds.indexOf(userId), 1);
 | 
	
		
			
				|  |  | +          if (reaction.userIds.length === 0) {
 | 
	
		
			
				|  |  | +            reactions.splice(reactions.indexOf(reaction), 1);
 | 
	
		
			
				|  |  | +          }
 | 
	
		
			
				|  |  | +        } else {
 | 
	
		
			
				|  |  | +          reaction.userIds.push(userId);
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  | -      } else {
 | 
	
		
			
				|  |  | -        reaction.userIds.push(userId);
 | 
	
		
			
				|  |  |        }
 | 
	
		
			
				|  |  | -    }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -    // If no reaction doc exists yet create otherwise update reaction set
 | 
	
		
			
				|  |  | -    if (!!cardCommentReactions) {
 | 
	
		
			
				|  |  | -      return CardCommentReactions.update({ _id: cardCommentReactions._id }, { $set: { reactions } });
 | 
	
		
			
				|  |  | -    } else {
 | 
	
		
			
				|  |  | -      return CardCommentReactions.insert({
 | 
	
		
			
				|  |  | -        boardId: this.boardId,
 | 
	
		
			
				|  |  | -        cardCommentId: this._id,
 | 
	
		
			
				|  |  | -        cardId: this.cardId,
 | 
	
		
			
				|  |  | -        reactions
 | 
	
		
			
				|  |  | -      });
 | 
	
		
			
				|  |  | +      // If no reaction doc exists yet create otherwise update reaction set
 | 
	
		
			
				|  |  | +      if (!!cardCommentReactions) {
 | 
	
		
			
				|  |  | +        return CardCommentReactions.update({ _id: cardCommentReactions._id }, { $set: { reactions } });
 | 
	
		
			
				|  |  | +      } else {
 | 
	
		
			
				|  |  | +        return CardCommentReactions.insert({
 | 
	
		
			
				|  |  | +          boardId: this.boardId,
 | 
	
		
			
				|  |  | +          cardCommentId: this._id,
 | 
	
		
			
				|  |  | +          cardId: this.cardId,
 | 
	
		
			
				|  |  | +          reactions
 | 
	
		
			
				|  |  | +        });
 | 
	
		
			
				|  |  | +      }
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |    }
 | 
	
		
			
				|  |  |  });
 |