diff --git a/History.md b/History.md index 44288cbe7..7fbcdf199 100644 --- a/History.md +++ b/History.md @@ -1,3 +1,8 @@ +## v0.9.2.7 + +* Now tracking list of comments `_id`s on `User` model. +* Show user comments on user profile page. + ## v0.9.2.6 “InviteScope” * Added new invite features (thanks [@callmephilip](https://github.com/callmephilip)!) diff --git a/client/views/users/user_profile.html b/client/views/users/user_profile.html index ae93d1f6d..d6de38db9 100644 --- a/client/views/users/user_profile.html +++ b/client/views/users/user_profile.html @@ -1,7 +1,6 @@ \ No newline at end of file diff --git a/client/views/users/user_profile.js b/client/views/users/user_profile.js index fae440e83..db4494a2f 100644 --- a/client/views/users/user_profile.js +++ b/client/views/users/user_profile.js @@ -24,11 +24,25 @@ Template[getTemplate('user_profile')].helpers({ }, upvotes: function () { // extend upvotes with each upvoted post - var extendedVotes = Meteor.user().profile.upvotedPosts.map(function (item) { - var post = Posts.findOne(item.itemId); - return _.extend(item, post); - }); - return extendedVotes + if(!!this.profile.upvotedPosts){ + var extendedVotes = this.profile.upvotedPosts.map(function (item) { + var post = Posts.findOne(item.itemId); + return _.extend(item, post); + }); + return extendedVotes + } + }, + comments: function () { + var comments = Comments.find({_id: {$in: this.profile.comments}}); + if(!!this.profile.comments){ + // extend comments with each commented post + var extendedComments = comments.map(function (comment) { + var post = Posts.findOne(comment.postId); + comment.postTitle = post.title; + return comment; + }); + return extendedComments + } } }); diff --git a/collections/comments.js b/collections/comments.js index c168e6efd..960a8ef93 100644 --- a/collections/comments.js +++ b/collections/comments.js @@ -123,7 +123,10 @@ Meteor.methods({ var newCommentId=Comments.insert(comment); // increment comment count - Meteor.users.update({_id: user._id}, {$inc: {commentCount: 1}}); + Meteor.users.update({_id: user._id}, { + $inc: {'profile.commentsCount': 1}, + $addToSet: {'profile.comments': newCommentId} + }); // extend comment with newly created _id comment = _.extend(comment, {_id: newCommentId}); @@ -177,8 +180,11 @@ Meteor.methods({ $pull: {commenters: comment.userId} }); - // decrement user comment count - Meteor.users.update({_id: comment.userId}, {$inc: {commentCount: -1}}); + // decrement user comment count and remove comment ID from user + Meteor.users.update({_id: comment.userId}, { + $inc: {'profile.commentsCount': -1}, + $pull: {'profile.comments': comment._id} + }); // note: should we also decrease user's comment karma ? Comments.remove(commentId); diff --git a/lib/router.js b/lib/router.js index cf7b06e00..9142abe3c 100644 --- a/lib/router.js +++ b/lib/router.js @@ -411,7 +411,9 @@ UserPageController = FastRender.RouteController.extend({ waitOn: function() { return [ coreSubscriptions.subscribe('singleUser', this.params._idOrSlug), - coreSubscriptions.subscribe('upvotedPosts', this.params._idOrSlug) + coreSubscriptions.subscribe('upvotedPosts', this.params._idOrSlug), // TODO: replace all 3 with single sub + coreSubscriptions.subscribe('userComments', this.params._idOrSlug), + coreSubscriptions.subscribe('commentedPosts', this.params._idOrSlug) ] }, data: function() { @@ -477,7 +479,7 @@ Router.map(function() { }, controller: PostsListController }); - + this.route('posts_top', { path: '/top/:limit?', controller: PostsListController diff --git a/server/migrations.js b/server/migrations.js index ccc49875e..6e006ac10 100644 --- a/server/migrations.js +++ b/server/migrations.js @@ -312,5 +312,16 @@ var migrationsList = { console.log("---------------------"); }); return i; + }, + addCommentsToUsers: function () { + var i = 0; + Comments.find().forEach(function (comment) { + i++; + console.log("Comment: "+comment._id); + console.log("User: "+comment.userId); + Meteor.users.update(comment.userId, { $addToSet: { 'profile.comments': comment._id}}, {multi: true, validate: false}); + console.log("---------------------"); + }); + return i; } } \ No newline at end of file diff --git a/server/publications.js b/server/publications.js index 9a49bf28a..5ce99cc73 100644 --- a/server/publications.js +++ b/server/publications.js @@ -39,13 +39,28 @@ Meteor.publish('upvotedPosts', function(userIdOrSlug) { var findBySlug = Meteor.users.findOne({slug: userIdOrSlug}); var user = typeof findById !== 'undefined' ? findById : findBySlug; var upvotedPostIds = _.pluck(user.profile.upvotedPosts, 'itemId'); - if (this.userId = user._id) { // only publish upvoted posts when users are browsing their own profile - return Posts.find({_id: {$in: upvotedPostIds}}); + return Posts.find({_id: {$in: upvotedPostIds}}); +}); + +Meteor.publish('userComments', function(userIdOrSlug) { + var findById = Meteor.users.findOne(userIdOrSlug); + var findBySlug = Meteor.users.findOne({slug: userIdOrSlug}); + var user = typeof findById !== 'undefined' ? findById : findBySlug; + return Comments.find({_id: {$in: user.profile.comments}}); +}); + +Meteor.publish('commentedPosts', function(userIdOrSlug) { + var findById = Meteor.users.findOne(userIdOrSlug); + var findBySlug = Meteor.users.findOne({slug: userIdOrSlug}); + var user = typeof findById !== 'undefined' ? findById : findBySlug; + var comments = Comments.find({_id: {$in: user.profile.comments}}); + if(!!comments.count()){ // there might not be any comments + var commentedPostIds = _.pluck(comments.fetch(), 'postId'); + return Posts.find({_id: {$in: commentedPostIds}}); } return []; }); - // Publish authors of the current post and its comments Meteor.publish('postUsers', function(postId) {