now showing comments on user profile page

This commit is contained in:
Sacha Greif 2014-08-22 17:06:22 +09:00
parent f2808ee764
commit 9b8e16f177
7 changed files with 104 additions and 34 deletions

View file

@ -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)!)

View file

@ -1,7 +1,6 @@
<template name="user_profile">
<div class="grid grid-module">
{{#with user}}
<div class="user-profile grid-block">
{{#with user}}
<div class="user-profile grid grid-module">
<table>
<tr>
<td colspan="2"><img class="user-avatar" src="{{avatarUrl}}"/></td>
@ -54,23 +53,41 @@
{{/if}}
{{/if}}
</div>
<div class="user-profile-votes grid-block">
<h3>{{i18n "Upvoted Posts"}}</h3>
<table>
<thead>
<tr>
<td>Post</td>
<td>Upvoted At</td>
</tr>
</thead>
{{#each upvotes}}
<tr>
<td><a href="/posts/{{_id}}/">{{title}}</a></td>
<td>{{formatDate votedAt "MM/DD/YYYY, HH:mm"}}</td>
</tr>
{{/each}}
</table>
<div class="user-profile-votes grid grid-module">
<h3>{{i18n "Upvoted Posts"}}</h3>
<table>
<thead>
<tr>
<td>Post</td>
<td>Upvoted At</td>
</tr>
</thead>
{{#each upvotes}}
<tr>
<td><a href="/posts/{{_id}}/">{{title}}</a></td>
<td>{{formatDate votedAt "MM/DD/YYYY, HH:mm"}}</td>
</tr>
{{/each}}
</table>
</div>
{{/with}}
</div>
<div class="user-profile-comments grid grid-module">
<h3>{{i18n "Comments"}}</h3>
<table>
<thead>
<tr>
<td>Post</td>
<td>Comment</td>
<td>Commented At</td>
</tr>
</thead>
{{#each comments}}
<tr>
<td><a href="/posts/{{postId}}/">{{postTitle}}</a></td>
<td>{{body}}</td>
<td>{{formatDate createdAt "MM/DD/YYYY, HH:mm"}}</td>
</tr>
{{/each}}
</table>
</div>
{{/with}}
</template>

View file

@ -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
}
}
});

View file

@ -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);

View file

@ -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

View file

@ -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;
}
}

View file

@ -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) {