appeals: raise appeal limits.

The old limit was one appeal per day. The new limit is based on your
upload limit. Each appeal costs 3 upload slots. If you have 15 upload
slots, then you can appeal up to 5 posts at once, but you won't be able
to appeal or upload more until your appeals are approved or rejected. If
you have unlimited uploads, then you have unlimited appeals.
This commit is contained in:
evazion
2020-08-08 10:37:51 -05:00
parent 3a17b5a13e
commit e8dcc9c56e
4 changed files with 48 additions and 31 deletions

View File

@@ -3,6 +3,7 @@ class UploadLimit
INITIAL_POINTS = 1000 INITIAL_POINTS = 1000
MAXIMUM_POINTS = 10_000 MAXIMUM_POINTS = 10_000
APPEAL_COST = 3
attr_reader :user attr_reader :user
@@ -31,10 +32,15 @@ class UploadLimit
end end
def used_upload_slots def used_upload_slots
pending = user.posts.pending pending_count = user.posts.pending.count
early_deleted = user.posts.deleted.where("created_at >= ?", 3.days.ago) appealed_count = user.post_appeals.pending.count
early_deleted_count = user.posts.deleted.where("created_at >= ?", 3.days.ago).count
pending.or(early_deleted).count pending_count + early_deleted_count + (appealed_count * APPEAL_COST)
end
def free_upload_slots
upload_slots - used_upload_slots
end end
def upload_slots def upload_slots
@@ -111,6 +117,4 @@ class UploadLimit
points_for_next_level(n - 1) points_for_next_level(n - 1)
end.sum end.sum
end end
memoize :used_upload_slots
end end

View File

@@ -1,8 +1,4 @@
class PostAppeal < ApplicationRecord class PostAppeal < ApplicationRecord
class Error < StandardError; end
MAX_APPEALS_PER_DAY = 1
belongs_to :creator, :class_name => "User" belongs_to :creator, :class_name => "User"
belongs_to :post belongs_to :post
@@ -33,19 +29,13 @@ class PostAppeal < ApplicationRecord
extend SearchMethods extend SearchMethods
def validate_creator_is_not_limited def validate_creator_is_not_limited
if appeal_count_for_creator >= MAX_APPEALS_PER_DAY errors[:creator] << "have reached your appeal limit" if creator.is_appeal_limited?
errors[:creator] << "can appeal at most #{MAX_APPEALS_PER_DAY} post a day"
end
end end
def validate_post_is_appealable def validate_post_is_appealable
errors[:post] << "cannot be appealed" if post.is_status_locked? || !post.is_appealable? errors[:post] << "cannot be appealed" if post.is_status_locked? || !post.is_appealable?
end end
def appeal_count_for_creator
creator.post_appeals.recent.count
end
def self.available_includes def self.available_includes
[:creator, :post] [:creator, :post]
end end

View File

@@ -349,6 +349,11 @@ class User < ApplicationRecord
end end
end end
def is_appeal_limited?
return false if can_upload_free?
upload_limit.free_upload_slots < UploadLimit::APPEAL_COST
end
def upload_limit def upload_limit
@upload_limit ||= UploadLimit.new(self) @upload_limit ||= UploadLimit.new(self)
end end

View File

@@ -3,37 +3,55 @@ require 'test_helper'
class PostAppealTest < ActiveSupport::TestCase class PostAppealTest < ActiveSupport::TestCase
context "In all cases" do context "In all cases" do
setup do setup do
@alice = create(:user) @user = create(:user, upload_points: 1000)
end end
context "a user" do context "a user" do
setup do setup do
@post = FactoryBot.create(:post, :tag_string => "aaa", :is_deleted => true) @post = create(:post, tag_string: "aaa", is_deleted: true)
end end
should "not be able to appeal a post more than twice" do should "not be able to appeal a post more than once" do
@post_appeal = create(:post_appeal, post: @post, creator: @alice) @post_appeal = create(:post_appeal, post: @post, creator: @user)
@post_appeal = build(:post_appeal, post: @post, creator: @alice) @post_appeal = build(:post_appeal, post: @post, creator: @user)
assert_equal(false, @post_appeal.valid?) assert_equal(false, @post_appeal.valid?)
assert_includes(@post_appeal.errors.full_messages, "You have already appealed this post") assert_includes(@post_appeal.errors.full_messages, "You have already appealed this post")
end end
should "not be able to appeal more than 1 post in 24 hours" do
@post_appeal = create(:post_appeal, post: @post, creator: @alice)
@post_appeal = build(:post_appeal, post: create(:post, is_deleted: true), creator: @alice)
assert_equal(false, @post_appeal.valid?)
assert_equal(["You can appeal at most 1 post a day"], @post_appeal.errors.full_messages)
end
should "not be able to appeal an active post" do should "not be able to appeal an active post" do
@post.update_attribute(:is_deleted, false) @post.update!(is_deleted: false)
@post_appeal = build(:post_appeal, post: @post, creator: @alice) @post_appeal = build(:post_appeal, post: @post, creator: @user)
assert_equal(false, @post_appeal.valid?) assert_equal(false, @post_appeal.valid?)
assert_equal(["Post cannot be appealed"], @post_appeal.errors.full_messages) assert_equal(["Post cannot be appealed"], @post_appeal.errors.full_messages)
end end
context "appeal limits" do
context "for members" do
should "not be able to appeal more than their upload limit" do
create_list(:post_appeal, 5, creator: @user)
assert_equal(15, @user.upload_limit.upload_slots)
assert_equal(15, @user.upload_limit.used_upload_slots)
@post_appeal = build(:post_appeal, creator: @user)
assert_equal(false, @post_appeal.valid?)
assert_equal(["have reached your appeal limit"], @post_appeal.errors[:creator])
end
end
context "for users with unrestricted uploads" do
should "should not have an appeal limit" do
@user = create(:user, can_upload_free: true)
create_list(:post_appeal, 10, creator: @user)
assert_equal(15, @user.upload_limit.upload_slots)
assert_equal(30, @user.upload_limit.used_upload_slots)
assert_equal(false, @user.is_appeal_limited?)
end
end
end
end end
end end
end end