2014-03-28 66 views
3

我正在開發一個聊天應用程序,用流星使用賬戶 - 用戶賬戶和賬戶 - 推特。如果他們濫用網站,我希望能夠禁止他人,但我不知道如何去做,或者甚至有可能。有沒有辦法做到這一點?下面是代碼我用它來運行它的聊天應用部分:禁止使用Meteor賬戶?

ui.js:

// render all of our messages in the ui 
Template.chatBox.helpers({ 
    "messages": function() { 
    return chatCollection.find(); 
    } 
}); 

// get the value for handlerbar helper user 
Template.chatMessage.helpers({ 
    "user": function() { 
    if(this.userId == 'me') { 
     return this.userId; 
    } else if(this.userId) { 
     getUsername(this.userId); 
     return Session.get('user-' + this.userId); 
    } else { 
     return 'anonymous-' + this.subscriptionId; 
    } 
    } 
}); 

// when Send Chat clicked at the message to the collection 
Template.chatBox.events({ 
    "click #send": function() { 
      if (Meteor.user() == null) { 
       alert("You must login to post"); 
      return; 
      } 
      $('#messages').animate({"scrollTop": $('#messages')[0].scrollHeight}, "fast"); 
      var message = $('#chat-message').val(); 

      // check to see if the message has any characters in it 
      if (message.length < 1) { 
       alert("You must enter a message to post."); 
      return; 
      } 

      if (message.length > 200) { 
       alert("Your message is too long... they can't read that fast!"); 
      return; 
      } 

      chatCollection.insert({ 
       userId: 'me', 
       message: message 
      }); 
      $('#chat-message').val(''); 

      //Validation 
      var bot =Check_bots(); 

      if(bot==false) 
      {  
      //add the message to the stream 
      chatStream.emit('chat', message); 
     } 
     else 
     { 
      alert("Slow down! No need to post that fast."); 
      return false; 
     } 
    }, 

    "keypress #chat-message": function(e) { 
     if (Meteor.user() == null) { 
      alert("You must login to post"); 
      return; 
     } 
     if (e.which == 13) { 

      //Validation 
     var bot =Check_bots(); 

     if(bot==false) 
     { 
      $('#messages').animate({"scrollTop": $('#messages')[0].scrollHeight}, "fast"); 
      console.log("you pressed enter"); 
      e.preventDefault(); 
      //repeat function from #send click event here 
      var message = $('#chat-message').val(); 

      // check to see if the message has any characters in it 
      if (message.length < 1) { 
       alert("You must enter a message to post."); 
      return; 
      } 

      if (message.length > 200) { 
       alert("Your message is too long... they can't read that fast!"); 
      return; 
      } 

      chatCollection.insert({ 
       userId: 'me', 
       message: message 
      }); 
      $('#chat-message').val(''); 

      //add the message to the stream 
      chatStream.emit('chat', message); 
     } 
     else 
     { 
      alert("Slow down! No need to post that fast."); 
      return false; 
     } 
    } 
    } 
}); 

chatStream.on('chat', function(message) { 
    chatCollection.insert({ 
    userId: this.userId, 
    subscriptionId: this.subscriptionId, 
    message: message 
    }); 
}); 

var lastintime=0; 
var defference=0; 
var msg_count=0; 

function Check_bots() 
{ 
    var seconds = new Date().getTime()/1000; 
    seconds=parseInt(seconds); 

    if(lastintime < seconds) 
    { 
     defference = seconds -lastintime; 
     lastintime=seconds; 

     if(defference<=5 && msg_count>=3) 
     { 
      return true; 
     } 
     else 
     { 
      return false; 
     } 
    } 
} 

users.js:

Accounts.ui.config({ 
    passwordSignupFields: "USERNAME_ONLY" 
}); 

getUsername = function(id) { 
    Meteor.subscribe('user-info', id); 
    Deps.autorun(function() { 
    var user = Meteor.users.findOne(id); 
    if(user) { 
     Session.set('user-' + id, user.username); 
    } 
    }); 
} 

有誰知道一個辦法做到這一點?

回答

8

你可以使用Accounts.validateLoginAttempt

服務器端代碼

Accounts.validateLoginAttempt(function(info) { 
    var user = info.user; 

    if(user.isBanned) throw new Meteor.Error(403, 'You are banned'); 

}); 

與上面的代碼:如果您添加isBanned: true在你的MongoDB數據庫中的任何用戶,該用戶將不能夠登錄。

+0

我只是創建一個新的.js文件並將該代碼放入它? – 5AMWE5T

+0

如何在MongoDB數據庫中爲用戶添加isBanned:true? – 5AMWE5T

+0

由於某些原因,當我添加代碼時未添加isBanned:對任何人(我不知道如何)均爲true,因此任何嘗試登錄的用戶都會輸入「Login Forbidden」。 – 5AMWE5T