Changed comments to CommentsCount; Now tracking _ids of commenters on each Post document

This commit is contained in:
Sacha Greif 2014-08-22 10:31:05 +09:00
parent 0a0ad6a501
commit 063242b78d
6 changed files with 40 additions and 6 deletions

View file

@ -4,6 +4,8 @@
* Changed `navItems` to `primaryNav` and added `secondaryNav`.
* Added new `themeSettings` object for storing theme-level settings.
* Notifications is now a nav menu item.
* Renamed `comments` to `commentsCount` on `Post` model.
* Now tracking list of commenters `_id`s on `Post` model.
## v0.9.2.5 “AccountScope”

View file

@ -1,7 +1,7 @@
<template name="postCommentsLink">
<div class="post-meta-item">
<a class="comments-link" href="/posts/{{_id}}">
<span class="count">{{comments}}</span>
<span class="count">{{commentsCount}}</span>
<span class="action">{{i18n 'Comments'}}</span>
</a>
</div>

View file

@ -1,7 +1,7 @@
<template name="postDiscuss">
<a class="discuss-link go-to-comments" href="/posts/{{_id}}">
<i class="icon-comment"></i>
<span class="count">{{comments}}</span>
<span class="count">{{commentsCount}}</span>
<span class="action">{{i18n 'Discuss'}}</span>
</a>
</template>

View file

@ -128,7 +128,11 @@ Meteor.methods({
// extend comment with newly created _id
comment = _.extend(comment, {_id: newCommentId});
Posts.update(postId, {$inc: {comments: 1}, $set: {lastCommentedAt: now}});
Posts.update(postId, {
$inc: {commentsCount: 1},
$set: {lastCommentedAt: now},
$addToSet: {commenters: user._id}
});
Meteor.call('upvoteComment', comment);
@ -167,8 +171,11 @@ Meteor.methods({
removeComment: function(commentId){
var comment=Comments.findOne(commentId);
if(canEdit(Meteor.user(), comment)){
// decrement post comment count
Posts.update(comment.postId, {$inc: {comments: -1}});
// decrement post comment count and remove user ID from post
Posts.update(comment.postId, {
$inc: {commentsCount: -1},
$pull: {commenters: comment.userId}
});
// decrement user comment count
Meteor.users.update({_id: comment.userId}, {$inc: {commentCount: -1}});

View file

@ -24,10 +24,14 @@ postSchemaObject = {
type: String,
optional: true
},
comments: {
commentsCount: {
type: Number,
optional: true
},
commenters: {
type: [String],
optional: true
},
lastCommentedAt: {
type: Date,
optional: true

View file

@ -280,5 +280,26 @@ var migrationsList = {
console.log("---------------------");
});
return i;
},
commentsToCommentsCount: function () {
var i = 0;
Posts.find({commentsCount: {$exists : false}}).forEach(function (post) {
i++;
console.log("Post: "+post._id);
Posts.update(post._id, { $rename: { 'comments': 'commentsCount'}}, {multi: true, validate: false});
console.log("---------------------");
});
return i;
},
addCommentersToPosts: function () {
var i = 0;
Comments.find().forEach(function (comment) {
i++;
console.log("Comment: "+comment._id);
console.log("Post: "+comment.postId);
Posts.update(comment.postId, { $addToSet: { 'commenters': comment.userId}}, {multi: true, validate: false});
console.log("---------------------");
});
return i;
}
}