improved user dashboard even more!

This commit is contained in:
Sacha Greif 2013-11-08 11:10:23 +09:00
parent c164e67572
commit 1c5c8fb575
8 changed files with 57 additions and 15 deletions

View file

@ -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” ## v0.7 “IronScope”
#### Huge update! #### Huge update!

View file

@ -5,15 +5,15 @@
<td>{{createdAtFormatted}}</td> <td>{{createdAtFormatted}}</td>
<td>{{profile.email}}</td> <td>{{profile.email}}</td>
<td> <td>
<a href="#">{{postsCount}}</a> <a href="#">{{postCount}}</a>
<ul class="posts-list hidden"> <ul class="posts-list hidden">
{{#each posts}} {{#each posts}}
<li><a href="/posts/{{_id}}">{{headline}}</a></li> <li><a href="/posts/{{_id}}">{{headline}}</a></li>
{{/each}} {{/each}}
</ul> </ul>
</td> </td>
<td><a href="#">{{commentsCount}}</a></td> <td><a href="#">{{commentCount}}</a></td>
<td>{{karma}}</td> <td>{{getKarma}}</td>
<td>{{#if isInvited}}<a class="uninvite-link" href="#"><i class="icon-check"></i>Uninvite</a>{{else}}<a href="#" class="invite-link">Invite</a>{{/if}}</td> <td>{{#if isInvited}}<a class="uninvite-link" href="#"><i class="icon-check"></i>Uninvite</a>{{else}}<a href="#" class="invite-link">Invite</a>{{/if}}</td>
<td>{{#if userIsAdmin}}<a class="unadmin-link" href="#"><i class="icon-check unadmin-link"></i>Unadmin</a>{{else}}<a href="#" class="admin-link">Make admin</a>{{/if}}</td> <td>{{#if userIsAdmin}}<a class="unadmin-link" href="#"><i class="icon-check unadmin-link"></i>Unadmin</a>{{else}}<a href="#" class="admin-link">Make admin</a>{{/if}}</td>
<td><a class="delete-link" href="#">Delete User</a></td> <td><a class="delete-link" href="#">Delete User</a></td>

View file

@ -14,21 +14,17 @@ Template.user_item.helpers({
posts: function(){ posts: function(){
return Posts.find({'userId':this._id}); return Posts.find({'userId':this._id});
}, },
postsCount: function(){
return Posts.find({'userId':this._id}).count();
},
comments: function(){ comments: function(){
return Comments.find({'userId':this._id}); 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(){ userIsAdmin: function(){
return isAdmin(this); return isAdmin(this);
}, },
profileUrl: function () { profileUrl: function () {
return getProfileUrl(this); return getProfileUrl(this);
},
getKarma: function() {
return Math.round(100*this.karma)/100;
} }
}); });

View file

@ -15,6 +15,8 @@
<a class="{{activeClass 'createdAt'}}" href="{{sortBy 'createdAt'}}">Created</a> <a class="{{activeClass 'createdAt'}}" href="{{sortBy 'createdAt'}}">Created</a>
<a class="{{activeClass 'karma'}}" href="{{sortBy 'karma'}}">Karma</a> <a class="{{activeClass 'karma'}}" href="{{sortBy 'karma'}}">Karma</a>
<a class="{{activeClass 'username'}}" href="{{sortBy 'username'}}">Username</a> <a class="{{activeClass 'username'}}" href="{{sortBy 'username'}}">Username</a>
<a class="{{activeClass 'postCount'}}" href="{{sortBy 'postCount'}}">Posts</a>
<a class="{{activeClass 'commentCount'}}" href="{{sortBy 'commentCount'}}">Comments</a>
</p> </p>
</div> </div>
<table> <table>

View file

@ -59,6 +59,9 @@ Meteor.methods({
var newCommentId=Comments.insert(comment); var newCommentId=Comments.insert(comment);
// increment comment count
Meteor.users.update({_id: userId}, {$inc: {commentCount: 1}});
// extend comment with newly created _id // extend comment with newly created _id
comment = _.extend(comment, {_id: newCommentId}); comment = _.extend(comment, {_id: newCommentId});
@ -123,6 +126,10 @@ Meteor.methods({
if(canEdit(Meteor.user(), comment)){ if(canEdit(Meteor.user(), comment)){
// decrement post comment count // decrement post comment count
Posts.update(comment.post, {$inc: {comments: -1}}); 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 ? // note: should we also decrease user's comment karma ?
Comments.remove(commentId); Comments.remove(commentId);
}else{ }else{

View file

@ -86,6 +86,9 @@ Meteor.methods({
postId = Posts.insert(post); postId = Posts.insert(post);
// increment posts count
Meteor.users.update({_id: userId}, {$inc: {postCount: 1}});
post = _.extend(post, {_id: postId}); post = _.extend(post, {_id: postId});
var postAuthor = Meteor.users.findOne(post.userId); var postAuthor = Meteor.users.findOne(post.userId);
@ -124,9 +127,13 @@ Meteor.methods({
}, },
deletePostById: function(postId) { deletePostById: function(postId) {
// remove post comments // remove post comments
if(!this.isSimulation) { // if(!this.isSimulation) {
Comments.remove({post: postId}); // 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); Posts.remove(postId);
} }
}); });

View file

@ -94,6 +94,11 @@ getUsersParameters = function(filterBy, sortBy, limit) {
case 'karma': case 'karma':
sort = {karma: -1}; sort = {karma: -1};
break; break;
case 'postCount':
sort = {postCount: -1};
break;
case 'commentCount':
sort = {commentCount: -1};
} }
return { return {
find: find, find: find,

View file

@ -17,8 +17,14 @@ Meteor.methods({
Meteor.users.update({}, {$inc:{invitesCount: 1}}, {multi:true}); Meteor.users.update({}, {$inc:{invitesCount: 1}}, {multi:true});
}, },
updateUserProfiles: function () { updateUserProfiles: function () {
console.log('//--------------------------//\nUpdating user profiles…')
if(isAdmin(Meteor.user())){ 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 // update user slug
if(getUserName(user)) if(getUserName(user))
Meteor.users.update(user._id, {$set:{slug: slugify(getUserName(user))}}); Meteor.users.update(user._id, {$set:{slug: slugify(getUserName(user))}});
@ -26,8 +32,19 @@ Meteor.methods({
// update user isAdmin flag // update user isAdmin flag
if(typeof user.isAdmin === 'undefined') if(typeof user.isAdmin === 'undefined')
Meteor.users.update(user._id, {$set: {isAdmin: false}}); 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 () { updatePostsSlugs: function () {
//TODO //TODO