Limit the rate at which messages can be sent.
authorTom Hughes <tom@compton.nu>
Sat, 31 Oct 2009 00:24:36 +0000 (00:24 +0000)
committerTom Hughes <tom@compton.nu>
Sat, 31 Oct 2009 00:24:36 +0000 (00:24 +0000)
app/controllers/message_controller.rb
config/application.yml
config/locales/en.yml

index db40cc03f7922b33625eb7ed9f17391e3b86b6a0..18b286973cc0513f3bec72c8d9d78b1ca961bb81 100644 (file)
@@ -15,15 +15,19 @@ class MessageController < ApplicationController
     @to_user = User.find_by_display_name(params[:display_name])
     if @to_user
       if params[:message]
-        @message = Message.new(params[:message])
-        @message.to_user_id = @to_user.id
-        @message.from_user_id = @user.id
-        @message.sent_on = Time.now.getutc
+        if @user.sent_messages.count(:conditions => ["sent_on >= ?", Time.now.getutc - 1.hour]) >= APP_CONFIG['max_messages_per_hour']
+          flash[:notice] = t 'message.new.limit_exceeded'
+        else
+          @message = Message.new(params[:message])
+          @message.to_user_id = @to_user.id
+          @message.from_user_id = @user.id
+          @message.sent_on = Time.now.getutc
 
-        if @message.save
-          flash[:notice] = t 'message.new.message_sent'
-          Notifier::deliver_message_notification(@message)
-          redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name
+          if @message.save
+            flash[:notice] = t 'message.new.message_sent'
+            Notifier::deliver_message_notification(@message)
+            redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name
+          end
         end
       else
         if params[:title]
index c14dc8168848062b53a88584c43cf3befaa50f18..a478217a40d1db39241bf7e57dfb7b36896ce8aa 100644 (file)
@@ -17,6 +17,8 @@ standard_settings: &standard_settings
   api_timeout: 300
   # Periods (in hours) which are allowed for user blocks
   user_block_periods: [0, 1, 3, 6, 12, 24, 48, 96]
+  # Rate limit for message sending
+  max_messages_per_hour: 60
  
 development:
   <<: *standard_settings
index 1c0171c2c3cdda1b7ae1a9accf453d0dd420b3f9..d77c9a0ddbc90e8204b597563c1f87764f8e2477 100644 (file)
@@ -581,6 +581,7 @@ en:
       send_button: "Send"
       back_to_inbox: "Back to inbox"
       message_sent: "Message sent"
+      limit_exceeded: "You have sent a lot of messages recently, please wait a while before trying to send any more."
     no_such_user:
       title: "No such user or message"
       heading: "No such user or message"