Skip to content

Commit

Permalink
GravityProject#54 - Added links to user's profile when a user is ment…
Browse files Browse the repository at this point in the history
…ioned
  • Loading branch information
mattvella07 committed May 12, 2016
1 parent 5dbddf0 commit 896834a
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion client/views/feed.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) + '<a href="/users/' + mentionedUser._id + '">' + body.slice(x, body.indexOf(' ', x)) + '</a>' +
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 {
Expand Down

0 comments on commit 896834a

Please sign in to comment.