From 1c5c8fb575e2b58e88800bd19a9a507f639edfae Mon Sep 17 00:00:00 2001
From: Sacha Greif
Date: Fri, 8 Nov 2013 11:10:23 +0900
Subject: [PATCH] improved user dashboard even more!
---
History.md | 8 ++++++++
client/views/users/user_item.html | 6 +++---
client/views/users/user_item.js | 10 +++-------
client/views/users/users.html | 2 ++
collections/comments.js | 7 +++++++
collections/posts.js | 13 ++++++++++---
lib/parameters.js | 5 +++++
server/toolbox.js | 21 +++++++++++++++++++--
8 files changed, 57 insertions(+), 15 deletions(-)
diff --git a/History.md b/History.md
index 52b51dddc..2320702c8 100644
--- a/History.md
+++ b/History.md
@@ -1,3 +1,11 @@
+## v0.7.1
+
+* Added karma redistribution.
+* Improved user dashboard.
+* Improved user profiles.
+
+Note: run the "update user profile" script from the toolbox after updating.
+
## v0.7 “IronScope”
#### Huge update!
diff --git a/client/views/users/user_item.html b/client/views/users/user_item.html
index 62c2a7a51..30473780c 100644
--- a/client/views/users/user_item.html
+++ b/client/views/users/user_item.html
@@ -5,15 +5,15 @@
{{createdAtFormatted}} |
{{profile.email}} |
- {{postsCount}}
+ {{postCount}}
{{#each posts}}
- {{headline}}
{{/each}}
|
- {{commentsCount}} |
- {{karma}} |
+ {{commentCount}} |
+ {{getKarma}} |
{{#if isInvited}}Uninvite{{else}}Invite{{/if}} |
{{#if userIsAdmin}}Unadmin{{else}}Make admin{{/if}} |
Delete User |
diff --git a/client/views/users/user_item.js b/client/views/users/user_item.js
index 4f44ea10a..35dfca1c6 100644
--- a/client/views/users/user_item.js
+++ b/client/views/users/user_item.js
@@ -14,21 +14,17 @@ Template.user_item.helpers({
posts: function(){
return Posts.find({'userId':this._id});
},
- postsCount: function(){
- return Posts.find({'userId':this._id}).count();
- },
comments: function(){
return Comments.find({'userId':this._id});
},
- commentsCount: function(){
- // Posts.find({'user_id':this._id}).forEach(function(post){console.log(post.headline);});
- return Comments.find({'userId':this._id}).count();
- },
userIsAdmin: function(){
return isAdmin(this);
},
profileUrl: function () {
return getProfileUrl(this);
+ },
+ getKarma: function() {
+ return Math.round(100*this.karma)/100;
}
});
diff --git a/client/views/users/users.html b/client/views/users/users.html
index 9f65d27ae..53a9ac43f 100644
--- a/client/views/users/users.html
+++ b/client/views/users/users.html
@@ -15,6 +15,8 @@
Created
Karma
Username
+ Posts
+
diff --git a/collections/comments.js b/collections/comments.js
index 934dee49c..575dc0029 100644
--- a/collections/comments.js
+++ b/collections/comments.js
@@ -59,6 +59,9 @@ Meteor.methods({
var newCommentId=Comments.insert(comment);
+ // increment comment count
+ Meteor.users.update({_id: userId}, {$inc: {commentCount: 1}});
+
// extend comment with newly created _id
comment = _.extend(comment, {_id: newCommentId});
@@ -123,6 +126,10 @@ Meteor.methods({
if(canEdit(Meteor.user(), comment)){
// decrement post comment count
Posts.update(comment.post, {$inc: {comments: -1}});
+
+ // decrement user comment count
+ Meteor.users.update({_id: comment.userId}, {$inc: {commentCount: -1}});
+
// note: should we also decrease user's comment karma ?
Comments.remove(commentId);
}else{
diff --git a/collections/posts.js b/collections/posts.js
index cfb24a891..56b699d61 100644
--- a/collections/posts.js
+++ b/collections/posts.js
@@ -86,6 +86,9 @@ Meteor.methods({
postId = Posts.insert(post);
+ // increment posts count
+ Meteor.users.update({_id: userId}, {$inc: {postCount: 1}});
+
post = _.extend(post, {_id: postId});
var postAuthor = Meteor.users.findOne(post.userId);
@@ -124,9 +127,13 @@ Meteor.methods({
},
deletePostById: function(postId) {
// remove post comments
- if(!this.isSimulation) {
- Comments.remove({post: postId});
- }
+ // if(!this.isSimulation) {
+ // Comments.remove({post: postId});
+ // }
+ // NOTE: actually, keep comments afer all
+
+ // decrement post count
+ Meteor.users.update({_id: post.userId}, {$inc: {postCount: -1}});
Posts.remove(postId);
}
});
diff --git a/lib/parameters.js b/lib/parameters.js
index 1a7b1b1dc..8d5b8e45c 100644
--- a/lib/parameters.js
+++ b/lib/parameters.js
@@ -94,6 +94,11 @@ getUsersParameters = function(filterBy, sortBy, limit) {
case 'karma':
sort = {karma: -1};
break;
+ case 'postCount':
+ sort = {postCount: -1};
+ break;
+ case 'commentCount':
+ sort = {commentCount: -1};
}
return {
find: find,
diff --git a/server/toolbox.js b/server/toolbox.js
index 5a647d56a..9b836cb3c 100644
--- a/server/toolbox.js
+++ b/server/toolbox.js
@@ -17,8 +17,14 @@ Meteor.methods({
Meteor.users.update({}, {$inc:{invitesCount: 1}}, {multi:true});
},
updateUserProfiles: function () {
+ console.log('//--------------------------//\nUpdating user profiles…')
if(isAdmin(Meteor.user())){
- Meteor.users.find().forEach(function(user){
+ var allUsers = Meteor.users.find();
+ console.log('> Found '+allUsers.count()+' users.\n');
+
+ allUsers.forEach(function(user){
+ console.log('> Updating user '+user._id+' ('+user.username+')');
+
// update user slug
if(getUserName(user))
Meteor.users.update(user._id, {$set:{slug: slugify(getUserName(user))}});
@@ -26,8 +32,19 @@ Meteor.methods({
// update user isAdmin flag
if(typeof user.isAdmin === 'undefined')
Meteor.users.update(user._id, {$set: {isAdmin: false}});
+
+ // update postCount
+ var postsByUser = Posts.find({userId: user._id});
+ Meteor.users.update(user._id, {$set: {postCount: postsByUser.count()}});
+ console.log(postsByUser.count())
+ // update commentCount
+ var commentsByUser = Comments.find({userId: user._id});
+ Meteor.users.update(user._id, {$set: {commentCount: commentsByUser.count()}});
+ console.log(commentsByUser.count())
+
});
- }
+ }
+ console.log('Done updating user profiles.\n//--------------------------//')
},
updatePostsSlugs: function () {
//TODO