def show
if current_user && current_user == @user_block.user
@user_block.needs_view = false
+ @user_block.deactivates_at = [@user_block.ends_at, Time.now.utc].max
@user_block.save!
end
end
:ends_at => now + @block_period.hours,
:needs_view => params[:user_block][:needs_view]
)
+ @user_block.deactivates_at = @user_block.ends_at unless @user_block.needs_view
if @user_block.save
flash[:notice] = t(".flash", :name => @user.display_name)
@user_block.reason = params[:user_block][:reason]
@user_block.needs_view = params[:user_block][:needs_view]
@user_block.ends_at = Time.now.utc + @block_period.hours
+ @user_block.deactivates_at = (@user_block.ends_at unless @user_block.needs_view)
if !user_block_was_active && @user_block.active?
flash.now[:error] = t(".inactive_block_cannot_be_reactivated")
render :action => "edit"
else
- @user_block.ends_at = @user_block.ends_at_was unless user_block_was_active
+ unless user_block_was_active
+ @user_block.ends_at = @user_block.ends_at_was
+ @user_block.deactivates_at = @user_block.deactivates_at_was
+ @user_block.deactivates_at = [@user_block.ends_at, @user_block.updated_at].max unless @user_block.deactivates_at # take updated_at into account before deactivates_at is backfilled
+ end
if @user_block.save
flash[:notice] = t(".success")
redirect_to @user_block
#
# Table name: user_blocks
#
-# id :integer not null, primary key
-# user_id :bigint(8) not null
-# creator_id :bigint(8) not null
-# reason :text not null
-# ends_at :datetime not null
-# needs_view :boolean default(FALSE), not null
-# revoker_id :bigint(8)
-# created_at :datetime
-# updated_at :datetime
-# reason_format :enum default("markdown"), not null
+# id :integer not null, primary key
+# user_id :bigint(8) not null
+# creator_id :bigint(8) not null
+# reason :text not null
+# ends_at :datetime not null
+# needs_view :boolean default(FALSE), not null
+# revoker_id :bigint(8)
+# created_at :datetime
+# updated_at :datetime
+# reason_format :enum default("markdown"), not null
+# deactivates_at :datetime
#
# Indexes
#
class UserBlock < ApplicationRecord
validate :moderator_permissions
validates :reason, :characters => true
+ validates :deactivates_at, :comparison => { :greater_than_or_equal_to => :ends_at }, :unless => -> { needs_view }
+ validates :deactivates_at, :absence => true, :if => -> { needs_view }
belongs_to :user, :class_name => "User"
belongs_to :creator, :class_name => "User"
def revoke!(revoker)
update(
:ends_at => Time.now.utc,
+ :deactivates_at => Time.now.utc,
:revoker_id => revoker.id,
:needs_view => false
)
assert_not_equal moderator_user, expired_block1.revoker
end
+ ##
+ # test changes to end/deactivation dates
+ def test_dates_when_viewed_before_end
+ blocked_user = create(:user)
+ moderator_user = create(:moderator_user)
+
+ freeze_time do
+ session_for(moderator_user)
+ assert_difference "UserBlock.count", 1 do
+ post user_blocks_path(:display_name => blocked_user.display_name,
+ :user_block_period => "48",
+ :user_block => { :needs_view => true, :reason => "Testing deactivates_at" })
+ end
+ block = UserBlock.last
+ assert_equal Time.now.utc + 2.days, block.ends_at
+ assert_nil block.deactivates_at
+
+ travel 1.day
+ session_for(blocked_user)
+ get user_block_path(block)
+ block.reload
+ assert_equal Time.now.utc + 1.day, block.ends_at
+ assert_equal Time.now.utc + 1.day, block.deactivates_at
+ end
+ end
+
+ def test_dates_when_viewed_after_end
+ blocked_user = create(:user)
+ moderator_user = create(:moderator_user)
+
+ freeze_time do
+ session_for(moderator_user)
+ assert_difference "UserBlock.count", 1 do
+ post user_blocks_path(:display_name => blocked_user.display_name,
+ :user_block_period => "24",
+ :user_block => { :needs_view => true, :reason => "Testing deactivates_at" })
+ end
+ block = UserBlock.last
+ assert_equal Time.now.utc + 1.day, block.ends_at
+ assert_nil block.deactivates_at
+
+ travel 2.days
+ session_for(blocked_user)
+ get user_block_path(block)
+ block.reload
+ assert_equal Time.now.utc - 1.day, block.ends_at
+ assert_equal Time.now.utc, block.deactivates_at
+ end
+ end
+
+ def test_dates_when_edited_before_end
+ blocked_user = create(:user)
+ moderator_user = create(:moderator_user)
+
+ freeze_time do
+ session_for(moderator_user)
+ assert_difference "UserBlock.count", 1 do
+ post user_blocks_path(:display_name => blocked_user.display_name,
+ :user_block_period => "48",
+ :user_block => { :needs_view => false, :reason => "Testing deactivates_at" })
+ end
+ block = UserBlock.last
+ assert_equal Time.now.utc + 2.days, block.ends_at
+ assert_equal Time.now.utc + 2.days, block.deactivates_at
+
+ travel 1.day
+ put user_block_path(block,
+ :user_block_period => "48",
+ :user_block => { :needs_view => false, :reason => "Testing deactivates_at updated" })
+ block.reload
+ assert_equal Time.now.utc + 2.days, block.ends_at
+ assert_equal Time.now.utc + 2.days, block.deactivates_at
+ end
+ end
+
+ def test_dates_when_edited_after_end
+ blocked_user = create(:user)
+ moderator_user = create(:moderator_user)
+
+ freeze_time do
+ session_for(moderator_user)
+ assert_difference "UserBlock.count", 1 do
+ post user_blocks_path(:display_name => blocked_user.display_name,
+ :user_block_period => "24",
+ :user_block => { :needs_view => false, :reason => "Testing deactivates_at" })
+ end
+ block = UserBlock.last
+ assert_equal Time.now.utc + 1.day, block.ends_at
+ assert_equal Time.now.utc + 1.day, block.deactivates_at
+
+ travel 2.days
+ put user_block_path(block,
+ :user_block_period => "0",
+ :user_block => { :needs_view => false, :reason => "Testing deactivates_at updated" })
+ block.reload
+ assert_equal Time.now.utc - 1.day, block.ends_at
+ assert_equal Time.now.utc - 1.day, block.deactivates_at
+ end
+ end
+
+ ##
+ # test updates on legacy records without correctly initialized deactivates_at
+ def test_update_legacy_deactivates_at
+ blocked_user = create(:user)
+ moderator_user = create(:moderator_user)
+
+ freeze_time do
+ block = UserBlock.new :user => blocked_user,
+ :creator => moderator_user,
+ :reason => "because",
+ :ends_at => Time.now.utc + 1.day,
+ :needs_view => false
+
+ assert_difference "UserBlock.count", 1 do
+ block.save :validate => false
+ end
+
+ travel 2.days
+ session_for(moderator_user)
+ put user_block_path(block,
+ :user_block_period => "0",
+ :user_block => { :needs_view => false, :reason => "Testing legacy block update" })
+ block.reload
+ assert_equal Time.now.utc - 1.day, block.ends_at
+ assert_equal Time.now.utc - 1.day, block.deactivates_at
+ end
+ end
+
##
# test the blocks_on action
def test_blocks_on