merging invites branch

This commit is contained in:
Sacha Greif 2012-10-05 13:29:35 +09:00
commit 51ced3e331
8 changed files with 49 additions and 37 deletions

View file

@ -14,7 +14,7 @@
{{/with}}
{{/if}}
{{#if currentUser}}
{{#if currentUser.approved}}
{{> comment_form}}
{{/if}}
</div>

View file

@ -50,7 +50,7 @@
<li><a id="signup" href="signup">Sign Up</a></li>
<li><a id="signin" href="signin">Sign In</a></li>
{{/if}} -->
{{#if currentUser}}
{{#if currentUser.approved}}
<li><a id="submit" class="submit button" href="/submit">Post</a></li>
{{/if}}
</ul>

View file

@ -15,7 +15,6 @@ Template.notifications.helpers({
notification_class: function(){
var notifications=Notifications.find({userId: Meteor.user()._id, read: false}).fetch();
if(notifications.length==0)
return 'no-notifications';
}
});
@ -27,4 +26,4 @@ Template.notifications.events({
'click .mark-as-read': function(){
Meteor.call('markAllNotificationsAsRead', Meteor.user());
}
})
})

View file

@ -3,7 +3,7 @@
{{#with post}}
{{> post_item}}
{{/with}}
{{#if currentUser}}
{{#if currentUser.approved}}
{{> comment_form}}
{{/if}}
{{> comment_list}}

View file

@ -1,7 +1,10 @@
Meteor.methods({
comment: function(postId, parentCommentId, text){
var user = Meteor.users.findOne(this.userId());
var user = Meteor.user();
if (!user || !user.approved)
throw new Meteor.Error('You need to login and be approved to post new comments.')
var comment = {
post: postId
, body: text

View file

@ -1,8 +1,12 @@
Meteor.methods({
post: function(post){
var user = Meteor.user();
if (!user || !user.approved)
throw new Meteor.Error('You need to login and be approved to post new stories.')
post = _.extend(post, {
userId: Meteor.user()._id,
author: Meteor.user().username,
userId: user._id,
author: user.username,
submitted: new Date().getTime(),
votes: 0,
comments: 0,

View file

@ -1,42 +1,42 @@
(function() {
// returns how much "power" a user's votes have
var getVotePower = function(userId){
var user = Meteor.users.findOne(userId);
var getVotePower = function(user){
return (user && user.isAdmin) ? 5 : 1;
};
var modifyKarma = function(userId, karma){
Meteor.users.update({_id: userId}, {$inc: {karma: karma}});
var modifyKarma = function(user_id, karma){
Meteor.users.update({_id: user_id}, {$inc: {karma: karma}});
};
var hasUpvotedItem= function(userId, collection, id){
var hasUpvotedItem= function(user, collection, id){
// see http://www.mongodb.org/display/DOCS/MongoDB+Data+Modeling+and+Rails
// 'is there an item with this id which contains this userId in its upvoters?'
// if such an item exists, it means we have voted.
return collection.findOne({_id: id, upvoters: userId}) !== undefined;
return collection.findOne({_id: id, upvoters: user._id}) !== undefined;
}
var hasDownvotedItem= function(userId, collection, id){
return collection.findOne({_id: id, downvoters: userId}) !== undefined;
var hasDownvotedItem= function(user, collection, id){
return collection.findOne({_id: id, downvoters: user._id}) !== undefined;
}
var upvote = function(collection, id) {
if (!this.userId() || hasUpvotedItem(this.userId(), collection, id))
var user = Meteor.user();
if (! user || ! user.approved || hasUpvotedItem(user, collection, id))
return false;
var votePower=getVotePower(this.userId());
var votePower=getVotePower(user);
var votedItem = collection.findOne(id);
// Votes & Score
collection.update({_id: id},
{$addToSet: {upvoters: this.userId()},
{$addToSet: {upvoters: user._id},
$inc: {votes: 1, baseScore: votePower}});
if(!this.isSimulation)
updateScore(collection, id);
// Karma
// user's posts and comments do not impact his own karma:
if (votedItem.user_id != this.userId()) {
if (votedItem.user_id != user._id) {
modifyKarma(votedItem.user_id, votePower);
}
@ -44,22 +44,23 @@
};
var downvote = function(collection, id) {
if (!this.userId() || hasDownvotedItem(this.userId(), collection, id))
var user = Meteor.user();
if (! user || ! user.approved || hasDownvotedItem(user, collection, id))
return false;
var votePower=getVotePower(this.userId());
var votePower=getVotePower(user);
var votedItem = collection.findOne(id);
// Votes & Score
collection.update({_id: id},
{$addToSet: {downvoters: this.userId()},
{$addToSet: {downvoters: user._id},
$inc: {votes: -1, baseScore: -votePower}});
if(!this.isSimulation)
updateScore(collection, id);
// Karma
// user's posts and comments do not impact his own karma:
if (votedItem.user_id != this.userId()) {
if (votedItem.user_id != user._id) {
modifyKarma(votedItem.user_id, -votePower);
}
@ -67,22 +68,23 @@
};
var cancelUpvote = function(collection, id) {
if (!this.userId() || !hasUpvotedItem(this.userId(), collection, id))
return false;
var votePower=getVotePower(this.userId());
var user = Meteor.user();
if (! user || ! user.approved || ! hasUpvotedItem(user, collection, id))
return false
var votePower=getVotePower(user);
var votedItem = collection.findOne(id);
// Votes & Score
collection.update({_id: id},
{$pull: {upvoters: this.userId()},
{$pull: {upvoters: user._id},
$inc: {votes: -1, baseScore: -votePower}});
if(!this.isSimulation)
updateScore(collection, id);
// Karma
// user's posts and comments do not impact his own karma:
if (votedItem.user_id != this.userId()) {
if (votedItem.user_id != user._id) {
modifyKarma(votedItem.user_id, -votePower);
}
@ -90,22 +92,23 @@
};
var cancelDownvote = function(collection, id) {
if (!this.userId() || !hasDownvotedItem(this.userId(), collection, id))
return false;
var user = Meteor.user();
if (! user || ! user.approved || ! hasDownvotedItem(user, collection, id))
return false
var votePower=getVotePower(this.userId());
var votePower=getVotePower(user);
var votedItem = collection.findOne(id);
// Votes & Score
collection.update({_id: id},
{$pull: {downvoters: this.userId()},
{$pull: {downvoters: user._id},
$inc: {votes: 1, baseScore: votePower}});
if(!this.isSimulation)
updateScore(collection, id);
// Karma
// user's posts and comments do not impact his own karma:
if (votedItem.user_id != this.userId()) {
if (votedItem.user_id != user._id) {
modifyKarma(votedItem.user_id, votePower);
}

View file

@ -4,6 +4,9 @@ Accounts.onCreateUser(function(options, extra, user){
user.karma = 0;
user.profile = user.profile || {};
// users start pending, need to be approved
user.approved = false
if (options.email)
user.profile.email = options.email;