diff --git a/TODO b/TODO index 71ec04c..5040631 100644 --- a/TODO +++ b/TODO @@ -12,7 +12,6 @@ Coding tasks * On up votes karma is transferred to posting user. * Automatically replace all the occurrences of Hacker in comments and news titles with H4k3r * Trottle by IP, using our INCR+TTL trick. -* Require a minimal delay between same user submissions. At least write a "please wait" message. * User profile * Complete the API. Missing: get news stream and a few more. * Handle "comment" news. diff --git a/app.rb b/app.rb index 584a979..d961319 100644 --- a/app.rb +++ b/app.rb @@ -388,6 +388,11 @@ if not check_api_secret return {:status => "err", :error => "Wrong form secret."}.to_json end + + if submitted_recently + return {:status => "err", :error => "You have submitted a story too recently, please wait #{allowed_to_post_in_seconds} seconds."}.to_json + end + # We can have an empty url or an empty first comment, but not both. if (!check_params "title","news_id",:url,:text) or (params[:url].length == 0 and @@ -681,6 +686,16 @@ def check_user_credentials(username,password) (user['password'] == hp) ? [user['auth'],user['apisecret']] : nil end +# Has the user submitted a news story in the last `NewsSubmissionBreak` seconds? +def submitted_recently + allowed_to_post_in_seconds > 0 +end + +# Indicates when the user is allowed to submit another story after the last. +def allowed_to_post_in_seconds + $r.ttl("user:#{$user['id']}:submitted_recently") +end + ################################################################################ # News ################################################################################ @@ -875,6 +890,8 @@ def insert_news(title,url,text,user_id) $r.zadd("news.top",rank,news_id) # Add the news url for some time to avoid reposts in short time $r.setex("url:"+url,PreventRepostTime,news_id) if !textpost + # Set a timeout indicating when the user may post again + $r.setex("user:#{$user['id']}:submitted_recently",NewsSubmissionBreak,'1') return news_id end diff --git a/app_config.rb b/app_config.rb index 562dc59..1d13ad3 100644 --- a/app_config.rb +++ b/app_config.rb @@ -29,3 +29,4 @@ NewsScoreLogBooster = 2 RankAgingFactor = 1.3 PreventRepostTime = 3600*48 +NewsSubmissionBreak = 60*15