From 896834a47170916663ed0fdb3013a8afe8e16ce9 Mon Sep 17 00:00:00 2001 From: Matt Vella Date: Wed, 11 May 2016 21:11:45 -0400 Subject: [PATCH] #54 - Added links to user's profile when a user is mentioned --- client/views/feed.js | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/client/views/feed.js b/client/views/feed.js index be4339a..c38b02f 100644 --- a/client/views/feed.js +++ b/client/views/feed.js @@ -72,7 +72,31 @@ Template.feed.events({ // Only continue if button isn't disabled if (!$('input[type=submit]').hasClass('disabled')) { - Meteor.call('posts.insert', template.find('[data-id=body]').value, (error, result) => { + let body = template.find('[data-id=body]').value; + + // If a user is mentioned in the post add span with class to highlight their username + if(body.indexOf('@') !== -1) { + for(let x = 0; x < body.length; x++) { + if(body[x] === '@') { + let u = body.slice(x + 1, body.indexOf(' ', x)); + let mentionedUser = Meteor.users.findOne({username: u}); + + // If a valid user + if(mentionedUser) { + console.log(mentionedUser._id); + // Add opening and closing span tags + body = body.slice(0, x) + '' + body.slice(x, body.indexOf(' ', x)) + '' + + body.slice(body.indexOf(' ', x)); + + // Increment by number of characters in openeing span tag + // so the same mention doesn't get evaluated multiple times + x+= 16; + } + } + } + } + + Meteor.call('posts.insert', body, (error, result) => { if (error) { Bert.alert(error.reason, 'danger', 'growl-top-right'); } else {