Skip to content

Commit

Permalink
Require a minimal delay between same user submissions, default is 15 …
Browse files Browse the repository at this point in the history
…mins.
  • Loading branch information
koudelka committed Oct 25, 2011
1 parent 294263a commit 5a702b7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
1 change: 0 additions & 1 deletion TODO
Expand Up @@ -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.
Expand Down
17 changes: 17 additions & 0 deletions app.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
################################################################################
Expand Down Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions app_config.rb
Expand Up @@ -29,3 +29,4 @@
NewsScoreLogBooster = 2
RankAgingFactor = 1.3
PreventRepostTime = 3600*48
NewsSubmissionBreak = 60*15

0 comments on commit 5a702b7

Please sign in to comment.