diff --git a/app/logical/api_limiter.rb b/app/logical/api_limiter.rb index 0534d1f74..881ac1da1 100644 --- a/app/logical/api_limiter.rb +++ b/app/logical/api_limiter.rb @@ -4,6 +4,12 @@ module ApiLimiter MEMCACHE.fetch(key, 1.hour, :raw => true) {0} MEMCACHE.incr(key).to_i > CurrentUser.user.api_hourly_limit end + + def remaining_hourly_limit(ip_addr) + key = "#{ip_addr}:#{Time.now.hour}" + requests = MEMCACHE.fetch(key, 1.hour, :raw => true) {0}.to_i + CurrentUser.user.api_hourly_limit - requests + end - module_function :throttled? + module_function :throttled?, :remaining_hourly_limit end diff --git a/app/models/user.rb b/app/models/user.rb index 3563495d9..3f91a6fce 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -506,6 +506,10 @@ class User < ActiveRecord::Base 3_000 end end + + def remaining_api_hourly_limit + ApiLimiter.remaining_hourly_limit(CurrentUser.ip_addr) + end def statement_timeout if is_platinum? @@ -523,12 +527,20 @@ class User < ActiveRecord::Base super + [:password_hash, :bcrypt_password_hash, :email, :email_verification_key, :time_zone, :updated_at, :receive_email_notifications, :last_logged_in_at, :last_forum_read_at, :has_mail, :default_image_size, :comment_threshold, :always_resize_images, :favorite_tags, :blacklisted_tags, :recent_tags, :enable_privacy_mode, :enable_post_navigation, :new_post_navigation_layout, :enable_sequential_post_navigation, :hide_deleted_posts, :per_page, :style_usernames, :enable_auto_complete, :custom_style, :show_deleted_children] end + def method_attributes + list = [:level_string] + if id == CurrentUser.user.id + list += [:remaining_api_hourly_limit] + end + list + end + def serializable_hash(options = {}) options ||= {} options[:except] ||= [] options[:except] += hidden_attributes options[:methods] ||= [] - options[:methods] += [:level_string] + options[:methods] += method_attributes super(options) end @@ -538,7 +550,7 @@ class User < ActiveRecord::Base options[:except] ||= [] options[:except] += hidden_attributes options[:methods] ||= [] - options[:methods] += [:level_string] + options[:methods] += method_attributes super(options, &block) end